altos/stm32f4: Wrong value for CK48MSEL_PLL_Q
[fw/altos] / src / stm32f4 / stm32f4.h
1 /*
2  * Copyright © 2018 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 _STM32F4_H_
16 #define _STM32F4_H_
17
18 #include <stdint.h>
19
20 typedef volatile uint32_t       vuint32_t;
21 typedef volatile void *         vvoid_t;
22 typedef volatile uint16_t       vuint16_t;
23 typedef volatile uint8_t        vuint8_t;
24
25 struct stm_pwr {
26         vuint32_t       cr;
27         vuint32_t       csr;
28 };
29
30 extern struct stm_pwr stm_pwr;
31
32 #define stm_pwr (*((struct stm_pwr *) 0x40007000))
33
34 #define STM_PWR_CR_FISSR        21
35 #define STM_PWR_CR_FMSSR        20
36 #define STM_PWR_CR_VOS          14
37 #define  STM_PWR_CR_VOS_SCALE_MODE_3    1
38 #define  STM_PWR_CR_VOS_SCALE_MODE_2    2
39 #define  STM_PWR_CR_VOS_SCALE_MODE_1    3
40 #define  STM_PWR_CR_VOS_SCALE_MODE_MASK 3
41 #define STM_PWR_CR_ADCDC1       13
42 #define STM_PWR_CR_MRLVDS       11
43 #define STM_PWR_CR_LPLVDS       10
44 #define STM_PWR_CR_FPDS         9
45 #define STM_PWR_CR_DBP          8
46 #define STM_PWR_CR_PLS          5
47 #define STM_PWR_CR_PVDE         4
48 #define STM_PWR_CR_CSBF         3
49 #define STM_PWR_CR_CWUF         2
50 #define STM_PWR_CR_PDDS         1
51 #define STM_PWR_CR_LPDS         0
52
53 struct stm_rcc {
54         vuint32_t       cr;
55         vuint32_t       pllcfgr;
56         vuint32_t       cfgr;
57         vuint32_t       cir;
58
59         vuint32_t       ahb1rstr;
60         vuint32_t       ahb2rstr;
61         vuint32_t       ahb3rstr;
62         uint32_t        pad_1c;
63
64         vuint32_t       apb1rstr;
65         vuint32_t       apb2rstr;
66         vuint32_t       pad_28;
67         vuint32_t       pad_2c;
68
69         vuint32_t       _ahb1enr;
70         vuint32_t       _ahb2enr;
71         vuint32_t       ahbdnr;
72         vuint32_t       pad_3c;
73
74         vuint32_t       apb1enr;
75         vuint32_t       apb2enr;
76         vuint32_t       pad_48;
77         vuint32_t       pad_4c;
78
79         vuint32_t       ahb1lpenr;
80         vuint32_t       ahb2lpenr;
81         vuint32_t       ahb3lpenr;
82         vuint32_t       pad_5c;
83
84         vuint32_t       apb1lpenr;
85         vuint32_t       apb2lpenr;
86         vuint32_t       pad_68;
87         vuint32_t       pad_6c;
88
89         vuint32_t       bdcr;
90         vuint32_t       csr;
91         vuint32_t       pad_78;
92         vuint32_t       pad_7c;
93
94         vuint32_t       sscgr;
95         vuint32_t       plli2scfgr;
96         vuint32_t       pad_88;
97         vuint32_t       dckcfgr;
98
99         vuint32_t       ckgatenr;
100         vuint32_t       dckcfgr2;
101 };
102
103 extern struct stm_rcc stm_rcc;
104
105 #define stm_rcc (*((struct stm_rcc *) 0x40023800))
106
107 /* Internal HSI is 16MHz */
108 #define STM_HSI_FREQ            16000000
109
110 #define STM_RCC_CR_PLLI2SRDY    (27)
111 #define STM_RCC_CR_PLLI2SON     (26)
112 #define STM_RCC_CR_PLLRDY       (25)
113 #define STM_RCC_CR_PLLON        (24)
114 #define STM_RCC_CR_CSSON        (19)
115 #define STM_RCC_CR_HSEBYP       (18)
116 #define STM_RCC_CR_HSERDY       (17)
117 #define STM_RCC_CR_HSEON        (16)
118 #define STM_RCC_CR_HSICAL       (8)
119 #define STM_RCC_CR_HSITRIM      (3)
120 #define STM_RCC_CR_HSIRDY       (1)
121 #define STM_RCC_CR_HSION        (0)
122
123 #define STM_RCC_PLLCFGR_PLLM    0
124 #define  STM_RCC_PLLCFGR_PLLM_MASK      0x3f
125 #define STM_RCC_PLLCFGR_PLLN    6
126 #define  STM_RCC_PLLCFGR_PLLN_MASK      0x1ff
127 #define STM_RCC_PLLCFGR_PLLP    16
128 #define  STM_RCC_PLLCFGR_PLLP_DIV_2     0
129 #define  STM_RCC_PLLCFGR_PLLP_DIV_4     1
130 #define  STM_RCC_PLLCFGR_PLLP_DIV_6     2
131 #define  STM_RCC_PLLCFGR_PLLP_DIV_8     3
132 #define  STM_RCC_PLLCFGR_PLLP_MASK      0x3
133 #define STM_RCC_PLLCFGR_PLLSRC  22
134 #define  STM_RCC_PLLCFGR_PLLSRC_HSI     0
135 #define  STM_RCC_PLLCFGR_PLLSRC_HSE     1
136 #define STM_RCC_PLLCFGR_PLLQ    24
137 #define  STM_RCC_PLLCFGR_PLLQ_MASK      0xf
138 #define STM_RCC_PLLCFGR_PLLR    28
139 #define  STM_RCC_PLLCFGR_PLLR_MASK      0x7
140
141 #define STM_RCC_CFGR_MCO2       (30)
142 #define STM_RCC_CFGR_MCO2PRE    (27)
143 #define STM_RCC_CFGR_MCO1PRE    (24)
144 #define STM_RCC_CFGR_MCO1       (21)
145 #define STM_RCC_CFGR_RTCPRE     (16)
146
147 #define STM_RCC_CFGR_PPRE2      (13)
148 #define  STM_RCC_CFGR_PPRE2_DIV_1       0
149 #define  STM_RCC_CFGR_PPRE2_DIV_2       4
150 #define  STM_RCC_CFGR_PPRE2_DIV_4       5
151 #define  STM_RCC_CFGR_PPRE2_DIV_8       6
152 #define  STM_RCC_CFGR_PPRE2_DIV_16      7
153 #define  STM_RCC_CFGR_PPRE2_MASK        7
154
155 #define STM_RCC_CFGR_PPRE1      (10)
156 #define  STM_RCC_CFGR_PPRE1_DIV_1       0
157 #define  STM_RCC_CFGR_PPRE1_DIV_2       4
158 #define  STM_RCC_CFGR_PPRE1_DIV_4       5
159 #define  STM_RCC_CFGR_PPRE1_DIV_8       6
160 #define  STM_RCC_CFGR_PPRE1_DIV_16      7
161 #define  STM_RCC_CFGR_PPRE1_MASK        7
162
163 #define STM_RCC_CFGR_HPRE       (4)
164 #define  STM_RCC_CFGR_HPRE_DIV_1        0x0
165 #define  STM_RCC_CFGR_HPRE_DIV_2        0x8
166 #define  STM_RCC_CFGR_HPRE_DIV_4        0x9
167 #define  STM_RCC_CFGR_HPRE_DIV_8        0xa
168 #define  STM_RCC_CFGR_HPRE_DIV_16       0xb
169 #define  STM_RCC_CFGR_HPRE_DIV_64       0xc
170 #define  STM_RCC_CFGR_HPRE_DIV_128      0xd
171 #define  STM_RCC_CFGR_HPRE_DIV_256      0xe
172 #define  STM_RCC_CFGR_HPRE_DIV_512      0xf
173 #define  STM_RCC_CFGR_HPRE_MASK         0xf
174
175 #define STM_RCC_CFGR_SWS        (2)
176 #define  STM_RCC_CFGR_SWS_HSI           0
177 #define  STM_RCC_CFGR_SWS_HSE           1
178 #define  STM_RCC_CFGR_SWS_PLL           2
179 #define  STM_RCC_CFGR_SWS_MASK          3
180
181 #define STM_RCC_CFGR_SW         (0)
182 #define  STM_RCC_CFGR_SW_HSI            0
183 #define  STM_RCC_CFGR_SW_HSE            1
184 #define  STM_RCC_CFGR_SW_PLL            2
185 #define  STM_RCC_CFGR_SW_MASK           3
186
187 #define STM_RCC_AHB1ENR_IOPAEN  0
188 #define STM_RCC_AHB1ENR_IOPBEN  1
189 #define STM_RCC_AHB1ENR_IOPCEN  2
190 #define STM_RCC_AHB1ENR_IOPDEN  3
191 #define STM_RCC_AHB1ENR_IOPEEN  4
192 #define STM_RCC_AHB1ENR_IOPFEN  5
193 #define STM_RCC_AHB1ENR_IOPGEN  6
194 #define STM_RCC_AHB1ENR_IOPHEN  7
195
196 #define STM_RCC_AHB2ENR_OTGFSEN         7
197 #define STM_RCC_AHB2ENR_RNGEN           6
198 #define STM_RCC_AHB2ENR_CRYPEN          4
199
200 #define STM_RCC_APB1ENR_UART8EN         31
201 #define STM_RCC_APB1ENR_UART7EN         30
202 #define STM_RCC_APB1ENR_DACEN           29
203 #define STM_RCC_APB1ENR_PWREN           28
204 #define STM_RCC_APB1ENR_CAN3EN          27
205 #define STM_RCC_APB1ENR_CAN2EN          26
206 #define STM_RCC_APB1ENR_CAN1EN          25
207 #define STM_RCC_APB1ENR_I2CFMP1EN       24
208 #define STM_RCC_APB1ENR_I2C3EN          23
209 #define STM_RCC_APB1ENR_I2C2EN          22
210 #define STM_RCC_APB1ENR_I2C1EN          21
211 #define STM_RCC_APB1ENR_UART5EN         20
212 #define STM_RCC_APB1ENR_UART4EN         19
213 #define STM_RCC_APB1ENR_USART3EN        18
214 #define STM_RCC_APB1ENR_USART2EN        17
215 #define STM_RCC_APB1ENR_SPI3EN          15
216 #define STM_RCC_APB1ENR_SPI2EN          14
217 #define STM_RCC_APB1ENR_WWDGEN          11
218 #define STM_RCC_APB1ENR_RTCAPBEN        10
219 #define STM_RCC_APB1ENR_LPTIMER1EN      9
220 #define STM_RCC_APB1ENR_TIM14EN         8
221 #define STM_RCC_APB1ENR_TIM13EN         7
222 #define STM_RCC_APB1ENR_TIM12EN         6
223 #define STM_RCC_APB1ENR_TIM7EN          5
224 #define STM_RCC_APB1ENR_TIM6EN          4
225 #define STM_RCC_APB1ENR_TIM5EN          3
226 #define STM_RCC_APB1ENR_TIM4EN          2
227 #define STM_RCC_APB1ENR_TIM3EN          1
228 #define STM_RCC_APB1ENR_TIM2EN          0
229
230 #define STM_RCC_APB2ENR_DFSDM2EN        25
231 #define STM_RCC_APB2ENR_DFSDM1EN        24
232 #define STM_RCC_APB2ENR_SAI1EN          22
233 #define STM_RCC_APB2ENR_SPI5EN          20
234 #define STM_RCC_APB2ENR_TIM11EN         18
235 #define STM_RCC_APB2ENR_TIM10EN         17
236 #define STM_RCC_APB2ENR_TIM9EN          16
237 #define STM_RCC_APB2ENR_EXITEN          15
238 #define STM_RCC_APB2ENR_SYSCFGEN        14
239 #define STM_RCC_APB2ENR_SPI4EN          13
240 #define STM_RCC_APB2ENR_SPI1EN          12
241 #define STM_RCC_APB2ENR_SDIOEN          11
242 #define STM_RCC_APB2ENR_ADC1EN          8
243 #define STM_RCC_APB2ENR_UART10EN        7
244 #define STM_RCC_APB2ENR_UART9EN         5
245 #define STM_RCC_APB2ENR_USART6EN        5
246 #define STM_RCC_APB2ENR_USART1EN        4
247 #define STM_RCC_APB2ENR_TIM8EN          1
248 #define STM_RCC_APB2ENR_TIM1EN          0
249
250 #define STM_RCC_CSR_RMVF                24
251
252 #define STM_RCC_DCKCFGR_CKDFSDMSEL      31
253 #define STM_RCC_DCKCFGR_I2S2SRC         27
254 #define STM_RCC_DCKCFGR_I2S1SRC         25
255 #define STM_RCC_DCKCFGR_TIMPRE          24
256 #define STM_RCC_DCKCFGR_SAII1BSRC       22
257 #define STM_RCC_DCKCFGR_SAII1ASRC       20
258 #define STM_RCC_DCKCFGR_CKDFSDM1ASEL    15
259 #define STM_RCC_DCKCFGR_CKDFSDM2ASEL    14
260 #define STM_RCC_DCKCFGR_PLLDIVR         8
261 #define STM_RCC_DCKCFGR_PLLI2SDIVR      0
262
263 #define STM_RCC_DCKCFGR2_LPTIMER1SEL    30
264 #define  STM_RCC_DCKCFGR2_LPTIMER1SEL_APB       0
265 #define  STM_RCC_DCKCFGR2_LPTIMER1SEL_HSI       1
266 #define  STM_RCC_DCKCFGR2_LPTIMER1SEL_LSI       2
267 #define  STM_RCC_DCKCFGR2_LPTIMER1SEL_LSE       3
268 #define STM_RCC_DCKCFGR2_SDIOSEL        28
269 #define  STM_RCC_DCKCFGR2_SDIOSEL_CK_48MHZ      0
270 #define  STM_RCC_DCKCFGR2_SDIOSEL_SYSTEM_CLOCK  1
271 #define STM_RCC_DCKCFGR2_CK48MSEL       27
272 #define  STM_RCC_DCKCFGR2_CK48MSEL_PLL_Q        0
273 #define  STM_RCC_DCKCFGR2_CK48MSEL_PLLI2S_Q     1
274 #define STM_RCC_DCKCFGR2_I2CFMP1SEL     22
275 #define  STM_RCC_DCKCFGR2_I2CFMP1SEL_APB                0
276 #define  STM_RCC_DCKCFGR2_I2CFMP1SEL_SYSTEM_CLOCK       1
277 #define  STM_RCC_DCKCFGR2_I2CFMP1SEL_HSI                2
278 #define  STM_RCC_DCKCFGR2_I2CFMP1SEL_APB_ALSO           3
279
280 static inline void
281 stm_rcc_ahb1_clk_enable(uint32_t bit)
282 {
283         stm_rcc._ahb1enr |= bit;
284         uint32_t value = stm_rcc._ahb1enr;
285         (void) value;
286 }
287
288 static inline void
289 stm_rcc_ahb1_clk_disable(uint32_t bit)
290 {
291         stm_rcc._ahb1enr &= ~bit;
292         uint32_t value = stm_rcc._ahb1enr;
293         (void) value;
294 }
295
296 static inline void
297 stm_rcc_ahb2_clk_enable(uint32_t bit)
298 {
299         stm_rcc._ahb2enr |= bit;
300         uint32_t value = stm_rcc._ahb2enr;
301         (void) value;
302 }
303
304 static inline void
305 stm_rcc_ahb2_clk_disable(uint32_t bit)
306 {
307         stm_rcc._ahb2enr &= ~bit;
308         uint32_t value = stm_rcc._ahb2enr;
309         (void) value;
310 }
311
312 struct stm_ictr {
313         vuint32_t       ictr;
314 };
315
316 extern struct stm_ictr stm_ictr;
317
318 #define stm_ictr (*((struct stm_ictr *) 0xe000e004))
319
320 #define STM_ICTR_ICTR_INTLINESNUM       0
321 #define  STM_ICTR_ICTR_INTLINESNUM_MASK         0xf
322
323 struct stm_nvic {
324         vuint32_t       iser[8];        /* 0x000 0xe000e100 Set Enable Register */
325
326         uint8_t         _unused020[0x080 - 0x020];
327
328         vuint32_t       icer[8];        /* 0x080 0xe000e180 Clear Enable Register */
329
330         uint8_t         _unused0a0[0x100 - 0x0a0];
331
332         vuint32_t       ispr[8];        /* 0x100 0xe000e200 Set Pending Register */
333
334         uint8_t         _unused120[0x180 - 0x120];
335
336         vuint32_t       icpr[8];        /* 0x180 0xe000e280 Clear Pending Register */
337
338         uint8_t         _unused1a0[0x200 - 0x1a0];
339
340         vuint32_t       iabr[8];        /* 0x200 0xe000e300 Active Bit Register */
341
342         uint8_t         _unused220[0x300 - 0x220];
343
344         vuint32_t       ipr[60];        /* 0x300 0xe000e400 Priority Register */
345 };
346
347 extern struct stm_nvic stm_nvic;
348
349 #define stm_nvic (*((struct stm_nvic *) 0xe000e100))
350
351 #define IRQ_REG(irq)    ((irq) >> 5)
352 #define IRQ_BIT(irq)    ((irq) & 0x1f)
353 #define IRQ_MASK(irq)   (1 << IRQ_BIT(irq))
354 #define IRQ_BOOL(v,irq) (((v) >> IRQ_BIT(irq)) & 1)
355
356 static inline void
357 stm_nvic_set_enable(int irq) {
358         stm_nvic.iser[IRQ_REG(irq)] = IRQ_MASK(irq);
359 }
360
361 static inline void
362 stm_nvic_clear_enable(int irq) {
363         stm_nvic.icer[IRQ_REG(irq)] = IRQ_MASK(irq);
364 }
365
366 static inline int
367 stm_nvic_enabled(int irq) {
368         return IRQ_BOOL(stm_nvic.iser[IRQ_REG(irq)], irq);
369 }
370
371 static inline void
372 stm_nvic_set_pending(int irq) {
373         stm_nvic.ispr[IRQ_REG(irq)] = IRQ_MASK(irq);
374 }
375
376 static inline void
377 stm_nvic_clear_pending(int irq) {
378         stm_nvic.icpr[IRQ_REG(irq)] = IRQ_MASK(irq);
379 }
380
381 static inline int
382 stm_nvic_pending(int irq) {
383         return IRQ_BOOL(stm_nvic.ispr[IRQ_REG(irq)], irq);
384 }
385
386 static inline int
387 stm_nvic_active(int irq) {
388         return IRQ_BOOL(stm_nvic.iabr[IRQ_REG(irq)], irq);
389 }
390
391 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
392 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
393 #define IRQ_PRIO_MASK(irq)      (0xff << IRQ_PRIO_BIT(irq))
394
395 static inline void
396 stm_nvic_set_priority(int irq, uint8_t prio) {
397         int             n = IRQ_PRIO_REG(irq);
398         uint32_t        v;
399
400         v = stm_nvic.ipr[n];
401         v &= ~IRQ_PRIO_MASK(irq);
402         v |= (prio) << IRQ_PRIO_BIT(irq);
403         stm_nvic.ipr[n] = v;
404 }
405
406 static inline uint8_t
407 stm_nvic_get_priority(int irq) {
408         return (stm_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
409 }
410
411 #define isr(name) void stm_ ## name ## _isr(void)
412
413 isr(halt);
414 isr(ignore);
415 isr(nmi);
416 isr(hardfault);
417 isr(memmanage);
418 isr(busfault);
419 isr(usagefault);
420 isr(svc);
421 isr(debugmon);
422 isr(pendsv);
423 isr(systick);
424 isr(wwdg);
425 isr(pvd);
426 isr(tamper_stamp);
427 isr(rtc_wkup);
428 isr(flash);
429 isr(rcc);
430 isr(exti0);
431 isr(exti1);
432 isr(exti2);
433 isr(exti3);
434 isr(exti4);
435 isr(dma1_stream0);
436 isr(dma1_stream1);
437 isr(dma1_stream2);
438 isr(dma1_stream3);
439 isr(dma1_stream4);
440 isr(dma1_stream5);
441 isr(dma1_stream6);
442 isr(adc);
443 isr(can1_tx);
444 isr(can1_rx0);
445 isr(can1_rx1);
446 isr(can1_sce);
447 isr(exti9_5);
448 isr(tim1_brk_tim9);
449 isr(tim1_up_tim10);
450 isr(tim_trg_com_tim11);
451 isr(tim1_cc);
452 isr(tim2);
453 isr(tim3);
454 isr(tim4);
455 isr(i2c1_evt);
456 isr(i2c1_err);
457 isr(i2c2_evt);
458 isr(i2c2_err);
459 isr(spi1);
460 isr(spi2);
461 isr(usart1);
462 isr(usart2);
463 isr(usart3);
464 isr(exti15_10);
465 isr(rtc_alarm);
466 isr(otg_fs_wkup);
467 isr(tim8_brk_tim12);
468 isr(tim8_up_tim13);
469 isr(tim8_trg_com_tim14);
470 isr(tim8_cc);
471 isr(dma1_stream7);
472 isr(fsmc);
473 isr(sdio);
474 isr(tim5);
475 isr(spi3);
476 isr(uart4);
477 isr(uart5);
478 isr(tim6_glb_it);
479 isr(tim7);
480 isr(dma2_stream0);
481 isr(dma2_stream1);
482 isr(dma2_stream2);
483 isr(dma2_stream3);
484 isr(dma2_stream4);
485 isr(dfsdm1_flt0);
486 isr(dfsdm1_flt1);
487 isr(can2_tx);
488 isr(can2_rx0);
489 isr(can2_rx1);
490 isr(can2_sce);
491 isr(otg_fs);
492 isr(dma2_stream5);
493 isr(dma2_stream6);
494 isr(dma2_stream7);
495 isr(usart6);
496 isr(i2c3_ev);
497 isr(i2c3_er);
498 isr(can3_tx);
499 isr(can3_rx0);
500 isr(can3_rx1);
501 isr(can3_sce);
502 isr(crypto);
503 isr(rng);
504 isr(fpu);
505 isr(uart7);
506 isr(uart8);
507 isr(spi4);
508 isr(spi5);
509 isr(sai1);
510 isr(uart9);
511 isr(uart10);
512 isr(quad_spi);
513 isr(i2cfmp1_ev);
514 isr(i2cfmp1_er);
515 isr(exti23);
516 isr(dfsdm2_flt0);
517 isr(dfsdm2_flt1);
518 isr(dfsdm2_flt2);
519 isr(dfsdm2_flt3);
520
521 #undef isr
522
523 #define STM_ISR_WWDG_POS                0
524 #define STM_ISR_PVD_POS                 1
525 #define STM_ISR_TAMPER_STAMP_POS        2
526 #define STM_ISR_RTC_WKUP_POS            3
527 #define STM_ISR_FLASH_POS               4
528 #define STM_ISR_RCC_POS                 5
529 #define STM_ISR_EXTI0_POS               6
530 #define STM_ISR_EXTI1_POS               7
531 #define STM_ISR_EXTI2_POS               8
532 #define STM_ISR_EXTI3_POS               9
533 #define STM_ISR_EXTI4_POS               10
534 #define STM_ISR_DMA1_STREAM0_POS        11
535 #define STM_ISR_DMA1_STREAM1_POS        12
536 #define STM_ISR_DMA1_STREAM2_POS        13
537 #define STM_ISR_DMA1_STREAM3_POS        14
538 #define STM_ISR_DMA1_STREAM4_POS        15
539 #define STM_ISR_DMA1_STREAM5_POS        16
540 #define STM_ISR_DMA1_STREAM6_POS        17
541 #define STM_ISR_ADC_POS                 18
542 #define STM_ISR_CAN1_TX_POS             19
543 #define STM_ISR_CAN1_RX0_POS            20
544 #define STM_ISR_CAN1_RX1_POS            21
545 #define STM_ISR_CAN1_SCE_POS            22
546 #define STM_ISR_EXTI9_5_POS             23
547 #define STM_ISR_TIM1_BRK_TIM9_POS       24
548 #define STM_ISR_TIM1_UP_TIM10_POS       25
549 #define STM_ISR_TIM_TRG_COM_TIM11_POS   26
550 #define STM_ISR_TIM1_CC_POS             27
551 #define STM_ISR_TIM2_POS                28
552 #define STM_ISR_TIM3_POS                29
553 #define STM_ISR_TIM4_POS                30
554 #define STM_ISR_I2C1_EVT_POS            31
555 #define STM_ISR_I2C1_ERR_POS            32
556 #define STM_ISR_I2C2_EVT_POS            33
557 #define STM_ISR_I2C2_ERR_POS            34
558 #define STM_ISR_SPI1_POS                35
559 #define STM_ISR_SPI2_POS                36
560 #define STM_ISR_USART1_POS              37
561 #define STM_ISR_USART2_POS              38
562 #define STM_ISR_USART3_POS              39
563 #define STM_ISR_EXTI15_10_POS           40
564 #define STM_ISR_EXTI17_RTC_ALARM_POS    41
565 #define STM_ISR_EXTI18_OTG_FS_WKUP_POS  42
566 #define STM_ISR_TIM2_BRK_TIM12_POS      43
567 #define STM_ISR_TIM8_UP_TIM13_POS       44
568 #define STM_ISR_TIM8_TRG_COM_TIM14_POS  45
569 #define STM_ISR_TIM8_CC_POS             46
570 #define STM_ISR_DMA1_STREAM7_POS        47
571 #define STM_ISR_FSMC_POS                48
572 #define STM_ISR_SDIO_POS                49
573 #define STM_ISR_TIM5_POS                50
574 #define STM_ISR_SPI3_POS                41
575 #define STM_ISR_UART4_POS               52
576 #define STM_ISR_UART5_POS               53
577 #define STM_ISR_TIM6_GLB_IT_DAC1_DAC2_POS       54
578 #define STM_ISR_TIM7_POS                55
579 #define STM_ISR_DMA2_STREAM0_POS        56
580 #define STM_ISR_DMA2_STREAM1_POS        57
581 #define STM_ISR_DMA2_STREAM2_POS        58
582 #define STM_ISR_DMA2_STREAM3_POS        59
583 #define STM_ISR_DMA2_STREAM4_POS        60
584 #define STM_ISR_DFSDM1_FLT0_POS         61
585 #define STM_ISR_DFSDM1_FLT1_POS         62
586 #define STM_ISR_CAN2_TX_POS             63
587 #define STM_ISR_CAN2_RX0_POS            64
588 #define STM_ISR_CAN2_RX1_POS            65
589 #define STM_ISR_CAN2_SCE_POS            66
590 #define STM_ISR_OTG_FS_POS              67
591 #define STM_ISR_DMA2_STREAM5_POS        68
592 #define STM_ISR_DMA2_STREAM6_POS        69
593 #define STM_ISR_DMA2_STREAM7_POS        70
594 #define STM_ISR_USART6_POS              71
595 #define STM_ISR_UART7_POS               82
596 #define STM_ISR_UART9_POS               88
597 #define STM_ISR_UART10_POS              89
598
599 #define STM_ISR_EXTI15_10_POS           40
600
601 struct stm_flash {
602         vuint32_t       acr;
603         vuint32_t       keyr;
604         vuint32_t       optkeyr;
605         vuint32_t       sr;
606
607         vuint32_t       cr;
608         vuint32_t       optcr;
609         vuint32_t       wrpr;
610 };
611
612 extern struct stm_flash stm_flash;
613
614 #define stm_flash (*((struct stm_flash *) 0x40023c00))
615
616 #define STM_FLASH_ACR_DCRST     12
617 #define STM_FLASH_ACR_ICRST     11
618 #define STM_FLASH_ACR_DCEN      10
619 #define STM_FLASH_ACR_ICEN      9
620 #define STM_FLASH_ACR_PRFTEN    8
621 #define STM_FLASH_ACR_LATENCY   0
622
623 struct stm_flash_size {
624         vuint16_t       f_size;
625 };
626
627 extern struct stm_flash_size stm_flash_size;
628
629 #define stm_flash_size  (*((struct stm_flash_size *) 0x1fff7a22))
630
631 struct stm_gpio {
632         vuint32_t       moder;
633         vuint32_t       otyper;
634         vuint32_t       ospeedr;
635         vuint32_t       pupdr;
636
637         vuint32_t       idr;
638         vuint32_t       odr;
639         vuint32_t       bsrr;
640         vuint32_t       lckr;
641
642         vuint32_t       afrl;
643         vuint32_t       afrh;
644 };
645
646 #define STM_MODER_SHIFT(pin)            ((pin) << 1)
647 #define STM_MODER_MASK                  3
648 #define STM_MODER_INPUT                 0
649 #define STM_MODER_OUTPUT                1
650 #define STM_MODER_ALTERNATE             2
651 #define STM_MODER_ANALOG                3
652
653 static inline void
654 stm_moder_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
655         gpio->moder = ((gpio->moder &
656                         ~(STM_MODER_MASK << STM_MODER_SHIFT(pin))) |
657                        value << STM_MODER_SHIFT(pin));
658 }
659
660 static inline uint32_t
661 stm_moder_get(struct stm_gpio *gpio, int pin) {
662         return (gpio->moder >> STM_MODER_SHIFT(pin)) & STM_MODER_MASK;
663 }
664
665 #define STM_OTYPER_SHIFT(pin)           (pin)
666 #define STM_OTYPER_MASK                 1
667 #define STM_OTYPER_PUSH_PULL            0
668 #define STM_OTYPER_OPEN_DRAIN           1
669
670 static inline void
671 stm_otyper_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
672         gpio->otyper = ((gpio->otyper &
673                          ~(STM_OTYPER_MASK << STM_OTYPER_SHIFT(pin))) |
674                         value << STM_OTYPER_SHIFT(pin));
675 }
676
677 static inline uint32_t
678 stm_otyper_get(struct stm_gpio *gpio, int pin) {
679         return (gpio->otyper >> STM_OTYPER_SHIFT(pin)) & STM_OTYPER_MASK;
680 }
681
682 #define STM_OSPEEDR_SHIFT(pin)          ((pin) << 1)
683 #define STM_OSPEEDR_MASK                3
684 #define STM_OSPEEDR_LOW                 0       /* 2-8MHz */
685 #define STM_OSPEEDR_MEDIUM              1       /* 12.5-50MHz */
686 #define STM_OSPEEDR_FAST                2       /* 25-100MHz */
687 #define STM_OSPEEDR_HIGH                3       /* 50-100MHz */
688
689 static inline void
690 stm_ospeedr_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
691         gpio->ospeedr = ((gpio->ospeedr &
692                         ~(STM_OSPEEDR_MASK << STM_OSPEEDR_SHIFT(pin))) |
693                        value << STM_OSPEEDR_SHIFT(pin));
694 }
695
696 static inline uint32_t
697 stm_ospeedr_get(struct stm_gpio *gpio, int pin) {
698         return (gpio->ospeedr >> STM_OSPEEDR_SHIFT(pin)) & STM_OSPEEDR_MASK;
699 }
700
701 #define STM_PUPDR_SHIFT(pin)            ((pin) << 1)
702 #define STM_PUPDR_MASK                  3
703 #define STM_PUPDR_NONE                  0
704 #define STM_PUPDR_PULL_UP               1
705 #define STM_PUPDR_PULL_DOWN             2
706 #define STM_PUPDR_RESERVED              3
707
708 static inline void
709 stm_pupdr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
710         gpio->pupdr = ((gpio->pupdr &
711                         ~(STM_PUPDR_MASK << STM_PUPDR_SHIFT(pin))) |
712                        value << STM_PUPDR_SHIFT(pin));
713 }
714
715 static inline uint32_t
716 stm_pupdr_get(struct stm_gpio *gpio, int pin) {
717         return (gpio->pupdr >> STM_PUPDR_SHIFT(pin)) & STM_PUPDR_MASK;
718 }
719
720 #define STM_AFR_SHIFT(pin)              ((pin) << 2)
721 #define STM_AFR_MASK                    0xf
722 #define STM_AFR_NONE                    0
723 #define STM_AFR_AF0                     0x0
724 #define STM_AFR_AF1                     0x1
725 #define STM_AFR_AF2                     0x2
726 #define STM_AFR_AF3                     0x3
727 #define STM_AFR_AF4                     0x4
728 #define STM_AFR_AF5                     0x5
729 #define STM_AFR_AF6                     0x6
730 #define STM_AFR_AF7                     0x7
731 #define STM_AFR_AF8                     0x8
732 #define STM_AFR_AF9                     0x9
733 #define STM_AFR_AF10                    0xa
734 #define STM_AFR_AF11                    0xb
735 #define STM_AFR_AF12                    0xc
736 #define STM_AFR_AF13                    0xd
737 #define STM_AFR_AF14                    0xe
738 #define STM_AFR_AF15                    0xf
739
740 static inline void
741 stm_afr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
742         /*
743          * Set alternate pin mode too
744          */
745         stm_moder_set(gpio, pin, STM_MODER_ALTERNATE);
746         if (pin < 8)
747                 gpio->afrl = ((gpio->afrl &
748                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
749                               value << STM_AFR_SHIFT(pin));
750         else {
751                 pin -= 8;
752                 gpio->afrh = ((gpio->afrh &
753                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
754                               value << STM_AFR_SHIFT(pin));
755         }
756 }
757
758 static inline uint32_t
759 stm_afr_get(struct stm_gpio *gpio, int pin) {
760         if (pin < 8)
761                 return (gpio->afrl >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
762         else {
763                 pin -= 8;
764                 return (gpio->afrh >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
765         }
766 }
767
768 static inline void
769 stm_gpio_set(struct stm_gpio *gpio, int pin, uint8_t value) {
770         /* Use the bit set/reset register to do this atomically */
771         gpio->bsrr = ((uint32_t) (value ^ 1) << (pin + 16)) | ((uint32_t) value << pin);
772 }
773
774 static inline uint8_t
775 stm_gpio_get(struct stm_gpio *gpio, int pin) {
776         return (gpio->idr >> pin) & 1;
777 }
778
779 static inline uint16_t
780 stm_gpio_get_all(struct stm_gpio *gpio) {
781         return gpio->idr;
782 }
783
784 /*
785  * We can't define these in registers.ld or our fancy
786  * ao_enable_gpio macro will expand into a huge pile of code
787  * as the compiler won't do correct constant folding and
788  * dead-code elimination
789  */
790
791 extern struct stm_gpio stm_gpioa;
792 extern struct stm_gpio stm_gpiob;
793 extern struct stm_gpio stm_gpioc;
794 extern struct stm_gpio stm_gpiod;
795 extern struct stm_gpio stm_gpioe;
796 extern struct stm_gpio stm_gpiof;
797 extern struct stm_gpio stm_gpiog;
798 extern struct stm_gpio stm_gpioh;
799
800 #define stm_gpioa  (*((struct stm_gpio *) 0x40020000))
801 #define stm_gpiob  (*((struct stm_gpio *) 0x40020400))
802 #define stm_gpioc  (*((struct stm_gpio *) 0x40020800))
803 #define stm_gpiod  (*((struct stm_gpio *) 0x40020c00))
804 #define stm_gpioe  (*((struct stm_gpio *) 0x40021000))
805 #define stm_gpiof  (*((struct stm_gpio *) 0x40021400))
806 #define stm_gpiog  (*((struct stm_gpio *) 0x40021800))
807 #define stm_gpioh  (*((struct stm_gpio *) 0x40021c00))
808
809 struct stm_scb {
810         vuint32_t       cpuid;
811         vuint32_t       icsr;
812         vuint32_t       vtor;
813         vuint32_t       aircr;
814
815         vuint32_t       scr;
816         vuint32_t       ccr;
817         vuint32_t       shpr1;
818         vuint32_t       shpr2;
819
820         vuint32_t       shpr3;
821         vuint32_t       shcsr;
822         vuint32_t       cfsr;
823         vuint32_t       hfsr;
824
825         vuint32_t       dfsr;
826         vuint32_t       mmcar;
827         vuint32_t       bcar;
828         vuint32_t       afsr;
829
830         vuint32_t       id_pfr0;
831         vuint32_t       id_pfr1;
832         vuint32_t       id_dfr0;
833         vuint32_t       id_afr0;
834
835         vuint32_t       id_mmfr0;
836         vuint32_t       id_mmfr1;
837         vuint32_t       id_mmfr2;
838         vuint32_t       id_mmfr3;
839
840         vuint32_t       id_isar0;
841         vuint32_t       id_isar1;
842         vuint32_t       id_isar2;
843         vuint32_t       id_isar3;
844
845         vuint32_t       id_isar4;
846         vuint32_t       pad_d74;
847         vuint32_t       pad_d78;
848         vuint32_t       pad_d7c;
849
850         vuint32_t       pad_d80;
851         vuint32_t       pad_d84;
852         vuint32_t       cpacr;
853         vuint32_t       pad_d8c;
854
855         vuint8_t        pad_d90[0xf00 - 0xd90];
856
857         vuint32_t       stir;
858 };
859
860 extern struct stm_scb stm_scb;
861
862 #define stm_scb (*((struct stm_scb *) 0xe000ed00))
863
864 #define STM_SCB_CPACR_CP(n)     ((n) <<1)
865 #define  STM_SCB_CPACR_DENIED           0
866 #define  STM_SCB_CPACR_PRIVILEGED       1
867 #define  STM_SCB_CPACR_RESERVED         2
868 #define  STM_SCB_CPACR_FULL             3
869 #define STM_SCB_CPACR_FP0       STM_SCB_CPACR_CP(10)
870 #define STM_SCB_CPACR_FP1       STM_SCB_CPACR_CP(11)
871
872 #define STM_SCB_AIRCR_VECTKEY           16
873 #define  STM_SCB_AIRCR_VECTKEY_KEY              0x05fa
874 #define STM_SCB_AIRCR_PRIGROUP          8
875 #define STM_SCB_AIRCR_SYSRESETREQ       2
876 #define STM_SCB_AIRCR_VECTCLRACTIVE     1
877 #define STM_SCB_AIRCR_VECTRESET         0
878
879 /* The SYSTICK starts at 0xe000e010 */
880
881 struct stm_systick {
882         vuint32_t       csr;
883         vuint32_t       rvr;
884         vuint32_t       cvr;
885         vuint32_t       calib;
886 };
887
888 extern struct stm_systick stm_systick;
889
890 #define stm_systick     (*((struct stm_systick *) 0xe000e010))
891
892 #define STM_SYSTICK_CSR_ENABLE          0
893 #define STM_SYSTICK_CSR_TICKINT         1
894 #define STM_SYSTICK_CSR_CLKSOURCE       2
895 #define  STM_SYSTICK_CSR_CLKSOURCE_AHB_8                0
896 #define  STM_SYSTICK_CSR_CLKSOURCE_AHB                  1
897 #define STM_SYSTICK_CSR_COUNTFLAG       16
898
899 #define STM_SYSCFG_EXTICR_PA            0
900 #define STM_SYSCFG_EXTICR_PB            1
901 #define STM_SYSCFG_EXTICR_PC            2
902 #define STM_SYSCFG_EXTICR_PD            3
903 #define STM_SYSCFG_EXTICR_PE            4
904 #define STM_SYSCFG_EXTICR_PF            5
905 #define STM_SYSCFG_EXTICR_PG            6
906 #define STM_SYSCFG_EXTICR_PH            7
907
908 struct stm_syscfg {
909         vuint32_t       memrmp;
910         vuint32_t       pmc;
911         vuint32_t       exticr[4];
912 };
913
914 extern struct stm_syscfg stm_syscfg;
915
916 #define stm_syscfg (*((struct stm_syscfg *) 0x40013800))
917
918 #define STM_SYSCFG_MEMRMP_MEM_MODE      0
919 #define  STM_SYSCFG_MEMRMP_MEM_MODE_MAIN_FLASH          0
920 #define  STM_SYSCFG_MEMRMP_MEM_MODE_SYSTEM_FLASH        1
921 #define  STM_SYSCFG_MEMRMP_MEM_MODE_SRAM                3
922 #define  STM_SYSCFG_MEMRMP_MEM_MODE_MASK                3
923
924 #define STM_SYSCFG_PMC_ADC1DC2          0
925
926 static inline void
927 stm_exticr_set(struct stm_gpio *gpio, int pin) {
928         uint8_t reg = pin >> 2;
929         uint8_t shift = (pin & 3) << 2;
930         uint8_t val = 0;
931
932         /* Enable SYSCFG */
933         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
934
935         if (gpio == &stm_gpioa)
936                 val = STM_SYSCFG_EXTICR_PA;
937         else if (gpio == &stm_gpiob)
938                 val = STM_SYSCFG_EXTICR_PB;
939         else if (gpio == &stm_gpioc)
940                 val = STM_SYSCFG_EXTICR_PC;
941         else if (gpio == &stm_gpiod)
942                 val = STM_SYSCFG_EXTICR_PD;
943         else if (gpio == &stm_gpioe)
944                 val = STM_SYSCFG_EXTICR_PE;
945         else if (gpio == &stm_gpiof)
946                 val = STM_SYSCFG_EXTICR_PF;
947         else if (gpio == &stm_gpiog)
948                 val = STM_SYSCFG_EXTICR_PG;
949         else if (gpio == &stm_gpioh)
950                 val = STM_SYSCFG_EXTICR_PH;
951
952         stm_syscfg.exticr[reg] = (stm_syscfg.exticr[reg] & ~(0xf << shift)) | val << shift;
953 }
954
955 struct stm_exti {
956         vuint32_t       imr;
957         vuint32_t       emr;
958         vuint32_t       rtsr;
959         vuint32_t       ftsr;
960
961         vuint32_t       swier;
962         vuint32_t       pr;
963 };
964
965 extern struct stm_exti stm_exti;
966
967 #define stm_exti        (*((struct stm_exti *) 0x40013c00))
968
969 struct stm_usart {
970         vuint32_t       sr;     /* status register */
971         vuint32_t       dr;     /* data register */
972         vuint32_t       brr;    /* baud rate register */
973         vuint32_t       cr1;    /* control register 1 */
974
975         vuint32_t       cr2;    /* control register 2 */
976         vuint32_t       cr3;    /* control register 3 */
977         vuint32_t       gtpr;   /* guard time and prescaler */
978 };
979
980 extern struct stm_usart stm_usart6;
981
982 #define stm_usart6      (*((struct stm_usart *) 0x40011400))
983
984 #define STM_USART_SR_CTS        (9)     /* CTS flag */
985 #define STM_USART_SR_LBD        (8)     /* LIN break detection flag */
986 #define STM_USART_SR_TXE        (7)     /* Transmit data register empty */
987 #define STM_USART_SR_TC         (6)     /* Transmission complete */
988 #define STM_USART_SR_RXNE       (5)     /* Read data register not empty */
989 #define STM_USART_SR_IDLE       (4)     /* IDLE line detected */
990 #define STM_USART_SR_ORE        (3)     /* Overrun error */
991 #define STM_USART_SR_NF         (2)     /* Noise detected flag */
992 #define STM_USART_SR_FE         (1)     /* Framing error */
993 #define STM_USART_SR_PE         (0)     /* Parity error */
994
995 #define STM_USART_CR1_OVER8     (15)    /* Oversampling mode */
996 #define STM_USART_CR1_UE        (13)    /* USART enable */
997 #define STM_USART_CR1_M         (12)    /* Word length */
998 #define STM_USART_CR1_WAKE      (11)    /* Wakeup method */
999 #define STM_USART_CR1_PCE       (10)    /* Parity control enable */
1000 #define STM_USART_CR1_PS        (9)     /* Parity selection */
1001 #define STM_USART_CR1_PEIE      (8)     /* PE interrupt enable */
1002 #define STM_USART_CR1_TXEIE     (7)     /* TXE interrupt enable */
1003 #define STM_USART_CR1_TCIE      (6)     /* Transmission complete interrupt enable */
1004 #define STM_USART_CR1_RXNEIE    (5)     /* RXNE interrupt enable */
1005 #define STM_USART_CR1_IDLEIE    (4)     /* IDLE interrupt enable */
1006 #define STM_USART_CR1_TE        (3)     /* Transmitter enable */
1007 #define STM_USART_CR1_RE        (2)     /* Receiver enable */
1008 #define STM_USART_CR1_RWU       (1)     /* Receiver wakeup */
1009 #define STM_USART_CR1_SBK       (0)     /* Send break */
1010
1011 #define STM_USART_CR2_LINEN     (14)    /* LIN mode enable */
1012 #define STM_USART_CR2_STOP      (12)    /* STOP bits */
1013 #define STM_USART_CR2_STOP_MASK 3
1014 #define STM_USART_CR2_STOP_1    0
1015 #define STM_USART_CR2_STOP_0_5  1
1016 #define STM_USART_CR2_STOP_2    2
1017 #define STM_USART_CR2_STOP_1_5  3
1018
1019 #define STM_USART_CR2_CLKEN     (11)    /* Clock enable */
1020 #define STM_USART_CR2_CPOL      (10)    /* Clock polarity */
1021 #define STM_USART_CR2_CPHA      (9)     /* Clock phase */
1022 #define STM_USART_CR2_LBCL      (8)     /* Last bit clock pulse */
1023 #define STM_USART_CR2_LBDIE     (6)     /* LIN break detection interrupt enable */
1024 #define STM_USART_CR2_LBDL      (5)     /* lin break detection length */
1025 #define STM_USART_CR2_ADD       (0)
1026 #define STM_USART_CR2_ADD_MASK  0xf
1027
1028 #define STM_USART_CR3_ONEBIT    (11)    /* One sample bit method enable */
1029 #define STM_USART_CR3_CTSIE     (10)    /* CTS interrupt enable */
1030 #define STM_USART_CR3_CTSE      (9)     /* CTS enable */
1031 #define STM_USART_CR3_RTSE      (8)     /* RTS enable */
1032 #define STM_USART_CR3_DMAT      (7)     /* DMA enable transmitter */
1033 #define STM_USART_CR3_DMAR      (6)     /* DMA enable receiver */
1034 #define STM_USART_CR3_SCEN      (5)     /* Smartcard mode enable */
1035 #define STM_USART_CR3_NACK      (4)     /* Smartcard NACK enable */
1036 #define STM_USART_CR3_HDSEL     (3)     /* Half-duplex selection */
1037 #define STM_USART_CR3_IRLP      (2)     /* IrDA low-power */
1038 #define STM_USART_CR3_IREN      (1)     /* IrDA mode enable */
1039 #define STM_USART_CR3_EIE       (0)     /* Error interrupt enable */
1040
1041 /* USB */
1042 struct stm_usb {
1043         vuint32_t       gotgctl;
1044         vuint32_t       gotgint;
1045         vuint32_t       gahbcfg;
1046         vuint32_t       gusbcfg;
1047
1048         vuint32_t       grstctl;
1049         vuint32_t       gintsts;
1050         vuint32_t       gintmsk;
1051         vuint32_t       grxstsr;
1052
1053         vuint32_t       grxstsp;
1054         vuint32_t       grxfsiz;
1055         vuint32_t       dieptxf0;
1056         vuint32_t       hnptxsts;
1057
1058         vuint32_t       pad_30;
1059         vuint32_t       pad_34;
1060         vuint32_t       gccfg;
1061         vuint32_t       cid;
1062
1063         vuint32_t       pad_40;
1064         vuint32_t       pad_44;
1065         vuint32_t       pad_48;
1066         vuint32_t       ghwcfg3;        /* not in docs? */
1067
1068         vuint32_t       pad_50;
1069         vuint32_t       glpmcfg;
1070         vuint32_t       pad_58;
1071         vuint32_t       gdfifocfg;      /* not in docs? */
1072
1073         uint8_t         pad_60[0x100 - 0x60];
1074
1075         vuint32_t       hptxfsiz;       /* 0x100 */
1076         vuint32_t       dieptxf[0xf];   /* 0x104 5 in docs? */
1077
1078         uint8_t         pad_140[0x400 - 0x140];
1079
1080         vuint32_t       hcfg;
1081         vuint32_t       hfir;
1082         vuint32_t       hfnum;
1083         vuint32_t       pad_40c;
1084
1085         vuint32_t       hptxsts;
1086         vuint32_t       haint;
1087         vuint32_t       haintmsk;
1088         vuint32_t       pad_41c;
1089
1090         uint8_t         pad_420[0x440-0x420];
1091
1092         vuint32_t       hprt;
1093
1094         uint8_t         pad_444[0x500 - 0x444];
1095
1096         vuint32_t       hcchar0;
1097         vuint32_t       pad_504;
1098         vuint32_t       hcint0;
1099         vuint32_t       hcintmsk0;
1100
1101         vuint32_t       hctsiz0;
1102         vuint32_t       pad_514;
1103         vuint32_t       pad_518;
1104         vuint32_t       pad_51c;
1105
1106         struct {
1107                 vuint32_t       hcchar;
1108                 vuint32_t       pad_4;
1109                 vuint32_t       hcint;
1110                 vuint32_t       hcintmsk;
1111
1112                 vuint32_t       hctsiz;
1113                 vuint32_t       pad_14;
1114                 vuint32_t       pad_18;
1115                 vuint32_t       pad_1c;
1116         } h[11];
1117
1118         uint8_t         pad_680[0x800 - 0x680];
1119
1120         vuint32_t       dcfg;
1121         vuint32_t       dctl;
1122         vuint32_t       dsts;
1123         vuint32_t       pad_80c;
1124
1125         vuint32_t       diepmsk;
1126         vuint32_t       doepmsk;
1127         vuint32_t       daint;
1128         vuint32_t       daintmsk;
1129
1130         vuint32_t       pad_820;
1131         vuint32_t       pad_824;
1132         vuint32_t       dvbusdis;
1133         vuint32_t       dvbuspulse;
1134
1135         vuint32_t       pad_830;
1136         vuint32_t       diepempmsk;
1137
1138         uint8_t         pad_838[0x900 - 0x838];
1139
1140         struct {
1141                 vuint32_t       diepctl;
1142                 vuint32_t       pad_04;
1143                 vuint32_t       diepint;
1144                 vuint32_t       pad_0c;
1145
1146                 vuint32_t       dieptsiz;
1147                 vuint32_t       pad_14;
1148                 vuint32_t       dtxfsts;
1149                 vuint32_t       pad_1c;
1150         } diep[6];
1151
1152         uint8_t         pad_9c0[0xb00 - 0x9c0];
1153
1154         struct {
1155                 vuint32_t       doepctl;
1156                 vuint32_t       pad_04;
1157                 vuint32_t       doepint;
1158                 vuint32_t       pad_0c;
1159
1160                 vuint32_t       doeptsiz;
1161                 vuint32_t       pad_14;
1162                 vuint32_t       pad_18;
1163                 vuint32_t       pad_1c;
1164         } doep[6];
1165
1166         uint8_t         pad_bc0[0xe00 - 0xbc0];
1167
1168         vuint32_t       pcgcctl;
1169
1170         uint8_t         pad_e04[0x1000 - 0xe04];
1171
1172         struct {
1173                 vuint32_t       fifo;
1174                 uint8_t         pad_004[0x1000 - 0x004];
1175         } dfifo[6];
1176 };
1177
1178 extern struct stm_usb stm_usb;
1179
1180 #define stm_usb (*((struct stm_usb *) 0x50000000))
1181
1182 #define STM_USB_GOTGCTL_CURMOD          21
1183 #define STM_USB_GOTGCTL_OTGVER          20
1184 #define STM_USB_GOTGCTL_BSVLD           19
1185 #define STM_USB_GOTGCTL_ASVLD           18
1186 #define STM_USB_GOTGCTL_DBCT            17
1187 #define STM_USB_GOTGCTL_CIDSTS          16
1188 #define STM_USB_GOTGCTL_EHEN            12
1189 #define STM_USB_GOTGCTL_DHNPEN          11
1190 #define STM_USB_GOTGCTL_HSHNPEN         10
1191 #define STM_USB_GOTGCTL_HNPRQ           9
1192 #define STM_USB_GOTGCTL_HNGSCS          8
1193 #define STM_USB_GOTGCTL_BVALOVAL        7
1194 #define STM_USB_GOTGCTL_BVALOEN         6
1195 #define STM_USB_GOTGCTL_AVALOVAL        5
1196 #define STM_USB_GOTGCTL_AVALOEN         4
1197 #define STM_USB_GOTGCTL_VBVALOVAL       3
1198 #define STM_USB_GOTGCTL_VBVALOEN        2
1199 #define STM_USB_GOTGCTL_SRQ             1
1200 #define STM_USB_GOTGCTL_SRQSCS          0
1201
1202 #define STM_USB_GOTGINT_IDCHNG          20
1203 #define STM_USB_GOTGINT_DBCDNE          19
1204 #define STM_USB_GOTGINT_ADTOCHG         18
1205 #define STM_USB_GOTGINT_HNGDET          17
1206 #define STM_USB_GOTGINT_HNSSCHG         9
1207 #define STM_USB_GOTGINT_SRSSCHG         8
1208 #define STM_USB_GOTGINT_SEDET           2
1209
1210 #define STM_USB_GAHBCFG_PTXFELVL        8
1211 #define STM_USB_GAHBCFG_TXFELVL         7
1212 #define STM_USB_GAHBCFG_GINTMSK         0
1213
1214 #define STM_USB_GUSBCFG_FDMOD           30
1215 #define STM_USB_GUSBCFG_FHMOD           29
1216 #define STM_USB_GUSBCFG_TRDT            10
1217 #define  STM_USB_GUSBCFG_TRDT_MASK      0xf
1218 #define STM_USB_GUSBCFG_HNPCAP          9
1219 #define STM_USB_GUSBCFG_SRPCAP          8
1220 #define STM_USB_GUSBCFG_PHYSEL          6
1221 #define STM_USB_GUSBCFG_TOCAL           0
1222 #define  STM_USB_GUSBCFG_TOCAL_MASK             0x7
1223
1224 #define STM_USB_GRSTCTL_AHBIDL          31
1225 #define STM_USB_GRSTCTL_TXFNUM          6
1226 #define  STM_USB_GRSTCTL_TXFNUM_ALL             0x10
1227 #define  STM_USB_GRSTCTL_TXFNUM_MASK            0x1f
1228 #define STM_USB_GRSTCTL_TXFFLSH         5
1229 #define STM_USB_GRSTCTL_RXFFLSH         4
1230 #define STM_USB_GRSTCTL_FCRST           2
1231 #define STM_USB_GRSTCTL_PSRST           1
1232 #define STM_USB_GRSTCTL_CSRST           0
1233
1234 #define STM_USB_GINTSTS_WKUPINT         31
1235 #define STM_USB_GINTSTS_SRQINT          30
1236 #define STM_USB_GINTSTS_DISCINT         29
1237 #define STM_USB_GINTSTS_CIDSCHG         28
1238 #define STM_USB_GINTSTS_LPMINT          27
1239 #define STM_USB_GINTSTS_PTXFE           26
1240 #define STM_USB_GINTSTS_HCINT           25
1241 #define STM_USB_GINTSTS_HPRTINT         24
1242 #define STM_USB_GINTSTS_RSTDET          23
1243 #define STM_USB_GINTSTS_IPXFER          21
1244 #define STM_USB_GINTSTS_IISOIXFR        20
1245 #define STM_USB_GINTSTS_OEPINT          19
1246 #define STM_USB_GINTSTS_IEPINT          18
1247 #define STM_USB_GINTSTS_EOPF            15
1248 #define STM_USB_GINTSTS_ISOODRP         14
1249 #define STM_USB_GINTSTS_ENUMDNE         13
1250 #define STM_USB_GINTSTS_USBRST          12
1251 #define STM_USB_GINTSTS_USBSUSP         11
1252 #define STM_USB_GINTSTS_ESUSP           10
1253 #define STM_USB_GINTSTS_GONAKEFF        7
1254 #define STM_USB_GINTSTS_GINAKEFF        6
1255 #define STM_USB_GINTSTS_NPTXFE          5
1256 #define STM_USB_GINTSTS_RXFLVL          4
1257 #define STM_USB_GINTSTS_SOF             3
1258 #define STM_USB_GINTSTS_OTGINT          2
1259 #define STM_USB_GINTSTS_MMIS            1
1260 #define STM_USB_GINTSTS_CMOD            0
1261
1262 #define STM_USB_GINTMSK_WUIM            31
1263 #define STM_USB_GINTMSK_SRQIM           30
1264 #define STM_USB_GINTMSK_DISCINT         29
1265 #define STM_USB_GINTMSK_CIDSCHGM        28
1266 #define STM_USB_GINTMSK_LPMINTM         27
1267 #define STM_USB_GINTMSK_PTXFEM          26
1268 #define STM_USB_GINTMSK_HCIM            25
1269 #define STM_USB_GINTMSK_PRTIM           24
1270 #define STM_USB_GINTMSK_RSTDETM         23
1271 #define STM_USB_GINTMSK_IPXFERM         21      /* host mode */
1272 #define STM_USB_GINTMSK_IISOOXFRM       21      /* device mode */
1273 #define STM_USB_GINTMSK_IISOIXFRM       20
1274 #define STM_USB_GINTMSK_OEPINT          19
1275 #define STM_USB_GINTMSK_IEPINT          18
1276 #define STM_USB_GINTMSK_EOPFM           15
1277 #define STM_USB_GINTMSK_ISOODRPM        14
1278 #define STM_USB_GINTMSK_ENUMDNEM        13
1279 #define STM_USB_GINTMSK_USBRST          12
1280 #define STM_USB_GINTMSK_USBSUSPM        11
1281 #define STM_USB_GINTMSK_ESUSPM          10
1282 #define STM_USB_GINTMSK_GONAKEFFM       7
1283 #define STM_USB_GINTMSK_GINAKEFFM       6
1284 #define STM_USB_GINTMSK_NPTXFEM         5
1285 #define STM_USB_GINTMSK_RXFLVLM         4
1286 #define STM_USB_GINTMSK_SOFM            3
1287 #define STM_USB_GINTMSK_OTGINT          2
1288 #define STM_USB_GINTMSK_MMISM           1
1289
1290 #define STM_USB_GRXSTSP_STSPHST         27
1291 #define STM_USB_GRXSTSP_FRMNUM          21
1292 #define  STM_USB_GRXSTSP_FRMNUM_MASK            0xf
1293 #define STM_USB_GRXSTSP_PKTSTS          17
1294 #define  STM_USB_GRXSTSP_PKTSTS_NAK             1
1295 #define  STM_USB_GRXSTSP_PKTSTS_OUT_DATA        2
1296 #define  STM_USB_GRXSTSP_PKTSTS_OUT_COMPLETE    3
1297 #define  STM_USB_GRXSTSP_PKTSTS_SETUP_COMPLETE  4
1298 #define  STM_USB_GRXSTSP_PKTSTS_SETUP_DATA      5
1299 #define  STM_USB_GRXSTSP_PKTSTS_MASK            0xf
1300 #define STM_USB_GRXSTSP_DPID            15
1301 #define  STM_USB_GRXSTSP_DPID_MASK              3
1302 #define STM_USB_GRXSTSP_BCNT            4
1303 #define STM_USB_GRXSTSP_BCNT            4
1304 #define  STM_USB_GRXSTSP_BCNT_MASK              0x3ff
1305 #define STM_USB_GRXSTSP_EPNUM           0
1306 #define  STM_USB_GRXSTSP_EPNUM_MASK             0xf
1307
1308 #define STM_USB_GRXFSIZ_RXFD            0
1309 #define  STM_USB_GRXFSIZ_RXFD_MASK              0xffff
1310
1311 #define STM_USB_GCCFG_VBDEN             21
1312 #define STM_USB_GCCFG_SDEN              20
1313 #define STM_USB_GCCFG_PDEN              19
1314 #define STM_USB_GCCFG_DCDEN             18
1315 #define STM_USB_GCCFG_BCDEN             17
1316 #define STM_USB_GCCFG_PWRDWN            16
1317 #define STM_USB_GCCFG_PS2DET            3
1318 #define STM_USB_GCCFG_SDET              2
1319 #define STM_USB_GCCFG_PDET              1
1320 #define STM_USB_GCCFG_DCDET             0
1321
1322 #define STM_USB_DIEPTXF0_TX0FD          16
1323 #define STM_USB_DIEPTXF0_TX0FSA          0
1324
1325 #define STM_USB_DCFG_ERRATIM            15
1326 #define STM_USB_DCFG_PFIVL              11
1327 #define  STM_USB_DCFG_PFIVL_80                  0
1328 #define  STM_USB_DCFG_PFIVL_85                  1
1329 #define  STM_USB_DCFG_PFIVL_90                  2
1330 #define  STM_USB_DCFG_PFIVL_95                  3
1331 #define  STM_USB_DCFG_PFIVL_MASK                3
1332 #define STM_USB_DCFG_DAD                4
1333 #define  STM_USB_DCFG_DAD_MASK                  0x7f
1334 #define STM_USB_DCFG_NZLSOHSK           2
1335 #define STM_USB_DCFG_DSPD               0
1336 #define  STM_USB_DCFG_DSPD_FULL_SPEED           3
1337 #define  STM_USB_DCFG_DSPD_MASK                 3
1338 #define STM_USB_DCFG_
1339 #define STM_USB_DCFG_
1340 #define STM_USB_DCFG_
1341 #define STM_USB_DCFG_
1342 #define STM_USB_DCFG_
1343 #define STM_USB_DCFG_
1344 #define STM_USB_DCFG_
1345
1346 #define STM_USB_DCTL_DSBESLRJCT         18
1347 #define STM_USB_DCTL_POPRGDNE           11
1348 #define STM_USB_DCTL_CGONAK             10
1349 #define STM_USB_DCTL_SGONAK             9
1350 #define STM_USB_DCTL_CGINAK             8
1351 #define STM_USB_DCTL_SGINAK             7
1352 #define STM_USB_DCTL_TCTL               4
1353 #define STM_USB_DCTL_GONSTS             3
1354 #define STM_USB_DCTL_GINSTS             2
1355 #define STM_USB_DCTL_SDIS               1
1356 #define STM_USB_DCTL_RWUSIG             0
1357
1358 #define STM_USB_DSTS_DEVLNSTS           22
1359 #define  STM_USB_DSTS_DEVLNSTS_MASK             0x3
1360 #define STM_USB_DSTS_FNSOF              8
1361 #define  STM_USB_DSTS_FNSOF_MASK                0x3fff
1362 #define STM_USB_DSTS_EERR               3
1363 #define STM_USB_DSTS_ENUMSPD            1
1364 #define  STM_USB_DSTS_ENUMSPD_MASK              3
1365 #define STM_USB_DSTS_SUSPSTS            0
1366
1367 #define STM_USB_DIEPMSK_NAKM            13
1368 #define STM_USB_DIEPMSK_TXFURM          8
1369 #define STM_USB_DIEPMSK_INEPNEM         6
1370 #define STM_USB_DIEPMSK_INEPNMM         5
1371 #define STM_USB_DIEPMSK_ITTXFEMSK       4
1372 #define STM_USB_DIEPMSK_TOM             3
1373 #define STM_USB_DIEPMSK_EPDM            1
1374 #define STM_USB_DIEPMSK_XFRCM           0
1375
1376 #define STM_USB_DOEPMSK_NYETMSK         14
1377 #define STM_USB_DOEPMSK_NAKMSK          13
1378 #define STM_USB_DOEPMSK_BERRM           12
1379 #define STM_USB_DOEPMSK_OUTPKTERRM      8
1380 #define STM_USB_DOEPMSK_STSPHSRXM       5
1381 #define STM_USB_DOEPMSK_OTEPDM          4
1382 #define STM_USB_DOEPMSK_STUPM           3
1383 #define STM_USB_DOEPMSK_EPDM            1
1384 #define STM_USB_DOEPMSK_XFRCM           0
1385
1386 #define STM_USB_DAINT_OEPINT            16
1387 #define  STM_USB_DAINT_OEPINT_MASK              0xffff
1388 #define STM_USB_DAINT_IEPINT            16
1389 #define  STM_USB_DAINT_IEPINT_MASK              0xffff
1390
1391 #define STM_USB_DAINTMSK_OEPM           16
1392 #define  STM_USB_DAINTMSK_OEPM_MASK             0xffff
1393 #define STM_USB_DAINTMSK_IEPM           0
1394 #define  STM_USB_DAINTMSK_IEPM_MASK             0xffff
1395
1396 #define STM_USB_DIEPCTL_EPENA           31
1397 #define STM_USB_DIEPCTL_EPDIS           30
1398 #define STM_USB_DIEPCTL_SNAK            27
1399 #define STM_USB_DIEPCTL_CNAK            26
1400 #define STM_USB_DIEPCTL_TXFNUM          22
1401 #define  STM_USB_DIEPCTL_TXFNUM_MASK            0xf
1402 #define STM_USB_DIEPCTL_STALL           21
1403 #define STM_USB_DIEPCTL_EPTYP           18
1404 #define  STM_USB_DIEPCTL_EPTYP_CONTROL          0
1405 #define  STM_USB_DIEPCTL_EPTYP_ISOCHRONOUS      1
1406 #define  STM_USB_DIEPCTL_EPTYP_BULK             2
1407 #define  STM_USB_DIEPCTL_EPTYP_INTERRUPT        3
1408 #define  STM_USB_DIEPCTL_EPTYP_MASK             3
1409 #define STM_USB_DIEPCTL_NAKSTS          17
1410 #define STM_USB_DIEPCTL_EONUM           16
1411 #define STM_USB_DIEPCTL_USBAEP          15
1412 #define STM_USB_DIEPCTL_MPSIZ           0
1413 #define  STM_USB_DIEPCTL_MPSIZ0_64              0
1414 #define  STM_USB_DIEPCTL_MPSIZ0_32              1
1415 #define  STM_USB_DIEPCTL_MPSIZ0_16              2
1416 #define  STM_USB_DIEPCTL_MPSIZ0_8               3
1417 #define  STM_USB_DIEPCTL_MPSIZ0_MASK            3
1418 #define  STM_USB_DIEPCTL_MPSIZ_MASK             0x7f
1419
1420 #define STM_USB_DIEPINT_NAK             13
1421 #define STM_USB_DIEPINT_PKTDRPSTS       11
1422 #define STM_USB_DIEPINT_TXFIFOUDRN      8
1423 #define STM_USB_DIEPINT_TXFE            7
1424 #define STM_USB_DIEPINT_INEPNE          6
1425 #define STM_USB_DIEPINT_INEPNM          5
1426 #define STM_USB_DIEPINT_ITTXFE          4
1427 #define STM_USB_DIEPINT_TOC             3
1428 #define STM_USB_DIEPINT_EPDISD          1
1429 #define STM_USB_DIEPINT_XFRC            0
1430
1431 #define STM_USB_DIEPTSIZ_MCNT           29
1432 #define  STM_USB_DIEPTSIZ_MCNT_MASK             3
1433 #define STM_USB_DIEPTSIZ_PKTCNT         19
1434 #define  STM_USB_DIEPTSIZ_PKTCNT0_MASK          3
1435 #define  STM_USB_DIEPTSIZ_PKTCNT_MASK           0x3ff
1436 #define STM_USB_DIEPTSIZ_XFRSIZ         0
1437 #define  STM_USB_DIEPTSIZ_XFRSIZ0_MASK          0x7f
1438 #define  STM_USB_DIEPTSIZ_XFRSIZ_MASK           0x7ffff
1439
1440 #define STM_USB_DOEPCTL_EPENA           31
1441 #define STM_USB_DOEPCTL_EPDIS           30
1442 #define STM_USB_DOEPCTL_SNAK            27
1443 #define STM_USB_DOEPCTL_CNAK            26
1444 #define STM_USB_DOEPCTL_STALL           21
1445 #define STM_USB_DOEPCTL_SNPM            20
1446 #define STM_USB_DOEPCTL_EPTYP           18
1447 #define  STM_USB_DOEPCTL_EPTYP_CONTROL          0
1448 #define  STM_USB_DOEPCTL_EPTYP_ISOCHRONOUS      1
1449 #define  STM_USB_DOEPCTL_EPTYP_BULK             2
1450 #define  STM_USB_DOEPCTL_EPTYP_INTERRUPT        3
1451 #define  STM_USB_DOEPCTL_EPTYP_MASK             3
1452 #define STM_USB_DOEPCTL_NAKSTS          17
1453 #define STM_USB_DOEPCTL_USBAEP          15
1454 #define STM_USB_DOEPCTL_MPSIZ           0
1455 #define  STM_USB_DOEPCTL_MPSIZ0_64              0
1456 #define  STM_USB_DOEPCTL_MPSIZ0_32              1
1457 #define  STM_USB_DOEPCTL_MPSIZ0_16              2
1458 #define  STM_USB_DOEPCTL_MPSIZ0_8               3
1459 #define  STM_USB_DOEPCTL_MPSIZ0_MASK            3
1460
1461 #define STM_USB_DOEPINT_NAK             13
1462 #define STM_USB_DOEPINT_BERR            12
1463 #define STM_USB_DOEPINT_OUTPKTERR       8
1464 #define STM_USB_DOEPINT_STSPHSRX        5
1465 #define STM_USB_DOEPINT_OTEPDIS         4
1466 #define STM_USB_DOEPINT_STUP            3
1467 #define STM_USB_DOEPINT_EPDISD          1
1468 #define STM_USB_DOEPINT_XFRC            0
1469
1470 #define STM_USB_DOEPTSIZ_STUPCNT        29
1471 #define  STM_USB_DOEPTSIZ_STUPCNT_MASK          3
1472 #define STM_USB_DOEPTSIZ_PKTCNT         19
1473 #define STM_USB_DOEPTSIZ_XFRSIZ         0
1474 #define  STM_USB_DOEPTSIZ_XFRSIZ_MASK           0x7f
1475
1476 /* Errata 2.1.5
1477
1478    Delay after an RCC peripheral clock enabling
1479
1480    Description
1481
1482    A delay between an RCC peripheral clock enable and the effective
1483    peripheral enabling should be taken into account in order to manage
1484    the peripheral read/write to registers.
1485
1486    This delay depends on the peripheral’s mapping:
1487
1488    • If the peripheral is mapped on AHB: the delay should be equal to
1489      2 AHB cycles.
1490
1491    • If the peripheral is mapped on APB: the delay should be equal to
1492      1 + (AHB/APB prescaler) cycles.
1493
1494    Workarounds
1495
1496    1. Use the DSB instruction to stall the Cortex-M4 CPU pipeline
1497       until the instruction is completed.
1498
1499    2. Insert “n” NOPs between the RCC enable bit write and the
1500       peripheral register writes
1501 */
1502
1503 static inline void
1504 stm32f4_set_rcc(uint32_t *rcc, uint32_t value)
1505 {
1506         *rcc = value;
1507         asm("dsb");
1508 }
1509
1510 /* Errata 2.1.8
1511
1512    In some specific cases, DMA2 data corruption occurs when managing
1513    AHB and APB2 peripherals in a concurrent way
1514
1515    Description
1516
1517    When the DMA2 is managing concurrent requests of AHB and APB2
1518    peripherals, the transfer on the AHB could be performed several
1519    times.
1520
1521    Impacted peripheral are:
1522
1523    • Quad-SPI: indirect mode read and write transfers
1524
1525    • FSMC: read and write operation with external device having FIFO
1526
1527    • GPIO: DMA2 transfers to GPIO registers (in memory-to-peripheral
1528      transfer mode).The transfers from GPIOs register are not
1529      impacted.
1530
1531
1532    The data corruption is due to multiple DMA2 accesses over AHB
1533    peripheral port impacting peripherals embedding a FIFO.
1534
1535    For transfer to the internal SRAM through the DMA2 AHB peripheral
1536    port the accesses could be performed several times but without data
1537    corruptions in cases of concurrent requests.
1538
1539    Workaround
1540
1541    • The DMA2 AHB memory port must be used when reading/writing
1542      from/to Quad-SPI and FSMC instead of DMA2 AHB default peripheral
1543      port.
1544
1545    • The DMA2 AHB memory port must be used when writing to GPIOs
1546      instead of DMA2 AHB default peripheral port.
1547
1548    Refer to application note AN4031 section “Take benefits of DMA2
1549    controller and system architecture flexibility” for more details
1550    about DMA controller feature.
1551
1552 */
1553
1554
1555
1556 #endif /* _STM32F4_H_ */