]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/common.c
ARM: OMAP: Update 32k sync timer for OMAP3
[linux-2.6-omap-h63xx.git] / 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/pm.h>
15 #include <linux/console.h>
16 #include <linux/serial.h>
17 #include <linux/tty.h>
18 #include <linux/serial_8250.h>
19 #include <linux/serial_reg.h>
20 #include <linux/clk.h>
21
22 #include <asm/hardware.h>
23 #include <asm/system.h>
24 #include <asm/pgtable.h>
25 #include <asm/mach/map.h>
26 #include <asm/io.h>
27 #include <asm/setup.h>
28
29 #include <asm/arch/board.h>
30 #include <asm/arch/mux.h>
31 #include <asm/arch/fpga.h>
32
33 #include <asm/arch/clock.h>
34
35 #define NO_LENGTH_CHECK 0xffffffff
36
37 unsigned char omap_bootloader_tag[512];
38 int omap_bootloader_tag_len;
39
40 struct omap_board_config_kernel *omap_board_config;
41 int omap_board_config_size;
42
43 #ifdef CONFIG_OMAP_BOOT_TAG
44
45 static int __init parse_tag_omap(const struct tag *tag)
46 {
47         u32 size = tag->hdr.size - (sizeof(tag->hdr) >> 2);
48
49         size <<= 2;
50         if (size > sizeof(omap_bootloader_tag))
51                 return -1;
52
53         memcpy(omap_bootloader_tag, tag->u.omap.data, size);
54         omap_bootloader_tag_len = size;
55
56         return 0;
57 }
58
59 __tagtable(ATAG_BOARD, parse_tag_omap);
60
61 #endif
62
63 static const void *get_config(u16 tag, size_t len, int skip, size_t *len_out)
64 {
65         struct omap_board_config_kernel *kinfo = NULL;
66         int i;
67
68 #ifdef CONFIG_OMAP_BOOT_TAG
69         struct omap_board_config_entry *info = NULL;
70
71         if (omap_bootloader_tag_len > 4)
72                 info = (struct omap_board_config_entry *) omap_bootloader_tag;
73         while (info != NULL) {
74                 u8 *next;
75
76                 if (info->tag == tag) {
77                         if (skip == 0)
78                                 break;
79                         skip--;
80                 }
81
82                 if ((info->len & 0x03) != 0) {
83                         /* We bail out to avoid an alignment fault */
84                         printk(KERN_ERR "OMAP peripheral config: Length (%d) not word-aligned (tag %04x)\n",
85                                info->len, info->tag);
86                         return NULL;
87                 }
88                 next = (u8 *) info + sizeof(*info) + info->len;
89                 if (next >= omap_bootloader_tag + omap_bootloader_tag_len)
90                         info = NULL;
91                 else
92                         info = (struct omap_board_config_entry *) next;
93         }
94         if (info != NULL) {
95                 /* Check the length as a lame attempt to check for
96                  * binary inconsistency. */
97                 if (len != NO_LENGTH_CHECK) {
98                         /* Word-align len */
99                         if (len & 0x03)
100                                 len = (len + 3) & ~0x03;
101                         if (info->len != len) {
102                                 printk(KERN_ERR "OMAP peripheral config: Length mismatch with tag %x (want %d, got %d)\n",
103                                        tag, len, info->len);
104                                 return NULL;
105                         }
106                 }
107                 if (len_out != NULL)
108                         *len_out = info->len;
109                 return info->data;
110         }
111 #endif
112         /* Try to find the config from the board-specific structures
113          * in the kernel. */
114         for (i = 0; i < omap_board_config_size; i++) {
115                 if (omap_board_config[i].tag == tag) {
116                         if (skip == 0) {
117                                 kinfo = &omap_board_config[i];
118                                 break;
119                         } else {
120                                 skip--;
121                         }
122                 }
123         }
124         if (kinfo == NULL)
125                 return NULL;
126         return kinfo->data;
127 }
128
129 const void *__omap_get_config(u16 tag, size_t len, int nr)
130 {
131         return get_config(tag, len, nr, NULL);
132 }
133 EXPORT_SYMBOL(__omap_get_config);
134
135 const void *omap_get_var_config(u16 tag, size_t *len)
136 {
137         return get_config(tag, NO_LENGTH_CHECK, 0, len);
138 }
139 EXPORT_SYMBOL(omap_get_var_config);
140
141 static int __init omap_add_serial_console(void)
142 {
143         const struct omap_serial_console_config *con_info;
144         const struct omap_uart_config *uart_info;
145         static char speed[11], *opt = NULL;
146         int line, i, uart_idx;
147
148         uart_info = omap_get_config(OMAP_TAG_UART, struct omap_uart_config);
149         con_info = omap_get_config(OMAP_TAG_SERIAL_CONSOLE,
150                                         struct omap_serial_console_config);
151         if (uart_info == NULL || con_info == NULL)
152                 return 0;
153
154         if (con_info->console_uart == 0)
155                 return 0;
156
157         if (con_info->console_speed) {
158                 snprintf(speed, sizeof(speed), "%u", con_info->console_speed);
159                 opt = speed;
160         }
161
162         uart_idx = con_info->console_uart - 1;
163         if (uart_idx >= OMAP_MAX_NR_PORTS) {
164                 printk(KERN_INFO "Console: external UART#%d. "
165                         "Not adding it as console this time.\n",
166                         uart_idx + 1);
167                 return 0;
168         }
169         if (!(uart_info->enabled_uarts & (1 << uart_idx))) {
170                 printk(KERN_ERR "Console: Selected UART#%d is "
171                         "not enabled for this platform\n",
172                         uart_idx + 1);
173                 return -1;
174         }
175         line = 0;
176         for (i = 0; i < uart_idx; i++) {
177                 if (uart_info->enabled_uarts & (1 << i))
178                         line++;
179         }
180         return add_preferred_console("ttyS", line, opt);
181 }
182 console_initcall(omap_add_serial_console);
183
184
185 /*
186  * 32KHz clocksource ... always available, on pretty most chips except
187  * OMAP 730 and 1510.  Other timers could be used as clocksources, with
188  * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
189  * but systems won't necessarily want to spend resources that way.
190  */
191
192 #if defined(CONFIG_ARCH_OMAP16XX)
193 #define TIMER_32K_SYNCHRONIZED          0xfffbc410
194 #elif defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
195 #define TIMER_32K_SYNCHRONIZED          (OMAP2_32KSYNCT_BASE + 0x10)
196 #endif
197
198 #ifdef  TIMER_32K_SYNCHRONIZED
199
200 #include <linux/clocksource.h>
201
202 static cycle_t omap_32k_read(void)
203 {
204         return omap_readl(TIMER_32K_SYNCHRONIZED);
205 }
206
207 static struct clocksource clocksource_32k = {
208         .name           = "32k_counter",
209         .rating         = 250,
210         .read           = omap_32k_read,
211         .mask           = CLOCKSOURCE_MASK(32),
212         .shift          = 10,
213         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
214 };
215
216 /*
217  * Rounds down to nearest nsec.
218  */
219 unsigned long long omap_32k_ticks_to_nsecs(unsigned long ticks_32k)
220 {
221         return cyc2ns(&clocksource_32k, ticks_32k);
222 }
223
224 /*
225  * Returns current time from boot in nsecs. It's OK for this to wrap
226  * around for now, as it's just a relative time stamp.
227  */
228 unsigned long long sched_clock(void)
229 {
230         return omap_32k_ticks_to_nsecs(omap_32k_read());
231 }
232
233 static int __init omap_init_clocksource_32k(void)
234 {
235         static char err[] __initdata = KERN_ERR
236                         "%s: can't register clocksource!\n";
237
238         if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
239                 struct clk *sync_32k_ick;
240
241                 sync_32k_ick = clk_get(NULL, "omap_32ksync_ick");
242                 if (sync_32k_ick)
243                         clk_enable(sync_32k_ick);
244
245                 clocksource_32k.mult = clocksource_hz2mult(32768,
246                                             clocksource_32k.shift);
247
248                 if (clocksource_register(&clocksource_32k))
249                         printk(err, clocksource_32k.name);
250         }
251         return 0;
252 }
253 arch_initcall(omap_init_clocksource_32k);
254
255 #endif  /* TIMER_32K_SYNCHRONIZED */