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