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 -----
25 // ----- Variables -----
27 uint32_t flash_ALLOW_BRICKABLE_ADDRESSES;
31 // ----- Functions -----
33 /* This will have to live in SRAM. */
34 __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call))
37 FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
44 // Wait for the operation to complete
45 struct FTFL_FSTAT_t stat;
46 while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
49 return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
50 //return (!!stat.mgstat0);
53 int flash_prepare_flashing()
55 /* switch to FlexRAM */
56 if ( !FTFL.fcnfg.ramrdy )
58 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
59 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
60 return (ftfl_submit_cmd());
65 int flash_read_1s_sector( uintptr_t addr, size_t num )
67 FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
68 FTFL.fccob.read_1s_section.addr = addr;
69 FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
70 FTFL.fccob.read_1s_section.num_words = num;
72 return ftfl_submit_cmd();
75 int flash_erase_sector( uintptr_t addr )
79 print("Erasing Sector: address(");
84 if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
86 FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
87 FTFL.fccob.erase.addr = addr;
89 return ftfl_submit_cmd();
92 int flash_program_section_longwords( uintptr_t addr, size_t num_words )
96 print("Programming Sector: address(");
98 print(") longwords(");
99 printHex( num_words );
103 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
104 FTFL.fccob.program_section.addr = addr;
105 FTFL.fccob.program_section.num_words = num_words;
107 return ftfl_submit_cmd();
110 int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
114 print("Programming Sector: address(");
117 printHex( num_phrases );
121 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
122 FTFL.fccob.program_section.addr = addr;
123 FTFL.fccob.program_section.num_words = num_phrases;
125 return ftfl_submit_cmd();
128 int flash_program_sector( uintptr_t addr, size_t len )
130 if ( len != USB_DFU_TRANSFER_SIZE )
133 #if defined(_mk20dx128vlf5_)
134 // Check if this is the beginning of a sector
135 // Only erase if necessary
136 if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
137 && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 4 )
138 && flash_erase_sector( addr ) )
142 return flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 );
143 #elif defined(_mk20dx256vlh7_)
144 // Check if beginning of sector and erase if not empty
145 // Each sector is 2 kB in length, but we can only write to half a sector at a time
146 // We can only erase an entire sector at a time
147 if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
148 && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 )
149 && flash_erase_sector( addr ) )
152 // Program half-sector
153 return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 );
157 int flash_prepare_reading()
162 int flash_read_sector( uintptr_t addr, size_t len )
167 void *flash_get_staging_area( uintptr_t addr, size_t len )
169 if ( (addr & (USB_DFU_TRANSFER_SIZE - 1)) != 0 || len != USB_DFU_TRANSFER_SIZE )