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 // ----- Includes -----
28 // ----- Variables -----
31 * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
33 static char staging[ USB_DFU_TRANSFER_SIZE ];
37 // ----- Functions -----
39 void sector_print( void* buf, size_t sector, size_t chunks )
41 uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE;
42 uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE;
45 // Verify if sector erased
46 FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
47 FTFL.fccob.read_1s_section.addr = (uintptr_t)start;
48 FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
49 FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
50 int retval = ftfl_submit_cmd();
56 printHex( (size_t)start );
58 printHex( (size_t)end );
64 for ( size_t line = 0; pos < end - 24; line++ )
67 printHex_op( (size_t)pos, 4 );
71 for ( size_t chunk = 0; chunk < chunks; chunk++ )
73 // Print out the two bytes (second one first)
74 printHex_op( *(pos + 1), 2 );
75 printHex_op( *pos, 2 );
84 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
86 // Calculate starting address from offset
87 *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
89 // Calculate length of transfer
91 *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
92 ? 0 : USB_DFU_TRANSFER_SIZE;
97 if ( *buf > (void*)&_app_rom_end )
98 return (DFU_STATUS_errADDRESS);
101 return (DFU_STATUS_OK);
104 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
108 if ( len > sizeof(staging) )
109 return (DFU_STATUS_errADDRESS);
111 // We only allow the last write to be less than one sector size.
114 if ( last && len != 0 )
115 return (DFU_STATUS_errADDRESS);
116 if ( len != FLASH_SECTOR_SIZE )
119 memset( staging, 0xff, sizeof(staging) );
123 return (DFU_STATUS_OK);
126 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
130 return (DFU_STATUS_OK);
132 target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
134 return (DFU_STATUS_errADDRESS);
135 memcpy( target, buf, len );
138 sector_print( target, 0, 16 );
140 // Depending on the error return a different status
141 switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
144 case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
145 case FTFL_FSTAT_ACCERR: // Flash Access Error
146 case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
147 return (DFU_STATUS_errADDRESS);
148 case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
149 return (DFU_STATUS_errADDRESS);
154 return (DFU_STATUS_OK);
159 static struct dfu_ctx dfu_ctx;
161 void init_usb_bootloader( int config )
163 dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
168 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
169 // XXX McHCK uses B16 instead of A19
171 // Enabling LED to indicate we are in the bootloader
172 GPIOA_PDDR |= (1<<19);
173 // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
174 PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
175 GPIOA_PSOR |= (1<<19);
177 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
178 // Enabling LED to indicate we are in the bootloader
179 GPIOA_PDDR |= (1<<5);
180 // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
181 PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
182 GPIOA_PSOR |= (1<<5);
184 #error "Incompatible chip for bootloader"
188 printNL( NL "Bootloader DFU-Mode" );
192 GPIOB_PDDR |= (1<<16);
193 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
194 GPIOB_PSOR |= (1<<16);
197 GPIOC_PDDR |= (1<<8);
198 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
199 GPIOC_PSOR |= (1<<8);
202 GPIOC_PDDR |= (1<<4);
203 PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
204 GPIOC_PCOR |= (1<<4);
208 GPIOC_PDDR |= (1<<1);
209 PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
210 GPIOC_PCOR |= (1<<1);
211 GPIOC_PDDR |= (1<<2);
212 PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
213 GPIOC_PCOR |= (1<<2);
214 GPIOC_PDDR |= (1<<3);
215 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
216 GPIOC_PCOR |= (1<<3);
220 // Read Firmware 1 Status
221 FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
222 FTFL.fccob.read_1s_block.addr = (uintptr_t)&_app_rom;
223 FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
225 int retval = ftfl_submit_cmd();
226 print("Firmware Erase Status: ");
231 // Read Bootloader 1 Status
232 FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
233 FTFL.fccob.read_1s_block.addr = (uintptr_t)&_bootloader;
234 FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
236 retval = ftfl_submit_cmd();
237 print("Bootloader Erase Status: ");
243 // Program First Longword of firmware
244 FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
245 FTFL.fccob.program_longword.addr = (uintptr_t)&_app_rom;
246 FTFL.fccob.program_longword.data_be[0] = 0x1;
247 FTFL.fccob.program_longword.data_be[1] = 0x2;
248 FTFL.fccob.program_longword.data_be[2] = 0x4;
249 FTFL.fccob.program_longword.data_be[3] = 0x8;
250 int retval = ftfl_submit_cmd();
251 print("Write Longword Status: ");
258 FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
259 FTFL.fccob.erase.addr = (uintptr_t)&_app_rom;
260 int retval = ftfl_submit_cmd();
261 print("Erase Status: ");
266 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
267 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
268 retval = ftfl_submit_cmd();
269 print("Set FlexRAM Status: ");
274 memset( FlexRAM, 0xB4, 1000 );
275 memset( &FlexRAM[1000], 0xE3, 1000 );
278 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
279 FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom;
280 FTFL.fccob.program_section.num_words = 128;
281 //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
282 retval = ftfl_submit_cmd();
283 print("Program Sector1 Status: ");
287 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
288 FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom + 0x400;
289 FTFL.fccob.program_section.num_words = 128;
290 //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
291 retval = ftfl_submit_cmd();
292 print("Program Sector2 Status: ");
296 for ( uint8_t sector = 0; sector < 1; sector++ )
297 //sector_print( &_bootloader, sector, 16 );
298 sector_print( &_app_rom, sector, 16 );
302 flash_prepare_flashing();
304 //uint32_t *position = &_app_rom;
305 usb_init( &dfu_device );