GNU Linux-libre 4.19.314-gnu1
[releases.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/sys_soc.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/mtd/physmap.h>
33 #include <linux/i2c.h>
34 #include <linux/gpio/machine.h>
35 #include <linux/spi/spi.h>
36 #include <linux/export.h>
37 #include <linux/irqchip/arm-vic.h>
38 #include <linux/reboot.h>
39 #include <linux/usb/ohci_pdriver.h>
40 #include <linux/random.h>
41
42 #include <mach/hardware.h>
43 #include <linux/platform_data/video-ep93xx.h>
44 #include <linux/platform_data/keypad-ep93xx.h>
45 #include <linux/platform_data/spi-ep93xx.h>
46 #include <mach/gpio-ep93xx.h>
47
48 #include <asm/mach/arch.h>
49 #include <asm/mach/map.h>
50
51 #include "soc.h"
52
53 /*************************************************************************
54  * Static I/O mappings that are needed for all EP93xx platforms
55  *************************************************************************/
56 static struct map_desc ep93xx_io_desc[] __initdata = {
57         {
58                 .virtual        = EP93XX_AHB_VIRT_BASE,
59                 .pfn            = __phys_to_pfn(EP93XX_AHB_PHYS_BASE),
60                 .length         = EP93XX_AHB_SIZE,
61                 .type           = MT_DEVICE,
62         }, {
63                 .virtual        = EP93XX_APB_VIRT_BASE,
64                 .pfn            = __phys_to_pfn(EP93XX_APB_PHYS_BASE),
65                 .length         = EP93XX_APB_SIZE,
66                 .type           = MT_DEVICE,
67         },
68 };
69
70 void __init ep93xx_map_io(void)
71 {
72         iotable_init(ep93xx_io_desc, ARRAY_SIZE(ep93xx_io_desc));
73 }
74
75 /*************************************************************************
76  * EP93xx IRQ handling
77  *************************************************************************/
78 void __init ep93xx_init_irq(void)
79 {
80         vic_init(EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK, 0);
81         vic_init(EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK, 0);
82 }
83
84
85 /*************************************************************************
86  * EP93xx System Controller Software Locked register handling
87  *************************************************************************/
88
89 /*
90  * syscon_swlock prevents anything else from writing to the syscon
91  * block while a software locked register is being written.
92  */
93 static DEFINE_SPINLOCK(syscon_swlock);
94
95 void ep93xx_syscon_swlocked_write(unsigned int val, void __iomem *reg)
96 {
97         unsigned long flags;
98
99         spin_lock_irqsave(&syscon_swlock, flags);
100
101         __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
102         __raw_writel(val, reg);
103
104         spin_unlock_irqrestore(&syscon_swlock, flags);
105 }
106
107 void ep93xx_devcfg_set_clear(unsigned int set_bits, unsigned int clear_bits)
108 {
109         unsigned long flags;
110         unsigned int val;
111
112         spin_lock_irqsave(&syscon_swlock, flags);
113
114         val = __raw_readl(EP93XX_SYSCON_DEVCFG);
115         val &= ~clear_bits;
116         val |= set_bits;
117         __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
118         __raw_writel(val, EP93XX_SYSCON_DEVCFG);
119
120         spin_unlock_irqrestore(&syscon_swlock, flags);
121 }
122
123 /**
124  * ep93xx_chip_revision() - returns the EP93xx chip revision
125  *
126  * See <mach/platform.h> for more information.
127  */
128 unsigned int ep93xx_chip_revision(void)
129 {
130         unsigned int v;
131
132         v = __raw_readl(EP93XX_SYSCON_SYSCFG);
133         v &= EP93XX_SYSCON_SYSCFG_REV_MASK;
134         v >>= EP93XX_SYSCON_SYSCFG_REV_SHIFT;
135         return v;
136 }
137 EXPORT_SYMBOL_GPL(ep93xx_chip_revision);
138
139 /*************************************************************************
140  * EP93xx GPIO
141  *************************************************************************/
142 static struct resource ep93xx_gpio_resource[] = {
143         DEFINE_RES_MEM(EP93XX_GPIO_PHYS_BASE, 0xcc),
144 };
145
146 static struct platform_device ep93xx_gpio_device = {
147         .name           = "gpio-ep93xx",
148         .id             = -1,
149         .num_resources  = ARRAY_SIZE(ep93xx_gpio_resource),
150         .resource       = ep93xx_gpio_resource,
151 };
152
153 /*************************************************************************
154  * EP93xx peripheral handling
155  *************************************************************************/
156 #define EP93XX_UART_MCR_OFFSET          (0x0100)
157
158 static void ep93xx_uart_set_mctrl(struct amba_device *dev,
159                                   void __iomem *base, unsigned int mctrl)
160 {
161         unsigned int mcr;
162
163         mcr = 0;
164         if (mctrl & TIOCM_RTS)
165                 mcr |= 2;
166         if (mctrl & TIOCM_DTR)
167                 mcr |= 1;
168
169         __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
170 }
171
172 static struct amba_pl010_data ep93xx_uart_data = {
173         .set_mctrl      = ep93xx_uart_set_mctrl,
174 };
175
176 static AMBA_APB_DEVICE(uart1, "apb:uart1", 0x00041010, EP93XX_UART1_PHYS_BASE,
177         { IRQ_EP93XX_UART1 }, &ep93xx_uart_data);
178
179 static AMBA_APB_DEVICE(uart2, "apb:uart2", 0x00041010, EP93XX_UART2_PHYS_BASE,
180         { IRQ_EP93XX_UART2 }, NULL);
181
182 static AMBA_APB_DEVICE(uart3, "apb:uart3", 0x00041010, EP93XX_UART3_PHYS_BASE,
183         { IRQ_EP93XX_UART3 }, &ep93xx_uart_data);
184
185 static struct resource ep93xx_rtc_resource[] = {
186         DEFINE_RES_MEM(EP93XX_RTC_PHYS_BASE, 0x10c),
187 };
188
189 static struct platform_device ep93xx_rtc_device = {
190         .name           = "ep93xx-rtc",
191         .id             = -1,
192         .num_resources  = ARRAY_SIZE(ep93xx_rtc_resource),
193         .resource       = ep93xx_rtc_resource,
194 };
195
196 /*************************************************************************
197  * EP93xx OHCI USB Host
198  *************************************************************************/
199
200 static struct clk *ep93xx_ohci_host_clock;
201
202 static int ep93xx_ohci_power_on(struct platform_device *pdev)
203 {
204         if (!ep93xx_ohci_host_clock) {
205                 ep93xx_ohci_host_clock = devm_clk_get(&pdev->dev, NULL);
206                 if (IS_ERR(ep93xx_ohci_host_clock))
207                         return PTR_ERR(ep93xx_ohci_host_clock);
208         }
209
210         return clk_enable(ep93xx_ohci_host_clock);
211 }
212
213 static void ep93xx_ohci_power_off(struct platform_device *pdev)
214 {
215         clk_disable(ep93xx_ohci_host_clock);
216 }
217
218 static struct usb_ohci_pdata ep93xx_ohci_pdata = {
219         .power_on       = ep93xx_ohci_power_on,
220         .power_off      = ep93xx_ohci_power_off,
221         .power_suspend  = ep93xx_ohci_power_off,
222 };
223
224 static struct resource ep93xx_ohci_resources[] = {
225         DEFINE_RES_MEM(EP93XX_USB_PHYS_BASE, 0x1000),
226         DEFINE_RES_IRQ(IRQ_EP93XX_USB),
227 };
228
229 static u64 ep93xx_ohci_dma_mask = DMA_BIT_MASK(32);
230
231 static struct platform_device ep93xx_ohci_device = {
232         .name           = "ohci-platform",
233         .id             = -1,
234         .num_resources  = ARRAY_SIZE(ep93xx_ohci_resources),
235         .resource       = ep93xx_ohci_resources,
236         .dev            = {
237                 .dma_mask               = &ep93xx_ohci_dma_mask,
238                 .coherent_dma_mask      = DMA_BIT_MASK(32),
239                 .platform_data          = &ep93xx_ohci_pdata,
240         },
241 };
242
243 /*************************************************************************
244  * EP93xx physmap'ed flash
245  *************************************************************************/
246 static struct physmap_flash_data ep93xx_flash_data;
247
248 static struct resource ep93xx_flash_resource = {
249         .flags          = IORESOURCE_MEM,
250 };
251
252 static struct platform_device ep93xx_flash = {
253         .name           = "physmap-flash",
254         .id             = 0,
255         .dev            = {
256                 .platform_data  = &ep93xx_flash_data,
257         },
258         .num_resources  = 1,
259         .resource       = &ep93xx_flash_resource,
260 };
261
262 /**
263  * ep93xx_register_flash() - Register the external flash device.
264  * @width:      bank width in octets
265  * @start:      resource start address
266  * @size:       resource size
267  */
268 void __init ep93xx_register_flash(unsigned int width,
269                                   resource_size_t start, resource_size_t size)
270 {
271         ep93xx_flash_data.width         = width;
272
273         ep93xx_flash_resource.start     = start;
274         ep93xx_flash_resource.end       = start + size - 1;
275
276         platform_device_register(&ep93xx_flash);
277 }
278
279
280 /*************************************************************************
281  * EP93xx ethernet peripheral handling
282  *************************************************************************/
283 static struct ep93xx_eth_data ep93xx_eth_data;
284
285 static struct resource ep93xx_eth_resource[] = {
286         DEFINE_RES_MEM(EP93XX_ETHERNET_PHYS_BASE, 0x10000),
287         DEFINE_RES_IRQ(IRQ_EP93XX_ETHERNET),
288 };
289
290 static u64 ep93xx_eth_dma_mask = DMA_BIT_MASK(32);
291
292 static struct platform_device ep93xx_eth_device = {
293         .name           = "ep93xx-eth",
294         .id             = -1,
295         .dev            = {
296                 .platform_data          = &ep93xx_eth_data,
297                 .coherent_dma_mask      = DMA_BIT_MASK(32),
298                 .dma_mask               = &ep93xx_eth_dma_mask,
299         },
300         .num_resources  = ARRAY_SIZE(ep93xx_eth_resource),
301         .resource       = ep93xx_eth_resource,
302 };
303
304 /**
305  * ep93xx_register_eth - Register the built-in ethernet platform device.
306  * @data:       platform specific ethernet configuration (__initdata)
307  * @copy_addr:  flag indicating that the MAC address should be copied
308  *              from the IndAd registers (as programmed by the bootloader)
309  */
310 void __init ep93xx_register_eth(struct ep93xx_eth_data *data, int copy_addr)
311 {
312         if (copy_addr)
313                 memcpy_fromio(data->dev_addr, EP93XX_ETHERNET_BASE + 0x50, 6);
314
315         ep93xx_eth_data = *data;
316         platform_device_register(&ep93xx_eth_device);
317 }
318
319
320 /*************************************************************************
321  * EP93xx i2c peripheral handling
322  *************************************************************************/
323
324 /* All EP93xx devices use the same two GPIO pins for I2C bit-banging */
325 static struct gpiod_lookup_table ep93xx_i2c_gpiod_table = {
326         .dev_id         = "i2c-gpio.0",
327         .table          = {
328                 /* Use local offsets on gpiochip/port "G" */
329                 GPIO_LOOKUP_IDX("G", 1, NULL, 0,
330                                 GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
331                 GPIO_LOOKUP_IDX("G", 0, NULL, 1,
332                                 GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
333                 { }
334         },
335 };
336
337 static struct platform_device ep93xx_i2c_device = {
338         .name           = "i2c-gpio",
339         .id             = 0,
340         .dev            = {
341                 .platform_data  = NULL,
342         },
343 };
344
345 /**
346  * ep93xx_register_i2c - Register the i2c platform device.
347  * @devices:    platform specific i2c bus device information (__initdata)
348  * @num:        the number of devices on the i2c bus
349  */
350 void __init ep93xx_register_i2c(struct i2c_board_info *devices, int num)
351 {
352         /*
353          * FIXME: this just sets the two pins as non-opendrain, as no
354          * platforms tries to do that anyway. Flag the applicable lines
355          * as open drain in the GPIO_LOOKUP above and the driver or
356          * gpiolib will handle open drain/open drain emulation as need
357          * be. Right now i2c-gpio emulates open drain which is not
358          * optimal.
359          */
360         __raw_writel((0 << 1) | (0 << 0),
361                      EP93XX_GPIO_EEDRIVE);
362
363         i2c_register_board_info(0, devices, num);
364         gpiod_add_lookup_table(&ep93xx_i2c_gpiod_table);
365         platform_device_register(&ep93xx_i2c_device);
366 }
367
368 /*************************************************************************
369  * EP93xx SPI peripheral handling
370  *************************************************************************/
371 static struct ep93xx_spi_info ep93xx_spi_master_data;
372
373 static struct resource ep93xx_spi_resources[] = {
374         DEFINE_RES_MEM(EP93XX_SPI_PHYS_BASE, 0x18),
375         DEFINE_RES_IRQ(IRQ_EP93XX_SSP),
376 };
377
378 static u64 ep93xx_spi_dma_mask = DMA_BIT_MASK(32);
379
380 static struct platform_device ep93xx_spi_device = {
381         .name           = "ep93xx-spi",
382         .id             = 0,
383         .dev            = {
384                 .platform_data          = &ep93xx_spi_master_data,
385                 .coherent_dma_mask      = DMA_BIT_MASK(32),
386                 .dma_mask               = &ep93xx_spi_dma_mask,
387         },
388         .num_resources  = ARRAY_SIZE(ep93xx_spi_resources),
389         .resource       = ep93xx_spi_resources,
390 };
391
392 /**
393  * ep93xx_register_spi() - registers spi platform device
394  * @info: ep93xx board specific spi master info (__initdata)
395  * @devices: SPI devices to register (__initdata)
396  * @num: number of SPI devices to register
397  *
398  * This function registers platform device for the EP93xx SPI controller and
399  * also makes sure that SPI pins are muxed so that I2S is not using those pins.
400  */
401 void __init ep93xx_register_spi(struct ep93xx_spi_info *info,
402                                 struct spi_board_info *devices, int num)
403 {
404         /*
405          * When SPI is used, we need to make sure that I2S is muxed off from
406          * SPI pins.
407          */
408         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_I2SONSSP);
409
410         ep93xx_spi_master_data = *info;
411         spi_register_board_info(devices, num);
412         platform_device_register(&ep93xx_spi_device);
413 }
414
415 /*************************************************************************
416  * EP93xx LEDs
417  *************************************************************************/
418 static const struct gpio_led ep93xx_led_pins[] __initconst = {
419         {
420                 .name   = "platform:grled",
421                 .gpio   = EP93XX_GPIO_LINE_GRLED,
422         }, {
423                 .name   = "platform:rdled",
424                 .gpio   = EP93XX_GPIO_LINE_RDLED,
425         },
426 };
427
428 static const struct gpio_led_platform_data ep93xx_led_data __initconst = {
429         .num_leds       = ARRAY_SIZE(ep93xx_led_pins),
430         .leds           = ep93xx_led_pins,
431 };
432
433 /*************************************************************************
434  * EP93xx pwm peripheral handling
435  *************************************************************************/
436 static struct resource ep93xx_pwm0_resource[] = {
437         DEFINE_RES_MEM(EP93XX_PWM_PHYS_BASE, 0x10),
438 };
439
440 static struct platform_device ep93xx_pwm0_device = {
441         .name           = "ep93xx-pwm",
442         .id             = 0,
443         .num_resources  = ARRAY_SIZE(ep93xx_pwm0_resource),
444         .resource       = ep93xx_pwm0_resource,
445 };
446
447 static struct resource ep93xx_pwm1_resource[] = {
448         DEFINE_RES_MEM(EP93XX_PWM_PHYS_BASE + 0x20, 0x10),
449 };
450
451 static struct platform_device ep93xx_pwm1_device = {
452         .name           = "ep93xx-pwm",
453         .id             = 1,
454         .num_resources  = ARRAY_SIZE(ep93xx_pwm1_resource),
455         .resource       = ep93xx_pwm1_resource,
456 };
457
458 void __init ep93xx_register_pwm(int pwm0, int pwm1)
459 {
460         if (pwm0)
461                 platform_device_register(&ep93xx_pwm0_device);
462
463         /* NOTE: EP9307 does not have PWMOUT1 (pin EGPIO14) */
464         if (pwm1)
465                 platform_device_register(&ep93xx_pwm1_device);
466 }
467
468 int ep93xx_pwm_acquire_gpio(struct platform_device *pdev)
469 {
470         int err;
471
472         if (pdev->id == 0) {
473                 err = 0;
474         } else if (pdev->id == 1) {
475                 err = gpio_request(EP93XX_GPIO_LINE_EGPIO14,
476                                    dev_name(&pdev->dev));
477                 if (err)
478                         return err;
479                 err = gpio_direction_output(EP93XX_GPIO_LINE_EGPIO14, 0);
480                 if (err)
481                         goto fail;
482
483                 /* PWM 1 output on EGPIO[14] */
484                 ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_PONG);
485         } else {
486                 err = -ENODEV;
487         }
488
489         return err;
490
491 fail:
492         gpio_free(EP93XX_GPIO_LINE_EGPIO14);
493         return err;
494 }
495 EXPORT_SYMBOL(ep93xx_pwm_acquire_gpio);
496
497 void ep93xx_pwm_release_gpio(struct platform_device *pdev)
498 {
499         if (pdev->id == 1) {
500                 gpio_direction_input(EP93XX_GPIO_LINE_EGPIO14);
501                 gpio_free(EP93XX_GPIO_LINE_EGPIO14);
502
503                 /* EGPIO[14] used for GPIO */
504                 ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_PONG);
505         }
506 }
507 EXPORT_SYMBOL(ep93xx_pwm_release_gpio);
508
509
510 /*************************************************************************
511  * EP93xx video peripheral handling
512  *************************************************************************/
513 static struct ep93xxfb_mach_info ep93xxfb_data;
514
515 static struct resource ep93xx_fb_resource[] = {
516         DEFINE_RES_MEM(EP93XX_RASTER_PHYS_BASE, 0x800),
517 };
518
519 static struct platform_device ep93xx_fb_device = {
520         .name                   = "ep93xx-fb",
521         .id                     = -1,
522         .dev                    = {
523                 .platform_data          = &ep93xxfb_data,
524                 .coherent_dma_mask      = DMA_BIT_MASK(32),
525                 .dma_mask               = &ep93xx_fb_device.dev.coherent_dma_mask,
526         },
527         .num_resources          = ARRAY_SIZE(ep93xx_fb_resource),
528         .resource               = ep93xx_fb_resource,
529 };
530
531 /* The backlight use a single register in the framebuffer's register space */
532 #define EP93XX_RASTER_REG_BRIGHTNESS 0x20
533
534 static struct resource ep93xx_bl_resources[] = {
535         DEFINE_RES_MEM(EP93XX_RASTER_PHYS_BASE +
536                        EP93XX_RASTER_REG_BRIGHTNESS, 0x04),
537 };
538
539 static struct platform_device ep93xx_bl_device = {
540         .name           = "ep93xx-bl",
541         .id             = -1,
542         .num_resources  = ARRAY_SIZE(ep93xx_bl_resources),
543         .resource       = ep93xx_bl_resources,
544 };
545
546 /**
547  * ep93xx_register_fb - Register the framebuffer platform device.
548  * @data:       platform specific framebuffer configuration (__initdata)
549  */
550 void __init ep93xx_register_fb(struct ep93xxfb_mach_info *data)
551 {
552         ep93xxfb_data = *data;
553         platform_device_register(&ep93xx_fb_device);
554         platform_device_register(&ep93xx_bl_device);
555 }
556
557
558 /*************************************************************************
559  * EP93xx matrix keypad peripheral handling
560  *************************************************************************/
561 static struct ep93xx_keypad_platform_data ep93xx_keypad_data;
562
563 static struct resource ep93xx_keypad_resource[] = {
564         DEFINE_RES_MEM(EP93XX_KEY_MATRIX_PHYS_BASE, 0x0c),
565         DEFINE_RES_IRQ(IRQ_EP93XX_KEY),
566 };
567
568 static struct platform_device ep93xx_keypad_device = {
569         .name           = "ep93xx-keypad",
570         .id             = -1,
571         .dev            = {
572                 .platform_data  = &ep93xx_keypad_data,
573         },
574         .num_resources  = ARRAY_SIZE(ep93xx_keypad_resource),
575         .resource       = ep93xx_keypad_resource,
576 };
577
578 /**
579  * ep93xx_register_keypad - Register the keypad platform device.
580  * @data:       platform specific keypad configuration (__initdata)
581  */
582 void __init ep93xx_register_keypad(struct ep93xx_keypad_platform_data *data)
583 {
584         ep93xx_keypad_data = *data;
585         platform_device_register(&ep93xx_keypad_device);
586 }
587
588 int ep93xx_keypad_acquire_gpio(struct platform_device *pdev)
589 {
590         int err;
591         int i;
592
593         for (i = 0; i < 8; i++) {
594                 err = gpio_request(EP93XX_GPIO_LINE_C(i), dev_name(&pdev->dev));
595                 if (err)
596                         goto fail_gpio_c;
597                 err = gpio_request(EP93XX_GPIO_LINE_D(i), dev_name(&pdev->dev));
598                 if (err)
599                         goto fail_gpio_d;
600         }
601
602         /* Enable the keypad controller; GPIO ports C and D used for keypad */
603         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_KEYS |
604                                  EP93XX_SYSCON_DEVCFG_GONK);
605
606         return 0;
607
608 fail_gpio_d:
609         gpio_free(EP93XX_GPIO_LINE_C(i));
610 fail_gpio_c:
611         for (--i; i >= 0; --i) {
612                 gpio_free(EP93XX_GPIO_LINE_C(i));
613                 gpio_free(EP93XX_GPIO_LINE_D(i));
614         }
615         return err;
616 }
617 EXPORT_SYMBOL(ep93xx_keypad_acquire_gpio);
618
619 void ep93xx_keypad_release_gpio(struct platform_device *pdev)
620 {
621         int i;
622
623         for (i = 0; i < 8; i++) {
624                 gpio_free(EP93XX_GPIO_LINE_C(i));
625                 gpio_free(EP93XX_GPIO_LINE_D(i));
626         }
627
628         /* Disable the keypad controller; GPIO ports C and D used for GPIO */
629         ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_KEYS |
630                                EP93XX_SYSCON_DEVCFG_GONK);
631 }
632 EXPORT_SYMBOL(ep93xx_keypad_release_gpio);
633
634 /*************************************************************************
635  * EP93xx I2S audio peripheral handling
636  *************************************************************************/
637 static struct resource ep93xx_i2s_resource[] = {
638         DEFINE_RES_MEM(EP93XX_I2S_PHYS_BASE, 0x100),
639         DEFINE_RES_IRQ(IRQ_EP93XX_SAI),
640 };
641
642 static struct platform_device ep93xx_i2s_device = {
643         .name           = "ep93xx-i2s",
644         .id             = -1,
645         .num_resources  = ARRAY_SIZE(ep93xx_i2s_resource),
646         .resource       = ep93xx_i2s_resource,
647 };
648
649 static struct platform_device ep93xx_pcm_device = {
650         .name           = "ep93xx-pcm-audio",
651         .id             = -1,
652 };
653
654 void __init ep93xx_register_i2s(void)
655 {
656         platform_device_register(&ep93xx_i2s_device);
657         platform_device_register(&ep93xx_pcm_device);
658 }
659
660 #define EP93XX_SYSCON_DEVCFG_I2S_MASK   (EP93XX_SYSCON_DEVCFG_I2SONSSP | \
661                                          EP93XX_SYSCON_DEVCFG_I2SONAC97)
662
663 #define EP93XX_I2SCLKDIV_MASK           (EP93XX_SYSCON_I2SCLKDIV_ORIDE | \
664                                          EP93XX_SYSCON_I2SCLKDIV_SPOL)
665
666 int ep93xx_i2s_acquire(void)
667 {
668         unsigned val;
669
670         ep93xx_devcfg_set_clear(EP93XX_SYSCON_DEVCFG_I2SONAC97,
671                         EP93XX_SYSCON_DEVCFG_I2S_MASK);
672
673         /*
674          * This is potentially racy with the clock api for i2s_mclk, sclk and 
675          * lrclk. Since the i2s driver is the only user of those clocks we
676          * rely on it to prevent parallel use of this function and the 
677          * clock api for the i2s clocks.
678          */
679         val = __raw_readl(EP93XX_SYSCON_I2SCLKDIV);
680         val &= ~EP93XX_I2SCLKDIV_MASK;
681         val |= EP93XX_SYSCON_I2SCLKDIV_ORIDE | EP93XX_SYSCON_I2SCLKDIV_SPOL;
682         ep93xx_syscon_swlocked_write(val, EP93XX_SYSCON_I2SCLKDIV);
683
684         return 0;
685 }
686 EXPORT_SYMBOL(ep93xx_i2s_acquire);
687
688 void ep93xx_i2s_release(void)
689 {
690         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_I2S_MASK);
691 }
692 EXPORT_SYMBOL(ep93xx_i2s_release);
693
694 /*************************************************************************
695  * EP93xx AC97 audio peripheral handling
696  *************************************************************************/
697 static struct resource ep93xx_ac97_resources[] = {
698         DEFINE_RES_MEM(EP93XX_AAC_PHYS_BASE, 0xac),
699         DEFINE_RES_IRQ(IRQ_EP93XX_AACINTR),
700 };
701
702 static struct platform_device ep93xx_ac97_device = {
703         .name           = "ep93xx-ac97",
704         .id             = -1,
705         .num_resources  = ARRAY_SIZE(ep93xx_ac97_resources),
706         .resource       = ep93xx_ac97_resources,
707 };
708
709 void __init ep93xx_register_ac97(void)
710 {
711         /*
712          * Make sure that the AC97 pins are not used by I2S.
713          */
714         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_I2SONAC97);
715
716         platform_device_register(&ep93xx_ac97_device);
717         platform_device_register(&ep93xx_pcm_device);
718 }
719
720 /*************************************************************************
721  * EP93xx Watchdog
722  *************************************************************************/
723 static struct resource ep93xx_wdt_resources[] = {
724         DEFINE_RES_MEM(EP93XX_WATCHDOG_PHYS_BASE, 0x08),
725 };
726
727 static struct platform_device ep93xx_wdt_device = {
728         .name           = "ep93xx-wdt",
729         .id             = -1,
730         .num_resources  = ARRAY_SIZE(ep93xx_wdt_resources),
731         .resource       = ep93xx_wdt_resources,
732 };
733
734 /*************************************************************************
735  * EP93xx IDE
736  *************************************************************************/
737 static struct resource ep93xx_ide_resources[] = {
738         DEFINE_RES_MEM(EP93XX_IDE_PHYS_BASE, 0x38),
739         DEFINE_RES_IRQ(IRQ_EP93XX_EXT3),
740 };
741
742 static struct platform_device ep93xx_ide_device = {
743         .name           = "ep93xx-ide",
744         .id             = -1,
745         .dev            = {
746                 .dma_mask               = &ep93xx_ide_device.dev.coherent_dma_mask,
747                 .coherent_dma_mask      = DMA_BIT_MASK(32),
748         },
749         .num_resources  = ARRAY_SIZE(ep93xx_ide_resources),
750         .resource       = ep93xx_ide_resources,
751 };
752
753 void __init ep93xx_register_ide(void)
754 {
755         platform_device_register(&ep93xx_ide_device);
756 }
757
758 int ep93xx_ide_acquire_gpio(struct platform_device *pdev)
759 {
760         int err;
761         int i;
762
763         err = gpio_request(EP93XX_GPIO_LINE_EGPIO2, dev_name(&pdev->dev));
764         if (err)
765                 return err;
766         err = gpio_request(EP93XX_GPIO_LINE_EGPIO15, dev_name(&pdev->dev));
767         if (err)
768                 goto fail_egpio15;
769         for (i = 2; i < 8; i++) {
770                 err = gpio_request(EP93XX_GPIO_LINE_E(i), dev_name(&pdev->dev));
771                 if (err)
772                         goto fail_gpio_e;
773         }
774         for (i = 4; i < 8; i++) {
775                 err = gpio_request(EP93XX_GPIO_LINE_G(i), dev_name(&pdev->dev));
776                 if (err)
777                         goto fail_gpio_g;
778         }
779         for (i = 0; i < 8; i++) {
780                 err = gpio_request(EP93XX_GPIO_LINE_H(i), dev_name(&pdev->dev));
781                 if (err)
782                         goto fail_gpio_h;
783         }
784
785         /* GPIO ports E[7:2], G[7:4] and H used by IDE */
786         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_EONIDE |
787                                  EP93XX_SYSCON_DEVCFG_GONIDE |
788                                  EP93XX_SYSCON_DEVCFG_HONIDE);
789         return 0;
790
791 fail_gpio_h:
792         for (--i; i >= 0; --i)
793                 gpio_free(EP93XX_GPIO_LINE_H(i));
794         i = 8;
795 fail_gpio_g:
796         for (--i; i >= 4; --i)
797                 gpio_free(EP93XX_GPIO_LINE_G(i));
798         i = 8;
799 fail_gpio_e:
800         for (--i; i >= 2; --i)
801                 gpio_free(EP93XX_GPIO_LINE_E(i));
802         gpio_free(EP93XX_GPIO_LINE_EGPIO15);
803 fail_egpio15:
804         gpio_free(EP93XX_GPIO_LINE_EGPIO2);
805         return err;
806 }
807 EXPORT_SYMBOL(ep93xx_ide_acquire_gpio);
808
809 void ep93xx_ide_release_gpio(struct platform_device *pdev)
810 {
811         int i;
812
813         for (i = 2; i < 8; i++)
814                 gpio_free(EP93XX_GPIO_LINE_E(i));
815         for (i = 4; i < 8; i++)
816                 gpio_free(EP93XX_GPIO_LINE_G(i));
817         for (i = 0; i < 8; i++)
818                 gpio_free(EP93XX_GPIO_LINE_H(i));
819         gpio_free(EP93XX_GPIO_LINE_EGPIO15);
820         gpio_free(EP93XX_GPIO_LINE_EGPIO2);
821
822
823         /* GPIO ports E[7:2], G[7:4] and H used by GPIO */
824         ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_EONIDE |
825                                EP93XX_SYSCON_DEVCFG_GONIDE |
826                                EP93XX_SYSCON_DEVCFG_HONIDE);
827 }
828 EXPORT_SYMBOL(ep93xx_ide_release_gpio);
829
830 /*************************************************************************
831  * EP93xx ADC
832  *************************************************************************/
833 static struct resource ep93xx_adc_resources[] = {
834         DEFINE_RES_MEM(EP93XX_ADC_PHYS_BASE, 0x28),
835         DEFINE_RES_IRQ(IRQ_EP93XX_TOUCH),
836 };
837
838 static struct platform_device ep93xx_adc_device = {
839         .name           = "ep93xx-adc",
840         .id             = -1,
841         .num_resources  = ARRAY_SIZE(ep93xx_adc_resources),
842         .resource       = ep93xx_adc_resources,
843 };
844
845 void __init ep93xx_register_adc(void)
846 {
847         /* Power up ADC, deactivate Touch Screen Controller */
848         ep93xx_devcfg_set_clear(EP93XX_SYSCON_DEVCFG_TIN,
849                                 EP93XX_SYSCON_DEVCFG_ADCPD);
850
851         platform_device_register(&ep93xx_adc_device);
852 }
853
854 /*************************************************************************
855  * EP93xx Security peripheral
856  *************************************************************************/
857
858 /*
859  * The Maverick Key is 256 bits of micro fuses blown at the factory during
860  * manufacturing to uniquely identify a part.
861  *
862  * See: http://arm.cirrus.com/forum/viewtopic.php?t=486&highlight=maverick+key
863  */
864 #define EP93XX_SECURITY_REG(x)          (EP93XX_SECURITY_BASE + (x))
865 #define EP93XX_SECURITY_SECFLG          EP93XX_SECURITY_REG(0x2400)
866 #define EP93XX_SECURITY_FUSEFLG         EP93XX_SECURITY_REG(0x2410)
867 #define EP93XX_SECURITY_UNIQID          EP93XX_SECURITY_REG(0x2440)
868 #define EP93XX_SECURITY_UNIQCHK         EP93XX_SECURITY_REG(0x2450)
869 #define EP93XX_SECURITY_UNIQVAL         EP93XX_SECURITY_REG(0x2460)
870 #define EP93XX_SECURITY_SECID1          EP93XX_SECURITY_REG(0x2500)
871 #define EP93XX_SECURITY_SECID2          EP93XX_SECURITY_REG(0x2504)
872 #define EP93XX_SECURITY_SECCHK1         EP93XX_SECURITY_REG(0x2520)
873 #define EP93XX_SECURITY_SECCHK2         EP93XX_SECURITY_REG(0x2524)
874 #define EP93XX_SECURITY_UNIQID2         EP93XX_SECURITY_REG(0x2700)
875 #define EP93XX_SECURITY_UNIQID3         EP93XX_SECURITY_REG(0x2704)
876 #define EP93XX_SECURITY_UNIQID4         EP93XX_SECURITY_REG(0x2708)
877 #define EP93XX_SECURITY_UNIQID5         EP93XX_SECURITY_REG(0x270c)
878
879 static char ep93xx_soc_id[33];
880
881 static const char __init *ep93xx_get_soc_id(void)
882 {
883         unsigned int id, id2, id3, id4, id5;
884
885         if (__raw_readl(EP93XX_SECURITY_UNIQVAL) != 1)
886                 return "bad Hamming code";
887
888         id = __raw_readl(EP93XX_SECURITY_UNIQID);
889         id2 = __raw_readl(EP93XX_SECURITY_UNIQID2);
890         id3 = __raw_readl(EP93XX_SECURITY_UNIQID3);
891         id4 = __raw_readl(EP93XX_SECURITY_UNIQID4);
892         id5 = __raw_readl(EP93XX_SECURITY_UNIQID5);
893
894         if (id != id2)
895                 return "invalid";
896
897         /* Toss the unique ID into the entropy pool */
898         add_device_randomness(&id2, 4);
899         add_device_randomness(&id3, 4);
900         add_device_randomness(&id4, 4);
901         add_device_randomness(&id5, 4);
902
903         snprintf(ep93xx_soc_id, sizeof(ep93xx_soc_id),
904                  "%08x%08x%08x%08x", id2, id3, id4, id5);
905
906         return ep93xx_soc_id;
907 }
908
909 static const char __init *ep93xx_get_soc_rev(void)
910 {
911         int rev = ep93xx_chip_revision();
912
913         switch (rev) {
914         case EP93XX_CHIP_REV_D0:
915                 return "D0";
916         case EP93XX_CHIP_REV_D1:
917                 return "D1";
918         case EP93XX_CHIP_REV_E0:
919                 return "E0";
920         case EP93XX_CHIP_REV_E1:
921                 return "E1";
922         case EP93XX_CHIP_REV_E2:
923                 return "E2";
924         default:
925                 return "unknown";
926         }
927 }
928
929 static const char __init *ep93xx_get_machine_name(void)
930 {
931         return kasprintf(GFP_KERNEL,"%s", machine_desc->name);
932 }
933
934 static struct device __init *ep93xx_init_soc(void)
935 {
936         struct soc_device_attribute *soc_dev_attr;
937         struct soc_device *soc_dev;
938
939         soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
940         if (!soc_dev_attr)
941                 return NULL;
942
943         soc_dev_attr->machine = ep93xx_get_machine_name();
944         soc_dev_attr->family = "Cirrus Logic EP93xx";
945         soc_dev_attr->revision = ep93xx_get_soc_rev();
946         soc_dev_attr->soc_id = ep93xx_get_soc_id();
947
948         soc_dev = soc_device_register(soc_dev_attr);
949         if (IS_ERR(soc_dev)) {
950                 kfree(soc_dev_attr->machine);
951                 kfree(soc_dev_attr);
952                 return NULL;
953         }
954
955         return soc_device_to_device(soc_dev);
956 }
957
958 struct device __init *ep93xx_init_devices(void)
959 {
960         struct device *parent;
961
962         /* Disallow access to MaverickCrunch initially */
963         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_CPENA);
964
965         /* Default all ports to GPIO */
966         ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_KEYS |
967                                EP93XX_SYSCON_DEVCFG_GONK |
968                                EP93XX_SYSCON_DEVCFG_EONIDE |
969                                EP93XX_SYSCON_DEVCFG_GONIDE |
970                                EP93XX_SYSCON_DEVCFG_HONIDE);
971
972         parent = ep93xx_init_soc();
973
974         /* Get the GPIO working early, other devices need it */
975         platform_device_register(&ep93xx_gpio_device);
976
977         amba_device_register(&uart1_device, &iomem_resource);
978         amba_device_register(&uart2_device, &iomem_resource);
979         amba_device_register(&uart3_device, &iomem_resource);
980
981         platform_device_register(&ep93xx_rtc_device);
982         platform_device_register(&ep93xx_ohci_device);
983         platform_device_register(&ep93xx_wdt_device);
984
985         gpio_led_register_device(-1, &ep93xx_led_data);
986
987         return parent;
988 }
989
990 void ep93xx_restart(enum reboot_mode mode, const char *cmd)
991 {
992         /*
993          * Set then clear the SWRST bit to initiate a software reset
994          */
995         ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_SWRST);
996         ep93xx_devcfg_clear_bits(EP93XX_SYSCON_DEVCFG_SWRST);
997
998         while (1)
999                 ;
1000 }
1001
1002 void __init ep93xx_init_late(void)
1003 {
1004         crunch_init();
1005 }