1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014 <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))
34 int ftfl_submit_cmd(void)
36 FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
42 struct FTFL_FSTAT_t stat;
43 while (!(stat = FTFL.fstat).ccif)
44 /* NOTHING */; /* XXX maybe WFI? */
45 return (!!stat.mgstat0);
48 int flash_prepare_flashing(void)
50 /* switch to FlexRAM */
51 if (!FTFL.fcnfg.ramrdy) {
52 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
53 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
54 return (ftfl_submit_cmd());
59 int flash_erase_sector(uintptr_t addr)
61 if (addr < (uintptr_t)&_app_rom &&
62 flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420)
64 FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
65 FTFL.fccob.erase.addr = addr;
66 return (ftfl_submit_cmd());
69 int flash_program_section(uintptr_t addr, size_t num_words)
71 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
72 FTFL.fccob.program_section.addr = addr;
73 FTFL.fccob.program_section.num_words = num_words;
74 return (ftfl_submit_cmd());
77 int flash_program_sector(uintptr_t addr, size_t len)
79 return (len != FLASH_SECTOR_SIZE ||
80 (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
81 flash_erase_sector(addr) ||
82 flash_program_section(addr, FLASH_SECTOR_SIZE/4));
85 void *flash_get_staging_area(uintptr_t addr, size_t len)
87 if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
88 len != FLASH_SECTOR_SIZE)