]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/flash.c
Merge remote-tracking branch 'upstream/master'
[kiibohd-controller.git] / Bootloader / flash.c
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014 <haata@kiibohd.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 // ----- Local Includes -----
19
20 #include "mchck.h"
21
22
23
24 // ----- Variables -----
25
26 uint32_t flash_ALLOW_BRICKABLE_ADDRESSES;
27
28
29
30 // ----- Functions -----
31
32 /* This will have to live in SRAM. */
33 __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call))
34 int ftfl_submit_cmd(void)
35 {
36         FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
37                         .ccif = 1,
38                                 .rdcolerr = 1,
39                                 .accerr = 1,
40                                 .fpviol = 1
41                                 }).raw;
42         struct FTFL_FSTAT_t stat;
43         while (!(stat = FTFL.fstat).ccif)
44                 /* NOTHING */; /* XXX maybe WFI? */
45         return (!!stat.mgstat0);
46 }
47
48 int flash_prepare_flashing(void)
49 {
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());
55         }
56         return (0);
57 }
58
59 int flash_erase_sector(uintptr_t addr)
60 {
61         if (addr < (uintptr_t)&_app_rom &&
62                 flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420)
63                 return (-1);
64         FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
65         FTFL.fccob.erase.addr = addr;
66         return (ftfl_submit_cmd());
67 }
68
69 int flash_program_section(uintptr_t addr, size_t num_words)
70 {
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());
75 }
76
77 int flash_program_sector(uintptr_t addr, size_t len)
78 {
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));
83 }
84
85 void *flash_get_staging_area(uintptr_t addr, size_t len)
86 {
87         if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
88             len != FLASH_SECTOR_SIZE)
89                 return (NULL);
90         return (FlexRAM);
91 }
92