]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/flash.c
Basic code for mk20dx256vlh7 flashing
[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()
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()
53 {
54         /* switch to FlexRAM */
55         if ( !FTFL.fcnfg.ramrdy )
56         {
57                 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
58                 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
59                 return (ftfl_submit_cmd());
60         }
61         return (0);
62 }
63
64 int flash_read_1s_sector( uintptr_t addr, size_t num )
65 {
66         FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
67         FTFL.fccob.read_1s_section.addr = addr;
68         FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
69         FTFL.fccob.read_1s_section.num_words = num;
70
71         return ftfl_submit_cmd();
72 }
73
74 int flash_erase_sector( uintptr_t addr )
75 {
76         if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
77                 return (-1);
78         FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
79         FTFL.fccob.erase.addr = addr;
80
81         return ftfl_submit_cmd();
82 }
83
84 int flash_program_section_longwords( uintptr_t addr, size_t num_words )
85 {
86         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
87         FTFL.fccob.program_section.addr = addr;
88         FTFL.fccob.program_section.num_words = num_words;
89
90         return ftfl_submit_cmd();
91 }
92
93 int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
94 {
95         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
96         FTFL.fccob.program_section.addr = addr;
97         FTFL.fccob.program_section.num_words = num_phrases;
98
99         return ftfl_submit_cmd();
100 }
101
102 int flash_program_sector( uintptr_t addr, size_t len )
103 {
104 #if defined(_mk20dx128vlf5_)
105         return (len != FLASH_SECTOR_SIZE
106                 || (addr & (FLASH_SECTOR_SIZE - 1)) != 0
107                 || flash_erase_sector( addr )
108                 || flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 ));
109 #elif defined(_mk20dx256vlh7_)
110         if ( len != FLASH_SECTOR_SIZE )
111                 return 1;
112
113         // Check if beginning of sector and erase if not empty
114         // Each sector is 2 kB in length, but we can only write to half a sector at a time
115         // We can only erase an entire sector at a time
116         if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
117                 && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 )
118                 && flash_erase_sector( addr ) )
119                         return 1;
120
121         // Program half-sector
122         return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 );
123 #endif
124 }
125
126 int flash_prepare_reading()
127 {
128         return (0);
129 }
130
131 int flash_read_sector( uintptr_t addr, size_t len )
132 {
133         return (0);
134 }
135
136 void *flash_get_staging_area( uintptr_t addr, size_t len )
137 {
138         if ( (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || len != FLASH_SECTOR_SIZE )
139                 return (NULL);
140         return (FlexRAM);
141 }
142