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 int 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();
57 printHex( (size_t)start );
59 printHex( (size_t)end );
66 for ( size_t line = 0; pos < end - 24; line++ )
69 printHex_op( (size_t)pos, 4 );
73 for ( size_t chunk = 0; chunk < chunks; chunk++ )
75 // Print out the two bytes (second one first)
76 printHex_op( *(pos + 1), 2 );
77 printHex_op( *pos, 2 );
88 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
90 // Calculate starting address from offset
91 *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
93 // Calculate length of transfer
95 *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
96 ? 0 : USB_DFU_TRANSFER_SIZE;
101 if ( *buf > (void*)&_app_rom_end )
102 return (DFU_STATUS_errADDRESS);
105 return (DFU_STATUS_OK);
108 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
114 print("Setup Write: offset(");
123 if ( len > sizeof(staging) )
124 return (DFU_STATUS_errADDRESS);
126 // We only allow the last write to be less than one sector size.
129 if ( last && len != 0 )
130 return (DFU_STATUS_errADDRESS);
131 if ( len != USB_DFU_TRANSFER_SIZE )
134 memset( staging, 0xff, sizeof(staging) );
138 return (DFU_STATUS_OK);
141 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
145 return (DFU_STATUS_OK);
147 target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
149 return (DFU_STATUS_errADDRESS);
150 memcpy( target, buf, len );
152 // Depending on the error return a different status
153 switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
156 case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
157 case FTFL_FSTAT_ACCERR: // Flash Access Error
158 case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
159 return (DFU_STATUS_errADDRESS);
160 case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
161 return (DFU_STATUS_errADDRESS);
166 return (DFU_STATUS_OK);
171 static struct dfu_ctx dfu_ctx;
173 void init_usb_bootloader( int config )
175 dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
180 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
181 // XXX McHCK uses B16 instead of A19
183 // Enabling LED to indicate we are in the bootloader
184 GPIOA_PDDR |= (1<<19);
185 // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
186 PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
187 GPIOA_PSOR |= (1<<19);
189 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
190 // Enabling LED to indicate we are in the bootloader
191 GPIOA_PDDR |= (1<<5);
192 // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
193 PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
194 GPIOA_PSOR |= (1<<5);
196 #error "Incompatible chip for bootloader"
200 printNL( NL "Bootloader DFU-Mode" );
202 // Bootloader Enter Reasons
203 print(" RCM_SRS0 - ");
204 printHex( RCM_SRS0 & 0x60 );
205 print( NL " RCM_SRS1 - ");
206 printHex( RCM_SRS1 & 0x02 );
207 print( NL " _app_rom - ");
208 printHex( (uint32_t)_app_rom );
209 print( NL " Soft Rst - " );
210 printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
215 GPIOB_PDDR |= (1<<16);
216 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
217 GPIOB_PSOR |= (1<<16);
220 GPIOC_PDDR |= (1<<8);
221 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
222 GPIOC_PSOR |= (1<<8);
225 GPIOC_PDDR |= (1<<4);
226 PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
227 GPIOC_PCOR |= (1<<4);
231 GPIOC_PDDR |= (1<<1);
232 PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
233 GPIOC_PCOR |= (1<<1);
234 GPIOC_PDDR |= (1<<2);
235 PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
236 GPIOC_PCOR |= (1<<2);
237 GPIOC_PDDR |= (1<<3);
238 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
239 GPIOC_PCOR |= (1<<3);
243 for ( uint8_t sector = 0; sector < 3; sector++ )
244 sector_print( &_app_rom, sector, 16 );
248 flash_prepare_flashing();
249 usb_init( &dfu_device );