]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/flash.c
Working mk20dx256vlh7 usb flash support
[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 #include "debug.h"
22
23
24
25 // ----- Variables -----
26
27 uint32_t flash_ALLOW_BRICKABLE_ADDRESSES;
28
29
30
31 // ----- Functions -----
32
33 /* This will have to live in SRAM. */
34 __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call))
35 int ftfl_submit_cmd()
36 {
37         FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
38                         .ccif = 1,
39                         //.rdcolerr = 1,
40                         .accerr = 1,
41                         .fpviol = 1
42                 }).raw;
43
44         // Wait for the operation to complete
45         struct FTFL_FSTAT_t stat;
46         while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
47
48         // Mask error bits
49         return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
50         //return (!!stat.mgstat0);
51 }
52
53 int flash_prepare_flashing()
54 {
55         /* switch to FlexRAM */
56         if ( !FTFL.fcnfg.ramrdy )
57         {
58                 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
59                 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
60                 return (ftfl_submit_cmd());
61         }
62         return (0);
63 }
64
65 int flash_read_1s_sector( uintptr_t addr, size_t num )
66 {
67         FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
68         FTFL.fccob.read_1s_section.addr = addr;
69         FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
70         FTFL.fccob.read_1s_section.num_words = num;
71
72         return ftfl_submit_cmd();
73 }
74
75 int flash_erase_sector( uintptr_t addr )
76 {
77 #ifdef FLASH_DEBUG
78         // Debug
79         print("Erasing Sector: address(");
80         printHex( addr );
81         printNL(")");
82 #endif
83
84         if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
85                 return (-1);
86         FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
87         FTFL.fccob.erase.addr = addr;
88
89         return ftfl_submit_cmd();
90 }
91
92 int flash_program_section_longwords( uintptr_t addr, size_t num_words )
93 {
94 #ifdef FLASH_DEBUG
95         // Debug
96         print("Programming Sector: address(");
97         printHex( addr );
98         print(") longwords(");
99         printHex( num_words );
100         printNL(")");
101 #endif
102
103         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
104         FTFL.fccob.program_section.addr = addr;
105         FTFL.fccob.program_section.num_words = num_words;
106
107         return ftfl_submit_cmd();
108 }
109
110 int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
111 {
112 #ifdef FLASH_DEBUG
113         // Debug
114         print("Programming Sector: address(");
115         printHex( addr );
116         print(") phrases(");
117         printHex( num_phrases );
118         printNL(")");
119 #endif
120
121         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
122         FTFL.fccob.program_section.addr = addr;
123         FTFL.fccob.program_section.num_words = num_phrases;
124
125         return ftfl_submit_cmd();
126 }
127
128 int flash_program_sector( uintptr_t addr, size_t len )
129 {
130         if ( len != USB_DFU_TRANSFER_SIZE )
131                 return 1;
132
133 #if defined(_mk20dx128vlf5_)
134         // Check if this is the beginning of a sector
135         // Only erase if necessary
136         if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
137                 && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 4 )
138                 && flash_erase_sector( addr ) )
139                         return 1;
140
141         // Program sector
142         return flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 );
143 #elif defined(_mk20dx256vlh7_)
144         // Check if beginning of sector and erase if not empty
145         // Each sector is 2 kB in length, but we can only write to half a sector at a time
146         // We can only erase an entire sector at a time
147         if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
148                 && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 )
149                 && flash_erase_sector( addr ) )
150                         return 1;
151
152         // Program half-sector
153         return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 );
154 #endif
155 }
156
157 int flash_prepare_reading()
158 {
159         return (0);
160 }
161
162 int flash_read_sector( uintptr_t addr, size_t len )
163 {
164         return (0);
165 }
166
167 void *flash_get_staging_area( uintptr_t addr, size_t len )
168 {
169         if ( (addr & (USB_DFU_TRANSFER_SIZE - 1)) != 0 || len != USB_DFU_TRANSFER_SIZE )
170                 return (NULL);
171         return (FlexRAM);
172 }
173