]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/flash.c
Some initial bringup of the dfu bootloader on the mk20dx256vlh7
[kiibohd-controller.git] / Bootloader / flash.c
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014-2015 <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
43         // Wait for the operation to complete
44         struct FTFL_FSTAT_t stat;
45         while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
46
47         // Mask error bits
48         return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
49         //return (!!stat.mgstat0);
50 }
51
52 int flash_prepare_flashing(void)
53 {
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());
59         }
60         return (0);
61 }
62
63 int flash_erase_sector(uintptr_t addr)
64 {
65         if (addr < (uintptr_t)&_app_rom &&
66                 flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420)
67                 return (-1);
68         FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
69         FTFL.fccob.erase.addr = addr;
70         return (ftfl_submit_cmd());
71 }
72
73 int flash_program_section_longwords(uintptr_t addr, size_t num_words)
74 {
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;
78
79         return ftfl_submit_cmd();
80 }
81
82 int flash_program_section_phrases(uintptr_t addr, size_t num_phrases)
83 {
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;
87
88         return ftfl_submit_cmd();
89 }
90
91 int flash_program_longword(uintptr_t addr, uint8_t *data)
92 {
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];
99
100         return ftfl_submit_cmd();
101 }
102
103 int flash_program_sector(uintptr_t addr, size_t len)
104 {
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_)
111         /*
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));
116         */
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));
121 #endif
122 }
123
124 int flash_prepare_reading(void)
125 {
126         return (0);
127 }
128
129 int flash_read_sector(uintptr_t addr, size_t len)
130 {
131         return (0);
132 }
133
134 void *flash_get_staging_area(uintptr_t addr, size_t len)
135 {
136         if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
137             len != FLASH_SECTOR_SIZE)
138                 return (NULL);
139         return (FlexRAM);
140 }
141