altos: Use explicit boot loader signal in ao_boot_reboot
[fw/altos] / src / lpc / ao_timer_lpc.c
1 /*
2  * Copyright © 2013 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 #include <ao.h>
19
20 volatile __data AO_TICK_TYPE ao_tick_count;
21
22 uint16_t
23 ao_time(void)
24 {
25         return ao_tick_count;
26 }
27
28 #if AO_DATA_ALL
29 volatile __data uint8_t ao_data_interval = 1;
30 volatile __data uint8_t ao_data_count;
31 #endif
32
33 void lpc_systick_isr(void)
34 {
35         if (lpc_systick.csr & (1 << LPC_SYSTICK_CSR_COUNTFLAG)) {
36                 ++ao_tick_count;
37 #if HAS_TASK_QUEUE
38                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
39                         ao_task_check_alarm((uint16_t) ao_tick_count);
40 #endif
41 #if AO_DATA_ALL
42                 if (++ao_data_count == ao_data_interval) {
43                         ao_data_count = 0;
44                         ao_adc_poll();
45 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
46                         ao_wakeup((void *) &ao_data_count);
47 #endif
48                 }
49 #endif
50         }
51 }
52
53 #if HAS_ADC
54 void
55 ao_timer_set_adc_interval(uint8_t interval)
56 {
57         ao_arch_critical(
58                 ao_data_interval = interval;
59                 ao_data_count = 0;
60                 );
61 }
62 #endif
63
64 #define SYSTICK_RELOAD ((AO_LPC_SYSCLK / 2) / 100 - 1)
65
66 /* Initialize our 100Hz clock */
67 void
68 ao_timer_init(void)
69 {
70         lpc_systick.rvr = SYSTICK_RELOAD;
71         lpc_systick.cvr = 0;
72         lpc_systick.csr = ((1 << LPC_SYSTICK_CSR_ENABLE) |
73                            (1 << LPC_SYSTICK_CSR_TICKINT) |
74                            (LPC_SYSTICK_CSR_CLKSOURCE_CPU_OVER_2 << LPC_SYSTICK_CSR_CLKSOURCE));
75 }
76
77 #define AO_LPC_M        ((AO_LPC_CLKOUT / AO_LPC_CLKIN) - 1)
78
79 #define AO_LPC_FCCO_MIN 156000000
80
81 static void
82 ao_clock_delay(void)
83 {
84         uint32_t        i;
85         for (i = 0; i < 200; i++)
86                 ao_arch_nop();
87 }
88
89 void
90 ao_clock_init(void)
91 {
92         uint8_t         p;
93         uint32_t        i;
94
95         /* Turn off all perhipherals except for GPIO configuration */
96         lpc_scb.sysahbclkctrl = ((1 << LPC_SCB_SYSAHBCLKCTRL_SYS) |
97                                  (1 << LPC_SCB_SYSAHBCLKCTRL_ROM) |
98                                  (1 << LPC_SCB_SYSAHBCLKCTRL_RAM0) |
99                                  (1 << LPC_SCB_SYSAHBCLKCTRL_FLASHARRAY) |
100                                  (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO) |
101                                  (1 << LPC_SCB_SYSAHBCLKCTRL_IOCON));
102                                  
103         /* Enable the brown-out detection at the highest voltage to
104          * make sure the flash part remains happy
105          */
106
107         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_BOD_PD);
108         lpc_scb.bodctrl = ((LPC_SCB_BOD_BODRSTLEV_2_63 << LPC_SCB_BOD_BODRSTLEV) |
109                            (LPC_SCB_BOD_BODINTVAL_RESERVED << LPC_SCB_BOD_BODINTVAL) |
110                            (1 << LPC_SCB_BOD_BODRSTENA));
111
112         /* Turn the IRC clock back on */
113         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_IRC_PD);
114         ao_clock_delay();
115         
116         /* Switch to the IRC clock */
117         lpc_scb.mainclksel = LPC_SCB_MAINCLKSEL_SEL_IRC << LPC_SCB_MAINCLKSEL_SEL;
118         lpc_scb.mainclkuen = (0 << LPC_SCB_MAINCLKUEN_ENA);
119         lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
120         while (!(lpc_scb.mainclkuen & (1 << LPC_SCB_MAINCLKUEN_ENA)))
121                 ;
122         
123         /* Switch USB to the main clock */
124         lpc_scb.usbclksel = (LPC_SCB_USBCLKSEL_SEL_MAIN_CLOCK << LPC_SCB_USBCLKSEL_SEL);
125         lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
126         lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
127         while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
128                 ;
129         
130         /* Find a PLL post divider ratio that gets the FCCO in range */
131         for (p = 0; p < 4; p++)
132                 if (AO_LPC_CLKOUT << (1 + p) >= AO_LPC_FCCO_MIN)
133                         break;
134
135         if (p == 4)
136                 ao_panic(AO_PANIC_CRASH);
137
138         /* Power down the PLL before touching the registers */
139         lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
140         ao_clock_delay();
141
142         /* Set PLL divider values */
143         lpc_scb.syspllctrl = ((AO_LPC_M << LPC_SCB_SYSPLLCTRL_MSEL) |
144                               (p << LPC_SCB_SYSPLLCTRL_PSEL));
145
146         /* Turn off the external crystal clock */
147         lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
148         ao_clock_delay();
149
150         /* Configure the crystal clock */
151         lpc_scb.sysoscctrl = ((0 << LPC_SCB_SYSOSCCTRL_BYPASS) |                           /* using a crystal */
152                               ((AO_LPC_CLKIN > 15000000) << LPC_SCB_SYSOSCCTRL_FREQRANGE));/* set range */
153
154         /* Turn on the external crystal clock */
155         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
156         ao_clock_delay();
157
158         /* Select crystal as PLL input */
159
160         lpc_scb.syspllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
161         lpc_scb.syspllclkuen = (1 << LPC_SCB_SYSPLLCLKUEN_ENA);
162         lpc_scb.syspllclkuen = (0 << LPC_SCB_SYSPLLCLKUEN_ENA);
163         lpc_scb.syspllclkuen = (1 << LPC_SCB_SYSPLLCLKUEN_ENA);
164         while (!(lpc_scb.syspllclkuen & (1 << LPC_SCB_SYSPLLCLKUEN_ENA)))
165                 ;
166         
167         /* Turn on the PLL */
168         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
169
170         /* Wait for it to lock */
171         
172         for (i = 0; i < 20000; i++)
173                 if (lpc_scb.syspllstat & (1 << LPC_SCB_SYSPLLSTAT_LOCK))
174                         break;
175         if (i == 20000)
176                 ao_panic(AO_PANIC_CRASH);
177
178         /* Switch to the PLL */
179         lpc_scb.mainclksel = LPC_SCB_MAINCLKSEL_SEL_PLL_OUTPUT << LPC_SCB_MAINCLKSEL_SEL;
180         lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
181         lpc_scb.mainclkuen = (0 << LPC_SCB_MAINCLKUEN_ENA);
182         lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
183         while (!(lpc_scb.mainclkuen & (1 << LPC_SCB_MAINCLKUEN_ENA)))
184                 ;
185
186         /* Set system clock divider */
187         lpc_scb.sysahbclkdiv = AO_LPC_CLKOUT / AO_LPC_SYSCLK;
188
189         /* Shut down perhipheral clocks (enabled as needed) */
190         lpc_scb.ssp0clkdiv = 0;
191         lpc_scb.uartclkdiv = 0;
192         lpc_scb.ssp1clkdiv = 0;
193         lpc_scb.usbclkdiv = 0;
194         lpc_scb.clkoutdiv = 0;
195
196         /* Switch USB PLL source to system osc so we can power down the IRC */
197         lpc_scb.usbpllclksel = (LPC_SCB_USBPLLCLKSEL_SEL_SYSOSC << LPC_SCB_USBPLLCLKSEL_SEL);
198         lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
199         lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
200         while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
201                 ;
202         
203         /* Power down everything we don't need */
204         lpc_scb.pdruncfg = ((1 << LPC_SCB_PDRUNCFG_IRCOUT_PD) |
205                             (1 << LPC_SCB_PDRUNCFG_IRC_PD) |
206                             (0 << LPC_SCB_PDRUNCFG_BOD_PD) |
207                             (1 << LPC_SCB_PDRUNCFG_ADC_PD) |
208                             (1 << LPC_SCB_PDRUNCFG_WDTOSC_PD) |
209                             (1 << LPC_SCB_PDRUNCFG_USBPLL_PD) |
210                             (1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
211                             (1 << 11) |
212                             (7 << 13));
213 }