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 -----
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){
43 // Wait for the operation to complete
44 struct FTFL_FSTAT_t stat;
45 while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
48 return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
49 //return (!!stat.mgstat0);
52 int flash_prepare_flashing(void)
54 /* switch to FlexRAM */
55 if (!FTFL.fcnfg.ramrdy) {
56 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
57 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
58 return (ftfl_submit_cmd());
63 int flash_erase_sector(uintptr_t addr)
65 if (addr < (uintptr_t)&_app_rom &&
66 flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420)
68 FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
69 FTFL.fccob.erase.addr = addr;
70 return (ftfl_submit_cmd());
73 int flash_program_section_longwords(uintptr_t addr, size_t num_words)
75 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
76 FTFL.fccob.program_section.addr = addr;
77 FTFL.fccob.program_section.num_words = num_words;
79 return ftfl_submit_cmd();
82 int flash_program_section_phrases(uintptr_t addr, size_t num_phrases)
84 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
85 FTFL.fccob.program_section.addr = addr;
86 FTFL.fccob.program_section.num_words = num_phrases;
88 return ftfl_submit_cmd();
91 int flash_program_longword(uintptr_t addr, uint8_t *data)
93 FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
94 FTFL.fccob.program_longword.addr = addr;
95 FTFL.fccob.program_longword.data_be[0] = data[0];
96 FTFL.fccob.program_longword.data_be[1] = data[1];
97 FTFL.fccob.program_longword.data_be[2] = data[2];
98 FTFL.fccob.program_longword.data_be[3] = data[3];
100 return ftfl_submit_cmd();
103 int flash_program_sector(uintptr_t addr, size_t len)
105 #if defined(_mk20dx128vlf5_)
106 return (len != FLASH_SECTOR_SIZE ||
107 (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
108 flash_erase_sector(addr) ||
109 flash_program_section_longwords(addr, FLASH_SECTOR_SIZE / 4));
110 #elif defined(_mk20dx256vlh7_)
112 return (len != FLASH_SECTOR_SIZE ||
113 (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
114 flash_erase_sector(addr) ||
115 flash_program_section_phrases(addr, FLASH_SECTOR_SIZE / 8));
117 return (len != FLASH_SECTOR_SIZE ||
118 (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
119 flash_erase_sector(addr) ||
120 flash_program_section_phrases(addr, FLASH_SECTOR_SIZE / 8));
124 int flash_prepare_reading(void)
129 int flash_read_sector(uintptr_t addr, size_t len)
134 void *flash_get_staging_area(uintptr_t addr, size_t len)
136 if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
137 len != FLASH_SECTOR_SIZE)