]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/usbotg.h
Code cleanup
[kiibohd-controller.git] / Bootloader / usbotg.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.h"
23 #include "usb-common.h"
24
25
26
27 // ----- Structs -----
28
29 /**
30  * Hardware structures
31  */
32
33 struct USB_ADDINFO_t {
34         UNION_STRUCT_START(8);
35         uint8_t iehost : 1;
36         uint8_t _rsvd0 : 2;
37         uint8_t irqnum : 5;
38         UNION_STRUCT_END;
39 };
40 CTASSERT_SIZE_BIT(struct USB_ADDINFO_t, 8);
41
42 struct USB_OTGSTAT_t {
43         UNION_STRUCT_START(8);
44         uint8_t avbus : 1;
45         uint8_t _rsvd0 : 1;
46         uint8_t b_sess : 1;
47         uint8_t sessvld : 1;
48         uint8_t _rsvd1 : 1;
49         uint8_t line_state : 1;
50         uint8_t onemsec : 1;
51         uint8_t idchg : 1;
52         UNION_STRUCT_END;
53 };
54 CTASSERT_SIZE_BIT(struct USB_OTGSTAT_t, 8);
55
56 struct USB_OTGCTL_t {
57         UNION_STRUCT_START(8);
58         uint8_t _rsvd0 : 2;
59         uint8_t otgen : 1;
60         uint8_t _rsvd1 : 1;
61         uint8_t dmlow : 1;
62         uint8_t dplow : 1;
63         uint8_t _rsvd2 : 1;
64         uint8_t dphigh : 1;
65         UNION_STRUCT_END;
66 };
67 CTASSERT_SIZE_BIT(struct USB_OTGCTL_t, 8);
68
69 struct USB_ISTAT_t {
70         UNION_STRUCT_START(8);
71         uint8_t usbrst : 1;
72         uint8_t error : 1;
73         uint8_t softok : 1;
74         uint8_t tokdne : 1;
75         uint8_t sleep : 1;
76         uint8_t resume : 1;
77         uint8_t attach : 1;
78         uint8_t stall : 1;
79         UNION_STRUCT_END;
80 };
81 CTASSERT_SIZE_BIT(struct USB_ISTAT_t, 8);
82
83 struct USB_ERRSTAT_t {
84         UNION_STRUCT_START(8);
85         uint8_t piderr : 1;
86         uint8_t crc5eof : 1;
87         uint8_t crc16 : 1;
88         uint8_t dfn8 : 1;
89         uint8_t btoerr : 1;
90         uint8_t dmaerr : 1;
91         uint8_t _rsvd0 : 1;
92         uint8_t btserr : 1;
93         UNION_STRUCT_END;
94 };
95 CTASSERT_SIZE_BIT(struct USB_ERRSTAT_t, 8);
96
97 struct USB_STAT_t {
98         UNION_STRUCT_START(8);
99         uint8_t _rsvd0 : 2;
100         enum usb_ep_pingpong pingpong : 1;
101         enum usb_ep_dir dir : 1;
102         uint8_t ep : 4;
103         UNION_STRUCT_END;
104 };
105 CTASSERT_SIZE_BIT(struct USB_STAT_t, 8);
106
107 struct USB_CTL_t {
108         union {
109                 struct /* common */ {
110                         uint8_t _rsvd1 : 1;
111                         uint8_t oddrst : 1;
112                         uint8_t resume : 1;
113                         uint8_t _rsvd2 : 3;
114                         uint8_t se0 : 1;
115                         uint8_t jstate : 1;
116                 };
117                 struct /* host */ {
118                         uint8_t sofen : 1;
119                         uint8_t _rsvd3 : 2;
120                         uint8_t hostmodeen : 1;
121                         uint8_t reset : 1;
122                         uint8_t token_busy : 1;
123                         uint8_t _rsvd4 : 2;
124                 };
125                 struct /* device */ {
126                         uint8_t usben : 1;
127                         uint8_t _rsvd5 : 4;
128                         uint8_t txd_suspend : 1;
129                         uint8_t _rsvd6 : 2;
130                 };
131                 uint8_t raw;
132         };
133 };
134 CTASSERT_SIZE_BIT(struct USB_CTL_t, 8);
135
136 struct USB_ADDR_t {
137         UNION_STRUCT_START(8);
138         uint8_t addr : 7;
139         uint8_t lsen : 1;
140         UNION_STRUCT_END;
141 };
142 CTASSERT_SIZE_BIT(struct USB_ADDR_t, 8);
143
144 struct USB_TOKEN_t {
145         UNION_STRUCT_START(8);
146         uint8_t endpt : 4;
147         enum usb_tok_pid pid : 4;
148         UNION_STRUCT_END;
149 };
150 CTASSERT_SIZE_BIT(struct USB_TOKEN_t, 8);
151
152 struct USB_ENDPT_t {
153         UNION_STRUCT_START(8);
154         uint8_t ephshk : 1;
155         uint8_t epstall : 1;
156         uint8_t eptxen : 1;
157         uint8_t eprxen : 1;
158         uint8_t epctldis : 1;
159         uint8_t _rsvd0 : 1;
160         uint8_t retrydis : 1;
161         uint8_t hostwohub : 1;
162         UNION_STRUCT_END;
163 };
164 CTASSERT_SIZE_BIT(struct USB_ENDPT_t, 8);
165
166 struct USB_USBCTRL_t {
167         UNION_STRUCT_START(8);
168         uint8_t _rsvd0 : 6;
169         uint8_t pde : 1;
170         uint8_t susp : 1;
171         UNION_STRUCT_END;
172 };
173 CTASSERT_SIZE_BIT(struct USB_USBCTRL_t, 8);
174
175 struct USB_OBSERVE_t {
176         UNION_STRUCT_START(8);
177         uint8_t _rsvd0 : 4;
178         uint8_t dmpd : 1;
179         uint8_t _rsvd1 : 1;
180         uint8_t dppd : 1;
181         uint8_t dppu : 1;
182         UNION_STRUCT_END;
183 };
184 CTASSERT_SIZE_BIT(struct USB_OBSERVE_t, 8);
185
186 struct USB_CONTROL_t {
187         UNION_STRUCT_START(8);
188         uint8_t _rsvd0 : 4;
189         uint8_t dppullupnonotg : 1;
190         uint8_t _rsvd1 : 3;
191         UNION_STRUCT_END;
192 };
193 CTASSERT_SIZE_BIT(struct USB_CONTROL_t, 8);
194
195 struct USB_USBTRC0_t {
196         UNION_STRUCT_START(8);
197         uint8_t usb_resume_int : 1;
198         uint8_t sync_det : 1;
199         uint8_t _rsvd0 : 3;
200         uint8_t usbresmen : 1;
201         uint8_t _rsvd1 : 1;
202         uint8_t usbreset : 1;
203         UNION_STRUCT_END;
204 };
205 CTASSERT_SIZE_BIT(struct USB_USBTRC0_t, 8);
206
207 struct USB_t {
208         uint8_t perid;
209         uint8_t _pad0[3];
210         uint8_t idcomp;
211         uint8_t _pad1[3];
212         uint8_t rev;
213         uint8_t _pad2[3];
214         struct USB_ADDINFO_t addinfo;
215         uint8_t _pad3[3];
216         struct USB_OTGSTAT_t otgistat;
217         uint8_t _pad4[3];
218         struct USB_OTGSTAT_t otgicr;
219         uint8_t _pad5[3];
220         struct USB_OTGSTAT_t otgstat;
221         uint8_t _pad6[3];
222         struct USB_OTGCTL_t otgctl;
223         uint8_t _pad7[3];
224         uint8_t _pad8[0x80 - 0x20];
225         struct USB_ISTAT_t istat;
226         uint8_t _pad9[3];
227         struct USB_ISTAT_t inten;
228         uint8_t _pad10[3];
229         struct USB_ERRSTAT_t errstat;
230         uint8_t _pad11[3];
231         struct USB_ERRSTAT_t erren;
232         uint8_t _pad12[3];
233         struct USB_STAT_t stat;
234         uint8_t _pad13[3];
235         struct USB_CTL_t ctl;
236         uint8_t _pad14[3];
237         struct USB_ADDR_t addr;
238         uint8_t _pad15[3];
239         uint8_t bdtpage1;
240         uint8_t _pad16[3];
241         uint8_t frmnuml;
242         uint8_t _pad17[3];
243         struct {
244                 uint8_t frmnumh : 3;
245                 uint8_t _rsvd0 : 5;
246         };
247         uint8_t _pad18[3];
248         struct USB_TOKEN_t token;
249         uint8_t _pad19[3];
250         uint8_t softhld;
251         uint8_t _pad20[3];
252         uint8_t bdtpage2;
253         uint8_t _pad21[3];
254         uint8_t bdtpage3;
255         uint8_t _pad22[3];
256         uint8_t _pad23[0xc0 - 0xb8];
257         struct {
258                 struct USB_ENDPT_t;
259                 uint8_t _pad24[3];
260         } endpt[16];
261         struct USB_USBCTRL_t usbctrl;
262         uint8_t _pad25[3];
263         struct USB_OBSERVE_t observe;
264         uint8_t _pad26[3];
265         struct USB_CONTROL_t control;
266         uint8_t _pad27[3];
267         struct USB_USBTRC0_t usbtrc0;
268         uint8_t _pad28[3];
269         uint8_t _pad29[4];
270         uint8_t usbfrmadjust;
271         uint8_t _pad30[3];
272 };
273 CTASSERT_SIZE_BYTE(struct USB_t, 0x118);
274
275 struct USB_BD_t {
276         struct USB_BD_BITS_t {
277                 union {
278                         struct {
279                                 uint32_t _rsvd0  : 2;
280                                 uint32_t stall   : 1;
281                                 uint32_t dts     : 1;
282                                 uint32_t ninc    : 1;
283                                 uint32_t keep    : 1;
284                                 enum usb_data01 data01 : 1;
285                                 uint32_t own     : 1;
286                                 uint32_t _rsvd1  : 8;
287                                 uint32_t bc      : 10;
288                                 uint32_t _rsvd2  : 6;
289                         };
290                         struct /* processor */ {
291                                 uint32_t _rsvd5  : 2;
292                                 enum usb_tok_pid tok_pid : 4;
293                                 uint32_t _rsvd6  : 26;
294                         };
295                         uint32_t raw;
296                 };
297         };
298         void *addr;
299 };
300 CTASSERT_SIZE_BYTE(struct USB_BD_t, 8);
301
302 extern volatile struct USB_t USB0;
303