blob: 39f2eeb219b40ce389096a20b11492a5306a7934 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/arm/mach-ixp4xx/common.c
3 *
4 * Generic code shared across all IXP4XX platforms
5 *
6 * Maintainer: Deepak Saxena <dsaxena@plexity.net>
7 *
8 * Copyright 2002 (c) Intel Corporation
9 * Copyright 2003-2004 (c) MontaVista, Software, Inc.
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/mm.h>
18#include <linux/init.h>
19#include <linux/serial.h>
20#include <linux/sched.h>
21#include <linux/tty.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/serial_core.h>
24#include <linux/bootmem.h>
25#include <linux/interrupt.h>
26#include <linux/bitops.h>
27#include <linux/time.h>
28#include <linux/timex.h>
Kevin Hilman84904d02006-09-22 00:58:57 +010029#include <linux/clocksource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Milan Svobodae520a362006-12-01 11:36:41 +010031#include <asm/arch/udc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/hardware.h>
33#include <asm/uaccess.h>
34#include <asm/io.h>
35#include <asm/pgtable.h>
36#include <asm/page.h>
37#include <asm/irq.h>
38
39#include <asm/mach/map.h>
40#include <asm/mach/irq.h>
41#include <asm/mach/time.h>
42
Kevin Hilmanf9a8ca12006-12-06 00:45:07 +010043static int __init ixp4xx_clocksource_init(void);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*************************************************************************
46 * IXP4xx chipset I/O mapping
47 *************************************************************************/
48static struct map_desc ixp4xx_io_desc[] __initdata = {
49 { /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
50 .virtual = IXP4XX_PERIPHERAL_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010051 .pfn = __phys_to_pfn(IXP4XX_PERIPHERAL_BASE_PHYS),
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .length = IXP4XX_PERIPHERAL_REGION_SIZE,
53 .type = MT_DEVICE
54 }, { /* Expansion Bus Config Registers */
55 .virtual = IXP4XX_EXP_CFG_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010056 .pfn = __phys_to_pfn(IXP4XX_EXP_CFG_BASE_PHYS),
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .length = IXP4XX_EXP_CFG_REGION_SIZE,
58 .type = MT_DEVICE
59 }, { /* PCI Registers */
60 .virtual = IXP4XX_PCI_CFG_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010061 .pfn = __phys_to_pfn(IXP4XX_PCI_CFG_BASE_PHYS),
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .length = IXP4XX_PCI_CFG_REGION_SIZE,
63 .type = MT_DEVICE
Deepak Saxena5932ae32005-06-24 20:54:35 +010064 },
65#ifdef CONFIG_DEBUG_LL
66 { /* Debug UART mapping */
67 .virtual = IXP4XX_DEBUG_UART_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010068 .pfn = __phys_to_pfn(IXP4XX_DEBUG_UART_BASE_PHYS),
Deepak Saxena5932ae32005-06-24 20:54:35 +010069 .length = IXP4XX_DEBUG_UART_REGION_SIZE,
70 .type = MT_DEVICE
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
Deepak Saxena5932ae32005-06-24 20:54:35 +010072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75void __init ixp4xx_map_io(void)
76{
77 iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
78}
79
80
81/*************************************************************************
82 * IXP4xx chipset IRQ handling
83 *
84 * TODO: GPIO IRQs should be marked invalid until the user of the IRQ
85 * (be it PCI or something else) configures that GPIO line
86 * as an IRQ.
87 **************************************************************************/
Deepak Saxenabdf82b52005-08-29 22:46:30 +010088enum ixp4xx_irq_type {
89 IXP4XX_IRQ_LEVEL, IXP4XX_IRQ_EDGE
90};
91
Kevin Hilman984d1152006-11-03 01:47:20 +010092/* Each bit represents an IRQ: 1: edge-triggered, 0: level triggered */
93static unsigned long long ixp4xx_irq_edge = 0;
Deepak Saxenabdf82b52005-08-29 22:46:30 +010094
95/*
96 * IRQ -> GPIO mapping table
97 */
Lennert Buytenhek6cc1b652006-04-20 21:24:38 +010098static signed char irq2gpio[32] = {
Deepak Saxenabdf82b52005-08-29 22:46:30 +010099 -1, -1, -1, -1, -1, -1, 0, 1,
100 -1, -1, -1, -1, -1, -1, -1, -1,
101 -1, -1, -1, 2, 3, 4, 5, 6,
102 7, 8, 9, 10, 11, 12, -1, -1,
103};
104
Milan Svoboda25735d12007-03-21 14:04:08 +0100105int gpio_to_irq(int gpio)
106{
107 int irq;
108
109 for (irq = 0; irq < 32; irq++) {
110 if (irq2gpio[irq] == gpio)
111 return irq;
112 }
113 return -EINVAL;
114}
115EXPORT_SYMBOL(gpio_to_irq);
116
117int irq_to_gpio(int irq)
118{
119 int gpio = (irq < 32) ? irq2gpio[irq] : -EINVAL;
120
121 if (gpio == -1)
122 return -EINVAL;
123
124 return gpio;
125}
126EXPORT_SYMBOL(irq_to_gpio);
127
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100128static int ixp4xx_set_irq_type(unsigned int irq, unsigned int type)
129{
130 int line = irq2gpio[irq];
131 u32 int_style;
132 enum ixp4xx_irq_type irq_type;
133 volatile u32 *int_reg;
134
135 /*
136 * Only for GPIO IRQs
137 */
138 if (line < 0)
139 return -EINVAL;
140
Mårten Wikström06e44792006-02-22 22:27:23 +0000141 switch (type){
142 case IRQT_BOTHEDGE:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100143 int_style = IXP4XX_GPIO_STYLE_TRANSITIONAL;
144 irq_type = IXP4XX_IRQ_EDGE;
Mårten Wikström06e44792006-02-22 22:27:23 +0000145 break;
146 case IRQT_RISING:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100147 int_style = IXP4XX_GPIO_STYLE_RISING_EDGE;
148 irq_type = IXP4XX_IRQ_EDGE;
Mårten Wikström06e44792006-02-22 22:27:23 +0000149 break;
150 case IRQT_FALLING:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100151 int_style = IXP4XX_GPIO_STYLE_FALLING_EDGE;
152 irq_type = IXP4XX_IRQ_EDGE;
Mårten Wikström06e44792006-02-22 22:27:23 +0000153 break;
154 case IRQT_HIGH:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100155 int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
156 irq_type = IXP4XX_IRQ_LEVEL;
Mårten Wikström06e44792006-02-22 22:27:23 +0000157 break;
158 case IRQT_LOW:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100159 int_style = IXP4XX_GPIO_STYLE_ACTIVE_LOW;
160 irq_type = IXP4XX_IRQ_LEVEL;
Mårten Wikström06e44792006-02-22 22:27:23 +0000161 break;
162 default:
David Vrabel6132f9e2005-09-26 19:52:56 +0100163 return -EINVAL;
Mårten Wikström06e44792006-02-22 22:27:23 +0000164 }
Kevin Hilman984d1152006-11-03 01:47:20 +0100165
166 if (irq_type == IXP4XX_IRQ_EDGE)
167 ixp4xx_irq_edge |= (1 << irq);
168 else
169 ixp4xx_irq_edge &= ~(1 << irq);
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100170
171 if (line >= 8) { /* pins 8-15 */
172 line -= 8;
173 int_reg = IXP4XX_GPIO_GPIT2R;
174 } else { /* pins 0-7 */
175 int_reg = IXP4XX_GPIO_GPIT1R;
176 }
177
178 /* Clear the style for the appropriate pin */
179 *int_reg &= ~(IXP4XX_GPIO_STYLE_CLEAR <<
180 (line * IXP4XX_GPIO_STYLE_SIZE));
181
Deepak Saxenaf7e8bbb82006-01-04 17:17:10 +0000182 *IXP4XX_GPIO_GPISR = (1 << line);
183
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100184 /* Set the new style */
185 *int_reg |= (int_style << (line * IXP4XX_GPIO_STYLE_SIZE));
David Vrabel6132f9e2005-09-26 19:52:56 +0100186
Alessandro Zummo73deb7d2006-03-20 17:10:12 +0000187 /* Configure the line as an input */
188 gpio_line_config(line, IXP4XX_GPIO_IN);
189
David Vrabel6132f9e2005-09-26 19:52:56 +0100190 return 0;
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static void ixp4xx_irq_mask(unsigned int irq)
194{
195 if (cpu_is_ixp46x() && irq >= 32)
196 *IXP4XX_ICMR2 &= ~(1 << (irq - 32));
197 else
198 *IXP4XX_ICMR &= ~(1 << irq);
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void ixp4xx_irq_ack(unsigned int irq)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int line = (irq < 32) ? irq2gpio[irq] : -1;
204
205 if (line >= 0)
Deepak Saxenaf7e8bbb82006-01-04 17:17:10 +0000206 *IXP4XX_GPIO_GPISR = (1 << line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/*
210 * Level triggered interrupts on GPIO lines can only be cleared when the
211 * interrupt condition disappears.
212 */
Kevin Hilman984d1152006-11-03 01:47:20 +0100213static void ixp4xx_irq_unmask(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Kevin Hilman984d1152006-11-03 01:47:20 +0100215 if (!(ixp4xx_irq_edge & (1 << irq)))
216 ixp4xx_irq_ack(irq);
217
218 if (cpu_is_ixp46x() && irq >= 32)
219 *IXP4XX_ICMR2 |= (1 << (irq - 32));
220 else
221 *IXP4XX_ICMR |= (1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Russell King10dd5ce2006-11-23 11:41:32 +0000224static struct irq_chip ixp4xx_irq_chip = {
Kevin Hilman984d1152006-11-03 01:47:20 +0100225 .name = "IXP4xx",
Russell King2be863c2005-09-06 23:13:17 +0100226 .ack = ixp4xx_irq_ack,
227 .mask = ixp4xx_irq_mask,
228 .unmask = ixp4xx_irq_unmask,
229 .set_type = ixp4xx_set_irq_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232void __init ixp4xx_init_irq(void)
233{
234 int i = 0;
235
236 /* Route all sources to IRQ instead of FIQ */
237 *IXP4XX_ICLR = 0x0;
238
239 /* Disable all interrupt */
240 *IXP4XX_ICMR = 0x0;
241
242 if (cpu_is_ixp46x()) {
243 /* Route upper 32 sources to IRQ instead of FIQ */
244 *IXP4XX_ICLR2 = 0x00;
245
246 /* Disable upper 32 interrupts */
247 *IXP4XX_ICMR2 = 0x00;
248 }
249
250 /* Default to all level triggered */
Kevin Hilman984d1152006-11-03 01:47:20 +0100251 for(i = 0; i < NR_IRQS; i++) {
252 set_irq_chip(i, &ixp4xx_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000253 set_irq_handler(i, handle_level_irq);
Kevin Hilman984d1152006-11-03 01:47:20 +0100254 set_irq_flags(i, IRQF_VALID);
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258
259/*************************************************************************
260 * IXP4xx timer tick
261 * We use OS timer1 on the CPU for the timer tick and the timestamp
262 * counter as a source of real clock ticks to account for missed jiffies.
263 *************************************************************************/
264
265static unsigned volatile last_jiffy_time;
266
267#define CLOCK_TICKS_PER_USEC ((CLOCK_TICK_RATE + USEC_PER_SEC/2) / USEC_PER_SEC)
268
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700269static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 write_seqlock(&xtime_lock);
272
273 /* Clear Pending Interrupt by writing '1' to it */
274 *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
275
276 /*
277 * Catch up with the real idea of time
278 */
Lennert Buytenhekf869afa2006-06-22 10:30:53 +0100279 while ((signed long)(*IXP4XX_OSTS - last_jiffy_time) >= LATCH) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700280 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 last_jiffy_time += LATCH;
282 }
283
284 write_sequnlock(&xtime_lock);
285
286 return IRQ_HANDLED;
287}
288
289static struct irqaction ixp4xx_timer_irq = {
290 .name = "IXP4xx Timer Tick",
Thomas Gleixner52e405e2006-07-03 02:20:05 +0200291 .flags = IRQF_DISABLED | IRQF_TIMER,
Russell King09b8b5f2005-06-26 17:06:36 +0100292 .handler = ixp4xx_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
295static void __init ixp4xx_timer_init(void)
296{
297 /* Clear Pending Interrupt by writing '1' to it */
298 *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
299
300 /* Setup the Timer counter value */
301 *IXP4XX_OSRT1 = (LATCH & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
302
303 /* Reset time-stamp counter */
304 *IXP4XX_OSTS = 0;
305 last_jiffy_time = 0;
306
307 /* Connect the interrupt handler and enable the interrupt */
308 setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
Kevin Hilmanf9a8ca12006-12-06 00:45:07 +0100309
310 ixp4xx_clocksource_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313struct sys_timer ixp4xx_timer = {
314 .init = ixp4xx_timer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Milan Svobodae520a362006-12-01 11:36:41 +0100317static struct pxa2xx_udc_mach_info ixp4xx_udc_info;
318
319void __init ixp4xx_set_udc_info(struct pxa2xx_udc_mach_info *info)
320{
321 memcpy(&ixp4xx_udc_info, info, sizeof *info);
322}
323
324static struct resource ixp4xx_udc_resources[] = {
325 [0] = {
326 .start = 0xc800b000,
327 .end = 0xc800bfff,
328 .flags = IORESOURCE_MEM,
329 },
330 [1] = {
331 .start = IRQ_IXP4XX_USB,
332 .end = IRQ_IXP4XX_USB,
333 .flags = IORESOURCE_IRQ,
334 },
335};
336
337/*
338 * USB device controller. The IXP4xx uses the same controller as PXA2XX,
339 * so we just use the same device.
340 */
341static struct platform_device ixp4xx_udc_device = {
342 .name = "pxa2xx-udc",
343 .id = -1,
344 .num_resources = 2,
345 .resource = ixp4xx_udc_resources,
346 .dev = {
347 .platform_data = &ixp4xx_udc_info,
348 },
349};
350
351static struct platform_device *ixp4xx_devices[] __initdata = {
352 &ixp4xx_udc_device,
353};
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static struct resource ixp46x_i2c_resources[] = {
356 [0] = {
357 .start = 0xc8011000,
358 .end = 0xc801101c,
359 .flags = IORESOURCE_MEM,
360 },
361 [1] = {
362 .start = IRQ_IXP4XX_I2C,
363 .end = IRQ_IXP4XX_I2C,
364 .flags = IORESOURCE_IRQ
365 }
366};
367
368/*
369 * I2C controller. The IXP46x uses the same block as the IOP3xx, so
370 * we just use the same device name.
371 */
372static struct platform_device ixp46x_i2c_controller = {
373 .name = "IOP3xx-I2C",
374 .id = 0,
375 .num_resources = 2,
376 .resource = ixp46x_i2c_resources
377};
378
379static struct platform_device *ixp46x_devices[] __initdata = {
380 &ixp46x_i2c_controller
381};
382
Deepak Saxena54e269e2006-01-05 20:59:29 +0000383unsigned long ixp4xx_exp_bus_size;
David Vrabel1e74c892006-01-18 22:46:43 +0000384EXPORT_SYMBOL(ixp4xx_exp_bus_size);
Deepak Saxena54e269e2006-01-05 20:59:29 +0000385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386void __init ixp4xx_sys_init(void)
387{
Deepak Saxena54e269e2006-01-05 20:59:29 +0000388 ixp4xx_exp_bus_size = SZ_16M;
389
Milan Svobodae520a362006-12-01 11:36:41 +0100390 platform_add_devices(ixp4xx_devices, ARRAY_SIZE(ixp4xx_devices));
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (cpu_is_ixp46x()) {
Deepak Saxena54e269e2006-01-05 20:59:29 +0000393 int region;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 platform_add_devices(ixp46x_devices,
396 ARRAY_SIZE(ixp46x_devices));
Deepak Saxena54e269e2006-01-05 20:59:29 +0000397
398 for (region = 0; region < 7; region++) {
399 if((*(IXP4XX_EXP_REG(0x4 * region)) & 0x200)) {
400 ixp4xx_exp_bus_size = SZ_32M;
401 break;
402 }
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Deepak Saxena54e269e2006-01-05 20:59:29 +0000405
David Vrabel1e74c892006-01-18 22:46:43 +0000406 printk("IXP4xx: Using %luMiB expansion bus window size\n",
Deepak Saxena54e269e2006-01-05 20:59:29 +0000407 ixp4xx_exp_bus_size >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Kevin Hilman84904d02006-09-22 00:58:57 +0100410cycle_t ixp4xx_get_cycles(void)
411{
412 return *IXP4XX_OSTS;
413}
414
415static struct clocksource clocksource_ixp4xx = {
416 .name = "OSTS",
417 .rating = 200,
418 .read = ixp4xx_get_cycles,
419 .mask = CLOCKSOURCE_MASK(32),
420 .shift = 20,
Thomas Gleixnerc66699a2007-02-16 01:27:37 -0800421 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Kevin Hilman84904d02006-09-22 00:58:57 +0100422};
423
424unsigned long ixp4xx_timer_freq = FREQ;
425static int __init ixp4xx_clocksource_init(void)
426{
427 clocksource_ixp4xx.mult =
428 clocksource_hz2mult(ixp4xx_timer_freq,
429 clocksource_ixp4xx.shift);
430 clocksource_register(&clocksource_ixp4xx);
431
432 return 0;
433}