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/gpio.h>
16 #include <linux/spi/spi.h>
17 #include <linux/usb/atmel_usba_udc.h>
18 #include <linux/atmel-mci.h>
23 #include <mach/at32ap700x.h>
24 #include <mach/board.h>
25 #include <mach/hmatrix.h>
26 #include <mach/portmux.h>
27 #include <mach/sram.h>
29 #include <sound/atmel-abdac.h>
30 #include <sound/atmel-ac97c.h>
32 #include <video/atmel_lcdc.h>
42 .end = base + 0x3ff, \
43 .flags = IORESOURCE_MEM, \
49 .flags = IORESOURCE_IRQ, \
51 #define NAMED_IRQ(num, _name) \
56 .flags = IORESOURCE_IRQ, \
59 /* REVISIT these assume *every* device supports DMA, but several
60 * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
62 #define DEFINE_DEV(_name, _id) \
63 static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
64 static struct platform_device _name##_id##_device = { \
68 .dma_mask = &_name##_id##_dma_mask, \
69 .coherent_dma_mask = DMA_BIT_MASK(32), \
71 .resource = _name##_id##_resource, \
72 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
74 #define DEFINE_DEV_DATA(_name, _id) \
75 static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
76 static struct platform_device _name##_id##_device = { \
80 .dma_mask = &_name##_id##_dma_mask, \
81 .platform_data = &_name##_id##_data, \
82 .coherent_dma_mask = DMA_BIT_MASK(32), \
84 .resource = _name##_id##_resource, \
85 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
88 #define select_peripheral(port, pin_mask, periph, flags) \
89 at32_select_periph(GPIO_##port##_BASE, pin_mask, \
92 #define DEV_CLK(_name, devname, bus, _index) \
93 static struct clk devname##_##_name = { \
95 .dev = &devname##_device.dev, \
96 .parent = &bus##_clk, \
97 .mode = bus##_clk_mode, \
98 .get_rate = bus##_clk_get_rate, \
102 static DEFINE_SPINLOCK(pm_lock
);
104 static struct clk osc0
;
105 static struct clk osc1
;
107 static unsigned long osc_get_rate(struct clk
*clk
)
109 return at32_board_osc_rates
[clk
->index
];
112 static unsigned long pll_get_rate(struct clk
*clk
, unsigned long control
)
114 unsigned long div
, mul
, rate
;
116 div
= PM_BFEXT(PLLDIV
, control
) + 1;
117 mul
= PM_BFEXT(PLLMUL
, control
) + 1;
119 rate
= clk
->parent
->get_rate(clk
->parent
);
120 rate
= (rate
+ div
/ 2) / div
;
126 static long pll_set_rate(struct clk
*clk
, unsigned long rate
,
130 unsigned long mul_best_fit
= 0;
132 unsigned long div_min
;
133 unsigned long div_max
;
134 unsigned long div_best_fit
= 0;
136 unsigned long pll_in
;
137 unsigned long actual
= 0;
138 unsigned long rate_error
;
139 unsigned long rate_error_prev
= ~0UL;
142 /* Rate must be between 80 MHz and 200 Mhz. */
143 if (rate
< 80000000UL || rate
> 200000000UL)
146 ctrl
= PM_BF(PLLOPT
, 4);
147 base
= clk
->parent
->get_rate(clk
->parent
);
149 /* PLL input frequency must be between 6 MHz and 32 MHz. */
150 div_min
= DIV_ROUND_UP(base
, 32000000UL);
151 div_max
= base
/ 6000000UL;
153 if (div_max
< div_min
)
156 for (div
= div_min
; div
<= div_max
; div
++) {
157 pll_in
= (base
+ div
/ 2) / div
;
158 mul
= (rate
+ pll_in
/ 2) / pll_in
;
163 actual
= pll_in
* mul
;
164 rate_error
= abs(actual
- rate
);
166 if (rate_error
< rate_error_prev
) {
169 rate_error_prev
= rate_error
;
176 if (div_best_fit
== 0)
179 ctrl
|= PM_BF(PLLMUL
, mul_best_fit
- 1);
180 ctrl
|= PM_BF(PLLDIV
, div_best_fit
- 1);
181 ctrl
|= PM_BF(PLLCOUNT
, 16);
183 if (clk
->parent
== &osc1
)
184 ctrl
|= PM_BIT(PLLOSC
);
191 static unsigned long pll0_get_rate(struct clk
*clk
)
195 control
= pm_readl(PLL0
);
197 return pll_get_rate(clk
, control
);
200 static void pll1_mode(struct clk
*clk
, int enabled
)
202 unsigned long timeout
;
206 ctrl
= pm_readl(PLL1
);
209 if (!PM_BFEXT(PLLMUL
, ctrl
) && !PM_BFEXT(PLLDIV
, ctrl
)) {
210 pr_debug("clk %s: failed to enable, rate not set\n",
215 ctrl
|= PM_BIT(PLLEN
);
216 pm_writel(PLL1
, ctrl
);
218 /* Wait for PLL lock. */
219 for (timeout
= 10000; timeout
; timeout
--) {
220 status
= pm_readl(ISR
);
221 if (status
& PM_BIT(LOCK1
))
226 if (!(status
& PM_BIT(LOCK1
)))
227 printk(KERN_ERR
"clk %s: timeout waiting for lock\n",
230 ctrl
&= ~PM_BIT(PLLEN
);
231 pm_writel(PLL1
, ctrl
);
235 static unsigned long pll1_get_rate(struct clk
*clk
)
239 control
= pm_readl(PLL1
);
241 return pll_get_rate(clk
, control
);
244 static long pll1_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
247 unsigned long actual_rate
;
249 actual_rate
= pll_set_rate(clk
, rate
, &ctrl
);
252 if (actual_rate
!= rate
)
256 pr_debug(KERN_INFO
"clk %s: new rate %lu (actual rate %lu)\n",
257 clk
->name
, rate
, actual_rate
);
258 pm_writel(PLL1
, ctrl
);
264 static int pll1_set_parent(struct clk
*clk
, struct clk
*parent
)
271 ctrl
= pm_readl(PLL1
);
272 WARN_ON(ctrl
& PM_BIT(PLLEN
));
275 ctrl
&= ~PM_BIT(PLLOSC
);
276 else if (parent
== &osc1
)
277 ctrl
|= PM_BIT(PLLOSC
);
281 pm_writel(PLL1
, ctrl
);
282 clk
->parent
= parent
;
288 * The AT32AP7000 has five primary clock sources: One 32kHz
289 * oscillator, two crystal oscillators and two PLLs.
291 static struct clk osc32k
= {
293 .get_rate
= osc_get_rate
,
297 static struct clk osc0
= {
299 .get_rate
= osc_get_rate
,
303 static struct clk osc1
= {
305 .get_rate
= osc_get_rate
,
308 static struct clk pll0
= {
310 .get_rate
= pll0_get_rate
,
313 static struct clk pll1
= {
316 .get_rate
= pll1_get_rate
,
317 .set_rate
= pll1_set_rate
,
318 .set_parent
= pll1_set_parent
,
323 * The main clock can be either osc0 or pll0. The boot loader may
324 * have chosen one for us, so we don't really know which one until we
325 * have a look at the SM.
327 static struct clk
*main_clock
;
330 * Synchronous clocks are generated from the main clock. The clocks
331 * must satisfy the constraint
332 * fCPU >= fHSB >= fPB
333 * i.e. each clock must not be faster than its parent.
335 static unsigned long bus_clk_get_rate(struct clk
*clk
, unsigned int shift
)
337 return main_clock
->get_rate(main_clock
) >> shift
;
340 static void cpu_clk_mode(struct clk
*clk
, int enabled
)
345 spin_lock_irqsave(&pm_lock
, flags
);
346 mask
= pm_readl(CPU_MASK
);
348 mask
|= 1 << clk
->index
;
350 mask
&= ~(1 << clk
->index
);
351 pm_writel(CPU_MASK
, mask
);
352 spin_unlock_irqrestore(&pm_lock
, flags
);
355 static unsigned long cpu_clk_get_rate(struct clk
*clk
)
357 unsigned long cksel
, shift
= 0;
359 cksel
= pm_readl(CKSEL
);
360 if (cksel
& PM_BIT(CPUDIV
))
361 shift
= PM_BFEXT(CPUSEL
, cksel
) + 1;
363 return bus_clk_get_rate(clk
, shift
);
366 static long cpu_clk_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
369 unsigned long parent_rate
, child_div
, actual_rate
, div
;
371 parent_rate
= clk
->parent
->get_rate(clk
->parent
);
372 control
= pm_readl(CKSEL
);
374 if (control
& PM_BIT(HSBDIV
))
375 child_div
= 1 << (PM_BFEXT(HSBSEL
, control
) + 1);
379 if (rate
> 3 * (parent_rate
/ 4) || child_div
== 1) {
380 actual_rate
= parent_rate
;
381 control
&= ~PM_BIT(CPUDIV
);
384 div
= (parent_rate
+ rate
/ 2) / rate
;
387 cpusel
= (div
> 1) ? (fls(div
) - 2) : 0;
388 control
= PM_BIT(CPUDIV
) | PM_BFINS(CPUSEL
, cpusel
, control
);
389 actual_rate
= parent_rate
/ (1 << (cpusel
+ 1));
392 pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
393 clk
->name
, rate
, actual_rate
);
396 pm_writel(CKSEL
, control
);
401 static void hsb_clk_mode(struct clk
*clk
, int enabled
)
406 spin_lock_irqsave(&pm_lock
, flags
);
407 mask
= pm_readl(HSB_MASK
);
409 mask
|= 1 << clk
->index
;
411 mask
&= ~(1 << clk
->index
);
412 pm_writel(HSB_MASK
, mask
);
413 spin_unlock_irqrestore(&pm_lock
, flags
);
416 static unsigned long hsb_clk_get_rate(struct clk
*clk
)
418 unsigned long cksel
, shift
= 0;
420 cksel
= pm_readl(CKSEL
);
421 if (cksel
& PM_BIT(HSBDIV
))
422 shift
= PM_BFEXT(HSBSEL
, cksel
) + 1;
424 return bus_clk_get_rate(clk
, shift
);
427 void pba_clk_mode(struct clk
*clk
, int enabled
)
432 spin_lock_irqsave(&pm_lock
, flags
);
433 mask
= pm_readl(PBA_MASK
);
435 mask
|= 1 << clk
->index
;
437 mask
&= ~(1 << clk
->index
);
438 pm_writel(PBA_MASK
, mask
);
439 spin_unlock_irqrestore(&pm_lock
, flags
);
442 unsigned long pba_clk_get_rate(struct clk
*clk
)
444 unsigned long cksel
, shift
= 0;
446 cksel
= pm_readl(CKSEL
);
447 if (cksel
& PM_BIT(PBADIV
))
448 shift
= PM_BFEXT(PBASEL
, cksel
) + 1;
450 return bus_clk_get_rate(clk
, shift
);
453 static void pbb_clk_mode(struct clk
*clk
, int enabled
)
458 spin_lock_irqsave(&pm_lock
, flags
);
459 mask
= pm_readl(PBB_MASK
);
461 mask
|= 1 << clk
->index
;
463 mask
&= ~(1 << clk
->index
);
464 pm_writel(PBB_MASK
, mask
);
465 spin_unlock_irqrestore(&pm_lock
, flags
);
468 static unsigned long pbb_clk_get_rate(struct clk
*clk
)
470 unsigned long cksel
, shift
= 0;
472 cksel
= pm_readl(CKSEL
);
473 if (cksel
& PM_BIT(PBBDIV
))
474 shift
= PM_BFEXT(PBBSEL
, cksel
) + 1;
476 return bus_clk_get_rate(clk
, shift
);
479 static struct clk cpu_clk
= {
481 .get_rate
= cpu_clk_get_rate
,
482 .set_rate
= cpu_clk_set_rate
,
485 static struct clk hsb_clk
= {
488 .get_rate
= hsb_clk_get_rate
,
490 static struct clk pba_clk
= {
493 .mode
= hsb_clk_mode
,
494 .get_rate
= pba_clk_get_rate
,
497 static struct clk pbb_clk
= {
500 .mode
= hsb_clk_mode
,
501 .get_rate
= pbb_clk_get_rate
,
506 /* --------------------------------------------------------------------
507 * Generic Clock operations
508 * -------------------------------------------------------------------- */
510 static void genclk_mode(struct clk
*clk
, int enabled
)
514 control
= pm_readl(GCCTRL(clk
->index
));
516 control
|= PM_BIT(CEN
);
518 control
&= ~PM_BIT(CEN
);
519 pm_writel(GCCTRL(clk
->index
), control
);
522 static unsigned long genclk_get_rate(struct clk
*clk
)
525 unsigned long div
= 1;
527 control
= pm_readl(GCCTRL(clk
->index
));
528 if (control
& PM_BIT(DIVEN
))
529 div
= 2 * (PM_BFEXT(DIV
, control
) + 1);
531 return clk
->parent
->get_rate(clk
->parent
) / div
;
534 static long genclk_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
537 unsigned long parent_rate
, actual_rate
, div
;
539 parent_rate
= clk
->parent
->get_rate(clk
->parent
);
540 control
= pm_readl(GCCTRL(clk
->index
));
542 if (rate
> 3 * parent_rate
/ 4) {
543 actual_rate
= parent_rate
;
544 control
&= ~PM_BIT(DIVEN
);
546 div
= (parent_rate
+ rate
) / (2 * rate
) - 1;
547 control
= PM_BFINS(DIV
, div
, control
) | PM_BIT(DIVEN
);
548 actual_rate
= parent_rate
/ (2 * (div
+ 1));
551 dev_dbg(clk
->dev
, "clk %s: new rate %lu (actual rate %lu)\n",
552 clk
->name
, rate
, actual_rate
);
555 pm_writel(GCCTRL(clk
->index
), control
);
560 int genclk_set_parent(struct clk
*clk
, struct clk
*parent
)
564 dev_dbg(clk
->dev
, "clk %s: new parent %s (was %s)\n",
565 clk
->name
, parent
->name
, clk
->parent
->name
);
567 control
= pm_readl(GCCTRL(clk
->index
));
569 if (parent
== &osc1
|| parent
== &pll1
)
570 control
|= PM_BIT(OSCSEL
);
571 else if (parent
== &osc0
|| parent
== &pll0
)
572 control
&= ~PM_BIT(OSCSEL
);
576 if (parent
== &pll0
|| parent
== &pll1
)
577 control
|= PM_BIT(PLLSEL
);
579 control
&= ~PM_BIT(PLLSEL
);
581 pm_writel(GCCTRL(clk
->index
), control
);
582 clk
->parent
= parent
;
587 static void __init
genclk_init_parent(struct clk
*clk
)
592 BUG_ON(clk
->index
> 7);
594 control
= pm_readl(GCCTRL(clk
->index
));
595 if (control
& PM_BIT(OSCSEL
))
596 parent
= (control
& PM_BIT(PLLSEL
)) ? &pll1
: &osc1
;
598 parent
= (control
& PM_BIT(PLLSEL
)) ? &pll0
: &osc0
;
600 clk
->parent
= parent
;
603 static struct dw_dma_platform_data dw_dmac0_data
= {
607 static struct resource dw_dmac0_resource
[] = {
611 DEFINE_DEV_DATA(dw_dmac
, 0);
612 DEV_CLK(hclk
, dw_dmac0
, hsb
, 10);
614 /* --------------------------------------------------------------------
616 * -------------------------------------------------------------------- */
617 static struct resource at32_pm0_resource
[] = {
621 .flags
= IORESOURCE_MEM
,
626 static struct resource at32ap700x_rtc0_resource
[] = {
630 .flags
= IORESOURCE_MEM
,
635 static struct resource at32_wdt0_resource
[] = {
639 .flags
= IORESOURCE_MEM
,
643 static struct resource at32_eic0_resource
[] = {
647 .flags
= IORESOURCE_MEM
,
652 DEFINE_DEV(at32_pm
, 0);
653 DEFINE_DEV(at32ap700x_rtc
, 0);
654 DEFINE_DEV(at32_wdt
, 0);
655 DEFINE_DEV(at32_eic
, 0);
658 * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
661 static struct clk at32_pm_pclk
= {
663 .dev
= &at32_pm0_device
.dev
,
665 .mode
= pbb_clk_mode
,
666 .get_rate
= pbb_clk_get_rate
,
671 static struct resource intc0_resource
[] = {
674 struct platform_device at32_intc0_device
= {
677 .resource
= intc0_resource
,
678 .num_resources
= ARRAY_SIZE(intc0_resource
),
680 DEV_CLK(pclk
, at32_intc0
, pbb
, 1);
682 static struct clk ebi_clk
= {
685 .mode
= hsb_clk_mode
,
686 .get_rate
= hsb_clk_get_rate
,
689 static struct clk hramc_clk
= {
692 .mode
= hsb_clk_mode
,
693 .get_rate
= hsb_clk_get_rate
,
697 static struct clk sdramc_clk
= {
698 .name
= "sdramc_clk",
700 .mode
= pbb_clk_mode
,
701 .get_rate
= pbb_clk_get_rate
,
706 static struct resource smc0_resource
[] = {
710 DEV_CLK(pclk
, smc0
, pbb
, 13);
711 DEV_CLK(mck
, smc0
, hsb
, 0);
713 static struct platform_device pdc_device
= {
717 DEV_CLK(hclk
, pdc
, hsb
, 4);
718 DEV_CLK(pclk
, pdc
, pba
, 16);
720 static struct clk pico_clk
= {
723 .mode
= cpu_clk_mode
,
724 .get_rate
= cpu_clk_get_rate
,
728 /* --------------------------------------------------------------------
730 * -------------------------------------------------------------------- */
732 struct clk at32_hmatrix_clk
= {
733 .name
= "hmatrix_clk",
735 .mode
= pbb_clk_mode
,
736 .get_rate
= pbb_clk_get_rate
,
742 * Set bits in the HMATRIX Special Function Register (SFR) used by the
743 * External Bus Interface (EBI). This can be used to enable special
744 * features like CompactFlash support, NAND Flash support, etc. on
745 * certain chipselects.
747 static inline void set_ebi_sfr_bits(u32 mask
)
749 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, mask
);
752 /* --------------------------------------------------------------------
754 * -------------------------------------------------------------------- */
756 static struct resource at32_tcb0_resource
[] = {
760 static struct platform_device at32_tcb0_device
= {
763 .resource
= at32_tcb0_resource
,
764 .num_resources
= ARRAY_SIZE(at32_tcb0_resource
),
766 DEV_CLK(t0_clk
, at32_tcb0
, pbb
, 3);
768 static struct resource at32_tcb1_resource
[] = {
772 static struct platform_device at32_tcb1_device
= {
775 .resource
= at32_tcb1_resource
,
776 .num_resources
= ARRAY_SIZE(at32_tcb1_resource
),
778 DEV_CLK(t0_clk
, at32_tcb1
, pbb
, 4);
780 /* --------------------------------------------------------------------
782 * -------------------------------------------------------------------- */
784 static struct resource pio0_resource
[] = {
789 DEV_CLK(mck
, pio0
, pba
, 10);
791 static struct resource pio1_resource
[] = {
796 DEV_CLK(mck
, pio1
, pba
, 11);
798 static struct resource pio2_resource
[] = {
803 DEV_CLK(mck
, pio2
, pba
, 12);
805 static struct resource pio3_resource
[] = {
810 DEV_CLK(mck
, pio3
, pba
, 13);
812 static struct resource pio4_resource
[] = {
817 DEV_CLK(mck
, pio4
, pba
, 14);
819 static int __init
system_device_init(void)
821 platform_device_register(&at32_pm0_device
);
822 platform_device_register(&at32_intc0_device
);
823 platform_device_register(&at32ap700x_rtc0_device
);
824 platform_device_register(&at32_wdt0_device
);
825 platform_device_register(&at32_eic0_device
);
826 platform_device_register(&smc0_device
);
827 platform_device_register(&pdc_device
);
828 platform_device_register(&dw_dmac0_device
);
830 platform_device_register(&at32_tcb0_device
);
831 platform_device_register(&at32_tcb1_device
);
833 platform_device_register(&pio0_device
);
834 platform_device_register(&pio1_device
);
835 platform_device_register(&pio2_device
);
836 platform_device_register(&pio3_device
);
837 platform_device_register(&pio4_device
);
841 core_initcall(system_device_init
);
843 /* --------------------------------------------------------------------
845 * -------------------------------------------------------------------- */
846 static struct resource atmel_psif0_resource
[] __initdata
= {
850 .flags
= IORESOURCE_MEM
,
854 static struct clk atmel_psif0_pclk
= {
857 .mode
= pba_clk_mode
,
858 .get_rate
= pba_clk_get_rate
,
862 static struct resource atmel_psif1_resource
[] __initdata
= {
866 .flags
= IORESOURCE_MEM
,
870 static struct clk atmel_psif1_pclk
= {
873 .mode
= pba_clk_mode
,
874 .get_rate
= pba_clk_get_rate
,
878 struct platform_device
*__init
at32_add_device_psif(unsigned int id
)
880 struct platform_device
*pdev
;
883 if (!(id
== 0 || id
== 1))
886 pdev
= platform_device_alloc("atmel_psif", id
);
892 pin_mask
= (1 << 8) | (1 << 9); /* CLOCK & DATA */
894 if (platform_device_add_resources(pdev
, atmel_psif0_resource
,
895 ARRAY_SIZE(atmel_psif0_resource
)))
896 goto err_add_resources
;
897 atmel_psif0_pclk
.dev
= &pdev
->dev
;
898 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
901 pin_mask
= (1 << 11) | (1 << 12); /* CLOCK & DATA */
903 if (platform_device_add_resources(pdev
, atmel_psif1_resource
,
904 ARRAY_SIZE(atmel_psif1_resource
)))
905 goto err_add_resources
;
906 atmel_psif1_pclk
.dev
= &pdev
->dev
;
907 select_peripheral(PIOB
, pin_mask
, PERIPH_A
, 0);
913 platform_device_add(pdev
);
917 platform_device_put(pdev
);
921 /* --------------------------------------------------------------------
923 * -------------------------------------------------------------------- */
925 static struct atmel_uart_data atmel_usart0_data
= {
929 static struct resource atmel_usart0_resource
[] = {
933 DEFINE_DEV_DATA(atmel_usart
, 0);
934 DEV_CLK(usart
, atmel_usart0
, pba
, 3);
936 static struct atmel_uart_data atmel_usart1_data
= {
940 static struct resource atmel_usart1_resource
[] = {
944 DEFINE_DEV_DATA(atmel_usart
, 1);
945 DEV_CLK(usart
, atmel_usart1
, pba
, 4);
947 static struct atmel_uart_data atmel_usart2_data
= {
951 static struct resource atmel_usart2_resource
[] = {
955 DEFINE_DEV_DATA(atmel_usart
, 2);
956 DEV_CLK(usart
, atmel_usart2
, pba
, 5);
958 static struct atmel_uart_data atmel_usart3_data
= {
962 static struct resource atmel_usart3_resource
[] = {
966 DEFINE_DEV_DATA(atmel_usart
, 3);
967 DEV_CLK(usart
, atmel_usart3
, pba
, 6);
969 static inline void configure_usart0_pins(int flags
)
971 u32 pin_mask
= (1 << 8) | (1 << 9); /* RXD & TXD */
972 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 6);
973 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 7);
974 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 10);
976 select_peripheral(PIOA
, pin_mask
, PERIPH_B
, AT32_GPIOF_PULLUP
);
979 static inline void configure_usart1_pins(int flags
)
981 u32 pin_mask
= (1 << 17) | (1 << 18); /* RXD & TXD */
982 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 19);
983 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 20);
984 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 16);
986 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, AT32_GPIOF_PULLUP
);
989 static inline void configure_usart2_pins(int flags
)
991 u32 pin_mask
= (1 << 26) | (1 << 27); /* RXD & TXD */
992 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 30);
993 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 29);
994 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 28);
996 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, AT32_GPIOF_PULLUP
);
999 static inline void configure_usart3_pins(int flags
)
1001 u32 pin_mask
= (1 << 18) | (1 << 17); /* RXD & TXD */
1002 if (flags
& ATMEL_USART_RTS
) pin_mask
|= (1 << 16);
1003 if (flags
& ATMEL_USART_CTS
) pin_mask
|= (1 << 15);
1004 if (flags
& ATMEL_USART_CLK
) pin_mask
|= (1 << 19);
1006 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, AT32_GPIOF_PULLUP
);
1009 static struct platform_device
*__initdata at32_usarts
[4];
1011 void __init
at32_map_usart(unsigned int hw_id
, unsigned int line
, int flags
)
1013 struct platform_device
*pdev
;
1017 pdev
= &atmel_usart0_device
;
1018 configure_usart0_pins(flags
);
1021 pdev
= &atmel_usart1_device
;
1022 configure_usart1_pins(flags
);
1025 pdev
= &atmel_usart2_device
;
1026 configure_usart2_pins(flags
);
1029 pdev
= &atmel_usart3_device
;
1030 configure_usart3_pins(flags
);
1036 if (PXSEG(pdev
->resource
[0].start
) == P4SEG
) {
1037 /* Addresses in the P4 segment are permanently mapped 1:1 */
1038 struct atmel_uart_data
*data
= pdev
->dev
.platform_data
;
1039 data
->regs
= (void __iomem
*)pdev
->resource
[0].start
;
1043 at32_usarts
[line
] = pdev
;
1046 struct platform_device
*__init
at32_add_device_usart(unsigned int id
)
1048 platform_device_register(at32_usarts
[id
]);
1049 return at32_usarts
[id
];
1052 struct platform_device
*atmel_default_console_device
;
1054 void __init
at32_setup_serial_console(unsigned int usart_id
)
1056 atmel_default_console_device
= at32_usarts
[usart_id
];
1059 /* --------------------------------------------------------------------
1061 * -------------------------------------------------------------------- */
1063 #ifdef CONFIG_CPU_AT32AP7000
1064 static struct eth_platform_data macb0_data
;
1065 static struct resource macb0_resource
[] = {
1069 DEFINE_DEV_DATA(macb
, 0);
1070 DEV_CLK(hclk
, macb0
, hsb
, 8);
1071 DEV_CLK(pclk
, macb0
, pbb
, 6);
1073 static struct eth_platform_data macb1_data
;
1074 static struct resource macb1_resource
[] = {
1078 DEFINE_DEV_DATA(macb
, 1);
1079 DEV_CLK(hclk
, macb1
, hsb
, 9);
1080 DEV_CLK(pclk
, macb1
, pbb
, 7);
1082 struct platform_device
*__init
1083 at32_add_device_eth(unsigned int id
, struct eth_platform_data
*data
)
1085 struct platform_device
*pdev
;
1090 pdev
= &macb0_device
;
1092 pin_mask
= (1 << 3); /* TXD0 */
1093 pin_mask
|= (1 << 4); /* TXD1 */
1094 pin_mask
|= (1 << 7); /* TXEN */
1095 pin_mask
|= (1 << 8); /* TXCK */
1096 pin_mask
|= (1 << 9); /* RXD0 */
1097 pin_mask
|= (1 << 10); /* RXD1 */
1098 pin_mask
|= (1 << 13); /* RXER */
1099 pin_mask
|= (1 << 15); /* RXDV */
1100 pin_mask
|= (1 << 16); /* MDC */
1101 pin_mask
|= (1 << 17); /* MDIO */
1103 if (!data
->is_rmii
) {
1104 pin_mask
|= (1 << 0); /* COL */
1105 pin_mask
|= (1 << 1); /* CRS */
1106 pin_mask
|= (1 << 2); /* TXER */
1107 pin_mask
|= (1 << 5); /* TXD2 */
1108 pin_mask
|= (1 << 6); /* TXD3 */
1109 pin_mask
|= (1 << 11); /* RXD2 */
1110 pin_mask
|= (1 << 12); /* RXD3 */
1111 pin_mask
|= (1 << 14); /* RXCK */
1112 #ifndef CONFIG_BOARD_MIMC200
1113 pin_mask
|= (1 << 18); /* SPD */
1117 select_peripheral(PIOC
, pin_mask
, PERIPH_A
, 0);
1122 pdev
= &macb1_device
;
1124 pin_mask
= (1 << 13); /* TXD0 */
1125 pin_mask
|= (1 << 14); /* TXD1 */
1126 pin_mask
|= (1 << 11); /* TXEN */
1127 pin_mask
|= (1 << 12); /* TXCK */
1128 pin_mask
|= (1 << 10); /* RXD0 */
1129 pin_mask
|= (1 << 6); /* RXD1 */
1130 pin_mask
|= (1 << 5); /* RXER */
1131 pin_mask
|= (1 << 4); /* RXDV */
1132 pin_mask
|= (1 << 3); /* MDC */
1133 pin_mask
|= (1 << 2); /* MDIO */
1135 #ifndef CONFIG_BOARD_MIMC200
1137 pin_mask
|= (1 << 15); /* SPD */
1140 select_peripheral(PIOD
, pin_mask
, PERIPH_B
, 0);
1142 if (!data
->is_rmii
) {
1143 pin_mask
= (1 << 19); /* COL */
1144 pin_mask
|= (1 << 23); /* CRS */
1145 pin_mask
|= (1 << 26); /* TXER */
1146 pin_mask
|= (1 << 27); /* TXD2 */
1147 pin_mask
|= (1 << 28); /* TXD3 */
1148 pin_mask
|= (1 << 29); /* RXD2 */
1149 pin_mask
|= (1 << 30); /* RXD3 */
1150 pin_mask
|= (1 << 24); /* RXCK */
1152 select_peripheral(PIOC
, pin_mask
, PERIPH_B
, 0);
1160 memcpy(pdev
->dev
.platform_data
, data
, sizeof(struct eth_platform_data
));
1161 platform_device_register(pdev
);
1167 /* --------------------------------------------------------------------
1169 * -------------------------------------------------------------------- */
1170 static struct resource atmel_spi0_resource
[] = {
1174 DEFINE_DEV(atmel_spi
, 0);
1175 DEV_CLK(spi_clk
, atmel_spi0
, pba
, 0);
1177 static struct resource atmel_spi1_resource
[] = {
1181 DEFINE_DEV(atmel_spi
, 1);
1182 DEV_CLK(spi_clk
, atmel_spi1
, pba
, 1);
1185 at32_spi_setup_slaves(unsigned int bus_num
, struct spi_board_info
*b
, unsigned int n
)
1188 * Manage the chipselects as GPIOs, normally using the same pins
1189 * the SPI controller expects; but boards can use other pins.
1191 static u8 __initdata spi_pins
[][4] = {
1192 { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
1193 GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
1194 { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
1195 GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
1197 unsigned int pin
, mode
;
1199 /* There are only 2 SPI controllers */
1203 for (; n
; n
--, b
++) {
1204 b
->bus_num
= bus_num
;
1205 if (b
->chip_select
>= 4)
1207 pin
= (unsigned)b
->controller_data
;
1209 pin
= spi_pins
[bus_num
][b
->chip_select
];
1210 b
->controller_data
= (void *)pin
;
1212 mode
= AT32_GPIOF_OUTPUT
;
1213 if (!(b
->mode
& SPI_CS_HIGH
))
1214 mode
|= AT32_GPIOF_HIGH
;
1215 at32_select_gpio(pin
, mode
);
1219 struct platform_device
*__init
1220 at32_add_device_spi(unsigned int id
, struct spi_board_info
*b
, unsigned int n
)
1222 struct platform_device
*pdev
;
1227 pdev
= &atmel_spi0_device
;
1228 pin_mask
= (1 << 1) | (1 << 2); /* MOSI & SCK */
1230 /* pullup MISO so a level is always defined */
1231 select_peripheral(PIOA
, (1 << 0), PERIPH_A
, AT32_GPIOF_PULLUP
);
1232 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1234 at32_spi_setup_slaves(0, b
, n
);
1238 pdev
= &atmel_spi1_device
;
1239 pin_mask
= (1 << 1) | (1 << 5); /* MOSI */
1241 /* pullup MISO so a level is always defined */
1242 select_peripheral(PIOB
, (1 << 0), PERIPH_B
, AT32_GPIOF_PULLUP
);
1243 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, 0);
1245 at32_spi_setup_slaves(1, b
, n
);
1252 spi_register_board_info(b
, n
);
1253 platform_device_register(pdev
);
1257 /* --------------------------------------------------------------------
1259 * -------------------------------------------------------------------- */
1260 static struct resource atmel_twi0_resource
[] __initdata
= {
1264 static struct clk atmel_twi0_pclk
= {
1267 .mode
= pba_clk_mode
,
1268 .get_rate
= pba_clk_get_rate
,
1272 struct platform_device
*__init
at32_add_device_twi(unsigned int id
,
1273 struct i2c_board_info
*b
,
1276 struct platform_device
*pdev
;
1282 pdev
= platform_device_alloc("atmel_twi", id
);
1286 if (platform_device_add_resources(pdev
, atmel_twi0_resource
,
1287 ARRAY_SIZE(atmel_twi0_resource
)))
1288 goto err_add_resources
;
1290 pin_mask
= (1 << 6) | (1 << 7); /* SDA & SDL */
1292 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1294 atmel_twi0_pclk
.dev
= &pdev
->dev
;
1297 i2c_register_board_info(id
, b
, n
);
1299 platform_device_add(pdev
);
1303 platform_device_put(pdev
);
1307 /* --------------------------------------------------------------------
1309 * -------------------------------------------------------------------- */
1310 static struct resource atmel_mci0_resource
[] __initdata
= {
1314 static struct clk atmel_mci0_pclk
= {
1317 .mode
= pbb_clk_mode
,
1318 .get_rate
= pbb_clk_get_rate
,
1322 struct platform_device
*__init
1323 at32_add_device_mci(unsigned int id
, struct mci_platform_data
*data
)
1325 struct platform_device
*pdev
;
1326 struct dw_dma_slave
*dws
;
1330 if (id
!= 0 || !data
)
1333 /* Must have at least one usable slot */
1334 if (!data
->slot
[0].bus_width
&& !data
->slot
[1].bus_width
)
1337 pdev
= platform_device_alloc("atmel_mci", id
);
1341 if (platform_device_add_resources(pdev
, atmel_mci0_resource
,
1342 ARRAY_SIZE(atmel_mci0_resource
)))
1345 dws
= kzalloc(sizeof(struct dw_dma_slave
), GFP_KERNEL
);
1347 dws
->dma_dev
= &dw_dmac0_device
.dev
;
1348 dws
->reg_width
= DW_DMA_SLAVE_WIDTH_32BIT
;
1349 dws
->cfg_hi
= (DWC_CFGH_SRC_PER(0)
1350 | DWC_CFGH_DST_PER(1));
1351 dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
1352 | DWC_CFGL_HS_SRC_POL
);
1354 data
->dma_slave
= dws
;
1356 if (platform_device_add_data(pdev
, data
,
1357 sizeof(struct mci_platform_data
)))
1360 /* CLK line is common to both slots */
1361 pioa_mask
= 1 << 10;
1363 switch (data
->slot
[0].bus_width
) {
1365 pioa_mask
|= 1 << 13; /* DATA1 */
1366 pioa_mask
|= 1 << 14; /* DATA2 */
1367 pioa_mask
|= 1 << 15; /* DATA3 */
1370 pioa_mask
|= 1 << 11; /* CMD */
1371 pioa_mask
|= 1 << 12; /* DATA0 */
1373 if (gpio_is_valid(data
->slot
[0].detect_pin
))
1374 at32_select_gpio(data
->slot
[0].detect_pin
, 0);
1375 if (gpio_is_valid(data
->slot
[0].wp_pin
))
1376 at32_select_gpio(data
->slot
[0].wp_pin
, 0);
1379 /* Slot is unused */
1385 select_peripheral(PIOA
, pioa_mask
, PERIPH_A
, 0);
1388 switch (data
->slot
[1].bus_width
) {
1390 piob_mask
|= 1 << 8; /* DATA1 */
1391 piob_mask
|= 1 << 9; /* DATA2 */
1392 piob_mask
|= 1 << 10; /* DATA3 */
1395 piob_mask
|= 1 << 6; /* CMD */
1396 piob_mask
|= 1 << 7; /* DATA0 */
1397 select_peripheral(PIOB
, piob_mask
, PERIPH_B
, 0);
1399 if (gpio_is_valid(data
->slot
[1].detect_pin
))
1400 at32_select_gpio(data
->slot
[1].detect_pin
, 0);
1401 if (gpio_is_valid(data
->slot
[1].wp_pin
))
1402 at32_select_gpio(data
->slot
[1].wp_pin
, 0);
1405 /* Slot is unused */
1408 if (!data
->slot
[0].bus_width
)
1411 data
->slot
[1].bus_width
= 0;
1415 atmel_mci0_pclk
.dev
= &pdev
->dev
;
1417 platform_device_add(pdev
);
1421 platform_device_put(pdev
);
1425 /* --------------------------------------------------------------------
1427 * -------------------------------------------------------------------- */
1428 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
1429 static struct atmel_lcdfb_info atmel_lcdfb0_data
;
1430 static struct resource atmel_lcdfb0_resource
[] = {
1432 .start
= 0xff000000,
1434 .flags
= IORESOURCE_MEM
,
1438 /* Placeholder for pre-allocated fb memory */
1439 .start
= 0x00000000,
1444 DEFINE_DEV_DATA(atmel_lcdfb
, 0);
1445 DEV_CLK(hck1
, atmel_lcdfb0
, hsb
, 7);
1446 static struct clk atmel_lcdfb0_pixclk
= {
1448 .dev
= &atmel_lcdfb0_device
.dev
,
1449 .mode
= genclk_mode
,
1450 .get_rate
= genclk_get_rate
,
1451 .set_rate
= genclk_set_rate
,
1452 .set_parent
= genclk_set_parent
,
1456 struct platform_device
*__init
1457 at32_add_device_lcdc(unsigned int id
, struct atmel_lcdfb_info
*data
,
1458 unsigned long fbmem_start
, unsigned long fbmem_len
,
1461 struct platform_device
*pdev
;
1462 struct atmel_lcdfb_info
*info
;
1463 struct fb_monspecs
*monspecs
;
1464 struct fb_videomode
*modedb
;
1465 unsigned int modedb_size
;
1466 u32 portc_mask
, portd_mask
, porte_mask
;
1469 * Do a deep copy of the fb data, monspecs and modedb. Make
1470 * sure all allocations are done before setting up the
1473 monspecs
= kmemdup(data
->default_monspecs
,
1474 sizeof(struct fb_monspecs
), GFP_KERNEL
);
1478 modedb_size
= sizeof(struct fb_videomode
) * monspecs
->modedb_len
;
1479 modedb
= kmemdup(monspecs
->modedb
, modedb_size
, GFP_KERNEL
);
1481 goto err_dup_modedb
;
1482 monspecs
->modedb
= modedb
;
1486 pdev
= &atmel_lcdfb0_device
;
1488 if (pin_mask
== 0ULL)
1489 /* Default to "full" lcdc control signals and 24bit */
1490 pin_mask
= ATMEL_LCDC_PRI_24BIT
| ATMEL_LCDC_PRI_CONTROL
;
1492 /* LCDC on port C */
1493 portc_mask
= pin_mask
& 0xfff80000;
1494 select_peripheral(PIOC
, portc_mask
, PERIPH_A
, 0);
1496 /* LCDC on port D */
1497 portd_mask
= pin_mask
& 0x0003ffff;
1498 select_peripheral(PIOD
, portd_mask
, PERIPH_A
, 0);
1500 /* LCDC on port E */
1501 porte_mask
= (pin_mask
>> 32) & 0x0007ffff;
1502 select_peripheral(PIOE
, porte_mask
, PERIPH_B
, 0);
1504 clk_set_parent(&atmel_lcdfb0_pixclk
, &pll0
);
1505 clk_set_rate(&atmel_lcdfb0_pixclk
, clk_get_rate(&pll0
));
1509 goto err_invalid_id
;
1513 pdev
->resource
[2].start
= fbmem_start
;
1514 pdev
->resource
[2].end
= fbmem_start
+ fbmem_len
- 1;
1515 pdev
->resource
[2].flags
= IORESOURCE_MEM
;
1518 info
= pdev
->dev
.platform_data
;
1519 memcpy(info
, data
, sizeof(struct atmel_lcdfb_info
));
1520 info
->default_monspecs
= monspecs
;
1522 platform_device_register(pdev
);
1533 /* --------------------------------------------------------------------
1535 * -------------------------------------------------------------------- */
1536 static struct resource atmel_pwm0_resource
[] __initdata
= {
1540 static struct clk atmel_pwm0_mck
= {
1543 .mode
= pbb_clk_mode
,
1544 .get_rate
= pbb_clk_get_rate
,
1548 struct platform_device
*__init
at32_add_device_pwm(u32 mask
)
1550 struct platform_device
*pdev
;
1556 pdev
= platform_device_alloc("atmel_pwm", 0);
1560 if (platform_device_add_resources(pdev
, atmel_pwm0_resource
,
1561 ARRAY_SIZE(atmel_pwm0_resource
)))
1564 if (platform_device_add_data(pdev
, &mask
, sizeof(mask
)))
1568 if (mask
& (1 << 0))
1569 pin_mask
|= (1 << 28);
1570 if (mask
& (1 << 1))
1571 pin_mask
|= (1 << 29);
1573 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1576 if (mask
& (1 << 2))
1577 pin_mask
|= (1 << 21);
1578 if (mask
& (1 << 3))
1579 pin_mask
|= (1 << 22);
1581 select_peripheral(PIOA
, pin_mask
, PERIPH_B
, 0);
1583 atmel_pwm0_mck
.dev
= &pdev
->dev
;
1585 platform_device_add(pdev
);
1590 platform_device_put(pdev
);
1594 /* --------------------------------------------------------------------
1596 * -------------------------------------------------------------------- */
1597 static struct resource ssc0_resource
[] = {
1602 DEV_CLK(pclk
, ssc0
, pba
, 7);
1604 static struct resource ssc1_resource
[] = {
1609 DEV_CLK(pclk
, ssc1
, pba
, 8);
1611 static struct resource ssc2_resource
[] = {
1616 DEV_CLK(pclk
, ssc2
, pba
, 9);
1618 struct platform_device
*__init
1619 at32_add_device_ssc(unsigned int id
, unsigned int flags
)
1621 struct platform_device
*pdev
;
1626 pdev
= &ssc0_device
;
1627 if (flags
& ATMEL_SSC_RF
)
1628 pin_mask
|= (1 << 21); /* RF */
1629 if (flags
& ATMEL_SSC_RK
)
1630 pin_mask
|= (1 << 22); /* RK */
1631 if (flags
& ATMEL_SSC_TK
)
1632 pin_mask
|= (1 << 23); /* TK */
1633 if (flags
& ATMEL_SSC_TF
)
1634 pin_mask
|= (1 << 24); /* TF */
1635 if (flags
& ATMEL_SSC_TD
)
1636 pin_mask
|= (1 << 25); /* TD */
1637 if (flags
& ATMEL_SSC_RD
)
1638 pin_mask
|= (1 << 26); /* RD */
1641 select_peripheral(PIOA
, pin_mask
, PERIPH_A
, 0);
1645 pdev
= &ssc1_device
;
1646 if (flags
& ATMEL_SSC_RF
)
1647 pin_mask
|= (1 << 0); /* RF */
1648 if (flags
& ATMEL_SSC_RK
)
1649 pin_mask
|= (1 << 1); /* RK */
1650 if (flags
& ATMEL_SSC_TK
)
1651 pin_mask
|= (1 << 2); /* TK */
1652 if (flags
& ATMEL_SSC_TF
)
1653 pin_mask
|= (1 << 3); /* TF */
1654 if (flags
& ATMEL_SSC_TD
)
1655 pin_mask
|= (1 << 4); /* TD */
1656 if (flags
& ATMEL_SSC_RD
)
1657 pin_mask
|= (1 << 5); /* RD */
1660 select_peripheral(PIOA
, pin_mask
, PERIPH_B
, 0);
1664 pdev
= &ssc2_device
;
1665 if (flags
& ATMEL_SSC_TD
)
1666 pin_mask
|= (1 << 13); /* TD */
1667 if (flags
& ATMEL_SSC_RD
)
1668 pin_mask
|= (1 << 14); /* RD */
1669 if (flags
& ATMEL_SSC_TK
)
1670 pin_mask
|= (1 << 15); /* TK */
1671 if (flags
& ATMEL_SSC_TF
)
1672 pin_mask
|= (1 << 16); /* TF */
1673 if (flags
& ATMEL_SSC_RF
)
1674 pin_mask
|= (1 << 17); /* RF */
1675 if (flags
& ATMEL_SSC_RK
)
1676 pin_mask
|= (1 << 18); /* RK */
1679 select_peripheral(PIOB
, pin_mask
, PERIPH_A
, 0);
1686 platform_device_register(pdev
);
1690 /* --------------------------------------------------------------------
1691 * USB Device Controller
1692 * -------------------------------------------------------------------- */
1693 static struct resource usba0_resource
[] __initdata
= {
1695 .start
= 0xff300000,
1697 .flags
= IORESOURCE_MEM
,
1699 .start
= 0xfff03000,
1701 .flags
= IORESOURCE_MEM
,
1705 static struct clk usba0_pclk
= {
1708 .mode
= pbb_clk_mode
,
1709 .get_rate
= pbb_clk_get_rate
,
1712 static struct clk usba0_hclk
= {
1715 .mode
= hsb_clk_mode
,
1716 .get_rate
= hsb_clk_get_rate
,
1720 #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
1724 .fifo_size = maxpkt, \
1725 .nr_banks = maxbk, \
1730 static struct usba_ep_data at32_usba_ep
[] __initdata
= {
1731 EP("ep0", 0, 64, 1, 0, 0),
1732 EP("ep1", 1, 512, 2, 1, 1),
1733 EP("ep2", 2, 512, 2, 1, 1),
1734 EP("ep3-int", 3, 64, 3, 1, 0),
1735 EP("ep4-int", 4, 64, 3, 1, 0),
1736 EP("ep5", 5, 1024, 3, 1, 1),
1737 EP("ep6", 6, 1024, 3, 1, 1),
1742 struct platform_device
*__init
1743 at32_add_device_usba(unsigned int id
, struct usba_platform_data
*data
)
1746 * pdata doesn't have room for any endpoints, so we need to
1747 * append room for the ones we need right after it.
1750 struct usba_platform_data pdata
;
1751 struct usba_ep_data ep
[7];
1753 struct platform_device
*pdev
;
1758 pdev
= platform_device_alloc("atmel_usba_udc", 0);
1762 if (platform_device_add_resources(pdev
, usba0_resource
,
1763 ARRAY_SIZE(usba0_resource
)))
1767 usba_data
.pdata
.vbus_pin
= data
->vbus_pin
;
1769 usba_data
.pdata
.vbus_pin
= -EINVAL
;
1771 data
= &usba_data
.pdata
;
1772 data
->num_ep
= ARRAY_SIZE(at32_usba_ep
);
1773 memcpy(data
->ep
, at32_usba_ep
, sizeof(at32_usba_ep
));
1775 if (platform_device_add_data(pdev
, data
, sizeof(usba_data
)))
1778 if (gpio_is_valid(data
->vbus_pin
))
1779 at32_select_gpio(data
->vbus_pin
, 0);
1781 usba0_pclk
.dev
= &pdev
->dev
;
1782 usba0_hclk
.dev
= &pdev
->dev
;
1784 platform_device_add(pdev
);
1789 platform_device_put(pdev
);
1793 /* --------------------------------------------------------------------
1794 * IDE / CompactFlash
1795 * -------------------------------------------------------------------- */
1796 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
1797 static struct resource at32_smc_cs4_resource
[] __initdata
= {
1799 .start
= 0x04000000,
1801 .flags
= IORESOURCE_MEM
,
1803 IRQ(~0UL), /* Magic IRQ will be overridden */
1805 static struct resource at32_smc_cs5_resource
[] __initdata
= {
1807 .start
= 0x20000000,
1809 .flags
= IORESOURCE_MEM
,
1811 IRQ(~0UL), /* Magic IRQ will be overridden */
1814 static int __init
at32_init_ide_or_cf(struct platform_device
*pdev
,
1815 unsigned int cs
, unsigned int extint
)
1817 static unsigned int extint_pin_map
[4] __initdata
= {
1823 static bool common_pins_initialized __initdata
= false;
1824 unsigned int extint_pin
;
1828 if (extint
>= ARRAY_SIZE(extint_pin_map
))
1830 extint_pin
= extint_pin_map
[extint
];
1834 ret
= platform_device_add_resources(pdev
,
1835 at32_smc_cs4_resource
,
1836 ARRAY_SIZE(at32_smc_cs4_resource
));
1841 select_peripheral(PIOE
, (1 << 21), PERIPH_A
, 0);
1842 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, HMATRIX_EBI_CF0_ENABLE
);
1845 ret
= platform_device_add_resources(pdev
,
1846 at32_smc_cs5_resource
,
1847 ARRAY_SIZE(at32_smc_cs5_resource
));
1852 select_peripheral(PIOE
, (1 << 22), PERIPH_A
, 0);
1853 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, HMATRIX_EBI_CF1_ENABLE
);
1859 if (!common_pins_initialized
) {
1860 pin_mask
= (1 << 19); /* CFCE1 -> CS0_N */
1861 pin_mask
|= (1 << 20); /* CFCE2 -> CS1_N */
1862 pin_mask
|= (1 << 23); /* CFRNW -> DIR */
1863 pin_mask
|= (1 << 24); /* NWAIT <- IORDY */
1865 select_peripheral(PIOE
, pin_mask
, PERIPH_A
, 0);
1867 common_pins_initialized
= true;
1870 select_peripheral(PIOB
, extint_pin
, PERIPH_A
, AT32_GPIOF_DEGLITCH
);
1872 pdev
->resource
[1].start
= EIM_IRQ_BASE
+ extint
;
1873 pdev
->resource
[1].end
= pdev
->resource
[1].start
;
1878 struct platform_device
*__init
1879 at32_add_device_ide(unsigned int id
, unsigned int extint
,
1880 struct ide_platform_data
*data
)
1882 struct platform_device
*pdev
;
1884 pdev
= platform_device_alloc("at32_ide", id
);
1888 if (platform_device_add_data(pdev
, data
,
1889 sizeof(struct ide_platform_data
)))
1892 if (at32_init_ide_or_cf(pdev
, data
->cs
, extint
))
1895 platform_device_add(pdev
);
1899 platform_device_put(pdev
);
1903 struct platform_device
*__init
1904 at32_add_device_cf(unsigned int id
, unsigned int extint
,
1905 struct cf_platform_data
*data
)
1907 struct platform_device
*pdev
;
1909 pdev
= platform_device_alloc("at32_cf", id
);
1913 if (platform_device_add_data(pdev
, data
,
1914 sizeof(struct cf_platform_data
)))
1917 if (at32_init_ide_or_cf(pdev
, data
->cs
, extint
))
1920 if (gpio_is_valid(data
->detect_pin
))
1921 at32_select_gpio(data
->detect_pin
, AT32_GPIOF_DEGLITCH
);
1922 if (gpio_is_valid(data
->reset_pin
))
1923 at32_select_gpio(data
->reset_pin
, 0);
1924 if (gpio_is_valid(data
->vcc_pin
))
1925 at32_select_gpio(data
->vcc_pin
, 0);
1926 /* READY is used as extint, so we can't select it as gpio */
1928 platform_device_add(pdev
);
1932 platform_device_put(pdev
);
1937 /* --------------------------------------------------------------------
1938 * NAND Flash / SmartMedia
1939 * -------------------------------------------------------------------- */
1940 static struct resource smc_cs3_resource
[] __initdata
= {
1942 .start
= 0x0c000000,
1944 .flags
= IORESOURCE_MEM
,
1946 .start
= 0xfff03c00,
1948 .flags
= IORESOURCE_MEM
,
1952 struct platform_device
*__init
1953 at32_add_device_nand(unsigned int id
, struct atmel_nand_data
*data
)
1955 struct platform_device
*pdev
;
1957 if (id
!= 0 || !data
)
1960 pdev
= platform_device_alloc("atmel_nand", id
);
1964 if (platform_device_add_resources(pdev
, smc_cs3_resource
,
1965 ARRAY_SIZE(smc_cs3_resource
)))
1968 if (platform_device_add_data(pdev
, data
,
1969 sizeof(struct atmel_nand_data
)))
1972 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI
, HMATRIX_EBI_NAND_ENABLE
);
1973 if (data
->enable_pin
)
1974 at32_select_gpio(data
->enable_pin
,
1975 AT32_GPIOF_OUTPUT
| AT32_GPIOF_HIGH
);
1977 at32_select_gpio(data
->rdy_pin
, 0);
1979 at32_select_gpio(data
->det_pin
, 0);
1981 platform_device_add(pdev
);
1985 platform_device_put(pdev
);
1989 /* --------------------------------------------------------------------
1991 * -------------------------------------------------------------------- */
1992 static struct resource atmel_ac97c0_resource
[] __initdata
= {
1996 static struct clk atmel_ac97c0_pclk
= {
1999 .mode
= pbb_clk_mode
,
2000 .get_rate
= pbb_clk_get_rate
,
2004 struct platform_device
*__init
2005 at32_add_device_ac97c(unsigned int id
, struct ac97c_platform_data
*data
,
2008 struct platform_device
*pdev
;
2009 struct dw_dma_slave
*rx_dws
;
2010 struct dw_dma_slave
*tx_dws
;
2011 struct ac97c_platform_data _data
;
2017 pdev
= platform_device_alloc("atmel_ac97c", id
);
2021 if (platform_device_add_resources(pdev
, atmel_ac97c0_resource
,
2022 ARRAY_SIZE(atmel_ac97c0_resource
)))
2023 goto out_free_resources
;
2027 memset(data
, 0, sizeof(struct ac97c_platform_data
));
2028 data
->reset_pin
= -ENODEV
;
2031 rx_dws
= &data
->rx_dws
;
2032 tx_dws
= &data
->tx_dws
;
2034 /* Check if DMA slave interface for capture should be configured. */
2035 if (flags
& AC97C_CAPTURE
) {
2036 rx_dws
->dma_dev
= &dw_dmac0_device
.dev
;
2037 rx_dws
->reg_width
= DW_DMA_SLAVE_WIDTH_16BIT
;
2038 rx_dws
->cfg_hi
= DWC_CFGH_SRC_PER(3);
2039 rx_dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
);
2042 /* Check if DMA slave interface for playback should be configured. */
2043 if (flags
& AC97C_PLAYBACK
) {
2044 tx_dws
->dma_dev
= &dw_dmac0_device
.dev
;
2045 tx_dws
->reg_width
= DW_DMA_SLAVE_WIDTH_16BIT
;
2046 tx_dws
->cfg_hi
= DWC_CFGH_DST_PER(4);
2047 tx_dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
);
2050 if (platform_device_add_data(pdev
, data
,
2051 sizeof(struct ac97c_platform_data
)))
2052 goto out_free_resources
;
2054 /* SDO | SYNC | SCLK | SDI */
2055 pin_mask
= (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
2057 select_peripheral(PIOB
, pin_mask
, PERIPH_B
, 0);
2059 if (gpio_is_valid(data
->reset_pin
))
2060 at32_select_gpio(data
->reset_pin
, AT32_GPIOF_OUTPUT
2063 atmel_ac97c0_pclk
.dev
= &pdev
->dev
;
2065 platform_device_add(pdev
);
2069 platform_device_put(pdev
);
2073 /* --------------------------------------------------------------------
2075 * -------------------------------------------------------------------- */
2076 static struct resource abdac0_resource
[] __initdata
= {
2080 static struct clk abdac0_pclk
= {
2083 .mode
= pbb_clk_mode
,
2084 .get_rate
= pbb_clk_get_rate
,
2087 static struct clk abdac0_sample_clk
= {
2088 .name
= "sample_clk",
2089 .mode
= genclk_mode
,
2090 .get_rate
= genclk_get_rate
,
2091 .set_rate
= genclk_set_rate
,
2092 .set_parent
= genclk_set_parent
,
2096 struct platform_device
*__init
2097 at32_add_device_abdac(unsigned int id
, struct atmel_abdac_pdata
*data
)
2099 struct platform_device
*pdev
;
2100 struct dw_dma_slave
*dws
;
2103 if (id
!= 0 || !data
)
2106 pdev
= platform_device_alloc("atmel_abdac", id
);
2110 if (platform_device_add_resources(pdev
, abdac0_resource
,
2111 ARRAY_SIZE(abdac0_resource
)))
2112 goto out_free_resources
;
2116 dws
->dma_dev
= &dw_dmac0_device
.dev
;
2117 dws
->reg_width
= DW_DMA_SLAVE_WIDTH_32BIT
;
2118 dws
->cfg_hi
= DWC_CFGH_DST_PER(2);
2119 dws
->cfg_lo
&= ~(DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
);
2121 if (platform_device_add_data(pdev
, data
,
2122 sizeof(struct atmel_abdac_pdata
)))
2123 goto out_free_resources
;
2125 pin_mask
= (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
2126 pin_mask
|= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
2128 select_peripheral(PIOB
, pin_mask
, PERIPH_A
, 0);
2130 abdac0_pclk
.dev
= &pdev
->dev
;
2131 abdac0_sample_clk
.dev
= &pdev
->dev
;
2133 platform_device_add(pdev
);
2137 platform_device_put(pdev
);
2141 /* --------------------------------------------------------------------
2143 * -------------------------------------------------------------------- */
2144 static struct clk gclk0
= {
2146 .mode
= genclk_mode
,
2147 .get_rate
= genclk_get_rate
,
2148 .set_rate
= genclk_set_rate
,
2149 .set_parent
= genclk_set_parent
,
2152 static struct clk gclk1
= {
2154 .mode
= genclk_mode
,
2155 .get_rate
= genclk_get_rate
,
2156 .set_rate
= genclk_set_rate
,
2157 .set_parent
= genclk_set_parent
,
2160 static struct clk gclk2
= {
2162 .mode
= genclk_mode
,
2163 .get_rate
= genclk_get_rate
,
2164 .set_rate
= genclk_set_rate
,
2165 .set_parent
= genclk_set_parent
,
2168 static struct clk gclk3
= {
2170 .mode
= genclk_mode
,
2171 .get_rate
= genclk_get_rate
,
2172 .set_rate
= genclk_set_rate
,
2173 .set_parent
= genclk_set_parent
,
2176 static struct clk gclk4
= {
2178 .mode
= genclk_mode
,
2179 .get_rate
= genclk_get_rate
,
2180 .set_rate
= genclk_set_rate
,
2181 .set_parent
= genclk_set_parent
,
2185 static __initdata
struct clk
*init_clocks
[] = {
2216 &atmel_usart0_usart
,
2217 &atmel_usart1_usart
,
2218 &atmel_usart2_usart
,
2219 &atmel_usart3_usart
,
2221 #if defined(CONFIG_CPU_AT32AP7000)
2227 &atmel_spi0_spi_clk
,
2228 &atmel_spi1_spi_clk
,
2231 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
2233 &atmel_lcdfb0_pixclk
,
2250 void __init
setup_platform(void)
2252 u32 cpu_mask
= 0, hsb_mask
= 0, pba_mask
= 0, pbb_mask
= 0;
2255 if (pm_readl(MCCTRL
) & PM_BIT(PLLSEL
)) {
2257 cpu_clk
.parent
= &pll0
;
2260 cpu_clk
.parent
= &osc0
;
2263 if (pm_readl(PLL0
) & PM_BIT(PLLOSC
))
2264 pll0
.parent
= &osc1
;
2265 if (pm_readl(PLL1
) & PM_BIT(PLLOSC
))
2266 pll1
.parent
= &osc1
;
2268 genclk_init_parent(&gclk0
);
2269 genclk_init_parent(&gclk1
);
2270 genclk_init_parent(&gclk2
);
2271 genclk_init_parent(&gclk3
);
2272 genclk_init_parent(&gclk4
);
2273 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
2274 genclk_init_parent(&atmel_lcdfb0_pixclk
);
2276 genclk_init_parent(&abdac0_sample_clk
);
2279 * Build initial dynamic clock list by registering all clocks
2281 * At the same time, turn on all clocks that have at least one
2282 * user already, and turn off everything else. We only do this
2283 * for module clocks, and even though it isn't particularly
2284 * pretty to check the address of the mode function, it should
2287 for (i
= 0; i
< ARRAY_SIZE(init_clocks
); i
++) {
2288 struct clk
*clk
= init_clocks
[i
];
2290 /* first, register clock */
2291 at32_clk_register(clk
);
2293 if (clk
->users
== 0)
2296 if (clk
->mode
== &cpu_clk_mode
)
2297 cpu_mask
|= 1 << clk
->index
;
2298 else if (clk
->mode
== &hsb_clk_mode
)
2299 hsb_mask
|= 1 << clk
->index
;
2300 else if (clk
->mode
== &pba_clk_mode
)
2301 pba_mask
|= 1 << clk
->index
;
2302 else if (clk
->mode
== &pbb_clk_mode
)
2303 pbb_mask
|= 1 << clk
->index
;
2306 pm_writel(CPU_MASK
, cpu_mask
);
2307 pm_writel(HSB_MASK
, hsb_mask
);
2308 pm_writel(PBA_MASK
, pba_mask
);
2309 pm_writel(PBB_MASK
, pbb_mask
);
2311 /* Initialize the port muxes */
2312 at32_init_pio(&pio0_device
);
2313 at32_init_pio(&pio1_device
);
2314 at32_init_pio(&pio2_device
);
2315 at32_init_pio(&pio3_device
);
2316 at32_init_pio(&pio4_device
);
2319 struct gen_pool
*sram_pool
;
2321 static int __init
sram_init(void)
2323 struct gen_pool
*pool
;
2325 /* 1KiB granularity */
2326 pool
= gen_pool_create(10, -1);
2330 if (gen_pool_add(pool
, 0x24000000, 0x8000, -1))
2337 gen_pool_destroy(pool
);
2339 pr_err("Failed to create SRAM pool\n");
2342 core_initcall(sram_init
);