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