X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Bootloader%2Fflash.c;h=d04cf382947d321ff550fa846d86b65c622f5af0;hb=b9fa9c8672a4d1fd89c0c11a358536dfe3ec28a1;hp=2a531174d63fb7b0754357615b376a192e0786c6;hpb=96e785b571a231265d64fbe5083371480cd3a549;p=kiibohd-controller.git diff --git a/Bootloader/flash.c b/Bootloader/flash.c index 2a53117..d04cf38 100644 --- a/Bootloader/flash.c +++ b/Bootloader/flash.c @@ -1,5 +1,5 @@ /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>. - * Modifications by Jacob Alexander 2014 + * Modifications by Jacob Alexander 2014-2015 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,24 +31,29 @@ uint32_t flash_ALLOW_BRICKABLE_ADDRESSES; /* This will have to live in SRAM. */ __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call)) -int ftfl_submit_cmd(void) +int ftfl_submit_cmd() { FTFL.fstat.raw = ((struct FTFL_FSTAT_t){ .ccif = 1, - .rdcolerr = 1, - .accerr = 1, - .fpviol = 1 - }).raw; + //.rdcolerr = 1, + .accerr = 1, + .fpviol = 1 + }).raw; + + // Wait for the operation to complete struct FTFL_FSTAT_t stat; - while (!(stat = FTFL.fstat).ccif) - /* NOTHING */; /* XXX maybe WFI? */ - return (!!stat.mgstat0); + while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI? + + // Mask error bits + return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0); + //return (!!stat.mgstat0); } -int flash_prepare_flashing(void) +int flash_prepare_flashing() { /* switch to FlexRAM */ - if (!FTFL.fcnfg.ramrdy) { + if ( !FTFL.fcnfg.ramrdy ) + { FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM; FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM; return (ftfl_submit_cmd()); @@ -56,36 +61,86 @@ int flash_prepare_flashing(void) return (0); } -int flash_erase_sector(uintptr_t addr) +int flash_read_1s_sector( uintptr_t addr, size_t num ) +{ + FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION; + FTFL.fccob.read_1s_section.addr = addr; + FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL; + FTFL.fccob.read_1s_section.num_words = num; + + return ftfl_submit_cmd(); +} + +int flash_erase_sector( uintptr_t addr ) { - if (addr < (uintptr_t)&_app_rom && - flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420) + if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 ) return (-1); FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR; FTFL.fccob.erase.addr = addr; - return (ftfl_submit_cmd()); + + return ftfl_submit_cmd(); } -int flash_program_section(uintptr_t addr, size_t num_words) +int flash_program_section_longwords( uintptr_t addr, size_t num_words ) { FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION; FTFL.fccob.program_section.addr = addr; FTFL.fccob.program_section.num_words = num_words; - return (ftfl_submit_cmd()); + + return ftfl_submit_cmd(); +} + +int flash_program_section_phrases( uintptr_t addr, size_t num_phrases ) +{ + FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION; + FTFL.fccob.program_section.addr = addr; + FTFL.fccob.program_section.num_words = num_phrases; + + return ftfl_submit_cmd(); +} + +int flash_program_sector( uintptr_t addr, size_t len ) +{ + if ( len != FLASH_SECTOR_SIZE ) + return 1; + +#if defined(_mk20dx128vlf5_) + // Check if this is the beginning of a sector + // Only erase if necessary + if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0 + && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 4 ) + && flash_erase_sector( addr ) ) + return 1; + + // Program sector + return flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 ); +#elif defined(_mk20dx256vlh7_) + // Check if beginning of sector and erase if not empty + // Each sector is 2 kB in length, but we can only write to half a sector at a time + // We can only erase an entire sector at a time + if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0 + && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 ) + && flash_erase_sector( addr ) ) + return 1; + + // Program half-sector + return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 ); +#endif } -int flash_program_sector(uintptr_t addr, size_t len) +int flash_prepare_reading() { - return (len != FLASH_SECTOR_SIZE || - (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || - flash_erase_sector(addr) || - flash_program_section(addr, FLASH_SECTOR_SIZE/4)); + return (0); +} + +int flash_read_sector( uintptr_t addr, size_t len ) +{ + return (0); } -void *flash_get_staging_area(uintptr_t addr, size_t len) +void *flash_get_staging_area( uintptr_t addr, size_t len ) { - if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 || - len != FLASH_SECTOR_SIZE) + if ( (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || len != FLASH_SECTOR_SIZE ) return (NULL); return (FlexRAM); }