1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // ----- Local Includes -----
24 // ----- Variables -----
26 uint32_t flash_ALLOW_BRICKABLE_ADDRESSES;
30 // ----- Functions -----
32 /* This will have to live in SRAM. */
33 __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call))
36 FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
43 // Wait for the operation to complete
44 struct FTFL_FSTAT_t stat;
45 while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
48 return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
49 //return (!!stat.mgstat0);
52 int flash_prepare_flashing()
54 /* switch to FlexRAM */
55 if ( !FTFL.fcnfg.ramrdy )
57 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
58 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
59 return (ftfl_submit_cmd());
64 int flash_read_1s_sector( uintptr_t addr, size_t num )
66 FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
67 FTFL.fccob.read_1s_section.addr = addr;
68 FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
69 FTFL.fccob.read_1s_section.num_words = num;
71 return ftfl_submit_cmd();
74 int flash_erase_sector( uintptr_t addr )
76 if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
78 FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
79 FTFL.fccob.erase.addr = addr;
81 return ftfl_submit_cmd();
84 int flash_program_section_longwords( uintptr_t addr, size_t num_words )
86 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
87 FTFL.fccob.program_section.addr = addr;
88 FTFL.fccob.program_section.num_words = num_words;
90 return ftfl_submit_cmd();
93 int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
95 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
96 FTFL.fccob.program_section.addr = addr;
97 FTFL.fccob.program_section.num_words = num_phrases;
99 return ftfl_submit_cmd();
102 int flash_program_sector( uintptr_t addr, size_t len )
104 #if defined(_mk20dx128vlf5_)
105 return (len != FLASH_SECTOR_SIZE
106 || (addr & (FLASH_SECTOR_SIZE - 1)) != 0
107 || flash_erase_sector( addr )
108 || flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 ));
109 #elif defined(_mk20dx256vlh7_)
110 if ( len != FLASH_SECTOR_SIZE )
113 // Check if beginning of sector and erase if not empty
114 // Each sector is 2 kB in length, but we can only write to half a sector at a time
115 // We can only erase an entire sector at a time
116 if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
117 && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 )
118 && flash_erase_sector( addr ) )
121 // Program half-sector
122 return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 );
126 int flash_prepare_reading()
131 int flash_read_sector( uintptr_t addr, size_t len )
136 void *flash_get_staging_area( uintptr_t addr, size_t len )
138 if ( (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || len != FLASH_SECTOR_SIZE )