altos: Add stack-guard code. Uses STM MPU to trap stack overflow.
[fw/altos] / src / stm / stm32l.h
1 /*
2  * Copyright © 2012 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #ifndef _STM32L_H_
19 #define _STM32L_H_
20
21 #include <stdint.h>
22
23 typedef volatile uint32_t       vuint32_t;
24 typedef volatile void *         vvoid_t;
25
26 struct stm_gpio {
27         vuint32_t       moder;
28         vuint32_t       otyper;
29         vuint32_t       ospeedr;
30         vuint32_t       pupdr;
31
32         vuint32_t       idr;
33         vuint32_t       odr;
34         vuint32_t       bsrr;
35         vuint32_t       lckr;
36
37         vuint32_t       afrl;
38         vuint32_t       afrh;
39 };
40
41 #define STM_MODER_SHIFT(pin)            ((pin) << 1)
42 #define STM_MODER_MASK                  3
43 #define STM_MODER_INPUT                 0
44 #define STM_MODER_OUTPUT                1
45 #define STM_MODER_ALTERNATE             2
46 #define STM_MODER_ANALOG                3
47
48 static inline void
49 stm_moder_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
50         gpio->moder = ((gpio->moder &
51                         ~(STM_MODER_MASK << STM_MODER_SHIFT(pin))) |
52                        value << STM_MODER_SHIFT(pin));
53 }
54         
55 static inline vuint32_t
56 stm_moder_get(struct stm_gpio *gpio, int pin) {
57         return (gpio->moder >> STM_MODER_SHIFT(pin)) & STM_MODER_MASK;
58 }
59
60 #define STM_OTYPER_SHIFT(pin)           (pin)
61 #define STM_OTYPER_MASK                 1
62 #define STM_OTYPER_PUSH_PULL            0
63 #define STM_OTYPER_OPEN_DRAIN           1
64
65 static inline void
66 stm_otyper_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
67         gpio->otyper = ((gpio->otyper &
68                          ~(STM_OTYPER_MASK << STM_OTYPER_SHIFT(pin))) |
69                         value << STM_OTYPER_SHIFT(pin));
70 }
71         
72 static inline vuint32_t
73 stm_otyper_get(struct stm_gpio *gpio, int pin) {
74         return (gpio->otyper >> STM_OTYPER_SHIFT(pin)) & STM_OTYPER_MASK;
75 }
76
77 #define STM_OSPEEDR_SHIFT(pin)          ((pin) << 1)
78 #define STM_OSPEEDR_MASK                3
79 #define STM_OSPEEDR_400kHz              0
80 #define STM_OSPEEDR_2MHz                1
81 #define STM_OSPEEDR_10MHz               2
82 #define STM_OSPEEDR_40MHz               3
83
84 static inline void
85 stm_ospeedr_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
86         gpio->ospeedr = ((gpio->ospeedr &
87                         ~(STM_OSPEEDR_MASK << STM_OSPEEDR_SHIFT(pin))) |
88                        value << STM_OSPEEDR_SHIFT(pin));
89 }
90         
91 static inline vuint32_t
92 stm_ospeedr_get(struct stm_gpio *gpio, int pin) {
93         return (gpio->ospeedr >> STM_OSPEEDR_SHIFT(pin)) & STM_OSPEEDR_MASK;
94 }
95
96 #define STM_PUPDR_SHIFT(pin)            ((pin) << 1)
97 #define STM_PUPDR_MASK                  3
98 #define STM_PUPDR_NONE                  0
99 #define STM_PUPDR_PULL_UP               1
100 #define STM_PUPDR_PULL_DOWN             2
101 #define STM_PUPDR_RESERVED              3
102
103 static inline void
104 stm_pupdr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
105         gpio->pupdr = ((gpio->pupdr &
106                         ~(STM_PUPDR_MASK << STM_PUPDR_SHIFT(pin))) |
107                        value << STM_PUPDR_SHIFT(pin));
108 }
109         
110 static inline uint32_t
111 stm_pupdr_get(struct stm_gpio *gpio, int pin) {
112         return (gpio->pupdr >> STM_PUPDR_SHIFT(pin)) & STM_PUPDR_MASK;
113 }
114
115 #define STM_AFR_SHIFT(pin)              ((pin) << 2)
116 #define STM_AFR_MASK                    0xf
117 #define STM_AFR_NONE                    0
118 #define STM_AFR_AF0                     0x0
119 #define STM_AFR_AF1                     0x1
120 #define STM_AFR_AF2                     0x2
121 #define STM_AFR_AF3                     0x3
122 #define STM_AFR_AF4                     0x4
123 #define STM_AFR_AF5                     0x5
124 #define STM_AFR_AF6                     0x6
125 #define STM_AFR_AF7                     0x7
126 #define STM_AFR_AF8                     0x8
127 #define STM_AFR_AF9                     0x9
128 #define STM_AFR_AF10                    0xa
129 #define STM_AFR_AF11                    0xb
130 #define STM_AFR_AF12                    0xc
131 #define STM_AFR_AF13                    0xd
132 #define STM_AFR_AF14                    0xe
133 #define STM_AFR_AF15                    0xf
134
135 static inline void
136 stm_afr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
137         /*
138          * Set alternate pin mode too
139          */
140         stm_moder_set(gpio, pin, STM_MODER_ALTERNATE);
141         if (pin < 8)
142                 gpio->afrl = ((gpio->afrl &
143                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
144                               value << STM_AFR_SHIFT(pin));
145         else {
146                 pin -= 8;
147                 gpio->afrh = ((gpio->afrh &
148                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
149                               value << STM_AFR_SHIFT(pin));
150         }
151 }
152         
153 static inline uint32_t
154 stm_afr_get(struct stm_gpio *gpio, int pin) {
155         if (pin < 8)
156                 return (gpio->afrl >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
157         else {
158                 pin -= 8;
159                 return (gpio->afrh >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
160         }
161 }
162
163 static inline void
164 stm_gpio_set(struct stm_gpio *gpio, int pin, uint8_t value) {
165         /* Use the bit set/reset register to do this atomically */
166         gpio->bsrr = ((uint32_t) (value ^ 1) << (pin + 16)) | ((uint32_t) value << pin);
167 }
168
169 static inline uint8_t
170 stm_gpio_get(struct stm_gpio *gpio, int pin) {
171         return (gpio->idr >> pin) & 1;
172 }
173
174 extern struct stm_gpio stm_gpioa;
175 extern struct stm_gpio stm_gpiob;
176 extern struct stm_gpio stm_gpioc;
177 extern struct stm_gpio stm_gpiod;
178 extern struct stm_gpio stm_gpioe;
179 extern struct stm_gpio stm_gpioh;
180
181 struct stm_usart {
182         vuint32_t       sr;     /* status register */
183         vuint32_t       dr;     /* data register */
184         vuint32_t       brr;    /* baud rate register */
185         vuint32_t       cr1;    /* control register 1 */
186
187         vuint32_t       cr2;    /* control register 2 */
188         vuint32_t       cr3;    /* control register 3 */
189         vuint32_t       gtpr;   /* guard time and prescaler */
190 };
191
192 extern struct stm_usart stm_usart1;
193 extern struct stm_usart stm_usart2;
194 extern struct stm_usart stm_usart3;
195
196 #define STM_USART_SR_CTS        (9)     /* CTS flag */
197 #define STM_USART_SR_LBD        (8)     /* LIN break detection flag */
198 #define STM_USART_SR_TXE        (7)     /* Transmit data register empty */
199 #define STM_USART_SR_TC         (6)     /* Transmission complete */
200 #define STM_USART_SR_RXNE       (5)     /* Read data register not empty */
201 #define STM_USART_SR_IDLE       (4)     /* IDLE line detected */
202 #define STM_USART_SR_ORE        (3)     /* Overrun error */
203 #define STM_USART_SR_NF         (2)     /* Noise detected flag */
204 #define STM_USART_SR_FE         (1)     /* Framing error */
205 #define STM_USART_SR_PE         (0)     /* Parity error */
206
207 #define STM_USART_CR1_OVER8     (15)    /* Oversampling mode */
208 #define STM_USART_CR1_UE        (13)    /* USART enable */
209 #define STM_USART_CR1_M         (12)    /* Word length */
210 #define STM_USART_CR1_WAKE      (11)    /* Wakeup method */
211 #define STM_USART_CR1_PCE       (10)    /* Parity control enable */
212 #define STM_USART_CR1_PS        (9)     /* Parity selection */
213 #define STM_USART_CR1_PEIE      (8)     /* PE interrupt enable */
214 #define STM_USART_CR1_TXEIE     (7)     /* TXE interrupt enable */
215 #define STM_USART_CR1_TCIE      (6)     /* Transmission complete interrupt enable */
216 #define STM_USART_CR1_RXNEIE    (5)     /* RXNE interrupt enable */
217 #define STM_USART_CR1_IDLEIE    (4)     /* IDLE interrupt enable */
218 #define STM_USART_CR1_TE        (3)     /* Transmitter enable */
219 #define STM_USART_CR1_RE        (2)     /* Receiver enable */
220 #define STM_USART_CR1_RWU       (1)     /* Receiver wakeup */
221 #define STM_USART_CR1_SBK       (0)     /* Send break */
222
223 #define STM_USART_CR2_LINEN     (14)    /* LIN mode enable */
224 #define STM_USART_CR2_STOP      (12)    /* STOP bits */
225 #define STM_USART_CR2_STOP_MASK 3
226 #define STM_USART_CR2_STOP_1    0
227 #define STM_USART_CR2_STOP_0_5  1
228 #define STM_USART_CR2_STOP_2    2
229 #define STM_USART_CR2_STOP_1_5  3
230
231 #define STM_USART_CR2_CLKEN     (11)    /* Clock enable */
232 #define STM_USART_CR2_CPOL      (10)    /* Clock polarity */
233 #define STM_USART_CR2_CPHA      (9)     /* Clock phase */
234 #define STM_USART_CR2_LBCL      (8)     /* Last bit clock pulse */
235 #define STM_USART_CR2_LBDIE     (6)     /* LIN break detection interrupt enable */
236 #define STM_USART_CR2_LBDL      (5)     /* lin break detection length */
237 #define STM_USART_CR2_ADD       (0)
238 #define STM_USART_CR2_ADD_MASK  0xf
239
240 #define STM_USART_CR3_ONEBITE   (11)    /* One sample bit method enable */
241 #define STM_USART_CR3_CTSIE     (10)    /* CTS interrupt enable */
242 #define STM_USART_CR3_CTSE      (9)     /* CTS enable */
243 #define STM_USART_CR3_RTSE      (8)     /* RTS enable */
244 #define STM_USART_CR3_DMAT      (7)     /* DMA enable transmitter */
245 #define STM_USART_CR3_DMAR      (6)     /* DMA enable receiver */
246 #define STM_USART_CR3_SCEN      (5)     /* Smartcard mode enable */
247 #define STM_USART_CR3_NACK      (4)     /* Smartcard NACK enable */
248 #define STM_USART_CR3_HDSEL     (3)     /* Half-duplex selection */
249 #define STM_USART_CR3_IRLP      (2)     /* IrDA low-power */
250 #define STM_USART_CR3_IREN      (1)     /* IrDA mode enable */
251 #define STM_USART_CR3_EIE       (0)     /* Error interrupt enable */
252
253 struct stm_tim {
254 };
255
256 extern struct stm_tim stm_tim9;
257
258 struct stm_tim1011 {
259         vuint32_t       cr1;
260         uint32_t        unused_4;
261         vuint32_t       smcr;
262         vuint32_t       dier;
263         vuint32_t       sr;
264         vuint32_t       egr;
265         vuint32_t       ccmr1;
266         uint32_t        unused_1c;
267         vuint32_t       ccer;
268         vuint32_t       cnt;
269         vuint32_t       psc;
270         vuint32_t       arr;
271         uint32_t        unused_30;
272         vuint32_t       ccr1;
273         uint32_t        unused_38;
274         uint32_t        unused_3c;
275         uint32_t        unused_40;
276         uint32_t        unused_44;
277         uint32_t        unused_48;
278         uint32_t        unused_4c;
279         vuint32_t       or;
280 };
281
282 extern struct stm_tim1011 stm_tim10;
283 extern struct stm_tim1011 stm_tim11;
284
285 #define STM_TIM1011_CR1_CKD     8
286 #define  STM_TIM1011_CR1_CKD_1          0
287 #define  STM_TIM1011_CR1_CKD_2          1
288 #define  STM_TIM1011_CR1_CKD_4          2
289 #define  STM_TIM1011_CR1_CKD_MASK       3
290 #define STM_TIM1011_CR1_ARPE    7
291 #define STM_TIM1011_CR1_URS     2
292 #define STM_TIM1011_CR1_UDIS    1
293 #define STM_TIM1011_CR1_CEN     0
294
295 #define STM_TIM1011_SMCR_ETP    15
296 #define STM_TIM1011_SMCR_ECE    14
297 #define STM_TIM1011_SMCR_ETPS   12
298 #define  STM_TIM1011_SMCR_ETPS_OFF      0
299 #define  STM_TIM1011_SMCR_ETPS_2        1
300 #define  STM_TIM1011_SMCR_ETPS_4        2
301 #define  STM_TIM1011_SMCR_ETPS_8        3
302 #define  STM_TIM1011_SMCR_ETPS_MASK     3
303 #define STM_TIM1011_SMCR_ETF    8
304 #define  STM_TIM1011_SMCR_ETF_NONE              0
305 #define  STM_TIM1011_SMCR_ETF_CK_INT_2          1
306 #define  STM_TIM1011_SMCR_ETF_CK_INT_4          2
307 #define  STM_TIM1011_SMCR_ETF_CK_INT_8          3
308 #define  STM_TIM1011_SMCR_ETF_DTS_2_6           4
309 #define  STM_TIM1011_SMCR_ETF_DTS_2_8           5
310 #define  STM_TIM1011_SMCR_ETF_DTS_4_6           6
311 #define  STM_TIM1011_SMCR_ETF_DTS_4_8           7
312 #define  STM_TIM1011_SMCR_ETF_DTS_8_6           8
313 #define  STM_TIM1011_SMCR_ETF_DTS_8_8           9
314 #define  STM_TIM1011_SMCR_ETF_DTS_16_5          10
315 #define  STM_TIM1011_SMCR_ETF_DTS_16_6          11
316 #define  STM_TIM1011_SMCR_ETF_DTS_16_8          12
317 #define  STM_TIM1011_SMCR_ETF_DTS_32_5          13
318 #define  STM_TIM1011_SMCR_ETF_DTS_32_6          14
319 #define  STM_TIM1011_SMCR_ETF_DTS_32_8          15
320 #define  STM_TIM1011_SMCR_ETF_MASK              15
321
322 #define STM_TIM1011_DIER_CC1E   1
323 #define STM_TIM1011_DIER_UIE    0
324
325 #define STM_TIM1011_SR_CC1OF    9
326 #define STM_TIM1011_SR_CC1IF    1
327 #define STM_TIM1011_SR_UIF      0
328
329 #define STM_TIM1011_EGR_CC1G    1
330 #define STM_TIM1011_EGR_UG      0
331
332 #define STM_TIM1011_CCMR1_OC1CE 7
333 #define STM_TIM1011_CCMR1_OC1M  4
334 #define  STM_TIM1011_CCMR1_OC1M_FROZEN                  0
335 #define  STM_TIM1011_CCMR1_OC1M_SET_1_ACTIVE_ON_MATCH   1
336 #define  STM_TIM1011_CCMR1_OC1M_SET_1_INACTIVE_ON_MATCH 2
337 #define  STM_TIM1011_CCMR1_OC1M_TOGGLE                  3
338 #define  STM_TIM1011_CCMR1_OC1M_FORCE_INACTIVE          4
339 #define  STM_TIM1011_CCMR1_OC1M_FORCE_ACTIVE            5
340 #define  STM_TIM1011_CCMR1_OC1M_PWM_MODE_1              6
341 #define  STM_TIM1011_CCMR1_OC1M_PWM_MODE_2              7
342 #define  STM_TIM1011_CCMR1_OC1M_MASK                    7
343 #define STM_TIM1011_CCMR1_OC1PE 3
344 #define STM_TIM1011_CCMR1_OC1FE 2
345 #define STM_TIM1011_CCMR1_CC1S  0
346 #define  STM_TIM1011_CCMR1_CC1S_OUTPUT                  0
347 #define  STM_TIM1011_CCMR1_CC1S_INPUT_TI1               1
348 #define  STM_TIM1011_CCMR1_CC1S_INPUT_TI2               2
349 #define  STM_TIM1011_CCMR1_CC1S_INPUT_TRC               3
350 #define  STM_TIM1011_CCMR1_CC1S_MASK                    3
351
352 #define  STM_TIM1011_CCMR1_IC1F_NONE            0
353 #define  STM_TIM1011_CCMR1_IC1F_CK_INT_2        1
354 #define  STM_TIM1011_CCMR1_IC1F_CK_INT_4        2
355 #define  STM_TIM1011_CCMR1_IC1F_CK_INT_8        3
356 #define  STM_TIM1011_CCMR1_IC1F_DTS_2_6         4
357 #define  STM_TIM1011_CCMR1_IC1F_DTS_2_8         5
358 #define  STM_TIM1011_CCMR1_IC1F_DTS_4_6         6
359 #define  STM_TIM1011_CCMR1_IC1F_DTS_4_8         7
360 #define  STM_TIM1011_CCMR1_IC1F_DTS_8_6         8
361 #define  STM_TIM1011_CCMR1_IC1F_DTS_8_8         9
362 #define  STM_TIM1011_CCMR1_IC1F_DTS_16_5        10
363 #define  STM_TIM1011_CCMR1_IC1F_DTS_16_6        11
364 #define  STM_TIM1011_CCMR1_IC1F_DTS_16_8        12
365 #define  STM_TIM1011_CCMR1_IC1F_DTS_32_5        13
366 #define  STM_TIM1011_CCMR1_IC1F_DTS_32_6        14
367 #define  STM_TIM1011_CCMR1_IC1F_DTS_32_8        15
368 #define  STM_TIM1011_CCMR1_IC1F_MASK            15
369 #define STM_TIM1011_CCMR1_IC1PSC        2
370 #define  STM_TIM1011_CCMR1_IC1PSC_1             0
371 #define  STM_TIM1011_CCMR1_IC1PSC_2             1
372 #define  STM_TIM1011_CCMR1_IC1PSC_4             2
373 #define  STM_TIM1011_CCMR1_IC1PSC_8             3
374 #define  STM_TIM1011_CCMR1_IC1PSC_MASK          3
375 #define STM_TIM1011_CCMR1_CC1S          0
376
377 #define STM_TIM1011_CCER_CC1NP          3
378 #define STM_TIM1011_CCER_CC1P           1
379 #define STM_TIM1011_CCER_CC1E           0
380
381 #define STM_TIM1011_OR_TI1_RMP_RI       3
382 #define STM_TIM1011_ETR_RMP             2
383 #define STM_TIM1011_TI1_RMP             0
384 #define  STM_TIM1011_TI1_RMP_GPIO               0
385 #define  STM_TIM1011_TI1_RMP_LSI                1
386 #define  STM_TIM1011_TI1_RMP_LSE                2
387 #define  STM_TIM1011_TI1_RMP_RTC                3
388 #define  STM_TIM1011_TI1_RMP_MASK               3
389
390 /* Flash interface */
391
392 struct stm_flash {
393         vuint32_t       acr;
394         vuint32_t       pecr;
395         vuint32_t       pdkeyr;
396         vuint32_t       pekeyr;
397
398         vuint32_t       prgkeyr;
399         vuint32_t       optkeyr;
400         vuint32_t       sr;
401         vuint32_t       obr;
402
403         vuint32_t       wrpr;
404 };
405
406 extern struct stm_flash stm_flash;
407
408 #define STM_FLASH_ACR_RUN_PD    (4)
409 #define STM_FLASH_ACR_SLEEP_PD  (3)
410 #define STM_FLASH_ACR_ACC64     (2)
411 #define STM_FLASH_ACR_PRFEN     (1)
412 #define STM_FLASH_ACR_LATENCY   (0)
413
414 #define STM_FLASH_PECR_OBL_LAUNCH       18
415 #define STM_FLASH_PECR_ERRIE            17
416 #define STM_FLASH_PECR_EOPIE            16
417 #define STM_FLASH_PECR_FPRG             10
418 #define STM_FLASH_PECR_ERASE            9
419 #define STM_FLASH_PECR_FTDW             8
420 #define STM_FLASH_PECR_DATA             4
421 #define STM_FLASH_PECR_PROG             3
422 #define STM_FLASH_PECR_OPTLOCK          2
423 #define STM_FLASH_PECR_PRGLOCK          1
424 #define STM_FLASH_PECR_PELOCK           0
425
426 #define STM_FLASH_SR_OPTVERR            11
427 #define STM_FLASH_SR_SIZERR             10
428 #define STM_FLASH_SR_PGAERR             9
429 #define STM_FLASH_SR_WRPERR             8
430 #define STM_FLASH_SR_READY              3
431 #define STM_FLASH_SR_ENDHV              2
432 #define STM_FLASH_SR_EOP                1
433 #define STM_FLASH_SR_BSY                0
434
435 #define STM_FLASH_PEKEYR_PEKEY1 0x89ABCDEF
436 #define STM_FLASH_PEKEYR_PEKEY2 0x02030405
437
438 struct stm_rcc {
439         vuint32_t       cr;
440         vuint32_t       icscr;
441         vuint32_t       cfgr;
442         vuint32_t       cir;
443
444         vuint32_t       ahbrstr;
445         vuint32_t       apb2rstr;
446         vuint32_t       apb1rstr;
447         vuint32_t       ahbenr;
448
449         vuint32_t       apb2enr;
450         vuint32_t       apb1enr;
451         vuint32_t       ahblenr;
452         vuint32_t       apb2lpenr;
453
454         vuint32_t       apb1lpenr;
455         vuint32_t       csr;
456 };
457
458 extern struct stm_rcc stm_rcc;
459
460 /* Nominal high speed internal oscillator frequency is 16MHz */
461 #define STM_HSI_FREQ            16000000
462
463 #define STM_RCC_CR_RTCPRE       (29)
464 #define  STM_RCC_CR_RTCPRE_HSE_DIV_2    0
465 #define  STM_RCC_CR_RTCPRE_HSE_DIV_4    1
466 #define  STM_RCC_CR_RTCPRE_HSE_DIV_8    2
467 #define  STM_RCC_CR_RTCPRE_HSE_DIV_16   3
468 #define  STM_RCC_CR_RTCPRE_HSE_MASK     3
469
470 #define STM_RCC_CR_CSSON        (28)
471 #define STM_RCC_CR_PLLRDY       (25)
472 #define STM_RCC_CR_PLLON        (24)
473 #define STM_RCC_CR_HSEBYP       (18)
474 #define STM_RCC_CR_HSERDY       (17)
475 #define STM_RCC_CR_HSEON        (16)
476 #define STM_RCC_CR_MSIRDY       (9)
477 #define STM_RCC_CR_MSION        (8)
478 #define STM_RCC_CR_HSIRDY       (1)
479 #define STM_RCC_CR_HSION        (0)
480
481 #define STM_RCC_CFGR_MCOPRE     (28)
482 #define  STM_RCC_CFGR_MCOPRE_DIV_1      0
483 #define  STM_RCC_CFGR_MCOPRE_DIV_2      1
484 #define  STM_RCC_CFGR_MCOPRE_DIV_4      2
485 #define  STM_RCC_CFGR_MCOPRE_DIV_8      3
486 #define  STM_RCC_CFGR_MCOPRE_DIV_16     4
487 #define  STM_RCC_CFGR_MCOPRE_DIV_MASK   7
488
489 #define STM_RCC_CFGR_MCOSEL     (24)
490 #define  STM_RCC_CFGR_MCOSEL_DISABLE    0
491 #define  STM_RCC_CFGR_MCOSEL_SYSCLK     1
492 #define  STM_RCC_CFGR_MCOSEL_HSI        2
493 #define  STM_RCC_CFGR_MCOSEL_MSI        3
494 #define  STM_RCC_CFGR_MCOSEL_HSE        4
495 #define  STM_RCC_CFGR_MCOSEL_PLL        5
496 #define  STM_RCC_CFGR_MCOSEL_LSI        6
497 #define  STM_RCC_CFGR_MCOSEL_LSE        7
498 #define  STM_RCC_CFGR_MCOSEL_MASK       7
499
500 #define STM_RCC_CFGR_PLLDIV     (22)
501 #define  STM_RCC_CFGR_PLLDIV_2          1
502 #define  STM_RCC_CFGR_PLLDIV_3          2
503 #define  STM_RCC_CFGR_PLLDIV_4          3
504 #define  STM_RCC_CFGR_PLLDIV_MASK       3
505
506 #define STM_RCC_CFGR_PLLMUL     (18)
507 #define  STM_RCC_CFGR_PLLMUL_3          0
508 #define  STM_RCC_CFGR_PLLMUL_4          1
509 #define  STM_RCC_CFGR_PLLMUL_6          2
510 #define  STM_RCC_CFGR_PLLMUL_8          3
511 #define  STM_RCC_CFGR_PLLMUL_12         4
512 #define  STM_RCC_CFGR_PLLMUL_16         5
513 #define  STM_RCC_CFGR_PLLMUL_24         6
514 #define  STM_RCC_CFGR_PLLMUL_32         7
515 #define  STM_RCC_CFGR_PLLMUL_48         8
516 #define  STM_RCC_CFGR_PLLMUL_MASK       0xf
517
518 #define STM_RCC_CFGR_PLLSRC     (16)
519
520 #define STM_RCC_CFGR_PPRE2      (11)
521 #define  STM_RCC_CFGR_PPRE2_DIV_1       0
522 #define  STM_RCC_CFGR_PPRE2_DIV_2       4
523 #define  STM_RCC_CFGR_PPRE2_DIV_4       5
524 #define  STM_RCC_CFGR_PPRE2_DIV_8       6
525 #define  STM_RCC_CFGR_PPRE2_DIV_16      7
526 #define  STM_RCC_CFGR_PPRE2_MASK        7
527
528 #define STM_RCC_CFGR_PPRE1      (8)
529 #define  STM_RCC_CFGR_PPRE1_DIV_1       0
530 #define  STM_RCC_CFGR_PPRE1_DIV_2       4
531 #define  STM_RCC_CFGR_PPRE1_DIV_4       5
532 #define  STM_RCC_CFGR_PPRE1_DIV_8       6
533 #define  STM_RCC_CFGR_PPRE1_DIV_16      7
534 #define  STM_RCC_CFGR_PPRE1_MASK        7
535
536 #define STM_RCC_CFGR_HPRE       (4)
537 #define  STM_RCC_CFGR_HPRE_DIV_1        0
538 #define  STM_RCC_CFGR_HPRE_DIV_2        8
539 #define  STM_RCC_CFGR_HPRE_DIV_4        9
540 #define  STM_RCC_CFGR_HPRE_DIV_8        0xa
541 #define  STM_RCC_CFGR_HPRE_DIV_16       0xb
542 #define  STM_RCC_CFGR_HPRE_DIV_64       0xc
543 #define  STM_RCC_CFGR_HPRE_DIV_128      0xd
544 #define  STM_RCC_CFGR_HPRE_DIV_256      0xe
545 #define  STM_RCC_CFGR_HPRE_DIV_512      0xf
546 #define  STM_RCC_CFGR_HPRE_MASK         0xf
547
548 #define STM_RCC_CFGR_SWS        (2)
549 #define  STM_RCC_CFGR_SWS_MSI           0
550 #define  STM_RCC_CFGR_SWS_HSI           1
551 #define  STM_RCC_CFGR_SWS_HSE           2
552 #define  STM_RCC_CFGR_SWS_PLL           3
553 #define  STM_RCC_CFGR_SWS_MASK          3
554
555 #define STM_RCC_CFGR_SW         (0)
556 #define  STM_RCC_CFGR_SW_MSI            0
557 #define  STM_RCC_CFGR_SW_HSI            1
558 #define  STM_RCC_CFGR_SW_HSE            2
559 #define  STM_RCC_CFGR_SW_PLL            3
560 #define  STM_RCC_CFGR_SW_MASK           3
561
562 #define STM_RCC_AHBENR_DMA1EN           (24)
563 #define STM_RCC_AHBENR_FLITFEN          (15)
564 #define STM_RCC_AHBENR_CRCEN            (12)
565 #define STM_RCC_AHBENR_GPIOHEN          (5)
566 #define STM_RCC_AHBENR_GPIOEEN          (4)
567 #define STM_RCC_AHBENR_GPIODEN          (3)
568 #define STM_RCC_AHBENR_GPIOCEN          (2)
569 #define STM_RCC_AHBENR_GPIOBEN          (1)
570 #define STM_RCC_AHBENR_GPIOAEN          (0)
571
572 #define STM_RCC_APB2ENR_USART1EN        (14)
573 #define STM_RCC_APB2ENR_SPI1EN          (12)
574 #define STM_RCC_APB2ENR_ADC1EN          (9)
575 #define STM_RCC_APB2ENR_TIM11EN         (4)
576 #define STM_RCC_APB2ENR_TIM10EN         (3)
577 #define STM_RCC_APB2ENR_TIM9EN          (2)
578 #define STM_RCC_APB2ENR_SYSCFGEN        (0)
579
580 #define STM_RCC_APB1ENR_COMPEN          (31)
581 #define STM_RCC_APB1ENR_DACEN           (29)
582 #define STM_RCC_APB1ENR_PWREN           (28)
583 #define STM_RCC_APB1ENR_USBEN           (23)
584 #define STM_RCC_APB1ENR_I2C2EN          (22)
585 #define STM_RCC_APB1ENR_I2C1EN          (21)
586 #define STM_RCC_APB1ENR_USART3EN        (18)
587 #define STM_RCC_APB1ENR_USART2EN        (17)
588 #define STM_RCC_APB1ENR_SPI2EN          (14)
589 #define STM_RCC_APB1ENR_WWDGEN          (11)
590 #define STM_RCC_APB1ENR_LCDEN           (9)
591 #define STM_RCC_APB1ENR_TIM7EN          (5)
592 #define STM_RCC_APB1ENR_TIM6EN          (4)
593 #define STM_RCC_APB1ENR_TIM4EN          (2)
594 #define STM_RCC_APB1ENR_TIM3EN          (1)
595 #define STM_RCC_APB1ENR_TIM2EN          (0)
596
597 #define STM_RCC_CSR_LPWRRSTF            (31)
598 #define STM_RCC_CSR_WWDGRSTF            (30)
599 #define STM_RCC_CSR_IWDGRSTF            (29)
600 #define STM_RCC_CSR_SFTRSTF             (28)
601 #define STM_RCC_CSR_PORRSTF             (27)
602 #define STM_RCC_CSR_PINRSTF             (26)
603 #define STM_RCC_CSR_OBLRSTF             (25)
604 #define STM_RCC_CSR_RMVF                (24)
605 #define STM_RCC_CSR_RTFRST              (23)
606 #define STM_RCC_CSR_RTCEN               (22)
607 #define STM_RCC_CSR_RTCSEL              (16)
608
609 #define  STM_RCC_CSR_RTCSEL_NONE                0
610 #define  STM_RCC_CSR_RTCSEL_LSE                 1
611 #define  STM_RCC_CSR_RTCSEL_LSI                 2
612 #define  STM_RCC_CSR_RTCSEL_HSE                 3
613 #define  STM_RCC_CSR_RTCSEL_MASK                3
614
615 #define STM_RCC_CSR_LSEBYP              (10)
616 #define STM_RCC_CSR_LSERDY              (9)
617 #define STM_RCC_CSR_LSEON               (8)
618 #define STM_RCC_CSR_LSIRDY              (1)
619 #define STM_RCC_CSR_LSION               (0)
620
621 struct stm_pwr {
622         vuint32_t       cr;
623         vuint32_t       csr;
624 };
625
626 extern struct stm_pwr stm_pwr;
627
628 #define STM_PWR_CR_LPRUN        (14)
629
630 #define STM_PWR_CR_VOS          (11)
631 #define  STM_PWR_CR_VOS_1_8             1
632 #define  STM_PWR_CR_VOS_1_5             2
633 #define  STM_PWR_CR_VOS_1_2             3
634 #define  STM_PWR_CR_VOS_MASK            3
635
636 #define STM_PWR_CR_FWU          (10)
637 #define STM_PWR_CR_ULP          (9)
638 #define STM_PWR_CR_DBP          (8)
639
640 #define STM_PWR_CR_PLS          (5)
641 #define  STM_PWR_CR_PLS_1_9     0
642 #define  STM_PWR_CR_PLS_2_1     1
643 #define  STM_PWR_CR_PLS_2_3     2
644 #define  STM_PWR_CR_PLS_2_5     3
645 #define  STM_PWR_CR_PLS_2_7     4
646 #define  STM_PWR_CR_PLS_2_9     5
647 #define  STM_PWR_CR_PLS_3_1     6
648 #define  STM_PWR_CR_PLS_EXT     7
649 #define  STM_PWR_CR_PLS_MASK    7
650
651 #define STM_PWR_CR_PVDE         (4)
652 #define STM_PWR_CR_CSBF         (3)
653 #define STM_PWR_CR_CWUF         (2)
654 #define STM_PWR_CR_PDDS         (1)
655 #define STM_PWR_CR_LPSDSR       (0)
656
657 #define STM_PWR_CSR_EWUP3       (10)
658 #define STM_PWR_CSR_EWUP2       (9)
659 #define STM_PWR_CSR_EWUP1       (8)
660 #define STM_PWR_CSR_REGLPF      (5)
661 #define STM_PWR_CSR_VOSF        (4)
662 #define STM_PWR_CSR_VREFINTRDYF (3)
663 #define STM_PWR_CSR_PVDO        (2)
664 #define STM_PWR_CSR_SBF         (1)
665 #define STM_PWR_CSR_WUF         (0)
666
667 struct stm_tim67 {
668         vuint32_t       cr1;
669         vuint32_t       cr2;
670         uint32_t        _unused_08;
671         vuint32_t       dier;
672
673         vuint32_t       sr;
674         vuint32_t       egr;
675         uint32_t        _unused_18;
676         uint32_t        _unused_1c;
677
678         uint32_t        _unused_20;
679         vuint32_t       cnt;
680         vuint32_t       psc;
681         vuint32_t       arr;
682 };
683
684 extern struct stm_tim67 stm_tim6;
685
686 #define STM_TIM67_CR1_ARPE      (7)
687 #define STM_TIM67_CR1_OPM       (3)
688 #define STM_TIM67_CR1_URS       (2)
689 #define STM_TIM67_CR1_UDIS      (1)
690 #define STM_TIM67_CR1_CEN       (0)
691
692 #define STM_TIM67_CR2_MMS       (4)
693 #define  STM_TIM67_CR2_MMS_RESET        0
694 #define  STM_TIM67_CR2_MMS_ENABLE       1
695 #define  STM_TIM67_CR2_MMS_UPDATE       2
696 #define  STM_TIM67_CR2_MMS_MASK         7
697
698 #define STM_TIM67_DIER_UDE      (8)
699 #define STM_TIM67_DIER_UIE      (0)
700
701 #define STM_TIM67_SR_UIF        (0)
702
703 #define STM_TIM67_EGR_UG        (0)
704
705 struct stm_lcd {
706         vuint32_t       cr;
707         vuint32_t       fcr;
708         vuint32_t       sr;
709         vuint32_t       clr;
710         uint32_t        unused_0x10;
711         vuint32_t       ram[8*2];
712 };
713
714 extern struct stm_lcd stm_lcd;
715
716 #define STM_LCD_CR_MUX_SEG              (7)
717
718 #define STM_LCD_CR_BIAS                 (5)
719 #define  STM_LCD_CR_BIAS_1_4            0
720 #define  STM_LCD_CR_BIAS_1_2            1
721 #define  STM_LCD_CR_BIAS_1_3            2
722 #define  STM_LCD_CR_BIAS_MASK           3
723
724 #define STM_LCD_CR_DUTY                 (2)
725 #define  STM_LCD_CR_DUTY_STATIC         0
726 #define  STM_LCD_CR_DUTY_1_2            1
727 #define  STM_LCD_CR_DUTY_1_3            2
728 #define  STM_LCD_CR_DUTY_1_4            3
729 #define  STM_LCD_CR_DUTY_1_8            4
730 #define  STM_LCD_CR_DUTY_MASK           7
731
732 #define STM_LCD_CR_VSEL                 (1)
733 #define STM_LCD_CR_LCDEN                (0)
734
735 #define STM_LCD_FCR_PS                  (22)
736 #define  STM_LCD_FCR_PS_1               0x0
737 #define  STM_LCD_FCR_PS_2               0x1
738 #define  STM_LCD_FCR_PS_4               0x2
739 #define  STM_LCD_FCR_PS_8               0x3
740 #define  STM_LCD_FCR_PS_16              0x4
741 #define  STM_LCD_FCR_PS_32              0x5
742 #define  STM_LCD_FCR_PS_64              0x6
743 #define  STM_LCD_FCR_PS_128             0x7
744 #define  STM_LCD_FCR_PS_256             0x8
745 #define  STM_LCD_FCR_PS_512             0x9
746 #define  STM_LCD_FCR_PS_1024            0xa
747 #define  STM_LCD_FCR_PS_2048            0xb
748 #define  STM_LCD_FCR_PS_4096            0xc
749 #define  STM_LCD_FCR_PS_8192            0xd
750 #define  STM_LCD_FCR_PS_16384           0xe
751 #define  STM_LCD_FCR_PS_32768           0xf
752 #define  STM_LCD_FCR_PS_MASK            0xf
753
754 #define STM_LCD_FCR_DIV                 (18)
755 #define STM_LCD_FCR_DIV_16              0x0
756 #define STM_LCD_FCR_DIV_17              0x1
757 #define STM_LCD_FCR_DIV_18              0x2
758 #define STM_LCD_FCR_DIV_19              0x3
759 #define STM_LCD_FCR_DIV_20              0x4
760 #define STM_LCD_FCR_DIV_21              0x5
761 #define STM_LCD_FCR_DIV_22              0x6
762 #define STM_LCD_FCR_DIV_23              0x7
763 #define STM_LCD_FCR_DIV_24              0x8
764 #define STM_LCD_FCR_DIV_25              0x9
765 #define STM_LCD_FCR_DIV_26              0xa
766 #define STM_LCD_FCR_DIV_27              0xb
767 #define STM_LCD_FCR_DIV_28              0xc
768 #define STM_LCD_FCR_DIV_29              0xd
769 #define STM_LCD_FCR_DIV_30              0xe
770 #define STM_LCD_FCR_DIV_31              0xf
771 #define STM_LCD_FCR_DIV_MASK            0xf
772
773 #define STM_LCD_FCR_BLINK               (16)
774 #define  STM_LCD_FCR_BLINK_DISABLE              0
775 #define  STM_LCD_FCR_BLINK_SEG0_COM0            1
776 #define  STM_LCD_FCR_BLINK_SEG0_COMALL          2
777 #define  STM_LCD_FCR_BLINK_SEGALL_COMALL        3
778 #define  STM_LCD_FCR_BLINK_MASK                 3
779
780 #define STM_LCD_FCR_BLINKF              (13)
781 #define  STM_LCD_FCR_BLINKF_8                   0
782 #define  STM_LCD_FCR_BLINKF_16                  1
783 #define  STM_LCD_FCR_BLINKF_32                  2
784 #define  STM_LCD_FCR_BLINKF_64                  3
785 #define  STM_LCD_FCR_BLINKF_128                 4
786 #define  STM_LCD_FCR_BLINKF_256                 5
787 #define  STM_LCD_FCR_BLINKF_512                 6
788 #define  STM_LCD_FCR_BLINKF_1024                7
789 #define  STM_LCD_FCR_BLINKF_MASK                7
790
791 #define STM_LCD_FCR_CC                  (10)
792 #define  STM_LCD_FCR_CC_MASK                    7
793
794 #define STM_LCD_FCR_DEAD                (7)
795 #define  STM_LCD_FCR_DEAD_MASK                  7
796
797 #define STM_LCD_FCR_PON                 (4)
798 #define  STM_LCD_FCR_PON_MASK                   7
799
800 #define STM_LCD_FCR_UDDIE               (3)
801 #define STM_LCD_FCR_SOFIE               (1)
802 #define STM_LCD_FCR_HD                  (0)
803
804 #define STM_LCD_SR_FCRSF                (5)
805 #define STM_LCD_SR_RDY                  (4)
806 #define STM_LCD_SR_UDD                  (3)
807 #define STM_LCD_SR_UDR                  (2)
808 #define STM_LCD_SR_SOF                  (1)
809 #define STM_LCD_SR_ENS                  (0)
810
811 #define STM_LCD_CLR_UDDC                (3)
812 #define STM_LCD_CLR_SOFC                (1)
813
814 struct stm_nvic {
815         vuint32_t       iser[3];        /* 0x000 */
816
817         uint8_t         _unused00c[0x080 - 0x00c];
818
819         vuint32_t       icer[3];        /* 0x080 */
820
821         uint8_t         _unused08c[0x100 - 0x08c];
822
823         vuint32_t       ispr[3];        /* 0x100 */
824
825         uint8_t         _unused10c[0x180 - 0x10c];
826
827         vuint32_t       icpr[3];        /* 0x180 */
828
829         uint8_t         _unused18c[0x200 - 0x18c];
830
831         vuint32_t       iabr[3];        /* 0x200 */
832
833         uint8_t         _unused20c[0x300 - 0x20c];
834
835         vuint32_t       ipr[21];        /* 0x300 */
836
837         uint8_t         _unused324[0xe00 - 0x324];
838
839         vuint32_t       stir;           /* 0xe00 */
840 };
841
842 extern struct stm_nvic stm_nvic;
843
844 #define IRQ_REG(irq)    ((irq) >> 5)
845 #define IRQ_BIT(irq)    ((irq) & 0x1f)
846 #define IRQ_MASK(irq)   (1 << IRQ_BIT(irq))
847 #define IRQ_BOOL(v,irq) (((v) >> IRQ_BIT(irq)) & 1)
848
849 static inline void
850 stm_nvic_set_enable(int irq) {
851         stm_nvic.iser[IRQ_REG(irq)] = IRQ_MASK(irq);
852 }
853
854 static inline void
855 stm_nvic_clear_enable(int irq) {
856         stm_nvic.icer[IRQ_REG(irq)] = IRQ_MASK(irq);
857 }
858
859 static inline int
860 stm_nvic_enabled(int irq) {
861         return IRQ_BOOL(stm_nvic.iser[IRQ_REG(irq)], irq);
862 }
863         
864 static inline void
865 stm_nvic_set_pending(int irq) {
866         stm_nvic.ispr[IRQ_REG(irq)] = IRQ_MASK(irq);
867 }
868
869 static inline void
870 stm_nvic_clear_pending(int irq) {
871         stm_nvic.icpr[IRQ_REG(irq)] = IRQ_MASK(irq);
872 }
873
874 static inline int
875 stm_nvic_pending(int irq) {
876         return IRQ_BOOL(stm_nvic.ispr[IRQ_REG(irq)], irq);
877 }
878
879 static inline int
880 stm_nvic_active(int irq) {
881         return IRQ_BOOL(stm_nvic.iabr[IRQ_REG(irq)], irq);
882 }
883
884 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
885 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
886 #define IRQ_PRIO_MASK(irq)      (0xff << IRQ_PRIO_BIT(irq))
887
888 static inline void
889 stm_nvic_set_priority(int irq, uint8_t prio) {
890         int             n = IRQ_PRIO_REG(irq);
891         uint32_t        v;
892
893         v = stm_nvic.ipr[n];
894         v &= ~IRQ_PRIO_MASK(irq);
895         v |= (prio) << IRQ_PRIO_BIT(irq);
896         stm_nvic.ipr[n] = v;
897 }
898
899 static inline uint8_t
900 stm_nvic_get_priority(int irq) {
901         return (stm_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
902 }
903
904 struct stm_mpu {
905         vuint32_t       typer;
906         vuint32_t       cr;
907         vuint32_t       rnr;
908         vuint32_t       rbar;
909
910         vuint32_t       rasr;
911         vuint32_t       rbar_a1;
912         vuint32_t       rasr_a1;
913         vuint32_t       rbar_a2;
914         vuint32_t       rasr_a2;
915         vuint32_t       rbar_a3;
916         vuint32_t       rasr_a3;
917 };
918
919 extern struct stm_mpu stm_mpu;
920
921 #define STM_MPU_TYPER_IREGION   16
922 #define  STM_MPU_TYPER_IREGION_MASK     0xff
923 #define STM_MPU_TYPER_DREGION   8
924 #define  STM_MPU_TYPER_DREGION_MASK     0xff
925 #define STM_MPU_TYPER_SEPARATE  0
926
927 #define STM_MPU_CR_PRIVDEFENA   2
928 #define STM_MPU_CR_HFNMIENA     1
929 #define STM_MPU_CR_ENABLE       0
930
931 #define STM_MPU_RNR_REGION      0
932 #define STM_MPU_RNR_REGION_MASK         0xff
933
934 #define STM_MPU_RBAR_ADDR       5
935 #define STM_MPU_RBAR_ADDR_MASK          0x7ffffff
936
937 #define STM_MPU_RBAR_VALID      4
938 #define STM_MPU_RBAR_REGION     0
939 #define STM_MPU_RBAR_REGION_MASK        0xf
940
941 #define STM_MPU_RASR_XN         28
942 #define STM_MPU_RASR_AP         24
943 #define  STM_MPU_RASR_AP_NONE_NONE      0
944 #define  STM_MPU_RASR_AP_RW_NONE        1
945 #define  STM_MPU_RASR_AP_RW_RO          2
946 #define  STM_MPU_RASR_AP_RW_RW          3
947 #define  STM_MPU_RASR_AP_RO_NONE        5
948 #define  STM_MPU_RASR_AP_RO_RO          6
949 #define  STM_MPU_RASR_AP_MASK           7
950 #define STM_MPU_RASR_TEX        19
951 #define  STM_MPU_RASR_TEX_MASK          7
952 #define STM_MPU_RASR_S          18
953 #define STM_MPU_RASR_C          17
954 #define STM_MPU_RASR_B          16
955 #define STM_MPU_RASR_SRD        8
956 #define  STM_MPU_RASR_SRD_MASK          0xff
957 #define STM_MPU_RASR_SIZE       1
958 #define  STM_MPU_RASR_SIZE_MASK         0x1f
959 #define STM_MPU_RASR_ENABLE     0
960
961 #define isr(name) void stm_ ## name ## _isr(void);
962
963 isr(nmi)
964 isr(hardfault)
965 isr(memmanage)
966 isr(busfault)
967 isr(usagefault)
968 isr(svc)
969 isr(debugmon)
970 isr(pendsv)
971 isr(systick)
972 isr(wwdg)
973 isr(pvd)
974 isr(tamper_stamp)
975 isr(rtc_wkup)
976 isr(flash)
977 isr(rcc)
978 isr(exti0)
979 isr(exti1)
980 isr(exti2)
981 isr(exti3)
982 isr(exti4)
983 isr(dma1_channel1)
984 isr(dma1_channel2)
985 isr(dma1_channel3)
986 isr(dma1_channel4)
987 isr(dma1_channel5)
988 isr(dma1_channel6)
989 isr(dma1_channel7)
990 isr(adc1)
991 isr(usb_hp)
992 isr(usb_lp)
993 isr(dac)
994 isr(comp)
995 isr(exti9_5)
996 isr(lcd)
997 isr(tim9)
998 isr(tim10)
999 isr(tim11)
1000 isr(tim2)
1001 isr(tim3)
1002 isr(tim4)
1003 isr(i2c1_ev)
1004 isr(i2c1_er)
1005 isr(i2c2_ev)
1006 isr(i2c2_er)
1007 isr(spi1)
1008 isr(spi2)
1009 isr(usart1)
1010 isr(usart2)
1011 isr(usart3)
1012 isr(exti15_10)
1013 isr(rtc_alarm)
1014 isr(usb_fs_wkup)
1015 isr(tim6)
1016 isr(tim7)
1017
1018 #undef isr
1019
1020 #define STM_ISR_WWDG_POS                0
1021 #define STM_ISR_PVD_POS                 1
1022 #define STM_ISR_TAMPER_STAMP_POS        2
1023 #define STM_ISR_RTC_WKUP_POS            3
1024 #define STM_ISR_FLASH_POS               4
1025 #define STM_ISR_RCC_POS                 5
1026 #define STM_ISR_EXTI0_POS               6
1027 #define STM_ISR_EXTI1_POS               7
1028 #define STM_ISR_EXTI2_POS               8
1029 #define STM_ISR_EXTI3_POS               9
1030 #define STM_ISR_EXTI4_POS               10
1031 #define STM_ISR_DMA1_CHANNEL1_POS       11
1032 #define STM_ISR_DMA2_CHANNEL1_POS       12
1033 #define STM_ISR_DMA3_CHANNEL1_POS       13
1034 #define STM_ISR_DMA4_CHANNEL1_POS       14
1035 #define STM_ISR_DMA5_CHANNEL1_POS       15
1036 #define STM_ISR_DMA6_CHANNEL1_POS       16
1037 #define STM_ISR_DMA7_CHANNEL1_POS       17
1038 #define STM_ISR_ADC1_POS                18
1039 #define STM_ISR_USB_HP_POS              19
1040 #define STM_ISR_USB_LP_POS              20
1041 #define STM_ISR_DAC_POS                 21
1042 #define STM_ISR_COMP_POS                22
1043 #define STM_ISR_EXTI9_5_POS             23
1044 #define STM_ISR_LCD_POS                 24
1045 #define STM_ISR_TIM9_POS                25
1046 #define STM_ISR_TIM10_POS               26
1047 #define STM_ISR_TIM11_POS               27
1048 #define STM_ISR_TIM2_POS                28
1049 #define STM_ISR_TIM3_POS                29
1050 #define STM_ISR_TIM4_POS                30
1051 #define STM_ISR_I2C1_EV_POS             31
1052 #define STM_ISR_I2C1_ER_POS             32
1053 #define STM_ISR_I2C2_EV_POS             33
1054 #define STM_ISR_I2C2_ER_POS             34
1055 #define STM_ISR_SPI1_POS                35
1056 #define STM_ISR_SPI2_POS                36
1057 #define STM_ISR_USART1_POS              37
1058 #define STM_ISR_USART2_POS              38
1059 #define STM_ISR_USART3_POS              39
1060 #define STM_ISR_EXTI15_10_POS           40
1061 #define STM_ISR_RTC_ALARM_POS           41
1062 #define STM_ISR_USB_FS_WKUP_POS         42
1063 #define STM_ISR_TIM6_POS                43
1064 #define STM_ISR_TIM7_POS                44
1065
1066 struct stm_syscfg {
1067         vuint32_t       memrmp;
1068         vuint32_t       pmc;
1069         vuint32_t       exticr[4];
1070 };
1071
1072 extern struct stm_syscfg stm_syscfg;
1073
1074 #define STM_SYSCFG_MEMRMP_MEM_MODE      0
1075 #define  STM_SYSCFG_MEMRMP_MEM_MODE_MAIN_FLASH          0
1076 #define  STM_SYSCFG_MEMRMP_MEM_MODE_SYSTEM_FLASH        1
1077 #define  STM_SYSCFG_MEMRMP_MEM_MODE_SRAM                3
1078 #define  STM_SYSCFG_MEMRMP_MEM_MODE_MASK                3
1079
1080 #define STM_SYSCFG_PMC_USB_PU           0
1081
1082 #define STM_SYSCFG_EXTICR_PA            0
1083 #define STM_SYSCFG_EXTICR_PB            1
1084 #define STM_SYSCFG_EXTICR_PC            2
1085 #define STM_SYSCFG_EXTICR_PD            3
1086 #define STM_SYSCFG_EXTICR_PE            4
1087 #define STM_SYSCFG_EXTICR_PH            5
1088
1089 static inline void
1090 stm_exticr_set(struct stm_gpio *gpio, int pin) {
1091         uint8_t reg = pin >> 2;
1092         uint8_t shift = (pin & 3) << 2;
1093         uint8_t val = 0;
1094
1095         /* Enable SYSCFG */
1096         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
1097
1098         if (gpio == &stm_gpioa)
1099                 val = STM_SYSCFG_EXTICR_PA;
1100         else if (gpio == &stm_gpiob)
1101                 val = STM_SYSCFG_EXTICR_PB;
1102         else if (gpio == &stm_gpioc)
1103                 val = STM_SYSCFG_EXTICR_PC;
1104         else if (gpio == &stm_gpiod)
1105                 val = STM_SYSCFG_EXTICR_PD;
1106         else if (gpio == &stm_gpioe)
1107                 val = STM_SYSCFG_EXTICR_PE;
1108
1109         stm_syscfg.exticr[reg] = (stm_syscfg.exticr[reg] & ~(0xf << shift)) | val << shift;
1110 }
1111
1112
1113 struct stm_dma_channel {
1114         vuint32_t       ccr;
1115         vuint32_t       cndtr;
1116         vvoid_t         cpar;
1117         vvoid_t         cmar;
1118         vuint32_t       reserved;
1119 };
1120
1121 #define STM_NUM_DMA     7
1122
1123 struct stm_dma {
1124         vuint32_t               isr;
1125         vuint32_t               ifcr;
1126         struct stm_dma_channel  channel[STM_NUM_DMA];
1127 };
1128
1129 extern struct stm_dma stm_dma;
1130
1131 /* DMA channels go from 1 to 7, instead of 0 to 6 (sigh)
1132  */
1133
1134 #define STM_DMA_INDEX(channel)          ((channel) - 1)
1135
1136 #define STM_DMA_ISR(index)              ((index) << 2)
1137 #define STM_DMA_ISR_MASK                        0xf
1138 #define STM_DMA_ISR_TEIF                        3
1139 #define STM_DMA_ISR_HTIF                        2
1140 #define STM_DMA_ISR_TCIF                        1
1141 #define STM_DMA_ISR_GIF                         0
1142
1143 #define STM_DMA_IFCR(index)             ((index) << 2)
1144 #define STM_DMA_IFCR_MASK                       0xf
1145 #define STM_DMA_IFCR_CTEIF                      3
1146 #define STM_DMA_IFCR_CHTIF                      2
1147 #define STM_DMA_IFCR_CTCIF                      1
1148 #define STM_DMA_IFCR_CGIF                       0
1149
1150 #define STM_DMA_CCR_MEM2MEM             (14)
1151
1152 #define STM_DMA_CCR_PL                  (12)
1153 #define  STM_DMA_CCR_PL_LOW                     (0)
1154 #define  STM_DMA_CCR_PL_MEDIUM                  (1)
1155 #define  STM_DMA_CCR_PL_HIGH                    (2)
1156 #define  STM_DMA_CCR_PL_VERY_HIGH               (3)
1157 #define  STM_DMA_CCR_PL_MASK                    (3)
1158
1159 #define STM_DMA_CCR_MSIZE               (10)
1160 #define  STM_DMA_CCR_MSIZE_8                    (0)
1161 #define  STM_DMA_CCR_MSIZE_16                   (1)
1162 #define  STM_DMA_CCR_MSIZE_32                   (2)
1163 #define  STM_DMA_CCR_MSIZE_MASK                 (3)
1164
1165 #define STM_DMA_CCR_PSIZE               (8)
1166 #define  STM_DMA_CCR_PSIZE_8                    (0)
1167 #define  STM_DMA_CCR_PSIZE_16                   (1)
1168 #define  STM_DMA_CCR_PSIZE_32                   (2)
1169 #define  STM_DMA_CCR_PSIZE_MASK                 (3)
1170
1171 #define STM_DMA_CCR_MINC                (7)
1172 #define STM_DMA_CCR_PINC                (6)
1173 #define STM_DMA_CCR_CIRC                (5)
1174 #define STM_DMA_CCR_DIR                 (4)
1175 #define  STM_DMA_CCR_DIR_PER_TO_MEM             0
1176 #define  STM_DMA_CCR_DIR_MEM_TO_PER             1
1177 #define STM_DMA_CCR_TEIE                (3)
1178 #define STM_DMA_CCR_HTIE                (2)
1179 #define STM_DMA_CCR_TCIE                (1)
1180 #define STM_DMA_CCR_EN                  (0)
1181
1182 #define STM_DMA_CHANNEL_ADC1            1
1183 #define STM_DMA_CHANNEL_SPI1_RX         2
1184 #define STM_DMA_CHANNEL_SPI1_TX         3
1185 #define STM_DMA_CHANNEL_SPI2_RX         4
1186 #define STM_DMA_CHANNEL_SPI2_TX         5
1187 #define STM_DMA_CHANNEL_USART3_TX       2
1188 #define STM_DMA_CHANNEL_USART3_RX       3
1189 #define STM_DMA_CHANNEL_USART1_TX       4
1190 #define STM_DMA_CHANNEL_USART1_RX       5
1191 #define STM_DMA_CHANNEL_USART2_RX       6
1192 #define STM_DMA_CHANNEL_USART2_TX       7
1193 #define STM_DMA_CHANNEL_I2C2_TX         4
1194 #define STM_DMA_CHANNEL_I2C2_RX         5
1195 #define STM_DMA_CHANNEL_I2C1_TX         6
1196 #define STM_DMA_CHANNEL_I2C1_RX         7
1197 #define STM_DMA_CHANNEL_TIM2_CH3        1
1198 #define STM_DMA_CHANNEL_TIM2_UP         2
1199 #define STM_DMA_CHANNEL_TIM2_CH1        5
1200 #define STM_DMA_CHANNEL_TIM2_CH2        7
1201 #define STM_DMA_CHANNEL_TIM2_CH4        7
1202 #define STM_DMA_CHANNEL_TIM3_CH3        2
1203 #define STM_DMA_CHANNEL_TIM3_CH4        3
1204 #define STM_DMA_CHANNEL_TIM3_UP         3
1205 #define STM_DMA_CHANNEL_TIM3_CH1        6
1206 #define STM_DMA_CHANNEL_TIM3_TRIG       6
1207 #define STM_DMA_CHANNEL_TIM4_CH1        1
1208 #define STM_DMA_CHANNEL_TIM4_CH2        4
1209 #define STM_DMA_CHANNEL_TIM4_CH3        5
1210 #define STM_DMA_CHANNEL_TIM4_UP         7
1211 #define STM_DMA_CHANNEL_TIM6_UP_DA      2
1212 #define STM_DMA_CHANNEL_C_CHANNEL1      2
1213 #define STM_DMA_CHANNEL_TIM7_UP_DA      3
1214 #define STM_DMA_CHANNEL_C_CHANNEL2      3
1215
1216 /*
1217  * Only spi channel 1 and 2 can use DMA
1218  */
1219 #define STM_NUM_SPI     2
1220
1221 struct stm_spi {
1222         vuint32_t       cr1;
1223         vuint32_t       cr2;
1224         vuint32_t       sr;
1225         vuint32_t       dr;
1226         vuint32_t       crcpr;
1227         vuint32_t       rxcrcr;
1228         vuint32_t       txcrcr;
1229 };
1230
1231 extern struct stm_spi stm_spi1, stm_spi2, stm_spi3;
1232
1233 /* SPI channels go from 1 to 3, instead of 0 to 2 (sigh)
1234  */
1235
1236 #define STM_SPI_INDEX(channel)          ((channel) - 1)
1237
1238 #define STM_SPI_CR1_BIDIMODE            15
1239 #define STM_SPI_CR1_BIDIOE              14
1240 #define STM_SPI_CR1_CRCEN               13
1241 #define STM_SPI_CR1_CRCNEXT             12
1242 #define STM_SPI_CR1_DFF                 11
1243 #define STM_SPI_CR1_RXONLY              10
1244 #define STM_SPI_CR1_SSM                 9
1245 #define STM_SPI_CR1_SSI                 8
1246 #define STM_SPI_CR1_LSBFIRST            7
1247 #define STM_SPI_CR1_SPE                 6
1248 #define STM_SPI_CR1_BR                  3
1249 #define  STM_SPI_CR1_BR_PCLK_2                  0
1250 #define  STM_SPI_CR1_BR_PCLK_4                  1
1251 #define  STM_SPI_CR1_BR_PCLK_8                  2
1252 #define  STM_SPI_CR1_BR_PCLK_16                 3
1253 #define  STM_SPI_CR1_BR_PCLK_32                 4
1254 #define  STM_SPI_CR1_BR_PCLK_64                 5
1255 #define  STM_SPI_CR1_BR_PCLK_128                6
1256 #define  STM_SPI_CR1_BR_PCLK_256                7
1257 #define  STM_SPI_CR1_BR_MASK                    7
1258
1259 #define STM_SPI_CR1_MSTR                2
1260 #define STM_SPI_CR1_CPOL                1
1261 #define STM_SPI_CR1_CPHA                0
1262
1263 #define STM_SPI_CR2_TXEIE       7
1264 #define STM_SPI_CR2_RXNEIE      6
1265 #define STM_SPI_CR2_ERRIE       5
1266 #define STM_SPI_CR2_SSOE        2
1267 #define STM_SPI_CR2_TXDMAEN     1
1268 #define STM_SPI_CR2_RXDMAEN     0
1269
1270 #define STM_SPI_SR_BSY          7
1271 #define STM_SPI_SR_OVR          6
1272 #define STM_SPI_SR_MODF         5
1273 #define STM_SPI_SR_CRCERR       4
1274 #define STM_SPI_SR_TXE          1
1275 #define STM_SPI_SR_RXNE         0
1276
1277 struct stm_adc {
1278         vuint32_t       sr;
1279         vuint32_t       cr1;
1280         vuint32_t       cr2;
1281         vuint32_t       smpr1;
1282         vuint32_t       smpr2;
1283         vuint32_t       smpr3;
1284         vuint32_t       jofr1;
1285         vuint32_t       jofr2;
1286         vuint32_t       jofr3;
1287         vuint32_t       jofr4;
1288         vuint32_t       htr;
1289         vuint32_t       ltr;
1290         vuint32_t       sqr1;
1291         vuint32_t       sqr2;
1292         vuint32_t       sqr3;
1293         vuint32_t       sqr4;
1294         vuint32_t       sqr5;
1295         vuint32_t       jsqr;
1296         vuint32_t       jdr1;
1297         vuint32_t       jdr2;
1298         vuint32_t       jdr3;
1299         vuint32_t       jdr4;
1300         vuint32_t       dr;
1301         uint8_t         reserved[0x300 - 0x5c];
1302         vuint32_t       csr;
1303         vuint32_t       ccr;
1304 };
1305
1306 extern struct stm_adc stm_adc;
1307
1308 #define STM_ADC_SR_JCNR         9
1309 #define STM_ADC_SR_RCNR         8
1310 #define STM_ADC_SR_ADONS        6
1311 #define STM_ADC_SR_OVR          5
1312 #define STM_ADC_SR_STRT         4
1313 #define STM_ADC_SR_JSTRT        3
1314 #define STM_ADC_SR_JEOC         2
1315 #define STM_ADC_SR_EOC          1
1316 #define STM_ADC_SR_AWD          0
1317
1318 #define STM_ADC_CR1_OVRIE       26
1319 #define STM_ADC_CR1_RES         24
1320 #define  STM_ADC_CR1_RES_12             0
1321 #define  STM_ADC_CR1_RES_10             1
1322 #define  STM_ADC_CR1_RES_8              2
1323 #define  STM_ADC_CR1_RES_6              3
1324 #define  STM_ADC_CR1_RES_MASK           3
1325 #define STM_ADC_CR1_AWDEN       23
1326 #define STM_ADC_CR1_JAWDEN      22
1327 #define STM_ADC_CR1_PDI         17
1328 #define STM_ADC_CR1_PDD         16
1329 #define STM_ADC_CR1_DISCNUM     13
1330 #define  STM_ADC_CR1_DISCNUM_1          0
1331 #define  STM_ADC_CR1_DISCNUM_2          1
1332 #define  STM_ADC_CR1_DISCNUM_3          2
1333 #define  STM_ADC_CR1_DISCNUM_4          3
1334 #define  STM_ADC_CR1_DISCNUM_5          4
1335 #define  STM_ADC_CR1_DISCNUM_6          5
1336 #define  STM_ADC_CR1_DISCNUM_7          6
1337 #define  STM_ADC_CR1_DISCNUM_8          7
1338 #define  STM_ADC_CR1_DISCNUM_MASK       7
1339 #define STM_ADC_CR1_JDISCEN     12
1340 #define STM_ADC_CR1_DISCEN      11
1341 #define STM_ADC_CR1_JAUTO       10
1342 #define STM_ADC_CR1_AWDSGL      9
1343 #define STM_ADC_CR1_SCAN        8
1344 #define STM_ADC_CR1_JEOCIE      7
1345 #define STM_ADC_CR1_AWDIE       6
1346 #define STM_ADC_CR1_EOCIE       5
1347 #define STM_ADC_CR1_AWDCH       0
1348 #define  STM_ADC_CR1_AWDCH_MASK         0x1f
1349
1350 #define STM_ADC_CR2_SWSTART     30
1351 #define STM_ADC_CR2_EXTEN       28
1352 #define  STM_ADC_CR2_EXTEN_DISABLE      0
1353 #define  STM_ADC_CR2_EXTEN_RISING       1
1354 #define  STM_ADC_CR2_EXTEN_FALLING      2
1355 #define  STM_ADC_CR2_EXTEN_BOTH         3
1356 #define  STM_ADC_CR2_EXTEN_MASK         3
1357 #define STM_ADC_CR2_EXTSEL      24
1358 #define  STM_ADC_CR2_EXTSEL_TIM9_CC2    0
1359 #define  STM_ADC_CR2_EXTSEL_TIM9_TRGO   1
1360 #define  STM_ADC_CR2_EXTSEL_TIM2_CC3    2
1361 #define  STM_ADC_CR2_EXTSEL_TIM2_CC2    3
1362 #define  STM_ADC_CR2_EXTSEL_TIM3_TRGO   4
1363 #define  STM_ADC_CR2_EXTSEL_TIM4_CC4    5
1364 #define  STM_ADC_CR2_EXTSEL_TIM2_TRGO   6
1365 #define  STM_ADC_CR2_EXTSEL_TIM3_CC1    7
1366 #define  STM_ADC_CR2_EXTSEL_TIM3_CC3    8
1367 #define  STM_ADC_CR2_EXTSEL_TIM4_TRGO   9
1368 #define  STM_ADC_CR2_EXTSEL_TIM6_TRGO   10
1369 #define  STM_ADC_CR2_EXTSEL_EXTI_11     15
1370 #define  STM_ADC_CR2_EXTSEL_MASK        15
1371 #define STM_ADC_CR2_JWSTART     22
1372 #define STM_ADC_CR2_JEXTEN      20
1373 #define  STM_ADC_CR2_JEXTEN_DISABLE     0
1374 #define  STM_ADC_CR2_JEXTEN_RISING      1
1375 #define  STM_ADC_CR2_JEXTEN_FALLING     2
1376 #define  STM_ADC_CR2_JEXTEN_BOTH        3
1377 #define  STM_ADC_CR2_JEXTEN_MASK        3
1378 #define STM_ADC_CR2_JEXTSEL     16
1379 #define  STM_ADC_CR2_JEXTSEL_TIM9_CC1   0
1380 #define  STM_ADC_CR2_JEXTSEL_TIM9_TRGO  1
1381 #define  STM_ADC_CR2_JEXTSEL_TIM2_TRGO  2
1382 #define  STM_ADC_CR2_JEXTSEL_TIM2_CC1   3
1383 #define  STM_ADC_CR2_JEXTSEL_TIM3_CC4   4
1384 #define  STM_ADC_CR2_JEXTSEL_TIM4_TRGO  5
1385 #define  STM_ADC_CR2_JEXTSEL_TIM4_CC1   6
1386 #define  STM_ADC_CR2_JEXTSEL_TIM4_CC2   7
1387 #define  STM_ADC_CR2_JEXTSEL_TIM4_CC3   8
1388 #define  STM_ADC_CR2_JEXTSEL_TIM10_CC1  9
1389 #define  STM_ADC_CR2_JEXTSEL_TIM7_TRGO  10
1390 #define  STM_ADC_CR2_JEXTSEL_EXTI_15    15
1391 #define  STM_ADC_CR2_JEXTSEL_MASK       15
1392 #define STM_ADC_CR2_ALIGN       11
1393 #define STM_ADC_CR2_EOCS        10
1394 #define STM_ADC_CR2_DDS         9
1395 #define STM_ADC_CR2_DMA         8
1396 #define STM_ADC_CR2_DELS        4
1397 #define  STM_ADC_CR2_DELS_NONE          0
1398 #define  STM_ADC_CR2_DELS_UNTIL_READ    1
1399 #define  STM_ADC_CR2_DELS_7             2
1400 #define  STM_ADC_CR2_DELS_15            3
1401 #define  STM_ADC_CR2_DELS_31            4
1402 #define  STM_ADC_CR2_DELS_63            5
1403 #define  STM_ADC_CR2_DELS_127           6
1404 #define  STM_ADC_CR2_DELS_255           7
1405 #define  STM_ADC_CR2_DELS_MASK          7
1406 #define STM_ADC_CR2_CONT        1
1407 #define STM_ADC_CR2_ADON        0
1408
1409 #define STM_ADC_CCR_TSVREFE     23
1410 #define STM_ADC_CCR_ADCPRE      16
1411 #define  STM_ADC_CCR_ADCPRE_HSI_1       0
1412 #define  STM_ADC_CCR_ADCPRE_HSI_2       1
1413 #define  STM_ADC_CCR_ADCPRE_HSI_4       2
1414 #define  STM_ADC_CCR_ADCPRE_MASK        3
1415
1416 struct stm_temp_cal {
1417         uint16_t        vref;
1418         uint16_t        ts_cal_cold;
1419         uint16_t        reserved;
1420         uint16_t        ts_cal_hot;
1421 };
1422
1423 extern struct stm_temp_cal      stm_temp_cal;
1424
1425 #define stm_temp_cal_cold       25
1426 #define stm_temp_cal_hot        110
1427
1428 #define STM_NUM_I2C     2
1429
1430 #define STM_I2C_INDEX(channel)  ((channel) - 1)
1431
1432 struct stm_i2c {
1433         vuint32_t       cr1;
1434         vuint32_t       cr2;
1435         vuint32_t       oar1;
1436         vuint32_t       oar2;
1437         vuint32_t       dr;
1438         vuint32_t       sr1;
1439         vuint32_t       sr2;
1440         vuint32_t       ccr;
1441         vuint32_t       trise;
1442 };
1443
1444 extern struct stm_i2c stm_i2c1, stm_i2c2;
1445
1446 #define STM_I2C_CR1_SWRST       15
1447 #define STM_I2C_CR1_ALERT       13
1448 #define STM_I2C_CR1_PEC         12
1449 #define STM_I2C_CR1_POS         11
1450 #define STM_I2C_CR1_ACK         10
1451 #define STM_I2C_CR1_STOP        9
1452 #define STM_I2C_CR1_START       8
1453 #define STM_I2C_CR1_NOSTRETCH   7
1454 #define STM_I2C_CR1_ENGC        6
1455 #define STM_I2C_CR1_ENPEC       5
1456 #define STM_I2C_CR1_ENARP       4
1457 #define STM_I2C_CR1_SMBTYPE     3
1458 #define STM_I2C_CR1_SMBUS       1
1459 #define STM_I2C_CR1_PE          0
1460
1461 #define STM_I2C_CR2_LAST        12
1462 #define STM_I2C_CR2_DMAEN       11
1463 #define STM_I2C_CR2_ITBUFEN     10
1464 #define STM_I2C_CR2_ITEVTEN     9
1465 #define STM_I2C_CR2_ITERREN     8
1466 #define STM_I2C_CR2_FREQ        0
1467 #define  STM_I2C_CR2_FREQ_2_MHZ         2
1468 #define  STM_I2C_CR2_FREQ_4_MHZ         4
1469 #define  STM_I2C_CR2_FREQ_8_MHZ         8
1470 #define  STM_I2C_CR2_FREQ_16_MHZ        16
1471 #define  STM_I2C_CR2_FREQ_32_MHZ        32
1472 #define  STM_I2C_CR2_FREQ_MASK          0x3f
1473
1474 #define STM_I2C_SR1_SMBALERT    15
1475 #define STM_I2C_SR1_TIMEOUT     14
1476 #define STM_I2C_SR1_PECERR      12
1477 #define STM_I2C_SR1_OVR         11
1478 #define STM_I2C_SR1_AF          10
1479 #define STM_I2C_SR1_ARLO        9
1480 #define STM_I2C_SR1_BERR        8
1481 #define STM_I2C_SR1_TXE         7
1482 #define STM_I2C_SR1_RXNE        6
1483 #define STM_I2C_SR1_STOPF       4
1484 #define STM_I2C_SR1_ADD10       3
1485 #define STM_I2C_SR1_BTF         2
1486 #define STM_I2C_SR1_ADDR        1
1487 #define STM_I2C_SR1_SB          0
1488
1489 #define STM_I2C_SR2_PEC         8
1490 #define  STM_I2C_SR2_PEC_MASK   0xff00
1491 #define STM_I2C_SR2_DUALF       7
1492 #define STM_I2C_SR2_SMBHOST     6
1493 #define STM_I2C_SR2_SMBDEFAULT  5
1494 #define STM_I2C_SR2_GENCALL     4
1495 #define STM_I2C_SR2_TRA         2
1496 #define STM_I2C_SR2_BUSY        1
1497 #define STM_I2C_SR2_MSL         0
1498
1499 #define STM_I2C_CCR_FS          15
1500 #define STM_I2C_CCR_DUTY        14
1501 #define STM_I2C_CCR_CCR         0
1502 #define  STM_I2C_CCR_MASK       0x7ff
1503
1504 struct stm_tim234 {
1505         vuint32_t       cr1;
1506         vuint32_t       cr2;
1507         vuint32_t       smcr;
1508         vuint32_t       dier;
1509
1510         vuint32_t       sr;
1511         vuint32_t       egr;
1512         vuint32_t       ccmr1;
1513         vuint32_t       ccmr2;
1514
1515         vuint32_t       ccer;
1516         vuint32_t       cnt;
1517         vuint32_t       psc;
1518         vuint32_t       arr;
1519
1520         uint32_t        reserved_30;
1521         vuint32_t       ccr1;
1522         vuint32_t       ccr2;
1523         vuint32_t       ccr3;
1524
1525         vuint32_t       ccr4;
1526         uint32_t        reserved_44;
1527         vuint32_t       dcr;
1528         vuint32_t       dmar;
1529
1530         uint32_t        reserved_50;
1531 };
1532
1533 extern struct stm_tim234 stm_tim2, stm_tim3, stm_tim4;
1534
1535 #define STM_TIM234_CR1_CKD      8
1536 #define  STM_TIM234_CR1_CKD_1           0
1537 #define  STM_TIM234_CR1_CKD_2           1
1538 #define  STM_TIM234_CR1_CKD_4           2
1539 #define  STM_TIM234_CR1_CKD_MASK        3
1540 #define STM_TIM234_CR1_ARPE     7
1541 #define STM_TIM234_CR1_CMS      5
1542 #define  STM_TIM234_CR1_CMS_EDGE        0
1543 #define  STM_TIM234_CR1_CMS_CENTER_1    1
1544 #define  STM_TIM234_CR1_CMS_CENTER_2    2
1545 #define  STM_TIM234_CR1_CMS_CENTER_3    3
1546 #define  STM_TIM234_CR1_CMS_MASK        3
1547 #define STM_TIM234_CR1_DIR      4
1548 #define  STM_TIM234_CR1_DIR_UP          0
1549 #define  STM_TIM234_CR1_DIR_DOWN        1
1550 #define STM_TIM234_CR1_OPM      3
1551 #define STM_TIM234_CR1_URS      2
1552 #define STM_TIM234_CR1_UDIS     1
1553 #define STM_TIM234_CR1_CEN      0
1554
1555 #define STM_TIM234_CR2_TI1S     7
1556 #define STM_TIM234_CR2_MMS      4
1557 #define  STM_TIM234_CR2_MMS_RESET               0
1558 #define  STM_TIM234_CR2_MMS_ENABLE              1
1559 #define  STM_TIM234_CR2_MMS_UPDATE              2
1560 #define  STM_TIM234_CR2_MMS_COMPARE_PULSE       3
1561 #define  STM_TIM234_CR2_MMS_COMPARE_OC1REF      4
1562 #define  STM_TIM234_CR2_MMS_COMPARE_OC2REF      5
1563 #define  STM_TIM234_CR2_MMS_COMPARE_OC3REF      6
1564 #define  STM_TIM234_CR2_MMS_COMPARE_OC4REF      7
1565 #define  STM_TIM234_CR2_MMS_MASK                7
1566 #define STM_TIM234_CR2_CCDS     3
1567
1568 #define STM_TIM234_SMCR_ETP     15
1569 #define STM_TIM234_SMCR_ECE     14
1570 #define STM_TIM234_SMCR_ETPS    12
1571 #define  STM_TIM234_SMCR_ETPS_OFF               0
1572 #define  STM_TIM234_SMCR_ETPS_DIV_2             1
1573 #define  STM_TIM234_SMCR_ETPS_DIV_4             2
1574 #define  STM_TIM234_SMCR_ETPS_DIV_8             3
1575 #define  STM_TIM234_SMCR_ETPS_MASK              3
1576 #define STM_TIM234_SMCR_ETF     8
1577 #define  STM_TIM234_SMCR_ETF_NONE               0
1578 #define  STM_TIM234_SMCR_ETF_INT_N_2            1
1579 #define  STM_TIM234_SMCR_ETF_INT_N_4            2
1580 #define  STM_TIM234_SMCR_ETF_INT_N_8            3
1581 #define  STM_TIM234_SMCR_ETF_DTS_2_N_6          4
1582 #define  STM_TIM234_SMCR_ETF_DTS_2_N_8          5
1583 #define  STM_TIM234_SMCR_ETF_DTS_4_N_6          6
1584 #define  STM_TIM234_SMCR_ETF_DTS_4_N_8          7
1585 #define  STM_TIM234_SMCR_ETF_DTS_8_N_6          8
1586 #define  STM_TIM234_SMCR_ETF_DTS_8_N_8          9
1587 #define  STM_TIM234_SMCR_ETF_DTS_16_N_5         10
1588 #define  STM_TIM234_SMCR_ETF_DTS_16_N_6         11
1589 #define  STM_TIM234_SMCR_ETF_DTS_16_N_8         12
1590 #define  STM_TIM234_SMCR_ETF_DTS_32_N_5         13
1591 #define  STM_TIM234_SMCR_ETF_DTS_32_N_6         14
1592 #define  STM_TIM234_SMCR_ETF_DTS_32_N_8         15
1593 #define  STM_TIM234_SMCR_ETF_MASK               15
1594 #define STM_TIM234_SMCR_MSM     7
1595 #define STM_TIM234_SMCR_TS      4
1596 #define  STM_TIM234_SMCR_TS_ITR0                0
1597 #define  STM_TIM234_SMCR_TS_ITR1                1
1598 #define  STM_TIM234_SMCR_TS_ITR2                2
1599 #define  STM_TIM234_SMCR_TS_ITR3                3
1600 #define  STM_TIM234_SMCR_TS_TI1F_ED             4
1601 #define  STM_TIM234_SMCR_TS_TI1FP1              5
1602 #define  STM_TIM234_SMCR_TS_TI2FP2              6
1603 #define  STM_TIM234_SMCR_TS_ETRF                7
1604 #define  STM_TIM234_SMCR_TS_MASK                7
1605 #define STM_TIM234_SMCR_OCCS    3
1606 #define STM_TIM234_SMCR_SMS     0
1607 #define  STM_TIM234_SMCR_SMS_DISABLE            0
1608 #define  STM_TIM234_SMCR_SMS_ENCODER_MODE_1     1
1609 #define  STM_TIM234_SMCR_SMS_ENCODER_MODE_2     2
1610 #define  STM_TIM234_SMCR_SMS_ENCODER_MODE_3     3
1611 #define  STM_TIM234_SMCR_SMS_RESET_MODE         4
1612 #define  STM_TIM234_SMCR_SMS_GATED_MODE         5
1613 #define  STM_TIM234_SMCR_SMS_TRIGGER_MODE       6
1614 #define  STM_TIM234_SMCR_SMS_EXTERNAL_CLOCK     7
1615 #define  STM_TIM234_SMCR_SMS_MASK               7
1616
1617 #define STM_TIM234_SR_CC4OF     12
1618 #define STM_TIM234_SR_CC3OF     11
1619 #define STM_TIM234_SR_CC2OF     10
1620 #define STM_TIM234_SR_CC1OF     9
1621 #define STM_TIM234_SR_TIF       6
1622 #define STM_TIM234_SR_CC4IF     4
1623 #define STM_TIM234_SR_CC3IF     3
1624 #define STM_TIM234_SR_CC2IF     2
1625 #define STM_TIM234_SR_CC1IF     1
1626 #define STM_TIM234_SR_UIF       0
1627
1628 #define STM_TIM234_EGR_TG       6
1629 #define STM_TIM234_EGR_CC4G     4
1630 #define STM_TIM234_EGR_CC3G     3
1631 #define STM_TIM234_EGR_CC2G     2
1632 #define STM_TIM234_EGR_CC1G     1
1633 #define STM_TIM234_EGR_UG       0
1634
1635 #define STM_TIM234_CCMR1_OC2CE  15
1636 #define STM_TIM234_CCMR1_OC2M   12
1637 #define  STM_TIM234_CCMR1_OC2M_FROZEN                   0
1638 #define  STM_TIM234_CCMR1_OC2M_SET_HIGH_ON_MATCH        1
1639 #define  STM_TIM234_CCMR1_OC2M_SET_LOW_ON_MATCH         2
1640 #define  STM_TIM234_CCMR1_OC2M_TOGGLE                   3
1641 #define  STM_TIM234_CCMR1_OC2M_FORCE_LOW                4
1642 #define  STM_TIM234_CCMR1_OC2M_FORCE_HIGH               5
1643 #define  STM_TIM234_CCMR1_OC2M_PWM_MODE_1               6
1644 #define  STM_TIM234_CCMR1_OC2M_PWM_MODE_2               7
1645 #define  STM_TIM234_CCMR1_OC2M_MASK                     7
1646 #define STM_TIM234_CCMR1_OC2PE  11
1647 #define STM_TIM234_CCMR1_OC2FE  10
1648 #define STM_TIM234_CCMR1_CC2S   8
1649 #define  STM_TIM234_CCMR1_CC2S_OUTPUT                   0
1650 #define  STM_TIM234_CCMR1_CC2S_INPUT_TI2                1
1651 #define  STM_TIM234_CCMR1_CC2S_INPUT_TI1                2
1652 #define  STM_TIM234_CCMR1_CC2S_INPUT_TRC                3
1653 #define  STM_TIM234_CCMR1_CC2S_MASK                     3
1654
1655 #define STM_TIM234_CCMR1_OC1CE  7
1656 #define STM_TIM234_CCMR1_OC1M   4
1657 #define  STM_TIM234_CCMR1_OC1M_FROZEN                   0
1658 #define  STM_TIM234_CCMR1_OC1M_SET_HIGH_ON_MATCH        1
1659 #define  STM_TIM234_CCMR1_OC1M_SET_LOW_ON_MATCH         2
1660 #define  STM_TIM234_CCMR1_OC1M_TOGGLE                   3
1661 #define  STM_TIM234_CCMR1_OC1M_FORCE_LOW                4
1662 #define  STM_TIM234_CCMR1_OC1M_FORCE_HIGH               5
1663 #define  STM_TIM234_CCMR1_OC1M_PWM_MODE_1               6
1664 #define  STM_TIM234_CCMR1_OC1M_PWM_MODE_2               7
1665 #define  STM_TIM234_CCMR1_OC1M_MASK                     7
1666 #define STM_TIM234_CCMR1_OC1PE  11
1667 #define STM_TIM234_CCMR1_OC1FE  2
1668 #define STM_TIM234_CCMR1_CC1S   0
1669 #define  STM_TIM234_CCMR1_CC1S_OUTPUT                   0
1670 #define  STM_TIM234_CCMR1_CC1S_INPUT_TI1                1
1671 #define  STM_TIM234_CCMR1_CC1S_INPUT_TI2                2
1672 #define  STM_TIM234_CCMR1_CC1S_INPUT_TRC                3
1673 #define  STM_TIM234_CCMR1_CC1S_MASK                     3
1674
1675 #define STM_TIM234_CCMR2_OC2CE  15
1676 #define STM_TIM234_CCMR2_OC4M   12
1677 #define  STM_TIM234_CCMR2_OC4M_FROZEN                   0
1678 #define  STM_TIM234_CCMR2_OC4M_SET_HIGH_ON_MATCH        1
1679 #define  STM_TIM234_CCMR2_OC4M_SET_LOW_ON_MATCH         2
1680 #define  STM_TIM234_CCMR2_OC4M_TOGGLE                   3
1681 #define  STM_TIM234_CCMR2_OC4M_FORCE_LOW                4
1682 #define  STM_TIM234_CCMR2_OC4M_FORCE_HIGH               5
1683 #define  STM_TIM234_CCMR2_OC4M_PWM_MODE_1               6
1684 #define  STM_TIM234_CCMR2_OC4M_PWM_MODE_2               7
1685 #define  STM_TIM234_CCMR2_OC4M_MASK                     7
1686 #define STM_TIM234_CCMR2_OC4PE  11
1687 #define STM_TIM234_CCMR2_OC4FE  10
1688 #define STM_TIM234_CCMR2_CC4S   8
1689 #define  STM_TIM234_CCMR2_CC4S_OUTPUT                   0
1690 #define  STM_TIM234_CCMR2_CC4S_INPUT_TI4                1
1691 #define  STM_TIM234_CCMR2_CC4S_INPUT_TI3                2
1692 #define  STM_TIM234_CCMR2_CC4S_INPUT_TRC                3
1693 #define  STM_TIM234_CCMR2_CC4S_MASK                     3
1694
1695 #define STM_TIM234_CCMR2_OC3CE  7
1696 #define STM_TIM234_CCMR2_OC3M   4
1697 #define  STM_TIM234_CCMR2_OC3M_FROZEN                   0
1698 #define  STM_TIM234_CCMR2_OC3M_SET_HIGH_ON_MATCH        1
1699 #define  STM_TIM234_CCMR2_OC3M_SET_LOW_ON_MATCH         2
1700 #define  STM_TIM234_CCMR2_OC3M_TOGGLE                   3
1701 #define  STM_TIM234_CCMR2_OC3M_FORCE_LOW                4
1702 #define  STM_TIM234_CCMR2_OC3M_FORCE_HIGH               5
1703 #define  STM_TIM234_CCMR2_OC3M_PWM_MODE_1               6
1704 #define  STM_TIM234_CCMR2_OC3M_PWM_MODE_2               7
1705 #define  STM_TIM234_CCMR2_OC3M_MASK                     7
1706 #define STM_TIM234_CCMR2_OC3PE  11
1707 #define STM_TIM234_CCMR2_OC3FE  2
1708 #define STM_TIM234_CCMR2_CC3S   0
1709 #define  STM_TIM234_CCMR2_CC3S_OUTPUT                   0
1710 #define  STM_TIM234_CCMR2_CC3S_INPUT_TI3                1
1711 #define  STM_TIM234_CCMR2_CC3S_INPUT_TI4                2
1712 #define  STM_TIM234_CCMR2_CC3S_INPUT_TRC                3
1713 #define  STM_TIM234_CCMR2_CC3S_MASK                     3
1714
1715 #define STM_TIM234_CCER_CC4NP   15
1716 #define STM_TIM234_CCER_CC4P    13
1717 #define STM_TIM234_CCER_CC4E    12
1718 #define STM_TIM234_CCER_CC3NP   11
1719 #define STM_TIM234_CCER_CC3P    9
1720 #define STM_TIM234_CCER_CC3E    8
1721 #define STM_TIM234_CCER_CC2NP   7
1722 #define STM_TIM234_CCER_CC2P    5
1723 #define STM_TIM234_CCER_CC2E    4
1724 #define STM_TIM234_CCER_CC1NP   3
1725 #define STM_TIM234_CCER_CC1P    1
1726 #define STM_TIM234_CCER_CC1E    0
1727
1728 struct stm_usb {
1729         vuint32_t       epr[8];
1730         uint8_t         reserved_20[0x40 - 0x20];
1731         vuint32_t       cntr;
1732         vuint32_t       istr;
1733         vuint32_t       fnr;
1734         vuint32_t       daddr;
1735         vuint32_t       btable;
1736 };
1737
1738 #define STM_USB_EPR_CTR_RX      15
1739 #define  STM_USB_EPR_CTR_RX_WRITE_INVARIANT             1
1740 #define STM_USB_EPR_DTOG_RX     14
1741 #define STM_USB_EPR_DTOG_RX_WRITE_INVARIANT             0
1742 #define STM_USB_EPR_STAT_RX     12
1743 #define  STM_USB_EPR_STAT_RX_DISABLED                   0
1744 #define  STM_USB_EPR_STAT_RX_STALL                      1
1745 #define  STM_USB_EPR_STAT_RX_NAK                        2
1746 #define  STM_USB_EPR_STAT_RX_VALID                      3
1747 #define  STM_USB_EPR_STAT_RX_MASK                       3
1748 #define  STM_USB_EPR_STAT_RX_WRITE_INVARIANT            0
1749 #define STM_USB_EPR_SETUP       11
1750 #define STM_USB_EPR_EP_TYPE     9
1751 #define  STM_USB_EPR_EP_TYPE_BULK                       0
1752 #define  STM_USB_EPR_EP_TYPE_CONTROL                    1
1753 #define  STM_USB_EPR_EP_TYPE_ISO                        2
1754 #define  STM_USB_EPR_EP_TYPE_INTERRUPT                  3
1755 #define  STM_USB_EPR_EP_TYPE_MASK                       3
1756 #define STM_USB_EPR_EP_KIND     8
1757 #define  STM_USB_EPR_EP_KIND_DBL_BUF                    1       /* Bulk */
1758 #define  STM_USB_EPR_EP_KIND_STATUS_OUT                 1       /* Control */
1759 #define STM_USB_EPR_CTR_TX      7
1760 #define  STM_USB_CTR_TX_WRITE_INVARIANT                 1
1761 #define STM_USB_EPR_DTOG_TX     6
1762 #define  STM_USB_EPR_DTOG_TX_WRITE_INVARIANT            0
1763 #define STM_USB_EPR_STAT_TX     4
1764 #define  STM_USB_EPR_STAT_TX_DISABLED                   0
1765 #define  STM_USB_EPR_STAT_TX_STALL                      1
1766 #define  STM_USB_EPR_STAT_TX_NAK                        2
1767 #define  STM_USB_EPR_STAT_TX_VALID                      3
1768 #define  STM_USB_EPR_STAT_TX_WRITE_INVARIANT            0
1769 #define  STM_USB_EPR_STAT_TX_MASK                       3
1770 #define STM_USB_EPR_EA          0
1771 #define  STM_USB_EPR_EA_MASK                            0xf
1772
1773 #define STM_USB_CNTR_CTRM       15
1774 #define STM_USB_CNTR_PMAOVRM    14
1775 #define STM_USB_CNTR_ERRM       13
1776 #define STM_USB_CNTR_WKUPM      12
1777 #define STM_USB_CNTR_SUSPM      11
1778 #define STM_USB_CNTR_RESETM     10
1779 #define STM_USB_CNTR_SOFM       9
1780 #define STM_USB_CNTR_ESOFM      8
1781 #define STM_USB_CNTR_RESUME     4
1782 #define STM_USB_CNTR_FSUSP      3
1783 #define STM_USB_CNTR_LP_MODE    2
1784 #define STM_USB_CNTR_PDWN       1
1785 #define STM_USB_CNTR_FRES       0
1786
1787 #define STM_USB_ISTR_CTR        15
1788 #define STM_USB_ISTR_PMAOVR     14
1789 #define STM_USB_ISTR_ERR        13
1790 #define STM_USB_ISTR_WKUP       12
1791 #define STM_USB_ISTR_SUSP       11
1792 #define STM_USB_ISTR_RESET      10
1793 #define STM_USB_ISTR_SOF        9
1794 #define STM_USB_ISTR_ESOF       8
1795 #define STM_USB_ISTR_DIR        4
1796 #define STM_USB_ISTR_EP_ID      0
1797 #define  STM_USB_ISTR_EP_ID_MASK                0xf
1798
1799 #define STM_USB_FNR_RXDP        15
1800 #define STM_USB_FNR_RXDM        14
1801 #define STM_USB_FNR_LCK         13
1802 #define STM_USB_FNR_LSOF        11
1803 #define  STM_USB_FNR_LSOF_MASK                  0x3
1804 #define STM_USB_FNR_FN          0
1805 #define  STM_USB_FNR_FN_MASK                    0x7ff
1806
1807 #define STM_USB_DADDR_EF        7
1808 #define STM_USB_DADDR_ADD       0
1809 #define  STM_USB_DADDR_ADD_MASK                 0x7f
1810
1811 extern struct stm_usb stm_usb;
1812
1813 union stm_usb_bdt {
1814         struct {
1815                 vuint32_t       addr_tx;
1816                 vuint32_t       count_tx;
1817                 vuint32_t       addr_rx;
1818                 vuint32_t       count_rx;
1819         } single;
1820         struct {
1821                 vuint32_t       addr;
1822                 vuint32_t       count;
1823         } double_tx[2];
1824         struct {
1825                 vuint32_t       addr;
1826                 vuint32_t       count;
1827         } double_rx[2];
1828 };
1829
1830 #define STM_USB_BDT_COUNT_RX_BL_SIZE    15
1831 #define STM_USB_BDT_COUNT_RX_NUM_BLOCK  10
1832 #define  STM_USB_BDT_COUNT_RX_NUM_BLOCK_MASK    0x1f
1833 #define STM_USB_BDT_COUNT_RX_COUNT_RX   0
1834 #define  STM_USB_BDT_COUNT_RX_COUNT_RX_MASK     0x1ff
1835
1836 #define STM_USB_BDT_SIZE        8
1837
1838 extern uint8_t stm_usb_sram[];
1839
1840 struct stm_exti {
1841         vuint32_t       imr;
1842         vuint32_t       emr;
1843         vuint32_t       rtsr;
1844         vuint32_t       ftsr;
1845
1846         vuint32_t       swier;
1847         vuint32_t       pr;
1848 };
1849
1850 extern struct stm_exti stm_exti;
1851
1852 #endif /* _STM32L_H_ */