Add defines for LCD controller and RTC clocking
[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
25 struct stm_gpio {
26         vuint32_t       moder;
27         vuint32_t       otyper;
28         vuint32_t       ospeedr;
29         vuint32_t       pupdr;
30
31         vuint32_t       idr;
32         vuint32_t       odr;
33         vuint32_t       bsrr;
34         vuint32_t       lckr;
35
36         vuint32_t       afrl;
37         vuint32_t       afrh;
38 };
39
40 #define STM_MODER_SHIFT(pin)            ((pin) << 1)
41 #define STM_MODER_MASK                  3
42 #define STM_MODER_INPUT                 0
43 #define STM_MODER_OUTPUT                1
44 #define STM_MODER_ALTERNATE             2
45 #define STM_MODER_ANALOG                3
46
47 static inline void
48 stm_moder_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
49         gpio->moder = ((gpio->moder &
50                         ~(STM_MODER_MASK << STM_MODER_SHIFT(pin))) |
51                        value << STM_MODER_SHIFT(pin));
52 }
53         
54 static inline vuint32_t
55 stm_moder_get(struct stm_gpio *gpio, int pin) {
56         return (gpio->moder >> STM_MODER_SHIFT(pin)) & STM_MODER_MASK;
57 }
58
59 #define STM_OTYPER_SHIFT(pin)           (pin)
60 #define STM_OTYPER_MASK                 1
61 #define STM_OTYPER_PUSH_PULL            0
62 #define STM_OTYPER_OPEN_DRAIN           1
63
64 static inline void
65 stm_otyper_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
66         gpio->otyper = ((gpio->otyper &
67                          ~(STM_OTYPER_MASK << STM_OTYPER_SHIFT(pin))) |
68                         value << STM_OTYPER_SHIFT(pin));
69 }
70         
71 static inline vuint32_t
72 stm_otyper_get(struct stm_gpio *gpio, int pin) {
73         return (gpio->otyper >> STM_OTYPER_SHIFT(pin)) & STM_OTYPER_MASK;
74 }
75
76 #define STM_OSPEEDR_SHIFT(pin)          ((pin) << 1)
77 #define STM_OSPEEDR_MASK                3
78 #define STM_OSPEEDR_400kHz              0
79 #define STM_OSPEEDR_2MHz                1
80 #define STM_OSPEEDR_10MHz               2
81 #define STM_OSPEEDR_40MHz               3
82
83 static inline void
84 stm_ospeedr_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
85         gpio->ospeedr = ((gpio->ospeedr &
86                         ~(STM_OSPEEDR_MASK << STM_OSPEEDR_SHIFT(pin))) |
87                        value << STM_OSPEEDR_SHIFT(pin));
88 }
89         
90 static inline vuint32_t
91 stm_ospeedr_get(struct stm_gpio *gpio, int pin) {
92         return (gpio->ospeedr >> STM_OSPEEDR_SHIFT(pin)) & STM_OSPEEDR_MASK;
93 }
94
95 #define STM_PUPDR_SHIFT(pin)            ((pin) << 1)
96 #define STM_PUPDR_MASK                  3
97 #define STM_PUPDR_NONE                  0
98 #define STM_PUPDR_PULL_UP               1
99 #define STM_PUPDR_PULL_DOWN             2
100 #define STM_PUPDR_RESERVED              3
101
102 static inline void
103 stm_pupdr_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
104         gpio->pupdr = ((gpio->pupdr &
105                         ~(STM_PUPDR_MASK << STM_PUPDR_SHIFT(pin))) |
106                        value << STM_PUPDR_SHIFT(pin));
107 }
108         
109 static inline vuint32_t
110 stm_pupdr_get(struct stm_gpio *gpio, int pin) {
111         return (gpio->pupdr >> STM_PUPDR_SHIFT(pin)) & STM_PUPDR_MASK;
112 }
113
114 #define STM_AFR_SHIFT(pin)              ((pin) << 2)
115 #define STM_AFR_MASK                    0xf
116 #define STM_AFR_NONE                    0
117 #define STM_AFR_AF0                     0x0
118 #define STM_AFR_AF1                     0x1
119 #define STM_AFR_AF2                     0x2
120 #define STM_AFR_AF3                     0x3
121 #define STM_AFR_AF4                     0x4
122 #define STM_AFR_AF5                     0x5
123 #define STM_AFR_AF6                     0x6
124 #define STM_AFR_AF7                     0x7
125 #define STM_AFR_AF8                     0x8
126 #define STM_AFR_AF9                     0x9
127 #define STM_AFR_AF10                    0xa
128 #define STM_AFR_AF11                    0xb
129 #define STM_AFR_AF12                    0xc
130 #define STM_AFR_AF13                    0xd
131 #define STM_AFR_AF14                    0xe
132 #define STM_AFR_AF15                    0xf
133
134 static inline void
135 stm_afr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
136         if (pin < 8)
137                 gpio->afrl = ((gpio->afrl &
138                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
139                               value << STM_AFR_SHIFT(pin));
140         else {
141                 pin -= 8;
142                 gpio->afrh = ((gpio->afrh &
143                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
144                               value << STM_AFR_SHIFT(pin));
145         }
146 }
147         
148 static inline uint32_t
149 stm_afr_get(struct stm_gpio *gpio, int pin) {
150         if (pin < 8)
151                 return (gpio->afrl >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
152         else {
153                 pin -= 8;
154                 return (gpio->afrh >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
155         }
156 }
157
158 extern struct stm_gpio stm_gpioa;
159 extern struct stm_gpio stm_gpiob;
160 extern struct stm_gpio stm_gpioc;
161 extern struct stm_gpio stm_gpiod;
162 extern struct stm_gpio stm_gpioe;
163 extern struct stm_gpio stm_gpioh;
164
165 struct stm_usart {
166         vuint32_t       sr;     /* status register */
167         vuint32_t       dr;     /* data register */
168         vuint32_t       brr;    /* baud rate register */
169         vuint32_t       cr1;    /* control register 1 */
170
171         vuint32_t       cr2;    /* control register 2 */
172         vuint32_t       cr3;    /* control register 3 */
173         vuint32_t       gtpr;   /* guard time and prescaler */
174 };
175
176 extern struct stm_usart stm_usart1;
177 extern struct stm_usart stm_usart2;
178 extern struct stm_usart stm_usart3;
179
180 #define STM_USART_SR_CTS        (9)     /* CTS flag */
181 #define STM_USART_SR_LBD        (8)     /* LIN break detection flag */
182 #define STM_USART_SR_TXE        (7)     /* Transmit data register empty */
183 #define STM_USART_SR_TC         (6)     /* Transmission complete */
184 #define STM_USART_SR_RXNE       (5)     /* Read data register not empty */
185 #define STM_USART_SR_IDLE       (4)     /* IDLE line detected */
186 #define STM_USART_SR_ORE        (3)     /* Overrun error */
187 #define STM_USART_SR_NF         (2)     /* Noise detected flag */
188 #define STM_USART_SR_FE         (1)     /* Framing error */
189 #define STM_USART_SR_PE         (0)     /* Parity error */
190
191 #define STM_USART_CR1_OVER8     (15)    /* Oversampling mode */
192 #define STM_USART_CR1_UE        (13)    /* USART enable */
193 #define STM_USART_CR1_M         (12)    /* Word length */
194 #define STM_USART_CR1_WAKE      (11)    /* Wakeup method */
195 #define STM_USART_CR1_PCE       (10)    /* Parity control enable */
196 #define STM_USART_CR1_PS        (9)     /* Parity selection */
197 #define STM_USART_CR1_PEIE      (8)     /* PE interrupt enable */
198 #define STM_USART_CR1_TXEIE     (7)     /* TXE interrupt enable */
199 #define STM_USART_CR1_TCIE      (6)     /* Transmission complete interrupt enable */
200 #define STM_USART_CR1_RXNEIE    (5)     /* RXNE interrupt enable */
201 #define STM_USART_CR1_IDLEIE    (4)     /* IDLE interrupt enable */
202 #define STM_USART_CR1_TE        (3)     /* Transmitter enable */
203 #define STM_USART_CR1_RE        (2)     /* Receiver enable */
204 #define STM_USART_CR1_RWU       (1)     /* Receiver wakeup */
205 #define STM_USART_CR1_SBK       (0)     /* Send break */
206
207 #define STM_USART_CR2_LINEN     (14)    /* LIN mode enable */
208 #define STM_USART_CR2_STOP      (12)    /* STOP bits */
209 #define STM_USART_CR2_STOP_MASK 3
210 #define STM_USART_CR2_STOP_1    0
211 #define STM_USART_CR2_STOP_0_5  1
212 #define STM_USART_CR2_STOP_2    2
213 #define STM_USART_CR2_STOP_1_5  3
214
215 #define STM_USART_CR2_CLKEN     (11)    /* Clock enable */
216 #define STM_USART_CR2_CPOL      (10)    /* Clock polarity */
217 #define STM_USART_CR2_CPHA      (9)     /* Clock phase */
218 #define STM_USART_CR2_LBCL      (8)     /* Last bit clock pulse */
219 #define STM_USART_CR2_LBDIE     (6)     /* LIN break detection interrupt enable */
220 #define STM_USART_CR2_LBDL      (5)     /* lin break detection length */
221 #define STM_USART_CR2_ADD       (0)
222 #define STM_USART_CR2_ADD_MASK  0xf
223
224 #define STM_USART_CR3_ONEBITE   (11)    /* One sample bit method enable */
225 #define STM_USART_CR3_CTSIE     (10)    /* CTS interrupt enable */
226 #define STM_USART_CR3_CTSE      (9)     /* CTS enable */
227 #define STM_USART_CR3_RTSE      (8)     /* RTS enable */
228 #define STM_USART_CR3_DMAT      (7)     /* DMA enable transmitter */
229 #define STM_USART_CR3_DMAR      (6)     /* DMA enable receiver */
230 #define STM_USART_CR3_SCEN      (5)     /* Smartcard mode enable */
231 #define STM_USART_CR3_NACK      (4)     /* Smartcard NACK enable */
232 #define STM_USART_CR3_HDSEL     (3)     /* Half-duplex selection */
233 #define STM_USART_CR3_IRLP      (2)     /* IrDA low-power */
234 #define STM_USART_CR3_IREN      (1)     /* IrDA mode enable */
235 #define STM_USART_CR3_EIE       (0)     /* Error interrupt enable */
236
237 struct stm_spi {
238 };
239
240 extern struct stm_spi stm_spi1;
241
242 struct stm_tim {
243 };
244
245 extern struct stm_tim stm_tim9;
246 extern struct stm_tim stm_tim10;
247 extern struct stm_tim stm_tim11;
248
249 /* Flash interface */
250
251 struct stm_flash {
252         vuint32_t       acr;
253         vuint32_t       pecr;
254         vuint32_t       pdkeyr;
255         vuint32_t       pekeyr;
256
257         vuint32_t       prgkeyr;
258         vuint32_t       optkeyr;
259         vuint32_t       sr;
260         vuint32_t       obr;
261
262         vuint32_t       wrpr;
263 };
264
265 extern struct stm_flash stm_flash;
266
267 #define STM_FLASH_ACR_RUN_PD    (4)
268 #define STM_FLASH_ACR_SLEEP_PD  (3)
269 #define STM_FLASH_ACR_ACC64     (2)
270 #define STM_FLASH_ACR_PRFEN     (1)
271 #define STM_FLASH_ACR_LATENCY   (0)
272
273 struct stm_rcc {
274         vuint32_t       cr;
275         vuint32_t       icscr;
276         vuint32_t       cfgr;
277         vuint32_t       cir;
278
279         vuint32_t       ahbrstr;
280         vuint32_t       apb2rstr;
281         vuint32_t       abp1rstr;
282         vuint32_t       ahbenr;
283
284         vuint32_t       apb2enr;
285         vuint32_t       apb1enr;
286         vuint32_t       ahblenr;
287         vuint32_t       apb2lpenr;
288
289         vuint32_t       apb1lpenr;
290         vuint32_t       csr;
291 };
292
293 extern struct stm_rcc stm_rcc;
294
295 #define STM_RCC_CR_RTCPRE       (29)
296 #define  STM_RCC_CR_RTCPRE_HSE_DIV_2    0
297 #define  STM_RCC_CR_RTCPRE_HSE_DIV_4    1
298 #define  STM_RCC_CR_RTCPRE_HSE_DIV_8    2
299 #define  STM_RCC_CR_RTCPRE_HSE_DIV_16   3
300 #define  STM_RCC_CR_RTCPRE_HSE_MASK     3
301
302 #define STM_RCC_CR_CSSON        (28)
303 #define STM_RCC_CR_PLLRDY       (25)
304 #define STM_RCC_CR_PLLON        (24)
305 #define STM_RCC_CR_HSEBYP       (18)
306 #define STM_RCC_CR_HSERDY       (17)
307 #define STM_RCC_CR_HSEON        (16)
308 #define STM_RCC_CR_MSIRDY       (9)
309 #define STM_RCC_CR_MSION        (8)
310 #define STM_RCC_CR_HSIRDY       (1)
311 #define STM_RCC_CR_HSION        (0)
312
313 #define STM_RCC_CFGR_MCOPRE     (28)
314 #define  STM_RCC_CFGR_MCOPRE_DIV_1      0
315 #define  STM_RCC_CFGR_MCOPRE_DIV_2      1
316 #define  STM_RCC_CFGR_MCOPRE_DIV_4      2
317 #define  STM_RCC_CFGR_MCOPRE_DIV_8      3
318 #define  STM_RCC_CFGR_MCOPRE_DIV_16     4
319 #define  STM_RCC_CFGR_MCOPRE_DIV_MASK   7
320
321 #define STM_RCC_CFGR_MCOSEL     (24)
322 #define  STM_RCC_CFGR_MCOSEL_DISABLE    0
323 #define  STM_RCC_CFGR_MCOSEL_SYSCLK     1
324 #define  STM_RCC_CFGR_MCOSEL_HSI        2
325 #define  STM_RCC_CFGR_MCOSEL_MSI        3
326 #define  STM_RCC_CFGR_MCOSEL_HSE        4
327 #define  STM_RCC_CFGR_MCOSEL_PLL        5
328 #define  STM_RCC_CFGR_MCOSEL_LSI        6
329 #define  STM_RCC_CFGR_MCOSEL_LSE        7
330 #define  STM_RCC_CFGR_MCOSEL_MASK       7
331
332 #define STM_RCC_CFGR_PLLDIV     (22)
333 #define  STM_RCC_CFGR_PLLDIV_2          1
334 #define  STM_RCC_CFGR_PLLDIV_3          2
335 #define  STM_RCC_CFGR_PLLDIV_4          3
336 #define  STM_RCC_CFGR_PLLDIV_MASK       3
337
338 #define STM_RCC_CFGR_PLLMUL     (18)
339 #define  STM_RCC_CFGR_PLLMUL_3          0
340 #define  STM_RCC_CFGR_PLLMUL_4          1
341 #define  STM_RCC_CFGR_PLLMUL_6          2
342 #define  STM_RCC_CFGR_PLLMUL_8          3
343 #define  STM_RCC_CFGR_PLLMUL_12         4
344 #define  STM_RCC_CFGR_PLLMUL_16         5
345 #define  STM_RCC_CFGR_PLLMUL_24         6
346 #define  STM_RCC_CFGR_PLLMUL_32         7
347 #define  STM_RCC_CFGR_PLLMUL_48         8
348 #define  STM_RCC_CFGR_PLLMUL_MASK       0xf
349
350 #define STM_RCC_CFGR_PLLSRC     (16)
351
352 #define STM_RCC_CFGR_PPRE2      (11)
353 #define  STM_RCC_CFGR_PPRE2_DIV_1       0
354 #define  STM_RCC_CFGR_PPRE2_DIV_2       4
355 #define  STM_RCC_CFGR_PPRE2_DIV_4       5
356 #define  STM_RCC_CFGR_PPRE2_DIV_8       6
357 #define  STM_RCC_CFGR_PPRE2_DIV_16      7
358 #define  STM_RCC_CFGR_PPRE2_MASK        7
359
360 #define STM_RCC_CFGR_PPRE1      (8)
361 #define  STM_RCC_CFGR_PPRE1_DIV_1       0
362 #define  STM_RCC_CFGR_PPRE1_DIV_2       4
363 #define  STM_RCC_CFGR_PPRE1_DIV_4       5
364 #define  STM_RCC_CFGR_PPRE1_DIV_8       6
365 #define  STM_RCC_CFGR_PPRE1_DIV_16      7
366 #define  STM_RCC_CFGR_PPRE1_MASK        7
367
368 #define STM_RCC_CFGR_HPRE       (4)
369 #define  STM_RCC_CFGR_HPRE_DIV_1        0
370 #define  STM_RCC_CFGR_HPRE_DIV_2        8
371 #define  STM_RCC_CFGR_HPRE_DIV_4        9
372 #define  STM_RCC_CFGR_HPRE_DIV_8        0xa
373 #define  STM_RCC_CFGR_HPRE_DIV_16       0xb
374 #define  STM_RCC_CFGR_HPRE_DIV_64       0xc
375 #define  STM_RCC_CFGR_HPRE_DIV_128      0xd
376 #define  STM_RCC_CFGR_HPRE_DIV_256      0xe
377 #define  STM_RCC_CFGR_HPRE_DIV_512      0xf
378 #define  STM_RCC_CFGR_HPRE_MASK         0xf
379
380 #define STM_RCC_CFGR_SWS        (2)
381 #define  STM_RCC_CFGR_SWS_MSI           0
382 #define  STM_RCC_CFGR_SWS_HSI           1
383 #define  STM_RCC_CFGR_SWS_HSE           2
384 #define  STM_RCC_CFGR_SWS_PLL           3
385 #define  STM_RCC_CFGR_SWS_MASK          3
386
387 #define STM_RCC_CFGR_SW         (0)
388 #define  STM_RCC_CFGR_SW_MSI            0
389 #define  STM_RCC_CFGR_SW_HSI            1
390 #define  STM_RCC_CFGR_SW_HSE            2
391 #define  STM_RCC_CFGR_SW_PLL            3
392 #define  STM_RCC_CFGR_SW_MASK           3
393
394 #define STM_RCC_AHBENR_DMA1EN           (24)
395 #define STM_RCC_AHBENR_FLITFEN          (15)
396 #define STM_RCC_AHBENR_CRCEN            (12)
397 #define STM_RCC_AHBENR_GPIOHEN          (5)
398 #define STM_RCC_AHBENR_GPIOEEN          (4)
399 #define STM_RCC_AHBENR_GPIODEN          (3)
400 #define STM_RCC_AHBENR_GPIOCEN          (2)
401 #define STM_RCC_AHBENR_GPIOBEN          (1)
402 #define STM_RCC_AHBENR_GPIOAEN          (0)
403
404 #define STM_RCC_APB2ENR_USART1EN        (14)
405 #define STM_RCC_APB2ENR_SPI1EN          (12)
406 #define STM_RCC_APB2ENR_ADC1EN          (9)
407 #define STM_RCC_APB2ENR_TIM11EN         (4)
408 #define STM_RCC_APB2ENR_TIM10EN         (3)
409 #define STM_RCC_APB2ENR_TIM9EN          (2)
410 #define STM_RCC_APB2ENR_SYSCFGEN        (0)
411
412 #define STM_RCC_APB1ENR_COMPEN          (31)
413 #define STM_RCC_APB1ENR_DACEN           (29)
414 #define STM_RCC_APB1ENR_PWREN           (28)
415 #define STM_RCC_APB1ENR_USBEN           (23)
416 #define STM_RCC_APB1ENR_I2C2EN          (22)
417 #define STM_RCC_APB1ENR_I2C1EN          (21)
418 #define STM_RCC_APB1ENR_USART3EN        (18)
419 #define STM_RCC_APB1ENR_USART2EN        (17)
420 #define STM_RCC_APB1ENR_SPI2EN          (14)
421 #define STM_RCC_APB1ENR_WWDGEN          (11)
422 #define STM_RCC_APB1ENR_LCDEN           (9)
423 #define STM_RCC_APB1ENR_TIM7EN          (5)
424 #define STM_RCC_APB1ENR_TIM6EN          (4)
425 #define STM_RCC_APB1ENR_TIM4EN          (2)
426 #define STM_RCC_APB1ENR_TIM3EN          (1)
427 #define STM_RCC_APB1ENR_TIM2EN          (0)
428
429 #define STM_RCC_CSR_LPWRRSTF            (31)
430 #define STM_RCC_CSR_WWDGRSTF            (30)
431 #define STM_RCC_CSR_IWDGRSTF            (29)
432 #define STM_RCC_CSR_SFTRSTF             (28)
433 #define STM_RCC_CSR_PORRSTF             (27)
434 #define STM_RCC_CSR_PINRSTF             (26)
435 #define STM_RCC_CSR_OBLRSTF             (25)
436 #define STM_RCC_CSR_RMVF                (24)
437 #define STM_RCC_CSR_RTFRST              (23)
438 #define STM_RCC_CSR_RTCEN               (22)
439 #define STM_RCC_CSR_RTCSEL              (16)
440
441 #define  STM_RCC_CSR_RTCSEL_NONE                0
442 #define  STM_RCC_CSR_RTCSEL_LSE                 1
443 #define  STM_RCC_CSR_RTCSEL_LSI                 2
444 #define  STM_RCC_CSR_RTCSEL_HSE                 3
445 #define  STM_RCC_CSR_RTCSEL_MASK                3
446
447 #define STM_RCC_CSR_LSEBYP              (10)
448 #define STM_RCC_CSR_LSERDY              (9)
449 #define STM_RCC_CSR_LSEON               (8)
450 #define STM_RCC_CSR_LSIRDY              (1)
451 #define STM_RCC_CSR_LSION               (0)
452
453 struct stm_pwr {
454         vuint32_t       cr;
455         vuint32_t       csr;
456 };
457
458 extern struct stm_pwr stm_pwr;
459
460 #define STM_PWR_CR_LPRUN        (14)
461
462 #define STM_PWR_CR_VOS          (11)
463 #define  STM_PWR_CR_VOS_1_8             1
464 #define  STM_PWR_CR_VOS_1_5             2
465 #define  STM_PWR_CR_VOS_1_2             3
466 #define  STM_PWR_CR_VOS_MASK            3
467
468 #define STM_PWR_CR_FWU          (10)
469 #define STM_PWR_CR_ULP          (9)
470 #define STM_PWR_CR_DBP          (8)
471
472 #define STM_PWR_CR_PLS          (5)
473 #define  STM_PWR_CR_PLS_1_9     0
474 #define  STM_PWR_CR_PLS_2_1     1
475 #define  STM_PWR_CR_PLS_2_3     2
476 #define  STM_PWR_CR_PLS_2_5     3
477 #define  STM_PWR_CR_PLS_2_7     4
478 #define  STM_PWR_CR_PLS_2_9     5
479 #define  STM_PWR_CR_PLS_3_1     6
480 #define  STM_PWR_CR_PLS_EXT     7
481 #define  STM_PWR_CR_PLS_MASK    7
482
483 #define STM_PWR_CR_PVDE         (4)
484 #define STM_PWR_CR_CSBF         (3)
485 #define STM_PWR_CR_CWUF         (2)
486 #define STM_PWR_CR_PDDS         (1)
487 #define STM_PWR_CR_LPSDSR       (0)
488
489 #define STM_PWR_CSR_EWUP3       (10)
490 #define STM_PWR_CSR_EWUP2       (9)
491 #define STM_PWR_CSR_EWUP1       (8)
492 #define STM_PWR_CSR_REGLPF      (5)
493 #define STM_PWR_CSR_VOSF        (4)
494 #define STM_PWR_CSR_VREFINTRDYF (3)
495 #define STM_PWR_CSR_PVDO        (2)
496 #define STM_PWR_CSR_SBF         (1)
497 #define STM_PWR_CSR_WUF         (0)
498
499 struct stm_tim67 {
500         vuint32_t       cr1;
501         vuint32_t       cr2;
502         uint32_t        _unused_08;
503         vuint32_t       dier;
504
505         vuint32_t       sr;
506         vuint32_t       egr;
507         uint32_t        _unused_18;
508         uint32_t        _unused_1c;
509
510         uint32_t        _unused_20;
511         vuint32_t       cnt;
512         vuint32_t       psc;
513         vuint32_t       arr;
514 };
515
516 extern struct stm_tim67 stm_tim6;
517
518 #define STM_TIM67_CR1_ARPE      (7)
519 #define STM_TIM67_CR1_OPM       (3)
520 #define STM_TIM67_CR1_URS       (2)
521 #define STM_TIM67_CR1_UDIS      (1)
522 #define STM_TIM67_CR1_CEN       (0)
523
524 #define STM_TIM67_CR2_MMS       (4)
525 #define  STM_TIM67_CR2_MMS_RESET        0
526 #define  STM_TIM67_CR2_MMS_ENABLE       1
527 #define  STM_TIM67_CR2_MMS_UPDATE       2
528 #define  STM_TIM67_CR2_MMS_MASK         7
529
530 #define STM_TIM67_DIER_UDE      (8)
531 #define STM_TIM67_DIER_UIE      (0)
532
533 #define STM_TIM67_SR_UIF        (0)
534
535 #define STM_TIM67_EGR_UG        (0)
536
537 struct stm_lcd {
538         vuint32_t       cr;
539         vuint32_t       fcr;
540         vuint32_t       sr;
541         vuint32_t       clr;
542         uint32_t        unused_0x10;
543         vuint32_t       ram[8*2];
544 };
545
546 extern struct stm_lcd stm_lcd;
547
548 #define STM_LCD_CR_MUX_SEG              (7)
549
550 #define STM_LCD_CR_BIAS                 (5)
551 #define  STM_LCD_CR_BIAS_1_4            0
552 #define  STM_LCD_CR_BIAS_1_2            1
553 #define  STM_LCD_CR_BIAS_1_3            2
554 #define  STM_LCD_CR_BIAS_MASK           3
555
556 #define STM_LCD_CR_DUTY                 (2)
557 #define  STM_LCD_CR_DUTY_STATIC         0
558 #define  STM_LCD_CR_DUTY_1_2            1
559 #define  STM_LCD_CR_DUTY_1_3            2
560 #define  STM_LCD_CR_DUTY_1_4            3
561 #define  STM_LCD_CR_DUTY_1_8            4
562 #define  STM_LCD_CR_DUTY_MASK           7
563
564 #define STM_LCD_CR_VSEL                 (1)
565 #define STM_LCD_CR_LCDEN                (0)
566
567 #define STM_LCD_FCR_PS                  (22)
568 #define  STM_LCD_FCR_PS_1               0x0
569 #define  STM_LCD_FCR_PS_2               0x1
570 #define  STM_LCD_FCR_PS_4               0x2
571 #define  STM_LCD_FCR_PS_8               0x3
572 #define  STM_LCD_FCR_PS_16              0x4
573 #define  STM_LCD_FCR_PS_32              0x5
574 #define  STM_LCD_FCR_PS_64              0x6
575 #define  STM_LCD_FCR_PS_128             0x7
576 #define  STM_LCD_FCR_PS_256             0x8
577 #define  STM_LCD_FCR_PS_512             0x9
578 #define  STM_LCD_FCR_PS_1024            0xa
579 #define  STM_LCD_FCR_PS_2048            0xb
580 #define  STM_LCD_FCR_PS_4096            0xc
581 #define  STM_LCD_FCR_PS_8192            0xd
582 #define  STM_LCD_FCR_PS_16384           0xe
583 #define  STM_LCD_FCR_PS_32768           0xf
584 #define  STM_LCD_FCR_PS_MASK            0xf
585
586 #define STM_LCD_FCR_DIV                 (18)
587 #define STM_LCD_FCR_DIV_16              0x0
588 #define STM_LCD_FCR_DIV_17              0x1
589 #define STM_LCD_FCR_DIV_18              0x2
590 #define STM_LCD_FCR_DIV_19              0x3
591 #define STM_LCD_FCR_DIV_20              0x4
592 #define STM_LCD_FCR_DIV_21              0x5
593 #define STM_LCD_FCR_DIV_22              0x6
594 #define STM_LCD_FCR_DIV_23              0x7
595 #define STM_LCD_FCR_DIV_24              0x8
596 #define STM_LCD_FCR_DIV_25              0x9
597 #define STM_LCD_FCR_DIV_26              0xa
598 #define STM_LCD_FCR_DIV_27              0xb
599 #define STM_LCD_FCR_DIV_28              0xc
600 #define STM_LCD_FCR_DIV_29              0xd
601 #define STM_LCD_FCR_DIV_30              0xe
602 #define STM_LCD_FCR_DIV_31              0xf
603 #define STM_LCD_FCR_DIV_MASK            0xf
604
605 #define STM_LCD_FCR_BLINK               (16)
606 #define  STM_LCD_FCR_BLINK_DISABLE              0
607 #define  STM_LCD_FCR_BLINK_SEG0_COM0            1
608 #define  STM_LCD_FCR_BLINK_SEG0_COMALL          2
609 #define  STM_LCD_FCR_BLINK_SEGALL_COMALL        3
610 #define  STM_LCD_FCR_BLINK_MASK                 3
611
612 #define STM_LCD_FCR_BLINKF              (13)
613 #define  STM_LCD_FCR_BLINKF_8                   0
614 #define  STM_LCD_FCR_BLINKF_16                  1
615 #define  STM_LCD_FCR_BLINKF_32                  2
616 #define  STM_LCD_FCR_BLINKF_64                  3
617 #define  STM_LCD_FCR_BLINKF_128                 4
618 #define  STM_LCD_FCR_BLINKF_256                 5
619 #define  STM_LCD_FCR_BLINKF_512                 6
620 #define  STM_LCD_FCR_BLINKF_1024                7
621 #define  STM_LCD_FCR_BLINKF_MASK                7
622
623 #define STM_LCD_FCR_CC                  (10)
624 #define  STM_LCD_FCR_CC_MASK                    7
625
626 #define STM_LCD_FCR_DEAD                (7)
627 #define  STM_LCD_FCR_DEAD_MASK                  7
628
629 #define STM_LCD_FCR_PON                 (4)
630 #define  STM_LCD_FCR_PON_MASK                   7
631
632 #define STM_LCD_FCR_UDDIE               (3)
633 #define STM_LCD_FCR_SOFIE               (1)
634 #define STM_LCD_FCR_HD                  (0)
635
636 #define STM_LCD_SR_FCRSF                (5)
637 #define STM_LCD_SR_RDY                  (4)
638 #define STM_LCD_SR_UDD                  (3)
639 #define STM_LCD_SR_UDR                  (2)
640 #define STM_LCD_SR_SOF                  (1)
641 #define STM_LCD_SR_ENS                  (0)
642
643 #define STM_LCD_CLR_UDDC                (3)
644 #define STM_LCD_CLR_SOFC                (1)
645
646 struct stm_nvic {
647         vuint32_t       iser[3];        /* 0x000 */
648
649         uint8_t         _unused00c[0x080 - 0x00c];
650
651         vuint32_t       icer[3];        /* 0x080 */
652
653         uint8_t         _unused08c[0x100 - 0x08c];
654
655         vuint32_t       ispr[3];        /* 0x100 */
656
657         uint8_t         _unused10c[0x180 - 0x10c];
658
659         vuint32_t       icpr[3];        /* 0x180 */
660
661         uint8_t         _unused18c[0x200 - 0x18c];
662
663         vuint32_t       iabr[3];        /* 0x200 */
664
665         uint8_t         _unused20c[0x300 - 0x20c];
666
667         vuint32_t       ipr[21];        /* 0x300 */
668
669         uint8_t         _unused324[0xe00 - 0x324];
670
671         vuint32_t       stir;           /* 0xe00 */
672 };
673
674 extern struct stm_nvic stm_nvic;
675
676 #define IRQ_REG(irq)    ((irq) >> 5)
677 #define IRQ_BIT(irq)    ((irq) & 0x1f)
678 #define IRQ_MASK(irq)   (1 << IRQ_BIT(irq))
679 #define IRQ_BOOL(v,irq) (((v) >> IRQ_BIT(irq)) & 1)
680
681 static inline void
682 stm_nvic_set_enable(int irq) {
683         stm_nvic.iser[IRQ_REG(irq)] = IRQ_MASK(irq);
684 }
685
686 static inline void
687 stm_nvic_clear_enable(int irq) {
688         stm_nvic.icer[IRQ_REG(irq)] = IRQ_MASK(irq);
689 }
690
691 static inline int
692 stm_nvic_enabled(int irq) {
693         return IRQ_BOOL(stm_nvic.iser[IRQ_REG(irq)], irq);
694 }
695         
696 static inline void
697 stm_nvic_set_pending(int irq) {
698         stm_nvic.ispr[IRQ_REG(irq)] = IRQ_MASK(irq);
699 }
700
701 static inline void
702 stm_nvic_clear_pending(int irq) {
703         stm_nvic.icpr[IRQ_REG(irq)] = IRQ_MASK(irq);
704 }
705
706 static inline int
707 stm_nvic_pending(int irq) {
708         return IRQ_BOOL(stm_nvic.ispr[IRQ_REG(irq)], irq);
709 }
710
711 static inline int
712 stm_nvic_active(int irq) {
713         return IRQ_BOOL(stm_nvic.iabr[IRQ_REG(irq)], irq);
714 }
715
716 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
717 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
718 #define IRQ_PRIO_MASK(irq)      (0xff << IRQ_PRIO_BIT(irq))
719
720 static inline void
721 stm_nvic_set_priority(int irq, uint8_t prio) {
722         int             n = IRQ_PRIO_REG(irq);
723         uint32_t        v;
724
725         v = stm_nvic.ipr[n];
726         v &= ~IRQ_PRIO_MASK(irq);
727         v |= (prio) << IRQ_PRIO_BIT(irq);
728         stm_nvic.ipr[n] = v;
729 }
730
731 static inline uint8_t
732 stm_nvic_get_priority(int irq) {
733         return (stm_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
734 }
735
736 #define isr(name) void stm_ ## name ## _isr(void);
737
738 isr(nmi)
739 isr(hardfault)
740 isr(memmanage)
741 isr(busfault)
742 isr(usagefault)
743 isr(svc)
744 isr(debugmon)
745 isr(pendsv)
746 isr(systick)
747 isr(wwdg)
748 isr(pvd)
749 isr(tamper_stamp)
750 isr(rtc_wkup)
751 isr(flash)
752 isr(rcc)
753 isr(exti0)
754 isr(exti1)
755 isr(exti2)
756 isr(exti3)
757 isr(exti4)
758 isr(dma1_channel1)
759 isr(dma1_channel2)
760 isr(dma1_channel3)
761 isr(dma1_channel4)
762 isr(dma1_channel5)
763 isr(dma1_channel6)
764 isr(dma1_channel7)
765 isr(adc1)
766 isr(usb_hp)
767 isr(usb_lp)
768 isr(dac)
769 isr(comp)
770 isr(exti9_5)
771 isr(lcd)
772 isr(tim9)
773 isr(tim10)
774 isr(tim11)
775 isr(tim2)
776 isr(tim3)
777 isr(tim4)
778 isr(i2c1_ev)
779 isr(i2c1_er)
780 isr(i2c2_ev)
781 isr(i2c2_er)
782 isr(spi1)
783 isr(spi2)
784 isr(usart1)
785 isr(usart2)
786 isr(usart3)
787 isr(exti15_10)
788 isr(rtc_alarm)
789 isr(usb_fs_wkup)
790 isr(tim6)
791 isr(tim7)
792
793 #undef isr
794
795 #define STM_ISR_WWDG_POS                0
796 #define STM_ISR_PVD_POS                 1
797 #define STM_ISR_TAMPER_STAMP_POS        2
798 #define STM_ISR_RTC_WKUP_POS            3
799 #define STM_ISR_FLASH_POS               4
800 #define STM_ISR_RCC_POS                 5
801 #define STM_ISR_EXTI0_POS               6
802 #define STM_ISR_EXTI1_POS               7
803 #define STM_ISR_EXTI2_POS               8
804 #define STM_ISR_EXTI3_POS               9
805 #define STM_ISR_EXTI4_POS               10
806 #define STM_ISR_DMA1_CHANNEL1_POS       11
807 #define STM_ISR_DMA2_CHANNEL1_POS       12
808 #define STM_ISR_DMA3_CHANNEL1_POS       13
809 #define STM_ISR_DMA4_CHANNEL1_POS       14
810 #define STM_ISR_DMA5_CHANNEL1_POS       15
811 #define STM_ISR_DMA6_CHANNEL1_POS       16
812 #define STM_ISR_DMA7_CHANNEL1_POS       17
813 #define STM_ISR_ADC1_POS                18
814 #define STM_ISR_USB_HP_POS              19
815 #define STM_ISR_USB_LP_POS              20
816 #define STM_ISR_DAC_POS                 21
817 #define STM_ISR_COMP_POS                22
818 #define STM_ISR_EXTI9_5_POS             23
819 #define STM_ISR_LCD_POS                 24
820 #define STM_ISR_TIM9_POS                25
821 #define STM_ISR_TIM10_POS               26
822 #define STM_ISR_TIM11_POS               27
823 #define STM_ISR_TIM2_POS                28
824 #define STM_ISR_TIM3_POS                29
825 #define STM_ISR_TIM4_POS                30
826 #define STM_ISR_I2C1_EV_POS             31
827 #define STM_ISR_I2C1_ER_POS             32
828 #define STM_ISR_I2C2_EV_POS             33
829 #define STM_ISR_I2C2_ER_POS             34
830 #define STM_ISR_SPI1_POS                35
831 #define STM_ISR_SPI2_POS                36
832 #define STM_ISR_USART1_POS              37
833 #define STM_ISR_USART2_POS              38
834 #define STM_ISR_USART3_POS              39
835 #define STM_ISR_EXTI15_10_POS           40
836 #define STM_ISR_RTC_ALARM_POS           41
837 #define STM_ISR_USB_FS_WKUP_POS         42
838 #define STM_ISR_TIM6_POS                43
839 #define STM_ISR_TIM7_POS                44
840
841 #endif /* _STM32L_H_ */