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