TeleMetrum v4.0 work in progress
[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_LVL_LVL0           0UL
478 #define  SAMD21_DMAC_CHCTRLB_LVL_LVL1           1UL
479 #define  SAMD21_DMAC_CHCTRLB_LVL_LVL2           2UL
480 #define  SAMD21_DMAC_CHCTRLB_LVL_LVL3           3UL
481 #define SAMD21_DMAC_CHCTRLB_TRIGSRC     8
482 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_DISABLE    0x00UL
483 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_SERCOM_RX(n)       (0x01UL + (n) * 2UL)
484 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_SERCOM_TX(n)       (0x02UL + (n) * 2UL)
485 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_OVF   0x0dUL
486 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC0   0x0eUL
487 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC1   0x0fUL
488 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC2   0x10UL
489 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC0_MC3   0x11UL
490 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC1_OVF   0x12UL
491 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC1_MC0   0x13UL
492 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC1_MC1   0x14UL
493 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC2_OVF   0x15UL
494 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC2_MC0   0x16UL
495 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC2_MC1   0x17UL
496 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC3_OVF    0x18UL
497 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC3_MC0    0x19UL
498 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC3_MC1    0x1aUL
499 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC4_OVF    0x1bUL
500 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC4_MC0    0x1cUL
501 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC4_MC1    0x1dUL
502 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC5_OVF    0x1eUL
503 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC5_MC0    0x1fUL
504 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC5_MC1    0x20UL
505 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC6_OVF    0x21UL
506 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC6_MC0    0x22UL
507 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC6_MC1    0x23UL
508 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC7_OVF    0x24UL
509 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC7_MC0    0x25UL
510 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TC7_MC1    0x26UL
511 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_ADC_RESRDY 0x27UL
512 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_DAC_EMPTY  0x28UL
513 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_RX_0   0x29UL
514 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_RX_1   0x2aUL
515 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_TX_0   0x2bUL
516 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_I2S_TX_1   0x2cUL
517 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_OVF   0x2dUL
518 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC0   0x2eUL
519 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC1   0x2fUL
520 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC2   0x30UL
521 #define  SAMD21_DMAC_CHCTRLB_TRIGSRC_TCC3_MC3   0x31UL
522
523 #define SAMD21_DMAC_CHCTRLB_TRIGACT     22
524 #define  SAMD21_DMAC_CHCTRLB_TRIGACT_BLOCK              0UL
525 #define  SAMD21_DMAC_CHCTRLB_TRIGACT_BEAT               2UL
526 #define  SAMD21_DMAC_CHCTRLB_TRIGACT_TRANSACTION        3UL
527
528 #define SAMD21_DMAC_CHCTRLB_CMD         24
529 #define  SAMD21_DMAC_CHCTRLB_CMD_NOACT          0UL
530 #define  SAMD21_DMAC_CHCTRLB_CMD_SUSPEND        1UL
531 #define  SAMD21_DMAC_CHCTRLB_CMD_RESUME         2UL
532
533 #define SAMD21_DMAC_CHINTFLAG_TERR      0
534 #define SAMD21_DMAC_CHINTFLAG_TCMPL     1
535 #define SAMD21_DMAC_CHINTFLAG_SUSP      2
536
537 #define SAMD21_DMAC_CHSTATUS_PEND       0
538 #define SAMD21_DMAC_CHSTATUS_BUSY       1
539 #define SAMD21_DMAC_CHSTATUS_FERR       2
540
541 #define SAMD21_DMAC_DESC_BTCTRL_VALID           0
542 #define SAMD21_DMAC_DESC_BTCTRL_EVOSEL          1
543 #define  SAMD21_DMAC_DESC_BTCTRL_EVOSEL_DISABLE         0UL
544 #define  SAMD21_DMAC_DESC_BTCTRL_EVOSEL_BLOCK           1UL
545 #define  SAMD21_DMAC_DESC_BTCTRL_EVOSEL_BEAT            3UL
546 #define SAMD21_DMAC_DESC_BTCTRL_BLOCKACT        3
547 #define  SAMD21_DMAC_DESC_BTCTRL_BLOCKACT_NOACT         0UL
548 #define  SAMD21_DMAC_DESC_BTCTRL_BLOCKACT_INT           1UL
549 #define  SAMD21_DMAC_DESC_BTCTRL_BLOCKACT_SUSPEND       2UL
550 #define  SAMD21_DMAC_DESC_BTCTRL_BLOCKACT_BOTH          3UL
551 #define SAMD21_DMAC_DESC_BTCTRL_BEATSIZE        8
552 #define  SAMD21_DMAC_DESC_BTCTRL_BEATSIZE_BYTE          0UL
553 #define  SAMD21_DMAC_DESC_BTCTRL_BEATSIZE_HWORD         1UL
554 #define  SAMD21_DMAC_DESC_BTCTRL_BEATSIZE_WORD          2UL
555 #define SAMD21_DMAC_DESC_BTCTRL_SRCINC          10
556 #define SAMD21_DMAC_DESC_BTCTRL_DSTINC          11
557 #define SAMD21_DMAC_DESC_BTCTRL_STEPSEL         12
558 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSEL_DST            0UL
559 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSEL_SRC            1UL
560 #define SAMD21_DMAC_DESC_BTCTRL_STEPSIZE        13
561 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X1            0UL
562 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X2            1UL
563 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X4            2UL
564 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X8            3UL
565 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X16           4UL
566 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X32           5UL
567 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X64           6UL
568 #define  SAMD21_DMAC_DESC_BTCTRL_STEPSIZE_X128          7UL
569
570 struct samd21_eic {
571         vuint8_t        ctrl;
572         vuint8_t        status;
573         vuint8_t        nmictrl;
574         vuint8_t        nmiflag;
575         vuint32_t       evctrl;
576         vuint32_t       intenclr;
577         vuint32_t       intenset;
578
579         vuint32_t       intflag;
580         vuint32_t       wakeup;
581         vuint32_t       config[2];
582 };
583
584 extern struct samd21_eic samd21_eic;
585
586 #define samd21_eic      (*(struct samd21_eic *) 0x40001800)
587
588 #define SAMD21_NUM_EIC                  16
589
590 #define SAMD21_EIC_CTRL_ENABLE          1
591 #define SAMD21_EIC_CTRL_SWRST           0
592
593 #define SAMD21_EIC_STATUS_SYNCBUSY      7
594
595 #define SAMD21_EIC_NMICTRL_NMIFILTEN    3
596 #define SAMD21_EIC_NMICTRL_NMISENSE     0
597
598 #define SAMD21_EIC_NMIFLAG_NMI          0
599
600 #define SAMD21_EIC_EVCTRL_EXTINTEO(n)   (n)
601
602 #define SAMD21_EIC_INTENCLR_EXTINT(n)   (n)
603
604 #define SAMD21_EIC_INTENSET_EXTINT(n)   (n)
605
606 #define SAMD21_EIC_INTFLAG_EXTINT(n)    (n)
607 #define SAMD21_EIC_WAKEUP_WAKEUPEN(n)   (n)
608 #define SAMD21_EIC_CONFIG_N(n)          ((n) >> 3)
609 #define SAMD21_EIC_CONFIG_SENSE(n)      (((n) & 7) << 2)
610 #define SAMD21_EIC_CONFIG_FILTEN(n)     (SAMD21_EIC_CONFIG_SENSE(n) + 3)
611 #define  SAMD21_EIC_CONFIG_SENSE_NONE           0
612 #define  SAMD21_EIC_CONFIG_SENSE_RISE           1
613 #define  SAMD21_EIC_CONFIG_SENSE_FALL           2
614 #define  SAMD21_EIC_CONFIG_SENSE_BOTH           3
615 #define  SAMD21_EIC_CONFIG_SENSE_HIGH           4
616 #define  SAMD21_EIC_CONFIG_SENSE_LOW            5
617 #define  SAMD21_EIC_CONFIG_SENSE_MASK           7UL
618
619 struct samd21_nvmctrl {
620         vuint32_t       ctrla;
621         vuint32_t       ctrlb;
622         vuint32_t       param;
623         vuint32_t       intenclr;
624
625         vuint32_t       intenset;
626         vuint32_t       intflag;
627         vuint32_t       status;
628         vuint32_t       addr;
629
630         vuint32_t       lock;
631 };
632
633 extern struct samd21_nvmctrl samd21_nvmctrl;
634
635 #define samd21_nvmctrl (*(struct samd21_nvmctrl *) 0x41004000)
636
637 #define SAMD21_NVMCTRL_CTRLA_CMD        0
638 #define  SAMD21_NVMCTRL_CTRLA_CMD_ER            0x02
639 #define  SAMD21_NVMCTRL_CTRLA_CMD_WP            0x04
640 #define  SAMD21_NVMCTRL_CTRLA_CMD_EAR           0x05
641 #define  SAMD21_NVMCTRL_CTRLA_CMD_WAP           0x06
642 #define  SAMD21_NVMCTRL_CTRLA_CMD_RWWEEER       0x1a
643 #define  SAMD21_NVMCTRL_CTRLA_CMD_RWEEEWP       0x1c
644 #define  SAMD21_NVMCTRL_CTRLA_CMD_LR            0x40
645 #define  SAMD21_NVMCTRL_CTRLA_CMD_UR            0x41
646 #define  SAMD21_NVMCTRL_CTRLA_CMD_SPRM          0x42
647 #define  SAMD21_NVMCTRL_CTRLA_CMD_CPRM          0x43
648 #define  SAMD21_NVMCTRL_CTRLA_CMD_PBC           0x44
649 #define  SAMD21_NVMCTRL_CTRLA_CMD_SSB           0x45
650 #define  SAMD21_NVMCTRL_CTRLA_CMD_INVALL        0x46
651 #define  SAMD21_NVMCTRL_CTRLA_CMD_LDR           0x47
652 #define  SAMD21_NVMCTRL_CTRLA_CMD_UDR           0x48
653 #define SAMD21_NVMCTRL_CTRLA_CMDEX      8
654 #define  SAMD21_NVMCTRL_CTRLA_CMDEX_KEY         0xa5
655
656 #define SAMD21_NVMCTRL_CTRLB_RWS        1
657 #define SAMD21_NVMCTRL_CTRLB_MANW       7
658 #define SAMD21_NVMCTRL_CTRLB_SLEEPRM    8
659 #define SAMD21_NVMCTRL_CTRLB_READMODE   16
660 #define SAMD21_NVMCTRL_CTRLB_CACHEDIS   18
661
662 #define SAMD21_NVMCTRL_INTENCLR_READY   0
663 #define SAMD21_NVMCTRL_INTENCLR_ERROR   1
664
665 #define SAMD21_NVMCTRL_INTENSET_READY   0
666 #define SAMD21_NVMCTRL_INTENSET_ERROR   1
667
668 #define SAMD21_NVMCTRL_INTFLAG_READY    0
669 #define SAMD21_NVMCTRL_INTFLAG_ERROR    1
670
671 #define SAMD21_NVMCTRL_STATUS_PRM       0
672 #define SAMD21_NVMCTRL_STATUS_LOAD      1
673 #define SAMD21_NVMCTRL_STATUS_PROGE     2
674 #define SAMD21_NVMCTRL_STATUS_LOCKE     3
675 #define SAMD21_NVMCTRL_STATUS_NVME      4
676 #define SAMD21_NVMCTRL_STATUS_SB        8
677
678 #define SAMD21_NVMCTRL_PARAM_NVMP       0
679 #define  SAMD21_NVMCTRL_PARAM_NVMP_MASK         0xffff
680 #define SAMD21_NVMCTRL_PARAM_PSZ        16
681 #define  SAMD21_NVMCTRL_PARAM_PSZ_MASK          0x7
682 #define SAMD21_NVMCTRL_PARAM_RWWEEP     20
683 #define  SAMD21_NVMCTRL_PARAM_RWWEEP_MASK       0xfff
684
685 static inline uint32_t
686 samd21_nvmctrl_page_shift(void)
687 {
688         return(3 + ((samd21_nvmctrl.param >> SAMD21_NVMCTRL_PARAM_PSZ) &
689                     SAMD21_NVMCTRL_PARAM_PSZ_MASK));
690 }
691
692 static inline uint32_t
693 samd21_nvmctrl_page_size(void)
694 {
695         return 1 << samd21_nvmctrl_page_shift();
696 }
697
698 uint32_t
699 samd21_flash_size(void);
700
701 struct samd21_port {
702         vuint32_t       dir;
703         vuint32_t       dirclr;
704         vuint32_t       dirset;
705         vuint32_t       dirtgl;
706
707         vuint32_t       out;
708         vuint32_t       outclr;
709         vuint32_t       outset;
710         vuint32_t       outtgl;
711
712         vuint32_t       in;
713         vuint32_t       ctrl;
714         vuint32_t       wrconfig;
715         vuint32_t       reserved_2c;
716
717         vuint8_t        pmux[16];
718
719         vuint8_t        pincfg[32];
720 };
721
722 extern struct samd21_port samd21_port_a;
723 extern struct samd21_port samd21_port_b;
724
725 #define samd21_port_a (*(struct samd21_port *) 0x41004400)
726 #define samd21_port_b (*(struct samd21_port *) 0x41004480)
727
728 #define SAMD21_PORT_PINCFG_PMUXEN       0
729 #define SAMD21_PORT_PINCFG_INEN         1
730 #define SAMD21_PORT_PINCFG_PULLEN       2
731 #define SAMD21_PORT_PINCFG_DRVSTR       6
732
733 #define SAMD21_PORT_PMUX_FUNC_A         0
734 #define SAMD21_PORT_PMUX_FUNC_B         1
735 #define SAMD21_PORT_PMUX_FUNC_C         2
736 #define SAMD21_PORT_PMUX_FUNC_D         3
737 #define SAMD21_PORT_PMUX_FUNC_E         4
738 #define SAMD21_PORT_PMUX_FUNC_F         5
739 #define SAMD21_PORT_PMUX_FUNC_G         6
740 #define SAMD21_PORT_PMUX_FUNC_H         7
741 #define SAMD21_PORT_PMUX_FUNC_I         8
742
743 #define SAMD21_PORT_DIR_OUT             1
744 #define SAMD21_PORT_DIR_IN              0
745
746 static inline void
747 samd21_port_dir_set(struct samd21_port *port, uint8_t pin, uint8_t dir)
748 {
749         if (dir)
750                 port->dirset = (1 << pin);
751         else
752                 port->dirclr = (1 << pin);
753 }
754
755 static inline void
756 samd21_port_pincfg_set(struct samd21_port *port, uint8_t pin, uint8_t pincfg_mask, uint8_t pincfg)
757 {
758         port->pincfg[pin] = (uint8_t) ((port->pincfg[pin] & ~pincfg_mask) | pincfg);
759 }
760
761 static inline uint8_t
762 samd21_port_pincfg_get(struct samd21_port *port, uint8_t pin)
763 {
764         return port->pincfg[pin];
765 }
766
767 static inline void
768 samd21_port_pmux_set(struct samd21_port *port, uint8_t pin, uint8_t func)
769 {
770         uint8_t byte = pin >> 1;
771         uint8_t bit = (pin & 1) << 2;
772         uint8_t mask = 0xf << bit;
773         uint8_t value = (uint8_t) ((port->pmux[byte] & ~mask) | (func << bit));
774         port->pmux[byte] = value;
775         samd21_port_pincfg_set(port, pin,
776                                (1 << SAMD21_PORT_PINCFG_PMUXEN),
777                                (1 << SAMD21_PORT_PINCFG_PMUXEN));
778 }
779
780 static inline void
781 samd21_port_pmux_clr(struct samd21_port *port, uint8_t pin)
782 {
783         samd21_port_pincfg_set(port, pin,
784                                (0 << SAMD21_PORT_PINCFG_PMUXEN),
785                                (1 << SAMD21_PORT_PINCFG_PMUXEN));
786 }
787
788 struct samd21_adc {
789         vuint8_t        ctrla;
790         vuint8_t        refctrl;
791         vuint8_t        avgctrl;
792         vuint8_t        sampctrl;
793         vuint16_t       ctrlb;
794         vuint16_t       reserved_06;
795         vuint8_t        winctrl;
796         vuint8_t        reserved_09;
797         vuint16_t       reserved_0a;
798         vuint8_t        swtrig;
799         vuint8_t        reserved_0d;
800         vuint16_t       reserved_0e;
801
802         vuint32_t       inputctrl;
803         vuint8_t        evctrl;
804         vuint8_t        reserved_15;
805         vuint8_t        intenclr;
806         vuint8_t        intenset;
807         vuint8_t        intflag;
808         vuint8_t        status;
809         vuint16_t       result;
810         vuint16_t       winlt;
811         vuint16_t       reserved_1e;
812
813         vuint16_t       winut;
814         vuint16_t       reserved_22;
815         vuint16_t       gaincorr;
816         vuint16_t       offsetcorr;
817         vuint16_t       calib;
818         vuint8_t        dbgctrl;
819         vuint8_t        reserved_2b;
820         vuint32_t       reserved_2c;
821 };
822
823 #define SAMD21_ADC_CTRLA_SWRST          0
824 #define SAMD21_ADC_CTRLA_ENABLE         1
825 #define SAMD21_ADC_CTRLA_RUNSTDBY       2
826
827 #define SAMD21_ADC_REFCTRL_REFSEL       0
828 #define  SAMD21_ADC_REFCTRL_REFSEL_INT1V        0
829 #define  SAMD21_ADC_REFCTRL_REFSEL_INTVCC0      1
830 #define  SAMD21_ADC_REFCTRL_REFSEL_INTVCC1      2
831 #define  SAMD21_ADC_REFCTRL_REFSEL_VREFA        3
832 #define  SAMD21_ADC_REFCTRL_REFSEL_VREFB        4
833 #define SAMD21_ADC_REFCTRL_REFCOMP      7
834
835 #define SAMD21_ADC_AVGCTRL_SAMPLENUM    0
836 #define SAMD21_ADC_AVGCTRL_ADJRES       4
837
838 #define SAMD21_ADC_SAMPCTRL_SAMPLEN     0
839
840 #define SAMD21_ADC_CTRLB_DIFFMODE       0
841 #define SAMD21_ADC_CTRLB_LEFTADJ        1
842 #define SAMD21_ADC_CTRLB_FREERUN        2
843 #define SAMD21_ADC_CTRLB_CORREN         3
844 #define SAMD21_ADC_CTRLB_RESSEL         4
845 #define  SAMD21_ADC_CTRLB_RESSEL_12BIT          0
846 #define  SAMD21_ADC_CTRLB_RESSEL_16BIT          1
847 #define  SAMD21_ADC_CTRLB_RESSEL_10BIT          2
848 #define  SAMD21_ADC_CTRLB_RESSEL_8BIT           3
849 #define SAMD21_ADC_CTRLB_PRESCALER      8
850 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV4        0
851 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV8        1
852 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV16       2
853 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV32       3
854 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV64       4
855 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV128      5
856 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV256      6
857 #define  SAMD21_ADC_CTRLB_PRESCALER_DIV512      7
858
859 #define SAMD21_ADC_SWTRIG_FLUSH         0
860 #define SAMD21_ADC_SWTRIG_START         1
861
862 #define SAMD21_ADC_INPUTCTRL_MUXPOS             0
863 # define SAMD21_ADC_INPUTCTRL_MUXPOS_TEMP               0x18
864 # define SAMD21_ADC_INPUTCTRL_MUXPOS_BANDGAP            0x19
865 # define SAMD21_ADC_INPUTCTRL_MUXPOS_SCALEDCOREVCC      0x1a
866 # define SAMD21_ADC_INPUTCTRL_MUXPOS_SCALEDIOVCC        0x1b
867 # define SAMD21_ADC_INPUTCTRL_MUXPOS_DAC                0x1c
868 #define SAMD21_ADC_INPUTCTRL_MUXNEG             8
869 # define SAMD21_ADC_INPUTCTRL_MUXNEG_GND                0x18
870 # define SAMD21_ADC_INPUTCTRL_MUXNEG_IOGND              0x19
871 #define SAMD21_ADC_INPUTCTRL_INPUTSCAN          16
872 #define SAMD21_ADC_INPUTCTRL_INPUTOFFSET        20
873 #define SAMD21_ADC_INPUTCTRL_GAIN               24
874 #define  SAMD21_ADC_INPUTCTRL_GAIN_1X                   0
875 #define  SAMD21_ADC_INPUTCTRL_GAIN_DIV2                 0xf
876
877 #define SAMD21_ADC_INTFLAG_RESRDY       0
878 #define SAMD21_ADC_INTFLAG_OVERRUN      1
879 #define SAMD21_ADC_INTFLAG_WINMON       2
880 #define SAMD21_ADC_INTFLAG_SYNCRDY      3
881
882 #define SAMD21_ADC_STATUS_SYNCBUSY      7
883
884 #define SAMD21_ADC_CALIB_LINEARITY_CAL  0
885 #define SAMD21_ADC_CALIB_BIAS_CAL       16
886
887 extern struct samd21_adc samd21_adc;
888
889 #define samd21_adc (*(struct samd21_adc *) 0x42004000)
890
891 struct samd21_dac {
892         vuint8_t        ctrla;
893         vuint8_t        ctrlb;
894         vuint8_t        evctrl;
895         uint8_t         reserved_03;
896
897         vuint8_t        intenclr;
898         vuint8_t        intenset;
899         vuint8_t        intflag;
900         vuint8_t        status;
901
902         vuint16_t       data;
903         uint16_t        reserved_0a;
904
905         vuint16_t       databuf;
906 };
907
908 #define SAMD21_DAC_CTRLA_SWRST          0
909 #define SAMD21_DAC_CTRLA_ENABLE         1
910 #define SAMD21_DAC_CTRLA_RUNSTDBY       2
911
912 #define SAMD21_DAC_CTRLB_EOEN           0
913 #define SAMD21_DAC_CTRLB_IOEN           1
914 #define SAMD21_DAC_CTRLB_LEFTADJ        2
915 #define SAMD21_DAC_CTRLB_VPD            3
916 #define SAMD21_DAC_CTRLB_BDWP           4
917 #define SAMD21_DAC_CTRLB_REFSEL         6
918 #define  SAMD21_DAC_CTRLB_REFSEL_INTREF         0
919 #define  SAMD21_DAC_CTRLB_REFSEL_VDDANA         1
920 #define  SAMD21_DAC_CTRLB_REFSEL_VREFA          2
921 #define  SAMD21_DAC_CTRLB_REFSEL_MASK           3
922
923 #define SAMD21_DAC_EVCTRL_STARTEI       0
924 #define SAMD21_DAC_EVCTRL_EMPTYEO       1
925
926 #define SAMD21_DAC_INTENCLR_UNDERRUN    0
927 #define SAMD21_DAC_INTENCLR_EMPTY       1
928 #define SAMD21_DAC_INTENCLR_SYNCRDY     2
929
930 #define SAMD21_DAC_INTENSET_UNDERRUN    0
931 #define SAMD21_DAC_INTENSET_EMPTY       1
932 #define SAMD21_DAC_INTENSET_SYNCRDY     2
933
934 #define SAMD21_DAC_INTFLAG_UNDERRUN     0
935 #define SAMD21_DAC_INTFLAG_EMPTY        1
936 #define SAMD21_DAC_INTFLAG_SYNCRDY      2
937
938 #define SAMD21_DAC_STATUS_SYNCBUSY      7
939
940 extern struct samd21_dac samd21_dac;
941 #define samd21_dac (*(struct samd21_dac *) 0x42004800)
942
943 /* TC */
944 struct samd21_tc {
945         vuint16_t       ctrla;
946         vuint16_t       readreq;
947         vuint8_t        ctrlbclr;
948         vuint8_t        ctrlbset;
949         vuint8_t        ctrlc;
950         vuint8_t        reserved_07;
951         vuint8_t        dbgctrl;
952         vuint8_t        reserved_09;
953         vuint16_t       evctrl;
954         vuint8_t        intenclr;
955         vuint8_t        intenset;
956         vuint8_t        intflag;
957         vuint8_t        status;
958
959         union {
960                 struct {
961                         vuint8_t        count;
962                         vuint8_t        reserved_11;
963                         vuint16_t       reserved_12;
964                         vuint8_t        per;
965                         vuint8_t        reserved_15;
966                         vuint16_t       reserved_16;
967                         vuint8_t        cc[2];
968                 } mode_8;
969                 struct {
970                         vuint16_t       count;
971                         vuint16_t       reserved_12;
972                         vuint32_t       reserved_14;
973                         vuint16_t       cc[2];
974                 } mode_16;
975                 struct {
976                         vuint32_t       count;
977                         vuint32_t       reserved_14;
978                         vuint32_t       cc[2];
979                 } mode_32;
980         };
981 };
982
983 extern struct samd21_tc samd21_tc3;
984 #define samd21_tc3 (*(struct samd21_tc *) 0x42002c00)
985
986 extern struct samd21_tc samd21_tc4;
987 #define samd21_tc4 (*(struct samd21_tc *) 0x42003000)
988
989 extern struct samd21_tc samd21_tc5;
990 #define samd21_tc5 (*(struct samd21_tc *) 0x42003400)
991
992 #ifdef ATSAMD21J
993 /* Present on all of the samd21j parts and the samd21g16l */
994 extern struct samd21_tc samd21_tc6;
995 #define samd21_tc6 (*(struct samd21_tc *) 0x42003800)
996
997 extern struct samd21_tc samd21_tc7;
998 #define samd21_tc7 (*(struct samd21_tc *) 0x42003c00)
999 #endif
1000
1001 #define SAMD21_TC_CTRLA_SWRST           0
1002 #define SAMD21_TC_CTRLA_ENABLE          1
1003 #define SAMD21_TC_CTRLA_MODE            2
1004 #define  SAMD21_TC_CTRLA_MODE_COUNT16           0
1005 #define  SAMD21_TC_CTRLA_MODE_COUNT8            1
1006 #define  SAMD21_TC_CTRLA_MODE_COUNT32           2
1007 #define SAMD21_TC_CTRLA_WAVEGEN         5
1008 #define  SAMD21_TC_CTRLA_WAVEGEN_NFRQ           0
1009 #define  SAMD21_TC_CTRLA_WAVEGEN_MFRQ           1
1010 #define  SAMD21_TC_CTRLA_WAVEGEN_NPWM           2
1011 #define  SAMD21_TC_CTRLA_WAVEGEN_MPWM           3
1012 #define SAMD21_TC_CTRLA_PRESCALER       8
1013 #define  SAMD21_TC_CTRLA_PRESCALER_DIV1         0
1014 #define  SAMD21_TC_CTRLA_PRESCALER_DIV2         1
1015 #define  SAMD21_TC_CTRLA_PRESCALER_DIV4         2
1016 #define  SAMD21_TC_CTRLA_PRESCALER_DIV8         3
1017 #define  SAMD21_TC_CTRLA_PRESCALER_DIV16        4
1018 #define  SAMD21_TC_CTRLA_PRESCALER_DIV64        5
1019 #define  SAMD21_TC_CTRLA_PRESCALER_DIV256       6
1020 #define  SAMD21_TC_CTRLA_PRESCALER_DIV1024      7
1021 #define SAMD21_TC_CTRLA_RUNSTDBY        11
1022 #define SAMD21_TC_CTRLA_PRESCSYNC       12
1023 #define  SAMD21_TC_CTRLA_PRESCSYNC_GCLK         0
1024 #define  SAMD21_TC_CTRLA_PRESCSYNC_PRSEC        1
1025 #define  SAMD21_TC_CTRLA_PRESCSYNC_RESYNC       2
1026
1027 #define SAMD21_TC_READREQ_ADDR          0
1028 #define SAMD21_TC_READREQ_RCONT         14
1029 #define SAMD21_TC_READREQ_RREQ          15
1030 #define SAMD21_TC_CTRLB_DIR             0
1031 #define SAMD21_TC_CTRLB_ONESHOT         2
1032 #define SAMD21_TC_CTRLB_CMD             6
1033 #define SAMD21_TC_CTRLC_INVEN(x)        (0 + (x))
1034 #define SAMD21_TC_CTRLC_CPTEN(x)        (4 + (x))
1035 #define SAMD21_TC_DBGCTRL_DBGRUN        0
1036 #define SAMD21_TC_EVCTRL_EVACT          0
1037 #define SAMD21_TC_EVCTRL_TCINV          4
1038 #define SAMD21_TC_EVCTRL_TCEI           5
1039 #define SAMD21_TC_EVCTRL_OVFEO          8
1040 #define SAMD21_TC_EVCTRL_MCEO(x)        (12 + (x))
1041
1042 #define SAMD21_TC_INTFLAG_MC(x)         (4 + (x))
1043 #define SAMD21_TC_INTFLAG_SYNCRDY       3
1044 #define SAMD21_TC_INTFLAG_ERR           1
1045 #define SAMD21_TC_INTFLAG_OVF           0
1046
1047 #define SAMD21_TC_STATUS_STOP           3
1048 #define SAMD21_TC_STATUS_FOLLOWER       4
1049 #define SAMD21_TC_STATUS_SYNCBUSY       7
1050
1051 /* TCC */
1052
1053 struct samd21_tcc {
1054         vuint32_t       ctrla;
1055         vuint8_t        ctrlbclr;
1056         vuint8_t        ctrlbset;
1057         vuint16_t       reserved_06;
1058         vuint32_t       syncbusy;
1059         vuint32_t       fctrla;
1060
1061         vuint32_t       fctlrb;
1062         vuint32_t       wexctrl;
1063         vuint32_t       drvctrl;
1064         vuint16_t       reserved_1c;
1065         vuint8_t        dbgctrl;
1066         vuint8_t        reserved_1f;
1067
1068         vuint32_t       evctrl;
1069         vuint32_t       intenclr;
1070         vuint32_t       intenset;
1071         vuint32_t       intflag;
1072
1073         vuint32_t       status;
1074         vuint32_t       count;
1075         vuint16_t       patt;
1076         vuint16_t       reserved_3a;
1077         vuint32_t       wave;
1078
1079         vuint32_t       per;
1080         vuint32_t       cc[4];
1081         vuint32_t       reserved_54;
1082         vuint32_t       reserved_58;
1083         vuint32_t       reserved_5c;
1084
1085         vuint32_t       reserved_60;
1086         vuint16_t       pattb;
1087         vuint16_t       reserved_66;
1088         vuint32_t       waveb;
1089         vuint32_t       perb;
1090
1091         vuint32_t       ccb[4];
1092 };
1093
1094 extern struct samd21_tcc samd21_tcc0;
1095 #define samd21_tcc0 (*(struct samd21_tcc *) 0x42002000)
1096
1097 extern struct samd21_tcc samd21_tcc1;
1098 #define samd21_tcc1 (*(struct samd21_tcc *) 0x42002400)
1099
1100 extern struct samd21_tcc samd21_tcc2;
1101 #define samd21_tcc2 (*(struct samd21_tcc *) 0x42002800)
1102
1103 #ifdef SAMD21E17D
1104 /* only on the samd21e17d */
1105 extern struct samd21_tcc samd21_tcc3;
1106 #define samd21_tcc3 (*(struct samd21_tcc *) 0x42006000)
1107 #endif
1108
1109 #define SAMD21_TCC_CTRLA_SWRST          0
1110 #define SAMD21_TCC_CTRLA_ENABLE         1
1111 #define SAMD21_TCC_CTRLA_RESOLUTION     5
1112 #define  SAMD21_TCC_CTRLA_RESOLUTION_NONE       0
1113 #define  SAMD21_TCC_CTRLA_RESOLUTION_DITH4      1
1114 #define  SAMD21_TCC_CTRLA_RESOLUTION_DITH5      2
1115 #define  SAMD21_TCC_CTRLA_RESOLUTION_DITH6      3
1116 #define SAMD21_TCC_CTRLA_PRESCALER      8
1117 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV1        0
1118 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV2        1
1119 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV4        2
1120 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV8        3
1121 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV16       4
1122 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV64       5
1123 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV256      6
1124 #define  SAMD21_TCC_CTRLA_PRESCALER_DIV1024     7
1125 #define SAMD21_TCC_CTRLA_RUNSTDBY       11
1126 #define SAMD21_TCC_CTRLA_PRESYNC        12
1127 #define  SAMD21_TCC_CTRLA_PRESYNC_GCLK          0
1128 #define  SAMD21_TCC_CTRLA_PRESYNC_PRESC         1
1129 #define  SAMD21_TCC_CTRLA_PRESYNC_RESYNC        2
1130 #define SAMD21_TCC_CTRLA_ALOCK          14
1131 #define SAMD21_TCC_CTRLA_CPTEN(n)       (24 + (n))
1132
1133 #define SAMD21_TCC_CTRLB_DIR            0
1134 #define SAMD21_TCC_CTRLB_LUPD           1
1135 #define SAMD21_TCC_CTRLB_ONESHOT        2
1136 #define SAMD21_TCC_CTRLB_IDXCMD         3
1137 #define  SAMD21_TCC_CTRLB_IDXCMD_DISABLE        0
1138 #define  SAMD21_TCC_CTRLB_IDXCMD_SET            1
1139 #define  SAMD21_TCC_CTRLB_IDXCMD_CLEAR          2
1140 #define  SAMD21_TCC_CTRLB_IDXCMD_HOLD           3
1141 #define SAMD21_TCC_CTRLB_CMD            5
1142 #define  SAMD21_TCC_CTRLB_CMD_NONE              0
1143 #define  SAMD21_TCC_CTRLB_CMD_RETRIGGER         1
1144 #define  SAMD21_TCC_CTRLB_CMD_STOP              2
1145 #define  SAMD21_TCC_CTRLB_CMD_UPDATE            3
1146 #define  SAMD21_TCC_CTRLB_CMD_READSYNC          4
1147 #define  SAMD21_TCC_CTRLB_CMD_DMAOS             5
1148
1149 #define SAMD21_TCC_SYNCBUSY_SWRST       0
1150 #define SAMD21_TCC_SYNCBUSY_ENABLE      1
1151 #define SAMD21_TCC_SYNCBUSY_CTRLB       2
1152 #define SAMD21_TCC_SYNCBUSY_STATUS      3
1153 #define SAMD21_TCC_SYNCBUSY_COUNT       4
1154 #define SAMD21_TCC_SYNCBUSY_PATT        5
1155 #define SAMD21_TCC_SYNCBUSY_WAVE        6
1156 #define SAMD21_TCC_SYNCBUSY_PER         7
1157 #define SAMD21_TCC_SYNCBUSY_CC(x)       (8 + (x))
1158 #define SAMD21_TCC_SYNCBUSY_PATTB       16
1159 #define SAMD21_TCC_SYNCBUSY_WAVEB       17
1160 #define SAMD21_TCC_SYNCBUSY_PERB        18
1161 #define SAMD21_TCC_SYNCBUSY_CCB(x)      ((19 + (x))
1162
1163 #define SAMD21_TCC_DBGCTRL_FDDBD        2
1164 #define SAMD21_TCC_DBGCTRL_DBGRUN       0
1165
1166 #define SAMD21_TCC_EVCTRL_EVACTO        0
1167 #define SAMD21_TCC_EVCTRL_EVACT1        3
1168 #define SAMD21_TCC_EVCTRL_CNTSEL        6
1169 #define SAMD21_TCC_EVCTRL_OVFEO         8
1170 #define SAMD21_TCC_EVCTRL_TRGEO         9
1171 #define SAMD21_TCC_EVCTRL_CNTEO         10
1172 #define SAMD21_TCC_EVCTRL_TCINV(x)      (12 + (x))
1173 #define SAMD21_TCC_EVCTRL_MCEI(x)       (16 + (x))
1174 #define SAMD21_TCC_EVCTRL_MCEO(x)       (24 + (x))
1175
1176 #define SAMD21_TCC_INTFLAG_OVF          0
1177 #define SAMD21_TCC_INTFLAG_TRG          1
1178 #define SAMD21_TCC_INTFLAG_CNT          2
1179 #define SAMD21_TCC_INTFLAG_ERR          3
1180 #define SAMD21_TCC_INTFLAG_UFS          10
1181 #define SAMD21_TCC_INTFLAG_DFS          11
1182 #define SAMD21_TCC_INTFLAG_FAULTA       12
1183 #define SAMD21_TCC_INTFLAG_FAULTB       13
1184 #define SAMD21_TCC_INTFLAG_FAULT0       14
1185 #define SAMD21_TCC_INTFLAG_FAULT1       15
1186 #define SAMD21_TCC_INTFLAG_MC(x)        (16 + (x))
1187
1188 #define SAMD21_TCC_WAVE_WAVEGEN         0
1189 #define  SAMD21_TCC_WAVE_WAVEGEN_NFRQ           0
1190 #define  SAMD21_TCC_WAVE_WAVEGEN_MFRQ           1
1191 #define  SAMD21_TCC_WAVE_WAVEGEN_NPWM           2
1192 #define  SAMD21_TCC_WAVE_WAVEGEN_DSCRITICAL     4
1193 #define  SAMD21_TCC_WAVE_WAVEGEN_DSBOTTOM       5
1194 #define  SAMD21_TCC_WAVE_WAVEGEN_DSBOTH         6
1195 #define  SAMD21_TCC_WAVE_WAVEGEN_DSTOP          7
1196 #define SAMD21_TCC_WAVE_RAMP            4
1197 #define SAMD21_TCC_WAVE_CIPEREN         7
1198 #define SAMD21_TCC_WAVE_CCCEN(x)        (8 + (x))
1199 #define SAMD21_TCC_WAVE_POL(x)          (16 + (x))
1200 #define SAMD21_TCC_WAVE_SWAP(x)         (24 + (x))
1201
1202 /* USB */
1203
1204 struct samd21_usb {
1205         vuint8_t        ctrla;
1206         vuint8_t        reserved_01;
1207         vuint8_t        syncbusy;
1208         vuint8_t        qosctrl;
1209
1210         vuint32_t       reserved_04;
1211         vuint16_t       ctrlb;
1212         vuint8_t        dadd;
1213         vuint8_t        reserved_0b;
1214         vuint8_t        status;
1215         vuint8_t        fsmstatus;
1216         vuint16_t       reserved_0e;
1217
1218         vuint16_t       fnum;
1219         vuint16_t       reserved_12;
1220         vuint16_t       intenclr;
1221         vuint16_t       reserved_16;
1222         vuint16_t       intenset;
1223         vuint16_t       reserved_1a;
1224         vuint16_t       intflag;
1225         vuint16_t       reserved_1e;
1226
1227         vuint16_t       epintsmry;
1228         vuint16_t       reserved_22;
1229
1230         vuint32_t       descadd;
1231         vuint16_t       padcal;
1232         uint8_t         reserved_2a[0x100 - 0x2a];
1233
1234         struct {
1235                 vuint8_t        epcfg;
1236                 vuint8_t        reserved_01;
1237                 vuint8_t        reserved_02;
1238                 vuint8_t        binterval;
1239                 vuint8_t        epstatusclr;
1240                 vuint8_t        epstatusset;
1241                 vuint8_t        epstatus;
1242                 vuint8_t        epintflag;
1243                 vuint8_t        epintenclr;
1244                 vuint8_t        epintenset;
1245                 vuint8_t        reserved_0a[0x20 - 0x0a];
1246         } ep[8];
1247 };
1248
1249 extern struct samd21_usb samd21_usb;
1250
1251 #define samd21_usb (*(struct samd21_usb *) 0x41005000)
1252
1253 #define SAMD21_USB_CTRLA_SWRST          0
1254 #define SAMD21_USB_CTRLA_ENABLE         1
1255 #define SAMD21_USB_CTRLA_RUNSTDBY       2
1256 #define SAMD21_USB_CTRLA_MODE           7
1257
1258 #define SAMD21_USB_SYNCBUSY_SWRST       0
1259 #define SAMD21_USB_SYNCBUSY_ENABLE      1
1260
1261 #define SAMD21_USB_QOSCTRL_CQOS         0
1262 #define SAMD21_USB_QOSCTRL_DQOS         2
1263
1264 #define SAMD21_USB_CTRLB_DETACH         0
1265 #define SAMD21_USB_CTRLB_UPRSM          1
1266 #define SAMD21_USB_CTRLB_SPDCONF        2
1267 #define  SAMD21_USB_CTRLB_SPDCONF_FS            0
1268 #define  SAMD21_USB_CTRLB_SPDCONF_LS            1
1269 #define  SAMD21_USB_CTRLB_SPDCONF_MASK          0x3
1270 #define SAMD21_USB_CTRLB_NREPLY         4
1271 #define SAMD21_USB_CTRLB_GNAK           9
1272 #define SAMD21_USB_CTRLB_LPMHDSK        10
1273 #define  SAMD21_USB_CTRLB_LPMHDSK_NONE          0
1274 #define  SAMD21_USB_CTRLB_LPMHDSK_ACK           1
1275 #define  SAMD21_USB_CTRLB_LPMHDSK_NYET          2
1276 #define  SAMD21_USB_CTRLB_LPMHDSK_MASK          3
1277
1278 #define SAMD21_USB_DADD_DADD            0
1279 #define SAMD21_USB_DADD_ADDEN           7
1280
1281 #define SAMD21_USB_STATUS_SPEED         2
1282 #define SAMD21_USB_STATUS_LINESTATE     6
1283 #define SAMD21_USB_FNUM_MFNUM           0
1284 #define SAMD21_USB_FNUM_FNUM            3
1285 #define SAMD21_USB_FNUM_FNCERR          15
1286 #define SAMD21_USB_INTFLAG_SUSPEND      0
1287 #define SAMD21_USB_INTFLAG_SOF          2
1288 #define SAMD21_USB_INTFLAG_EORST        3
1289 #define SAMD21_USB_INTFLAG_WAKEUP       4
1290 #define SAMD21_USB_INTFLAG_EORSM        5
1291 #define SAMD21_USB_INTFLAG_UPRSM        6
1292 #define SAMD21_USB_INTFLAG_RAMACER      7
1293 #define SAMD21_USB_INTFLAG_LPMNYET      8
1294 #define SAMD21_USB_INTFLAG_LPMSUSP      9
1295
1296 #define SAMD21_USB_PADCAL_TRANSP        0
1297 #define SAMD21_USB_PADCAL_TRANSN        6
1298 #define SAMD21_USB_PADCAL_TRIM          12
1299
1300 #define SAMD21_USB_EP_EPCFG_EP_TYPE_OUT         0
1301 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_DISABLED       0
1302 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_CONTROL        1
1303 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_ISOCHRONOUS    2
1304 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_BULK           3
1305 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_INTERRUPT      4
1306 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_DUAL_BANK      5
1307 #define SAMD21_USB_EP_EPCFG_EP_TYPE_IN          4
1308 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_DISABLED        0
1309 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_CONTROL 1
1310 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_ISOCHRONOUS     2
1311 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_BULK            3
1312 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_INTERRUPT       4
1313 #define  SAMD21_USB_EP_EPCFG_EP_TYPE_IN_DUAL_BANK       5
1314
1315 #define SAMD21_USB_EP_EPSTATUS_DTGLOUT                  0
1316 #define SAMD21_USB_EP_EPSTATUS_DTGLIN                   1
1317 #define SAMD21_USB_EP_EPSTATUS_CURBK                    2
1318 #define SAMD21_USB_EP_EPSTATUS_STALLRQ0                 4
1319 #define SAMD21_USB_EP_EPSTATUS_STALLRQ1                 5
1320 #define SAMD21_USB_EP_EPSTATUS_BK0RDY                   6
1321 #define SAMD21_USB_EP_EPSTATUS_BK1RDY                   7
1322
1323 #define SAMD21_USB_EP_EPINTFLAG_TRCPT0                  0
1324 #define SAMD21_USB_EP_EPINTFLAG_TRCPT1                  1
1325 #define SAMD21_USB_EP_EPINTFLAG_TRFAIL0                 2
1326 #define SAMD21_USB_EP_EPINTFLAG_TRFAIL1                 3
1327 #define SAMD21_USB_EP_EPINTFLAG_RXSTP                   4
1328 #define SAMD21_USB_EP_EPINTFLAG_STALL                   5
1329
1330 struct samd21_usb_desc_bank {
1331         vuint32_t       addr;
1332         vuint32_t       pcksize;
1333         vuint16_t       extreg;
1334         vuint8_t        status_bk;
1335         vuint8_t        reserved_0b;
1336         vuint32_t       reserved_0c;
1337 };
1338
1339 struct samd21_usb_desc {
1340         struct samd21_usb_desc_bank bank[2];
1341 };
1342
1343 extern struct samd21_usb_desc   samd21_usb_desc[8];
1344
1345 #define SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT              0
1346 #define  SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT_MASK                0x3fffU
1347 #define SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE       14
1348 #define  SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE_MASK         0x3fffU
1349 #define SAMD21_USB_DESC_PCKSIZE_SIZE                    28
1350 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_8                         0
1351 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_16                        1
1352 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_32                        2
1353 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_64                        3
1354 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_128                       4
1355 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_256                       5
1356 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_512                       6
1357 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_1023                      7
1358 #define  SAMD21_USB_DESC_PCKSIZE_SIZE_MASK                      7U
1359 #define SAMD21_USB_DESC_PCKSIZE_AUTO_ZLP                31
1360
1361 static inline uint16_t
1362 samd21_usb_desc_get_byte_count(uint8_t ep, uint8_t bank)
1363 {
1364         return ((samd21_usb_desc[ep].bank[bank].pcksize >> SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT) &
1365                 SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT_MASK);
1366 }
1367
1368 static inline void
1369 samd21_usb_desc_set_byte_count(uint8_t ep, uint8_t bank, uint32_t count)
1370 {
1371         uint32_t pcksize = samd21_usb_desc[ep].bank[bank].pcksize;
1372
1373         pcksize &= ~(SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT_MASK << SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT);
1374         pcksize &= ~(SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE_MASK << SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE);
1375         pcksize |= (count << SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT);
1376         samd21_usb_desc[ep].bank[bank].pcksize = pcksize;
1377 }
1378
1379 static inline void
1380 samd21_usb_desc_set_size(uint8_t ep, uint8_t bank, uint32_t size)
1381 {
1382         uint32_t pcksize = samd21_usb_desc[ep].bank[bank].pcksize;
1383
1384         pcksize &= ~(SAMD21_USB_DESC_PCKSIZE_SIZE_MASK << SAMD21_USB_DESC_PCKSIZE_SIZE);
1385
1386         uint32_t size_bits = 0;
1387         switch (size) {
1388         case 8: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_8; break;
1389         case 16: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_16; break;
1390         case 32: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_32; break;
1391         case 64: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_64; break;
1392         case 128: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_128; break;
1393         case 256: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_256; break;
1394         case 512: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_512; break;
1395         case 1023: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_1023; break;
1396         }
1397         pcksize |= (size_bits << SAMD21_USB_DESC_PCKSIZE_SIZE);
1398         samd21_usb_desc[ep].bank[bank].pcksize = pcksize;
1399 }
1400
1401 static inline void
1402 samd21_usb_ep_set_ready(uint8_t ep, uint8_t bank)
1403 {
1404         samd21_usb.ep[ep].epstatusset = (1 << (SAMD21_USB_EP_EPSTATUS_BK0RDY + bank));
1405         samd21_usb.ep[ep].epintflag = (1 << (SAMD21_USB_EP_EPINTFLAG_TRFAIL0 + bank));
1406 }
1407
1408 static inline void
1409 samd21_usb_ep_clr_ready(uint8_t ep, uint8_t bank)
1410 {
1411         samd21_usb.ep[ep].epstatusclr = (1 << (SAMD21_USB_EP_EPSTATUS_BK0RDY + bank));
1412 }
1413
1414 static inline uint8_t
1415 samd21_usb_ep_ready(uint8_t ep)
1416 {
1417         return (samd21_usb.ep[ep].epstatus >> SAMD21_USB_EP_EPSTATUS_BK0RDY) & 3;
1418 }
1419
1420 static inline uint8_t
1421 samd21_usb_ep_curbk(uint8_t ep)
1422 {
1423         return (samd21_usb.ep[ep].epstatus >> SAMD21_USB_EP_EPSTATUS_CURBK) & 1;
1424 }
1425
1426 /* evsys */
1427
1428 struct samd21_evsys {
1429         vuint8_t        ctrl;
1430         vuint8_t        reserved_01;
1431         vuint16_t       reserved_02;
1432         vuint32_t       channel;
1433         vuint16_t       user;
1434         vuint16_t       reserved_0a;
1435         vuint32_t       chstatus;
1436
1437         vuint32_t       intenclr;
1438         vuint32_t       intenset;
1439         vuint32_t       intflag;
1440 };
1441
1442 extern struct samd21_evsys samd21_evsys;
1443
1444 #define SAMD21_NUM_EVSYS        16
1445
1446 #define samd21_evsys    (*(struct samd21_evsys *) 0x42000400)
1447
1448 #define SAMD21_EVSYS_CONTROL_SWRST      0
1449 #define SAMD21_EVSYS_CONTROL_GCLKREQ    4
1450
1451 #define SAMD21_EVSYS_CHANNEL_CHANNEL    0
1452
1453 #define SAMD21_EVSYS_CHANNEL_SWEVT      8
1454
1455 #define SAMD21_EVSYS_CHANNEL_EVGEN      16
1456 #define  SAMD21_EVSYS_CHANNEL_EVGEN_NONE                0x00
1457 #define  SAMD21_EVSYS_CHANNEL_EVGEN_RTC_CMP(i)          (0x01 + (i))
1458 #define  SAMD21_EVSYS_CHANNEL_EVGEN_OVF                 0x03
1459 #define  SAMD21_EVSYS_CHANNEL_EVGEN_PER(i)              (0x04 + (i))
1460 #define  SAMD21_EVSYS_CHANNEL_EVGEN_EXTINT(i)           (0x0c + (i))
1461 #define  SAMD21_EVSYS_CHANNEL_EVGEN_DMAC_CH(i)          (0x1e + (i))
1462 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC0_OVF            0x22
1463 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC0_TRG            0x23
1464 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC0_CNT            0x29
1465 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC0_MCX(i)         (0x25 + (i))
1466 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC1_OVF            0x29
1467 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC1_TRG            0x2a
1468 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC1_CNT            0x2b
1469 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC1_MCX(i)         (0x2c + (i))
1470 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC2_OVF            0x2e
1471 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC2_TRG            0x2f
1472 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC2_CNT            0x30
1473 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC2_MCX(i)         (0x31 + (i))
1474 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC3_OVF             0x33
1475 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC3_MC(i)           (0x34 + (i))
1476 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC4_OVF             0x36
1477 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC4_MC(i)           (0x37 + (i))
1478 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC5_OVF             0x39
1479 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC5_MC(i)           (0x3a + (i))
1480 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC6_OVF             0x3c
1481 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC6_MC(i)           (0x3d + (i))
1482 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC7_OVF             0x3f
1483 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TC7_MC(i)           (0x40 + (i))
1484 #define  SAMD21_EVSYS_CHANNEL_EVGEN_ADC_RESRDY          0x42
1485 #define  SAMD21_EVSYS_CHANNEL_EVGEN_ADC_WINMON          0x43
1486 #define  SAMD21_EVSYS_CHANNEL_EVGEN_AC_COMP0            0x44
1487 #define  SAMD21_EVSYS_CHANNEL_EVGEN_AC_COMP1            0x45
1488 #define  SAMD21_EVSYS_CHANNEL_EVGEN_AC_WIN0             0x46
1489 #define  SAMD21_EVSYS_CHANNEL_EVGEN_DAC_EMPTY           0x47
1490 #define  SAMD21_EVSYS_CHANNEL_EVGEN_PTC_EOC             0x48
1491 #define  SAMD21_EVSYS_CHANNEL_EVGEN_PTC_WCOMP           0x49
1492 #define  SAMD21_EVSYS_CHANNEL_EVGEN_AC_COMP2            0x4a
1493 #define  SAMD21_EVSYS_CHANNEL_EVGEN_AC_COMP3            0x4b
1494 #define  SAMD21_EVSYS_CHANNEL_EVGEN_AC_WIN1             0x4c
1495 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC3_OVF            0x4d
1496 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC3_TRG            0x4e
1497 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC3_CNT            0x4f
1498 #define  SAMD21_EVSYS_CHANNEL_EVGEN_TCC3_MCX(i)         (0x50 + (i))
1499
1500 #define SAMD21_EVSYS_CHANNEL_PATH       24
1501 #define  SAMD21_EVSYS_CHANNEL_PATH_SYNCHRONOUS          0
1502 #define  SAMD21_EVSYS_CHANNEL_PATH_RESYNCHRONIZED       1
1503 #define  SAMD21_EVSYS_CHANNEL_PATH_ASYNCHRONOUS         2
1504
1505 #define SAMD21_EVSYS_CHANNEL_EDGESEL    26
1506 #define  SAMD21_EVSYS_CHANNEL_EDGESEL_NO_EVT_OUTPUT     0
1507 #define  SAMD21_EVSYS_CHANNEL_EDGESEL_RISING_EDGE       1
1508 #define  SAMD21_EVSYS_CHANNEL_EDGESEL_FALLING_EDGE      2
1509 #define  SAMD21_EVSYS_CHANNEL_EDGESEL_BOTH_EDGES        3
1510
1511 #define SAMD21_EVSYS_USER_USER          0
1512 #define  SAMD21_EVSYS_USER_USER_DMAC_CH(n)      (0x00 + (n))
1513 #define  SAMD21_EVSYS_USER_USER_TCC0_EV(n)      (0x04 + (n))
1514 #define  SAMD21_EVSYS_USER_USER_TCC0_MC(n)      (0x06 + (n))
1515 #define  SAMD21_EVSYS_USER_USER_TCC1_EV(n)      (0x0a + (n))
1516 #define  SAMD21_EVSYS_USER_USER_TCC1_MC(n)      (0x0c + (n))
1517 #define  SAMD21_EVSYS_USER_USER_TCC2_EV(n)      (0x0e + (n))
1518 #define  SAMD21_EVSYS_USER_USER_TCC2_MC(n)      (0x10 + (n))
1519 #define  SAMD21_EVSYS_USER_USER_TC(n)           (0x12 + (n))
1520 #define  SAMD21_EVSYS_USER_USER_ADC_START       (0x17)
1521 #define  SAMD21_EVSYS_USER_USER_ADC_SYNC        0x18
1522 #define  SAMD21_EVSYS_USER_USER_AC_COMP0        0x19
1523 #define  SAMD21_EVSYS_USER_USER_AC_COMP1        0x1a
1524 #define  SAMD21_EVSYS_USER_USER_DAC_START       0x1b
1525 #define  SAMD21_EVSYS_USER_USER_PTC_STCONV      0x1c
1526 #define  SAMD21_EVSYS_USER_USER_AC_COMP2        0x1d
1527 #define  SAMD21_EVSYS_USER_USER_AC_COMP3        0x1e
1528 #define  SAMD21_EVSYS_USER_USER_TCC3_EV(n)      (0x1f + (n))
1529 #define  SAMD21_EVSYS_USER_USER_TCC3_MC(n)      (0x21 + (n))
1530
1531 #define SAMD21_EVSYS_USER_CHANNEL       8
1532 #define  SAMD21_EVSYS_USER_CHANNEL_NONE         0
1533 #define  SAMD21_EVSYS_USER_CHANNEL_NUM(n)       ((n) + 1)
1534
1535
1536 #define SAMD21_EVSYS_CHSTATUS_USRRDY(n) (((n) & 7) | (((n) & 8) << 1))
1537 #define SAMD21_EVSYS_CHSTATUS_CHBUSY(n) (((n) & 7) | (((n) & 8) << 1) | 8)
1538
1539 /* sercom */
1540
1541 struct samd21_sercom {
1542         vuint32_t       ctrla;
1543         vuint32_t       ctrlb;
1544         vuint32_t       reserved_08;
1545         vuint16_t       baud;
1546         vuint8_t        rxpl;
1547         vuint8_t        reserved_0f;
1548
1549         vuint32_t       reserved_10;
1550         vuint8_t        intenclr;
1551         vuint8_t        reserved_15;
1552         vuint8_t        intenset;
1553         vuint8_t        reserved_17;
1554         vuint8_t        intflag;
1555         vuint8_t        reserved_19;
1556         vuint16_t       status;
1557         vuint32_t       syncbusy;
1558
1559         vuint32_t       reserved_20;
1560         vuint32_t       addr;
1561         vuint16_t       data;
1562         vuint16_t       reserved_2a;
1563         vuint32_t       reserved_2c;
1564
1565         vuint8_t        dbgctrl;
1566         vuint8_t        reserved_31;
1567         vuint16_t       reserved_32;
1568         vuint16_t       fifospace;
1569         vuint16_t       fifoptr;
1570 };
1571
1572 extern struct samd21_sercom samd21_sercom0;
1573 extern struct samd21_sercom samd21_sercom1;
1574 extern struct samd21_sercom samd21_sercom2;
1575 extern struct samd21_sercom samd21_sercom3;
1576 extern struct samd21_sercom samd21_sercom4;
1577 extern struct samd21_sercom samd21_sercom5;
1578
1579 #define SAMD21_NUM_SERCOM       6
1580
1581 #define samd21_sercom0  (*(struct samd21_sercom *) 0x42000800)
1582 #define samd21_sercom1  (*(struct samd21_sercom *) 0x42000c00)
1583 #define samd21_sercom2  (*(struct samd21_sercom *) 0x42001000)
1584 #define samd21_sercom3  (*(struct samd21_sercom *) 0x42001400)
1585 #define samd21_sercom4  (*(struct samd21_sercom *) 0x42001800)
1586 #define samd21_sercom5  (*(struct samd21_sercom *) 0x42001c00)
1587
1588 #define SAMD21_SERCOM_CTRLA_SWRST       0
1589 #define SAMD21_SERCOM_CTRLA_ENABLE      1
1590 #define SAMD21_SERCOM_CTRLA_MODE        2
1591 # define SAMD21_SERCOM_CTRLA_MODE_USART         1
1592 # define SAMD21_SERCOM_CTRLA_MODE_SPI_CLIENT    2
1593 # define SAMD21_SERCOM_CTRLA_MODE_SPI_HOST      3
1594 # define SAMD21_SERCOM_CTRLA_MODE_I2C_CLIENT    4
1595 # define SAMD21_SERCOM_CTRLA_MODE_I2C_HOST      5
1596
1597 #define SAMD21_SERCOM_CTRLA_RUNSTDBY    7
1598
1599 /* USART mode */
1600 #define SAMD21_SERCOM_CTRLA_IBON        8
1601 #define SAMD21_SERCOM_CTRLA_SAMPR       13
1602 #define SAMD21_SERCOM_CTRLA_TXPO        16
1603 #define  SAMD21_SERCOM_CTRLA_TXPO_TX_0                  0
1604 #define  SAMD21_SERCOM_CTRLA_TXPO_TX_2                  1
1605 #define  SAMD21_SERCOM_CTRLA_TXPO_TX_0_RTS_2_CTS_3      2
1606 #define SAMD21_SERCOM_CTRLA_RXPO        20
1607 #define  SAMD21_SERCOM_CTRLA_RXPO_RX_0                  0
1608 #define  SAMD21_SERCOM_CTRLA_RXPO_RX_1                  1
1609 #define  SAMD21_SERCOM_CTRLA_RXPO_RX_2                  2
1610 #define  SAMD21_SERCOM_CTRLA_RXPO_RX_3                  3
1611 #define SAMD21_SERCOM_CTRLA_SAMPA       22
1612 #define SAMD21_SERCOM_CTRLA_FORM        24
1613 #define SAMD21_SERCOM_CTRLA_CMODE       28
1614 #define SAMD21_SERCOM_CTRLA_CPOL        29
1615 #define SAMD21_SERCOM_CTRLA_DORD        30
1616
1617 /* I2C controller mode */
1618 #define SAMD21_SERCOM_CTRLA_PINOUT      16
1619 #define SAMD21_SERCOM_CTRLA_SDAHOLD     20
1620 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_DIS        0
1621 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_75NS       1
1622 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_450NS      2
1623 #define  SAMD21_SERCOM_CTRLA_SDAHOLD_600NS      3
1624 #define SAMD21_SERCOM_CTRLA_MEXTTOEN    22
1625 #define SAMD21_SERCOM_CTRLA_SEXTTOEN    23
1626 #define SAMD21_SERCOM_CTRLA_SPEED       24
1627 #define  SAMD21_SERCOM_CTRLA_SPEED_STANDARD     0
1628 #define  SAMD21_SERCOM_CTRLA_SPEED_FAST         1
1629 #define  SAMD21_SERCOM_CTRLA_SPEED_HIGH         2
1630 #define SAMD21_SERCOM_CTRLA_SCLSM       27
1631 #define SAMD21_SERCOM_CTRLA_INACTOUT    28
1632 #define  SAMD21_SERCOM_CTRLA_INACTOUT_DIS       0
1633 #define  SAMD21_SERCOM_CTRLA_INACTOUT_55US      1
1634 #define  SAMD21_SERCOM_CTRLA_INACTOUT_105US     2
1635 #define  SAMD21_SERCOM_CTRLA_INACTOUT_205US     3
1636 #define SAMD21_SERCOM_CTRLA_LOWTOUT     30
1637
1638 /* SPI controller mode */
1639 #define SAMD21_SERCOM_CTRLA_DOPO        16
1640 #define  SAMD21_SERCOM_CTRLA_DOPO_MOSI_0_SCLK_1 0UL
1641 #define  SAMD21_SERCOM_CTRLA_DOPO_MOSI_2_SCLK_3 1UL
1642 #define  SAMD21_SERCOM_CTRLA_DOPO_MOSI_3_SCLK_1 2UL
1643 #define  SAMD21_SERCOM_CTRLA_DOPO_MOSI_0_SCLK_3 3UL
1644 #define  SAMD21_SERCOM_CTRLA_DOPO_MASK          3UL
1645
1646 #define SAMD21_SERCOM_CTRLA_DIPO        20
1647 #define  SAMD21_SERCOM_CTRLA_DIPO_MISO_0        0UL
1648 #define  SAMD21_SERCOM_CTRLA_DIPO_MISO_1        1UL
1649 #define  SAMD21_SERCOM_CTRLA_DIPO_MISO_2        2UL
1650 #define  SAMD21_SERCOM_CTRLA_DIPO_MISO_3        3UL
1651 #define  SAMD21_SERCOM_CTRLA_DIPO_MASK          3UL
1652
1653 #define SAMD21_SERCOM_CTRLA_FORM        24
1654 #define SAMD21_SERCOM_CTRLA_CPHA        28
1655 #define SAMD21_SERCOM_CTRLA_CPOL        29
1656 #define SAMD21_SERCOM_CTRLA_DORD        30
1657 #define  SAMD21_SERCOM_CTRLA_DORD_LSB   1
1658 #define  SAMD21_SERCOM_CTRLA_DORD_MSB   0
1659
1660 /* USART mode */
1661 #define SAMD21_SERCOM_CTRLB_CHSIZE      0
1662 #define SAMD21_SERCOM_CTRLB_SBMODE      6
1663 #define SAMD21_SERCOM_CTRLB_COLDEN      8
1664 #define SAMD21_SERCOM_CTRLB_SFDE        9
1665 #define SAMD21_SERCOM_CTRLB_ENC         10
1666 #define SAMD21_SERCOM_CTRLB_PMODE       13
1667 #define SAMD21_SERCOM_CTRLB_TXEN        16
1668 #define SAMD21_SERCOM_CTRLB_RXEN        17
1669 #define SAMD21_SERCOM_CTRLB_FIFOCLR     22
1670
1671 /* I2C mode */
1672 #define SAMD21_SERCOM_CTRLB_SMEN        8
1673 #define SAMD21_SERCOM_CTRLB_QCEN        9
1674 #define SAMD21_SERCOM_CTRLB_CMD         16
1675 #define  SAMD21_SERCOM_CTRLB_CMD_NOP            0
1676 #define  SAMD21_SERCOM_CTRLB_CMD_START          1
1677 #define  SAMD21_SERCOM_CTRLB_CMD_READ           2
1678 #define  SAMD21_SERCOM_CTRLB_CMD_STOP           3
1679 #define SAMD21_SERCOM_CTRLB_ACKACT      18
1680 #define  SAMD21_SERCOM_CTRLB_ACKACT_ACK         0
1681 #define  SAMD21_SERCOM_CTRLB_ACKACT_NACK        1
1682 #define SAMD21_SERCOM_CTRLB_FIFOCLR     22
1683
1684 /* SPI mode */
1685 #define SAMD21_SERCOM_CTRLB_CHSIZE      0
1686 # define SAMD21_SERCOM_CTRLB_CHSIZE_8           0
1687 #define SAMD21_SERCOM_CTRLB_PLOADEN     6
1688 #define SAMD21_SERCOM_CTRLB_SSDE        9
1689 #define SAMD21_SERCOM_CTRLB_MSSEN       13
1690 #define SAMD21_SERCOM_CTRLB_AMODE       14
1691 #define SAMD21_SERCOM_CTRLB_RXEN        17
1692
1693 /* USART mode */
1694 #define SAMD21_SERCOM_INTFLAG_DRE       0
1695 #define SAMD21_SERCOM_INTFLAG_TXC       1
1696 #define SAMD21_SERCOM_INTFLAG_RXC       2
1697 #define SAMD21_SERCOM_INTFLAG_RXS       3
1698 #define SAMD21_SERCOM_INTFLAG_CTSIC     4
1699 #define SAMD21_SERCOM_INTFLAG_RXBRK     5
1700 #define SAMD21_SERCOM_INTFLAG_ERROR     7
1701
1702 /* I2C mode */
1703 #define SAMD21_SERCOM_INTFLAG_ERROR     7
1704 #define SAMD21_SERCOM_INTFLAG_RXFF      4
1705 #define SAMD21_SERCOM_INTFLAG_TXFE      3
1706 #define SAMD21_SERCOM_INTFLAG_SB        1
1707 #define SAMD21_SERCOM_INTFLAG_MB        0
1708
1709 /* SPI mode */
1710 #define SAMD21_SERCOM_INTFLAG_SSL       3
1711
1712 #define SAMD21_SERCOM_INTENCLR_DRE      0
1713 #define SAMD21_SERCOM_INTENCLR_TXC      1
1714 #define SAMD21_SERCOM_INTENCLR_RXC      2
1715 #define SAMD21_SERCOM_INTENCLR_RXS      3
1716 #define SAMD21_SERCOM_INTENCLR_CTSIC    4
1717 #define SAMD21_SERCOM_INTENCLR_RXBRK    5
1718 #define SAMD21_SERCOM_INTENCLR_ERROR    7
1719
1720 #define SAMD21_SERCOM_STATUS_PERR       0
1721 #define SAMD21_SERCOM_STATUS_FERR       1
1722 #define SAMD21_SERCOM_STATUS_BUFOVF     2
1723 #define SAMD21_SERCOM_STATUS_CTS        3
1724 #define SAMD21_SERCOM_STATUS_ISF        4
1725 #define SAMD21_SERCOM_STATUS_COLL       5
1726 #define SAMD21_SERCOM_STATUS_TXE        6
1727
1728 #define SAMD21_SERCOM_SYNCBUSY_SWRST    0
1729 #define SAMD21_SERCOM_SYNCBUSY_ENABLE   1
1730 #define SAMD21_SERCOM_SYNCBUSY_CTRLB    2
1731 #define SAMD21_SERCOM_SYNCBUSY_SYSOP    2
1732
1733 #define SAMD21_SERCOM_ADDR_ADDR         0
1734 #define SAMD21_SERCOM_ADDR_LENEN        13
1735 #define SAMD21_SERCOM_ADDR_HS           14
1736 #define SAMD21_SERCOM_ADDR_TENBITEN     15
1737 #define SAMD21_SERCOM_ADDR_LEN          16
1738
1739 #define SAMD21_SERCOM_DBGCTRL_DBGSTOP   0
1740
1741 #define SAMD21_SERCOM_FIFOSPACE_TXSPACE 0
1742 #define  SAMD21_SERCOM_FIFOSPACE_TXSPACE_MASK   0x1f
1743 #define SAMD21_SERCOM_FIFOSPACE_RXSPACE 8
1744 #define  SAMD21_SERCOM_FIFOSPACE_RXSPACE_MASK   0x1f
1745
1746 #define SAMD21_SERCOM_FIFOPTR_CPUWRPTR  0
1747 #define  SAMD21_SERCOM_FIFOPTR_CPUWRPTR_MASK    0xf
1748 #define SAMD21_SERCOM_FIFOPTR_CPURDPTR  8
1749 #define  SAMD21_SERCOM_FIFOPTR_CPURDPTR_MASK    0xf
1750
1751 /* The SYSTICK starts at 0xe000e010 */
1752 struct samd21_systick {
1753         vuint32_t       csr;
1754         vuint32_t       rvr;
1755         vuint32_t       cvr;
1756         vuint32_t       calib;
1757 };
1758
1759 extern struct samd21_systick samd21_systick;
1760
1761 #define samd21_systick (*(struct samd21_systick *) 0xe000e010)
1762
1763 #define SAMD21_SYSTICK_CSR_ENABLE       0
1764 #define SAMD21_SYSTICK_CSR_TICKINT      1
1765 #define SAMD21_SYSTICK_CSR_CLKSOURCE    2
1766 #define  SAMD21_SYSTICK_CSR_CLKSOURCE_EXTERNAL          0
1767 #define  SAMD21_SYSTICK_CSR_CLKSOURCE_HCLK_8            1
1768 #define SAMD21_SYSTICK_CSR_COUNTFLAG    16
1769
1770 #define SAMD21_SYSTICK_PRI              15
1771
1772 /* The NVIC starts at 0xe000e100, so add that to the offsets to find the absolute address */
1773
1774 struct samd21_nvic {
1775         vuint32_t       iser;           /* 0x000 0xe000e100 Set Enable Register */
1776
1777         uint8_t         _unused020[0x080 - 0x004];
1778
1779         vuint32_t       icer;           /* 0x080 0xe000e180 Clear Enable Register */
1780
1781         uint8_t         _unused0a0[0x100 - 0x084];
1782
1783         vuint32_t       ispr;           /* 0x100 0xe000e200 Set Pending Register */
1784
1785         uint8_t         _unused120[0x180 - 0x104];
1786
1787         vuint32_t       icpr;           /* 0x180 0xe000e280 Clear Pending Register */
1788
1789         uint8_t         _unused1a0[0x300 - 0x184];
1790
1791         vuint32_t       ipr[8];         /* 0x300 0xe000e400 Priority Register */
1792 };
1793
1794 extern struct samd21_nvic samd21_nvic;
1795
1796 #define samd21_nvic (*(struct samd21_nvic *) 0xe000e100)
1797
1798 #define SAMD21_NVIC_ISR_PM_POS          0
1799 #define SAMD21_NVIC_ISR_SYSCTRL_POS     1
1800 #define SAMD21_NVIC_ISR_WDT_POS         2
1801 #define SAMD21_NVIC_ISR_RTC_POS         3
1802 #define SAMD21_NVIC_ISR_EIC_POS         4
1803 #define SAMD21_NVIC_ISR_NVMCTRL_POS     5
1804 #define SAMD21_NVIC_ISR_DMAC_POS        6
1805 #define SAMD21_NVIC_ISR_USB_POS         7
1806 #define SAMD21_NVIC_ISR_EVSYS_POS       8
1807 #define SAMD21_NVIC_ISR_SERCOM0_POS     9
1808 #define SAMD21_NVIC_ISR_SERCOM1_POS     10
1809 #define SAMD21_NVIC_ISR_SERCOM2_POS     11
1810 #define SAMD21_NVIC_ISR_SERCOM3_POS     12
1811 #define SAMD21_NVIC_ISR_SERCOM4_POS     13
1812 #define SAMD21_NVIC_ISR_SERCOM5_POS     14
1813 #define SAMD21_NVIC_ISR_TCC0_POS        15
1814 #define SAMD21_NVIC_ISR_TCC1_POS        16
1815 #define SAMD21_NVIC_ISR_TCC2_POS        17
1816 #define SAMD21_NVIC_ISR_TC3_POS         18
1817 #define SAMD21_NVIC_ISR_TC4_POS         19
1818 #define SAMD21_NVIC_ISR_TC5_POS         20
1819 #define SAMD21_NVIC_ISR_TC6_POS         21
1820 #define SAMD21_NVIC_ISR_TC7_POS         22
1821 #define SAMD21_NVIC_ISR_ADC_POS         23
1822 #define SAMD21_NVIC_ISR_AC_POS          24
1823 #define SAMD21_NVIC_ISR_DAC_POS         25
1824 #define SAMD21_NVIC_ISR_PTC_POS         26
1825 #define SAMD21_NVIC_ISR_I2S_POS         27
1826 #define SAMD21_NVIC_ISR_AC1_POS         28
1827 #define SAMD21_NVIC_ISR_TCC3_POS        29
1828
1829 #define IRQ_MASK(irq)   (1 << (irq))
1830 #define IRQ_BOOL(v,irq) (((v) >> (irq)) & 1)
1831
1832 static inline void
1833 samd21_nvic_set_enable(int irq) {
1834         samd21_nvic.iser = IRQ_MASK(irq);
1835 }
1836
1837 static inline void
1838 samd21_nvic_clear_enable(int irq) {
1839         samd21_nvic.icer = IRQ_MASK(irq);
1840 }
1841
1842 static inline int
1843 samd21_nvic_enabled(int irq) {
1844         return IRQ_BOOL(samd21_nvic.iser, irq);
1845 }
1846
1847 static inline void
1848 samd21_nvic_set_pending(int irq) {
1849         samd21_nvic.ispr = IRQ_MASK(irq);
1850 }
1851
1852 static inline void
1853 samd21_nvic_clear_pending(int irq) {
1854         samd21_nvic.icpr = IRQ_MASK(irq);
1855 }
1856
1857 static inline int
1858 samd21_nvic_pending(int irq) {
1859         return IRQ_BOOL(samd21_nvic.ispr, irq);
1860 }
1861
1862 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
1863 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
1864 #define IRQ_PRIO_MASK(irq)      (0xffU << IRQ_PRIO_BIT(irq))
1865
1866 static inline void
1867 samd21_nvic_set_priority(int irq, uint8_t prio) {
1868         int             n = IRQ_PRIO_REG(irq);
1869         uint32_t        v;
1870
1871         v = samd21_nvic.ipr[n];
1872         v &= ~IRQ_PRIO_MASK(irq);
1873         v |= (prio) << IRQ_PRIO_BIT(irq);
1874         samd21_nvic.ipr[n] = v;
1875 }
1876
1877 static inline uint8_t
1878 samd21_nvic_get_priority(int irq) {
1879         return (samd21_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
1880 }
1881
1882
1883
1884 /* Cortex M0+ SCB */
1885
1886 struct samd21_scb {
1887         vuint32_t       cpuid;
1888         vuint32_t       icsr;
1889         vuint32_t       vtor;
1890         vuint32_t       aircr;
1891
1892         vuint32_t       scr;
1893         vuint32_t       ccr;
1894         vuint32_t       shpr1;
1895         vuint32_t       shpr2;
1896
1897         vuint32_t       shpr3;
1898         vuint32_t       shcrs;
1899         vuint32_t       cfsr;
1900         vuint32_t       hfsr;
1901
1902         uint32_t        unused_30;
1903         vuint32_t       mmfar;
1904         vuint32_t       bfar;
1905 };
1906
1907 extern struct samd21_scb samd21_scb;
1908
1909 #define samd21_scb (*(struct samd21_scb *) 0xe000ed00)
1910
1911 #define SAMD21_SCB_AIRCR_VECTKEY        16
1912 #define  SAMD21_SCB_AIRCR_VECTKEY_KEY           0x05fa
1913 #define SAMD21_SCB_AIRCR_PRIGROUP       8
1914 #define SAMD21_SCB_AIRCR_SYSRESETREQ    2
1915 #define SAMD21_SCB_AIRCR_VECTCLRACTIVE  1
1916 #define SAMD21_SCB_AIRCR_VECTRESET      0
1917
1918 /* The NVM Calibration and auxiliary space starts at 0x00800000 */
1919
1920 struct samd21_aux0 {
1921         vuint64_t       userrow;
1922 };
1923
1924 extern struct samd21_aux0 samd21_aux0;
1925
1926 #define samd21_aux0 (*(struct samd21_aux0 *) 0x00804000)
1927
1928 #define SAMD21_AUX0_USERROW_BOOTPROT    0
1929 #define SAMD21_AUX0_USERROW_EEPROM              4
1930 #define SAMD21_AUX0_USERROW_BOD33_LEVEL 8
1931 #define SAMD21_AUX0_USERROW_BOD33_ENABLE        14
1932 #define SAMD21_AUX0_USERROW_BOD33_ACTION        15
1933 #define SAMD21_AUX0_USERROW_WDT_ENABLE  25
1934 #define SAMD21_AUX0_USERROW_WDT_ALWAYS_ON       26
1935 #define SAMD21_AUX0_USERROW_WDT_PERIOD  27
1936 #define SAMD21_AUX0_USERROW_WDT_WINDOW  31
1937 #define SAMD21_AUX0_USERROW_WDT_EWOFFSET        35
1938 #define SAMD21_AUX0_USERROW_WDT_WEN             39
1939 #define SAMD21_AUX0_USERROW_BOD33_HYST  40
1940 #define SAMD21_AUX0_USERROW_LOCK                48
1941
1942 struct samd21_aux1 {
1943         vuint64_t       reserved_00;
1944         vuint64_t       device_config;
1945
1946         vuint64_t       reserved_10;
1947         vuint64_t       reserved_18;
1948
1949         vuint64_t       calibration;
1950         vuint64_t       reserved_28;
1951 };
1952
1953 extern struct samd21_aux1 samd21_aux1;
1954
1955 #define samd21_aux1 (*(struct samd21_aux1 *) 0x00806000)
1956
1957 #define SAMD21_AUX1_CALIBRATION_ADC_LINEARITY   27
1958 #define  SAMD21_AUX1_CALIBRATION_ADC_LINEARITY_MASK     0xff
1959 #define SAMD21_AUX1_CALIBRATION_ADC_BIASCAL     35
1960 #define  SAMD21_AUX1_CALIBRATION_ADC_BIASCAL_MASK       0x7
1961 #define SAMD21_AUX1_CALIBRATION_OSC32K_CAL      38
1962 #define SAMD21_AUX1_CALIBRATION_USB_TRANSN      45
1963 #define  SAMD21_AUX1_CALIBRATION_USB_TRANSN_MASK        0x1f
1964 #define SAMD21_AUX1_CALIBRATION_USB_TRANSP      50
1965 #define  SAMD21_AUX1_CALIBRATION_USB_TRANSP_MASK        0x1f
1966 #define SAMD21_AUX1_CALIBRATION_USB_TRIM        55
1967 #define  SAMD21_AUX1_CALIBRATION_USB_TRIM_MASK          0x07
1968 #define SAMD21_AUX1_CALIBRATION_DFLL48M_COARSE_CAL      58
1969 #define  SAMD21_AUX1_CALIBRATION_DFLL48M_COARSE_CAL_MASK        0x3f
1970
1971 struct samd21_serial {
1972         vuint32_t       reserved_00;
1973         vuint32_t       reserved_04;
1974         vuint32_t       reserved_08;
1975         vuint32_t       word0;
1976
1977         vuint32_t       reserved_10;
1978         vuint32_t       reserved_14;
1979         vuint32_t       reserved_18;
1980         vuint32_t       reserved_1c;
1981
1982         vuint32_t       reserved_20;
1983         vuint32_t       reserved_24;
1984         vuint32_t       reserved_28;
1985         vuint32_t       reserved_2c;
1986
1987         vuint32_t       reserved_30;
1988         vuint32_t       reserved_34;
1989         vuint32_t       reserved_38;
1990         vuint32_t       reserved_3c;
1991
1992         vuint32_t       word1;
1993         vuint32_t       word2;
1994         vuint32_t       word3;
1995         vuint32_t       reserved_4c;
1996 };
1997
1998 extern struct samd21_serial samd21_serial;
1999
2000 #define samd21_serial (*(struct samd21_serial *) 0x0080a000)
2001
2002 static inline void
2003 samd21_gclk_wait_sync(void)
2004 {
2005         while (samd21_gclk.status & (1 << SAMD21_GCLK_STATUS_SYNCBUSY))
2006                 ;
2007 }
2008
2009 static inline void
2010 samd21_dfll_wait_sync(void)
2011 {
2012         while ((samd21_sysctrl.pclksr & (1 << SAMD21_SYSCTRL_PCLKSR_DFLLRDY)) == 0)
2013                 ;
2014 }
2015
2016 static inline void
2017 samd21_gclk_gendiv(uint32_t id, uint32_t div)
2018 {
2019         if (div == 1)
2020                 div = 0;
2021         samd21_gclk.gendiv = ((id << SAMD21_GCLK_GENDIV_ID) |
2022                               (div << SAMD21_GCLK_GENDIV_DIV));
2023         samd21_gclk_wait_sync();
2024 }
2025
2026 static inline void
2027 samd21_gclk_genctrl(uint32_t src, uint32_t id)
2028 {
2029         samd21_gclk.genctrl = ((id << SAMD21_GCLK_GENCTRL_ID) |
2030                                (src << SAMD21_GCLK_GENCTRL_SRC) |
2031                                (0 << SAMD21_GCLK_GENCTRL_OE) |
2032                                (1 << SAMD21_GCLK_GENCTRL_GENEN));
2033         samd21_gclk_wait_sync();
2034 }
2035
2036 static inline void
2037 samd21_gclk_clkctrl(uint32_t gen, uint32_t id)
2038 {
2039         samd21_gclk.clkctrl = (uint16_t) ((gen << SAMD21_GCLK_CLKCTRL_GEN) |
2040                                           (id << SAMD21_GCLK_CLKCTRL_ID) |
2041                                           (1U << SAMD21_GCLK_CLKCTRL_CLKEN));
2042         samd21_gclk_wait_sync();
2043 }
2044
2045 #define isr_decl(name) \
2046         void samd21_ ## name ## _isr(void)
2047
2048 isr_decl(halt);
2049 isr_decl(ignore);
2050 isr_decl(nmi);
2051 isr_decl(hardfault);
2052 isr_decl(memmanage);
2053 isr_decl(busfault);
2054 isr_decl(usagefault);
2055 isr_decl(svc);
2056 isr_decl(debugmon);
2057 isr_decl(pendsv);
2058 isr_decl(systick);
2059 isr_decl(pm);           /* IRQ0 */
2060 isr_decl(sysctrl);
2061 isr_decl(wdt);
2062 isr_decl(rtc);
2063 isr_decl(eic);
2064 isr_decl(nvmctrl);
2065 isr_decl(dmac);
2066 isr_decl(usb);
2067 isr_decl(evsys);
2068 isr_decl(sercom0);
2069 isr_decl(sercom1);
2070 isr_decl(sercom2);
2071 isr_decl(sercom3);
2072 isr_decl(sercom4);
2073 isr_decl(sercom5);
2074 isr_decl(tcc0);
2075 isr_decl(tcc1);
2076 isr_decl(tcc2);
2077 isr_decl(tc3);
2078 isr_decl(tc4);
2079 isr_decl(tc5);
2080 isr_decl(tc6);
2081 isr_decl(tc7);
2082 isr_decl(adc);
2083 isr_decl(ac);
2084 isr_decl(dac);
2085 isr_decl(ptc);
2086 isr_decl(i2s);
2087 isr_decl(ac1);
2088 isr_decl(tcc3);
2089
2090 #undef isr_decl
2091
2092 #endif /* _SAMD21_H_ */