2 * Copyright (C) 2005-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/delay.h>
10 #include <linux/dw_dmac.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/slab.h>
16 #include <linux/gpio.h>
17 #include <linux/spi/spi.h>
18 #include <linux/usb/atmel_usba_udc.h>
20 #include <mach/atmel-mci.h>
21 #include <linux/atmel-mci.h>
26 #include <mach/at32ap700x.h>
27 #include <mach/board.h>
28 #include <mach/hmatrix.h>
29 #include <mach/portmux.h>
30 #include <mach/sram.h>
32 #include <sound/atmel-abdac.h>
33 #include <sound/atmel-ac97c.h>
35 #include <video/atmel_lcdc.h>
45 .end = base + 0x3ff, \
46 .flags = IORESOURCE_MEM, \
52 .flags = IORESOURCE_IRQ, \
54 #define NAMED_IRQ(num, _name) \
59 .flags = IORESOURCE_IRQ, \
62 /* REVISIT these assume *every* device supports DMA, but several
63 * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
65 #define DEFINE_DEV(_name, _id) \
66 static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
67 static struct platform_device _name##_id##_device = { \
71 .dma_mask = &_name##_id##_dma_mask, \
72 .coherent_dma_mask = DMA_BIT_MASK(32), \
74 .resource = _name##_id##_resource, \
75 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
77 #define DEFINE_DEV_DATA(_name, _id) \
78 static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
79 static struct platform_device _name##_id##_device = { \
83 .dma_mask = &_name##_id##_dma_mask, \
84 .platform_data = &_name##_id##_data, \
85 .coherent_dma_mask = DMA_BIT_MASK(32), \
87 .resource = _name##_id##_resource, \
88 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
91 #define select_peripheral(port, pin_mask, periph, flags) \
92 at32_select_periph(GPIO_##port##_BASE, pin_mask, \
95 #define DEV_CLK(_name, devname, bus, _index) \
96 static struct clk devname##_##_name = { \
98 .dev = &devname##_device.dev, \
99 .parent = &bus##_clk, \
100 .mode = bus##_clk_mode, \
101 .get_rate = bus##_clk_get_rate, \
105 static DEFINE_SPINLOCK(pm_lock
);
107 static struct clk osc0
;
108 static struct clk osc1
;
110 static unsigned long osc_get_rate(struct clk
*clk
)
112 return at32_board_osc_rates
[clk
->index
];
115 static unsigned long pll_get_rate(struct clk
*clk
, unsigned long control
)
117 unsigned long div
, mul
, rate
;
119 div
= PM_BFEXT(PLLDIV
, control
) + 1;
120 mul
= PM_BFEXT(PLLMUL
, control
) + 1;
122 rate
= clk
->parent
->get_rate(clk
->parent
);
123 rate
= (rate
+ div
/ 2) / div
;
129 static long pll_set_rate(struct clk
*clk
, unsigned long rate
,
133 unsigned long mul_best_fit
= 0;
135 unsigned long div_min
;
136 unsigned long div_max
;
137 unsigned long div_best_fit
= 0;
139 unsigned long pll_in
;
140 unsigned long actual
= 0;
141 unsigned long rate_error
;
142 unsigned long rate_error_prev
= ~0UL;
145 /* Rate must be between 80 MHz and 200 Mhz. */
146 if (rate
< 80000000UL || rate
> 200000000UL)
149 ctrl
= PM_BF(PLLOPT
, 4);
150 base
= clk
->parent
->get_rate(clk
->parent
);
152 /* PLL input frequency must be between 6 MHz and 32 MHz. */
153 div_min
= DIV_ROUND_UP(base
, 32000000UL);
154 div_max
= base
/ 6000000UL;
156 if (div_max
< div_min
)
159 for (div
= div_min
; div
<= div_max
; div
++) {
160 pll_in
= (base
+ div
/ 2) / div
;
161 mul
= (rate
+ pll_in
/ 2) / pll_in
;
166 actual
= pll_in
* mul
;
167 rate_error
= abs(actual
- rate
);
169 if (rate_error
< rate_error_prev
) {
172 rate_error_prev
= rate_error
;
179 if (div_best_fit
== 0)
182 ctrl
|= PM_BF(PLLMUL
, mul_best_fit
- 1);
183 ctrl
|= PM_BF(PLLDIV
, div_best_fit
- 1);
184 ctrl
|= PM_BF(PLLCOUNT
, 16);
186 if (clk
->parent
== &osc1
)
187 ctrl
|= PM_BIT(PLLOSC
);
194 static unsigned long pll0_get_rate(struct clk
*clk
)
198 control
= pm_readl(PLL0
);
200 return pll_get_rate(clk
, control
);
203 static void pll1_mode(struct clk
*clk
, int enabled
)
205 unsigned long timeout
;
209 ctrl
= pm_readl(PLL1
);
212 if (!PM_BFEXT(PLLMUL
, ctrl
) && !PM_BFEXT(PLLDIV
, ctrl
)) {
213 pr_debug("clk %s: failed to enable, rate not set\n",
218 ctrl
|= PM_BIT(PLLEN
);
219 pm_writel(PLL1
, ctrl
);
221 /* Wait for PLL lock. */
222 for (timeout
= 10000; timeout
; timeout
--) {
223 status
= pm_readl(ISR
);
224 if (status
& PM_BIT(LOCK1
))
229 if (!(status
& PM_BIT(LOCK1
)))
230 printk(KERN_ERR
"clk %s: timeout waiting for lock\n",
233 ctrl
&= ~PM_BIT(PLLEN
);
234 pm_writel(PLL1
, ctrl
);
238 static unsigned long pll1_get_rate(struct clk
*clk
)
242 control
= pm_readl(PLL1
);
244 return pll_get_rate(clk
, control
);
247 static long pll1_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
250 unsigned long actual_rate
;
252 actual_rate
= pll_set_rate(clk
, rate
, &ctrl
);
255 if (actual_rate
!= rate
)
259 pr_debug(KERN_INFO
"clk %s: new rate %lu (actual rate %lu)\n",
260 clk
->name
, rate
, actual_rate
);
261 pm_writel(PLL1
, ctrl
);
267 static int pll1_set_parent(struct clk
*clk
, struct clk
*parent
)
274 ctrl
= pm_readl(PLL1
);
275 WARN_ON(ctrl
& PM_BIT(PLLEN
));
278 ctrl
&= ~PM_BIT(PLLOSC
);
279 else if (parent
== &osc1
)
280 ctrl
|= PM_BIT(PLLOSC
);
284 pm_writel(PLL1
, ctrl
);
285 clk
->parent
= parent
;
291 * The AT32AP7000 has five primary clock sources: One 32kHz
292 * oscillator, two crystal oscillators and two PLLs.
294 static struct clk osc32k
= {
296 .get_rate
= osc_get_rate
,
300 static struct clk osc0
= {
302 .get_rate
= osc_get_rate
,
306 static struct clk osc1
= {
308 .get_rate
= osc_get_rate
,
311 static struct clk pll0
= {
313 .get_rate
= pll0_get_rate
,
316 static struct clk pll1
= {
319 .get_rate
= pll1_get_rate
,
320 .set_rate
= pll1_set_rate
,
321 .set_parent
= pll1_set_parent
,
326 * The main clock can be either osc0 or pll0. The boot loader may
327 * have chosen one for us, so we don't really know which one until we
328 * have a look at the SM.
330 static struct clk
*main_clock
;
333 * Synchronous clocks are generated from the main clock. The clocks
334 * must satisfy the constraint
335 * fCPU >= fHSB >= fPB
336 * i.e. each clock must not be faster than its parent.
338 static unsigned long bus_clk_get_rate(struct clk
*clk
, unsigned int shift
)
340 return main_clock
->get_rate(main_clock
) >> shift
;
343 static void cpu_clk_mode(struct clk
*clk
, int enabled
)
348 spin_lock_irqsave(&pm_lock
, flags
);
349 mask
= pm_readl(CPU_MASK
);
351 mask
|= 1 << clk
->index
;
353 mask
&= ~(1 << clk
->index
);
354 pm_writel(CPU_MASK
, mask
);
355 spin_unlock_irqrestore(&pm_lock
, flags
);
358 static unsigned long cpu_clk_get_rate(struct clk
*clk
)
360 unsigned long cksel
, shift
= 0;
362 cksel
= pm_readl(CKSEL
);
363 if (cksel
& PM_BIT(CPUDIV
))
364 shift
= PM_BFEXT(CPUSEL
, cksel
) + 1;
366 return bus_clk_get_rate(clk
, shift
);
369 static long cpu_clk_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
372 unsigned long parent_rate
, child_div
, actual_rate
, div
;
374 parent_rate
= clk
->parent
->get_rate(clk
->parent
);
375 control
= pm_readl(CKSEL
);
377 if (control
& PM_BIT(HSBDIV
))
378 child_div
= 1 << (PM_BFEXT(HSBSEL
, control
) + 1);
382 if (rate
> 3 * (parent_rate
/ 4) || child_div
== 1) {
383 actual_rate
= parent_rate
;
384 control
&= ~PM_BIT(CPUDIV
);
387 div
= (parent_rate
+ rate
/ 2) / rate
;
390 cpusel
= (div
> 1) ? (fls(div
) - 2) : 0;
391 control
= PM_BIT(CPUDIV
) | PM_BFINS(CPUSEL
, cpusel
, control
);
392 actual_rate
= parent_rate
/ (1 << (cpusel
+ 1));
395 pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
396 clk
->name
, rate
, actual_rate
);
399 pm_writel(CKSEL
, control
);
404 static void hsb_clk_mode(struct clk
*clk
, int enabled
)
409 spin_lock_irqsave(&pm_lock
, flags
);
410 mask
= pm_readl(HSB_MASK
);
412 mask
|= 1 << clk
->index
;
414 mask
&= ~(1 << clk
->index
);
415 pm_writel(HSB_MASK
, mask
);
416 spin_unlock_irqrestore(&pm_lock
, flags
);
419 static unsigned long hsb_clk_get_rate(struct clk
*clk
)
421 unsigned long cksel
, shift
= 0;
423 cksel
= pm_readl(CKSEL
);
424 if (cksel
& PM_BIT(HSBDIV
))
425 shift
= PM_BFEXT(HSBSEL
, cksel
) + 1;
427 return bus_clk_get_rate(clk
, shift
);
430 void pba_clk_mode(struct clk
*clk
, int enabled
)
435 spin_lock_irqsave(&pm_lock
, flags
);
436 mask
= pm_readl(PBA_MASK
);
438 mask
|= 1 << clk
->index
;
440 mask
&= ~(1 << clk
->index
);
441 pm_writel(PBA_MASK
, mask
);
442 spin_unlock_irqrestore(&pm_lock
, flags
);
445 unsigned long pba_clk_get_rate(struct clk
*clk
)
447 unsigned long cksel
, shift
= 0;
449 cksel
= pm_readl(CKSEL
);
450 if (cksel
& PM_BIT(PBADIV
))
451 shift
= PM_BFEXT(PBASEL
, cksel
) + 1;
453 return bus_clk_get_rate(clk
, shift
);
456 static void pbb_clk_mode(struct clk
*clk
, int enabled
)
461 spin_lock_irqsave(&pm_lock
, flags
);
462 mask
= pm_readl(PBB_MASK
);
464 mask
|= 1 << clk
->index
;
466 mask
&= ~(1 << clk
->index
);
467 pm_writel(PBB_MASK
, mask
);
468 spin_unlock_irqrestore(&pm_lock
, flags
);
471 static unsigned long pbb_clk_get_rate(struct clk
*clk
)
473 unsigned long cksel
, shift
= 0;
475 cksel
= pm_readl(CKSEL
);
476 if (cksel
& PM_BIT(PBBDIV
))
477 shift
= PM_BFEXT(PBBSEL
, cksel
) + 1;
479 return bus_clk_get_rate(clk
, shift
);
482 static struct clk cpu_clk
= {
484 .get_rate
= cpu_clk_get_rate
,
485 .set_rate
= cpu_clk_set_rate
,
488 static struct clk hsb_clk
= {
491 .get_rate
= hsb_clk_get_rate
,
493 static struct clk pba_clk
= {
496 .mode
= hsb_clk_mode
,
497 .get_rate
= pba_clk_get_rate
,
500 static struct clk pbb_clk
= {
503 .mode
= hsb_clk_mode
,
504 .get_rate
= pbb_clk_get_rate
,
509 /* --------------------------------------------------------------------
510 * Generic Clock operations
511 * -------------------------------------------------------------------- */
513 static void genclk_mode(struct clk
*clk
, int enabled
)
517 control
= pm_readl(GCCTRL(clk
->index
));
519 control
|= PM_BIT(CEN
);
521 control
&= ~PM_BIT(CEN
);
522 pm_writel(GCCTRL(clk
->index
), control
);
525 static unsigned long genclk_get_rate(struct clk
*clk
)
528 unsigned long div
= 1;
530 control
= pm_readl(GCCTRL(clk
->index
));
531 if (control
& PM_BIT(DIVEN
))
532 div
= 2 * (PM_BFEXT(DIV
, control
) + 1);
534 return clk
->parent
->get_rate(clk
->parent
) / div
;
537 static long genclk_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
540 unsigned long parent_rate
, actual_rate
, div
;
542 parent_rate
= clk
->parent
->get_rate(clk
->parent
);
543 control
= pm_readl(GCCTRL(clk
->index
));
545 if (rate
> 3 * parent_rate
/ 4) {
546 actual_rate
= parent_rate
;
547 control
&= ~PM_BIT(DIVEN
);
549 div
= (parent_rate
+ rate
) / (2 * rate
) - 1;
550 control
= PM_BFINS(DIV
, div
, control
) | PM_BIT(DIVEN
);
551 actual_rate
= parent_rate
/ (2 * (div
+ 1));
554 dev_dbg(clk
->dev
, "clk %s: new rate %lu (actual rate %lu)\n",
555 clk
->name
, rate
, actual_rate
);
558 pm_writel(GCCTRL(clk
->index
), control
);
563 int genclk_set_parent(struct clk
*clk
, struct clk
*parent
)
567 dev_dbg(clk
->dev
, "clk %s: new parent %s (was %s)\n",
568 clk
->name
, parent
->name
, clk
->parent
->name
);
570 control
= pm_readl(GCCTRL(clk
->index
));
572 if (parent
== &osc1
|| parent
== &pll1
)
573 control
|= PM_BIT(OSCSEL
);
574 else if (parent
== &osc0
|| parent
== &pll0
)
575 control
&= ~PM_BIT(OSCSEL
);
579 if (parent
== &pll0
|| parent
== &pll1
)
580 control
|= PM_BIT(PLLSEL
);
582 control
&= ~PM_BIT(PLLSEL
);
584 pm_writel(GCCTRL(clk
->index
), control
);
585 clk
->parent
= parent
;
590 static void __init
genclk_init_parent(struct clk
*clk
)
595 BUG_ON(clk
->index
> 7);
597 control
= pm_readl(GCCTRL(clk
->index
));
598 if (control
& PM_BIT(OSCSEL
))
599 parent
= (control
& PM_BIT(PLLSEL
)) ? &pll1
: &osc1
;
601 parent
= (control
& PM_BIT(PLLSEL
)) ? &pll0
: &osc0
;
603 clk
->parent
= parent
;
606 static struct dw_dma_platform_data dw_dmac0_data
= {
610 static struct resource dw_dmac0_resource
[] = {
614 DEFINE_DEV_DATA(dw_dmac
, 0);
615 DEV_CLK(hclk
, dw_dmac0
, hsb
, 10);
617 /* --------------------------------------------------------------------
619 * -------------------------------------------------------------------- */
620 static struct resource at32_pm0_resource
[] = {
624 .flags
= IORESOURCE_MEM
,
629 static struct resource at32ap700x_rtc0_resource
[] = {
633 .flags
= IORESOURCE_MEM
,
638 static struct resource at32_wdt0_resource
[] = {
642 .flags
= IORESOURCE_MEM
,
646 static struct resource at32_eic0_resource
[] = {
650 .flags
= IORESOURCE_MEM
,
655 DEFINE_DEV(at32_pm
, 0);
656 DEFINE_DEV(at32ap700x_rtc
, 0);
657 DEFINE_DEV(at32_wdt
, 0);
658 DEFINE_DEV(at32_eic
, 0);
661 * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
664 static struct clk at32_pm_pclk
= {
666 .dev
= &at32_pm0_device
.dev
,
668 .mode
= pbb_clk_mode
,
669 .get_rate
= pbb_clk_get_rate
,
674 static struct resource intc0_resource
[] = {
677 struct platform_device at32_intc0_device
= {
680 .resource
= intc0_resource
,
681 .num_resources
= ARRAY_SIZE(intc0_resource
),
683 DEV_CLK(pclk
, at32_intc0
, pbb
, 1);
685 static struct clk ebi_clk
= {
688 .mode
= hsb_clk_mode
,
689 .get_rate
= hsb_clk_get_rate
,
692 static struct clk hramc_clk
= {
695 .mode
= hsb_clk_mode
,
696 .get_rate
= hsb_clk_get_rate
,
700 static struct clk sdramc_clk
= {
701 .name
= "sdramc_clk",
703 .mode
= pbb_clk_mode
,
704 .get_rate
= pbb_clk_get_rate
,
709 static struct resource smc0_resource
[] = {
713 DEV_CLK(pclk
, smc0
, pbb
, 13);
714 DEV_CLK(mck
, smc0
, hsb
, 0);
716 static struct platform_device pdc_device
= {
720 DEV_CLK(hclk
, pdc
, hsb
, 4);
721 DEV_CLK(pclk
, pdc
, pba
, 16);
723 static struct clk pico_clk
= {
726 .mode
= cpu_clk_mode
,
727 .get_rate
= cpu_clk_get_rate
,
731 /* --------------------------------------------------------------------
733 * -------------------------------------------------------------------- */
735 struct clk at32_hmatrix_clk
= {
736 .name
= "hmatrix_clk",
738 .mode
= pbb_clk_mode
,
739 .get_rate
= pbb_clk_get_rate
,
745 * Set bits in the HMATRIX Special Function Register (SFR) used by the
746 * External Bus Interface (EBI). This can be used to enable special
747 * features like CompactFlash support, NAND Flash support, etc. on
748 * certain chipselects.
750 static inline void set_ebi_sfr_bits(u32 mask
)
752 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, mask
);
755 /* --------------------------------------------------------------------
757 * -------------------------------------------------------------------- */
759 static struct resource at32_tcb0_resource
[] = {
763 static struct platform_device at32_tcb0_device
= {
766 .resource
= at32_tcb0_resource
,
767 .num_resources
= ARRAY_SIZE(at32_tcb0_resource
),
769 DEV_CLK(t0_clk
, at32_tcb0
, pbb
, 3);
771 static struct resource at32_tcb1_resource
[] = {
775 static struct platform_device at32_tcb1_device
= {
778 .resource
= at32_tcb1_resource
,
779 .num_resources
= ARRAY_SIZE(at32_tcb1_resource
),
781 DEV_CLK(t0_clk
, at32_tcb1
, pbb
, 4);
783 /* --------------------------------------------------------------------
785 * -------------------------------------------------------------------- */
787 static struct resource pio0_resource
[] = {
792 DEV_CLK(mck
, pio0
, pba
, 10);
794 static struct resource pio1_resource
[] = {
799 DEV_CLK(mck
, pio1
, pba
, 11);
801 static struct resource pio2_resource
[] = {
806 DEV_CLK(mck
, pio2
, pba
, 12);
808 static struct resource pio3_resource
[] = {
813 DEV_CLK(mck
, pio3
, pba
, 13);
815 static struct resource pio4_resource
[] = {
820 DEV_CLK(mck
, pio4
, pba
, 14);
822 static int __init
system_device_init(void)
824 platform_device_register(&at32_pm0_device
);
825 platform_device_register(&at32_intc0_device
);
826 platform_device_register(&at32ap700x_rtc0_device
);
827 platform_device_register(&at32_wdt0_device
);
828 platform_device_register(&at32_eic0_device
);
829 platform_device_register(&smc0_device
);
830 platform_device_register(&pdc_device
);
831 platform_device_register(&dw_dmac0_device
);
833 platform_device_register(&at32_tcb0_device
);
834 platform_device_register(&at32_tcb1_device
);
836 platform_device_register(&pio0_device
);
837 platform_device_register(&pio1_device
);
838 platform_device_register(&pio2_device
);
839 platform_device_register(&pio3_device
);
840 platform_device_register(&pio4_device
);
844 core_initcall(system_device_init
);
846 /* --------------------------------------------------------------------
848 * -------------------------------------------------------------------- */
849 static struct resource atmel_psif0_resource
[] __initdata
= {
853 .flags
= IORESOURCE_MEM
,
857 static struct clk atmel_psif0_pclk
= {
860 .mode
= pba_clk_mode
,
861 .get_rate
= pba_clk_get_rate
,
865 static struct resource atmel_psif1_resource
[] __initdata
= {
869 .flags
= IORESOURCE_MEM
,
873 static struct clk atmel_psif1_pclk
= {
876 .mode
= pba_clk_mode
,
877 .get_rate
= pba_clk_get_rate
,
881 struct platform_device
*__init
at32_add_device_psif(unsigned int id
)
883 struct platform_device
*pdev
;
886 if (!(id
== 0 || id
== 1))
889 pdev
= platform_device_alloc("atmel_psif", id
);
895 pin_mask
= (1 << 8) | (1 << 9); /* CLOCK & DATA */
897 if (platform_device_add_resources(pdev
, atmel_psif0_resource
,
898 ARRAY_SIZE(atmel_psif0_resource
)))
899 goto err_add_resources
;
900 atmel_psif0_pclk
.dev
= &pdev
->dev
;
901 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
904 pin_mask
= (1 << 11) | (1 << 12); /* CLOCK & DATA */
906 if (platform_device_add_resources(pdev
, atmel_psif1_resource
,
907 ARRAY_SIZE(atmel_psif1_resource
)))
908 goto err_add_resources
;
909 atmel_psif1_pclk
.dev
= &pdev
->dev
;
910 select_peripheral(PIOB
, pin_mask
, PERIPH_A
, 0);
916 platform_device_add(pdev
);
920 platform_device_put(pdev
);
924 /* --------------------------------------------------------------------
926 * -------------------------------------------------------------------- */
928 static struct atmel_uart_data atmel_usart0_data
= {
932 static struct resource atmel_usart0_resource
[] = {
936 DEFINE_DEV_DATA(atmel_usart
, 0);
937 DEV_CLK(usart
, atmel_usart0
, pba
, 3);
939 static struct atmel_uart_data atmel_usart1_data
= {
943 static struct resource atmel_usart1_resource
[] = {
947 DEFINE_DEV_DATA(atmel_usart
, 1);
948 DEV_CLK(usart
, atmel_usart1
, pba
, 4);
950 static struct atmel_uart_data atmel_usart2_data
= {
954 static struct resource atmel_usart2_resource
[] = {
958 DEFINE_DEV_DATA(atmel_usart
, 2);
959 DEV_CLK(usart
, atmel_usart2
, pba
, 5);
961 static struct atmel_uart_data atmel_usart3_data
= {
965 static struct resource atmel_usart3_resource
[] = {
969 DEFINE_DEV_DATA(atmel_usart
, 3);
970 DEV_CLK(usart
, atmel_usart3
, pba
, 6);
972 static inline void configure_usart0_pins(int flags
)
974 u32 pin_mask
= (1 << 8) | (1 << 9); /* RXD & TXD */
975 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 6);
976 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 7);
977 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 10);
979 select_peripheral(PIOA
, pin_mask
, PERIPH_B
, AT32_GPIOF_PULLUP
);
982 static inline void configure_usart1_pins(int flags
)
984 u32 pin_mask
= (1 << 17) | (1 << 18); /* RXD & TXD */
985 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 19);
986 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 20);
987 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 16);
989 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, AT32_GPIOF_PULLUP
);
992 static inline void configure_usart2_pins(int flags
)
994 u32 pin_mask
= (1 << 26) | (1 << 27); /* RXD & TXD */
995 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 30);
996 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 29);
997 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 28);
999 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, AT32_GPIOF_PULLUP
);
1002 static inline void configure_usart3_pins(int flags
)
1004 u32 pin_mask
= (1 << 18) | (1 << 17); /* RXD & TXD */
1005 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 16);
1006 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 15);
1007 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 19);
1009 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, AT32_GPIOF_PULLUP
);
1012 static struct platform_device
*__initdata at32_usarts
[4];
1014 void __init
at32_map_usart(unsigned int hw_id
, unsigned int line
, int flags
)
1016 struct platform_device
*pdev
;
1020 pdev
= &atmel_usart0_device
;
1021 configure_usart0_pins(flags
);
1024 pdev
= &atmel_usart1_device
;
1025 configure_usart1_pins(flags
);
1028 pdev
= &atmel_usart2_device
;
1029 configure_usart2_pins(flags
);
1032 pdev
= &atmel_usart3_device
;
1033 configure_usart3_pins(flags
);
1039 if (PXSEG(pdev
->resource
[0].start
) == P4SEG
) {
1040 /* Addresses in the P4 segment are permanently mapped 1:1 */
1041 struct atmel_uart_data
*data
= pdev
->dev
.platform_data
;
1042 data
->regs
= (void __iomem
*)pdev
->resource
[0].start
;
1046 at32_usarts
[line
] = pdev
;
1049 struct platform_device
*__init
at32_add_device_usart(unsigned int id
)
1051 platform_device_register(at32_usarts
[id
]);
1052 return at32_usarts
[id
];
1055 struct platform_device
*atmel_default_console_device
;
1057 void __init
at32_setup_serial_console(unsigned int usart_id
)
1059 atmel_default_console_device
= at32_usarts
[usart_id
];
1062 /* --------------------------------------------------------------------
1064 * -------------------------------------------------------------------- */
1066 #ifdef CONFIG_CPU_AT32AP7000
1067 static struct eth_platform_data macb0_data
;
1068 static struct resource macb0_resource
[] = {
1072 DEFINE_DEV_DATA(macb
, 0);
1073 DEV_CLK(hclk
, macb0
, hsb
, 8);
1074 DEV_CLK(pclk
, macb0
, pbb
, 6);
1076 static struct eth_platform_data macb1_data
;
1077 static struct resource macb1_resource
[] = {
1081 DEFINE_DEV_DATA(macb
, 1);
1082 DEV_CLK(hclk
, macb1
, hsb
, 9);
1083 DEV_CLK(pclk
, macb1
, pbb
, 7);
1085 struct platform_device
*__init
1086 at32_add_device_eth(unsigned int id
, struct eth_platform_data
*data
)
1088 struct platform_device
*pdev
;
1093 pdev
= &macb0_device
;
1095 pin_mask
= (1 << 3); /* TXD0 */
1096 pin_mask
|= (1 << 4); /* TXD1 */
1097 pin_mask
|= (1 << 7); /* TXEN */
1098 pin_mask
|= (1 << 8); /* TXCK */
1099 pin_mask
|= (1 << 9); /* RXD0 */
1100 pin_mask
|= (1 << 10); /* RXD1 */
1101 pin_mask
|= (1 << 13); /* RXER */
1102 pin_mask
|= (1 << 15); /* RXDV */
1103 pin_mask
|= (1 << 16); /* MDC */
1104 pin_mask
|= (1 << 17); /* MDIO */
1106 if (!data
->is_rmii
) {
1107 pin_mask
|= (1 << 0); /* COL */
1108 pin_mask
|= (1 << 1); /* CRS */
1109 pin_mask
|= (1 << 2); /* TXER */
1110 pin_mask
|= (1 << 5); /* TXD2 */
1111 pin_mask
|= (1 << 6); /* TXD3 */
1112 pin_mask
|= (1 << 11); /* RXD2 */
1113 pin_mask
|= (1 << 12); /* RXD3 */
1114 pin_mask
|= (1 << 14); /* RXCK */
1115 #ifndef CONFIG_BOARD_MIMC200
1116 pin_mask
|= (1 << 18); /* SPD */
1120 select_peripheral(PIOC
, pin_mask
, PERIPH_A
, 0);
1125 pdev
= &macb1_device
;
1127 pin_mask
= (1 << 13); /* TXD0 */
1128 pin_mask
|= (1 << 14); /* TXD1 */
1129 pin_mask
|= (1 << 11); /* TXEN */
1130 pin_mask
|= (1 << 12); /* TXCK */
1131 pin_mask
|= (1 << 10); /* RXD0 */
1132 pin_mask
|= (1 << 6); /* RXD1 */
1133 pin_mask
|= (1 << 5); /* RXER */
1134 pin_mask
|= (1 << 4); /* RXDV */
1135 pin_mask
|= (1 << 3); /* MDC */
1136 pin_mask
|= (1 << 2); /* MDIO */
1138 #ifndef CONFIG_BOARD_MIMC200
1140 pin_mask
|= (1 << 15); /* SPD */
1143 select_peripheral(PIOD
, pin_mask
, PERIPH_B
, 0);
1145 if (!data
->is_rmii
) {
1146 pin_mask
= (1 << 19); /* COL */
1147 pin_mask
|= (1 << 23); /* CRS */
1148 pin_mask
|= (1 << 26); /* TXER */
1149 pin_mask
|= (1 << 27); /* TXD2 */
1150 pin_mask
|= (1 << 28); /* TXD3 */
1151 pin_mask
|= (1 << 29); /* RXD2 */
1152 pin_mask
|= (1 << 30); /* RXD3 */
1153 pin_mask
|= (1 << 24); /* RXCK */
1155 select_peripheral(PIOC
, pin_mask
, PERIPH_B
, 0);
1163 memcpy(pdev
->dev
.platform_data
, data
, sizeof(struct eth_platform_data
));
1164 platform_device_register(pdev
);
1170 /* --------------------------------------------------------------------
1172 * -------------------------------------------------------------------- */
1173 static struct resource atmel_spi0_resource
[] = {
1177 DEFINE_DEV(atmel_spi
, 0);
1178 DEV_CLK(spi_clk
, atmel_spi0
, pba
, 0);
1180 static struct resource atmel_spi1_resource
[] = {
1184 DEFINE_DEV(atmel_spi
, 1);
1185 DEV_CLK(spi_clk
, atmel_spi1
, pba
, 1);
1188 at32_spi_setup_slaves(unsigned int bus_num
, struct spi_board_info
*b
, unsigned int n
)
1191 * Manage the chipselects as GPIOs, normally using the same pins
1192 * the SPI controller expects; but boards can use other pins.
1194 static u8 __initdata spi_pins
[][4] = {
1195 { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
1196 GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
1197 { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
1198 GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
1200 unsigned int pin
, mode
;
1202 /* There are only 2 SPI controllers */
1206 for (; n
; n
--, b
++) {
1207 b
->bus_num
= bus_num
;
1208 if (b
->chip_select
>= 4)
1210 pin
= (unsigned)b
->controller_data
;
1212 pin
= spi_pins
[bus_num
][b
->chip_select
];
1213 b
->controller_data
= (void *)pin
;
1215 mode
= AT32_GPIOF_OUTPUT
;
1216 if (!(b
->mode
& SPI_CS_HIGH
))
1217 mode
|= AT32_GPIOF_HIGH
;
1218 at32_select_gpio(pin
, mode
);
1222 struct platform_device
*__init
1223 at32_add_device_spi(unsigned int id
, struct spi_board_info
*b
, unsigned int n
)
1225 struct platform_device
*pdev
;
1230 pdev
= &atmel_spi0_device
;
1231 pin_mask
= (1 << 1) | (1 << 2); /* MOSI & SCK */
1233 /* pullup MISO so a level is always defined */
1234 select_peripheral(PIOA
, (1 << 0), PERIPH_A
, AT32_GPIOF_PULLUP
);
1235 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1237 at32_spi_setup_slaves(0, b
, n
);
1241 pdev
= &atmel_spi1_device
;
1242 pin_mask
= (1 << 1) | (1 << 5); /* MOSI */
1244 /* pullup MISO so a level is always defined */
1245 select_peripheral(PIOB
, (1 << 0), PERIPH_B
, AT32_GPIOF_PULLUP
);
1246 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, 0);
1248 at32_spi_setup_slaves(1, b
, n
);
1255 spi_register_board_info(b
, n
);
1256 platform_device_register(pdev
);
1260 /* --------------------------------------------------------------------
1262 * -------------------------------------------------------------------- */
1263 static struct resource atmel_twi0_resource
[] __initdata
= {
1267 static struct clk atmel_twi0_pclk
= {
1270 .mode
= pba_clk_mode
,
1271 .get_rate
= pba_clk_get_rate
,
1275 struct platform_device
*__init
at32_add_device_twi(unsigned int id
,
1276 struct i2c_board_info
*b
,
1279 struct platform_device
*pdev
;
1285 pdev
= platform_device_alloc("atmel_twi", id
);
1289 if (platform_device_add_resources(pdev
, atmel_twi0_resource
,
1290 ARRAY_SIZE(atmel_twi0_resource
)))
1291 goto err_add_resources
;
1293 pin_mask
= (1 << 6) | (1 << 7); /* SDA & SDL */
1295 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1297 atmel_twi0_pclk
.dev
= &pdev
->dev
;
1300 i2c_register_board_info(id
, b
, n
);
1302 platform_device_add(pdev
);
1306 platform_device_put(pdev
);
1310 /* --------------------------------------------------------------------
1312 * -------------------------------------------------------------------- */
1313 static struct resource atmel_mci0_resource
[] __initdata
= {
1317 static struct clk atmel_mci0_pclk
= {
1320 .mode
= pbb_clk_mode
,
1321 .get_rate
= pbb_clk_get_rate
,
1325 struct platform_device
*__init
1326 at32_add_device_mci(unsigned int id
, struct mci_platform_data
*data
)
1328 struct platform_device
*pdev
;
1329 struct mci_dma_data
*slave
;
1333 if (id
!= 0 || !data
)
1336 /* Must have at least one usable slot */
1337 if (!data
->slot
[0].bus_width
&& !data
->slot
[1].bus_width
)
1340 pdev
= platform_device_alloc("atmel_mci", id
);
1344 if (platform_device_add_resources(pdev
, atmel_mci0_resource
,
1345 ARRAY_SIZE(atmel_mci0_resource
)))
1348 slave
= kzalloc(sizeof(struct mci_dma_data
), GFP_KERNEL
);
1352 slave
->sdata
.dma_dev
= &dw_dmac0_device
.dev
;
1353 slave
->sdata
.reg_width
= DW_DMA_SLAVE_WIDTH_32BIT
;
1354 slave
->sdata
.cfg_hi
= (DWC_CFGH_SRC_PER(0)
1355 | DWC_CFGH_DST_PER(1));
1356 slave
->sdata
.cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
1357 | DWC_CFGL_HS_SRC_POL
);
1359 data
->dma_slave
= slave
;
1361 if (platform_device_add_data(pdev
, data
,
1362 sizeof(struct mci_platform_data
)))
1365 /* CLK line is common to both slots */
1366 pioa_mask
= 1 << 10;
1368 switch (data
->slot
[0].bus_width
) {
1370 pioa_mask
|= 1 << 13; /* DATA1 */
1371 pioa_mask
|= 1 << 14; /* DATA2 */
1372 pioa_mask
|= 1 << 15; /* DATA3 */
1375 pioa_mask
|= 1 << 11; /* CMD */
1376 pioa_mask
|= 1 << 12; /* DATA0 */
1378 if (gpio_is_valid(data
->slot
[0].detect_pin
))
1379 at32_select_gpio(data
->slot
[0].detect_pin
, 0);
1380 if (gpio_is_valid(data
->slot
[0].wp_pin
))
1381 at32_select_gpio(data
->slot
[0].wp_pin
, 0);
1384 /* Slot is unused */
1390 select_peripheral(PIOA
, pioa_mask
, PERIPH_A
, 0);
1393 switch (data
->slot
[1].bus_width
) {
1395 piob_mask
|= 1 << 8; /* DATA1 */
1396 piob_mask
|= 1 << 9; /* DATA2 */
1397 piob_mask
|= 1 << 10; /* DATA3 */
1400 piob_mask
|= 1 << 6; /* CMD */
1401 piob_mask
|= 1 << 7; /* DATA0 */
1402 select_peripheral(PIOB
, piob_mask
, PERIPH_B
, 0);
1404 if (gpio_is_valid(data
->slot
[1].detect_pin
))
1405 at32_select_gpio(data
->slot
[1].detect_pin
, 0);
1406 if (gpio_is_valid(data
->slot
[1].wp_pin
))
1407 at32_select_gpio(data
->slot
[1].wp_pin
, 0);
1410 /* Slot is unused */
1413 if (!data
->slot
[0].bus_width
)
1416 data
->slot
[1].bus_width
= 0;
1420 atmel_mci0_pclk
.dev
= &pdev
->dev
;
1422 platform_device_add(pdev
);
1428 data
->dma_slave
= NULL
;
1429 platform_device_put(pdev
);
1433 /* --------------------------------------------------------------------
1435 * -------------------------------------------------------------------- */
1436 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
1437 static struct atmel_lcdfb_info atmel_lcdfb0_data
;
1438 static struct resource atmel_lcdfb0_resource
[] = {
1440 .start
= 0xff000000,
1442 .flags
= IORESOURCE_MEM
,
1446 /* Placeholder for pre-allocated fb memory */
1447 .start
= 0x00000000,
1452 DEFINE_DEV_DATA(atmel_lcdfb
, 0);
1453 DEV_CLK(hck1
, atmel_lcdfb0
, hsb
, 7);
1454 static struct clk atmel_lcdfb0_pixclk
= {
1456 .dev
= &atmel_lcdfb0_device
.dev
,
1457 .mode
= genclk_mode
,
1458 .get_rate
= genclk_get_rate
,
1459 .set_rate
= genclk_set_rate
,
1460 .set_parent
= genclk_set_parent
,
1464 struct platform_device
*__init
1465 at32_add_device_lcdc(unsigned int id
, struct atmel_lcdfb_info
*data
,
1466 unsigned long fbmem_start
, unsigned long fbmem_len
,
1469 struct platform_device
*pdev
;
1470 struct atmel_lcdfb_info
*info
;
1471 struct fb_monspecs
*monspecs
;
1472 struct fb_videomode
*modedb
;
1473 unsigned int modedb_size
;
1474 u32 portc_mask
, portd_mask
, porte_mask
;
1477 * Do a deep copy of the fb data, monspecs and modedb. Make
1478 * sure all allocations are done before setting up the
1481 monspecs
= kmemdup(data
->default_monspecs
,
1482 sizeof(struct fb_monspecs
), GFP_KERNEL
);
1486 modedb_size
= sizeof(struct fb_videomode
) * monspecs
->modedb_len
;
1487 modedb
= kmemdup(monspecs
->modedb
, modedb_size
, GFP_KERNEL
);
1489 goto err_dup_modedb
;
1490 monspecs
->modedb
= modedb
;
1494 pdev
= &atmel_lcdfb0_device
;
1496 if (pin_mask
== 0ULL)
1497 /* Default to "full" lcdc control signals and 24bit */
1498 pin_mask
= ATMEL_LCDC_PRI_24BIT
| ATMEL_LCDC_PRI_CONTROL
;
1500 /* LCDC on port C */
1501 portc_mask
= pin_mask
& 0xfff80000;
1502 select_peripheral(PIOC
, portc_mask
, PERIPH_A
, 0);
1504 /* LCDC on port D */
1505 portd_mask
= pin_mask
& 0x0003ffff;
1506 select_peripheral(PIOD
, portd_mask
, PERIPH_A
, 0);
1508 /* LCDC on port E */
1509 porte_mask
= (pin_mask
>> 32) & 0x0007ffff;
1510 select_peripheral(PIOE
, porte_mask
, PERIPH_B
, 0);
1512 clk_set_parent(&atmel_lcdfb0_pixclk
, &pll0
);
1513 clk_set_rate(&atmel_lcdfb0_pixclk
, clk_get_rate(&pll0
));
1517 goto err_invalid_id
;
1521 pdev
->resource
[2].start
= fbmem_start
;
1522 pdev
->resource
[2].end
= fbmem_start
+ fbmem_len
- 1;
1523 pdev
->resource
[2].flags
= IORESOURCE_MEM
;
1526 info
= pdev
->dev
.platform_data
;
1527 memcpy(info
, data
, sizeof(struct atmel_lcdfb_info
));
1528 info
->default_monspecs
= monspecs
;
1530 platform_device_register(pdev
);
1541 /* --------------------------------------------------------------------
1543 * -------------------------------------------------------------------- */
1544 static struct resource atmel_pwm0_resource
[] __initdata
= {
1548 static struct clk atmel_pwm0_mck
= {
1551 .mode
= pbb_clk_mode
,
1552 .get_rate
= pbb_clk_get_rate
,
1556 struct platform_device
*__init
at32_add_device_pwm(u32 mask
)
1558 struct platform_device
*pdev
;
1564 pdev
= platform_device_alloc("atmel_pwm", 0);
1568 if (platform_device_add_resources(pdev
, atmel_pwm0_resource
,
1569 ARRAY_SIZE(atmel_pwm0_resource
)))
1572 if (platform_device_add_data(pdev
, &mask
, sizeof(mask
)))
1576 if (mask
& (1 << 0))
1577 pin_mask
|= (1 << 28);
1578 if (mask
& (1 << 1))
1579 pin_mask
|= (1 << 29);
1581 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1584 if (mask
& (1 << 2))
1585 pin_mask
|= (1 << 21);
1586 if (mask
& (1 << 3))
1587 pin_mask
|= (1 << 22);
1589 select_peripheral(PIOA
, pin_mask
, PERIPH_B
, 0);
1591 atmel_pwm0_mck
.dev
= &pdev
->dev
;
1593 platform_device_add(pdev
);
1598 platform_device_put(pdev
);
1602 /* --------------------------------------------------------------------
1604 * -------------------------------------------------------------------- */
1605 static struct resource ssc0_resource
[] = {
1610 DEV_CLK(pclk
, ssc0
, pba
, 7);
1612 static struct resource ssc1_resource
[] = {
1617 DEV_CLK(pclk
, ssc1
, pba
, 8);
1619 static struct resource ssc2_resource
[] = {
1624 DEV_CLK(pclk
, ssc2
, pba
, 9);
1626 struct platform_device
*__init
1627 at32_add_device_ssc(unsigned int id
, unsigned int flags
)
1629 struct platform_device
*pdev
;
1634 pdev
= &ssc0_device
;
1635 if (flags
& ATMEL_SSC_RF
)
1636 pin_mask
|= (1 << 21); /* RF */
1637 if (flags
& ATMEL_SSC_RK
)
1638 pin_mask
|= (1 << 22); /* RK */
1639 if (flags
& ATMEL_SSC_TK
)
1640 pin_mask
|= (1 << 23); /* TK */
1641 if (flags
& ATMEL_SSC_TF
)
1642 pin_mask
|= (1 << 24); /* TF */
1643 if (flags
& ATMEL_SSC_TD
)
1644 pin_mask
|= (1 << 25); /* TD */
1645 if (flags
& ATMEL_SSC_RD
)
1646 pin_mask
|= (1 << 26); /* RD */
1649 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1653 pdev
= &ssc1_device
;
1654 if (flags
& ATMEL_SSC_RF
)
1655 pin_mask
|= (1 << 0); /* RF */
1656 if (flags
& ATMEL_SSC_RK
)
1657 pin_mask
|= (1 << 1); /* RK */
1658 if (flags
& ATMEL_SSC_TK
)
1659 pin_mask
|= (1 << 2); /* TK */
1660 if (flags
& ATMEL_SSC_TF
)
1661 pin_mask
|= (1 << 3); /* TF */
1662 if (flags
& ATMEL_SSC_TD
)
1663 pin_mask
|= (1 << 4); /* TD */
1664 if (flags
& ATMEL_SSC_RD
)
1665 pin_mask
|= (1 << 5); /* RD */
1668 select_peripheral(PIOA
, pin_mask
, PERIPH_B
, 0);
1672 pdev
= &ssc2_device
;
1673 if (flags
& ATMEL_SSC_TD
)
1674 pin_mask
|= (1 << 13); /* TD */
1675 if (flags
& ATMEL_SSC_RD
)
1676 pin_mask
|= (1 << 14); /* RD */
1677 if (flags
& ATMEL_SSC_TK
)
1678 pin_mask
|= (1 << 15); /* TK */
1679 if (flags
& ATMEL_SSC_TF
)
1680 pin_mask
|= (1 << 16); /* TF */
1681 if (flags
& ATMEL_SSC_RF
)
1682 pin_mask
|= (1 << 17); /* RF */
1683 if (flags
& ATMEL_SSC_RK
)
1684 pin_mask
|= (1 << 18); /* RK */
1687 select_peripheral(PIOB
, pin_mask
, PERIPH_A
, 0);
1694 platform_device_register(pdev
);
1698 /* --------------------------------------------------------------------
1699 * USB Device Controller
1700 * -------------------------------------------------------------------- */
1701 static struct resource usba0_resource
[] __initdata
= {
1703 .start
= 0xff300000,
1705 .flags
= IORESOURCE_MEM
,
1707 .start
= 0xfff03000,
1709 .flags
= IORESOURCE_MEM
,
1713 static struct clk usba0_pclk
= {
1716 .mode
= pbb_clk_mode
,
1717 .get_rate
= pbb_clk_get_rate
,
1720 static struct clk usba0_hclk
= {
1723 .mode
= hsb_clk_mode
,
1724 .get_rate
= hsb_clk_get_rate
,
1728 #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
1732 .fifo_size = maxpkt, \
1733 .nr_banks = maxbk, \
1738 static struct usba_ep_data at32_usba_ep
[] __initdata
= {
1739 EP("ep0", 0, 64, 1, 0, 0),
1740 EP("ep1", 1, 512, 2, 1, 1),
1741 EP("ep2", 2, 512, 2, 1, 1),
1742 EP("ep3-int", 3, 64, 3, 1, 0),
1743 EP("ep4-int", 4, 64, 3, 1, 0),
1744 EP("ep5", 5, 1024, 3, 1, 1),
1745 EP("ep6", 6, 1024, 3, 1, 1),
1750 struct platform_device
*__init
1751 at32_add_device_usba(unsigned int id
, struct usba_platform_data
*data
)
1754 * pdata doesn't have room for any endpoints, so we need to
1755 * append room for the ones we need right after it.
1758 struct usba_platform_data pdata
;
1759 struct usba_ep_data ep
[7];
1761 struct platform_device
*pdev
;
1766 pdev
= platform_device_alloc("atmel_usba_udc", 0);
1770 if (platform_device_add_resources(pdev
, usba0_resource
,
1771 ARRAY_SIZE(usba0_resource
)))
1775 usba_data
.pdata
.vbus_pin
= data
->vbus_pin
;
1776 usba_data
.pdata
.vbus_pin_inverted
= data
->vbus_pin_inverted
;
1778 usba_data
.pdata
.vbus_pin
= -EINVAL
;
1779 usba_data
.pdata
.vbus_pin_inverted
= -EINVAL
;
1782 data
= &usba_data
.pdata
;
1783 data
->num_ep
= ARRAY_SIZE(at32_usba_ep
);
1784 memcpy(data
->ep
, at32_usba_ep
, sizeof(at32_usba_ep
));
1786 if (platform_device_add_data(pdev
, data
, sizeof(usba_data
)))
1789 if (gpio_is_valid(data
->vbus_pin
))
1790 at32_select_gpio(data
->vbus_pin
, 0);
1792 usba0_pclk
.dev
= &pdev
->dev
;
1793 usba0_hclk
.dev
= &pdev
->dev
;
1795 platform_device_add(pdev
);
1800 platform_device_put(pdev
);
1804 /* --------------------------------------------------------------------
1805 * IDE / CompactFlash
1806 * -------------------------------------------------------------------- */
1807 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
1808 static struct resource at32_smc_cs4_resource
[] __initdata
= {
1810 .start
= 0x04000000,
1812 .flags
= IORESOURCE_MEM
,
1814 IRQ(~0UL), /* Magic IRQ will be overridden */
1816 static struct resource at32_smc_cs5_resource
[] __initdata
= {
1818 .start
= 0x20000000,
1820 .flags
= IORESOURCE_MEM
,
1822 IRQ(~0UL), /* Magic IRQ will be overridden */
1825 static int __init
at32_init_ide_or_cf(struct platform_device
*pdev
,
1826 unsigned int cs
, unsigned int extint
)
1828 static unsigned int extint_pin_map
[4] __initdata
= {
1834 static bool common_pins_initialized __initdata
= false;
1835 unsigned int extint_pin
;
1839 if (extint
>= ARRAY_SIZE(extint_pin_map
))
1841 extint_pin
= extint_pin_map
[extint
];
1845 ret
= platform_device_add_resources(pdev
,
1846 at32_smc_cs4_resource
,
1847 ARRAY_SIZE(at32_smc_cs4_resource
));
1852 select_peripheral(PIOE
, (1 << 21), PERIPH_A
, 0);
1853 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, HMATRIX_EBI_CF0_ENABLE
);
1856 ret
= platform_device_add_resources(pdev
,
1857 at32_smc_cs5_resource
,
1858 ARRAY_SIZE(at32_smc_cs5_resource
));
1863 select_peripheral(PIOE
, (1 << 22), PERIPH_A
, 0);
1864 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, HMATRIX_EBI_CF1_ENABLE
);
1870 if (!common_pins_initialized
) {
1871 pin_mask
= (1 << 19); /* CFCE1 -> CS0_N */
1872 pin_mask
|= (1 << 20); /* CFCE2 -> CS1_N */
1873 pin_mask
|= (1 << 23); /* CFRNW -> DIR */
1874 pin_mask
|= (1 << 24); /* NWAIT <- IORDY */
1876 select_peripheral(PIOE
, pin_mask
, PERIPH_A
, 0);
1878 common_pins_initialized
= true;
1881 select_peripheral(PIOB
, extint_pin
, PERIPH_A
, AT32_GPIOF_DEGLITCH
);
1883 pdev
->resource
[1].start
= EIM_IRQ_BASE
+ extint
;
1884 pdev
->resource
[1].end
= pdev
->resource
[1].start
;
1889 struct platform_device
*__init
1890 at32_add_device_ide(unsigned int id
, unsigned int extint
,
1891 struct ide_platform_data
*data
)
1893 struct platform_device
*pdev
;
1895 pdev
= platform_device_alloc("at32_ide", id
);
1899 if (platform_device_add_data(pdev
, data
,
1900 sizeof(struct ide_platform_data
)))
1903 if (at32_init_ide_or_cf(pdev
, data
->cs
, extint
))
1906 platform_device_add(pdev
);
1910 platform_device_put(pdev
);
1914 struct platform_device
*__init
1915 at32_add_device_cf(unsigned int id
, unsigned int extint
,
1916 struct cf_platform_data
*data
)
1918 struct platform_device
*pdev
;
1920 pdev
= platform_device_alloc("at32_cf", id
);
1924 if (platform_device_add_data(pdev
, data
,
1925 sizeof(struct cf_platform_data
)))
1928 if (at32_init_ide_or_cf(pdev
, data
->cs
, extint
))
1931 if (gpio_is_valid(data
->detect_pin
))
1932 at32_select_gpio(data
->detect_pin
, AT32_GPIOF_DEGLITCH
);
1933 if (gpio_is_valid(data
->reset_pin
))
1934 at32_select_gpio(data
->reset_pin
, 0);
1935 if (gpio_is_valid(data
->vcc_pin
))
1936 at32_select_gpio(data
->vcc_pin
, 0);
1937 /* READY is used as extint, so we can't select it as gpio */
1939 platform_device_add(pdev
);
1943 platform_device_put(pdev
);
1948 /* --------------------------------------------------------------------
1949 * NAND Flash / SmartMedia
1950 * -------------------------------------------------------------------- */
1951 static struct resource smc_cs3_resource
[] __initdata
= {
1953 .start
= 0x0c000000,
1955 .flags
= IORESOURCE_MEM
,
1957 .start
= 0xfff03c00,
1959 .flags
= IORESOURCE_MEM
,
1963 struct platform_device
*__init
1964 at32_add_device_nand(unsigned int id
, struct atmel_nand_data
*data
)
1966 struct platform_device
*pdev
;
1968 if (id
!= 0 || !data
)
1971 pdev
= platform_device_alloc("atmel_nand", id
);
1975 if (platform_device_add_resources(pdev
, smc_cs3_resource
,
1976 ARRAY_SIZE(smc_cs3_resource
)))
1979 if (platform_device_add_data(pdev
, data
,
1980 sizeof(struct atmel_nand_data
)))
1983 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, HMATRIX_EBI_NAND_ENABLE
);
1984 if (data
->enable_pin
)
1985 at32_select_gpio(data
->enable_pin
,
1986 AT32_GPIOF_OUTPUT
| AT32_GPIOF_HIGH
);
1988 at32_select_gpio(data
->rdy_pin
, 0);
1990 at32_select_gpio(data
->det_pin
, 0);
1992 platform_device_add(pdev
);
1996 platform_device_put(pdev
);
2000 /* --------------------------------------------------------------------
2002 * -------------------------------------------------------------------- */
2003 static struct resource atmel_ac97c0_resource
[] __initdata
= {
2007 static struct clk atmel_ac97c0_pclk
= {
2010 .mode
= pbb_clk_mode
,
2011 .get_rate
= pbb_clk_get_rate
,
2015 struct platform_device
*__init
2016 at32_add_device_ac97c(unsigned int id
, struct ac97c_platform_data
*data
,
2019 struct platform_device
*pdev
;
2020 struct dw_dma_slave
*rx_dws
;
2021 struct dw_dma_slave
*tx_dws
;
2022 struct ac97c_platform_data _data
;
2028 pdev
= platform_device_alloc("atmel_ac97c", id
);
2032 if (platform_device_add_resources(pdev
, atmel_ac97c0_resource
,
2033 ARRAY_SIZE(atmel_ac97c0_resource
)))
2034 goto out_free_resources
;
2038 memset(data
, 0, sizeof(struct ac97c_platform_data
));
2039 data
->reset_pin
= -ENODEV
;
2042 rx_dws
= &data
->rx_dws
;
2043 tx_dws
= &data
->tx_dws
;
2045 /* Check if DMA slave interface for capture should be configured. */
2046 if (flags
& AC97C_CAPTURE
) {
2047 rx_dws
->dma_dev
= &dw_dmac0_device
.dev
;
2048 rx_dws
->reg_width
= DW_DMA_SLAVE_WIDTH_16BIT
;
2049 rx_dws
->cfg_hi
= DWC_CFGH_SRC_PER(3);
2050 rx_dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
);
2053 /* Check if DMA slave interface for playback should be configured. */
2054 if (flags
& AC97C_PLAYBACK
) {
2055 tx_dws
->dma_dev
= &dw_dmac0_device
.dev
;
2056 tx_dws
->reg_width
= DW_DMA_SLAVE_WIDTH_16BIT
;
2057 tx_dws
->cfg_hi
= DWC_CFGH_DST_PER(4);
2058 tx_dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
);
2061 if (platform_device_add_data(pdev
, data
,
2062 sizeof(struct ac97c_platform_data
)))
2063 goto out_free_resources
;
2065 /* SDO | SYNC | SCLK | SDI */
2066 pin_mask
= (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
2068 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, 0);
2070 if (gpio_is_valid(data
->reset_pin
))
2071 at32_select_gpio(data
->reset_pin
, AT32_GPIOF_OUTPUT
2074 atmel_ac97c0_pclk
.dev
= &pdev
->dev
;
2076 platform_device_add(pdev
);
2080 platform_device_put(pdev
);
2084 /* --------------------------------------------------------------------
2086 * -------------------------------------------------------------------- */
2087 static struct resource abdac0_resource
[] __initdata
= {
2091 static struct clk abdac0_pclk
= {
2094 .mode
= pbb_clk_mode
,
2095 .get_rate
= pbb_clk_get_rate
,
2098 static struct clk abdac0_sample_clk
= {
2099 .name
= "sample_clk",
2100 .mode
= genclk_mode
,
2101 .get_rate
= genclk_get_rate
,
2102 .set_rate
= genclk_set_rate
,
2103 .set_parent
= genclk_set_parent
,
2107 struct platform_device
*__init
2108 at32_add_device_abdac(unsigned int id
, struct atmel_abdac_pdata
*data
)
2110 struct platform_device
*pdev
;
2111 struct dw_dma_slave
*dws
;
2114 if (id
!= 0 || !data
)
2117 pdev
= platform_device_alloc("atmel_abdac", id
);
2121 if (platform_device_add_resources(pdev
, abdac0_resource
,
2122 ARRAY_SIZE(abdac0_resource
)))
2123 goto out_free_resources
;
2127 dws
->dma_dev
= &dw_dmac0_device
.dev
;
2128 dws
->reg_width
= DW_DMA_SLAVE_WIDTH_32BIT
;
2129 dws
->cfg_hi
= DWC_CFGH_DST_PER(2);
2130 dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
);
2132 if (platform_device_add_data(pdev
, data
,
2133 sizeof(struct atmel_abdac_pdata
)))
2134 goto out_free_resources
;
2136 pin_mask
= (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
2137 pin_mask
|= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
2139 select_peripheral(PIOB
, pin_mask
, PERIPH_A
, 0);
2141 abdac0_pclk
.dev
= &pdev
->dev
;
2142 abdac0_sample_clk
.dev
= &pdev
->dev
;
2144 platform_device_add(pdev
);
2148 platform_device_put(pdev
);
2152 /* --------------------------------------------------------------------
2154 * -------------------------------------------------------------------- */
2155 static struct clk gclk0
= {
2157 .mode
= genclk_mode
,
2158 .get_rate
= genclk_get_rate
,
2159 .set_rate
= genclk_set_rate
,
2160 .set_parent
= genclk_set_parent
,
2163 static struct clk gclk1
= {
2165 .mode
= genclk_mode
,
2166 .get_rate
= genclk_get_rate
,
2167 .set_rate
= genclk_set_rate
,
2168 .set_parent
= genclk_set_parent
,
2171 static struct clk gclk2
= {
2173 .mode
= genclk_mode
,
2174 .get_rate
= genclk_get_rate
,
2175 .set_rate
= genclk_set_rate
,
2176 .set_parent
= genclk_set_parent
,
2179 static struct clk gclk3
= {
2181 .mode
= genclk_mode
,
2182 .get_rate
= genclk_get_rate
,
2183 .set_rate
= genclk_set_rate
,
2184 .set_parent
= genclk_set_parent
,
2187 static struct clk gclk4
= {
2189 .mode
= genclk_mode
,
2190 .get_rate
= genclk_get_rate
,
2191 .set_rate
= genclk_set_rate
,
2192 .set_parent
= genclk_set_parent
,
2196 static __initdata
struct clk
*init_clocks
[] = {
2227 &atmel_usart0_usart
,
2228 &atmel_usart1_usart
,
2229 &atmel_usart2_usart
,
2230 &atmel_usart3_usart
,
2232 #if defined(CONFIG_CPU_AT32AP7000)
2238 &atmel_spi0_spi_clk
,
2239 &atmel_spi1_spi_clk
,
2242 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
2244 &atmel_lcdfb0_pixclk
,
2261 void __init
setup_platform(void)
2263 u32 cpu_mask
= 0, hsb_mask
= 0, pba_mask
= 0, pbb_mask
= 0;
2266 if (pm_readl(MCCTRL
) & PM_BIT(PLLSEL
)) {
2268 cpu_clk
.parent
= &pll0
;
2271 cpu_clk
.parent
= &osc0
;
2274 if (pm_readl(PLL0
) & PM_BIT(PLLOSC
))
2275 pll0
.parent
= &osc1
;
2276 if (pm_readl(PLL1
) & PM_BIT(PLLOSC
))
2277 pll1
.parent
= &osc1
;
2279 genclk_init_parent(&gclk0
);
2280 genclk_init_parent(&gclk1
);
2281 genclk_init_parent(&gclk2
);
2282 genclk_init_parent(&gclk3
);
2283 genclk_init_parent(&gclk4
);
2284 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
2285 genclk_init_parent(&atmel_lcdfb0_pixclk
);
2287 genclk_init_parent(&abdac0_sample_clk
);
2290 * Build initial dynamic clock list by registering all clocks
2292 * At the same time, turn on all clocks that have at least one
2293 * user already, and turn off everything else. We only do this
2294 * for module clocks, and even though it isn't particularly
2295 * pretty to check the address of the mode function, it should
2298 for (i
= 0; i
< ARRAY_SIZE(init_clocks
); i
++) {
2299 struct clk
*clk
= init_clocks
[i
];
2301 /* first, register clock */
2302 at32_clk_register(clk
);
2304 if (clk
->users
== 0)
2307 if (clk
->mode
== &cpu_clk_mode
)
2308 cpu_mask
|= 1 << clk
->index
;
2309 else if (clk
->mode
== &hsb_clk_mode
)
2310 hsb_mask
|= 1 << clk
->index
;
2311 else if (clk
->mode
== &pba_clk_mode
)
2312 pba_mask
|= 1 << clk
->index
;
2313 else if (clk
->mode
== &pbb_clk_mode
)
2314 pbb_mask
|= 1 << clk
->index
;
2317 pm_writel(CPU_MASK
, cpu_mask
);
2318 pm_writel(HSB_MASK
, hsb_mask
);
2319 pm_writel(PBA_MASK
, pba_mask
);
2320 pm_writel(PBB_MASK
, pbb_mask
);
2322 /* Initialize the port muxes */
2323 at32_init_pio(&pio0_device
);
2324 at32_init_pio(&pio1_device
);
2325 at32_init_pio(&pio2_device
);
2326 at32_init_pio(&pio3_device
);
2327 at32_init_pio(&pio4_device
);
2330 struct gen_pool
*sram_pool
;
2332 static int __init
sram_init(void)
2334 struct gen_pool
*pool
;
2336 /* 1KiB granularity */
2337 pool
= gen_pool_create(10, -1);
2341 if (gen_pool_add(pool
, 0x24000000, 0x8000, -1))
2348 gen_pool_destroy(pool
);
2350 pr_err("Failed to create SRAM pool\n");
2353 core_initcall(sram_init
);