OMAP2/3: PM: push core PM code from linux-omap
[linux-2.6] / arch / arm / plat-omap / common.c
1 /*
2  * linux/arch/arm/plat-omap/common.c
3  *
4  * Code common to all OMAP machines.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/console.h>
15 #include <linux/serial.h>
16 #include <linux/tty.h>
17 #include <linux/serial_8250.h>
18 #include <linux/serial_reg.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21
22 #include <mach/hardware.h>
23 #include <asm/system.h>
24 #include <asm/pgtable.h>
25 #include <asm/mach/map.h>
26 #include <asm/setup.h>
27
28 #include <mach/common.h>
29 #include <mach/board.h>
30 #include <mach/control.h>
31 #include <mach/mux.h>
32 #include <mach/fpga.h>
33
34 #include <mach/clock.h>
35
36 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
37 # include "../mach-omap2/sdrc.h"
38 #endif
39
40 #define NO_LENGTH_CHECK 0xffffffff
41
42 unsigned char omap_bootloader_tag[512];
43 int omap_bootloader_tag_len;
44
45 struct omap_board_config_kernel *omap_board_config;
46 int omap_board_config_size;
47
48 static const void *get_config(u16 tag, size_t len, int skip, size_t *len_out)
49 {
50         struct omap_board_config_kernel *kinfo = NULL;
51         int i;
52
53 #ifdef CONFIG_OMAP_BOOT_TAG
54         struct omap_board_config_entry *info = NULL;
55
56         if (omap_bootloader_tag_len > 4)
57                 info = (struct omap_board_config_entry *) omap_bootloader_tag;
58         while (info != NULL) {
59                 u8 *next;
60
61                 if (info->tag == tag) {
62                         if (skip == 0)
63                                 break;
64                         skip--;
65                 }
66
67                 if ((info->len & 0x03) != 0) {
68                         /* We bail out to avoid an alignment fault */
69                         printk(KERN_ERR "OMAP peripheral config: Length (%d) not word-aligned (tag %04x)\n",
70                                info->len, info->tag);
71                         return NULL;
72                 }
73                 next = (u8 *) info + sizeof(*info) + info->len;
74                 if (next >= omap_bootloader_tag + omap_bootloader_tag_len)
75                         info = NULL;
76                 else
77                         info = (struct omap_board_config_entry *) next;
78         }
79         if (info != NULL) {
80                 /* Check the length as a lame attempt to check for
81                  * binary inconsistency. */
82                 if (len != NO_LENGTH_CHECK) {
83                         /* Word-align len */
84                         if (len & 0x03)
85                                 len = (len + 3) & ~0x03;
86                         if (info->len != len) {
87                                 printk(KERN_ERR "OMAP peripheral config: Length mismatch with tag %x (want %d, got %d)\n",
88                                        tag, len, info->len);
89                                 return NULL;
90                         }
91                 }
92                 if (len_out != NULL)
93                         *len_out = info->len;
94                 return info->data;
95         }
96 #endif
97         /* Try to find the config from the board-specific structures
98          * in the kernel. */
99         for (i = 0; i < omap_board_config_size; i++) {
100                 if (omap_board_config[i].tag == tag) {
101                         if (skip == 0) {
102                                 kinfo = &omap_board_config[i];
103                                 break;
104                         } else {
105                                 skip--;
106                         }
107                 }
108         }
109         if (kinfo == NULL)
110                 return NULL;
111         return kinfo->data;
112 }
113
114 const void *__omap_get_config(u16 tag, size_t len, int nr)
115 {
116         return get_config(tag, len, nr, NULL);
117 }
118 EXPORT_SYMBOL(__omap_get_config);
119
120 const void *omap_get_var_config(u16 tag, size_t *len)
121 {
122         return get_config(tag, NO_LENGTH_CHECK, 0, len);
123 }
124 EXPORT_SYMBOL(omap_get_var_config);
125
126 static int __init omap_add_serial_console(void)
127 {
128         const struct omap_serial_console_config *con_info;
129         const struct omap_uart_config *uart_info;
130         static char speed[11], *opt = NULL;
131         int line, i, uart_idx;
132
133         uart_info = omap_get_config(OMAP_TAG_UART, struct omap_uart_config);
134         con_info = omap_get_config(OMAP_TAG_SERIAL_CONSOLE,
135                                         struct omap_serial_console_config);
136         if (uart_info == NULL || con_info == NULL)
137                 return 0;
138
139         if (con_info->console_uart == 0)
140                 return 0;
141
142         if (con_info->console_speed) {
143                 snprintf(speed, sizeof(speed), "%u", con_info->console_speed);
144                 opt = speed;
145         }
146
147         uart_idx = con_info->console_uart - 1;
148         if (uart_idx >= OMAP_MAX_NR_PORTS) {
149                 printk(KERN_INFO "Console: external UART#%d. "
150                         "Not adding it as console this time.\n",
151                         uart_idx + 1);
152                 return 0;
153         }
154         if (!(uart_info->enabled_uarts & (1 << uart_idx))) {
155                 printk(KERN_ERR "Console: Selected UART#%d is "
156                         "not enabled for this platform\n",
157                         uart_idx + 1);
158                 return -1;
159         }
160         line = 0;
161         for (i = 0; i < uart_idx; i++) {
162                 if (uart_info->enabled_uarts & (1 << i))
163                         line++;
164         }
165         return add_preferred_console("ttyS", line, opt);
166 }
167 console_initcall(omap_add_serial_console);
168
169
170 /*
171  * 32KHz clocksource ... always available, on pretty most chips except
172  * OMAP 730 and 1510.  Other timers could be used as clocksources, with
173  * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
174  * but systems won't necessarily want to spend resources that way.
175  */
176
177 #define OMAP16XX_TIMER_32K_SYNCHRONIZED         0xfffbc410
178
179 #if !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX))
180
181 #include <linux/clocksource.h>
182
183 #ifdef CONFIG_ARCH_OMAP16XX
184 static cycle_t omap16xx_32k_read(struct clocksource *cs)
185 {
186         return omap_readl(OMAP16XX_TIMER_32K_SYNCHRONIZED);
187 }
188 #else
189 #define omap16xx_32k_read       NULL
190 #endif
191
192 #ifdef CONFIG_ARCH_OMAP2420
193 static cycle_t omap2420_32k_read(struct clocksource *cs)
194 {
195         return omap_readl(OMAP2420_32KSYNCT_BASE + 0x10);
196 }
197 #else
198 #define omap2420_32k_read       NULL
199 #endif
200
201 #ifdef CONFIG_ARCH_OMAP2430
202 static cycle_t omap2430_32k_read(struct clocksource *cs)
203 {
204         return omap_readl(OMAP2430_32KSYNCT_BASE + 0x10);
205 }
206 #else
207 #define omap2430_32k_read       NULL
208 #endif
209
210 #ifdef CONFIG_ARCH_OMAP34XX
211 static cycle_t omap34xx_32k_read(struct clocksource *cs)
212 {
213         return omap_readl(OMAP3430_32KSYNCT_BASE + 0x10);
214 }
215 #else
216 #define omap34xx_32k_read       NULL
217 #endif
218
219 /*
220  * Kernel assumes that sched_clock can be called early but may not have
221  * things ready yet.
222  */
223 static cycle_t omap_32k_read_dummy(struct clocksource *cs)
224 {
225         return 0;
226 }
227
228 static struct clocksource clocksource_32k = {
229         .name           = "32k_counter",
230         .rating         = 250,
231         .read           = omap_32k_read_dummy,
232         .mask           = CLOCKSOURCE_MASK(32),
233         .shift          = 10,
234         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
235 };
236
237 /*
238  * Returns current time from boot in nsecs. It's OK for this to wrap
239  * around for now, as it's just a relative time stamp.
240  */
241 unsigned long long sched_clock(void)
242 {
243         unsigned long long ret;
244
245         ret = (unsigned long long)clocksource_32k.read(&clocksource_32k);
246         ret = (ret * clocksource_32k.mult_orig) >> clocksource_32k.shift;
247         return ret;
248 }
249
250 static int __init omap_init_clocksource_32k(void)
251 {
252         static char err[] __initdata = KERN_ERR
253                         "%s: can't register clocksource!\n";
254
255         if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
256                 struct clk *sync_32k_ick;
257
258                 if (cpu_is_omap16xx())
259                         clocksource_32k.read = omap16xx_32k_read;
260                 else if (cpu_is_omap2420())
261                         clocksource_32k.read = omap2420_32k_read;
262                 else if (cpu_is_omap2430())
263                         clocksource_32k.read = omap2430_32k_read;
264                 else if (cpu_is_omap34xx())
265                         clocksource_32k.read = omap34xx_32k_read;
266                 else
267                         return -ENODEV;
268
269                 sync_32k_ick = clk_get(NULL, "omap_32ksync_ick");
270                 if (sync_32k_ick)
271                         clk_enable(sync_32k_ick);
272
273                 clocksource_32k.mult = clocksource_hz2mult(32768,
274                                             clocksource_32k.shift);
275
276                 if (clocksource_register(&clocksource_32k))
277                         printk(err, clocksource_32k.name);
278         }
279         return 0;
280 }
281 arch_initcall(omap_init_clocksource_32k);
282
283 #endif  /* !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX)) */
284
285 /* Global address base setup code */
286
287 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
288
289 static void __init __omap2_set_globals(struct omap_globals *omap2_globals)
290 {
291         omap2_set_globals_tap(omap2_globals);
292         omap2_set_globals_sdrc(omap2_globals);
293         omap2_set_globals_control(omap2_globals);
294         omap2_set_globals_prcm(omap2_globals);
295 }
296
297 #endif
298
299 #if defined(CONFIG_ARCH_OMAP2420)
300
301 static struct omap_globals omap242x_globals = {
302         .class  = OMAP242X_CLASS,
303         .tap    = OMAP2_IO_ADDRESS(0x48014000),
304         .sdrc   = OMAP2_IO_ADDRESS(OMAP2420_SDRC_BASE),
305         .sms    = OMAP2_IO_ADDRESS(OMAP2420_SMS_BASE),
306         .ctrl   = OMAP2_IO_ADDRESS(OMAP2420_CTRL_BASE),
307         .prm    = OMAP2_IO_ADDRESS(OMAP2420_PRM_BASE),
308         .cm     = OMAP2_IO_ADDRESS(OMAP2420_CM_BASE),
309 };
310
311 void __init omap2_set_globals_242x(void)
312 {
313         __omap2_set_globals(&omap242x_globals);
314 }
315 #endif
316
317 #if defined(CONFIG_ARCH_OMAP2430)
318
319 static struct omap_globals omap243x_globals = {
320         .class  = OMAP243X_CLASS,
321         .tap    = OMAP2_IO_ADDRESS(0x4900a000),
322         .sdrc   = OMAP2_IO_ADDRESS(OMAP243X_SDRC_BASE),
323         .sms    = OMAP2_IO_ADDRESS(OMAP243X_SMS_BASE),
324         .ctrl   = OMAP2_IO_ADDRESS(OMAP243X_CTRL_BASE),
325         .prm    = OMAP2_IO_ADDRESS(OMAP2430_PRM_BASE),
326         .cm     = OMAP2_IO_ADDRESS(OMAP2430_CM_BASE),
327 };
328
329 void __init omap2_set_globals_243x(void)
330 {
331         __omap2_set_globals(&omap243x_globals);
332 }
333 #endif
334
335 #if defined(CONFIG_ARCH_OMAP3430)
336
337 static struct omap_globals omap343x_globals = {
338         .class  = OMAP343X_CLASS,
339         .tap    = OMAP2_IO_ADDRESS(0x4830A000),
340         .sdrc   = OMAP2_IO_ADDRESS(OMAP343X_SDRC_BASE),
341         .sms    = OMAP2_IO_ADDRESS(OMAP343X_SMS_BASE),
342         .ctrl   = OMAP2_IO_ADDRESS(OMAP343X_CTRL_BASE),
343         .prm    = OMAP2_IO_ADDRESS(OMAP3430_PRM_BASE),
344         .cm     = OMAP2_IO_ADDRESS(OMAP3430_CM_BASE),
345 };
346
347 void __init omap2_set_globals_343x(void)
348 {
349         __omap2_set_globals(&omap343x_globals);
350 }
351 #endif
352