ARM: 5954/1: ep93xx: move gpio interrupt support to gpio.c
[linux-2.6.git] / arch / arm / mach-ep93xx / core.c
1 /*
2  * arch/arm/mach-ep93xx/core.c
3  * Core routines for Cirrus EP93xx chips.
4  *
5  * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
6  * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
7  *
8  * Thanks go to Michael Burian and Ray Lehtiniemi for their key
9  * role in the ep93xx linux community.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  */
16
17 #define pr_fmt(fmt) "ep93xx " KBUILD_MODNAME ": " fmt
18
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/timex.h>
25 #include <linux/irq.h>
26 #include <linux/io.h>
27 #include <linux/gpio.h>
28 #include <linux/leds.h>
29 #include <linux/termios.h>
30 #include <linux/amba/bus.h>
31 #include <linux/amba/serial.h>
32 #include <linux/i2c.h>
33 #include <linux/i2c-gpio.h>
34
35 #include <mach/hardware.h>
36 #include <mach/fb.h>
37 #include <mach/ep93xx_keypad.h>
38
39 #include <asm/mach/map.h>
40 #include <asm/mach/time.h>
41
42 #include <asm/hardware/vic.h>
43
44
45 /*************************************************************************
46  * Static I/O mappings that are needed for all EP93xx platforms
47  *************************************************************************/
48 static struct map_desc ep93xx_io_desc[] __initdata = {
49         {
50                 .virtual        = EP93XX_AHB_VIRT_BASE,
51                 .pfn            = __phys_to_pfn(EP93XX_AHB_PHYS_BASE),
52                 .length         = EP93XX_AHB_SIZE,
53                 .type           = MT_DEVICE,
54         }, {
55                 .virtual        = EP93XX_APB_VIRT_BASE,
56                 .pfn            = __phys_to_pfn(EP93XX_APB_PHYS_BASE),
57                 .length         = EP93XX_APB_SIZE,
58                 .type           = MT_DEVICE,
59         },
60 };
61
62 void __init ep93xx_map_io(void)
63 {
64         iotable_init(ep93xx_io_desc, ARRAY_SIZE(ep93xx_io_desc));
65 }
66
67
68 /*************************************************************************
69  * Timer handling for EP93xx
70  *************************************************************************
71  * The ep93xx has four internal timers.  Timers 1, 2 (both 16 bit) and
72  * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
73  * an interrupt on underflow.  Timer 4 (40 bit) counts down at 983.04 kHz,
74  * is free-running, and can't generate interrupts.
75  *
76  * The 508 kHz timers are ideal for use for the timer interrupt, as the
77  * most common values of HZ divide 508 kHz nicely.  We pick one of the 16
78  * bit timers (timer 1) since we don't need more than 16 bits of reload
79  * value as long as HZ >= 8.
80  *
81  * The higher clock rate of timer 4 makes it a better choice than the
82  * other timers for use in gettimeoffset(), while the fact that it can't
83  * generate interrupts means we don't have to worry about not being able
84  * to use this timer for something else.  We also use timer 4 for keeping
85  * track of lost jiffies.
86  */
87 static unsigned int last_jiffy_time;
88
89 #define TIMER4_TICKS_PER_JIFFY          DIV_ROUND_CLOSEST(CLOCK_TICK_RATE, HZ)
90
91 static irqreturn_t ep93xx_timer_interrupt(int irq, void *dev_id)
92 {
93         __raw_writel(1, EP93XX_TIMER1_CLEAR);
94         while ((signed long)
95                 (__raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time)
96                                                 >= TIMER4_TICKS_PER_JIFFY) {
97                 last_jiffy_time += TIMER4_TICKS_PER_JIFFY;
98                 timer_tick();
99         }
100
101         return IRQ_HANDLED;
102 }
103
104 static struct irqaction ep93xx_timer_irq = {
105         .name           = "ep93xx timer",
106         .flags          = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
107         .handler        = ep93xx_timer_interrupt,
108 };
109
110 static void __init ep93xx_timer_init(void)
111 {
112         /* Enable periodic HZ timer.  */
113         __raw_writel(0x48, EP93XX_TIMER1_CONTROL);
114         __raw_writel((508469 / HZ) - 1, EP93XX_TIMER1_LOAD);
115         __raw_writel(0xc8, EP93XX_TIMER1_CONTROL);
116
117         /* Enable lost jiffy timer.  */
118         __raw_writel(0x100, EP93XX_TIMER4_VALUE_HIGH);
119
120         setup_irq(IRQ_EP93XX_TIMER1, &ep93xx_timer_irq);
121 }
122
123 static unsigned long ep93xx_gettimeoffset(void)
124 {
125         int offset;
126
127         offset = __raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time;
128
129         /* Calculate (1000000 / 983040) * offset.  */
130         return offset + (53 * offset / 3072);
131 }
132
133 struct sys_timer ep93xx_timer = {
134         .init           = ep93xx_timer_init,
135         .offset         = ep93xx_gettimeoffset,
136 };
137
138
139 /*************************************************************************
140  * EP93xx IRQ handling
141  *************************************************************************/
142 extern void ep93xx_gpio_init_irq(void);
143
144 void __init ep93xx_init_irq(void)
145 {
146         vic_init(EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK, 0);
147         vic_init(EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK, 0);
148
149         ep93xx_gpio_init_irq();
150 }
151
152
153 /*************************************************************************
154  * EP93xx System Controller Software Locked register handling
155  *************************************************************************/
156
157 /*
158  * syscon_swlock prevents anything else from writing to the syscon
159  * block while a software locked register is being written.
160  */
161 static DEFINE_SPINLOCK(syscon_swlock);
162
163 void ep93xx_syscon_swlocked_write(unsigned int val, void __iomem *reg)
164 {
165         unsigned long flags;
166
167         spin_lock_irqsave(&syscon_swlock, flags);
168
169         __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
170         __raw_writel(val, reg);
171
172         spin_unlock_irqrestore(&syscon_swlock, flags);
173 }
174 EXPORT_SYMBOL(ep93xx_syscon_swlocked_write);
175
176 void ep93xx_devcfg_set_clear(unsigned int set_bits, unsigned int clear_bits)
177 {
178         unsigned long flags;
179         unsigned int val;
180
181         spin_lock_irqsave(&syscon_swlock, flags);
182
183         val = __raw_readl(EP93XX_SYSCON_DEVCFG);
184         val |= set_bits;
185         val &= ~clear_bits;
186         __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
187         __raw_writel(val, EP93XX_SYSCON_DEVCFG);
188
189         spin_unlock_irqrestore(&syscon_swlock, flags);
190 }
191 EXPORT_SYMBOL(ep93xx_devcfg_set_clear);
192
193
194 /*************************************************************************
195  * EP93xx peripheral handling
196  *************************************************************************/
197 #define EP93XX_UART_MCR_OFFSET          (0x0100)
198
199 static void ep93xx_uart_set_mctrl(struct amba_device *dev,
200                                   void __iomem *base, unsigned int mctrl)
201 {
202         unsigned int mcr;
203
204         mcr = 0;
205         if (!(mctrl & TIOCM_RTS))
206                 mcr |= 2;
207         if (!(mctrl & TIOCM_DTR))
208                 mcr |= 1;
209
210         __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
211 }
212
213 static struct amba_pl010_data ep93xx_uart_data = {
214         .set_mctrl      = ep93xx_uart_set_mctrl,
215 };
216
217 static struct amba_device uart1_device = {
218         .dev            = {
219                 .init_name      = "apb:uart1",
220                 .platform_data  = &ep93xx_uart_data,
221         },
222         .res            = {
223                 .start  = EP93XX_UART1_PHYS_BASE,
224                 .end    = EP93XX_UART1_PHYS_BASE + 0x0fff,
225                 .flags  = IORESOURCE_MEM,
226         },
227         .irq            = { IRQ_EP93XX_UART1, NO_IRQ },
228         .periphid       = 0x00041010,
229 };
230
231 static struct amba_device uart2_device = {
232         .dev            = {
233                 .init_name      = "apb:uart2",
234                 .platform_data  = &ep93xx_uart_data,
235         },
236         .res            = {
237                 .start  = EP93XX_UART2_PHYS_BASE,
238                 .end    = EP93XX_UART2_PHYS_BASE + 0x0fff,
239                 .flags  = IORESOURCE_MEM,
240         },
241         .irq            = { IRQ_EP93XX_UART2, NO_IRQ },
242         .periphid       = 0x00041010,
243 };
244
245 static struct amba_device uart3_device = {
246         .dev            = {
247                 .init_name      = "apb:uart3",
248                 .platform_data  = &ep93xx_uart_data,
249         },
250         .res            = {
251                 .start  = EP93XX_UART3_PHYS_BASE,
252                 .end    = EP93XX_UART3_PHYS_BASE + 0x0fff,
253                 .flags  = IORESOURCE_MEM,
254         },
255         .irq            = { IRQ_EP93XX_UART3, NO_IRQ },
256         .periphid       = 0x00041010,
257 };
258
259
260 static struct resource ep93xx_rtc_resource[] = {
261         {
262                 .start          = EP93XX_RTC_PHYS_BASE,
263                 .end            = EP93XX_RTC_PHYS_BASE + 0x10c - 1,
264                 .flags          = IORESOURCE_MEM,
265         },
266 };
267
268 static struct platform_device ep93xx_rtc_device = {
269         .name           = "ep93xx-rtc",
270         .id             = -1,
271         .num_resources  = ARRAY_SIZE(ep93xx_rtc_resource),
272         .resource       = ep93xx_rtc_resource,
273 };
274
275
276 static struct resource ep93xx_ohci_resources[] = {
277         [0] = {
278                 .start  = EP93XX_USB_PHYS_BASE,
279                 .end    = EP93XX_USB_PHYS_BASE + 0x0fff,
280                 .flags  = IORESOURCE_MEM,
281         },
282         [1] = {
283                 .start  = IRQ_EP93XX_USB,
284                 .end    = IRQ_EP93XX_USB,
285                 .flags  = IORESOURCE_IRQ,
286         },
287 };
288
289
290 static struct platform_device ep93xx_ohci_device = {
291         .name           = "ep93xx-ohci",
292         .id             = -1,
293         .dev            = {
294                 .dma_mask               = &ep93xx_ohci_device.dev.coherent_dma_mask,
295                 .coherent_dma_mask      = DMA_BIT_MASK(32),
296         },
297         .num_resources  = ARRAY_SIZE(ep93xx_ohci_resources),
298         .resource       = ep93xx_ohci_resources,
299 };
300
301 static struct ep93xx_eth_data ep93xx_eth_data;
302
303 static struct resource ep93xx_eth_resource[] = {
304         {
305                 .start  = EP93XX_ETHERNET_PHYS_BASE,
306                 .end    = EP93XX_ETHERNET_PHYS_BASE + 0xffff,
307                 .flags  = IORESOURCE_MEM,
308         }, {
309                 .start  = IRQ_EP93XX_ETHERNET,
310                 .end    = IRQ_EP93XX_ETHERNET,
311                 .flags  = IORESOURCE_IRQ,
312         }
313 };
314
315 static struct platform_device ep93xx_eth_device = {
316         .name           = "ep93xx-eth",
317         .id             = -1,
318         .dev            = {
319                 .platform_data  = &ep93xx_eth_data,
320         },
321         .num_resources  = ARRAY_SIZE(ep93xx_eth_resource),
322         .resource       = ep93xx_eth_resource,
323 };
324
325 void __init ep93xx_register_eth(struct ep93xx_eth_data *data, int copy_addr)
326 {
327         if (copy_addr)
328                 memcpy_fromio(data->dev_addr, EP93XX_ETHERNET_BASE + 0x50, 6);
329
330         ep93xx_eth_data = *data;
331         platform_device_register(&ep93xx_eth_device);
332 }
333
334
335 /*************************************************************************
336  * EP93xx i2c peripheral handling
337  *************************************************************************/
338 static struct i2c_gpio_platform_data ep93xx_i2c_data;
339
340 static struct platform_device ep93xx_i2c_device = {
341         .name                   = "i2c-gpio",
342         .id                     = 0,
343         .dev.platform_data      = &ep93xx_i2c_data,
344 };
345
346 void __init ep93xx_register_i2c(struct i2c_gpio_platform_data *data,
347                                 struct i2c_board_info *devices, int num)
348 {
349         /*
350          * Set the EEPROM interface pin drive type control.
351          * Defines the driver type for the EECLK and EEDAT pins as either
352          * open drain, which will require an external pull-up, or a normal
353          * CMOS driver.
354          */
355         if (data->sda_is_open_drain && data->sda_pin != EP93XX_GPIO_LINE_EEDAT)
356                 pr_warning("sda != EEDAT, open drain has no effect\n");
357         if (data->scl_is_open_drain && data->scl_pin != EP93XX_GPIO_LINE_EECLK)
358                 pr_warning("scl != EECLK, open drain has no effect\n");
359
360         __raw_writel((data->sda_is_open_drain << 1) |
361                      (data->scl_is_open_drain << 0),
362                      EP93XX_GPIO_EEDRIVE);
363
364         ep93xx_i2c_data = *data;
365         i2c_register_board_info(0, devices, num);
366         platform_device_register(&ep93xx_i2c_device);
367 }
368
369
370 /*************************************************************************
371  * EP93xx LEDs
372  *************************************************************************/
373 static struct gpio_led ep93xx_led_pins[] = {
374         {
375                 .name                   = "platform:grled",
376                 .gpio                   = EP93XX_GPIO_LINE_GRLED,
377         }, {
378                 .name                   = "platform:rdled",
379                 .gpio                   = EP93XX_GPIO_LINE_RDLED,
380         },
381 };
382
383 static struct gpio_led_platform_data ep93xx_led_data = {
384         .num_leds       = ARRAY_SIZE(ep93xx_led_pins),
385         .leds           = ep93xx_led_pins,
386 };
387
388 static struct platform_device ep93xx_leds = {
389         .name           = "leds-gpio",
390         .id             = -1,
391         .dev            = {
392                 .platform_data  = &ep93xx_led_data,
393         },
394 };
395
396
397 /*************************************************************************
398  * EP93xx pwm peripheral handling
399  *************************************************************************/
400 static struct resource ep93xx_pwm0_resource[] = {
401         {
402                 .start  = EP93XX_PWM_PHYS_BASE,
403                 .end    = EP93XX_PWM_PHYS_BASE + 0x10 - 1,
404                 .flags  = IORESOURCE_MEM,
405         },
406 };
407
408 static struct platform_device ep93xx_pwm0_device = {
409         .name           = "ep93xx-pwm",
410         .id             = 0,
411         .num_resources  = ARRAY_SIZE(ep93xx_pwm0_resource),
412         .resource       = ep93xx_pwm0_resource,
413 };
414
415 static struct resource ep93xx_pwm1_resource[] = {
416         {
417                 .start  = EP93XX_PWM_PHYS_BASE + 0x20,
418                 .end    = EP93XX_PWM_PHYS_BASE + 0x30 - 1,
419                 .flags  = IORESOURCE_MEM,
420         },
421 };
422
423 static struct platform_device ep93xx_pwm1_device = {
424         .name           = "ep93xx-pwm",
425         .id             = 1,
426         .num_resources  = ARRAY_SIZE(ep93xx_pwm1_resource),
427         .resource       = ep93xx_pwm1_resource,
428 };
429
430 void __init ep93xx_register_pwm(int pwm0, int pwm1)
431 {
432         if (pwm0)
433                 platform_device_register(&ep93xx_pwm0_device);
434
435         /* NOTE: EP9307 does not have PWMOUT1 (pin EGPIO14) */
436         if (pwm1)
437                 platform_device_register(&ep93xx_pwm1_device);
438 }
439
440 int ep93xx_pwm_acquire_gpio(struct platform_device *pdev)
441 {
442         int err;
443
444         if (pdev->id == 0) {
445                 err = 0;
446         } else if (pdev->id == 1) {
447                 err = gpio_request(EP93XX_GPIO_LINE_EGPIO14,
448                                    dev_name(&pdev->dev));
449                 if (err)
450                         return err;
451                 err = gpio_direction_output(EP93XX_GPIO_LINE_EGPIO14, 0);
452                 if (err)
453                         goto fail;
454
455                 /* PWM 1 output on EGPIO[14] */
456                 ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_PONG);
457         } else {
458                 err = -ENODEV;
459         }
460
461         return err;
462
463 fail:
464         gpio_free(EP93XX_GPIO_LINE_EGPIO14);
465         return err;
466 }
467 EXPORT_SYMBOL(ep93xx_pwm_acquire_gpio);
468
469 void ep93xx_pwm_release_gpio(struct platform_device *pdev)
470 {
471         if (pdev->id == 1) {
472                 gpio_direction_input(EP93XX_GPIO_LINE_EGPIO14);
473                 gpio_free(EP93XX_GPIO_LINE_EGPIO14);
474
475                 /* EGPIO[14] used for GPIO */
476                 ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_PONG);
477         }
478 }
479 EXPORT_SYMBOL(ep93xx_pwm_release_gpio);
480
481
482 /*************************************************************************
483  * EP93xx video peripheral handling
484  *************************************************************************/
485 static struct ep93xxfb_mach_info ep93xxfb_data;
486
487 static struct resource ep93xx_fb_resource[] = {
488         {
489                 .start          = EP93XX_RASTER_PHYS_BASE,
490                 .end            = EP93XX_RASTER_PHYS_BASE + 0x800 - 1,
491                 .flags          = IORESOURCE_MEM,
492         },
493 };
494
495 static struct platform_device ep93xx_fb_device = {
496         .name                   = "ep93xx-fb",
497         .id                     = -1,
498         .dev                    = {
499                 .platform_data  = &ep93xxfb_data,
500                 .coherent_dma_mask      = DMA_BIT_MASK(32),
501                 .dma_mask               = &ep93xx_fb_device.dev.coherent_dma_mask,
502         },
503         .num_resources          = ARRAY_SIZE(ep93xx_fb_resource),
504         .resource               = ep93xx_fb_resource,
505 };
506
507 void __init ep93xx_register_fb(struct ep93xxfb_mach_info *data)
508 {
509         ep93xxfb_data = *data;
510         platform_device_register(&ep93xx_fb_device);
511 }
512
513
514 /*************************************************************************
515  * EP93xx matrix keypad peripheral handling
516  *************************************************************************/
517 static struct resource ep93xx_keypad_resource[] = {
518         {
519                 .start  = EP93XX_KEY_MATRIX_PHYS_BASE,
520                 .end    = EP93XX_KEY_MATRIX_PHYS_BASE + 0x0c - 1,
521                 .flags  = IORESOURCE_MEM,
522         }, {
523                 .start  = IRQ_EP93XX_KEY,
524                 .end    = IRQ_EP93XX_KEY,
525                 .flags  = IORESOURCE_IRQ,
526         },
527 };
528
529 static struct platform_device ep93xx_keypad_device = {
530         .name                   = "ep93xx-keypad",
531         .id                     = -1,
532         .num_resources          = ARRAY_SIZE(ep93xx_keypad_resource),
533         .resource               = ep93xx_keypad_resource,
534 };
535
536 void __init ep93xx_register_keypad(struct ep93xx_keypad_platform_data *data)
537 {
538         ep93xx_keypad_device.dev.platform_data = data;
539         platform_device_register(&ep93xx_keypad_device);
540 }
541
542 int ep93xx_keypad_acquire_gpio(struct platform_device *pdev)
543 {
544         int err;
545         int i;
546
547         for (i = 0; i < 8; i++) {
548                 err = gpio_request(EP93XX_GPIO_LINE_C(i), dev_name(&pdev->dev));
549                 if (err)
550                         goto fail_gpio_c;
551                 err = gpio_request(EP93XX_GPIO_LINE_D(i), dev_name(&pdev->dev));
552                 if (err)
553                         goto fail_gpio_d;
554         }
555
556         /* Enable the keypad controller; GPIO ports C and D used for keypad */
557         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_KEYS |
558                                  EP93XX_SYSCON_DEVCFG_GONK);
559
560         return 0;
561
562 fail_gpio_d:
563         gpio_free(EP93XX_GPIO_LINE_C(i));
564 fail_gpio_c:
565         for ( ; i >= 0; --i) {
566                 gpio_free(EP93XX_GPIO_LINE_C(i));
567                 gpio_free(EP93XX_GPIO_LINE_D(i));
568         }
569         return err;
570 }
571 EXPORT_SYMBOL(ep93xx_keypad_acquire_gpio);
572
573 void ep93xx_keypad_release_gpio(struct platform_device *pdev)
574 {
575         int i;
576
577         for (i = 0; i < 8; i++) {
578                 gpio_free(EP93XX_GPIO_LINE_C(i));
579                 gpio_free(EP93XX_GPIO_LINE_D(i));
580         }
581
582         /* Disable the keypad controller; GPIO ports C and D used for GPIO */
583         ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_KEYS |
584                                EP93XX_SYSCON_DEVCFG_GONK);
585 }
586 EXPORT_SYMBOL(ep93xx_keypad_release_gpio);
587
588
589 extern void ep93xx_gpio_init(void);
590
591 void __init ep93xx_init_devices(void)
592 {
593         /* Disallow access to MaverickCrunch initially */
594         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_CPENA);
595
596         ep93xx_gpio_init();
597
598         amba_device_register(&uart1_device, &iomem_resource);
599         amba_device_register(&uart2_device, &iomem_resource);
600         amba_device_register(&uart3_device, &iomem_resource);
601
602         platform_device_register(&ep93xx_rtc_device);
603         platform_device_register(&ep93xx_ohci_device);
604         platform_device_register(&ep93xx_leds);
605 }