altos: Get SAMD21 SPI driver working in non-DMA mode
[fw/altos] / src / samd21 / samd21.h
1 /*
2  * Copyright © 2019 Keith Packard <keithp@keithp.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 2 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, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #ifndef _SAMD21_H_
16 #define _SAMD21_H_
17
18 #include <stdint.h>
19
20 typedef volatile uint64_t       vuint64_t;
21 typedef volatile uint32_t       vuint32_t;
22 typedef volatile void *         vvoid_t;
23 typedef volatile uint16_t       vuint16_t;
24 typedef volatile uint8_t        vuint8_t;
25
26 struct samd21_pac {
27         vuint32_t       wpclr;
28         vuint32_t       wpset;
29 };
30
31 extern struct samd21_pac samd21_pac0;
32 extern struct samd21_pac samd21_pac1;
33 extern struct samd21_pac samd21_pac2;
34
35 #define samd21_pac0 (*(struct samd21_pac *) 0x40000000)
36 #define samd21_pac1 (*(struct samd21_pac *) 0x41000000)
37 #define samd21_pac2 (*(struct samd21_pac *) 0x42000000)
38
39 struct samd21_gclk {
40         vuint8_t        ctrl;
41         vuint8_t        status;
42         vuint16_t       clkctrl;
43         vuint32_t       genctrl;
44         vuint32_t       gendiv;
45 };
46
47 extern struct samd21_gclk samd21_gclk;
48
49 #define samd21_gclk     (*(struct samd21_gclk *) 0x40000c00)
50
51 #define SAMD21_GCLK_CTRL_SWRST          0
52
53 #define SAMD21_GCLK_STATUS_SYNCBUSY     7
54
55 #define SAMD21_GCLK_CLKCTRL_ID          0
56 #define  SAMD21_GCLK_CLKCTRL_ID_DFLL48M_REF             0
57 #define  SAMD21_GCLK_CLKCTRL_ID_DPLL                    1
58 #define  SAMD21_GCLK_CLKCTRL_ID_DPLL_32K                2
59 #define  SAMD21_GCLK_CLKCTRL_ID_WDT                     3
60 #define  SAMD21_GCLK_CLKCTRL_ID_RTC                     4
61 #define  SAMD21_GCLK_CLKCTRL_ID_EIC                     5
62 #define  SAMD21_GCLK_CLKCTRL_ID_USB                     6
63 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_0         0x07
64 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_1         0x08
65 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_2         0x09
66 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_3         0x0a
67 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_4         0x0b
68 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_5         0x0c
69 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_6         0x0d
70 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_7         0x0e
71 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_8         0e0f
72 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_9         0x10
73 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_10        0x11
74 #define  SAMD21_GCLK_CLKCTRL_ID_EVSYS_CHANNEL_11        0x12
75 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOMx_SLOW            0x13
76 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOM0_CORE            0x14
77 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOM1_CORE            0x15
78 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOM2_CORE            0x16
79 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOM3_CORE            0x17
80 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOM4_CORE            0x18
81 #define  SAMD21_GCLK_CLKCTRL_ID_SERCOM5_CORE            0x19
82 #define  SAMD21_GCLK_CLKCTRL_ID_TCC0_TCC1               0x1a
83 #define  SAMD21_GCLK_CLKCTRL_ID_TCC2_TC3                0x1b
84 #define  SAMD21_GCLK_CLKCTRL_ID_TC4_TC5                 0x1c
85 #define  SAMD21_GCLK_CLKCTRL_ID_TC6_TC7                 0x1d
86 #define  SAMD21_GCLK_CLKCTRL_ID_ADC                     0x1e
87 #define  SAMD21_GCLK_CLKCTRL_ID_AC_DIG                  0x1f
88 #define  SAMD21_GCLK_CLKCTRL_ID_AC_ANA                  0x20
89 #define  SAMD21_GCLK_CLKCTRL_ID_DAC                     0x21
90 #define  SAMD21_GCLK_CLKCTRL_ID_PTC                     0x22
91 #define  SAMD21_GCLK_CLKCTRL_ID_I2S_0                   0x23
92 #define  SAMD21_GCLK_CLKCTRL_ID_I2S_1                   0x24
93 #define  SAMD21_GCLK_CLKCTRL_ID_TCC3                    0x25
94
95 #define SAMD21_GCLK_CLKCTRL_GEN         8
96 #define SAMD21_GCLK_CLKCTRL_CLKEN       14
97 #define SAMD21_GCLK_CLKCTRL_WRTLOCK     15
98
99 #define SAMD21_GCLK_GENCTRL_ID          0
100 #define SAMD21_GCLK_GENCTRL_SRC         8
101 #define  SAMD21_GCLK_GENCTRL_SRC_XOSC           0
102 #define  SAMD21_GCLK_GENCTRL_SRC_GCLKIN         1
103 #define  SAMD21_GCLK_GENCTRL_SRC_GCLKGEN1       2
104 #define  SAMD21_GCLK_GENCTRL_SRC_OSCULP32K      3
105 #define  SAMD21_GCLK_GENCTRL_SRC_OSC32K         4
106 #define  SAMD21_GCLK_GENCTRL_SRC_XOSC32K        5
107 #define  SAMD21_GCLK_GENCTRL_SRC_OSC8M          6
108 #define  SAMD21_GCLK_GENCTRL_SRC_DFLL48M        7
109 #define  SAMD21_GCLK_GENCTRL_SRC_FDPLL96M       8
110
111 #define SAMD21_GCLK_GENCTRL_GENEN       16
112 #define SAMD21_GCLK_GENCTRL_IDC         17
113 #define SAMD21_GCLK_GENCTRL_OOV         18
114 #define SAMD21_GCLK_GENCTRL_OE          19
115 #define SAMD21_GCLK_GENCTRL_DIVSEL      20
116 #define SAMD21_GCLK_GENCTRL_RUNSTDBY    21
117
118 #define SAMD21_GCLK_GENDIV_ID           0
119 #define SAMD21_GCLK_GENDIV_DIV          8
120
121 struct samd21_pm {
122         vuint8_t        ctrl;
123         vuint8_t        sleep;
124         vuint8_t        reserved_02;
125         vuint8_t        reserved_03;
126         vuint32_t       reserved_04;
127         vuint8_t        cpusel;
128         vuint8_t        apbasel;
129         vuint8_t        apbbsel;
130         vuint8_t        apbcsel;
131         vuint32_t       reserved_0c;
132
133         vuint32_t       reserved_10;
134         vuint32_t       ahbmask;
135         vuint32_t       apbamask;
136         vuint32_t       apbbmask;
137
138         vuint32_t       apbcmask;
139         vuint32_t       reserved_24;
140         vuint32_t       reserved_28;
141         vuint32_t       reserved_2c;
142
143         vuint32_t       reserved_30;
144         vuint8_t        intenclr;
145         vuint8_t        intelset;
146         vuint8_t        intflag;
147         vuint8_t        reserved_37;
148         vuint8_t        rcause;
149 };
150
151 extern struct samd21_pm samd21_pm;
152
153 #define samd21_pm       (*(struct samd21_pm *) 0x40000400)
154
155 #define SAMD21_PM_CPUSEL_CPUDIV         0
156 #define SAMD21_PM_APBASEL_APBADIV       0
157 #define SAMD21_PM_APBBSEL_APBBDIV       0
158 #define SAMD21_PM_APBCSEL_APBCDIV       0
159
160 #define SAMD21_PM_APBAMASK_PAC0         0
161 #define SAMD21_PM_APBAMASK_PM           1
162 #define SAMD21_PM_APBAMASK_SYSCTRL      2
163 #define SAMD21_PM_APBAMASK_GCLK         3
164 #define SAMD21_PM_APBAMASK_WDT          4
165 #define SAMD21_PM_APBAMASK_RTC          5
166 #define SAMD21_PM_APBAMASK_EIC          6
167
168 #define SAMD21_PM_AHBMASK_HPB0          0
169 #define SAMD21_PM_AHBMASK_HPB1          1
170 #define SAMD21_PM_AHBMASK_HPB2          2
171 #define SAMD21_PM_AHBMASK_DSU           3
172 #define SAMD21_PM_AHBMASK_NVMCTRL       4
173 #define SAMD21_PM_AHBMASK_DMAC          5
174 #define SAMD21_PM_AHBMASK_USB           6
175
176 #define SAMD21_PM_APBBMASK_PAC1         0
177 #define SAMD21_PM_APBBMASK_DSU          1
178 #define SAMD21_PM_APBBMASK_NVMCTRL      2
179 #define SAMD21_PM_APBBMASK_PORT         3
180 #define SAMD21_PM_APBBMASK_DMAC         4
181 #define SAMD21_PM_APBBMASK_USB          5
182
183 #define SAMD21_PM_APBCMASK_PAC2         0
184 #define SAMD21_PM_APBCMASK_EVSYS        1
185 #define SAMD21_PM_APBCMASK_SERCOM0      2
186 #define SAMD21_PM_APBCMASK_SERCOM1      3
187 #define SAMD21_PM_APBCMASK_SERCOM2      4
188 #define SAMD21_PM_APBCMASK_SERCOM3      5
189 #define SAMD21_PM_APBCMASK_SERCOM4      6
190 #define SAMD21_PM_APBCMASK_SERCOM5      7
191 #define SAMD21_PM_APBCMASK_TCC0         8
192 #define SAMD21_PM_APBCMASK_TCC1         9
193 #define SAMD21_PM_APBCMASK_TCC2         10
194 #define SAMD21_PM_APBCMASK_TC3          11
195 #define SAMD21_PM_APBCMASK_TC4          12
196 #define SAMD21_PM_APBCMASK_TC5          13
197 #define SAMD21_PM_APBCMASK_TC6          14
198 #define SAMD21_PM_APBCMASK_TC7          15
199 #define SAMD21_PM_APBCMASK_ADC          16
200 #define SAMD21_PM_APBCMASK_AC           17
201 #define SAMD21_PM_APBCMASK_DAC          18
202 #define SAMD21_PM_APBCMASK_PTC          19
203 #define SAMD21_PM_APBCMASK_I2S          20
204 #define SAMD21_PM_APBCMASK_AC1          21
205 #define SAMD21_PM_APBCMASK_TCC3         24
206
207 struct samd21_sysctrl {
208         vuint32_t       intenclr;
209         vuint32_t       intenset;
210         vuint32_t       intflag;
211         vuint32_t       pclksr;
212
213         vuint32_t       xosc;
214         vuint32_t       xosc32k;
215         vuint32_t       osc32k;
216         vuint32_t       osculp32k;
217
218         vuint32_t       osc8m;
219         vuint32_t       dfllctrl;
220         vuint32_t       dfllval;
221         vuint32_t       dfllmul;
222
223         vuint32_t       dfllsync;
224         vuint32_t       bod33;
225         vuint32_t       reserved_38;
226         vuint32_t       vreg;
227
228         vuint32_t       vref;
229         vuint32_t       dpllctrla;
230         vuint32_t       dpllratio;
231         vuint32_t       dpllctrlb;
232
233         vuint32_t       dpllstatus;
234 };
235
236 extern struct samd21_sysctrl samd21_sysctrl;
237
238 #define samd21_sysctrl (*(struct samd21_sysctrl *) 0x40000800)
239
240 #define SAMD21_SYSCTRL_PCLKSR_XOSCRDY           0
241 #define SAMD21_SYSCTRL_PCLKSR_XOSC32KRDY        1
242 #define SAMD21_SYSCTRL_PCLKSR_OSC32KRDY         2
243 #define SAMD21_SYSCTRL_PCLKSR_OSC8MRDY          3
244 #define SAMD21_SYSCTRL_PCLKSR_DFLLRDY           4
245 #define SAMD21_SYSCTRL_PCLKSR_DFLLOOB           5
246 #define SAMD21_SYSCTRL_PCLKSR_DFLLLCKF          6
247 #define SAMD21_SYSCTRL_PCLKSR_DFLLLCKC          7
248 #define SAMD21_SYSCTRL_PCLKSR_DFLLRCS           8
249 #define SAMD21_SYSCTRL_PCLKSR_BOD33RDY          9
250 #define SAMD21_SYSCTRL_PCLKSR_BOD33DET          10
251 #define SAMD21_SYSCTRL_PCLKSR_B33SRDY           11
252 #define SAMD21_SYSCTRL_PCLKSR_DBPLLLCKR         15
253 #define SAMD21_SYSCTRL_PCLKSR_DPLLLCKF          16
254 #define SAMD21_SYSCTRL_PCLKSR_DPLLTO            17
255
256 #define SAMD21_SYSCTRL_XOSC_ENABLE              1
257 #define SAMD21_SYSCTRL_XOSC_XTALEN              2
258 #define SAMD21_SYSCTRL_XOSC_RUNSTDBY            6
259 #define SAMD21_SYSCTRL_XOSC_ONDEMAND            7
260 #define SAMD21_SYSCTRL_XOSC_GAIN                8
261 #define  SAMD21_SYSCTRL_XOSC_GAIN_2MHz                  0
262 #define  SAMD21_SYSCTRL_XOSC_GAIN_4MHz                  1
263 #define  SAMD21_SYSCTRL_XOSC_GAIN_8MHz                  2
264 #define  SAMD21_SYSCTRL_XOSC_GAIN_16MHz                 3
265 #define  SAMD21_SYSCTRL_XOSC_GAIN_30MHz                 4
266 #define SAMD21_SYSCTRL_XOSC_AMPGC               11
267 #define SAMD21_SYSCTRL_XOSC_STARTUP             12
268 #define  SAMD21_SYSCTRL_XOSC_STARTUP_1                  0
269 #define  SAMD21_SYSCTRL_XOSC_STARTUP_2                  1
270 #define  SAMD21_SYSCTRL_XOSC_STARTUP_4                  2
271 #define  SAMD21_SYSCTRL_XOSC_STARTUP_8                  3
272 #define  SAMD21_SYSCTRL_XOSC_STARTUP_16                 4
273 #define  SAMD21_SYSCTRL_XOSC_STARTUP_32                 5
274 #define  SAMD21_SYSCTRL_XOSC_STARTUP_64                 6
275 #define  SAMD21_SYSCTRL_XOSC_STARTUP_128                7
276 #define  SAMD21_SYSCTRL_XOSC_STARTUP_256                8
277 #define  SAMD21_SYSCTRL_XOSC_STARTUP_512                9
278 #define  SAMD21_SYSCTRL_XOSC_STARTUP_1024               10
279 #define  SAMD21_SYSCTRL_XOSC_STARTUP_2048               11
280 #define  SAMD21_SYSCTRL_XOSC_STARTUP_4096               12
281 #define  SAMD21_SYSCTRL_XOSC_STARTUP_8192               13
282 #define  SAMD21_SYSCTRL_XOSC_STARTUP_16384              14
283 #define  SAMD21_SYSCTRL_XOSC_STARTUP_32768              15
284
285 #define SAMD21_SYSCTRL_XOSC32K_ENABLE   1
286 #define SAMD21_SYSCTRL_XOSC32K_XTALEN   2
287 #define SAMD21_SYSCTRL_XOSC32K_EN32K    3
288 #define SAMD21_SYSCTRL_XOSC32K_AAMPEN   5
289 #define SAMD21_SYSCTRL_XOSC32K_RUNSTDBY 6
290 #define SAMD21_SYSCTRL_XOSC32K_ONDEMAND 7
291 #define SAMD21_SYSCTRL_XOSC32K_STARTUP  8
292 #define SAMD21_SYSCTRL_XOSC32K_WRTLOCK  12
293
294 #define SAMD21_SYSCTRL_OSC8M_ENABLE     1
295 #define SAMD21_SYSCTRL_OSC8M_RUNSTDBY   6
296 #define SAMD21_SYSCTRL_OSC8M_ONDEMAND   7
297 #define SAMD21_SYSCTRL_OSC8M_PRESC      8
298 #define  SAMD21_SYSCTRL_OSC8M_PRESC_1           0
299 #define  SAMD21_SYSCTRL_OSC8M_PRESC_2           1
300 #define  SAMD21_SYSCTRL_OSC8M_PRESC_4           2
301 #define  SAMD21_SYSCTRL_OSC8M_PRESC_8           3
302 #define  SAMD21_SYSCTRL_OSC8M_PRESC_MASK        3
303 #define SAMD21_SYSCTRL_OSC8M_CALIB      16
304 #define SAMD21_SYSCTRL_OSC8M_FRANGE     30
305 #define  SAMD21_SYSCTRL_OSC8M_FRANGE_4_6        0
306 #define  SAMD21_SYSCTRL_OSC8M_FRANGE_6_8        1
307 #define  SAMD21_SYSCTRL_OSC8M_FRANGE_8_11       2
308 #define  SAMD21_SYSCTRL_OSC8M_FRANGE_11_15      3
309
310 #define SAMD21_SYSCTRL_DFLLCTRL_ENABLE          1
311 #define SAMD21_SYSCTRL_DFLLCTRL_MODE            2
312 #define SAMD21_SYSCTRL_DFLLCTRL_STABLE          3
313 #define SAMD21_SYSCTRL_DFLLCTRL_LLAW            4
314 #define SAMD21_SYSCTRL_DFLLCTRL_USBCRM          5
315 #define SAMD21_SYSCTRL_DFLLCTRL_RUNSTDBY        6
316 #define SAMD21_SYSCTRL_DFLLCTRL_ONDEMAND        7
317 #define SAMD21_SYSCTRL_DFLLCTRL_CCDIS           8
318 #define SAMD21_SYSCTRL_DFLLCTRL_QLDIS           9
319 #define SAMD21_SYSCTRL_DFLLCTRL_BPLCKC          10
320 #define SAMD21_SYSCTRL_DFLLCTRL_WAITLOCK        11
321
322 #define SAMD21_SYSCTRL_DFLLVAL_FINE             0
323 #define SAMD21_SYSCTRL_DFLLVAL_COARSE           10
324 #define SAMD21_SYSCTRL_DFLLVAL_DIFF             16
325
326 #define SAMD21_SYSCTRL_DFLLMUL_MUL              0
327 #define SAMD21_SYSCTRL_DFLLMUL_FSTEP            16
328 #define SAMD21_SYSCTRL_DFLLMUL_CSTEP            26
329
330 #define SAMD21_SYSCTRL_DFLLSYNC_READREQ         7
331
332 #define SAMD21_SYSCTRL_DPLLCTRLA_ENABLE         1
333 #define SAMD21_SYSCTRL_DPLLCTRLA_RUNSTDBY       6
334 #define SAMD21_SYSCTRL_DPLLCTRLA_ONDEMAND       7
335
336 #define SAMD21_SYSCTRL_DPLLRATIO_LDR            0
337 #define SAMD21_SYSCTRL_DPLLRATIO_LDRFRAC        0
338
339 #define SAMD21_SYSCTRL_DPLLCTRLB_FILTER         0
340 #define  SAMD21_SYSCTRL_DPLLCTRLB_FILTER_DEFAULT        0
341 #define  SAMD21_SYSCTRL_DPLLCTRLB_FILTER_LBFILT         1
342 #define  SAMD21_SYSCTRL_DPLLCTRLB_FILTER_HBFILT         2
343 #define  SAMD21_SYSCTRL_DPLLCTRLB_FILTER_HDFILT         3
344 #define SAMD21_SYSCTRL_DPLLCTRLB_LPEN           2
345 #define SAMD21_SYSCTRL_DPLLCTRLB_WUF            3
346 #define SAMD21_SYSCTRL_DPLLCTRLB_REFCLK         4
347 #define  SAMD21_SYSCTRL_DPLLCTRLB_REFCLK_XOSC32         0
348 #define  SAMD21_SYSCTRL_DPLLCTRLB_REFCLK_XOSC           1
349 #define  SAMD21_SYSCTRL_DPLLCTRLB_REFCLK_GCLK_DPLL      2
350 #define SAMD21_SYSCTRL_DPLLCTRLB_LTIME          8
351 #define  SAMD21_SYSCTRL_DPLLCTRLB_LTIME_DEFAULT         0
352 #define  SAMD21_SYSCTRL_DPLLCTRLB_LTIME_8MS             4
353 #define  SAMD21_SYSCTRL_DPLLCTRLB_LTIME_9MS             5
354 #define  SAMD21_SYSCTRL_DPLLCTRLB_LTIME_10MS            6
355 #define  SAMD21_SYSCTRL_DPLLCTRLB_LTIME_11MS            7
356 #define SAMD21_SYSCTRL_DPLLCTRLB_LBYPASS        12
357 #define SAMD21_SYSCTRL_DPLLCTRLB_DIV            16
358
359 #define SAMD21_SYSCTRL_DPLLSTATUS_LOCK          0
360 #define SAMD21_SYSCTRL_DPLLSTATUS_CLKRDY        1
361 #define SAMD21_SYSCTRL_DPLLSTATUS_ENABLE        2
362 #define SAMD21_SYSCTRL_DPLLSTATUS_DIV           3
363
364 struct samd21_dmac {
365         vuint16_t       ctrl;
366         vuint16_t       crcctrl;
367         vuint32_t       crcdatain;
368         vuint32_t       crcchksum;
369         vuint8_t        crcstatus;
370         vuint8_t        dbgctrl;
371         vuint8_t        qosctrl;
372         uint8_t         reserved_0f;
373
374         vuint32_t       swtrigctrl;
375         vuint32_t       prictrl0;
376         uint32_t        reserved_18;
377         uint32_t        reserved_1c;
378
379         vuint16_t       intpend;
380         uint16_t        reserved_22;
381         vuint32_t       intstatus;
382         vuint32_t       busych;
383         vuint32_t       pendch;
384
385         vuint32_t       active;
386         vuint32_t       baseaddr;
387         vuint32_t       wrbaddr;
388         uint16_t        reserved_3c;
389         uint8_t         reserved_3e;
390         vuint8_t        chid;
391
392         vuint8_t        chctrla;
393         uint8_t         reserved_41;
394         uint16_t        reserved_42;
395         vuint32_t       chctrlb;
396         uint32_t        reserved_48;
397         vuint8_t        chintenclr;
398         vuint8_t        chintenset;
399         vuint8_t        chintflag;
400         vuint8_t        chstatus;
401 };
402
403 extern struct samd21_dmac samd21_dmac;
404
405 #define samd21_dmac (*(struct samd21_dmac *) 0x41004800)
406
407 struct samd21_dmac_desc {
408         vuint16_t       btctrl;
409         vuint16_t       btcnt;
410         vuint32_t       srcaddr;
411         vuint32_t       dstaddr;
412         vuint32_t       descaddr;
413 } __attribute__((aligned(8)));
414
415 #define SAMD21_DMAC_NCHAN               12
416
417 #define SAMD21_DMAC_CTRL_SWRST          0
418 #define SAMD21_DMAC_CTRL_DMAENABLE      1
419 #define SAMD21_DMAC_CTRL_CRCENABLE      2
420 #define SAMD21_DMAC_CTRL_LVLEN(x)       (8 + (x))
421
422 #define SAMD21_DMAC_QOSCTRL_WRBQOS      0
423 #define SAMD21_DMAC_QOSCTRL_FQOS        2
424 #define SAMD21_DMAC_QOSCTRL_DQOS        4
425
426 #define SAMD21_DMAC_QOSCTRL_DISABLE     0
427 #define SAMD21_DMAC_QOSCTRL_LOW         1
428 #define SAMD21_DMAC_QOSCTRL_MEDIUM      2
429 #define SAMD21_DMAC_QOSCTRL_HIGH        3
430
431 #define SAMD21_DMAC_SWTRIGCTRL_SWTRIG(n)        (0 + (n))
432
433 #define SAMD21_DMAC_PRICTRL0_LVLPRI0    0
434 #define SAMD21_DMAC_PRICTRL0_RRLVLEN0   7
435 #define SAMD21_DMAC_PRICTRL0_LVLPRI1    8
436 #define SAMD21_DMAC_PRICTRL0_RRLVLEN1   15
437 #define SAMD21_DMAC_PRICTRL0_LVLPRI2    16
438 #define SAMD21_DMAC_PRICTRL0_RRLVLEN2   23
439 #define SAMD21_DMAC_PRICTRL0_LVLPRI3    24
440 #define SAMD21_DMAC_PRICTRL0_RRLVLEN3   31
441
442 #define SAMD21_DMAC_INTPEND_ID          0
443 #define  SAMD21_DMAC_INTPEND_ID_MASK            0xf
444 #define SAMD21_DMAC_INTPEND_TERR        8
445 #define SAMD21_DMAC_INTPEND_TCMPL       9
446 #define SAMD21_DMAC_INTPEND_SUSP        10
447 #define SAMD21_DMAC_INTPEND_FERR        13
448 #define SAMD21_DMAC_INTPEND_BUSY        14
449 #define SAMD21_DMAC_INTPEND_PEND        15
450
451 #define SAMD21_DMAC_INTSTATUS_CHINT(n)  (0 + (n))
452
453 #define SAMD21_DMAC_BUSYCH_BUSYCH(n)    (0 + (n))
454
455 #define SAMD21_DMAC_PENDCH_PENDCH(n)    (0 + (n))
456
457 #define SAMD21_DMAC_ACTIVE_LVLEX(x)     (0 + (x))
458 #define SAMD21_DMAC_ACTIVE_ID           8
459 #define SAMD21_DMAC_ACTIVE_ABUSY        15
460 #define SAMD21_DMAC_ACTIVE_BTCNT        16
461
462 #define SAMD21_DMAC_CHCTRLA_SWRST       0
463 #define SAMD21_DMAC_CHCTRLA_ENABLE      1
464
465 #define SAMD21_DMAC_CHCTRLB_EVACT       0
466 #define  SAMD21_DMAC_CHCTRLB_EVACT_NOACT        0
467 #define  SAMD21_DMAC_CHCTRLB_EVACT_TRIG         1
468 #define  SAMD21_DMAC_CHCTRLB_EVACT_CTRIG        2
469 #define  SAMD21_DMAC_CHCTRLB_EVACT_CBLOCK       3
470 #define  SAMD21_DMAC_CHCTRLB_EVACT_SUSPEND      4
471 #define  SAMD21_DMAC_CHCTRLB_EVACT_RESUME       5
472 #define  SAMD21_DMAC_CHCTRLB_EVACT_SSKIP        6
473
474 #define SAMD21_DMAC_CHCTRLB_EVIE        3
475 #define SAMD21_DMAC_CHCTRLB_EVOE        4
476 #define SAMD21_DMAC_CHCTRLB_LVL         5
477 #define SAMD21_DMAC_CHCTRLB_TRIGSRC     8
478 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_DISABLE    0x00
479 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_SERCOM_RX(n)       (0x01 + (n) * 2)
480 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_SERCOM_TX(n)       (0x02 + (n) * 2)
481 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_OVF   0x0d
482 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC0   0x0e
483 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC1   0x0f
484 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC2   0x10
485 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC3   0x11
486 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC1_OVF   0x12
487 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC1_MC0   0x13
488 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC1_MC1   0x14
489 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC2_OVF   0x15
490 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC2_MC0   0x16
491 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC2_MC1   0x17
492 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC3_OVF    0x18
493 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC3_MC0    0x19
494 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC3_MC1    0x1a
495 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC4_OVF    0x1b
496 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC4_MC0    0x1c
497 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC4_MC1    0x1d
498 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC5_OVF    0x1e
499 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC5_MC0    0x1f
500 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC5_MC1    0x20
501 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC6_OVF    0x21
502 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC6_MC0    0x22
503 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC6_MC1    0x23
504 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC7_OVF    0x24
505 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC7_MC0    0x25
506 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC7_MC1    0x26
507 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_ADC_RESRDY 0x27
508 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_DAC_EMPTY  0x28
509 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_RX_0   0x29
510 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_RX_1   0x2a
511 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_TX_0   0x2b
512 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_TX_1   0x2c
513 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_OVF   0x2d
514 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC0   0x2e
515 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC1   0x2f
516 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC2   0x30
517 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC3   0x31
518
519 #define SAMD21_DMAC_CHCTRLB_TRIGACT     22
520 #define  SAMD21_DMAC_CHCTRLB_TRIGACT_BLOCK              0
521 #define  SAMD21_DMAC_CHCTRLB_TRIGACT_BEAT               2
522 #define  SAMD21_DMAC_CHCTRLB_TRIGACT_TRANSACTION        3
523
524 #define SAMD21_DMAC_CHCTRLB_CMD         24
525 #define  SAMD21_DMAC_CHCTRLB_CMD_NOACT          0
526 #define  SAMD21_DMAC_CHCTRLB_CMD_SUSPEND        1
527 #define  SAMD21_DMAC_CHCTRLB_CMD_RESUME         2
528
529 #define SAMD21_DMAC_CHINTFLAG_TERR      0
530 #define SAMD21_DMAC_CHINTFLAG_TCMPL     1
531 #define SAMD21_DMAC_CHINTFLAG_SUSP      2
532
533 #define SAMD21_DMAC_CHSTATUS_PEND       0
534 #define SAMD21_DMAC_CHSTATUS_BUSY       1
535 #define SAMD21_DMAC_CHSTATUS_FERR       2
536
537 #define SAMD21_DMAC_DESC_BTCTRL_VALID           0
538 #define SAMD21_DMAC_DESC_BTCTRL_EVOSEL          1
539 #define SAMD21_DMAC_DESC_BTCTRL_BLOCKACT        3
540 #define SAMD21_DMAC_DESC_BTCTRL_BEATSIZE        8
541 #define  SAMD21_DMAC_DESC_BTCTRL_BEATSIZE_BYTE          0
542 #define  SAMD21_DMAC_DESC_BTCTRL_BEATSIZE_HWORD         1
543 #define  SAMD21_DMAC_DESC_BTCTRL_BEATSIZE_WORD          2
544 #define SAMD21_DMAC_DESC_BTCTRL_SRCINC          10
545 #define SAMD21_DMAC_DESC_BTCTRL_DSTINC          11
546 #define SAMD21_DMAC_DESC_BTCTRL_STEPSEL         12
547 #define SAMD21_DMAC_DESC_BTCTRL_STEPSIZE        13
548
549 struct samd21_nvmctrl {
550         vuint32_t       ctrla;
551         vuint32_t       ctrlb;
552         vuint32_t       param;
553         vuint32_t       intenclr;
554
555         vuint32_t       intenset;
556         vuint32_t       intflag;
557         vuint32_t       status;
558         vuint32_t       addr;
559
560         vuint32_t       lock;
561 };
562
563 extern struct samd21_nvmctrl samd21_nvmctrl;
564
565 #define samd21_nvmctrl (*(struct samd21_nvmctrl *) 0x41004000)
566
567 #define SAMD21_NVMCTRL_CTRLA_CMD        0
568 #define  SAMD21_NVMCTRL_CTRLA_CMD_ER            0x02
569 #define  SAMD21_NVMCTRL_CTRLA_CMD_WP            0x04
570 #define  SAMD21_NVMCTRL_CTRLA_CMD_EAR           0x05
571 #define  SAMD21_NVMCTRL_CTRLA_CMD_WAP           0x06
572 #define  SAMD21_NVMCTRL_CTRLA_CMD_RWWEEER       0x1a
573 #define  SAMD21_NVMCTRL_CTRLA_CMD_RWEEEWP       0x1c
574 #define  SAMD21_NVMCTRL_CTRLA_CMD_LR            0x40
575 #define  SAMD21_NVMCTRL_CTRLA_CMD_UR            0x41
576 #define  SAMD21_NVMCTRL_CTRLA_CMD_SPRM          0x42
577 #define  SAMD21_NVMCTRL_CTRLA_CMD_CPRM          0x43
578 #define  SAMD21_NVMCTRL_CTRLA_CMD_PBC           0x44
579 #define  SAMD21_NVMCTRL_CTRLA_CMD_SSB           0x45
580 #define  SAMD21_NVMCTRL_CTRLA_CMD_INVALL        0x46
581 #define  SAMD21_NVMCTRL_CTRLA_CMD_LDR           0x47
582 #define  SAMD21_NVMCTRL_CTRLA_CMD_UDR           0x48
583 #define SAMD21_NVMCTRL_CTRLA_CMDEX      8
584 #define  SAMD21_NVMCTRL_CTRLA_CMDEX_KEY         0xa5
585
586 #define SAMD21_NVMCTRL_CTRLB_RWS        1
587 #define SAMD21_NVMCTRL_CTRLB_MANW       7
588 #define SAMD21_NVMCTRL_CTRLB_SLEEPRM    8
589 #define SAMD21_NVMCTRL_CTRLB_READMODE   16
590 #define SAMD21_NVMCTRL_CTRLB_CACHEDIS   18
591
592 #define SAMD21_NVMCTRL_INTENCLR_READY   0
593 #define SAMD21_NVMCTRL_INTENCLR_ERROR   1
594
595 #define SAMD21_NVMCTRL_INTENSET_READY   0
596 #define SAMD21_NVMCTRL_INTENSET_ERROR   1
597
598 #define SAMD21_NVMCTRL_INTFLAG_READY    0
599 #define SAMD21_NVMCTRL_INTFLAG_ERROR    1
600
601 #define SAMD21_NVMCTRL_STATUS_PRM       0
602 #define SAMD21_NVMCTRL_STATUS_LOAD      1
603 #define SAMD21_NVMCTRL_STATUS_PROGE     2
604 #define SAMD21_NVMCTRL_STATUS_LOCKE     3
605 #define SAMD21_NVMCTRL_STATUS_NVME      4
606 #define SAMD21_NVMCTRL_STATUS_SB        8
607
608 #define SAMD21_NVMCTRL_PARAM_NVMP       0
609 #define  SAMD21_NVMCTRL_PARAM_NVMP_MASK         0xffff
610 #define SAMD21_NVMCTRL_PARAM_PSZ        16
611 #define  SAMD21_NVMCTRL_PARAM_PSZ_MASK          0x7
612 #define SAMD21_NVMCTRL_PARAM_RWWEEP     20
613 #define  SAMD21_NVMCTRL_PARAM_RWWEEP_MASK       0xfff
614
615 static inline uint32_t
616 samd21_nvmctrl_page_size(void)
617 {
618         return 1 << (3 + ((samd21_nvmctrl.param >> SAMD21_NVMCTRL_PARAM_PSZ) &
619                           SAMD21_NVMCTRL_PARAM_PSZ_MASK));
620 }
621
622 uint32_t
623 samd21_flash_size(void);
624
625 struct samd21_port {
626         vuint32_t       dir;
627         vuint32_t       dirclr;
628         vuint32_t       dirset;
629         vuint32_t       dirtgl;
630
631         vuint32_t       out;
632         vuint32_t       outclr;
633         vuint32_t       outset;
634         vuint32_t       outtgl;
635
636         vuint32_t       in;
637         vuint32_t       ctrl;
638         vuint32_t       wrconfig;
639         vuint32_t       reserved_2c;
640
641         vuint8_t        pmux[16];
642
643         vuint8_t        pincfg[32];
644 };
645
646 extern struct samd21_port samd21_port_a;
647 extern struct samd21_port samd21_port_b;
648
649 #define samd21_port_a (*(struct samd21_port *) 0x41004400)
650 #define samd21_port_b (*(struct samd21_port *) 0x41004480)
651
652 #define SAMD21_PORT_PINCFG_PMUXEN       0
653 #define SAMD21_PORT_PINCFG_INEN         1
654 #define SAMD21_PORT_PINCFG_PULLEN       2
655 #define SAMD21_PORT_PINCFG_DRVSTR       6
656
657 #define SAMD21_PORT_PMUX_FUNC_A         0
658 #define SAMD21_PORT_PMUX_FUNC_B         1
659 #define SAMD21_PORT_PMUX_FUNC_C         2
660 #define SAMD21_PORT_PMUX_FUNC_D         3
661 #define SAMD21_PORT_PMUX_FUNC_E         4
662 #define SAMD21_PORT_PMUX_FUNC_F         5
663 #define SAMD21_PORT_PMUX_FUNC_G         6
664 #define SAMD21_PORT_PMUX_FUNC_H         7
665 #define SAMD21_PORT_PMUX_FUNC_I         8
666
667 #define SAMD21_PORT_DIR_OUT             1
668 #define SAMD21_PORT_DIR_IN              0
669
670 static inline void
671 samd21_port_dir_set(struct samd21_port *port, uint8_t pin, uint8_t dir)
672 {
673         if (dir)
674                 port->dirset = (1 << pin);
675         else
676                 port->dirclr = (1 << pin);
677 }
678
679 static inline void
680 samd21_port_pincfg_set(struct samd21_port *port, uint8_t pin, uint8_t pincfg_mask, uint8_t pincfg)
681 {
682         port->pincfg[pin] = (uint8_t) ((port->pincfg[pin] & ~pincfg_mask) | pincfg);
683 }
684
685 static inline uint8_t
686 samd21_port_pincfg_get(struct samd21_port *port, uint8_t pin)
687 {
688         return port->pincfg[pin];
689 }
690
691 static inline void
692 samd21_port_pmux_set(struct samd21_port *port, uint8_t pin, uint8_t func)
693 {
694         uint8_t byte = pin >> 1;
695         uint8_t bit = (pin & 1) << 2;
696         uint8_t mask = 0xf << bit;
697         uint8_t value = (uint8_t) ((port->pmux[byte] & ~mask) | (func << bit));
698         port->pmux[byte] = value;
699         samd21_port_pincfg_set(port, pin,
700                                (1 << SAMD21_PORT_PINCFG_PMUXEN),
701                                (1 << SAMD21_PORT_PINCFG_PMUXEN));
702 }
703
704 static inline void
705 samd21_port_pmux_clr(struct samd21_port *port, uint8_t pin)
706 {
707         samd21_port_pincfg_set(port, pin,
708                                (0 << SAMD21_PORT_PINCFG_PMUXEN),
709                                (1 << SAMD21_PORT_PINCFG_PMUXEN));
710 }
711
712 struct samd21_adc {
713         vuint8_t        ctrla;
714         vuint8_t        refctrl;
715         vuint8_t        avgctrl;
716         vuint8_t        sampctrl;
717         vuint16_t       ctrlb;
718         vuint16_t       reserved_06;
719         vuint8_t        winctrl;
720         vuint8_t        reserved_09;
721         vuint16_t       reserved_0a;
722         vuint8_t        swtrig;
723         vuint8_t        reserved_0d;
724         vuint16_t       reserved_0e;
725
726         vuint32_t       inputctrl;
727         vuint8_t        evctrl;
728         vuint8_t        reserved_15;
729         vuint8_t        intenclr;
730         vuint8_t        intenset;
731         vuint8_t        intflag;
732         vuint8_t        status;
733         vuint16_t       result;
734         vuint16_t       winlt;
735         vuint16_t       reserved_1e;
736
737         vuint16_t       winut;
738         vuint16_t       reserved_22;
739         vuint16_t       gaincorr;
740         vuint16_t       offsetcorr;
741         vuint16_t       calib;
742         vuint8_t        dbgctrl;
743         vuint8_t        reserved_2b;
744         vuint32_t       reserved_2c;
745 };
746
747 #define SAMD21_ADC_CTRLA_SWRST          0
748 #define SAMD21_ADC_CTRLA_ENABLE         1
749 #define SAMD21_ADC_CTRLA_RUNSTDBY       2
750
751 #define SAMD21_ADC_REFCTRL_REFSEL       0
752 #define  SAMD21_ADC_REFCTRL_REFSEL_INT1V        0
753 #define  SAMD21_ADC_REFCTRL_REFSEL_INTVCC0      1
754 #define  SAMD21_ADC_REFCTRL_REFSEL_INTVCC1      2
755 #define  SAMD21_ADC_REFCTRL_REFSEL_VREFA        3
756 #define  SAMD21_ADC_REFCTRL_REFSEL_VREFB        4
757 #define SAMD21_ADC_REFCTRL_REFCOMP      7
758
759 #define SAMD21_ADC_AVGCTRL_SAMPLENUM    0
760 #define SAMD21_ADC_AVGCTRL_ADJRES       4
761
762 #define SAMD21_ADC_SAMPCTRL_SAMPLEN     0
763
764 #define SAMD21_ADC_CTRLB_DIFFMODE       0
765 #define SAMD21_ADC_CTRLB_LEFTADJ        1
766 #define SAMD21_ADC_CTRLB_FREERUN        2
767 #define SAMD21_ADC_CTRLB_CORREN         3
768 #define SAMD21_ADC_CTRLB_RESSEL         4
769 #define  SAMD21_ADC_CTRLB_RESSEL_12BIT          0
770 #define  SAMD21_ADC_CTRLB_RESSEL_16BIT          1
771 #define  SAMD21_ADC_CTRLB_RESSEL_10BIT          2
772 #define  SAMD21_ADC_CTRLB_RESSEL_8BIT           3
773 #define SAMD21_ADC_CTRLB_PRESCALER      8
774 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV4        0
775 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV8        1
776 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV16       2
777 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV32       3
778 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV64       4
779 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV128      5
780 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV256      6
781 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV512      7
782
783 #define SAMD21_ADC_SWTRIG_FLUSH         0
784 #define SAMD21_ADC_SWTRIG_START         1
785
786 #define SAMD21_ADC_INPUTCTRL_MUXPOS             0
787 # define SAMD21_ADC_INPUTCTRL_MUXPOS_BANDGAP            0x19
788 # define SAMD21_ADC_INPUTCTRL_MUXPOS_SCALEDCOREVCC      0x1a
789 # define SAMD21_ADC_INPUTCTRL_MUXPOS_SCALEDIOVCC        0x1b
790 # define SAMD21_ADC_INPUTCTRL_MUXPOS_DAC                0x1c
791 #define SAMD21_ADC_INPUTCTRL_MUXNEG             8
792 # define SAMD21_ADC_INPUTCTRL_MUXNEG_GND                0x18
793 # define SAMD21_ADC_INPUTCTRL_MUXNEG_IOGND              0x19
794 #define SAMD21_ADC_INPUTCTRL_INPUTSCAN          16
795 #define SAMD21_ADC_INPUTCTRL_INPUTOFFSET        20
796 #define SAMD21_ADC_INPUTCTRL_GAIN               24
797 #define  SAMD21_ADC_INPUTCTRL_GAIN_1X                   0
798 #define  SAMD21_ADC_INPUTCTRL_GAIN_DIV2                 0xf
799
800 #define SAMD21_ADC_INTFLAG_RESRDY       0
801 #define SAMD21_ADC_INTFLAG_OVERRUN      1
802 #define SAMD21_ADC_INTFLAG_WINMON       2
803 #define SAMD21_ADC_INTFLAG_SYNCRDY      3
804
805 #define SAMD21_ADC_STATUS_SYNCBUSY      7
806
807 #define SAMD21_ADC_CALIB_LINEARITY_CAL  0
808 #define SAMD21_ADC_CALIB_BIAS_CAL       16
809
810 extern struct samd21_adc samd21_adc;
811
812 #define samd21_adc (*(struct samd21_adc *) 0x42004000)
813
814 struct samd21_dac {
815         vuint8_t        ctrla;
816         vuint8_t        ctrlb;
817         vuint8_t        evctrl;
818         uint8_t         reserved_03;
819
820         vuint8_t        intenclr;
821         vuint8_t        intenset;
822         vuint8_t        intflag;
823         vuint8_t        status;
824
825         vuint16_t       data;
826         uint16_t        reserved_0a;
827
828         vuint16_t       databuf;
829 };
830
831 #define SAMD21_DAC_CTRLA_SWRST          0
832 #define SAMD21_DAC_CTRLA_ENABLE         1
833 #define SAMD21_DAC_CTRLA_RUNSTDBY       2
834
835 #define SAMD21_DAC_CTRLB_EOEN           0
836 #define SAMD21_DAC_CTRLB_IOEN           1
837 #define SAMD21_DAC_CTRLB_LEFTADJ        2
838 #define SAMD21_DAC_CTRLB_VPD            3
839 #define SAMD21_DAC_CTRLB_BDWP           4
840 #define SAMD21_DAC_CTRLB_REFSEL         6
841 #define  SAMD21_DAC_CTRLB_REFSEL_INTREF         0
842 #define  SAMD21_DAC_CTRLB_REFSEL_VDDANA         1
843 #define  SAMD21_DAC_CTRLB_REFSEL_VREFA          2
844 #define  SAMD21_DAC_CTRLB_REFSEL_MASK           3
845
846 #define SAMD21_DAC_EVCTRL_STARTEI       0
847 #define SAMD21_DAC_EVCTRL_EMPTYEO       1
848
849 #define SAMD21_DAC_INTENCLR_UNDERRUN    0
850 #define SAMD21_DAC_INTENCLR_EMPTY       1
851 #define SAMD21_DAC_INTENCLR_SYNCRDY     2
852
853 #define SAMD21_DAC_INTENSET_UNDERRUN    0
854 #define SAMD21_DAC_INTENSET_EMPTY       1
855 #define SAMD21_DAC_INTENSET_SYNCRDY     2
856
857 #define SAMD21_DAC_INTFLAG_UNDERRUN     0
858 #define SAMD21_DAC_INTFLAG_EMPTY        1
859 #define SAMD21_DAC_INTFLAG_SYNCRDY      2
860
861 #define SAMD21_DAC_STATUS_SYNCBUSY      7
862
863 extern struct samd21_dac samd21_dac;
864 #define samd21_dac (*(struct samd21_dac *) 0x42004800)
865
866 /* TC */
867 struct samd21_tc {
868         vuint16_t       ctrla;
869         vuint16_t       readreq;
870         vuint8_t        ctrlbclr;
871         vuint8_t        ctrlbset;
872         vuint8_t        ctrlc;
873         vuint8_t        reserved_07;
874         vuint8_t        dbgctrl;
875         vuint8_t        reserved_09;
876         vuint16_t       evctrl;
877         vuint8_t        intenclr;
878         vuint8_t        intenset;
879         vuint8_t        intflag;
880         vuint8_t        status;
881
882         union {
883                 struct {
884                         vuint8_t        count;
885                         vuint8_t        reserved_11;
886                         vuint16_t       reserved_12;
887                         vuint8_t        per;
888                         vuint8_t        reserved_15;
889                         vuint16_t       reserved_16;
890                         vuint8_t        cc[2];
891                 } mode_8;
892                 struct {
893                         vuint16_t       count;
894                         vuint16_t       reserved_12;
895                         vuint32_t       reserved_14;
896                         vuint16_t       cc[2];
897                 } mode_16;
898                 struct {
899                         vuint32_t       count;
900                         vuint32_t       reserved_14;
901                         vuint32_t       cc[2];
902                 } mode_32;
903         };
904 };
905
906 extern struct samd21_tc samd21_tc3;
907 #define samd21_tc3 (*(struct samd21_tc *) 0x42002c00)
908
909 extern struct samd21_tc samd21_tc4;
910 #define samd21_tc4 (*(struct samd21_tc *) 0x42003000)
911
912 extern struct samd21_tc samd21_tc5;
913 #define samd21_tc5 (*(struct samd21_tc *) 0x42003400)
914
915 #ifdef ATSAMD21J
916 /* Present on all of the samd21j parts and the samd21g16l */
917 extern struct samd21_tc samd21_tc6;
918 #define samd21_tc6 (*(struct samd21_tc *) 0x42003800)
919
920 extern struct samd21_tc samd21_tc7;
921 #define samd21_tc7 (*(struct samd21_tc *) 0x42003c00)
922 #endif
923
924 #define SAMD21_TC_CTRLA_SWRST           0
925 #define SAMD21_TC_CTRLA_ENABLE          1
926 #define SAMD21_TC_CTRLA_MODE            2
927 #define  SAMD21_TC_CTRLA_MODE_COUNT16           0
928 #define  SAMD21_TC_CTRLA_MODE_COUNT8            1
929 #define  SAMD21_TC_CTRLA_MODE_COUNT32           2
930 #define SAMD21_TC_CTRLA_WAVEGEN         5
931 #define  SAMD21_TC_CTRLA_WAVEGEN_NFRQ           0
932 #define  SAMD21_TC_CTRLA_WAVEGEN_MFRQ           1
933 #define  SAMD21_TC_CTRLA_WAVEGEN_NPWM           2
934 #define  SAMD21_TC_CTRLA_WAVEGEN_MPWM           3
935 #define SAMD21_TC_CTRLA_PRESCALER       8
936 #define  SAMD21_TC_CTRLA_PRESCALER_DIV1         0
937 #define  SAMD21_TC_CTRLA_PRESCALER_DIV2         1
938 #define  SAMD21_TC_CTRLA_PRESCALER_DIV4         2
939 #define  SAMD21_TC_CTRLA_PRESCALER_DIV8         3
940 #define  SAMD21_TC_CTRLA_PRESCALER_DIV16        4
941 #define  SAMD21_TC_CTRLA_PRESCALER_DIV64        5
942 #define  SAMD21_TC_CTRLA_PRESCALER_DIV256       6
943 #define  SAMD21_TC_CTRLA_PRESCALER_DIV1024      7
944 #define SAMD21_TC_CTRLA_RUNSTDBY        11
945 #define SAMD21_TC_CTRLA_PRESCSYNC       12
946 #define  SAMD21_TC_CTRLA_PRESCSYNC_GCLK         0
947 #define  SAMD21_TC_CTRLA_PRESCSYNC_PRSEC        1
948 #define  SAMD21_TC_CTRLA_PRESCSYNC_RESYNC       2
949
950 #define SAMD21_TC_READREQ_ADDR          0
951 #define SAMD21_TC_READREQ_RCONT         14
952 #define SAMD21_TC_READREQ_RREQ          15
953 #define SAMD21_TC_CTRLB_DIR             0
954 #define SAMD21_TC_CTRLB_ONESHOT         2
955 #define SAMD21_TC_CTRLB_CMD             6
956 #define SAMD21_TC_CTRLC_INVEN(x)        (0 + (x))
957 #define SAMD21_TC_CTRLC_CPTEN(x)        (4 + (x))
958 #define SAMD21_TC_DBGCTRL_DBGRUN        0
959 #define SAMD21_TC_EVCTRL_EVACT          0
960 #define SAMD21_TC_EVCTRL_TCINV          4
961 #define SAMD21_TC_EVCTRL_TCEI           5
962 #define SAMD21_TC_EVCTRL_OVFEO          8
963 #define SAMD21_TC_EVCTRL_MCEO(x)        (12 + (x))
964
965 #define SAMD21_TC_INTFLAG_MC(x)         (4 + (x))
966 #define SAMD21_TC_INTFLAG_SYNCRDY       3
967 #define SAMD21_TC_INTFLAG_ERR           1
968 #define SAMD21_TC_INTFLAG_OVF           0
969
970 #define SAMD21_TC_STATUS_STOP           3
971 #define SAMD21_TC_STATUS_FOLLOWER       4
972 #define SAMD21_TC_STATUS_SYNCBUSY       7
973
974 /* TCC */
975
976 struct samd21_tcc {
977         vuint32_t       ctrla;
978         vuint8_t        ctrlbclr;
979         vuint8_t        ctrlbset;
980         vuint16_t       reserved_06;
981         vuint32_t       syncbusy;
982         vuint32_t       fctrla;
983
984         vuint32_t       fctlrb;
985         vuint32_t       wexctrl;
986         vuint32_t       drvctrl;
987         vuint16_t       reserved_1c;
988         vuint8_t        dbgctrl;
989         vuint8_t        reserved_1f;
990
991         vuint32_t       evctrl;
992         vuint32_t       intenclr;
993         vuint32_t       intenset;
994         vuint32_t       intflag;
995
996         vuint32_t       status;
997         vuint32_t       count;
998         vuint16_t       patt;
999         vuint16_t       reserved_3a;
1000         vuint32_t       wave;
1001
1002         vuint32_t       per;
1003         vuint32_t       cc[4];
1004         vuint32_t       reserved_54;
1005         vuint32_t       reserved_58;
1006         vuint32_t       reserved_5c;
1007
1008         vuint32_t       reserved_60;
1009         vuint16_t       pattb;
1010         vuint16_t       reserved_66;
1011         vuint32_t       waveb;
1012         vuint32_t       perb;
1013
1014         vuint32_t       ccb[4];
1015 };
1016
1017 extern struct samd21_tcc samd21_tcc0;
1018 #define samd21_tcc0 (*(struct samd21_tcc *) 0x42002000)
1019
1020 extern struct samd21_tcc samd21_tcc1;
1021 #define samd21_tcc1 (*(struct samd21_tcc *) 0x42002400)
1022
1023 extern struct samd21_tcc samd21_tcc2;
1024 #define samd21_tcc2 (*(struct samd21_tcc *) 0x42002800)
1025
1026 #ifdef SAMD21E17D
1027 /* only on the samd21e17d */
1028 extern struct samd21_tcc samd21_tcc3;
1029 #define samd21_tcc3 (*(struct samd21_tcc *) 0x42006000)
1030 #endif
1031
1032 #define SAMD21_TCC_CTRLA_SWRST          0
1033 #define SAMD21_TCC_CTRLA_ENABLE         1
1034 #define SAMD21_TCC_CTRLA_RESOLUTION     5
1035 #define  SAMD21_TCC_CTRLA_RESOLUTION_NONE       0
1036 #define  SAMD21_TCC_CTRLA_RESOLUTION_DITH4      1
1037 #define  SAMD21_TCC_CTRLA_RESOLUTION_DITH5      2
1038 #define  SAMD21_TCC_CTRLA_RESOLUTION_DITH6      3
1039 #define SAMD21_TCC_CTRLA_PRESCALER      8
1040 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV1        0
1041 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV2        1
1042 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV4        2
1043 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV8        3
1044 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV16       4
1045 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV64       5
1046 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV256      6
1047 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV1024     7
1048 #define SAMD21_TCC_CTRLA_RUNSTDBY       11
1049 #define SAMD21_TCC_CTRLA_PRESYNC        12
1050 #define  SAMD21_TCC_CTRLA_PRESYNC_GCLK          0
1051 #define  SAMD21_TCC_CTRLA_PRESYNC_PRESC         1
1052 #define  SAMD21_TCC_CTRLA_PRESYNC_RESYNC        2
1053 #define SAMD21_TCC_CTRLA_ALOCK          14
1054 #define SAMD21_TCC_CTRLA_CPTEN(n)       (24 + (n))
1055
1056 #define SAMD21_TCC_CTRLB_DIR            0
1057 #define SAMD21_TCC_CTRLB_LUPD           1
1058 #define SAMD21_TCC_CTRLB_ONESHOT        2
1059 #define SAMD21_TCC_CTRLB_IDXCMD         3
1060 #define  SAMD21_TCC_CTRLB_IDXCMD_DISABLE        0
1061 #define  SAMD21_TCC_CTRLB_IDXCMD_SET            1
1062 #define  SAMD21_TCC_CTRLB_IDXCMD_CLEAR          2
1063 #define  SAMD21_TCC_CTRLB_IDXCMD_HOLD           3
1064 #define SAMD21_TCC_CTRLB_CMD            5
1065 #define  SAMD21_TCC_CTRLB_CMD_NONE              0
1066 #define  SAMD21_TCC_CTRLB_CMD_RETRIGGER         1
1067 #define  SAMD21_TCC_CTRLB_CMD_STOP              2
1068 #define  SAMD21_TCC_CTRLB_CMD_UPDATE            3
1069 #define  SAMD21_TCC_CTRLB_CMD_READSYNC          4
1070 #define  SAMD21_TCC_CTRLB_CMD_DMAOS             5
1071
1072 #define SAMD21_TCC_SYNCBUSY_SWRST       0
1073 #define SAMD21_TCC_SYNCBUSY_ENABLE      1
1074 #define SAMD21_TCC_SYNCBUSY_CTRLB       2
1075 #define SAMD21_TCC_SYNCBUSY_STATUS      3
1076 #define SAMD21_TCC_SYNCBUSY_COUNT       4
1077 #define SAMD21_TCC_SYNCBUSY_PATT        5
1078 #define SAMD21_TCC_SYNCBUSY_WAVE        6
1079 #define SAMD21_TCC_SYNCBUSY_PER         7
1080 #define SAMD21_TCC_SYNCBUSY_CC(x)       (8 + (x))
1081 #define SAMD21_TCC_SYNCBUSY_PATTB       16
1082 #define SAMD21_TCC_SYNCBUSY_WAVEB       17
1083 #define SAMD21_TCC_SYNCBUSY_PERB        18
1084 #define SAMD21_TCC_SYNCBUSY_CCB(x)      ((19 + (x))
1085
1086 #define SAMD21_TCC_DBGCTRL_FDDBD        2
1087 #define SAMD21_TCC_DBGCTRL_DBGRUN       0
1088
1089 #define SAMD21_TCC_EVCTRL_EVACTO        0
1090 #define SAMD21_TCC_EVCTRL_EVACT1        3
1091 #define SAMD21_TCC_EVCTRL_CNTSEL        6
1092 #define SAMD21_TCC_EVCTRL_OVFEO         8
1093 #define SAMD21_TCC_EVCTRL_TRGEO         9
1094 #define SAMD21_TCC_EVCTRL_CNTEO         10
1095 #define SAMD21_TCC_EVCTRL_TCINV(x)      (12 + (x))
1096 #define SAMD21_TCC_EVCTRL_MCEI(x)       (16 + (x))
1097 #define SAMD21_TCC_EVCTRL_MCEO(x)       (24 + (x))
1098
1099 #define SAMD21_TCC_INTFLAG_OVF          0
1100 #define SAMD21_TCC_INTFLAG_TRG          1
1101 #define SAMD21_TCC_INTFLAG_CNT          2
1102 #define SAMD21_TCC_INTFLAG_ERR          3
1103 #define SAMD21_TCC_INTFLAG_UFS          10
1104 #define SAMD21_TCC_INTFLAG_DFS          11
1105 #define SAMD21_TCC_INTFLAG_FAULTA       12
1106 #define SAMD21_TCC_INTFLAG_FAULTB       13
1107 #define SAMD21_TCC_INTFLAG_FAULT0       14
1108 #define SAMD21_TCC_INTFLAG_FAULT1       15
1109 #define SAMD21_TCC_INTFLAG_MC(x)        (16 + (x))
1110
1111 #define SAMD21_TCC_WAVE_WAVEGEN         0
1112 #define  SAMD21_TCC_WAVE_WAVEGEN_NFRQ           0
1113 #define  SAMD21_TCC_WAVE_WAVEGEN_MFRQ           1
1114 #define  SAMD21_TCC_WAVE_WAVEGEN_NPWM           2
1115 #define  SAMD21_TCC_WAVE_WAVEGEN_DSCRITICAL     4
1116 #define  SAMD21_TCC_WAVE_WAVEGEN_DSBOTTOM       5
1117 #define  SAMD21_TCC_WAVE_WAVEGEN_DSBOTH         6
1118 #define  SAMD21_TCC_WAVE_WAVEGEN_DSTOP          7
1119 #define SAMD21_TCC_WAVE_RAMP            4
1120 #define SAMD21_TCC_WAVE_CIPEREN         7
1121 #define SAMD21_TCC_WAVE_CCCEN(x)        (8 + (x))
1122 #define SAMD21_TCC_WAVE_POL(x)          (16 + (x))
1123 #define SAMD21_TCC_WAVE_SWAP(x)         (24 + (x))
1124
1125 /* USB */
1126
1127 struct samd21_usb {
1128         vuint8_t        ctrla;
1129         vuint8_t        reserved_01;
1130         vuint8_t        syncbusy;
1131         vuint8_t        qosctrl;
1132
1133         vuint32_t       reserved_04;
1134         vuint16_t       ctrlb;
1135         vuint8_t        dadd;
1136         vuint8_t        reserved_0b;
1137         vuint8_t        status;
1138         vuint8_t        fsmstatus;
1139         vuint16_t       reserved_0e;
1140
1141         vuint16_t       fnum;
1142         vuint16_t       reserved_12;
1143         vuint16_t       intenclr;
1144         vuint16_t       reserved_16;
1145         vuint16_t       intenset;
1146         vuint16_t       reserved_1a;
1147         vuint16_t       intflag;
1148         vuint16_t       reserved_1e;
1149
1150         vuint16_t       epintsmry;
1151         vuint16_t       reserved_22;
1152
1153         vuint32_t       descadd;
1154         vuint16_t       padcal;
1155         uint8_t         reserved_2a[0x100 - 0x2a];
1156
1157         struct {
1158                 vuint8_t        epcfg;
1159                 vuint8_t        reserved_01;
1160                 vuint8_t        reserved_02;
1161                 vuint8_t        binterval;
1162                 vuint8_t        epstatusclr;
1163                 vuint8_t        epstatusset;
1164                 vuint8_t        epstatus;
1165                 vuint8_t        epintflag;
1166                 vuint8_t        epintenclr;
1167                 vuint8_t        epintenset;
1168                 vuint8_t        reserved_0a[0x20 - 0x0a];
1169         } ep[8];
1170 };
1171
1172 extern struct samd21_usb samd21_usb;
1173
1174 #define samd21_usb (*(struct samd21_usb *) 0x41005000)
1175
1176 #define SAMD21_USB_CTRLA_SWRST          0
1177 #define SAMD21_USB_CTRLA_ENABLE         1
1178 #define SAMD21_USB_CTRLA_RUNSTDBY       2
1179 #define SAMD21_USB_CTRLA_MODE           7
1180
1181 #define SAMD21_USB_SYNCBUSY_SWRST       0
1182 #define SAMD21_USB_SYNCBUSY_ENABLE      1
1183
1184 #define SAMD21_USB_QOSCTRL_CQOS         0
1185 #define SAMD21_USB_QOSCTRL_DQOS         2
1186
1187 #define SAMD21_USB_CTRLB_DETACH         0
1188 #define SAMD21_USB_CTRLB_UPRSM          1
1189 #define SAMD21_USB_CTRLB_SPDCONF        2
1190 #define  SAMD21_USB_CTRLB_SPDCONF_FS            0
1191 #define  SAMD21_USB_CTRLB_SPDCONF_LS            1
1192 #define  SAMD21_USB_CTRLB_SPDCONF_MASK          0x3
1193 #define SAMD21_USB_CTRLB_NREPLY         4
1194 #define SAMD21_USB_CTRLB_GNAK           9
1195 #define SAMD21_USB_CTRLB_LPMHDSK        10
1196 #define  SAMD21_USB_CTRLB_LPMHDSK_NONE          0
1197 #define  SAMD21_USB_CTRLB_LPMHDSK_ACK           1
1198 #define  SAMD21_USB_CTRLB_LPMHDSK_NYET          2
1199 #define  SAMD21_USB_CTRLB_LPMHDSK_MASK          3
1200
1201 #define SAMD21_USB_DADD_DADD            0
1202 #define SAMD21_USB_DADD_ADDEN           7
1203
1204 #define SAMD21_USB_STATUS_SPEED         2
1205 #define SAMD21_USB_STATUS_LINESTATE     6
1206 #define SAMD21_USB_FNUM_MFNUM           0
1207 #define SAMD21_USB_FNUM_FNUM            3
1208 #define SAMD21_USB_FNUM_FNCERR          15
1209 #define SAMD21_USB_INTFLAG_SUSPEND      0
1210 #define SAMD21_USB_INTFLAG_SOF          2
1211 #define SAMD21_USB_INTFLAG_EORST        3
1212 #define SAMD21_USB_INTFLAG_WAKEUP       4
1213 #define SAMD21_USB_INTFLAG_EORSM        5
1214 #define SAMD21_USB_INTFLAG_UPRSM        6
1215 #define SAMD21_USB_INTFLAG_RAMACER      7
1216 #define SAMD21_USB_INTFLAG_LPMNYET      8
1217 #define SAMD21_USB_INTFLAG_LPMSUSP      9
1218
1219 #define SAMD21_USB_PADCAL_TRANSP        0
1220 #define SAMD21_USB_PADCAL_TRANSN        6
1221 #define SAMD21_USB_PADCAL_TRIM          12
1222
1223 #define SAMD21_USB_EP_EPCFG_EP_TYPE_OUT         0
1224 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_DISABLED       0
1225 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_CONTROL        1
1226 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_ISOCHRONOUS    2
1227 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_BULK           3
1228 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_INTERRUPT      4
1229 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_DUAL_BANK      5
1230 #define SAMD21_USB_EP_EPCFG_EP_TYPE_IN          4
1231 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_DISABLED        0
1232 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_CONTROL 1
1233 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_ISOCHRONOUS     2
1234 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_BULK            3
1235 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_INTERRUPT       4
1236 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_DUAL_BANK       5
1237
1238 #define SAMD21_USB_EP_EPSTATUS_DTGLOUT                  0
1239 #define SAMD21_USB_EP_EPSTATUS_DTGLIN                   1
1240 #define SAMD21_USB_EP_EPSTATUS_CURBK                    2
1241 #define SAMD21_USB_EP_EPSTATUS_STALLRQ0                 4
1242 #define SAMD21_USB_EP_EPSTATUS_STALLRQ1                 5
1243 #define SAMD21_USB_EP_EPSTATUS_BK0RDY                   6
1244 #define SAMD21_USB_EP_EPSTATUS_BK1RDY                   7
1245
1246 #define SAMD21_USB_EP_EPINTFLAG_TRCPT0                  0
1247 #define SAMD21_USB_EP_EPINTFLAG_TRCPT1                  1
1248 #define SAMD21_USB_EP_EPINTFLAG_TRFAIL0                 2
1249 #define SAMD21_USB_EP_EPINTFLAG_TRFAIL1                 3
1250 #define SAMD21_USB_EP_EPINTFLAG_RXSTP                   4
1251 #define SAMD21_USB_EP_EPINTFLAG_STALL                   5
1252
1253 struct samd21_usb_desc_bank {
1254         vuint32_t       addr;
1255         vuint32_t       pcksize;
1256         vuint16_t       extreg;
1257         vuint8_t        status_bk;
1258         vuint8_t        reserved_0b;
1259         vuint32_t       reserved_0c;
1260 };
1261
1262 struct samd21_usb_desc {
1263         struct samd21_usb_desc_bank bank[2];
1264 };
1265
1266 extern struct samd21_usb_desc   samd21_usb_desc[8];
1267
1268 #define SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT              0
1269 #define  SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT_MASK                0x3fffU
1270 #define SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE       14
1271 #define  SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE_MASK         0x3fffU
1272 #define SAMD21_USB_DESC_PCKSIZE_SIZE                    28
1273 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_8                         0
1274 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_16                        1
1275 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_32                        2
1276 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_64                        3
1277 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_128                       4
1278 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_256                       5
1279 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_512                       6
1280 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_1023                      7
1281 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_MASK                      7U
1282 #define SAMD21_USB_DESC_PCKSIZE_AUTO_ZLP                31
1283
1284 static inline uint16_t
1285 samd21_usb_desc_get_byte_count(uint8_t ep, uint8_t bank)
1286 {
1287         return ((samd21_usb_desc[ep].bank[bank].pcksize >> SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT) &
1288                 SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT_MASK);
1289 }
1290
1291 static inline void
1292 samd21_usb_desc_set_byte_count(uint8_t ep, uint8_t bank, uint32_t count)
1293 {
1294         uint32_t pcksize = samd21_usb_desc[ep].bank[bank].pcksize;
1295
1296         pcksize &= ~(SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT_MASK << SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT);
1297         pcksize &= ~(SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE_MASK << SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE);
1298         pcksize |= (count << SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT);
1299         samd21_usb_desc[ep].bank[bank].pcksize = pcksize;
1300 }
1301
1302 static inline void
1303 samd21_usb_desc_set_size(uint8_t ep, uint8_t bank, uint32_t size)
1304 {
1305         uint32_t pcksize = samd21_usb_desc[ep].bank[bank].pcksize;
1306
1307         pcksize &= ~(SAMD21_USB_DESC_PCKSIZE_SIZE_MASK << SAMD21_USB_DESC_PCKSIZE_SIZE);
1308
1309         uint32_t size_bits = 0;
1310         switch (size) {
1311         case 8: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_8; break;
1312         case 16: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_16; break;
1313         case 32: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_32; break;
1314         case 64: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_64; break;
1315         case 128: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_128; break;
1316         case 256: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_256; break;
1317         case 512: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_512; break;
1318         case 1023: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_1023; break;
1319         }
1320         pcksize |= (size_bits << SAMD21_USB_DESC_PCKSIZE_SIZE);
1321         samd21_usb_desc[ep].bank[bank].pcksize = pcksize;
1322 }
1323
1324 static inline void
1325 samd21_usb_ep_set_ready(uint8_t ep, uint8_t bank)
1326 {
1327         samd21_usb.ep[ep].epstatusset = (1 << (SAMD21_USB_EP_EPSTATUS_BK0RDY + bank));
1328         samd21_usb.ep[ep].epintflag = (1 << (SAMD21_USB_EP_EPINTFLAG_TRFAIL0 + bank));
1329 }
1330
1331 static inline void
1332 samd21_usb_ep_clr_ready(uint8_t ep, uint8_t bank)
1333 {
1334         samd21_usb.ep[ep].epstatusclr = (1 << (SAMD21_USB_EP_EPSTATUS_BK0RDY + bank));
1335 }
1336
1337 static inline uint8_t
1338 samd21_usb_ep_ready(uint8_t ep)
1339 {
1340         return (samd21_usb.ep[ep].epstatus >> SAMD21_USB_EP_EPSTATUS_BK0RDY) & 3;
1341 }
1342
1343 static inline uint8_t
1344 samd21_usb_ep_curbk(uint8_t ep)
1345 {
1346         return (samd21_usb.ep[ep].epstatus >> SAMD21_USB_EP_EPSTATUS_CURBK) & 1;
1347 }
1348
1349 /* sercom */
1350
1351 struct samd21_sercom {
1352         vuint32_t       ctrla;
1353         vuint32_t       ctrlb;
1354         vuint32_t       reserved_08;
1355         vuint16_t       baud;
1356         vuint8_t        rxpl;
1357         vuint8_t        reserved_0f;
1358
1359         vuint32_t       reserved_10;
1360         vuint8_t        intenclr;
1361         vuint8_t        reserved_15;
1362         vuint8_t        intenset;
1363         vuint8_t        reserved_17;
1364         vuint8_t        intflag;
1365         vuint8_t        reserved_19;
1366         vuint16_t       status;
1367         vuint32_t       syncbusy;
1368
1369         vuint32_t       reserved_20;
1370         vuint32_t       addr;
1371         vuint16_t       data;
1372         vuint16_t       reserved_2a;
1373         vuint32_t       reserved_2c;
1374
1375         vuint8_t        dbgctrl;
1376         vuint8_t        reserved_31;
1377         vuint16_t       reserved_32;
1378         vuint16_t       fifospace;
1379         vuint16_t       fifoptr;
1380 };
1381
1382 extern struct samd21_sercom samd21_sercom0;
1383 extern struct samd21_sercom samd21_sercom1;
1384 extern struct samd21_sercom samd21_sercom2;
1385 extern struct samd21_sercom samd21_sercom3;
1386 extern struct samd21_sercom samd21_sercom4;
1387 extern struct samd21_sercom samd21_sercom5;
1388
1389 #define SAMD21_NUM_SERCOM       6
1390
1391 #define samd21_sercom0  (*(struct samd21_sercom *) 0x42000800)
1392 #define samd21_sercom1  (*(struct samd21_sercom *) 0x42000c00)
1393 #define samd21_sercom2  (*(struct samd21_sercom *) 0x42001000)
1394 #define samd21_sercom3  (*(struct samd21_sercom *) 0x42001400)
1395 #define samd21_sercom4  (*(struct samd21_sercom *) 0x42001800)
1396 #define samd21_sercom5  (*(struct samd21_sercom *) 0x42001c00)
1397
1398 #define SAMD21_SERCOM_CTRLA_SWRST       0
1399 #define SAMD21_SERCOM_CTRLA_ENABLE      1
1400 #define SAMD21_SERCOM_CTRLA_MODE        2
1401 # define SAMD21_SERCOM_CTRLA_MODE_USART         1
1402 # define SAMD21_SERCOM_CTRLA_MODE_SPI_CLIENT    2
1403 # define SAMD21_SERCOM_CTRLA_MODE_SPI_HOST      3
1404 # define SAMD21_SERCOM_CTRLA_MODE_I2C_CLIENT    4
1405 # define SAMD21_SERCOM_CTRLA_MODE_I2C_HOST      5
1406
1407 #define SAMD21_SERCOM_CTRLA_RUNSTDBY    7
1408
1409 /* USART mode */
1410 #define SAMD21_SERCOM_CTRLA_IBON        8
1411 #define SAMD21_SERCOM_CTRLA_SAMPR       13
1412 #define SAMD21_SERCOM_CTRLA_TXPO        16
1413 #define SAMD21_SERCOM_CTRLA_RXPO        20
1414 #define SAMD21_SERCOM_CTRLA_SAMPA       22
1415 #define SAMD21_SERCOM_CTRLA_FORM        24
1416 #define SAMD21_SERCOM_CTRLA_CMODE       28
1417 #define SAMD21_SERCOM_CTRLA_CPOL        29
1418 #define SAMD21_SERCOM_CTRLA_DORD        30
1419
1420 /* I2C controller mode */
1421 #define SAMD21_SERCOM_CTRLA_PINOUT      16
1422 #define SAMD21_SERCOM_CTRLA_SDAHOLD     20
1423 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_DIS        0
1424 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_75NS       1
1425 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_450NS      2
1426 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_600NS      3
1427 #define SAMD21_SERCOM_CTRLA_MEXTTOEN    22
1428 #define SAMD21_SERCOM_CTRLA_SEXTTOEN    23
1429 #define SAMD21_SERCOM_CTRLA_SPEED       24
1430 #define  SAMD21_SERCOM_CTRLA_SPEED_STANDARD     0
1431 #define  SAMD21_SERCOM_CTRLA_SPEED_FAST         1
1432 #define  SAMD21_SERCOM_CTRLA_SPEED_HIGH         2
1433 #define SAMD21_SERCOM_CTRLA_SCLSM       27
1434 #define SAMD21_SERCOM_CTRLA_INACTOUT    28
1435 #define  SAMD21_SERCOM_CTRLA_INACTOUT_DIS       0
1436 #define  SAMD21_SERCOM_CTRLA_INACTOUT_55US      1
1437 #define  SAMD21_SERCOM_CTRLA_INACTOUT_105US     2
1438 #define  SAMD21_SERCOM_CTRLA_INACTOUT_205US     3
1439 #define SAMD21_SERCOM_CTRLA_LOWTOUT     30
1440
1441 /* SPI controller mode */
1442 #define SAMD21_SERCOM_CTRLA_DOPO        16
1443 #define SAMD21_SERCOM_CTRLA_DIPO        20
1444 #define SAMD21_SERCOM_CTRLA_FORM        24
1445 #define SAMD21_SERCOM_CTRLA_CPHA        28
1446 #define SAMD21_SERCOM_CTRLA_CPOL        29
1447 #define SAMD21_SERCOM_CTRLA_DORD        30
1448 #define  SAMD21_SERCOM_CTRLA_DORD_LSB   1
1449 #define  SAMD21_SERCOM_CTRLA_DORD_MSB   0
1450
1451 /* USART mode */
1452 #define SAMD21_SERCOM_CTRLB_CHSIZE      0
1453 #define SAMD21_SERCOM_CTRLB_SBMODE      6
1454 #define SAMD21_SERCOM_CTRLB_COLDEN      8
1455 #define SAMD21_SERCOM_CTRLB_SFDE        9
1456 #define SAMD21_SERCOM_CTRLB_ENC         10
1457 #define SAMD21_SERCOM_CTRLB_PMODE       13
1458 #define SAMD21_SERCOM_CTRLB_TXEN        16
1459 #define SAMD21_SERCOM_CTRLB_RXEN        17
1460 #define SAMD21_SERCOM_CTRLB_FIFOCLR     22
1461
1462 /* I2C mode */
1463 #define SAMD21_SERCOM_CTRLB_SMEN        8
1464 #define SAMD21_SERCOM_CTRLB_QCEN        9
1465 #define SAMD21_SERCOM_CTRLB_CMD         16
1466 #define  SAMD21_SERCOM_CTRLB_CMD_NOP            0
1467 #define  SAMD21_SERCOM_CTRLB_CMD_START          1
1468 #define  SAMD21_SERCOM_CTRLB_CMD_READ           2
1469 #define  SAMD21_SERCOM_CTRLB_CMD_STOP           3
1470 #define SAMD21_SERCOM_CTRLB_ACKACT      18
1471 #define  SAMD21_SERCOM_CTRLB_ACKACT_ACK         0
1472 #define  SAMD21_SERCOM_CTRLB_ACKACT_NACK        1
1473 #define SAMD21_SERCOM_CTRLB_FIFOCLR     22
1474
1475 /* SPI mode */
1476 #define SAMD21_SERCOM_CTRLB_CHSIZE      0
1477 # define SAMD21_SERCOM_CTRLB_CHSIZE_8           0
1478 #define SAMD21_SERCOM_CTRLB_PLOADEN     6
1479 #define SAMD21_SERCOM_CTRLB_SSDE        9
1480 #define SAMD21_SERCOM_CTRLB_MSSEN       13
1481 #define SAMD21_SERCOM_CTRLB_AMODE       14
1482 #define SAMD21_SERCOM_CTRLB_RXEN        17
1483
1484 /* USART mode */
1485 #define SAMD21_SERCOM_INTFLAG_DRE       0
1486 #define SAMD21_SERCOM_INTFLAG_TXC       1
1487 #define SAMD21_SERCOM_INTFLAG_RXC       2
1488 #define SAMD21_SERCOM_INTFLAG_RXS       3
1489 #define SAMD21_SERCOM_INTFLAG_CTSIC     4
1490 #define SAMD21_SERCOM_INTFLAG_RXBRK     5
1491 #define SAMD21_SERCOM_INTFLAG_ERROR     7
1492
1493 /* I2C mode */
1494 #define SAMD21_SERCOM_INTFLAG_ERROR     7
1495 #define SAMD21_SERCOM_INTFLAG_RXFF      4
1496 #define SAMD21_SERCOM_INTFLAG_TXFE      3
1497 #define SAMD21_SERCOM_INTFLAG_SB        1
1498 #define SAMD21_SERCOM_INTFLAG_MB        0
1499
1500 /* SPI mode */
1501 #define SAMD21_SERCOM_INTFLAG_SSL       3
1502
1503 #define SAMD21_SERCOM_INTENCLR_DRE      0
1504 #define SAMD21_SERCOM_INTENCLR_TXC      1
1505 #define SAMD21_SERCOM_INTENCLR_RXC      2
1506 #define SAMD21_SERCOM_INTENCLR_RXS      3
1507 #define SAMD21_SERCOM_INTENCLR_CTSIC    4
1508 #define SAMD21_SERCOM_INTENCLR_RXBRK    5
1509 #define SAMD21_SERCOM_INTENCLR_ERROR    7
1510
1511 #define SAMD21_SERCOM_STATUS_PERR       0
1512 #define SAMD21_SERCOM_STATUS_FERR       1
1513 #define SAMD21_SERCOM_STATUS_BUFOVF     2
1514 #define SAMD21_SERCOM_STATUS_CTS        3
1515 #define SAMD21_SERCOM_STATUS_ISF        4
1516 #define SAMD21_SERCOM_STATUS_COLL       5
1517 #define SAMD21_SERCOM_STATUS_TXE        6
1518
1519 #define SAMD21_SERCOM_SYNCBUSY_SWRST    0
1520 #define SAMD21_SERCOM_SYNCBUSY_ENABLE   1
1521 #define SAMD21_SERCOM_SYNCBUSY_CTRLB    2
1522 #define SAMD21_SERCOM_SYNCBUSY_SYSOP    2
1523
1524 #define SAMD21_SERCOM_ADDR_ADDR         0
1525 #define SAMD21_SERCOM_ADDR_LENEN        13
1526 #define SAMD21_SERCOM_ADDR_HS           14
1527 #define SAMD21_SERCOM_ADDR_TENBITEN     15
1528 #define SAMD21_SERCOM_ADDR_LEN          16
1529
1530 #define SAMD21_SERCOM_DBGCTRL_DBGSTOP   0
1531
1532 #define SAMD21_SERCOM_FIFOSPACE_TXSPACE 0
1533 #define  SAMD21_SERCOM_FIFOSPACE_TXSPACE_MASK   0x1f
1534 #define SAMD21_SERCOM_FIFOSPACE_RXSPACE 8
1535 #define  SAMD21_SERCOM_FIFOSPACE_RXSPACE_MASK   0x1f
1536
1537 #define SAMD21_SERCOM_FIFOPTR_CPUWRPTR  0
1538 #define  SAMD21_SERCOM_FIFOPTR_CPUWRPTR_MASK    0xf
1539 #define SAMD21_SERCOM_FIFOPTR_CPURDPTR  8
1540 #define  SAMD21_SERCOM_FIFOPTR_CPURDPTR_MASK    0xf
1541
1542 /* The SYSTICK starts at 0xe000e010 */
1543 struct samd21_systick {
1544         vuint32_t       csr;
1545         vuint32_t       rvr;
1546         vuint32_t       cvr;
1547         vuint32_t       calib;
1548 };
1549
1550 extern struct samd21_systick samd21_systick;
1551
1552 #define samd21_systick (*(struct samd21_systick *) 0xe000e010)
1553
1554 #define SAMD21_SYSTICK_CSR_ENABLE       0
1555 #define SAMD21_SYSTICK_CSR_TICKINT      1
1556 #define SAMD21_SYSTICK_CSR_CLKSOURCE    2
1557 #define  SAMD21_SYSTICK_CSR_CLKSOURCE_EXTERNAL          0
1558 #define  SAMD21_SYSTICK_CSR_CLKSOURCE_HCLK_8            1
1559 #define SAMD21_SYSTICK_CSR_COUNTFLAG    16
1560
1561 #define SAMD21_SYSTICK_PRI              15
1562
1563 /* The NVIC starts at 0xe000e100, so add that to the offsets to find the absolute address */
1564
1565 struct samd21_nvic {
1566         vuint32_t       iser;           /* 0x000 0xe000e100 Set Enable Register */
1567
1568         uint8_t         _unused020[0x080 - 0x004];
1569
1570         vuint32_t       icer;           /* 0x080 0xe000e180 Clear Enable Register */
1571
1572         uint8_t         _unused0a0[0x100 - 0x084];
1573
1574         vuint32_t       ispr;           /* 0x100 0xe000e200 Set Pending Register */
1575
1576         uint8_t         _unused120[0x180 - 0x104];
1577
1578         vuint32_t       icpr;           /* 0x180 0xe000e280 Clear Pending Register */
1579
1580         uint8_t         _unused1a0[0x300 - 0x184];
1581
1582         vuint32_t       ipr[8];         /* 0x300 0xe000e400 Priority Register */
1583 };
1584
1585 extern struct samd21_nvic samd21_nvic;
1586
1587 #define samd21_nvic (*(struct samd21_nvic *) 0xe000e100)
1588
1589 #define SAMD21_NVIC_ISR_PM_POS          0
1590 #define SAMD21_NVIC_ISR_SYSCTRL_POS     1
1591 #define SAMD21_NVIC_ISR_WDT_POS         2
1592 #define SAMD21_NVIC_ISR_RTC_POS         3
1593 #define SAMD21_NVIC_ISR_EIC_POS         4
1594 #define SAMD21_NVIC_ISR_NVMCTRL_POS     5
1595 #define SAMD21_NVIC_ISR_DMAC_POS        6
1596 #define SAMD21_NVIC_ISR_USB_POS         7
1597 #define SAMD21_NVIC_ISR_EVSYS_POS       8
1598 #define SAMD21_NVIC_ISR_SERCOM0_POS     9
1599 #define SAMD21_NVIC_ISR_SERCOM1_POS     10
1600 #define SAMD21_NVIC_ISR_SERCOM2_POS     11
1601 #define SAMD21_NVIC_ISR_SERCOM3_POS     12
1602 #define SAMD21_NVIC_ISR_SERCOM4_POS     13
1603 #define SAMD21_NVIC_ISR_SERCOM5_POS     14
1604 #define SAMD21_NVIC_ISR_TCC0_POS        15
1605 #define SAMD21_NVIC_ISR_TCC1_POS        16
1606 #define SAMD21_NVIC_ISR_TCC2_POS        17
1607 #define SAMD21_NVIC_ISR_TC3_POS         18
1608 #define SAMD21_NVIC_ISR_TC4_POS         19
1609 #define SAMD21_NVIC_ISR_TC5_POS         20
1610 #define SAMD21_NVIC_ISR_TC6_POS         21
1611 #define SAMD21_NVIC_ISR_TC7_POS         22
1612 #define SAMD21_NVIC_ISR_ADC_POS         23
1613 #define SAMD21_NVIC_ISR_AC_POS          24
1614 #define SAMD21_NVIC_ISR_DAC_POS         25
1615 #define SAMD21_NVIC_ISR_PTC_POS         26
1616 #define SAMD21_NVIC_ISR_I2S_POS         27
1617 #define SAMD21_NVIC_ISR_AC1_POS         28
1618 #define SAMD21_NVIC_ISR_TCC3_POS        29
1619
1620 #define IRQ_MASK(irq)   (1 << (irq))
1621 #define IRQ_BOOL(v,irq) (((v) >> (irq)) & 1)
1622
1623 static inline void
1624 samd21_nvic_set_enable(int irq) {
1625         samd21_nvic.iser = IRQ_MASK(irq);
1626 }
1627
1628 static inline void
1629 samd21_nvic_clear_enable(int irq) {
1630         samd21_nvic.icer = IRQ_MASK(irq);
1631 }
1632
1633 static inline int
1634 samd21_nvic_enabled(int irq) {
1635         return IRQ_BOOL(samd21_nvic.iser, irq);
1636 }
1637
1638 static inline void
1639 samd21_nvic_set_pending(int irq) {
1640         samd21_nvic.ispr = IRQ_MASK(irq);
1641 }
1642
1643 static inline void
1644 samd21_nvic_clear_pending(int irq) {
1645         samd21_nvic.icpr = IRQ_MASK(irq);
1646 }
1647
1648 static inline int
1649 samd21_nvic_pending(int irq) {
1650         return IRQ_BOOL(samd21_nvic.ispr, irq);
1651 }
1652
1653 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
1654 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
1655 #define IRQ_PRIO_MASK(irq)      (0xffU << IRQ_PRIO_BIT(irq))
1656
1657 static inline void
1658 samd21_nvic_set_priority(int irq, uint8_t prio) {
1659         int             n = IRQ_PRIO_REG(irq);
1660         uint32_t        v;
1661
1662         v = samd21_nvic.ipr[n];
1663         v &= ~IRQ_PRIO_MASK(irq);
1664         v |= (prio) << IRQ_PRIO_BIT(irq);
1665         samd21_nvic.ipr[n] = v;
1666 }
1667
1668 static inline uint8_t
1669 samd21_nvic_get_priority(int irq) {
1670         return (samd21_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
1671 }
1672
1673
1674
1675 /* Cortex M0+ SCB */
1676
1677 struct samd21_scb {
1678         vuint32_t       cpuid;
1679         vuint32_t       icsr;
1680         vuint32_t       vtor;
1681         vuint32_t       aircr;
1682
1683         vuint32_t       scr;
1684         vuint32_t       ccr;
1685         vuint32_t       shpr1;
1686         vuint32_t       shpr2;
1687
1688         vuint32_t       shpr3;
1689         vuint32_t       shcrs;
1690         vuint32_t       cfsr;
1691         vuint32_t       hfsr;
1692
1693         uint32_t        unused_30;
1694         vuint32_t       mmfar;
1695         vuint32_t       bfar;
1696 };
1697
1698 extern struct samd21_scb samd21_scb;
1699
1700 #define samd21_scb (*(struct samd21_scb *) 0xe000ed00)
1701
1702 #define SAMD21_SCB_AIRCR_VECTKEY        16
1703 #define  SAMD21_SCB_AIRCR_VECTKEY_KEY           0x05fa
1704 #define SAMD21_SCB_AIRCR_PRIGROUP       8
1705 #define SAMD21_SCB_AIRCR_SYSRESETREQ    2
1706 #define SAMD21_SCB_AIRCR_VECTCLRACTIVE  1
1707 #define SAMD21_SCB_AIRCR_VECTRESET      0
1708
1709 /* The NVM Calibration and auxiliary space starts at 0x00800000 */
1710
1711 struct samd21_aux0 {
1712         vuint64_t       userrow;
1713 };
1714
1715 extern struct samd21_aux0 samd21_aux0;
1716
1717 #define samd21_aux0 (*(struct samd21_aux0 *) 0x00804000)
1718
1719 #define SAMD21_AUX0_USERROW_BOOTPROT    0
1720 #define SAMD21_AUX0_USERROW_EEPROM              4
1721 #define SAMD21_AUX0_USERROW_BOD33_LEVEL 8
1722 #define SAMD21_AUX0_USERROW_BOD33_ENABLE        14
1723 #define SAMD21_AUX0_USERROW_BOD33_ACTION        15
1724 #define SAMD21_AUX0_USERROW_WDT_ENABLE  25
1725 #define SAMD21_AUX0_USERROW_WDT_ALWAYS_ON       26
1726 #define SAMD21_AUX0_USERROW_WDT_PERIOD  27
1727 #define SAMD21_AUX0_USERROW_WDT_WINDOW  31
1728 #define SAMD21_AUX0_USERROW_WDT_EWOFFSET        35
1729 #define SAMD21_AUX0_USERROW_WDT_WEN             39
1730 #define SAMD21_AUX0_USERROW_BOD33_HYST  40
1731 #define SAMD21_AUX0_USERROW_LOCK                48
1732
1733 struct samd21_aux1 {
1734         vuint64_t       reserved_00;
1735         vuint64_t       device_config;
1736
1737         vuint64_t       reserved_10;
1738         vuint64_t       reserved_18;
1739
1740         vuint64_t       calibration;
1741         vuint64_t       reserved_28;
1742 };
1743
1744 extern struct samd21_aux1 samd21_aux1;
1745
1746 #define samd21_aux1 (*(struct samd21_aux1 *) 0x00806000)
1747
1748 #define SAMD21_AUX1_CALIBRATION_ADC_LINEARITY   27
1749 #define  SAMD21_AUX1_CALIBRATION_ADC_LINEARITY_MASK     0xff
1750 #define SAMD21_AUX1_CALIBRATION_ADC_BIASCAL     35
1751 #define  SAMD21_AUX1_CALIBRATION_ADC_BIASCAL_MASK       0x7
1752 #define SAMD21_AUX1_CALIBRATION_OSC32K_CAL      38
1753 #define SAMD21_AUX1_CALIBRATION_USB_TRANSN      45
1754 #define  SAMD21_AUX1_CALIBRATION_USB_TRANSN_MASK        0x1f
1755 #define SAMD21_AUX1_CALIBRATION_USB_TRANSP      50
1756 #define  SAMD21_AUX1_CALIBRATION_USB_TRANSP_MASK        0x1f
1757 #define SAMD21_AUX1_CALIBRATION_USB_TRIM        55
1758 #define  SAMD21_AUX1_CALIBRATION_USB_TRIM_MASK          0x07
1759 #define SAMD21_AUX1_CALIBRATION_DFLL48M_COARSE_CAL      58
1760 #define  SAMD21_AUX1_CALIBRATION_DFLL48M_COARSE_CAL_MASK        0x3f
1761
1762 struct samd21_serial {
1763         vuint32_t       reserved_00;
1764         vuint32_t       reserved_04;
1765         vuint32_t       reserved_08;
1766         vuint32_t       word0;
1767
1768         vuint32_t       reserved_10;
1769         vuint32_t       reserved_14;
1770         vuint32_t       reserved_18;
1771         vuint32_t       reserved_1c;
1772
1773         vuint32_t       reserved_20;
1774         vuint32_t       reserved_24;
1775         vuint32_t       reserved_28;
1776         vuint32_t       reserved_2c;
1777
1778         vuint32_t       reserved_30;
1779         vuint32_t       reserved_34;
1780         vuint32_t       reserved_38;
1781         vuint32_t       reserved_3c;
1782
1783         vuint32_t       word1;
1784         vuint32_t       word2;
1785         vuint32_t       word3;
1786         vuint32_t       reserved_4c;
1787 };
1788
1789 extern struct samd21_serial samd21_serial;
1790
1791 #define samd21_serial (*(struct samd21_serial *) 0x0080a000)
1792
1793 static inline void
1794 samd21_gclk_wait_sync(void)
1795 {
1796         while (samd21_gclk.status & (1 << SAMD21_GCLK_STATUS_SYNCBUSY))
1797                 ;
1798 }
1799
1800 static inline void
1801 samd21_dfll_wait_sync(void)
1802 {
1803         while ((samd21_sysctrl.pclksr & (1 << SAMD21_SYSCTRL_PCLKSR_DFLLRDY)) == 0)
1804                 ;
1805 }
1806
1807 static inline void
1808 samd21_gclk_gendiv(uint32_t id, uint32_t div)
1809 {
1810         if (div == 1)
1811                 div = 0;
1812         samd21_gclk.gendiv = ((id << SAMD21_GCLK_GENDIV_ID) |
1813                               (div << SAMD21_GCLK_GENDIV_DIV));
1814         samd21_gclk_wait_sync();
1815 }
1816
1817 static inline void
1818 samd21_gclk_genctrl(uint32_t src, uint32_t id)
1819 {
1820         samd21_gclk.genctrl = ((id << SAMD21_GCLK_GENCTRL_ID) |
1821                                (src << SAMD21_GCLK_GENCTRL_SRC) |
1822                                (0 << SAMD21_GCLK_GENCTRL_OE) |
1823                                (1 << SAMD21_GCLK_GENCTRL_GENEN));
1824         samd21_gclk_wait_sync();
1825 }
1826
1827 static inline void
1828 samd21_gclk_clkctrl(uint32_t gen, uint32_t id)
1829 {
1830         samd21_gclk.clkctrl = (uint16_t) ((gen << SAMD21_GCLK_CLKCTRL_GEN) |
1831                                           (id << SAMD21_GCLK_CLKCTRL_ID) |
1832                                           (1U << SAMD21_GCLK_CLKCTRL_CLKEN));
1833         samd21_gclk_wait_sync();
1834 }
1835
1836 #define isr_decl(name) \
1837         void samd21_ ## name ## _isr(void)
1838
1839 isr_decl(halt);
1840 isr_decl(ignore);
1841 isr_decl(nmi);
1842 isr_decl(hardfault);
1843 isr_decl(memmanage);
1844 isr_decl(busfault);
1845 isr_decl(usagefault);
1846 isr_decl(svc);
1847 isr_decl(debugmon);
1848 isr_decl(pendsv);
1849 isr_decl(systick);
1850 isr_decl(pm);           /* IRQ0 */
1851 isr_decl(sysctrl);
1852 isr_decl(wdt);
1853 isr_decl(rtc);
1854 isr_decl(eic);
1855 isr_decl(nvmctrl);
1856 isr_decl(dmac);
1857 isr_decl(usb);
1858 isr_decl(evsys);
1859 isr_decl(sercom0);
1860 isr_decl(sercom1);
1861 isr_decl(sercom2);
1862 isr_decl(sercom3);
1863 isr_decl(sercom4);
1864 isr_decl(sercom5);
1865 isr_decl(tcc0);
1866 isr_decl(tcc1);
1867 isr_decl(tcc2);
1868 isr_decl(tc3);
1869 isr_decl(tc4);
1870 isr_decl(tc5);
1871 isr_decl(tc6);
1872 isr_decl(tc7);
1873 isr_decl(adc);
1874 isr_decl(ac);
1875 isr_decl(dac);
1876 isr_decl(ptc);
1877 isr_decl(i2s);
1878 isr_decl(ac1);
1879 isr_decl(tcc3);
1880
1881 #undef isr_decl
1882
1883 #endif /* _SAMD21_H_ */