]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/ftfl.h
Code cleanup
[kiibohd-controller.git] / Bootloader / ftfl.h
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 #pragma once
19
20 // ----- Local Includes -----
21
22 #include "mchck-cdefs.h"
23
24
25
26 // ----- Structs -----
27
28 struct FTFL_FSTAT_t {
29         UNION_STRUCT_START(8);
30         uint8_t mgstat0 : 1;
31         uint8_t _rsvd0 : 3;
32         uint8_t fpviol : 1;
33         uint8_t accerr : 1;
34         uint8_t rdcolerr : 1;
35         uint8_t ccif : 1;
36         UNION_STRUCT_END;
37 };
38 CTASSERT_SIZE_BIT(struct FTFL_FSTAT_t, 8);
39
40 struct FTFL_FCNFG_t {
41         UNION_STRUCT_START(8);
42         uint8_t eeerdy : 1;
43         uint8_t ramrdy : 1;
44         uint8_t pflsh : 1;
45         uint8_t _rsvd0 : 1;
46         uint8_t erssusp : 1;
47         uint8_t ersareq : 1;
48         uint8_t rdcollie : 1;
49         uint8_t ccie : 1;
50         UNION_STRUCT_END;
51 };
52 CTASSERT_SIZE_BIT(struct FTFL_FCNFG_t, 8);
53
54 struct FTFL_FSEC_t {
55         UNION_STRUCT_START(8);
56         enum {
57                 FTFL_FSEC_SEC_UNSECURE = 2,
58                 FTFL_FSEC_SEC_SECURE = 3
59         } sec : 2;
60         enum {
61                 FTFL_FSEC_FSLACC_DENY = 1,
62                 FTFL_FSEC_FSLACC_GRANT = 3
63         } fslacc : 2;
64         enum {
65                 FTFL_FSEC_MEEN_DISABLE = 2,
66                 FTFL_FSEC_MEEN_ENABLE = 3
67         } meen : 2;
68         enum {
69                 FTFL_FSEC_KEYEN_DISABLE = 1,
70                 FTFL_FSEC_KEYEN_ENABLE = 2
71         } keyen : 2;
72         UNION_STRUCT_END;
73 };
74 CTASSERT_SIZE_BIT(struct FTFL_FSEC_t, 8);
75
76 struct FTFL_FOPT_t {
77         UNION_STRUCT_START(8);
78         uint8_t lpboot : 1;
79         uint8_t ezport_dis : 1;
80         uint8_t nmi_dis : 1;
81         uint8_t _rsvd0 : 5;
82         UNION_STRUCT_END;
83 };
84 CTASSERT_SIZE_BIT(struct FTFL_FOPT_t, 8);
85
86 /**
87  * The FCOOB is a weird register file, because it is double big endian,
88  * which makes for odd gaps and for some data that is big endian, and for
89  * some that is little endian.
90  */
91 union FTFL_FCCOB_t {
92         struct ftfl_generic {
93                 uint32_t addr : 24;
94                 enum FTFL_FCMD {
95                         FTFL_FCMD_READ_1s_BLOCK = 0x00,
96                         FTFL_FCMD_READ_1s_SECTION = 0x01,
97                         FTFL_FCMD_PROGRAM_CHECK = 0x02,
98                         FTFL_FCMD_READ_RESOURCE = 0x03,
99                         FTFL_FCMD_PROGRAM_LONGWORD = 0x06,
100                         FTFL_FCMD_ERASE_BLOCK = 0x08,
101                         FTFL_FCMD_ERASE_SECTOR = 0x09,
102                         FTFL_FCMD_PROGRAM_SECTION = 0x0b,
103                         FTFL_FCMD_READ_1s_ALL_BLOCKS = 0x40,
104                         FTFL_FCMD_READ_ONCE = 0x41,
105                         FTFL_FCMD_PROGRAM_ONCE = 0x43,
106                         FTFL_FCMD_ERASE_ALL_BLOCKS = 0x44,
107                         FTFL_FCMD_VERIFY_KEY = 0x45,
108                         FTFL_FCMD_PROGRAM_PARTITION = 0x80,
109                         FTFL_FCMD_SET_FLEXRAM = 0x81
110                 } fcmd : 8;
111                 uint8_t data_be[8];
112         } generic;
113         struct {
114                 uint32_t addr : 24;
115                 enum FTFL_FCMD fcmd : 8;
116                 uint8_t _rsvd0[3];
117                 enum FTFL_MARGIN_CHOICE {
118                         FTFL_MARGIN_NORMAL = 0x00,
119                         FTFL_MARGIN_USER = 0x01,
120                         FTFL_MARGIN_FACTORY = 0x02
121                 } margin : 8;
122         } read_1s_block;
123         struct ftfl_data_num_words {
124                 uint32_t addr : 24;
125                 enum FTFL_FCMD fcmd : 8;
126                 uint8_t _rsvd0;
127                 enum FTFL_MARGIN_CHOICE margin : 8;
128                 uint16_t num_words;
129         } read_1s_section;
130         struct {
131                 uint32_t addr : 24;
132                 enum FTFL_FCMD fcmd : 8;
133                 uint8_t _rsvd0[3];
134                 enum FTFL_MARGIN_CHOICE margin : 8;
135                 uint8_t data_be[4];
136         } program_check;
137         struct {
138                 uint32_t addr : 24;
139                 enum FTFL_FCMD fcmd : 8;
140                 uint32_t data;
141                 uint8_t _rsvd0[3];
142                 enum FTFL_RESOURCE_SELECT {
143                         FTFL_RESOURCE_IFR = 0x00,
144                         FTFL_RESOURCE_VERSION = 0x01
145                 } resource_select : 8;
146         } read_resource;
147         struct {
148                 uint32_t addr : 24;
149                 enum FTFL_FCMD fcmd : 8;
150                 uint8_t data_be[4];
151         } program_longword;
152         struct {
153                 uint32_t addr : 24;
154                 enum FTFL_FCMD fcmd : 8;
155         } erase;
156         struct ftfl_data_num_words program_section;
157         struct {
158                 uint8_t _rsvd0[2];
159                 enum FTFL_MARGIN_CHOICE margin : 8;
160                 enum FTFL_FCMD fcmd : 8;
161         } read_1s_all_blocks;
162         struct ftfl_cmd_once {
163                 uint8_t _rsvd0[2];
164                 uint8_t idx;
165                 enum FTFL_FCMD fcmd : 8;
166                 uint8_t data_be[4];
167         } read_once;
168         struct ftfl_cmd_once program_once;
169         struct {
170                 uint8_t _rsvd0[3];
171                 enum FTFL_FCMD fcmd : 8;
172         } erase_all;
173         struct {
174                 uint8_t _rsvd0[3];
175                 enum FTFL_FCMD fcmd : 8;
176                 uint8_t key_be[8];
177         } verify_key;
178         struct {
179                 uint8_t _rsvd0[3];
180                 enum FTFL_FCMD fcmd : 8;
181                 uint8_t _rsvd1[2];
182
183                 /* the following enum is analogous to enum
184                  * SIM_FLEXNVM_PARTITION in sim.h, but this one is padded
185                  * with four 1-bits to make an 8-bit value.
186                  */
187
188                 enum FTFL_FLEXNVM_PARTITION {
189                         FTFL_FLEXNVM_DATA_32_EEPROM_0 = 0xF0,
190                         FTFL_FLEXNVM_DATA_24_EEPROM_8 = 0xF1,
191                         FTFL_FLEXNVM_DATA_16_EEPROM_16 = 0xF2,
192                         FTFL_FLEXNVM_DATA_8_EEPROM_24 = 0xF9,
193                         FTFL_FLEXNVM_DATA_0_EEPROM_32 = 0xF3
194                 } flexnvm_partition : 8;
195                 enum FTFL_EEPROM_SIZE {
196                         FTFL_EEPROM_SIZE_0 = 0x3f,
197                         FTFL_EEPROM_SIZE_32 = 0x39,
198                         FTFL_EEPROM_SIZE_64 = 0x38,
199                         FTFL_EEPROM_SIZE_128 = 0x37,
200                         FTFL_EEPROM_SIZE_256 = 0x36,
201                         FTFL_EEPROM_SIZE_512 = 0x35,
202                         FTFL_EEPROM_SIZE_1024 = 0x34,
203                         FTFL_EEPROM_SIZE_2048 = 0x33
204                 } eeprom_size : 8;
205         } program_partition;
206         struct {
207                 uint8_t _rsvd0[2];
208                 enum FTFL_FLEXRAM_FUNCTION {
209                         FTFL_FLEXRAM_EEPROM = 0x00,
210                         FTFL_FLEXRAM_RAM = 0xff
211                 } flexram_function : 8;
212                 enum FTFL_FCMD fcmd : 8;
213         } set_flexram;
214 };
215 CTASSERT_SIZE_BYTE(union FTFL_FCCOB_t, 12);
216
217 struct FTFL_t {
218         struct FTFL_FSTAT_t fstat;
219         struct FTFL_FCNFG_t fcnfg;
220         struct FTFL_FSEC_t fsec;
221         struct FTFL_FOPT_t fopt;
222         union FTFL_FCCOB_t fccob;
223         uint8_t fprot_be[4];
224         uint8_t feprot;
225         uint8_t fdprot;
226 };
227 CTASSERT_SIZE_BYTE(struct FTFL_t, 0x18);
228
229 /* Flash Configuration Field, see Sub-Family Reference Manual, section 28.3.1 */
230 struct FTFL_CONFIG_t {
231         uint8_t key[8];
232         uint8_t fprot[4];
233         struct FTFL_FSEC_t fsec;
234         struct FTFL_FOPT_t fopt;
235         uint8_t feprot;
236         uint8_t fdprot;
237 };
238 CTASSERT_SIZE_BYTE(struct FTFL_CONFIG_t, 16);
239
240 extern volatile struct FTFL_t FTFL;
241 extern char FlexRAM[];
242 extern struct FTFL_CONFIG_t FTFL_CONFIG;
243