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/spi/spi.h>
16 #include <linux/usb/atmel_usba_udc.h>
18 #include <asm/atmel-mci.h>
22 #include <asm/arch/at32ap700x.h>
23 #include <asm/arch/board.h>
24 #include <asm/arch/portmux.h>
25 #include <asm/arch/sram.h>
27 #include <video/atmel_lcdc.h>
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 static struct clk osc0;
100 static struct clk osc1;
102 static unsigned long osc_get_rate(struct clk *clk)
104 return at32_board_osc_rates[clk->index];
107 static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
109 unsigned long div, mul, rate;
111 div = PM_BFEXT(PLLDIV, control) + 1;
112 mul = PM_BFEXT(PLLMUL, control) + 1;
114 rate = clk->parent->get_rate(clk->parent);
115 rate = (rate + div / 2) / div;
121 static long pll_set_rate(struct clk *clk, unsigned long rate,
125 unsigned long mul_best_fit = 0;
127 unsigned long div_min;
128 unsigned long div_max;
129 unsigned long div_best_fit = 0;
131 unsigned long pll_in;
132 unsigned long actual = 0;
133 unsigned long rate_error;
134 unsigned long rate_error_prev = ~0UL;
137 /* Rate must be between 80 MHz and 200 Mhz. */
138 if (rate < 80000000UL || rate > 200000000UL)
141 ctrl = PM_BF(PLLOPT, 4);
142 base = clk->parent->get_rate(clk->parent);
144 /* PLL input frequency must be between 6 MHz and 32 MHz. */
145 div_min = DIV_ROUND_UP(base, 32000000UL);
146 div_max = base / 6000000UL;
148 if (div_max < div_min)
151 for (div = div_min; div <= div_max; div++) {
152 pll_in = (base + div / 2) / div;
153 mul = (rate + pll_in / 2) / pll_in;
158 actual = pll_in * mul;
159 rate_error = abs(actual - rate);
161 if (rate_error < rate_error_prev) {
164 rate_error_prev = rate_error;
171 if (div_best_fit == 0)
174 ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
175 ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
176 ctrl |= PM_BF(PLLCOUNT, 16);
178 if (clk->parent == &osc1)
179 ctrl |= PM_BIT(PLLOSC);
186 static unsigned long pll0_get_rate(struct clk *clk)
190 control = pm_readl(PLL0);
192 return pll_get_rate(clk, control);
195 static void pll1_mode(struct clk *clk, int enabled)
197 unsigned long timeout;
201 ctrl = pm_readl(PLL1);
204 if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
205 pr_debug("clk %s: failed to enable, rate not set\n",
210 ctrl |= PM_BIT(PLLEN);
211 pm_writel(PLL1, ctrl);
213 /* Wait for PLL lock. */
214 for (timeout = 10000; timeout; timeout--) {
215 status = pm_readl(ISR);
216 if (status & PM_BIT(LOCK1))
221 if (!(status & PM_BIT(LOCK1)))
222 printk(KERN_ERR "clk %s: timeout waiting for lock\n",
225 ctrl &= ~PM_BIT(PLLEN);
226 pm_writel(PLL1, ctrl);
230 static unsigned long pll1_get_rate(struct clk *clk)
234 control = pm_readl(PLL1);
236 return pll_get_rate(clk, control);
239 static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
242 unsigned long actual_rate;
244 actual_rate = pll_set_rate(clk, rate, &ctrl);
247 if (actual_rate != rate)
251 pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
252 clk->name, rate, actual_rate);
253 pm_writel(PLL1, ctrl);
259 static int pll1_set_parent(struct clk *clk, struct clk *parent)
266 ctrl = pm_readl(PLL1);
267 WARN_ON(ctrl & PM_BIT(PLLEN));
270 ctrl &= ~PM_BIT(PLLOSC);
271 else if (parent == &osc1)
272 ctrl |= PM_BIT(PLLOSC);
276 pm_writel(PLL1, ctrl);
277 clk->parent = parent;
283 * The AT32AP7000 has five primary clock sources: One 32kHz
284 * oscillator, two crystal oscillators and two PLLs.
286 static struct clk osc32k = {
288 .get_rate = osc_get_rate,
292 static struct clk osc0 = {
294 .get_rate = osc_get_rate,
298 static struct clk osc1 = {
300 .get_rate = osc_get_rate,
303 static struct clk pll0 = {
305 .get_rate = pll0_get_rate,
308 static struct clk pll1 = {
311 .get_rate = pll1_get_rate,
312 .set_rate = pll1_set_rate,
313 .set_parent = pll1_set_parent,
318 * The main clock can be either osc0 or pll0. The boot loader may
319 * have chosen one for us, so we don't really know which one until we
320 * have a look at the SM.
322 static struct clk *main_clock;
325 * Synchronous clocks are generated from the main clock. The clocks
326 * must satisfy the constraint
327 * fCPU >= fHSB >= fPB
328 * i.e. each clock must not be faster than its parent.
330 static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
332 return main_clock->get_rate(main_clock) >> shift;
335 static void cpu_clk_mode(struct clk *clk, int enabled)
340 spin_lock_irqsave(&pm_lock, flags);
341 mask = pm_readl(CPU_MASK);
343 mask |= 1 << clk->index;
345 mask &= ~(1 << clk->index);
346 pm_writel(CPU_MASK, mask);
347 spin_unlock_irqrestore(&pm_lock, flags);
350 static unsigned long cpu_clk_get_rate(struct clk *clk)
352 unsigned long cksel, shift = 0;
354 cksel = pm_readl(CKSEL);
355 if (cksel & PM_BIT(CPUDIV))
356 shift = PM_BFEXT(CPUSEL, cksel) + 1;
358 return bus_clk_get_rate(clk, shift);
361 static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
364 unsigned long parent_rate, child_div, actual_rate, div;
366 parent_rate = clk->parent->get_rate(clk->parent);
367 control = pm_readl(CKSEL);
369 if (control & PM_BIT(HSBDIV))
370 child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
374 if (rate > 3 * (parent_rate / 4) || child_div == 1) {
375 actual_rate = parent_rate;
376 control &= ~PM_BIT(CPUDIV);
379 div = (parent_rate + rate / 2) / rate;
382 cpusel = (div > 1) ? (fls(div) - 2) : 0;
383 control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
384 actual_rate = parent_rate / (1 << (cpusel + 1));
387 pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
388 clk->name, rate, actual_rate);
391 pm_writel(CKSEL, control);
396 static void hsb_clk_mode(struct clk *clk, int enabled)
401 spin_lock_irqsave(&pm_lock, flags);
402 mask = pm_readl(HSB_MASK);
404 mask |= 1 << clk->index;
406 mask &= ~(1 << clk->index);
407 pm_writel(HSB_MASK, mask);
408 spin_unlock_irqrestore(&pm_lock, flags);
411 static unsigned long hsb_clk_get_rate(struct clk *clk)
413 unsigned long cksel, shift = 0;
415 cksel = pm_readl(CKSEL);
416 if (cksel & PM_BIT(HSBDIV))
417 shift = PM_BFEXT(HSBSEL, cksel) + 1;
419 return bus_clk_get_rate(clk, shift);
422 static void pba_clk_mode(struct clk *clk, int enabled)
427 spin_lock_irqsave(&pm_lock, flags);
428 mask = pm_readl(PBA_MASK);
430 mask |= 1 << clk->index;
432 mask &= ~(1 << clk->index);
433 pm_writel(PBA_MASK, mask);
434 spin_unlock_irqrestore(&pm_lock, flags);
437 static unsigned long pba_clk_get_rate(struct clk *clk)
439 unsigned long cksel, shift = 0;
441 cksel = pm_readl(CKSEL);
442 if (cksel & PM_BIT(PBADIV))
443 shift = PM_BFEXT(PBASEL, cksel) + 1;
445 return bus_clk_get_rate(clk, shift);
448 static void pbb_clk_mode(struct clk *clk, int enabled)
453 spin_lock_irqsave(&pm_lock, flags);
454 mask = pm_readl(PBB_MASK);
456 mask |= 1 << clk->index;
458 mask &= ~(1 << clk->index);
459 pm_writel(PBB_MASK, mask);
460 spin_unlock_irqrestore(&pm_lock, flags);
463 static unsigned long pbb_clk_get_rate(struct clk *clk)
465 unsigned long cksel, shift = 0;
467 cksel = pm_readl(CKSEL);
468 if (cksel & PM_BIT(PBBDIV))
469 shift = PM_BFEXT(PBBSEL, cksel) + 1;
471 return bus_clk_get_rate(clk, shift);
474 static struct clk cpu_clk = {
476 .get_rate = cpu_clk_get_rate,
477 .set_rate = cpu_clk_set_rate,
480 static struct clk hsb_clk = {
483 .get_rate = hsb_clk_get_rate,
485 static struct clk pba_clk = {
488 .mode = hsb_clk_mode,
489 .get_rate = pba_clk_get_rate,
492 static struct clk pbb_clk = {
495 .mode = hsb_clk_mode,
496 .get_rate = pbb_clk_get_rate,
501 /* --------------------------------------------------------------------
502 * Generic Clock operations
503 * -------------------------------------------------------------------- */
505 static void genclk_mode(struct clk *clk, int enabled)
509 control = pm_readl(GCCTRL(clk->index));
511 control |= PM_BIT(CEN);
513 control &= ~PM_BIT(CEN);
514 pm_writel(GCCTRL(clk->index), control);
517 static unsigned long genclk_get_rate(struct clk *clk)
520 unsigned long div = 1;
522 control = pm_readl(GCCTRL(clk->index));
523 if (control & PM_BIT(DIVEN))
524 div = 2 * (PM_BFEXT(DIV, control) + 1);
526 return clk->parent->get_rate(clk->parent) / div;
529 static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
532 unsigned long parent_rate, actual_rate, div;
534 parent_rate = clk->parent->get_rate(clk->parent);
535 control = pm_readl(GCCTRL(clk->index));
537 if (rate > 3 * parent_rate / 4) {
538 actual_rate = parent_rate;
539 control &= ~PM_BIT(DIVEN);
541 div = (parent_rate + rate) / (2 * rate) - 1;
542 control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
543 actual_rate = parent_rate / (2 * (div + 1));
546 dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
547 clk->name, rate, actual_rate);
550 pm_writel(GCCTRL(clk->index), control);
555 int genclk_set_parent(struct clk *clk, struct clk *parent)
559 dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
560 clk->name, parent->name, clk->parent->name);
562 control = pm_readl(GCCTRL(clk->index));
564 if (parent == &osc1 || parent == &pll1)
565 control |= PM_BIT(OSCSEL);
566 else if (parent == &osc0 || parent == &pll0)
567 control &= ~PM_BIT(OSCSEL);
571 if (parent == &pll0 || parent == &pll1)
572 control |= PM_BIT(PLLSEL);
574 control &= ~PM_BIT(PLLSEL);
576 pm_writel(GCCTRL(clk->index), control);
577 clk->parent = parent;
582 static void __init genclk_init_parent(struct clk *clk)
587 BUG_ON(clk->index > 7);
589 control = pm_readl(GCCTRL(clk->index));
590 if (control & PM_BIT(OSCSEL))
591 parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
593 parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
595 clk->parent = parent;
598 static struct dw_dma_platform_data dw_dmac0_data = {
602 static struct resource dw_dmac0_resource[] = {
606 DEFINE_DEV_DATA(dw_dmac, 0);
607 DEV_CLK(hclk, dw_dmac0, hsb, 10);
609 /* --------------------------------------------------------------------
611 * -------------------------------------------------------------------- */
612 static struct resource at32_pm0_resource[] = {
616 .flags = IORESOURCE_MEM,
621 static struct resource at32ap700x_rtc0_resource[] = {
625 .flags = IORESOURCE_MEM,
630 static struct resource at32_wdt0_resource[] = {
634 .flags = IORESOURCE_MEM,
638 static struct resource at32_eic0_resource[] = {
642 .flags = IORESOURCE_MEM,
647 DEFINE_DEV(at32_pm, 0);
648 DEFINE_DEV(at32ap700x_rtc, 0);
649 DEFINE_DEV(at32_wdt, 0);
650 DEFINE_DEV(at32_eic, 0);
653 * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
656 static struct clk at32_pm_pclk = {
658 .dev = &at32_pm0_device.dev,
660 .mode = pbb_clk_mode,
661 .get_rate = pbb_clk_get_rate,
666 static struct resource intc0_resource[] = {
669 struct platform_device at32_intc0_device = {
672 .resource = intc0_resource,
673 .num_resources = ARRAY_SIZE(intc0_resource),
675 DEV_CLK(pclk, at32_intc0, pbb, 1);
677 static struct clk ebi_clk = {
680 .mode = hsb_clk_mode,
681 .get_rate = hsb_clk_get_rate,
684 static struct clk hramc_clk = {
687 .mode = hsb_clk_mode,
688 .get_rate = hsb_clk_get_rate,
692 static struct clk sdramc_clk = {
693 .name = "sdramc_clk",
695 .mode = pbb_clk_mode,
696 .get_rate = pbb_clk_get_rate,
701 static struct resource smc0_resource[] = {
705 DEV_CLK(pclk, smc0, pbb, 13);
706 DEV_CLK(mck, smc0, hsb, 0);
708 static struct platform_device pdc_device = {
712 DEV_CLK(hclk, pdc, hsb, 4);
713 DEV_CLK(pclk, pdc, pba, 16);
715 static struct clk pico_clk = {
718 .mode = cpu_clk_mode,
719 .get_rate = cpu_clk_get_rate,
723 /* --------------------------------------------------------------------
725 * -------------------------------------------------------------------- */
727 static struct clk hmatrix_clk = {
728 .name = "hmatrix_clk",
730 .mode = pbb_clk_mode,
731 .get_rate = pbb_clk_get_rate,
735 #define HMATRIX_BASE ((void __iomem *)0xfff00800)
737 #define hmatrix_readl(reg) \
738 __raw_readl((HMATRIX_BASE) + HMATRIX_##reg)
739 #define hmatrix_writel(reg,value) \
740 __raw_writel((value), (HMATRIX_BASE) + HMATRIX_##reg)
743 * Set bits in the HMATRIX Special Function Register (SFR) used by the
744 * External Bus Interface (EBI). This can be used to enable special
745 * features like CompactFlash support, NAND Flash support, etc. on
746 * certain chipselects.
748 static inline void set_ebi_sfr_bits(u32 mask)
752 clk_enable(&hmatrix_clk);
753 sfr = hmatrix_readl(SFR4);
755 hmatrix_writel(SFR4, sfr);
756 clk_disable(&hmatrix_clk);
759 /* --------------------------------------------------------------------
761 * -------------------------------------------------------------------- */
763 static struct resource at32_tcb0_resource[] = {
767 static struct platform_device at32_tcb0_device = {
770 .resource = at32_tcb0_resource,
771 .num_resources = ARRAY_SIZE(at32_tcb0_resource),
773 DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
775 static struct resource at32_tcb1_resource[] = {
779 static struct platform_device at32_tcb1_device = {
782 .resource = at32_tcb1_resource,
783 .num_resources = ARRAY_SIZE(at32_tcb1_resource),
785 DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
787 /* --------------------------------------------------------------------
789 * -------------------------------------------------------------------- */
791 static struct resource pio0_resource[] = {
796 DEV_CLK(mck, pio0, pba, 10);
798 static struct resource pio1_resource[] = {
803 DEV_CLK(mck, pio1, pba, 11);
805 static struct resource pio2_resource[] = {
810 DEV_CLK(mck, pio2, pba, 12);
812 static struct resource pio3_resource[] = {
817 DEV_CLK(mck, pio3, pba, 13);
819 static struct resource pio4_resource[] = {
824 DEV_CLK(mck, pio4, pba, 14);
826 void __init at32_add_system_devices(void)
828 platform_device_register(&at32_pm0_device);
829 platform_device_register(&at32_intc0_device);
830 platform_device_register(&at32ap700x_rtc0_device);
831 platform_device_register(&at32_wdt0_device);
832 platform_device_register(&at32_eic0_device);
833 platform_device_register(&smc0_device);
834 platform_device_register(&pdc_device);
835 platform_device_register(&dw_dmac0_device);
837 platform_device_register(&at32_tcb0_device);
838 platform_device_register(&at32_tcb1_device);
840 platform_device_register(&pio0_device);
841 platform_device_register(&pio1_device);
842 platform_device_register(&pio2_device);
843 platform_device_register(&pio3_device);
844 platform_device_register(&pio4_device);
847 /* --------------------------------------------------------------------
849 * -------------------------------------------------------------------- */
850 static struct resource atmel_psif0_resource[] __initdata = {
854 .flags = IORESOURCE_MEM,
858 static struct clk atmel_psif0_pclk = {
861 .mode = pba_clk_mode,
862 .get_rate = pba_clk_get_rate,
866 static struct resource atmel_psif1_resource[] __initdata = {
870 .flags = IORESOURCE_MEM,
874 static struct clk atmel_psif1_pclk = {
877 .mode = pba_clk_mode,
878 .get_rate = pba_clk_get_rate,
882 struct platform_device *__init at32_add_device_psif(unsigned int id)
884 struct platform_device *pdev;
886 if (!(id == 0 || id == 1))
889 pdev = platform_device_alloc("atmel_psif", id);
895 if (platform_device_add_resources(pdev, atmel_psif0_resource,
896 ARRAY_SIZE(atmel_psif0_resource)))
897 goto err_add_resources;
898 atmel_psif0_pclk.dev = &pdev->dev;
899 select_peripheral(PA(8), PERIPH_A, 0); /* CLOCK */
900 select_peripheral(PA(9), PERIPH_A, 0); /* 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(PB(11), PERIPH_A, 0); /* CLOCK */
908 select_peripheral(PB(12), PERIPH_A, 0); /* DATA */
914 platform_device_add(pdev);
918 platform_device_put(pdev);
922 /* --------------------------------------------------------------------
924 * -------------------------------------------------------------------- */
926 static struct atmel_uart_data atmel_usart0_data = {
930 static struct resource atmel_usart0_resource[] = {
934 DEFINE_DEV_DATA(atmel_usart, 0);
935 DEV_CLK(usart, atmel_usart0, pba, 3);
937 static struct atmel_uart_data atmel_usart1_data = {
941 static struct resource atmel_usart1_resource[] = {
945 DEFINE_DEV_DATA(atmel_usart, 1);
946 DEV_CLK(usart, atmel_usart1, pba, 4);
948 static struct atmel_uart_data atmel_usart2_data = {
952 static struct resource atmel_usart2_resource[] = {
956 DEFINE_DEV_DATA(atmel_usart, 2);
957 DEV_CLK(usart, atmel_usart2, pba, 5);
959 static struct atmel_uart_data atmel_usart3_data = {
963 static struct resource atmel_usart3_resource[] = {
967 DEFINE_DEV_DATA(atmel_usart, 3);
968 DEV_CLK(usart, atmel_usart3, pba, 6);
970 static inline void configure_usart0_pins(void)
972 select_peripheral(PA(8), PERIPH_B, 0); /* RXD */
973 select_peripheral(PA(9), PERIPH_B, 0); /* TXD */
976 static inline void configure_usart1_pins(void)
978 select_peripheral(PA(17), PERIPH_A, 0); /* RXD */
979 select_peripheral(PA(18), PERIPH_A, 0); /* TXD */
982 static inline void configure_usart2_pins(void)
984 select_peripheral(PB(26), PERIPH_B, 0); /* RXD */
985 select_peripheral(PB(27), PERIPH_B, 0); /* TXD */
988 static inline void configure_usart3_pins(void)
990 select_peripheral(PB(18), PERIPH_B, 0); /* RXD */
991 select_peripheral(PB(17), PERIPH_B, 0); /* TXD */
994 static struct platform_device *__initdata at32_usarts[4];
996 void __init at32_map_usart(unsigned int hw_id, unsigned int line)
998 struct platform_device *pdev;
1002 pdev = &atmel_usart0_device;
1003 configure_usart0_pins();
1006 pdev = &atmel_usart1_device;
1007 configure_usart1_pins();
1010 pdev = &atmel_usart2_device;
1011 configure_usart2_pins();
1014 pdev = &atmel_usart3_device;
1015 configure_usart3_pins();
1021 if (PXSEG(pdev->resource[0].start) == P4SEG) {
1022 /* Addresses in the P4 segment are permanently mapped 1:1 */
1023 struct atmel_uart_data *data = pdev->dev.platform_data;
1024 data->regs = (void __iomem *)pdev->resource[0].start;
1028 at32_usarts[line] = pdev;
1031 struct platform_device *__init at32_add_device_usart(unsigned int id)
1033 platform_device_register(at32_usarts[id]);
1034 return at32_usarts[id];
1037 struct platform_device *atmel_default_console_device;
1039 void __init at32_setup_serial_console(unsigned int usart_id)
1041 atmel_default_console_device = at32_usarts[usart_id];
1044 /* --------------------------------------------------------------------
1046 * -------------------------------------------------------------------- */
1048 #ifdef CONFIG_CPU_AT32AP7000
1049 static struct eth_platform_data macb0_data;
1050 static struct resource macb0_resource[] = {
1054 DEFINE_DEV_DATA(macb, 0);
1055 DEV_CLK(hclk, macb0, hsb, 8);
1056 DEV_CLK(pclk, macb0, pbb, 6);
1058 static struct eth_platform_data macb1_data;
1059 static struct resource macb1_resource[] = {
1063 DEFINE_DEV_DATA(macb, 1);
1064 DEV_CLK(hclk, macb1, hsb, 9);
1065 DEV_CLK(pclk, macb1, pbb, 7);
1067 struct platform_device *__init
1068 at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
1070 struct platform_device *pdev;
1074 pdev = &macb0_device;
1076 select_peripheral(PC(3), PERIPH_A, 0); /* TXD0 */
1077 select_peripheral(PC(4), PERIPH_A, 0); /* TXD1 */
1078 select_peripheral(PC(7), PERIPH_A, 0); /* TXEN */
1079 select_peripheral(PC(8), PERIPH_A, 0); /* TXCK */
1080 select_peripheral(PC(9), PERIPH_A, 0); /* RXD0 */
1081 select_peripheral(PC(10), PERIPH_A, 0); /* RXD1 */
1082 select_peripheral(PC(13), PERIPH_A, 0); /* RXER */
1083 select_peripheral(PC(15), PERIPH_A, 0); /* RXDV */
1084 select_peripheral(PC(16), PERIPH_A, 0); /* MDC */
1085 select_peripheral(PC(17), PERIPH_A, 0); /* MDIO */
1087 if (!data->is_rmii) {
1088 select_peripheral(PC(0), PERIPH_A, 0); /* COL */
1089 select_peripheral(PC(1), PERIPH_A, 0); /* CRS */
1090 select_peripheral(PC(2), PERIPH_A, 0); /* TXER */
1091 select_peripheral(PC(5), PERIPH_A, 0); /* TXD2 */
1092 select_peripheral(PC(6), PERIPH_A, 0); /* TXD3 */
1093 select_peripheral(PC(11), PERIPH_A, 0); /* RXD2 */
1094 select_peripheral(PC(12), PERIPH_A, 0); /* RXD3 */
1095 select_peripheral(PC(14), PERIPH_A, 0); /* RXCK */
1096 select_peripheral(PC(18), PERIPH_A, 0); /* SPD */
1101 pdev = &macb1_device;
1103 select_peripheral(PD(13), PERIPH_B, 0); /* TXD0 */
1104 select_peripheral(PD(14), PERIPH_B, 0); /* TXD1 */
1105 select_peripheral(PD(11), PERIPH_B, 0); /* TXEN */
1106 select_peripheral(PD(12), PERIPH_B, 0); /* TXCK */
1107 select_peripheral(PD(10), PERIPH_B, 0); /* RXD0 */
1108 select_peripheral(PD(6), PERIPH_B, 0); /* RXD1 */
1109 select_peripheral(PD(5), PERIPH_B, 0); /* RXER */
1110 select_peripheral(PD(4), PERIPH_B, 0); /* RXDV */
1111 select_peripheral(PD(3), PERIPH_B, 0); /* MDC */
1112 select_peripheral(PD(2), PERIPH_B, 0); /* MDIO */
1114 if (!data->is_rmii) {
1115 select_peripheral(PC(19), PERIPH_B, 0); /* COL */
1116 select_peripheral(PC(23), PERIPH_B, 0); /* CRS */
1117 select_peripheral(PC(26), PERIPH_B, 0); /* TXER */
1118 select_peripheral(PC(27), PERIPH_B, 0); /* TXD2 */
1119 select_peripheral(PC(28), PERIPH_B, 0); /* TXD3 */
1120 select_peripheral(PC(29), PERIPH_B, 0); /* RXD2 */
1121 select_peripheral(PC(30), PERIPH_B, 0); /* RXD3 */
1122 select_peripheral(PC(24), PERIPH_B, 0); /* RXCK */
1123 select_peripheral(PD(15), PERIPH_B, 0); /* SPD */
1131 memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
1132 platform_device_register(pdev);
1138 /* --------------------------------------------------------------------
1140 * -------------------------------------------------------------------- */
1141 static struct resource atmel_spi0_resource[] = {
1145 DEFINE_DEV(atmel_spi, 0);
1146 DEV_CLK(spi_clk, atmel_spi0, pba, 0);
1148 static struct resource atmel_spi1_resource[] = {
1152 DEFINE_DEV(atmel_spi, 1);
1153 DEV_CLK(spi_clk, atmel_spi1, pba, 1);
1156 at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
1157 unsigned int n, const u8 *pins)
1159 unsigned int pin, mode;
1161 for (; n; n--, b++) {
1162 b->bus_num = bus_num;
1163 if (b->chip_select >= 4)
1165 pin = (unsigned)b->controller_data;
1167 pin = pins[b->chip_select];
1168 b->controller_data = (void *)pin;
1170 mode = AT32_GPIOF_OUTPUT;
1171 if (!(b->mode & SPI_CS_HIGH))
1172 mode |= AT32_GPIOF_HIGH;
1173 at32_select_gpio(pin, mode);
1177 struct platform_device *__init
1178 at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
1181 * Manage the chipselects as GPIOs, normally using the same pins
1182 * the SPI controller expects; but boards can use other pins.
1184 static u8 __initdata spi0_pins[] =
1185 { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
1186 GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
1187 static u8 __initdata spi1_pins[] =
1188 { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
1189 GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
1190 struct platform_device *pdev;
1194 pdev = &atmel_spi0_device;
1195 /* pullup MISO so a level is always defined */
1196 select_peripheral(PA(0), PERIPH_A, AT32_GPIOF_PULLUP);
1197 select_peripheral(PA(1), PERIPH_A, 0); /* MOSI */
1198 select_peripheral(PA(2), PERIPH_A, 0); /* SCK */
1199 at32_spi_setup_slaves(0, b, n, spi0_pins);
1203 pdev = &atmel_spi1_device;
1204 /* pullup MISO so a level is always defined */
1205 select_peripheral(PB(0), PERIPH_B, AT32_GPIOF_PULLUP);
1206 select_peripheral(PB(1), PERIPH_B, 0); /* MOSI */
1207 select_peripheral(PB(5), PERIPH_B, 0); /* SCK */
1208 at32_spi_setup_slaves(1, b, n, spi1_pins);
1215 spi_register_board_info(b, n);
1216 platform_device_register(pdev);
1220 /* --------------------------------------------------------------------
1222 * -------------------------------------------------------------------- */
1223 static struct resource atmel_twi0_resource[] __initdata = {
1227 static struct clk atmel_twi0_pclk = {
1230 .mode = pba_clk_mode,
1231 .get_rate = pba_clk_get_rate,
1235 struct platform_device *__init at32_add_device_twi(unsigned int id,
1236 struct i2c_board_info *b,
1239 struct platform_device *pdev;
1244 pdev = platform_device_alloc("atmel_twi", id);
1248 if (platform_device_add_resources(pdev, atmel_twi0_resource,
1249 ARRAY_SIZE(atmel_twi0_resource)))
1250 goto err_add_resources;
1252 select_peripheral(PA(6), PERIPH_A, 0); /* SDA */
1253 select_peripheral(PA(7), PERIPH_A, 0); /* SDL */
1255 atmel_twi0_pclk.dev = &pdev->dev;
1258 i2c_register_board_info(id, b, n);
1260 platform_device_add(pdev);
1264 platform_device_put(pdev);
1268 /* --------------------------------------------------------------------
1270 * -------------------------------------------------------------------- */
1271 static struct resource atmel_mci0_resource[] __initdata = {
1275 static struct clk atmel_mci0_pclk = {
1278 .mode = pbb_clk_mode,
1279 .get_rate = pbb_clk_get_rate,
1283 struct platform_device *__init
1284 at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
1286 struct mci_platform_data _data;
1287 struct platform_device *pdev;
1288 struct dw_dma_slave *dws;
1293 pdev = platform_device_alloc("atmel_mci", id);
1297 if (platform_device_add_resources(pdev, atmel_mci0_resource,
1298 ARRAY_SIZE(atmel_mci0_resource)))
1303 memset(data, 0, sizeof(struct mci_platform_data));
1306 if (platform_device_add_data(pdev, data,
1307 sizeof(struct mci_platform_data)))
1310 select_peripheral(PA(10), PERIPH_A, 0); /* CLK */
1311 select_peripheral(PA(11), PERIPH_A, 0); /* CMD */
1312 select_peripheral(PA(12), PERIPH_A, 0); /* DATA0 */
1313 select_peripheral(PA(13), PERIPH_A, 0); /* DATA1 */
1314 select_peripheral(PA(14), PERIPH_A, 0); /* DATA2 */
1315 select_peripheral(PA(15), PERIPH_A, 0); /* DATA3 */
1318 if (data->detect_pin != GPIO_PIN_NONE)
1319 at32_select_gpio(data->detect_pin, 0);
1320 if (data->wp_pin != GPIO_PIN_NONE)
1321 at32_select_gpio(data->wp_pin, 0);
1324 atmel_mci0_pclk.dev = &pdev->dev;
1326 platform_device_add(pdev);
1330 platform_device_put(pdev);
1334 /* --------------------------------------------------------------------
1336 * -------------------------------------------------------------------- */
1337 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
1338 static struct atmel_lcdfb_info atmel_lcdfb0_data;
1339 static struct resource atmel_lcdfb0_resource[] = {
1341 .start = 0xff000000,
1343 .flags = IORESOURCE_MEM,
1347 /* Placeholder for pre-allocated fb memory */
1348 .start = 0x00000000,
1353 DEFINE_DEV_DATA(atmel_lcdfb, 0);
1354 DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
1355 static struct clk atmel_lcdfb0_pixclk = {
1357 .dev = &atmel_lcdfb0_device.dev,
1358 .mode = genclk_mode,
1359 .get_rate = genclk_get_rate,
1360 .set_rate = genclk_set_rate,
1361 .set_parent = genclk_set_parent,
1365 struct platform_device *__init
1366 at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
1367 unsigned long fbmem_start, unsigned long fbmem_len,
1368 unsigned int pin_config)
1370 struct platform_device *pdev;
1371 struct atmel_lcdfb_info *info;
1372 struct fb_monspecs *monspecs;
1373 struct fb_videomode *modedb;
1374 unsigned int modedb_size;
1377 * Do a deep copy of the fb data, monspecs and modedb. Make
1378 * sure all allocations are done before setting up the
1381 monspecs = kmemdup(data->default_monspecs,
1382 sizeof(struct fb_monspecs), GFP_KERNEL);
1386 modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
1387 modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
1389 goto err_dup_modedb;
1390 monspecs->modedb = modedb;
1394 pdev = &atmel_lcdfb0_device;
1396 switch (pin_config) {
1398 select_peripheral(PC(19), PERIPH_A, 0); /* CC */
1399 select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
1400 select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
1401 select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
1402 select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
1403 select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
1404 select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
1405 select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
1406 select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
1407 select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
1408 select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
1409 select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
1410 select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
1411 select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
1412 select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
1413 select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
1414 select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
1415 select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
1416 select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
1417 select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
1418 select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
1419 select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
1420 select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
1421 select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
1422 select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
1423 select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
1424 select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
1425 select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
1426 select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
1427 select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
1428 select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
1431 select_peripheral(PE(0), PERIPH_B, 0); /* CC */
1432 select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
1433 select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
1434 select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
1435 select_peripheral(PE(1), PERIPH_B, 0); /* DVAL */
1436 select_peripheral(PE(2), PERIPH_B, 0); /* MODE */
1437 select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
1438 select_peripheral(PE(3), PERIPH_B, 0); /* DATA0 */
1439 select_peripheral(PE(4), PERIPH_B, 0); /* DATA1 */
1440 select_peripheral(PE(5), PERIPH_B, 0); /* DATA2 */
1441 select_peripheral(PE(6), PERIPH_B, 0); /* DATA3 */
1442 select_peripheral(PE(7), PERIPH_B, 0); /* DATA4 */
1443 select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
1444 select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
1445 select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
1446 select_peripheral(PE(8), PERIPH_B, 0); /* DATA8 */
1447 select_peripheral(PE(9), PERIPH_B, 0); /* DATA9 */
1448 select_peripheral(PE(10), PERIPH_B, 0); /* DATA10 */
1449 select_peripheral(PE(11), PERIPH_B, 0); /* DATA11 */
1450 select_peripheral(PE(12), PERIPH_B, 0); /* DATA12 */
1451 select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
1452 select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
1453 select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
1454 select_peripheral(PE(13), PERIPH_B, 0); /* DATA16 */
1455 select_peripheral(PE(14), PERIPH_B, 0); /* DATA17 */
1456 select_peripheral(PE(15), PERIPH_B, 0); /* DATA18 */
1457 select_peripheral(PE(16), PERIPH_B, 0); /* DATA19 */
1458 select_peripheral(PE(17), PERIPH_B, 0); /* DATA20 */
1459 select_peripheral(PE(18), PERIPH_B, 0); /* DATA21 */
1460 select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
1461 select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
1464 goto err_invalid_id;
1467 clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
1468 clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
1472 goto err_invalid_id;
1476 pdev->resource[2].start = fbmem_start;
1477 pdev->resource[2].end = fbmem_start + fbmem_len - 1;
1478 pdev->resource[2].flags = IORESOURCE_MEM;
1481 info = pdev->dev.platform_data;
1482 memcpy(info, data, sizeof(struct atmel_lcdfb_info));
1483 info->default_monspecs = monspecs;
1485 platform_device_register(pdev);
1496 /* --------------------------------------------------------------------
1498 * -------------------------------------------------------------------- */
1499 static struct resource atmel_pwm0_resource[] __initdata = {
1503 static struct clk atmel_pwm0_mck = {
1506 .mode = pbb_clk_mode,
1507 .get_rate = pbb_clk_get_rate,
1511 struct platform_device *__init at32_add_device_pwm(u32 mask)
1513 struct platform_device *pdev;
1518 pdev = platform_device_alloc("atmel_pwm", 0);
1522 if (platform_device_add_resources(pdev, atmel_pwm0_resource,
1523 ARRAY_SIZE(atmel_pwm0_resource)))
1526 if (platform_device_add_data(pdev, &mask, sizeof(mask)))
1529 if (mask & (1 << 0))
1530 select_peripheral(PA(28), PERIPH_A, 0);
1531 if (mask & (1 << 1))
1532 select_peripheral(PA(29), PERIPH_A, 0);
1533 if (mask & (1 << 2))
1534 select_peripheral(PA(21), PERIPH_B, 0);
1535 if (mask & (1 << 3))
1536 select_peripheral(PA(22), PERIPH_B, 0);
1538 atmel_pwm0_mck.dev = &pdev->dev;
1540 platform_device_add(pdev);
1545 platform_device_put(pdev);
1549 /* --------------------------------------------------------------------
1551 * -------------------------------------------------------------------- */
1552 static struct resource ssc0_resource[] = {
1557 DEV_CLK(pclk, ssc0, pba, 7);
1559 static struct resource ssc1_resource[] = {
1564 DEV_CLK(pclk, ssc1, pba, 8);
1566 static struct resource ssc2_resource[] = {
1571 DEV_CLK(pclk, ssc2, pba, 9);
1573 struct platform_device *__init
1574 at32_add_device_ssc(unsigned int id, unsigned int flags)
1576 struct platform_device *pdev;
1580 pdev = &ssc0_device;
1581 if (flags & ATMEL_SSC_RF)
1582 select_peripheral(PA(21), PERIPH_A, 0); /* RF */
1583 if (flags & ATMEL_SSC_RK)
1584 select_peripheral(PA(22), PERIPH_A, 0); /* RK */
1585 if (flags & ATMEL_SSC_TK)
1586 select_peripheral(PA(23), PERIPH_A, 0); /* TK */
1587 if (flags & ATMEL_SSC_TF)
1588 select_peripheral(PA(24), PERIPH_A, 0); /* TF */
1589 if (flags & ATMEL_SSC_TD)
1590 select_peripheral(PA(25), PERIPH_A, 0); /* TD */
1591 if (flags & ATMEL_SSC_RD)
1592 select_peripheral(PA(26), PERIPH_A, 0); /* RD */
1595 pdev = &ssc1_device;
1596 if (flags & ATMEL_SSC_RF)
1597 select_peripheral(PA(0), PERIPH_B, 0); /* RF */
1598 if (flags & ATMEL_SSC_RK)
1599 select_peripheral(PA(1), PERIPH_B, 0); /* RK */
1600 if (flags & ATMEL_SSC_TK)
1601 select_peripheral(PA(2), PERIPH_B, 0); /* TK */
1602 if (flags & ATMEL_SSC_TF)
1603 select_peripheral(PA(3), PERIPH_B, 0); /* TF */
1604 if (flags & ATMEL_SSC_TD)
1605 select_peripheral(PA(4), PERIPH_B, 0); /* TD */
1606 if (flags & ATMEL_SSC_RD)
1607 select_peripheral(PA(5), PERIPH_B, 0); /* RD */
1610 pdev = &ssc2_device;
1611 if (flags & ATMEL_SSC_TD)
1612 select_peripheral(PB(13), PERIPH_A, 0); /* TD */
1613 if (flags & ATMEL_SSC_RD)
1614 select_peripheral(PB(14), PERIPH_A, 0); /* RD */
1615 if (flags & ATMEL_SSC_TK)
1616 select_peripheral(PB(15), PERIPH_A, 0); /* TK */
1617 if (flags & ATMEL_SSC_TF)
1618 select_peripheral(PB(16), PERIPH_A, 0); /* TF */
1619 if (flags & ATMEL_SSC_RF)
1620 select_peripheral(PB(17), PERIPH_A, 0); /* RF */
1621 if (flags & ATMEL_SSC_RK)
1622 select_peripheral(PB(18), PERIPH_A, 0); /* RK */
1628 platform_device_register(pdev);
1632 /* --------------------------------------------------------------------
1633 * USB Device Controller
1634 * -------------------------------------------------------------------- */
1635 static struct resource usba0_resource[] __initdata = {
1637 .start = 0xff300000,
1639 .flags = IORESOURCE_MEM,
1641 .start = 0xfff03000,
1643 .flags = IORESOURCE_MEM,
1647 static struct clk usba0_pclk = {
1650 .mode = pbb_clk_mode,
1651 .get_rate = pbb_clk_get_rate,
1654 static struct clk usba0_hclk = {
1657 .mode = hsb_clk_mode,
1658 .get_rate = hsb_clk_get_rate,
1662 #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
1666 .fifo_size = maxpkt, \
1667 .nr_banks = maxbk, \
1672 static struct usba_ep_data at32_usba_ep[] __initdata = {
1673 EP("ep0", 0, 64, 1, 0, 0),
1674 EP("ep1", 1, 512, 2, 1, 1),
1675 EP("ep2", 2, 512, 2, 1, 1),
1676 EP("ep3-int", 3, 64, 3, 1, 0),
1677 EP("ep4-int", 4, 64, 3, 1, 0),
1678 EP("ep5", 5, 1024, 3, 1, 1),
1679 EP("ep6", 6, 1024, 3, 1, 1),
1684 struct platform_device *__init
1685 at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
1688 * pdata doesn't have room for any endpoints, so we need to
1689 * append room for the ones we need right after it.
1692 struct usba_platform_data pdata;
1693 struct usba_ep_data ep[7];
1695 struct platform_device *pdev;
1700 pdev = platform_device_alloc("atmel_usba_udc", 0);
1704 if (platform_device_add_resources(pdev, usba0_resource,
1705 ARRAY_SIZE(usba0_resource)))
1709 usba_data.pdata.vbus_pin = data->vbus_pin;
1711 usba_data.pdata.vbus_pin = -EINVAL;
1713 data = &usba_data.pdata;
1714 data->num_ep = ARRAY_SIZE(at32_usba_ep);
1715 memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
1717 if (platform_device_add_data(pdev, data, sizeof(usba_data)))
1720 if (data->vbus_pin >= 0)
1721 at32_select_gpio(data->vbus_pin, 0);
1723 usba0_pclk.dev = &pdev->dev;
1724 usba0_hclk.dev = &pdev->dev;
1726 platform_device_add(pdev);
1731 platform_device_put(pdev);
1735 /* --------------------------------------------------------------------
1736 * IDE / CompactFlash
1737 * -------------------------------------------------------------------- */
1738 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
1739 static struct resource at32_smc_cs4_resource[] __initdata = {
1741 .start = 0x04000000,
1743 .flags = IORESOURCE_MEM,
1745 IRQ(~0UL), /* Magic IRQ will be overridden */
1747 static struct resource at32_smc_cs5_resource[] __initdata = {
1749 .start = 0x20000000,
1751 .flags = IORESOURCE_MEM,
1753 IRQ(~0UL), /* Magic IRQ will be overridden */
1756 static int __init at32_init_ide_or_cf(struct platform_device *pdev,
1757 unsigned int cs, unsigned int extint)
1759 static unsigned int extint_pin_map[4] __initdata = {
1765 static bool common_pins_initialized __initdata = false;
1766 unsigned int extint_pin;
1769 if (extint >= ARRAY_SIZE(extint_pin_map))
1771 extint_pin = extint_pin_map[extint];
1775 ret = platform_device_add_resources(pdev,
1776 at32_smc_cs4_resource,
1777 ARRAY_SIZE(at32_smc_cs4_resource));
1781 select_peripheral(PE(21), PERIPH_A, 0); /* NCS4 -> OE_N */
1782 set_ebi_sfr_bits(HMATRIX_BIT(CS4A));
1785 ret = platform_device_add_resources(pdev,
1786 at32_smc_cs5_resource,
1787 ARRAY_SIZE(at32_smc_cs5_resource));
1791 select_peripheral(PE(22), PERIPH_A, 0); /* NCS5 -> OE_N */
1792 set_ebi_sfr_bits(HMATRIX_BIT(CS5A));
1798 if (!common_pins_initialized) {
1799 select_peripheral(PE(19), PERIPH_A, 0); /* CFCE1 -> CS0_N */
1800 select_peripheral(PE(20), PERIPH_A, 0); /* CFCE2 -> CS1_N */
1801 select_peripheral(PE(23), PERIPH_A, 0); /* CFRNW -> DIR */
1802 select_peripheral(PE(24), PERIPH_A, 0); /* NWAIT <- IORDY */
1803 common_pins_initialized = true;
1806 at32_select_periph(extint_pin, GPIO_PERIPH_A, AT32_GPIOF_DEGLITCH);
1808 pdev->resource[1].start = EIM_IRQ_BASE + extint;
1809 pdev->resource[1].end = pdev->resource[1].start;
1814 struct platform_device *__init
1815 at32_add_device_ide(unsigned int id, unsigned int extint,
1816 struct ide_platform_data *data)
1818 struct platform_device *pdev;
1820 pdev = platform_device_alloc("at32_ide", id);
1824 if (platform_device_add_data(pdev, data,
1825 sizeof(struct ide_platform_data)))
1828 if (at32_init_ide_or_cf(pdev, data->cs, extint))
1831 platform_device_add(pdev);
1835 platform_device_put(pdev);
1839 struct platform_device *__init
1840 at32_add_device_cf(unsigned int id, unsigned int extint,
1841 struct cf_platform_data *data)
1843 struct platform_device *pdev;
1845 pdev = platform_device_alloc("at32_cf", id);
1849 if (platform_device_add_data(pdev, data,
1850 sizeof(struct cf_platform_data)))
1853 if (at32_init_ide_or_cf(pdev, data->cs, extint))
1856 if (data->detect_pin != GPIO_PIN_NONE)
1857 at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
1858 if (data->reset_pin != GPIO_PIN_NONE)
1859 at32_select_gpio(data->reset_pin, 0);
1860 if (data->vcc_pin != GPIO_PIN_NONE)
1861 at32_select_gpio(data->vcc_pin, 0);
1862 /* READY is used as extint, so we can't select it as gpio */
1864 platform_device_add(pdev);
1868 platform_device_put(pdev);
1873 /* --------------------------------------------------------------------
1874 * NAND Flash / SmartMedia
1875 * -------------------------------------------------------------------- */
1876 static struct resource smc_cs3_resource[] __initdata = {
1878 .start = 0x0c000000,
1880 .flags = IORESOURCE_MEM,
1882 .start = 0xfff03c00,
1884 .flags = IORESOURCE_MEM,
1888 struct platform_device *__init
1889 at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
1891 struct platform_device *pdev;
1893 if (id != 0 || !data)
1896 pdev = platform_device_alloc("atmel_nand", id);
1900 if (platform_device_add_resources(pdev, smc_cs3_resource,
1901 ARRAY_SIZE(smc_cs3_resource)))
1904 if (platform_device_add_data(pdev, data,
1905 sizeof(struct atmel_nand_data)))
1908 set_ebi_sfr_bits(HMATRIX_BIT(CS3A));
1909 if (data->enable_pin)
1910 at32_select_gpio(data->enable_pin,
1911 AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
1913 at32_select_gpio(data->rdy_pin, 0);
1915 at32_select_gpio(data->det_pin, 0);
1917 platform_device_add(pdev);
1921 platform_device_put(pdev);
1925 /* --------------------------------------------------------------------
1927 * -------------------------------------------------------------------- */
1928 static struct resource atmel_ac97c0_resource[] __initdata = {
1932 static struct clk atmel_ac97c0_pclk = {
1935 .mode = pbb_clk_mode,
1936 .get_rate = pbb_clk_get_rate,
1940 struct platform_device *__init at32_add_device_ac97c(unsigned int id)
1942 struct platform_device *pdev;
1947 pdev = platform_device_alloc("atmel_ac97c", id);
1951 if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
1952 ARRAY_SIZE(atmel_ac97c0_resource)))
1953 goto err_add_resources;
1955 select_peripheral(PB(20), PERIPH_B, 0); /* SYNC */
1956 select_peripheral(PB(21), PERIPH_B, 0); /* SDO */
1957 select_peripheral(PB(22), PERIPH_B, 0); /* SDI */
1958 select_peripheral(PB(23), PERIPH_B, 0); /* SCLK */
1960 atmel_ac97c0_pclk.dev = &pdev->dev;
1962 platform_device_add(pdev);
1966 platform_device_put(pdev);
1970 /* --------------------------------------------------------------------
1972 * -------------------------------------------------------------------- */
1973 static struct resource abdac0_resource[] __initdata = {
1977 static struct clk abdac0_pclk = {
1980 .mode = pbb_clk_mode,
1981 .get_rate = pbb_clk_get_rate,
1984 static struct clk abdac0_sample_clk = {
1985 .name = "sample_clk",
1986 .mode = genclk_mode,
1987 .get_rate = genclk_get_rate,
1988 .set_rate = genclk_set_rate,
1989 .set_parent = genclk_set_parent,
1993 struct platform_device *__init at32_add_device_abdac(unsigned int id)
1995 struct platform_device *pdev;
2000 pdev = platform_device_alloc("abdac", id);
2004 if (platform_device_add_resources(pdev, abdac0_resource,
2005 ARRAY_SIZE(abdac0_resource)))
2006 goto err_add_resources;
2008 select_peripheral(PB(20), PERIPH_A, 0); /* DATA1 */
2009 select_peripheral(PB(21), PERIPH_A, 0); /* DATA0 */
2010 select_peripheral(PB(22), PERIPH_A, 0); /* DATAN1 */
2011 select_peripheral(PB(23), PERIPH_A, 0); /* DATAN0 */
2013 abdac0_pclk.dev = &pdev->dev;
2014 abdac0_sample_clk.dev = &pdev->dev;
2016 platform_device_add(pdev);
2020 platform_device_put(pdev);
2024 /* --------------------------------------------------------------------
2026 * -------------------------------------------------------------------- */
2027 static struct clk gclk0 = {
2029 .mode = genclk_mode,
2030 .get_rate = genclk_get_rate,
2031 .set_rate = genclk_set_rate,
2032 .set_parent = genclk_set_parent,
2035 static struct clk gclk1 = {
2037 .mode = genclk_mode,
2038 .get_rate = genclk_get_rate,
2039 .set_rate = genclk_set_rate,
2040 .set_parent = genclk_set_parent,
2043 static struct clk gclk2 = {
2045 .mode = genclk_mode,
2046 .get_rate = genclk_get_rate,
2047 .set_rate = genclk_set_rate,
2048 .set_parent = genclk_set_parent,
2051 static struct clk gclk3 = {
2053 .mode = genclk_mode,
2054 .get_rate = genclk_get_rate,
2055 .set_rate = genclk_set_rate,
2056 .set_parent = genclk_set_parent,
2059 static struct clk gclk4 = {
2061 .mode = genclk_mode,
2062 .get_rate = genclk_get_rate,
2063 .set_rate = genclk_set_rate,
2064 .set_parent = genclk_set_parent,
2068 struct clk *at32_clock_list[] = {
2099 &atmel_usart0_usart,
2100 &atmel_usart1_usart,
2101 &atmel_usart2_usart,
2102 &atmel_usart3_usart,
2104 #if defined(CONFIG_CPU_AT32AP7000)
2110 &atmel_spi0_spi_clk,
2111 &atmel_spi1_spi_clk,
2114 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
2116 &atmel_lcdfb0_pixclk,
2132 unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
2134 void __init setup_platform(void)
2136 u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
2139 if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
2141 cpu_clk.parent = &pll0;
2144 cpu_clk.parent = &osc0;
2147 if (pm_readl(PLL0) & PM_BIT(PLLOSC))
2148 pll0.parent = &osc1;
2149 if (pm_readl(PLL1) & PM_BIT(PLLOSC))
2150 pll1.parent = &osc1;
2152 genclk_init_parent(&gclk0);
2153 genclk_init_parent(&gclk1);
2154 genclk_init_parent(&gclk2);
2155 genclk_init_parent(&gclk3);
2156 genclk_init_parent(&gclk4);
2157 #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
2158 genclk_init_parent(&atmel_lcdfb0_pixclk);
2160 genclk_init_parent(&abdac0_sample_clk);
2163 * Turn on all clocks that have at least one user already, and
2164 * turn off everything else. We only do this for module
2165 * clocks, and even though it isn't particularly pretty to
2166 * check the address of the mode function, it should do the
2169 for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
2170 struct clk *clk = at32_clock_list[i];
2172 if (clk->users == 0)
2175 if (clk->mode == &cpu_clk_mode)
2176 cpu_mask |= 1 << clk->index;
2177 else if (clk->mode == &hsb_clk_mode)
2178 hsb_mask |= 1 << clk->index;
2179 else if (clk->mode == &pba_clk_mode)
2180 pba_mask |= 1 << clk->index;
2181 else if (clk->mode == &pbb_clk_mode)
2182 pbb_mask |= 1 << clk->index;
2185 pm_writel(CPU_MASK, cpu_mask);
2186 pm_writel(HSB_MASK, hsb_mask);
2187 pm_writel(PBA_MASK, pba_mask);
2188 pm_writel(PBB_MASK, pbb_mask);
2190 /* Initialize the port muxes */
2191 at32_init_pio(&pio0_device);
2192 at32_init_pio(&pio1_device);
2193 at32_init_pio(&pio2_device);
2194 at32_init_pio(&pio3_device);
2195 at32_init_pio(&pio4_device);
2198 struct gen_pool *sram_pool;
2200 static int __init sram_init(void)
2202 struct gen_pool *pool;
2204 /* 1KiB granularity */
2205 pool = gen_pool_create(10, -1);
2209 if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
2216 gen_pool_destroy(pool);
2218 pr_err("Failed to create SRAM pool\n");
2221 core_initcall(sram_init);