blob: fbadf3021b9ea0e70074d4388ee127df012dfc4b [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
16#include <linux/config.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/init.h>
20#include <linux/serial.h>
21#include <linux/sched.h>
22#include <linux/tty.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010023#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/serial_core.h>
25#include <linux/bootmem.h>
26#include <linux/interrupt.h>
27#include <linux/bitops.h>
28#include <linux/time.h>
29#include <linux/timex.h>
30
31#include <asm/hardware.h>
32#include <asm/uaccess.h>
33#include <asm/io.h>
34#include <asm/pgtable.h>
35#include <asm/page.h>
36#include <asm/irq.h>
37
38#include <asm/mach/map.h>
39#include <asm/mach/irq.h>
40#include <asm/mach/time.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*************************************************************************
43 * IXP4xx chipset I/O mapping
44 *************************************************************************/
45static struct map_desc ixp4xx_io_desc[] __initdata = {
46 { /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
47 .virtual = IXP4XX_PERIPHERAL_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010048 .pfn = __phys_to_pfn(IXP4XX_PERIPHERAL_BASE_PHYS),
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .length = IXP4XX_PERIPHERAL_REGION_SIZE,
50 .type = MT_DEVICE
51 }, { /* Expansion Bus Config Registers */
52 .virtual = IXP4XX_EXP_CFG_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010053 .pfn = __phys_to_pfn(IXP4XX_EXP_CFG_BASE_PHYS),
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .length = IXP4XX_EXP_CFG_REGION_SIZE,
55 .type = MT_DEVICE
56 }, { /* PCI Registers */
57 .virtual = IXP4XX_PCI_CFG_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010058 .pfn = __phys_to_pfn(IXP4XX_PCI_CFG_BASE_PHYS),
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .length = IXP4XX_PCI_CFG_REGION_SIZE,
60 .type = MT_DEVICE
Deepak Saxena5932ae32005-06-24 20:54:35 +010061 },
62#ifdef CONFIG_DEBUG_LL
63 { /* Debug UART mapping */
64 .virtual = IXP4XX_DEBUG_UART_BASE_VIRT,
Deepak Saxena87fe04b2005-10-28 15:18:59 +010065 .pfn = __phys_to_pfn(IXP4XX_DEBUG_UART_BASE_PHYS),
Deepak Saxena5932ae32005-06-24 20:54:35 +010066 .length = IXP4XX_DEBUG_UART_REGION_SIZE,
67 .type = MT_DEVICE
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Deepak Saxena5932ae32005-06-24 20:54:35 +010069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
72void __init ixp4xx_map_io(void)
73{
74 iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
75}
76
77
78/*************************************************************************
79 * IXP4xx chipset IRQ handling
80 *
81 * TODO: GPIO IRQs should be marked invalid until the user of the IRQ
82 * (be it PCI or something else) configures that GPIO line
83 * as an IRQ.
84 **************************************************************************/
Deepak Saxenabdf82b52005-08-29 22:46:30 +010085enum ixp4xx_irq_type {
86 IXP4XX_IRQ_LEVEL, IXP4XX_IRQ_EDGE
87};
88
89static void ixp4xx_config_irq(unsigned irq, enum ixp4xx_irq_type type);
90
91/*
92 * IRQ -> GPIO mapping table
93 */
94static int irq2gpio[32] = {
95 -1, -1, -1, -1, -1, -1, 0, 1,
96 -1, -1, -1, -1, -1, -1, -1, -1,
97 -1, -1, -1, 2, 3, 4, 5, 6,
98 7, 8, 9, 10, 11, 12, -1, -1,
99};
100
101static int ixp4xx_set_irq_type(unsigned int irq, unsigned int type)
102{
103 int line = irq2gpio[irq];
104 u32 int_style;
105 enum ixp4xx_irq_type irq_type;
106 volatile u32 *int_reg;
107
108 /*
109 * Only for GPIO IRQs
110 */
111 if (line < 0)
112 return -EINVAL;
113
Mårten Wikström06e44792006-02-22 22:27:23 +0000114 switch (type){
115 case IRQT_BOTHEDGE:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100116 int_style = IXP4XX_GPIO_STYLE_TRANSITIONAL;
117 irq_type = IXP4XX_IRQ_EDGE;
Mårten Wikström06e44792006-02-22 22:27:23 +0000118 break;
119 case IRQT_RISING:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100120 int_style = IXP4XX_GPIO_STYLE_RISING_EDGE;
121 irq_type = IXP4XX_IRQ_EDGE;
Mårten Wikström06e44792006-02-22 22:27:23 +0000122 break;
123 case IRQT_FALLING:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100124 int_style = IXP4XX_GPIO_STYLE_FALLING_EDGE;
125 irq_type = IXP4XX_IRQ_EDGE;
Mårten Wikström06e44792006-02-22 22:27:23 +0000126 break;
127 case IRQT_HIGH:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100128 int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
129 irq_type = IXP4XX_IRQ_LEVEL;
Mårten Wikström06e44792006-02-22 22:27:23 +0000130 break;
131 case IRQT_LOW:
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100132 int_style = IXP4XX_GPIO_STYLE_ACTIVE_LOW;
133 irq_type = IXP4XX_IRQ_LEVEL;
Mårten Wikström06e44792006-02-22 22:27:23 +0000134 break;
135 default:
David Vrabel6132f9e2005-09-26 19:52:56 +0100136 return -EINVAL;
Mårten Wikström06e44792006-02-22 22:27:23 +0000137 }
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100138 ixp4xx_config_irq(irq, irq_type);
139
140 if (line >= 8) { /* pins 8-15 */
141 line -= 8;
142 int_reg = IXP4XX_GPIO_GPIT2R;
143 } else { /* pins 0-7 */
144 int_reg = IXP4XX_GPIO_GPIT1R;
145 }
146
147 /* Clear the style for the appropriate pin */
148 *int_reg &= ~(IXP4XX_GPIO_STYLE_CLEAR <<
149 (line * IXP4XX_GPIO_STYLE_SIZE));
150
Deepak Saxenaf7e8bbb82006-01-04 17:17:10 +0000151 *IXP4XX_GPIO_GPISR = (1 << line);
152
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100153 /* Set the new style */
154 *int_reg |= (int_style << (line * IXP4XX_GPIO_STYLE_SIZE));
David Vrabel6132f9e2005-09-26 19:52:56 +0100155
156 return 0;
Deepak Saxenabdf82b52005-08-29 22:46:30 +0100157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void ixp4xx_irq_mask(unsigned int irq)
160{
161 if (cpu_is_ixp46x() && irq >= 32)
162 *IXP4XX_ICMR2 &= ~(1 << (irq - 32));
163 else
164 *IXP4XX_ICMR &= ~(1 << irq);
165}
166
167static void ixp4xx_irq_unmask(unsigned int irq)
168{
169 if (cpu_is_ixp46x() && irq >= 32)
170 *IXP4XX_ICMR2 |= (1 << (irq - 32));
171 else
172 *IXP4XX_ICMR |= (1 << irq);
173}
174
175static void ixp4xx_irq_ack(unsigned int irq)
176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int line = (irq < 32) ? irq2gpio[irq] : -1;
178
179 if (line >= 0)
Deepak Saxenaf7e8bbb82006-01-04 17:17:10 +0000180 *IXP4XX_GPIO_GPISR = (1 << line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
184 * Level triggered interrupts on GPIO lines can only be cleared when the
185 * interrupt condition disappears.
186 */
187static void ixp4xx_irq_level_unmask(unsigned int irq)
188{
189 ixp4xx_irq_ack(irq);
190 ixp4xx_irq_unmask(irq);
191}
192
193static struct irqchip ixp4xx_irq_level_chip = {
Russell King2be863c2005-09-06 23:13:17 +0100194 .ack = ixp4xx_irq_mask,
195 .mask = ixp4xx_irq_mask,
196 .unmask = ixp4xx_irq_level_unmask,
197 .set_type = ixp4xx_set_irq_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200static struct irqchip ixp4xx_irq_edge_chip = {
Russell King2be863c2005-09-06 23:13:17 +0100201 .ack = ixp4xx_irq_ack,
202 .mask = ixp4xx_irq_mask,
203 .unmask = ixp4xx_irq_unmask,
204 .set_type = ixp4xx_set_irq_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
207static void ixp4xx_config_irq(unsigned irq, enum ixp4xx_irq_type type)
208{
209 switch (type) {
210 case IXP4XX_IRQ_LEVEL:
211 set_irq_chip(irq, &ixp4xx_irq_level_chip);
212 set_irq_handler(irq, do_level_IRQ);
213 break;
214 case IXP4XX_IRQ_EDGE:
215 set_irq_chip(irq, &ixp4xx_irq_edge_chip);
216 set_irq_handler(irq, do_edge_IRQ);
217 break;
218 }
219 set_irq_flags(irq, IRQF_VALID);
220}
221
222void __init ixp4xx_init_irq(void)
223{
224 int i = 0;
225
226 /* Route all sources to IRQ instead of FIQ */
227 *IXP4XX_ICLR = 0x0;
228
229 /* Disable all interrupt */
230 *IXP4XX_ICMR = 0x0;
231
232 if (cpu_is_ixp46x()) {
233 /* Route upper 32 sources to IRQ instead of FIQ */
234 *IXP4XX_ICLR2 = 0x00;
235
236 /* Disable upper 32 interrupts */
237 *IXP4XX_ICMR2 = 0x00;
238 }
239
240 /* Default to all level triggered */
241 for(i = 0; i < NR_IRQS; i++)
242 ixp4xx_config_irq(i, IXP4XX_IRQ_LEVEL);
243}
244
245
246/*************************************************************************
247 * IXP4xx timer tick
248 * We use OS timer1 on the CPU for the timer tick and the timestamp
249 * counter as a source of real clock ticks to account for missed jiffies.
250 *************************************************************************/
251
252static unsigned volatile last_jiffy_time;
253
254#define CLOCK_TICKS_PER_USEC ((CLOCK_TICK_RATE + USEC_PER_SEC/2) / USEC_PER_SEC)
255
256/* IRQs are disabled before entering here from do_gettimeofday() */
257static unsigned long ixp4xx_gettimeoffset(void)
258{
259 u32 elapsed;
260
261 elapsed = *IXP4XX_OSTS - last_jiffy_time;
262
263 return elapsed / CLOCK_TICKS_PER_USEC;
264}
265
266static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
267{
268 write_seqlock(&xtime_lock);
269
270 /* Clear Pending Interrupt by writing '1' to it */
271 *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
272
273 /*
274 * Catch up with the real idea of time
275 */
276 while ((*IXP4XX_OSTS - last_jiffy_time) > LATCH) {
277 timer_tick(regs);
278 last_jiffy_time += LATCH;
279 }
280
281 write_sequnlock(&xtime_lock);
282
283 return IRQ_HANDLED;
284}
285
286static struct irqaction ixp4xx_timer_irq = {
287 .name = "IXP4xx Timer Tick",
Russell King09b8b5f2005-06-26 17:06:36 +0100288 .flags = SA_INTERRUPT | SA_TIMER,
289 .handler = ixp4xx_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291
292static void __init ixp4xx_timer_init(void)
293{
294 /* Clear Pending Interrupt by writing '1' to it */
295 *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
296
297 /* Setup the Timer counter value */
298 *IXP4XX_OSRT1 = (LATCH & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
299
300 /* Reset time-stamp counter */
301 *IXP4XX_OSTS = 0;
302 last_jiffy_time = 0;
303
304 /* Connect the interrupt handler and enable the interrupt */
305 setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
306}
307
308struct sys_timer ixp4xx_timer = {
309 .init = ixp4xx_timer_init,
310 .offset = ixp4xx_gettimeoffset,
311};
312
313static struct resource ixp46x_i2c_resources[] = {
314 [0] = {
315 .start = 0xc8011000,
316 .end = 0xc801101c,
317 .flags = IORESOURCE_MEM,
318 },
319 [1] = {
320 .start = IRQ_IXP4XX_I2C,
321 .end = IRQ_IXP4XX_I2C,
322 .flags = IORESOURCE_IRQ
323 }
324};
325
326/*
327 * I2C controller. The IXP46x uses the same block as the IOP3xx, so
328 * we just use the same device name.
329 */
330static struct platform_device ixp46x_i2c_controller = {
331 .name = "IOP3xx-I2C",
332 .id = 0,
333 .num_resources = 2,
334 .resource = ixp46x_i2c_resources
335};
336
337static struct platform_device *ixp46x_devices[] __initdata = {
338 &ixp46x_i2c_controller
339};
340
Deepak Saxena54e269e2006-01-05 20:59:29 +0000341unsigned long ixp4xx_exp_bus_size;
David Vrabel1e74c892006-01-18 22:46:43 +0000342EXPORT_SYMBOL(ixp4xx_exp_bus_size);
Deepak Saxena54e269e2006-01-05 20:59:29 +0000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344void __init ixp4xx_sys_init(void)
345{
Deepak Saxena54e269e2006-01-05 20:59:29 +0000346 ixp4xx_exp_bus_size = SZ_16M;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (cpu_is_ixp46x()) {
Deepak Saxena54e269e2006-01-05 20:59:29 +0000349 int region;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 platform_add_devices(ixp46x_devices,
352 ARRAY_SIZE(ixp46x_devices));
Deepak Saxena54e269e2006-01-05 20:59:29 +0000353
354 for (region = 0; region < 7; region++) {
355 if((*(IXP4XX_EXP_REG(0x4 * region)) & 0x200)) {
356 ixp4xx_exp_bus_size = SZ_32M;
357 break;
358 }
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Deepak Saxena54e269e2006-01-05 20:59:29 +0000361
David Vrabel1e74c892006-01-18 22:46:43 +0000362 printk("IXP4xx: Using %luMiB expansion bus window size\n",
Deepak Saxena54e269e2006-01-05 20:59:29 +0000363 ixp4xx_exp_bus_size >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365