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.
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/spi/spi.h>
17 #include <asm/arch/at32ap7000.h>
18 #include <asm/arch/board.h>
19 #include <asm/arch/portmux.h>
21 #include <video/atmel_lcdc.h>
29 * We can reduce the code size a bit by using a constant here. Since
30 * this file is completely chip-specific, it's safe to not use
31 * ioremap. Generic drivers should of course never do this.
33 #define AT32_PM_BASE 0xfff00000
38 .end = base + 0x3ff, \
39 .flags = IORESOURCE_MEM, \
45 .flags = IORESOURCE_IRQ, \
47 #define NAMED_IRQ(num, _name) \
52 .flags = IORESOURCE_IRQ, \
55 /* REVISIT these assume *every* device supports DMA, but several
56 * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
58 #define DEFINE_DEV(_name, _id) \
59 static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
60 static struct platform_device _name##_id##_device = { \
64 .dma_mask = &_name##_id##_dma_mask, \
65 .coherent_dma_mask = DMA_32BIT_MASK, \
67 .resource = _name##_id##_resource, \
68 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
70 #define DEFINE_DEV_DATA(_name, _id) \
71 static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
72 static struct platform_device _name##_id##_device = { \
76 .dma_mask = &_name##_id##_dma_mask, \
77 .platform_data = &_name##_id##_data, \
78 .coherent_dma_mask = DMA_32BIT_MASK, \
80 .resource = _name##_id##_resource, \
81 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
84 #define select_peripheral(pin, periph, flags) \
85 at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
87 #define DEV_CLK(_name, devname, bus, _index) \
88 static struct clk devname##_##_name = { \
90 .dev = &devname##_device.dev, \
91 .parent = &bus##_clk, \
92 .mode = bus##_clk_mode, \
93 .get_rate = bus##_clk_get_rate, \
97 static DEFINE_SPINLOCK(pm_lock
);
99 unsigned long at32ap7000_osc_rates
[3] = {
101 /* FIXME: these are ATSTK1002-specific */
106 static unsigned long osc_get_rate(struct clk
*clk
)
108 return at32ap7000_osc_rates
[clk
->index
];
111 static unsigned long pll_get_rate(struct clk
*clk
, unsigned long control
)
113 unsigned long div
, mul
, rate
;
115 if (!(control
& PM_BIT(PLLEN
)))
118 div
= PM_BFEXT(PLLDIV
, control
) + 1;
119 mul
= PM_BFEXT(PLLMUL
, control
) + 1;
121 rate
= clk
->parent
->get_rate(clk
->parent
);
122 rate
= (rate
+ div
/ 2) / div
;
128 static unsigned long pll0_get_rate(struct clk
*clk
)
132 control
= pm_readl(PLL0
);
134 return pll_get_rate(clk
, control
);
137 static unsigned long pll1_get_rate(struct clk
*clk
)
141 control
= pm_readl(PLL1
);
143 return pll_get_rate(clk
, control
);
147 * The AT32AP7000 has five primary clock sources: One 32kHz
148 * oscillator, two crystal oscillators and two PLLs.
150 static struct clk osc32k
= {
152 .get_rate
= osc_get_rate
,
156 static struct clk osc0
= {
158 .get_rate
= osc_get_rate
,
162 static struct clk osc1
= {
164 .get_rate
= osc_get_rate
,
167 static struct clk pll0
= {
169 .get_rate
= pll0_get_rate
,
172 static struct clk pll1
= {
174 .get_rate
= pll1_get_rate
,
179 * The main clock can be either osc0 or pll0. The boot loader may
180 * have chosen one for us, so we don't really know which one until we
181 * have a look at the SM.
183 static struct clk
*main_clock
;
186 * Synchronous clocks are generated from the main clock. The clocks
187 * must satisfy the constraint
188 * fCPU >= fHSB >= fPB
189 * i.e. each clock must not be faster than its parent.
191 static unsigned long bus_clk_get_rate(struct clk
*clk
, unsigned int shift
)
193 return main_clock
->get_rate(main_clock
) >> shift
;
196 static void cpu_clk_mode(struct clk
*clk
, int enabled
)
201 spin_lock_irqsave(&pm_lock
, flags
);
202 mask
= pm_readl(CPU_MASK
);
204 mask
|= 1 << clk
->index
;
206 mask
&= ~(1 << clk
->index
);
207 pm_writel(CPU_MASK
, mask
);
208 spin_unlock_irqrestore(&pm_lock
, flags
);
211 static unsigned long cpu_clk_get_rate(struct clk
*clk
)
213 unsigned long cksel
, shift
= 0;
215 cksel
= pm_readl(CKSEL
);
216 if (cksel
& PM_BIT(CPUDIV
))
217 shift
= PM_BFEXT(CPUSEL
, cksel
) + 1;
219 return bus_clk_get_rate(clk
, shift
);
222 static long cpu_clk_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
225 unsigned long parent_rate
, child_div
, actual_rate
, div
;
227 parent_rate
= clk
->parent
->get_rate(clk
->parent
);
228 control
= pm_readl(CKSEL
);
230 if (control
& PM_BIT(HSBDIV
))
231 child_div
= 1 << (PM_BFEXT(HSBSEL
, control
) + 1);
235 if (rate
> 3 * (parent_rate
/ 4) || child_div
== 1) {
236 actual_rate
= parent_rate
;
237 control
&= ~PM_BIT(CPUDIV
);
240 div
= (parent_rate
+ rate
/ 2) / rate
;
243 cpusel
= (div
> 1) ? (fls(div
) - 2) : 0;
244 control
= PM_BIT(CPUDIV
) | PM_BFINS(CPUSEL
, cpusel
, control
);
245 actual_rate
= parent_rate
/ (1 << (cpusel
+ 1));
248 pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
249 clk
->name
, rate
, actual_rate
);
252 pm_writel(CKSEL
, control
);
257 static void hsb_clk_mode(struct clk
*clk
, int enabled
)
262 spin_lock_irqsave(&pm_lock
, flags
);
263 mask
= pm_readl(HSB_MASK
);
265 mask
|= 1 << clk
->index
;
267 mask
&= ~(1 << clk
->index
);
268 pm_writel(HSB_MASK
, mask
);
269 spin_unlock_irqrestore(&pm_lock
, flags
);
272 static unsigned long hsb_clk_get_rate(struct clk
*clk
)
274 unsigned long cksel
, shift
= 0;
276 cksel
= pm_readl(CKSEL
);
277 if (cksel
& PM_BIT(HSBDIV
))
278 shift
= PM_BFEXT(HSBSEL
, cksel
) + 1;
280 return bus_clk_get_rate(clk
, shift
);
283 static void pba_clk_mode(struct clk
*clk
, int enabled
)
288 spin_lock_irqsave(&pm_lock
, flags
);
289 mask
= pm_readl(PBA_MASK
);
291 mask
|= 1 << clk
->index
;
293 mask
&= ~(1 << clk
->index
);
294 pm_writel(PBA_MASK
, mask
);
295 spin_unlock_irqrestore(&pm_lock
, flags
);
298 static unsigned long pba_clk_get_rate(struct clk
*clk
)
300 unsigned long cksel
, shift
= 0;
302 cksel
= pm_readl(CKSEL
);
303 if (cksel
& PM_BIT(PBADIV
))
304 shift
= PM_BFEXT(PBASEL
, cksel
) + 1;
306 return bus_clk_get_rate(clk
, shift
);
309 static void pbb_clk_mode(struct clk
*clk
, int enabled
)
314 spin_lock_irqsave(&pm_lock
, flags
);
315 mask
= pm_readl(PBB_MASK
);
317 mask
|= 1 << clk
->index
;
319 mask
&= ~(1 << clk
->index
);
320 pm_writel(PBB_MASK
, mask
);
321 spin_unlock_irqrestore(&pm_lock
, flags
);
324 static unsigned long pbb_clk_get_rate(struct clk
*clk
)
326 unsigned long cksel
, shift
= 0;
328 cksel
= pm_readl(CKSEL
);
329 if (cksel
& PM_BIT(PBBDIV
))
330 shift
= PM_BFEXT(PBBSEL
, cksel
) + 1;
332 return bus_clk_get_rate(clk
, shift
);
335 static struct clk cpu_clk
= {
337 .get_rate
= cpu_clk_get_rate
,
338 .set_rate
= cpu_clk_set_rate
,
341 static struct clk hsb_clk
= {
344 .get_rate
= hsb_clk_get_rate
,
346 static struct clk pba_clk
= {
349 .mode
= hsb_clk_mode
,
350 .get_rate
= pba_clk_get_rate
,
353 static struct clk pbb_clk
= {
356 .mode
= hsb_clk_mode
,
357 .get_rate
= pbb_clk_get_rate
,
362 /* --------------------------------------------------------------------
363 * Generic Clock operations
364 * -------------------------------------------------------------------- */
366 static void genclk_mode(struct clk
*clk
, int enabled
)
370 control
= pm_readl(GCCTRL(clk
->index
));
372 control
|= PM_BIT(CEN
);
374 control
&= ~PM_BIT(CEN
);
375 pm_writel(GCCTRL(clk
->index
), control
);
378 static unsigned long genclk_get_rate(struct clk
*clk
)
381 unsigned long div
= 1;
383 control
= pm_readl(GCCTRL(clk
->index
));
384 if (control
& PM_BIT(DIVEN
))
385 div
= 2 * (PM_BFEXT(DIV
, control
) + 1);
387 return clk
->parent
->get_rate(clk
->parent
) / div
;
390 static long genclk_set_rate(struct clk
*clk
, unsigned long rate
, int apply
)
393 unsigned long parent_rate
, actual_rate
, div
;
395 parent_rate
= clk
->parent
->get_rate(clk
->parent
);
396 control
= pm_readl(GCCTRL(clk
->index
));
398 if (rate
> 3 * parent_rate
/ 4) {
399 actual_rate
= parent_rate
;
400 control
&= ~PM_BIT(DIVEN
);
402 div
= (parent_rate
+ rate
) / (2 * rate
) - 1;
403 control
= PM_BFINS(DIV
, div
, control
) | PM_BIT(DIVEN
);
404 actual_rate
= parent_rate
/ (2 * (div
+ 1));
407 dev_dbg(clk
->dev
, "clk %s: new rate %lu (actual rate %lu)\n",
408 clk
->name
, rate
, actual_rate
);
411 pm_writel(GCCTRL(clk
->index
), control
);
416 int genclk_set_parent(struct clk
*clk
, struct clk
*parent
)
420 dev_dbg(clk
->dev
, "clk %s: new parent %s (was %s)\n",
421 clk
->name
, parent
->name
, clk
->parent
->name
);
423 control
= pm_readl(GCCTRL(clk
->index
));
425 if (parent
== &osc1
|| parent
== &pll1
)
426 control
|= PM_BIT(OSCSEL
);
427 else if (parent
== &osc0
|| parent
== &pll0
)
428 control
&= ~PM_BIT(OSCSEL
);
432 if (parent
== &pll0
|| parent
== &pll1
)
433 control
|= PM_BIT(PLLSEL
);
435 control
&= ~PM_BIT(PLLSEL
);
437 pm_writel(GCCTRL(clk
->index
), control
);
438 clk
->parent
= parent
;
443 static void __init
genclk_init_parent(struct clk
*clk
)
448 BUG_ON(clk
->index
> 7);
450 control
= pm_readl(GCCTRL(clk
->index
));
451 if (control
& PM_BIT(OSCSEL
))
452 parent
= (control
& PM_BIT(PLLSEL
)) ? &pll1
: &osc1
;
454 parent
= (control
& PM_BIT(PLLSEL
)) ? &pll0
: &osc0
;
456 clk
->parent
= parent
;
459 /* --------------------------------------------------------------------
461 * -------------------------------------------------------------------- */
462 static struct resource at32_pm0_resource
[] = {
466 .flags
= IORESOURCE_MEM
,
471 static struct resource at32ap700x_rtc0_resource
[] = {
475 .flags
= IORESOURCE_MEM
,
480 static struct resource at32_wdt0_resource
[] = {
484 .flags
= IORESOURCE_MEM
,
488 static struct resource at32_eic0_resource
[] = {
492 .flags
= IORESOURCE_MEM
,
497 DEFINE_DEV(at32_pm
, 0);
498 DEFINE_DEV(at32ap700x_rtc
, 0);
499 DEFINE_DEV(at32_wdt
, 0);
500 DEFINE_DEV(at32_eic
, 0);
503 * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
506 static struct clk at32_pm_pclk
= {
508 .dev
= &at32_pm0_device
.dev
,
510 .mode
= pbb_clk_mode
,
511 .get_rate
= pbb_clk_get_rate
,
516 static struct resource intc0_resource
[] = {
519 struct platform_device at32_intc0_device
= {
522 .resource
= intc0_resource
,
523 .num_resources
= ARRAY_SIZE(intc0_resource
),
525 DEV_CLK(pclk
, at32_intc0
, pbb
, 1);
527 static struct clk ebi_clk
= {
530 .mode
= hsb_clk_mode
,
531 .get_rate
= hsb_clk_get_rate
,
534 static struct clk hramc_clk
= {
537 .mode
= hsb_clk_mode
,
538 .get_rate
= hsb_clk_get_rate
,
543 static struct resource smc0_resource
[] = {
547 DEV_CLK(pclk
, smc0
, pbb
, 13);
548 DEV_CLK(mck
, smc0
, hsb
, 0);
550 static struct platform_device pdc_device
= {
554 DEV_CLK(hclk
, pdc
, hsb
, 4);
555 DEV_CLK(pclk
, pdc
, pba
, 16);
557 static struct clk pico_clk
= {
560 .mode
= cpu_clk_mode
,
561 .get_rate
= cpu_clk_get_rate
,
565 /* --------------------------------------------------------------------
567 * -------------------------------------------------------------------- */
569 static struct clk hmatrix_clk
= {
570 .name
= "hmatrix_clk",
572 .mode
= pbb_clk_mode
,
573 .get_rate
= pbb_clk_get_rate
,
577 #define HMATRIX_BASE ((void __iomem *)0xfff00800)
579 #define hmatrix_readl(reg) \
580 __raw_readl((HMATRIX_BASE) + HMATRIX_##reg)
581 #define hmatrix_writel(reg,value) \
582 __raw_writel((value), (HMATRIX_BASE) + HMATRIX_##reg)
585 * Set bits in the HMATRIX Special Function Register (SFR) used by the
586 * External Bus Interface (EBI). This can be used to enable special
587 * features like CompactFlash support, NAND Flash support, etc. on
588 * certain chipselects.
590 static inline void set_ebi_sfr_bits(u32 mask
)
594 clk_enable(&hmatrix_clk
);
595 sfr
= hmatrix_readl(SFR4
);
597 hmatrix_writel(SFR4
, sfr
);
598 clk_disable(&hmatrix_clk
);
601 /* --------------------------------------------------------------------
602 * System Timer/Counter (TC)
603 * -------------------------------------------------------------------- */
604 static struct resource at32_systc0_resource
[] = {
608 struct platform_device at32_systc0_device
= {
611 .resource
= at32_systc0_resource
,
612 .num_resources
= ARRAY_SIZE(at32_systc0_resource
),
614 DEV_CLK(pclk
, at32_systc0
, pbb
, 3);
616 /* --------------------------------------------------------------------
618 * -------------------------------------------------------------------- */
620 static struct resource pio0_resource
[] = {
625 DEV_CLK(mck
, pio0
, pba
, 10);
627 static struct resource pio1_resource
[] = {
632 DEV_CLK(mck
, pio1
, pba
, 11);
634 static struct resource pio2_resource
[] = {
639 DEV_CLK(mck
, pio2
, pba
, 12);
641 static struct resource pio3_resource
[] = {
646 DEV_CLK(mck
, pio3
, pba
, 13);
648 static struct resource pio4_resource
[] = {
653 DEV_CLK(mck
, pio4
, pba
, 14);
655 void __init
at32_add_system_devices(void)
657 platform_device_register(&at32_pm0_device
);
658 platform_device_register(&at32_intc0_device
);
659 platform_device_register(&at32ap700x_rtc0_device
);
660 platform_device_register(&at32_wdt0_device
);
661 platform_device_register(&at32_eic0_device
);
662 platform_device_register(&smc0_device
);
663 platform_device_register(&pdc_device
);
665 platform_device_register(&at32_systc0_device
);
667 platform_device_register(&pio0_device
);
668 platform_device_register(&pio1_device
);
669 platform_device_register(&pio2_device
);
670 platform_device_register(&pio3_device
);
671 platform_device_register(&pio4_device
);
674 /* --------------------------------------------------------------------
676 * -------------------------------------------------------------------- */
678 static struct atmel_uart_data atmel_usart0_data
= {
682 static struct resource atmel_usart0_resource
[] = {
686 DEFINE_DEV_DATA(atmel_usart
, 0);
687 DEV_CLK(usart
, atmel_usart0
, pba
, 4);
689 static struct atmel_uart_data atmel_usart1_data
= {
693 static struct resource atmel_usart1_resource
[] = {
697 DEFINE_DEV_DATA(atmel_usart
, 1);
698 DEV_CLK(usart
, atmel_usart1
, pba
, 4);
700 static struct atmel_uart_data atmel_usart2_data
= {
704 static struct resource atmel_usart2_resource
[] = {
708 DEFINE_DEV_DATA(atmel_usart
, 2);
709 DEV_CLK(usart
, atmel_usart2
, pba
, 5);
711 static struct atmel_uart_data atmel_usart3_data
= {
715 static struct resource atmel_usart3_resource
[] = {
719 DEFINE_DEV_DATA(atmel_usart
, 3);
720 DEV_CLK(usart
, atmel_usart3
, pba
, 6);
722 static inline void configure_usart0_pins(void)
724 select_peripheral(PA(8), PERIPH_B
, 0); /* RXD */
725 select_peripheral(PA(9), PERIPH_B
, 0); /* TXD */
728 static inline void configure_usart1_pins(void)
730 select_peripheral(PA(17), PERIPH_A
, 0); /* RXD */
731 select_peripheral(PA(18), PERIPH_A
, 0); /* TXD */
734 static inline void configure_usart2_pins(void)
736 select_peripheral(PB(26), PERIPH_B
, 0); /* RXD */
737 select_peripheral(PB(27), PERIPH_B
, 0); /* TXD */
740 static inline void configure_usart3_pins(void)
742 select_peripheral(PB(18), PERIPH_B
, 0); /* RXD */
743 select_peripheral(PB(17), PERIPH_B
, 0); /* TXD */
746 static struct platform_device
*__initdata at32_usarts
[4];
748 void __init
at32_map_usart(unsigned int hw_id
, unsigned int line
)
750 struct platform_device
*pdev
;
754 pdev
= &atmel_usart0_device
;
755 configure_usart0_pins();
758 pdev
= &atmel_usart1_device
;
759 configure_usart1_pins();
762 pdev
= &atmel_usart2_device
;
763 configure_usart2_pins();
766 pdev
= &atmel_usart3_device
;
767 configure_usart3_pins();
773 if (PXSEG(pdev
->resource
[0].start
) == P4SEG
) {
774 /* Addresses in the P4 segment are permanently mapped 1:1 */
775 struct atmel_uart_data
*data
= pdev
->dev
.platform_data
;
776 data
->regs
= (void __iomem
*)pdev
->resource
[0].start
;
780 at32_usarts
[line
] = pdev
;
783 struct platform_device
*__init
at32_add_device_usart(unsigned int id
)
785 platform_device_register(at32_usarts
[id
]);
786 return at32_usarts
[id
];
789 struct platform_device
*atmel_default_console_device
;
791 void __init
at32_setup_serial_console(unsigned int usart_id
)
793 atmel_default_console_device
= at32_usarts
[usart_id
];
796 /* --------------------------------------------------------------------
798 * -------------------------------------------------------------------- */
800 static struct eth_platform_data macb0_data
;
801 static struct resource macb0_resource
[] = {
805 DEFINE_DEV_DATA(macb
, 0);
806 DEV_CLK(hclk
, macb0
, hsb
, 8);
807 DEV_CLK(pclk
, macb0
, pbb
, 6);
809 static struct eth_platform_data macb1_data
;
810 static struct resource macb1_resource
[] = {
814 DEFINE_DEV_DATA(macb
, 1);
815 DEV_CLK(hclk
, macb1
, hsb
, 9);
816 DEV_CLK(pclk
, macb1
, pbb
, 7);
818 struct platform_device
*__init
819 at32_add_device_eth(unsigned int id
, struct eth_platform_data
*data
)
821 struct platform_device
*pdev
;
825 pdev
= &macb0_device
;
827 select_peripheral(PC(3), PERIPH_A
, 0); /* TXD0 */
828 select_peripheral(PC(4), PERIPH_A
, 0); /* TXD1 */
829 select_peripheral(PC(7), PERIPH_A
, 0); /* TXEN */
830 select_peripheral(PC(8), PERIPH_A
, 0); /* TXCK */
831 select_peripheral(PC(9), PERIPH_A
, 0); /* RXD0 */
832 select_peripheral(PC(10), PERIPH_A
, 0); /* RXD1 */
833 select_peripheral(PC(13), PERIPH_A
, 0); /* RXER */
834 select_peripheral(PC(15), PERIPH_A
, 0); /* RXDV */
835 select_peripheral(PC(16), PERIPH_A
, 0); /* MDC */
836 select_peripheral(PC(17), PERIPH_A
, 0); /* MDIO */
838 if (!data
->is_rmii
) {
839 select_peripheral(PC(0), PERIPH_A
, 0); /* COL */
840 select_peripheral(PC(1), PERIPH_A
, 0); /* CRS */
841 select_peripheral(PC(2), PERIPH_A
, 0); /* TXER */
842 select_peripheral(PC(5), PERIPH_A
, 0); /* TXD2 */
843 select_peripheral(PC(6), PERIPH_A
, 0); /* TXD3 */
844 select_peripheral(PC(11), PERIPH_A
, 0); /* RXD2 */
845 select_peripheral(PC(12), PERIPH_A
, 0); /* RXD3 */
846 select_peripheral(PC(14), PERIPH_A
, 0); /* RXCK */
847 select_peripheral(PC(18), PERIPH_A
, 0); /* SPD */
852 pdev
= &macb1_device
;
854 select_peripheral(PD(13), PERIPH_B
, 0); /* TXD0 */
855 select_peripheral(PD(14), PERIPH_B
, 0); /* TXD1 */
856 select_peripheral(PD(11), PERIPH_B
, 0); /* TXEN */
857 select_peripheral(PD(12), PERIPH_B
, 0); /* TXCK */
858 select_peripheral(PD(10), PERIPH_B
, 0); /* RXD0 */
859 select_peripheral(PD(6), PERIPH_B
, 0); /* RXD1 */
860 select_peripheral(PD(5), PERIPH_B
, 0); /* RXER */
861 select_peripheral(PD(4), PERIPH_B
, 0); /* RXDV */
862 select_peripheral(PD(3), PERIPH_B
, 0); /* MDC */
863 select_peripheral(PD(2), PERIPH_B
, 0); /* MDIO */
865 if (!data
->is_rmii
) {
866 select_peripheral(PC(19), PERIPH_B
, 0); /* COL */
867 select_peripheral(PC(23), PERIPH_B
, 0); /* CRS */
868 select_peripheral(PC(26), PERIPH_B
, 0); /* TXER */
869 select_peripheral(PC(27), PERIPH_B
, 0); /* TXD2 */
870 select_peripheral(PC(28), PERIPH_B
, 0); /* TXD3 */
871 select_peripheral(PC(29), PERIPH_B
, 0); /* RXD2 */
872 select_peripheral(PC(30), PERIPH_B
, 0); /* RXD3 */
873 select_peripheral(PC(24), PERIPH_B
, 0); /* RXCK */
874 select_peripheral(PD(15), PERIPH_B
, 0); /* SPD */
882 memcpy(pdev
->dev
.platform_data
, data
, sizeof(struct eth_platform_data
));
883 platform_device_register(pdev
);
888 /* --------------------------------------------------------------------
890 * -------------------------------------------------------------------- */
891 static struct resource atmel_spi0_resource
[] = {
895 DEFINE_DEV(atmel_spi
, 0);
896 DEV_CLK(spi_clk
, atmel_spi0
, pba
, 0);
898 static struct resource atmel_spi1_resource
[] = {
902 DEFINE_DEV(atmel_spi
, 1);
903 DEV_CLK(spi_clk
, atmel_spi1
, pba
, 1);
906 at32_spi_setup_slaves(unsigned int bus_num
, struct spi_board_info
*b
,
907 unsigned int n
, const u8
*pins
)
909 unsigned int pin
, mode
;
911 for (; n
; n
--, b
++) {
912 b
->bus_num
= bus_num
;
913 if (b
->chip_select
>= 4)
915 pin
= (unsigned)b
->controller_data
;
917 pin
= pins
[b
->chip_select
];
918 b
->controller_data
= (void *)pin
;
920 mode
= AT32_GPIOF_OUTPUT
;
921 if (!(b
->mode
& SPI_CS_HIGH
))
922 mode
|= AT32_GPIOF_HIGH
;
923 at32_select_gpio(pin
, mode
);
927 struct platform_device
*__init
928 at32_add_device_spi(unsigned int id
, struct spi_board_info
*b
, unsigned int n
)
931 * Manage the chipselects as GPIOs, normally using the same pins
932 * the SPI controller expects; but boards can use other pins.
934 static u8 __initdata spi0_pins
[] =
935 { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
936 GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
937 static u8 __initdata spi1_pins
[] =
938 { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
939 GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
940 struct platform_device
*pdev
;
944 pdev
= &atmel_spi0_device
;
945 select_peripheral(PA(0), PERIPH_A
, 0); /* MISO */
946 select_peripheral(PA(1), PERIPH_A
, 0); /* MOSI */
947 select_peripheral(PA(2), PERIPH_A
, 0); /* SCK */
948 at32_spi_setup_slaves(0, b
, n
, spi0_pins
);
952 pdev
= &atmel_spi1_device
;
953 select_peripheral(PB(0), PERIPH_B
, 0); /* MISO */
954 select_peripheral(PB(1), PERIPH_B
, 0); /* MOSI */
955 select_peripheral(PB(5), PERIPH_B
, 0); /* SCK */
956 at32_spi_setup_slaves(1, b
, n
, spi1_pins
);
963 spi_register_board_info(b
, n
);
964 platform_device_register(pdev
);
968 /* --------------------------------------------------------------------
970 * -------------------------------------------------------------------- */
971 static struct atmel_lcdfb_info atmel_lcdfb0_data
;
972 static struct resource atmel_lcdfb0_resource
[] = {
976 .flags
= IORESOURCE_MEM
,
980 /* Placeholder for pre-allocated fb memory */
986 DEFINE_DEV_DATA(atmel_lcdfb
, 0);
987 DEV_CLK(hck1
, atmel_lcdfb0
, hsb
, 7);
988 static struct clk atmel_lcdfb0_pixclk
= {
990 .dev
= &atmel_lcdfb0_device
.dev
,
992 .get_rate
= genclk_get_rate
,
993 .set_rate
= genclk_set_rate
,
994 .set_parent
= genclk_set_parent
,
998 struct platform_device
*__init
999 at32_add_device_lcdc(unsigned int id
, struct atmel_lcdfb_info
*data
,
1000 unsigned long fbmem_start
, unsigned long fbmem_len
)
1002 struct platform_device
*pdev
;
1003 struct atmel_lcdfb_info
*info
;
1004 struct fb_monspecs
*monspecs
;
1005 struct fb_videomode
*modedb
;
1006 unsigned int modedb_size
;
1009 * Do a deep copy of the fb data, monspecs and modedb. Make
1010 * sure all allocations are done before setting up the
1013 monspecs
= kmemdup(data
->default_monspecs
,
1014 sizeof(struct fb_monspecs
), GFP_KERNEL
);
1018 modedb_size
= sizeof(struct fb_videomode
) * monspecs
->modedb_len
;
1019 modedb
= kmemdup(monspecs
->modedb
, modedb_size
, GFP_KERNEL
);
1021 goto err_dup_modedb
;
1022 monspecs
->modedb
= modedb
;
1026 pdev
= &atmel_lcdfb0_device
;
1027 select_peripheral(PC(19), PERIPH_A
, 0); /* CC */
1028 select_peripheral(PC(20), PERIPH_A
, 0); /* HSYNC */
1029 select_peripheral(PC(21), PERIPH_A
, 0); /* PCLK */
1030 select_peripheral(PC(22), PERIPH_A
, 0); /* VSYNC */
1031 select_peripheral(PC(23), PERIPH_A
, 0); /* DVAL */
1032 select_peripheral(PC(24), PERIPH_A
, 0); /* MODE */
1033 select_peripheral(PC(25), PERIPH_A
, 0); /* PWR */
1034 select_peripheral(PC(26), PERIPH_A
, 0); /* DATA0 */
1035 select_peripheral(PC(27), PERIPH_A
, 0); /* DATA1 */
1036 select_peripheral(PC(28), PERIPH_A
, 0); /* DATA2 */
1037 select_peripheral(PC(29), PERIPH_A
, 0); /* DATA3 */
1038 select_peripheral(PC(30), PERIPH_A
, 0); /* DATA4 */
1039 select_peripheral(PC(31), PERIPH_A
, 0); /* DATA5 */
1040 select_peripheral(PD(0), PERIPH_A
, 0); /* DATA6 */
1041 select_peripheral(PD(1), PERIPH_A
, 0); /* DATA7 */
1042 select_peripheral(PD(2), PERIPH_A
, 0); /* DATA8 */
1043 select_peripheral(PD(3), PERIPH_A
, 0); /* DATA9 */
1044 select_peripheral(PD(4), PERIPH_A
, 0); /* DATA10 */
1045 select_peripheral(PD(5), PERIPH_A
, 0); /* DATA11 */
1046 select_peripheral(PD(6), PERIPH_A
, 0); /* DATA12 */
1047 select_peripheral(PD(7), PERIPH_A
, 0); /* DATA13 */
1048 select_peripheral(PD(8), PERIPH_A
, 0); /* DATA14 */
1049 select_peripheral(PD(9), PERIPH_A
, 0); /* DATA15 */
1050 select_peripheral(PD(10), PERIPH_A
, 0); /* DATA16 */
1051 select_peripheral(PD(11), PERIPH_A
, 0); /* DATA17 */
1052 select_peripheral(PD(12), PERIPH_A
, 0); /* DATA18 */
1053 select_peripheral(PD(13), PERIPH_A
, 0); /* DATA19 */
1054 select_peripheral(PD(14), PERIPH_A
, 0); /* DATA20 */
1055 select_peripheral(PD(15), PERIPH_A
, 0); /* DATA21 */
1056 select_peripheral(PD(16), PERIPH_A
, 0); /* DATA22 */
1057 select_peripheral(PD(17), PERIPH_A
, 0); /* DATA23 */
1059 clk_set_parent(&atmel_lcdfb0_pixclk
, &pll0
);
1060 clk_set_rate(&atmel_lcdfb0_pixclk
, clk_get_rate(&pll0
));
1064 goto err_invalid_id
;
1068 pdev
->resource
[2].start
= fbmem_start
;
1069 pdev
->resource
[2].end
= fbmem_start
+ fbmem_len
- 1;
1070 pdev
->resource
[2].flags
= IORESOURCE_MEM
;
1073 info
= pdev
->dev
.platform_data
;
1074 memcpy(info
, data
, sizeof(struct atmel_lcdfb_info
));
1075 info
->default_monspecs
= monspecs
;
1077 platform_device_register(pdev
);
1087 /* --------------------------------------------------------------------
1089 * -------------------------------------------------------------------- */
1090 static struct resource ssc0_resource
[] = {
1095 DEV_CLK(pclk
, ssc0
, pba
, 7);
1097 static struct resource ssc1_resource
[] = {
1102 DEV_CLK(pclk
, ssc1
, pba
, 8);
1104 static struct resource ssc2_resource
[] = {
1109 DEV_CLK(pclk
, ssc2
, pba
, 9);
1111 struct platform_device
*__init
1112 at32_add_device_ssc(unsigned int id
, unsigned int flags
)
1114 struct platform_device
*pdev
;
1118 pdev
= &ssc0_device
;
1119 if (flags
& ATMEL_SSC_RF
)
1120 select_peripheral(PA(21), PERIPH_A
, 0); /* RF */
1121 if (flags
& ATMEL_SSC_RK
)
1122 select_peripheral(PA(22), PERIPH_A
, 0); /* RK */
1123 if (flags
& ATMEL_SSC_TK
)
1124 select_peripheral(PA(23), PERIPH_A
, 0); /* TK */
1125 if (flags
& ATMEL_SSC_TF
)
1126 select_peripheral(PA(24), PERIPH_A
, 0); /* TF */
1127 if (flags
& ATMEL_SSC_TD
)
1128 select_peripheral(PA(25), PERIPH_A
, 0); /* TD */
1129 if (flags
& ATMEL_SSC_RD
)
1130 select_peripheral(PA(26), PERIPH_A
, 0); /* RD */
1133 pdev
= &ssc1_device
;
1134 if (flags
& ATMEL_SSC_RF
)
1135 select_peripheral(PA(0), PERIPH_B
, 0); /* RF */
1136 if (flags
& ATMEL_SSC_RK
)
1137 select_peripheral(PA(1), PERIPH_B
, 0); /* RK */
1138 if (flags
& ATMEL_SSC_TK
)
1139 select_peripheral(PA(2), PERIPH_B
, 0); /* TK */
1140 if (flags
& ATMEL_SSC_TF
)
1141 select_peripheral(PA(3), PERIPH_B
, 0); /* TF */
1142 if (flags
& ATMEL_SSC_TD
)
1143 select_peripheral(PA(4), PERIPH_B
, 0); /* TD */
1144 if (flags
& ATMEL_SSC_RD
)
1145 select_peripheral(PA(5), PERIPH_B
, 0); /* RD */
1148 pdev
= &ssc2_device
;
1149 if (flags
& ATMEL_SSC_TD
)
1150 select_peripheral(PB(13), PERIPH_A
, 0); /* TD */
1151 if (flags
& ATMEL_SSC_RD
)
1152 select_peripheral(PB(14), PERIPH_A
, 0); /* RD */
1153 if (flags
& ATMEL_SSC_TK
)
1154 select_peripheral(PB(15), PERIPH_A
, 0); /* TK */
1155 if (flags
& ATMEL_SSC_TF
)
1156 select_peripheral(PB(16), PERIPH_A
, 0); /* TF */
1157 if (flags
& ATMEL_SSC_RF
)
1158 select_peripheral(PB(17), PERIPH_A
, 0); /* RF */
1159 if (flags
& ATMEL_SSC_RK
)
1160 select_peripheral(PB(18), PERIPH_A
, 0); /* RK */
1166 platform_device_register(pdev
);
1170 /* --------------------------------------------------------------------
1172 * -------------------------------------------------------------------- */
1173 static struct clk gclk0
= {
1175 .mode
= genclk_mode
,
1176 .get_rate
= genclk_get_rate
,
1177 .set_rate
= genclk_set_rate
,
1178 .set_parent
= genclk_set_parent
,
1181 static struct clk gclk1
= {
1183 .mode
= genclk_mode
,
1184 .get_rate
= genclk_get_rate
,
1185 .set_rate
= genclk_set_rate
,
1186 .set_parent
= genclk_set_parent
,
1189 static struct clk gclk2
= {
1191 .mode
= genclk_mode
,
1192 .get_rate
= genclk_get_rate
,
1193 .set_rate
= genclk_set_rate
,
1194 .set_parent
= genclk_set_parent
,
1197 static struct clk gclk3
= {
1199 .mode
= genclk_mode
,
1200 .get_rate
= genclk_get_rate
,
1201 .set_rate
= genclk_set_rate
,
1202 .set_parent
= genclk_set_parent
,
1205 static struct clk gclk4
= {
1207 .mode
= genclk_mode
,
1208 .get_rate
= genclk_get_rate
,
1209 .set_rate
= genclk_set_rate
,
1210 .set_parent
= genclk_set_parent
,
1214 struct clk
*at32_clock_list
[] = {
1240 &atmel_usart0_usart
,
1241 &atmel_usart1_usart
,
1242 &atmel_usart2_usart
,
1243 &atmel_usart3_usart
,
1248 &atmel_spi0_spi_clk
,
1249 &atmel_spi1_spi_clk
,
1251 &atmel_lcdfb0_pixclk
,
1261 unsigned int at32_nr_clocks
= ARRAY_SIZE(at32_clock_list
);
1263 void __init
at32_portmux_init(void)
1265 at32_init_pio(&pio0_device
);
1266 at32_init_pio(&pio1_device
);
1267 at32_init_pio(&pio2_device
);
1268 at32_init_pio(&pio3_device
);
1269 at32_init_pio(&pio4_device
);
1272 void __init
at32_clock_init(void)
1274 u32 cpu_mask
= 0, hsb_mask
= 0, pba_mask
= 0, pbb_mask
= 0;
1277 if (pm_readl(MCCTRL
) & PM_BIT(PLLSEL
)) {
1279 cpu_clk
.parent
= &pll0
;
1282 cpu_clk
.parent
= &osc0
;
1285 if (pm_readl(PLL0
) & PM_BIT(PLLOSC
))
1286 pll0
.parent
= &osc1
;
1287 if (pm_readl(PLL1
) & PM_BIT(PLLOSC
))
1288 pll1
.parent
= &osc1
;
1290 genclk_init_parent(&gclk0
);
1291 genclk_init_parent(&gclk1
);
1292 genclk_init_parent(&gclk2
);
1293 genclk_init_parent(&gclk3
);
1294 genclk_init_parent(&gclk4
);
1295 genclk_init_parent(&atmel_lcdfb0_pixclk
);
1298 * Turn on all clocks that have at least one user already, and
1299 * turn off everything else. We only do this for module
1300 * clocks, and even though it isn't particularly pretty to
1301 * check the address of the mode function, it should do the
1304 for (i
= 0; i
< ARRAY_SIZE(at32_clock_list
); i
++) {
1305 struct clk
*clk
= at32_clock_list
[i
];
1307 if (clk
->users
== 0)
1310 if (clk
->mode
== &cpu_clk_mode
)
1311 cpu_mask
|= 1 << clk
->index
;
1312 else if (clk
->mode
== &hsb_clk_mode
)
1313 hsb_mask
|= 1 << clk
->index
;
1314 else if (clk
->mode
== &pba_clk_mode
)
1315 pba_mask
|= 1 << clk
->index
;
1316 else if (clk
->mode
== &pbb_clk_mode
)
1317 pbb_mask
|= 1 << clk
->index
;
1320 pm_writel(CPU_MASK
, cpu_mask
);
1321 pm_writel(HSB_MASK
, hsb_mask
);
1322 pm_writel(PBA_MASK
, pba_mask
);
1323 pm_writel(PBB_MASK
, pbb_mask
);