ARM: ixp4xx: Switch to use new IRQ+GPIO drivers
[sfrench/cifs-2.6.git] / arch / arm / mach-ixp4xx / common.c
1 /*
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/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/init.h>
19 #include <linux/serial.h>
20 #include <linux/tty.h>
21 #include <linux/platform_device.h>
22 #include <linux/serial_core.h>
23 #include <linux/interrupt.h>
24 #include <linux/bitops.h>
25 #include <linux/time.h>
26 #include <linux/clocksource.h>
27 #include <linux/clockchips.h>
28 #include <linux/io.h>
29 #include <linux/export.h>
30 #include <linux/cpu.h>
31 #include <linux/pci.h>
32 #include <linux/sched_clock.h>
33 #include <linux/bitops.h>
34 #include <linux/irqchip/irq-ixp4xx.h>
35 #include <mach/udc.h>
36 #include <mach/hardware.h>
37 #include <mach/io.h>
38 #include <linux/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/page.h>
41 #include <asm/exception.h>
42 #include <asm/irq.h>
43 #include <asm/system_misc.h>
44 #include <asm/mach/map.h>
45 #include <asm/mach/irq.h>
46 #include <asm/mach/time.h>
47
48 #include "irqs.h"
49
50 #define IXP4XX_TIMER_FREQ 66666000
51
52 /*
53  * The timer register doesn't allow to specify the two least significant bits of
54  * the timeout value and assumes them being zero. So make sure IXP4XX_LATCH is
55  * the best value with the two least significant bits unset.
56  */
57 #define IXP4XX_LATCH DIV_ROUND_CLOSEST(IXP4XX_TIMER_FREQ, \
58                                        (IXP4XX_OST_RELOAD_MASK + 1) * HZ) * \
59                         (IXP4XX_OST_RELOAD_MASK + 1)
60
61 static void __init ixp4xx_clocksource_init(void);
62 static void __init ixp4xx_clockevent_init(void);
63 static struct clock_event_device clockevent_ixp4xx;
64
65 /*************************************************************************
66  * IXP4xx chipset I/O mapping
67  *************************************************************************/
68 static struct map_desc ixp4xx_io_desc[] __initdata = {
69         {       /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
70                 .virtual        = (unsigned long)IXP4XX_PERIPHERAL_BASE_VIRT,
71                 .pfn            = __phys_to_pfn(IXP4XX_PERIPHERAL_BASE_PHYS),
72                 .length         = IXP4XX_PERIPHERAL_REGION_SIZE,
73                 .type           = MT_DEVICE
74         }, {    /* Expansion Bus Config Registers */
75                 .virtual        = (unsigned long)IXP4XX_EXP_CFG_BASE_VIRT,
76                 .pfn            = __phys_to_pfn(IXP4XX_EXP_CFG_BASE_PHYS),
77                 .length         = IXP4XX_EXP_CFG_REGION_SIZE,
78                 .type           = MT_DEVICE
79         }, {    /* PCI Registers */
80                 .virtual        = (unsigned long)IXP4XX_PCI_CFG_BASE_VIRT,
81                 .pfn            = __phys_to_pfn(IXP4XX_PCI_CFG_BASE_PHYS),
82                 .length         = IXP4XX_PCI_CFG_REGION_SIZE,
83                 .type           = MT_DEVICE
84         }, {    /* Queue Manager */
85                 .virtual        = (unsigned long)IXP4XX_QMGR_BASE_VIRT,
86                 .pfn            = __phys_to_pfn(IXP4XX_QMGR_BASE_PHYS),
87                 .length         = IXP4XX_QMGR_REGION_SIZE,
88                 .type           = MT_DEVICE
89         },
90 };
91
92 void __init ixp4xx_map_io(void)
93 {
94         iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
95 }
96
97 void __init ixp4xx_init_irq(void)
98 {
99         /*
100          * ixp4xx does not implement the XScale PWRMODE register
101          * so it must not call cpu_do_idle().
102          */
103         cpu_idle_poll_ctrl(true);
104
105         ixp4xx_irq_init(IXP4XX_INTC_BASE_PHYS,
106                         (cpu_is_ixp46x() || cpu_is_ixp43x()));
107 }
108
109 /*************************************************************************
110  * IXP4xx timer tick
111  * We use OS timer1 on the CPU for the timer tick and the timestamp 
112  * counter as a source of real clock ticks to account for missed jiffies.
113  *************************************************************************/
114
115 static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
116 {
117         struct clock_event_device *evt = dev_id;
118
119         /* Clear Pending Interrupt by writing '1' to it */
120         *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
121
122         evt->event_handler(evt);
123
124         return IRQ_HANDLED;
125 }
126
127 void __init ixp4xx_timer_init(void)
128 {
129         /* Reset/disable counter */
130         *IXP4XX_OSRT1 = 0;
131
132         /* Clear Pending Interrupt by writing '1' to it */
133         *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
134
135         /* Reset time-stamp counter */
136         *IXP4XX_OSTS = 0;
137
138         ixp4xx_clocksource_init();
139         ixp4xx_clockevent_init();
140 }
141
142 static struct pxa2xx_udc_mach_info ixp4xx_udc_info;
143
144 void __init ixp4xx_set_udc_info(struct pxa2xx_udc_mach_info *info)
145 {
146         memcpy(&ixp4xx_udc_info, info, sizeof *info);
147 }
148
149 static struct resource ixp4xx_udc_resources[] = {
150         [0] = {
151                 .start  = 0xc800b000,
152                 .end    = 0xc800bfff,
153                 .flags  = IORESOURCE_MEM,
154         },
155         [1] = {
156                 .start  = IRQ_IXP4XX_USB,
157                 .end    = IRQ_IXP4XX_USB,
158                 .flags  = IORESOURCE_IRQ,
159         },
160 };
161
162 static struct resource ixp4xx_gpio_resource[] = {
163         {
164                 .start = IXP4XX_GPIO_BASE_PHYS,
165                 .end = IXP4XX_GPIO_BASE_PHYS + 0xfff,
166                 .flags = IORESOURCE_MEM,
167         },
168 };
169
170 static struct platform_device ixp4xx_gpio_device = {
171         .name           = "ixp4xx-gpio",
172         .id             = -1,
173         .dev = {
174                 .coherent_dma_mask      = DMA_BIT_MASK(32),
175         },
176         .resource = ixp4xx_gpio_resource,
177         .num_resources  = ARRAY_SIZE(ixp4xx_gpio_resource),
178 };
179
180 /*
181  * USB device controller. The IXP4xx uses the same controller as PXA25X,
182  * so we just use the same device.
183  */
184 static struct platform_device ixp4xx_udc_device = {
185         .name           = "pxa25x-udc",
186         .id             = -1,
187         .num_resources  = 2,
188         .resource       = ixp4xx_udc_resources,
189         .dev            = {
190                 .platform_data = &ixp4xx_udc_info,
191         },
192 };
193
194 static struct platform_device *ixp4xx_devices[] __initdata = {
195         &ixp4xx_gpio_device,
196         &ixp4xx_udc_device,
197 };
198
199 static struct resource ixp46x_i2c_resources[] = {
200         [0] = {
201                 .start  = 0xc8011000,
202                 .end    = 0xc801101c,
203                 .flags  = IORESOURCE_MEM,
204         },
205         [1] = {
206                 .start  = IRQ_IXP4XX_I2C,
207                 .end    = IRQ_IXP4XX_I2C,
208                 .flags  = IORESOURCE_IRQ
209         }
210 };
211
212 /*
213  * I2C controller. The IXP46x uses the same block as the IOP3xx, so
214  * we just use the same device name.
215  */
216 static struct platform_device ixp46x_i2c_controller = {
217         .name           = "IOP3xx-I2C",
218         .id             = 0,
219         .num_resources  = 2,
220         .resource       = ixp46x_i2c_resources
221 };
222
223 static struct platform_device *ixp46x_devices[] __initdata = {
224         &ixp46x_i2c_controller
225 };
226
227 unsigned long ixp4xx_exp_bus_size;
228 EXPORT_SYMBOL(ixp4xx_exp_bus_size);
229
230 void __init ixp4xx_sys_init(void)
231 {
232         ixp4xx_exp_bus_size = SZ_16M;
233
234         platform_add_devices(ixp4xx_devices, ARRAY_SIZE(ixp4xx_devices));
235
236         if (cpu_is_ixp46x()) {
237                 int region;
238
239                 platform_add_devices(ixp46x_devices,
240                                 ARRAY_SIZE(ixp46x_devices));
241
242                 for (region = 0; region < 7; region++) {
243                         if((*(IXP4XX_EXP_REG(0x4 * region)) & 0x200)) {
244                                 ixp4xx_exp_bus_size = SZ_32M;
245                                 break;
246                         }
247                 }
248         }
249
250         printk("IXP4xx: Using %luMiB expansion bus window size\n",
251                         ixp4xx_exp_bus_size >> 20);
252 }
253
254 /*
255  * sched_clock()
256  */
257 static u64 notrace ixp4xx_read_sched_clock(void)
258 {
259         return *IXP4XX_OSTS;
260 }
261
262 /*
263  * clocksource
264  */
265
266 static u64 ixp4xx_clocksource_read(struct clocksource *c)
267 {
268         return *IXP4XX_OSTS;
269 }
270
271 unsigned long ixp4xx_timer_freq = IXP4XX_TIMER_FREQ;
272 EXPORT_SYMBOL(ixp4xx_timer_freq);
273 static void __init ixp4xx_clocksource_init(void)
274 {
275         sched_clock_register(ixp4xx_read_sched_clock, 32, ixp4xx_timer_freq);
276
277         clocksource_mmio_init(NULL, "OSTS", ixp4xx_timer_freq, 200, 32,
278                         ixp4xx_clocksource_read);
279 }
280
281 /*
282  * clockevents
283  */
284 static int ixp4xx_set_next_event(unsigned long evt,
285                                  struct clock_event_device *unused)
286 {
287         unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
288
289         *IXP4XX_OSRT1 = (evt & ~IXP4XX_OST_RELOAD_MASK) | opts;
290
291         return 0;
292 }
293
294 static int ixp4xx_shutdown(struct clock_event_device *evt)
295 {
296         unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
297         unsigned long osrt = *IXP4XX_OSRT1 & ~IXP4XX_OST_RELOAD_MASK;
298
299         opts &= ~IXP4XX_OST_ENABLE;
300         *IXP4XX_OSRT1 = osrt | opts;
301         return 0;
302 }
303
304 static int ixp4xx_set_oneshot(struct clock_event_device *evt)
305 {
306         unsigned long opts = IXP4XX_OST_ENABLE | IXP4XX_OST_ONE_SHOT;
307         unsigned long osrt = 0;
308
309         /* period set by 'set next_event' */
310         *IXP4XX_OSRT1 = osrt | opts;
311         return 0;
312 }
313
314 static int ixp4xx_set_periodic(struct clock_event_device *evt)
315 {
316         unsigned long opts = IXP4XX_OST_ENABLE;
317         unsigned long osrt = IXP4XX_LATCH & ~IXP4XX_OST_RELOAD_MASK;
318
319         *IXP4XX_OSRT1 = osrt | opts;
320         return 0;
321 }
322
323 static int ixp4xx_resume(struct clock_event_device *evt)
324 {
325         unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
326         unsigned long osrt = *IXP4XX_OSRT1 & ~IXP4XX_OST_RELOAD_MASK;
327
328         opts |= IXP4XX_OST_ENABLE;
329         *IXP4XX_OSRT1 = osrt | opts;
330         return 0;
331 }
332
333 static struct clock_event_device clockevent_ixp4xx = {
334         .name                   = "ixp4xx timer1",
335         .features               = CLOCK_EVT_FEAT_PERIODIC |
336                                   CLOCK_EVT_FEAT_ONESHOT,
337         .rating                 = 200,
338         .set_state_shutdown     = ixp4xx_shutdown,
339         .set_state_periodic     = ixp4xx_set_periodic,
340         .set_state_oneshot      = ixp4xx_set_oneshot,
341         .tick_resume            = ixp4xx_resume,
342         .set_next_event         = ixp4xx_set_next_event,
343 };
344
345 static void __init ixp4xx_clockevent_init(void)
346 {
347         int ret;
348
349         clockevent_ixp4xx.cpumask = cpumask_of(0);
350         clockevent_ixp4xx.irq = IRQ_IXP4XX_TIMER1;
351         ret = request_irq(IRQ_IXP4XX_TIMER1, ixp4xx_timer_interrupt,
352                           IRQF_TIMER, "IXP4XX-TIMER1", &clockevent_ixp4xx);
353         if (ret) {
354                 pr_crit("no timer IRQ\n");
355                 return;
356         }
357         clockevents_config_and_register(&clockevent_ixp4xx, IXP4XX_TIMER_FREQ,
358                                         0xf, 0xfffffffe);
359 }
360
361 void ixp4xx_restart(enum reboot_mode mode, const char *cmd)
362 {
363         if (mode == REBOOT_SOFT) {
364                 /* Jump into ROM at address 0 */
365                 soft_restart(0);
366         } else {
367                 /* Use on-chip reset capability */
368
369                 /* set the "key" register to enable access to
370                  * "timer" and "enable" registers
371                  */
372                 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
373
374                 /* write 0 to the timer register for an immediate reset */
375                 *IXP4XX_OSWT = 0;
376
377                 *IXP4XX_OSWE = IXP4XX_WDT_RESET_ENABLE | IXP4XX_WDT_COUNT_ENABLE;
378         }
379 }
380
381 #ifdef CONFIG_PCI
382 static int ixp4xx_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
383 {
384         return (dma_addr + size) > SZ_64M;
385 }
386
387 static int ixp4xx_platform_notify_remove(struct device *dev)
388 {
389         if (dev_is_pci(dev))
390                 dmabounce_unregister_dev(dev);
391
392         return 0;
393 }
394 #endif
395
396 /*
397  * Setup DMA mask to 64MB on PCI devices and 4 GB on all other things.
398  */
399 static int ixp4xx_platform_notify(struct device *dev)
400 {
401         dev->dma_mask = &dev->coherent_dma_mask;
402
403 #ifdef CONFIG_PCI
404         if (dev_is_pci(dev)) {
405                 dev->coherent_dma_mask = DMA_BIT_MASK(28); /* 64 MB */
406                 dmabounce_register_dev(dev, 2048, 4096, ixp4xx_needs_bounce);
407                 return 0;
408         }
409 #endif
410
411         dev->coherent_dma_mask = DMA_BIT_MASK(32);
412         return 0;
413 }
414
415 int dma_set_coherent_mask(struct device *dev, u64 mask)
416 {
417         if (dev_is_pci(dev))
418                 mask &= DMA_BIT_MASK(28); /* 64 MB */
419
420         if ((mask & DMA_BIT_MASK(28)) == DMA_BIT_MASK(28)) {
421                 dev->coherent_dma_mask = mask;
422                 return 0;
423         }
424
425         return -EIO;            /* device wanted sub-64MB mask */
426 }
427 EXPORT_SYMBOL(dma_set_coherent_mask);
428
429 #ifdef CONFIG_IXP4XX_INDIRECT_PCI
430 /*
431  * In the case of using indirect PCI, we simply return the actual PCI
432  * address and our read/write implementation use that to drive the
433  * access registers. If something outside of PCI is ioremap'd, we
434  * fallback to the default.
435  */
436
437 static void __iomem *ixp4xx_ioremap_caller(phys_addr_t addr, size_t size,
438                                            unsigned int mtype, void *caller)
439 {
440         if (!is_pci_memory(addr))
441                 return __arm_ioremap_caller(addr, size, mtype, caller);
442
443         return (void __iomem *)addr;
444 }
445
446 static void ixp4xx_iounmap(volatile void __iomem *addr)
447 {
448         if (!is_pci_memory((__force u32)addr))
449                 __iounmap(addr);
450 }
451 #endif
452
453 void __init ixp4xx_init_early(void)
454 {
455         platform_notify = ixp4xx_platform_notify;
456 #ifdef CONFIG_PCI
457         platform_notify_remove = ixp4xx_platform_notify_remove;
458 #endif
459 #ifdef CONFIG_IXP4XX_INDIRECT_PCI
460         arch_ioremap_caller = ixp4xx_ioremap_caller;
461         arch_iounmap = ixp4xx_iounmap;
462 #endif
463 }