iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[deliverable/linux.git] / arch / avr32 / mach-at32ap / at32ap700x.c
CommitLineData
5f97f7f9
HS
1/*
2 * Copyright (C) 2005-2006 Atmel Corporation
3 *
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.
7 */
8#include <linux/clk.h>
35bf50cc 9#include <linux/delay.h>
3bfb1d20 10#include <linux/dw_dmac.h>
d0a2b7af 11#include <linux/fb.h>
5f97f7f9
HS
12#include <linux/init.h>
13#include <linux/platform_device.h>
6b84bbfc 14#include <linux/dma-mapping.h>
3c26e170 15#include <linux/gpio.h>
41d8ca45 16#include <linux/spi/spi.h>
8d855317 17#include <linux/usb/atmel_usba_udc.h>
2635d1ba
NF
18
19#include <mach/atmel-mci.h>
c42aa775 20#include <linux/atmel-mci.h>
5f97f7f9
HS
21
22#include <asm/io.h>
e7ba176b 23#include <asm/irq.h>
5f97f7f9 24
3663b736
HS
25#include <mach/at32ap700x.h>
26#include <mach/board.h>
b47eb409 27#include <mach/hmatrix.h>
3663b736
HS
28#include <mach/portmux.h>
29#include <mach/sram.h>
5f97f7f9 30
6b0c9351 31#include <sound/atmel-abdac.h>
2f47c8c5 32#include <sound/atmel-ac97c.h>
6b0c9351 33
d0a2b7af
HS
34#include <video/atmel_lcdc.h>
35
5f97f7f9
HS
36#include "clock.h"
37#include "pio.h"
7a5b8059
HS
38#include "pm.h"
39
5f97f7f9
HS
40
41#define PBMEM(base) \
42 { \
43 .start = base, \
44 .end = base + 0x3ff, \
45 .flags = IORESOURCE_MEM, \
46 }
47#define IRQ(num) \
48 { \
49 .start = num, \
50 .end = num, \
51 .flags = IORESOURCE_IRQ, \
52 }
53#define NAMED_IRQ(num, _name) \
54 { \
55 .start = num, \
56 .end = num, \
57 .name = _name, \
58 .flags = IORESOURCE_IRQ, \
59 }
60
6b84bbfc
DB
61/* REVISIT these assume *every* device supports DMA, but several
62 * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
63 */
5f97f7f9 64#define DEFINE_DEV(_name, _id) \
284901a9 65static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
5f97f7f9
HS
66static struct platform_device _name##_id##_device = { \
67 .name = #_name, \
68 .id = _id, \
6b84bbfc
DB
69 .dev = { \
70 .dma_mask = &_name##_id##_dma_mask, \
284901a9 71 .coherent_dma_mask = DMA_BIT_MASK(32), \
6b84bbfc 72 }, \
5f97f7f9
HS
73 .resource = _name##_id##_resource, \
74 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
75}
76#define DEFINE_DEV_DATA(_name, _id) \
284901a9 77static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
5f97f7f9
HS
78static struct platform_device _name##_id##_device = { \
79 .name = #_name, \
80 .id = _id, \
81 .dev = { \
6b84bbfc 82 .dma_mask = &_name##_id##_dma_mask, \
5f97f7f9 83 .platform_data = &_name##_id##_data, \
284901a9 84 .coherent_dma_mask = DMA_BIT_MASK(32), \
5f97f7f9
HS
85 }, \
86 .resource = _name##_id##_resource, \
87 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
88}
89
caf18f19
JM
90#define select_peripheral(port, pin_mask, periph, flags) \
91 at32_select_periph(GPIO_##port##_BASE, pin_mask, \
92 GPIO_##periph, flags)
c3e2a79c 93
5f97f7f9
HS
94#define DEV_CLK(_name, devname, bus, _index) \
95static struct clk devname##_##_name = { \
96 .name = #_name, \
97 .dev = &devname##_device.dev, \
98 .parent = &bus##_clk, \
99 .mode = bus##_clk_mode, \
100 .get_rate = bus##_clk_get_rate, \
101 .index = _index, \
102}
103
7a5b8059
HS
104static DEFINE_SPINLOCK(pm_lock);
105
35bf50cc
HCE
106static struct clk osc0;
107static struct clk osc1;
108
5f97f7f9
HS
109static unsigned long osc_get_rate(struct clk *clk)
110{
60ed7951 111 return at32_board_osc_rates[clk->index];
5f97f7f9
HS
112}
113
114static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
115{
116 unsigned long div, mul, rate;
117
7a5b8059
HS
118 div = PM_BFEXT(PLLDIV, control) + 1;
119 mul = PM_BFEXT(PLLMUL, control) + 1;
5f97f7f9
HS
120
121 rate = clk->parent->get_rate(clk->parent);
122 rate = (rate + div / 2) / div;
123 rate *= mul;
124
125 return rate;
126}
127
35bf50cc
HCE
128static long pll_set_rate(struct clk *clk, unsigned long rate,
129 u32 *pll_ctrl)
130{
131 unsigned long mul;
132 unsigned long mul_best_fit = 0;
133 unsigned long div;
134 unsigned long div_min;
135 unsigned long div_max;
136 unsigned long div_best_fit = 0;
137 unsigned long base;
138 unsigned long pll_in;
139 unsigned long actual = 0;
140 unsigned long rate_error;
141 unsigned long rate_error_prev = ~0UL;
142 u32 ctrl;
143
144 /* Rate must be between 80 MHz and 200 Mhz. */
145 if (rate < 80000000UL || rate > 200000000UL)
146 return -EINVAL;
147
148 ctrl = PM_BF(PLLOPT, 4);
149 base = clk->parent->get_rate(clk->parent);
150
151 /* PLL input frequency must be between 6 MHz and 32 MHz. */
152 div_min = DIV_ROUND_UP(base, 32000000UL);
153 div_max = base / 6000000UL;
154
155 if (div_max < div_min)
156 return -EINVAL;
157
158 for (div = div_min; div <= div_max; div++) {
159 pll_in = (base + div / 2) / div;
160 mul = (rate + pll_in / 2) / pll_in;
161
162 if (mul == 0)
163 continue;
164
165 actual = pll_in * mul;
166 rate_error = abs(actual - rate);
167
168 if (rate_error < rate_error_prev) {
169 mul_best_fit = mul;
170 div_best_fit = div;
171 rate_error_prev = rate_error;
172 }
173
174 if (rate_error == 0)
175 break;
176 }
177
178 if (div_best_fit == 0)
179 return -EINVAL;
180
181 ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
182 ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
183 ctrl |= PM_BF(PLLCOUNT, 16);
184
185 if (clk->parent == &osc1)
186 ctrl |= PM_BIT(PLLOSC);
187
188 *pll_ctrl = ctrl;
189
190 return actual;
191}
192
5f97f7f9
HS
193static unsigned long pll0_get_rate(struct clk *clk)
194{
195 u32 control;
196
7a5b8059 197 control = pm_readl(PLL0);
5f97f7f9
HS
198
199 return pll_get_rate(clk, control);
200}
201
35bf50cc
HCE
202static void pll1_mode(struct clk *clk, int enabled)
203{
204 unsigned long timeout;
205 u32 status;
206 u32 ctrl;
207
208 ctrl = pm_readl(PLL1);
209
210 if (enabled) {
211 if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
212 pr_debug("clk %s: failed to enable, rate not set\n",
213 clk->name);
214 return;
215 }
216
217 ctrl |= PM_BIT(PLLEN);
218 pm_writel(PLL1, ctrl);
219
220 /* Wait for PLL lock. */
221 for (timeout = 10000; timeout; timeout--) {
222 status = pm_readl(ISR);
223 if (status & PM_BIT(LOCK1))
224 break;
225 udelay(10);
226 }
227
228 if (!(status & PM_BIT(LOCK1)))
229 printk(KERN_ERR "clk %s: timeout waiting for lock\n",
230 clk->name);
231 } else {
232 ctrl &= ~PM_BIT(PLLEN);
233 pm_writel(PLL1, ctrl);
234 }
235}
236
5f97f7f9
HS
237static unsigned long pll1_get_rate(struct clk *clk)
238{
239 u32 control;
240
7a5b8059 241 control = pm_readl(PLL1);
5f97f7f9
HS
242
243 return pll_get_rate(clk, control);
244}
245
35bf50cc
HCE
246static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
247{
248 u32 ctrl = 0;
249 unsigned long actual_rate;
250
251 actual_rate = pll_set_rate(clk, rate, &ctrl);
252
253 if (apply) {
254 if (actual_rate != rate)
255 return -EINVAL;
256 if (clk->users > 0)
257 return -EBUSY;
258 pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
259 clk->name, rate, actual_rate);
260 pm_writel(PLL1, ctrl);
261 }
262
263 return actual_rate;
264}
265
266static int pll1_set_parent(struct clk *clk, struct clk *parent)
267{
268 u32 ctrl;
269
270 if (clk->users > 0)
271 return -EBUSY;
272
273 ctrl = pm_readl(PLL1);
274 WARN_ON(ctrl & PM_BIT(PLLEN));
275
276 if (parent == &osc0)
277 ctrl &= ~PM_BIT(PLLOSC);
278 else if (parent == &osc1)
279 ctrl |= PM_BIT(PLLOSC);
280 else
281 return -EINVAL;
282
283 pm_writel(PLL1, ctrl);
284 clk->parent = parent;
285
286 return 0;
287}
288
5f97f7f9
HS
289/*
290 * The AT32AP7000 has five primary clock sources: One 32kHz
291 * oscillator, two crystal oscillators and two PLLs.
292 */
293static struct clk osc32k = {
294 .name = "osc32k",
295 .get_rate = osc_get_rate,
296 .users = 1,
297 .index = 0,
298};
299static struct clk osc0 = {
300 .name = "osc0",
301 .get_rate = osc_get_rate,
302 .users = 1,
303 .index = 1,
304};
305static struct clk osc1 = {
306 .name = "osc1",
307 .get_rate = osc_get_rate,
308 .index = 2,
309};
310static struct clk pll0 = {
311 .name = "pll0",
312 .get_rate = pll0_get_rate,
313 .parent = &osc0,
314};
315static struct clk pll1 = {
316 .name = "pll1",
35bf50cc 317 .mode = pll1_mode,
5f97f7f9 318 .get_rate = pll1_get_rate,
35bf50cc
HCE
319 .set_rate = pll1_set_rate,
320 .set_parent = pll1_set_parent,
5f97f7f9
HS
321 .parent = &osc0,
322};
323
324/*
325 * The main clock can be either osc0 or pll0. The boot loader may
326 * have chosen one for us, so we don't really know which one until we
327 * have a look at the SM.
328 */
329static struct clk *main_clock;
330
331/*
332 * Synchronous clocks are generated from the main clock. The clocks
333 * must satisfy the constraint
334 * fCPU >= fHSB >= fPB
335 * i.e. each clock must not be faster than its parent.
336 */
337static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
338{
339 return main_clock->get_rate(main_clock) >> shift;
340};
341
342static void cpu_clk_mode(struct clk *clk, int enabled)
343{
5f97f7f9
HS
344 unsigned long flags;
345 u32 mask;
346
7a5b8059
HS
347 spin_lock_irqsave(&pm_lock, flags);
348 mask = pm_readl(CPU_MASK);
5f97f7f9
HS
349 if (enabled)
350 mask |= 1 << clk->index;
351 else
352 mask &= ~(1 << clk->index);
7a5b8059
HS
353 pm_writel(CPU_MASK, mask);
354 spin_unlock_irqrestore(&pm_lock, flags);
5f97f7f9
HS
355}
356
357static unsigned long cpu_clk_get_rate(struct clk *clk)
358{
359 unsigned long cksel, shift = 0;
360
7a5b8059
HS
361 cksel = pm_readl(CKSEL);
362 if (cksel & PM_BIT(CPUDIV))
363 shift = PM_BFEXT(CPUSEL, cksel) + 1;
5f97f7f9
HS
364
365 return bus_clk_get_rate(clk, shift);
366}
367
9e58e185
HCE
368static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
369{
370 u32 control;
371 unsigned long parent_rate, child_div, actual_rate, div;
372
373 parent_rate = clk->parent->get_rate(clk->parent);
374 control = pm_readl(CKSEL);
375
376 if (control & PM_BIT(HSBDIV))
377 child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
378 else
379 child_div = 1;
380
381 if (rate > 3 * (parent_rate / 4) || child_div == 1) {
382 actual_rate = parent_rate;
383 control &= ~PM_BIT(CPUDIV);
384 } else {
385 unsigned int cpusel;
386 div = (parent_rate + rate / 2) / rate;
387 if (div > child_div)
388 div = child_div;
389 cpusel = (div > 1) ? (fls(div) - 2) : 0;
390 control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
391 actual_rate = parent_rate / (1 << (cpusel + 1));
392 }
393
394 pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
395 clk->name, rate, actual_rate);
396
397 if (apply)
398 pm_writel(CKSEL, control);
399
400 return actual_rate;
401}
402
5f97f7f9
HS
403static void hsb_clk_mode(struct clk *clk, int enabled)
404{
5f97f7f9
HS
405 unsigned long flags;
406 u32 mask;
407
7a5b8059
HS
408 spin_lock_irqsave(&pm_lock, flags);
409 mask = pm_readl(HSB_MASK);
5f97f7f9
HS
410 if (enabled)
411 mask |= 1 << clk->index;
412 else
413 mask &= ~(1 << clk->index);
7a5b8059
HS
414 pm_writel(HSB_MASK, mask);
415 spin_unlock_irqrestore(&pm_lock, flags);
5f97f7f9
HS
416}
417
418static unsigned long hsb_clk_get_rate(struct clk *clk)
419{
420 unsigned long cksel, shift = 0;
421
7a5b8059
HS
422 cksel = pm_readl(CKSEL);
423 if (cksel & PM_BIT(HSBDIV))
424 shift = PM_BFEXT(HSBSEL, cksel) + 1;
5f97f7f9
HS
425
426 return bus_clk_get_rate(clk, shift);
427}
428
dd5e1339 429void pba_clk_mode(struct clk *clk, int enabled)
5f97f7f9 430{
5f97f7f9
HS
431 unsigned long flags;
432 u32 mask;
433
7a5b8059
HS
434 spin_lock_irqsave(&pm_lock, flags);
435 mask = pm_readl(PBA_MASK);
5f97f7f9
HS
436 if (enabled)
437 mask |= 1 << clk->index;
438 else
439 mask &= ~(1 << clk->index);
7a5b8059
HS
440 pm_writel(PBA_MASK, mask);
441 spin_unlock_irqrestore(&pm_lock, flags);
5f97f7f9
HS
442}
443
dd5e1339 444unsigned long pba_clk_get_rate(struct clk *clk)
5f97f7f9
HS
445{
446 unsigned long cksel, shift = 0;
447
7a5b8059
HS
448 cksel = pm_readl(CKSEL);
449 if (cksel & PM_BIT(PBADIV))
450 shift = PM_BFEXT(PBASEL, cksel) + 1;
5f97f7f9
HS
451
452 return bus_clk_get_rate(clk, shift);
453}
454
455static void pbb_clk_mode(struct clk *clk, int enabled)
456{
5f97f7f9
HS
457 unsigned long flags;
458 u32 mask;
459
7a5b8059
HS
460 spin_lock_irqsave(&pm_lock, flags);
461 mask = pm_readl(PBB_MASK);
5f97f7f9
HS
462 if (enabled)
463 mask |= 1 << clk->index;
464 else
465 mask &= ~(1 << clk->index);
7a5b8059
HS
466 pm_writel(PBB_MASK, mask);
467 spin_unlock_irqrestore(&pm_lock, flags);
5f97f7f9
HS
468}
469
470static unsigned long pbb_clk_get_rate(struct clk *clk)
471{
472 unsigned long cksel, shift = 0;
473
7a5b8059
HS
474 cksel = pm_readl(CKSEL);
475 if (cksel & PM_BIT(PBBDIV))
476 shift = PM_BFEXT(PBBSEL, cksel) + 1;
5f97f7f9
HS
477
478 return bus_clk_get_rate(clk, shift);
479}
480
481static struct clk cpu_clk = {
482 .name = "cpu",
483 .get_rate = cpu_clk_get_rate,
9e58e185 484 .set_rate = cpu_clk_set_rate,
5f97f7f9
HS
485 .users = 1,
486};
487static struct clk hsb_clk = {
488 .name = "hsb",
489 .parent = &cpu_clk,
490 .get_rate = hsb_clk_get_rate,
491};
492static struct clk pba_clk = {
493 .name = "pba",
494 .parent = &hsb_clk,
495 .mode = hsb_clk_mode,
496 .get_rate = pba_clk_get_rate,
497 .index = 1,
498};
499static struct clk pbb_clk = {
500 .name = "pbb",
501 .parent = &hsb_clk,
502 .mode = hsb_clk_mode,
503 .get_rate = pbb_clk_get_rate,
504 .users = 1,
505 .index = 2,
506};
507
508/* --------------------------------------------------------------------
509 * Generic Clock operations
510 * -------------------------------------------------------------------- */
511
512static void genclk_mode(struct clk *clk, int enabled)
513{
514 u32 control;
515
7a5b8059 516 control = pm_readl(GCCTRL(clk->index));
5f97f7f9 517 if (enabled)
7a5b8059 518 control |= PM_BIT(CEN);
5f97f7f9 519 else
7a5b8059
HS
520 control &= ~PM_BIT(CEN);
521 pm_writel(GCCTRL(clk->index), control);
5f97f7f9
HS
522}
523
524static unsigned long genclk_get_rate(struct clk *clk)
525{
526 u32 control;
527 unsigned long div = 1;
528
7a5b8059
HS
529 control = pm_readl(GCCTRL(clk->index));
530 if (control & PM_BIT(DIVEN))
531 div = 2 * (PM_BFEXT(DIV, control) + 1);
5f97f7f9
HS
532
533 return clk->parent->get_rate(clk->parent) / div;
534}
535
536static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
537{
538 u32 control;
539 unsigned long parent_rate, actual_rate, div;
540
5f97f7f9 541 parent_rate = clk->parent->get_rate(clk->parent);
7a5b8059 542 control = pm_readl(GCCTRL(clk->index));
5f97f7f9
HS
543
544 if (rate > 3 * parent_rate / 4) {
545 actual_rate = parent_rate;
7a5b8059 546 control &= ~PM_BIT(DIVEN);
5f97f7f9
HS
547 } else {
548 div = (parent_rate + rate) / (2 * rate) - 1;
7a5b8059 549 control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
5f97f7f9
HS
550 actual_rate = parent_rate / (2 * (div + 1));
551 }
552
7a5b8059
HS
553 dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
554 clk->name, rate, actual_rate);
5f97f7f9
HS
555
556 if (apply)
7a5b8059 557 pm_writel(GCCTRL(clk->index), control);
5f97f7f9
HS
558
559 return actual_rate;
560}
561
562int genclk_set_parent(struct clk *clk, struct clk *parent)
563{
564 u32 control;
565
7a5b8059
HS
566 dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
567 clk->name, parent->name, clk->parent->name);
5f97f7f9 568
7a5b8059 569 control = pm_readl(GCCTRL(clk->index));
5f97f7f9
HS
570
571 if (parent == &osc1 || parent == &pll1)
7a5b8059 572 control |= PM_BIT(OSCSEL);
5f97f7f9 573 else if (parent == &osc0 || parent == &pll0)
7a5b8059 574 control &= ~PM_BIT(OSCSEL);
5f97f7f9
HS
575 else
576 return -EINVAL;
577
578 if (parent == &pll0 || parent == &pll1)
7a5b8059 579 control |= PM_BIT(PLLSEL);
5f97f7f9 580 else
7a5b8059 581 control &= ~PM_BIT(PLLSEL);
5f97f7f9 582
7a5b8059 583 pm_writel(GCCTRL(clk->index), control);
5f97f7f9
HS
584 clk->parent = parent;
585
586 return 0;
587}
588
7a5fe238
HS
589static void __init genclk_init_parent(struct clk *clk)
590{
591 u32 control;
592 struct clk *parent;
593
594 BUG_ON(clk->index > 7);
595
7a5b8059
HS
596 control = pm_readl(GCCTRL(clk->index));
597 if (control & PM_BIT(OSCSEL))
598 parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
7a5fe238 599 else
7a5b8059 600 parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
7a5fe238
HS
601
602 clk->parent = parent;
603}
604
3bfb1d20
HS
605static struct dw_dma_platform_data dw_dmac0_data = {
606 .nr_channels = 3,
607};
608
609static struct resource dw_dmac0_resource[] = {
610 PBMEM(0xff200000),
611 IRQ(2),
612};
613DEFINE_DEV_DATA(dw_dmac, 0);
614DEV_CLK(hclk, dw_dmac0, hsb, 10);
615
5f97f7f9
HS
616/* --------------------------------------------------------------------
617 * System peripherals
618 * -------------------------------------------------------------------- */
7a5b8059
HS
619static struct resource at32_pm0_resource[] = {
620 {
621 .start = 0xfff00000,
622 .end = 0xfff0007f,
623 .flags = IORESOURCE_MEM,
624 },
625 IRQ(20),
5f97f7f9 626};
7a5b8059
HS
627
628static struct resource at32ap700x_rtc0_resource[] = {
629 {
630 .start = 0xfff00080,
631 .end = 0xfff000af,
632 .flags = IORESOURCE_MEM,
633 },
634 IRQ(21),
5f97f7f9 635};
7a5b8059
HS
636
637static struct resource at32_wdt0_resource[] = {
638 {
639 .start = 0xfff000b0,
9797bed2 640 .end = 0xfff000cf,
7a5b8059
HS
641 .flags = IORESOURCE_MEM,
642 },
643};
644
645static struct resource at32_eic0_resource[] = {
646 {
647 .start = 0xfff00100,
648 .end = 0xfff0013f,
649 .flags = IORESOURCE_MEM,
650 },
651 IRQ(19),
652};
653
654DEFINE_DEV(at32_pm, 0);
655DEFINE_DEV(at32ap700x_rtc, 0);
656DEFINE_DEV(at32_wdt, 0);
657DEFINE_DEV(at32_eic, 0);
658
659/*
660 * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
661 * is always running.
662 */
663static struct clk at32_pm_pclk = {
188ff65d 664 .name = "pclk",
7a5b8059 665 .dev = &at32_pm0_device.dev,
188ff65d
HS
666 .parent = &pbb_clk,
667 .mode = pbb_clk_mode,
668 .get_rate = pbb_clk_get_rate,
669 .users = 1,
670 .index = 0,
671};
5f97f7f9
HS
672
673static struct resource intc0_resource[] = {
674 PBMEM(0xfff00400),
675};
676struct platform_device at32_intc0_device = {
677 .name = "intc",
678 .id = 0,
679 .resource = intc0_resource,
680 .num_resources = ARRAY_SIZE(intc0_resource),
681};
682DEV_CLK(pclk, at32_intc0, pbb, 1);
683
684static struct clk ebi_clk = {
685 .name = "ebi",
686 .parent = &hsb_clk,
687 .mode = hsb_clk_mode,
688 .get_rate = hsb_clk_get_rate,
689 .users = 1,
690};
691static struct clk hramc_clk = {
692 .name = "hramc",
693 .parent = &hsb_clk,
694 .mode = hsb_clk_mode,
695 .get_rate = hsb_clk_get_rate,
696 .users = 1,
188ff65d 697 .index = 3,
5f97f7f9 698};
7951f188
HS
699static struct clk sdramc_clk = {
700 .name = "sdramc_clk",
701 .parent = &pbb_clk,
702 .mode = pbb_clk_mode,
703 .get_rate = pbb_clk_get_rate,
704 .users = 1,
705 .index = 14,
706};
5f97f7f9 707
bc157b75
HS
708static struct resource smc0_resource[] = {
709 PBMEM(0xfff03400),
710};
711DEFINE_DEV(smc, 0);
712DEV_CLK(pclk, smc0, pbb, 13);
713DEV_CLK(mck, smc0, hsb, 0);
714
5f97f7f9
HS
715static struct platform_device pdc_device = {
716 .name = "pdc",
717 .id = 0,
718};
719DEV_CLK(hclk, pdc, hsb, 4);
720DEV_CLK(pclk, pdc, pba, 16);
721
722static struct clk pico_clk = {
723 .name = "pico",
724 .parent = &cpu_clk,
725 .mode = cpu_clk_mode,
726 .get_rate = cpu_clk_get_rate,
727 .users = 1,
728};
729
9c8f8e75
HS
730/* --------------------------------------------------------------------
731 * HMATRIX
732 * -------------------------------------------------------------------- */
733
b47eb409 734struct clk at32_hmatrix_clk = {
9c8f8e75
HS
735 .name = "hmatrix_clk",
736 .parent = &pbb_clk,
737 .mode = pbb_clk_mode,
738 .get_rate = pbb_clk_get_rate,
739 .index = 2,
740 .users = 1,
741};
9c8f8e75
HS
742
743/*
744 * Set bits in the HMATRIX Special Function Register (SFR) used by the
745 * External Bus Interface (EBI). This can be used to enable special
746 * features like CompactFlash support, NAND Flash support, etc. on
747 * certain chipselects.
748 */
749static inline void set_ebi_sfr_bits(u32 mask)
750{
b47eb409 751 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);
9c8f8e75
HS
752}
753
7760989e 754/* --------------------------------------------------------------------
e723ff66 755 * Timer/Counter (TC)
7760989e 756 * -------------------------------------------------------------------- */
e723ff66
DB
757
758static struct resource at32_tcb0_resource[] = {
7760989e
HCE
759 PBMEM(0xfff00c00),
760 IRQ(22),
761};
e723ff66
DB
762static struct platform_device at32_tcb0_device = {
763 .name = "atmel_tcb",
7760989e 764 .id = 0,
e723ff66
DB
765 .resource = at32_tcb0_resource,
766 .num_resources = ARRAY_SIZE(at32_tcb0_resource),
767};
768DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
769
770static struct resource at32_tcb1_resource[] = {
771 PBMEM(0xfff01000),
772 IRQ(23),
773};
774static struct platform_device at32_tcb1_device = {
775 .name = "atmel_tcb",
776 .id = 1,
777 .resource = at32_tcb1_resource,
778 .num_resources = ARRAY_SIZE(at32_tcb1_resource),
7760989e 779};
e723ff66 780DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
7760989e 781
5f97f7f9
HS
782/* --------------------------------------------------------------------
783 * PIO
784 * -------------------------------------------------------------------- */
785
786static struct resource pio0_resource[] = {
787 PBMEM(0xffe02800),
788 IRQ(13),
789};
790DEFINE_DEV(pio, 0);
791DEV_CLK(mck, pio0, pba, 10);
792
793static struct resource pio1_resource[] = {
794 PBMEM(0xffe02c00),
795 IRQ(14),
796};
797DEFINE_DEV(pio, 1);
798DEV_CLK(mck, pio1, pba, 11);
799
800static struct resource pio2_resource[] = {
801 PBMEM(0xffe03000),
802 IRQ(15),
803};
804DEFINE_DEV(pio, 2);
805DEV_CLK(mck, pio2, pba, 12);
806
807static struct resource pio3_resource[] = {
808 PBMEM(0xffe03400),
809 IRQ(16),
810};
811DEFINE_DEV(pio, 3);
812DEV_CLK(mck, pio3, pba, 13);
813
7f9f4678
HS
814static struct resource pio4_resource[] = {
815 PBMEM(0xffe03800),
816 IRQ(17),
817};
818DEFINE_DEV(pio, 4);
819DEV_CLK(mck, pio4, pba, 14);
820
e82c6106 821static int __init system_device_init(void)
5f97f7f9 822{
7a5b8059 823 platform_device_register(&at32_pm0_device);
5f97f7f9 824 platform_device_register(&at32_intc0_device);
7a5b8059
HS
825 platform_device_register(&at32ap700x_rtc0_device);
826 platform_device_register(&at32_wdt0_device);
827 platform_device_register(&at32_eic0_device);
bc157b75 828 platform_device_register(&smc0_device);
5f97f7f9 829 platform_device_register(&pdc_device);
3bfb1d20 830 platform_device_register(&dw_dmac0_device);
5f97f7f9 831
e723ff66
DB
832 platform_device_register(&at32_tcb0_device);
833 platform_device_register(&at32_tcb1_device);
7760989e 834
5f97f7f9
HS
835 platform_device_register(&pio0_device);
836 platform_device_register(&pio1_device);
837 platform_device_register(&pio2_device);
838 platform_device_register(&pio3_device);
7f9f4678 839 platform_device_register(&pio4_device);
e82c6106
HS
840
841 return 0;
5f97f7f9 842}
e82c6106 843core_initcall(system_device_init);
5f97f7f9 844
d86d314f
HCE
845/* --------------------------------------------------------------------
846 * PSIF
847 * -------------------------------------------------------------------- */
848static struct resource atmel_psif0_resource[] __initdata = {
849 {
850 .start = 0xffe03c00,
851 .end = 0xffe03cff,
852 .flags = IORESOURCE_MEM,
853 },
854 IRQ(18),
855};
856static struct clk atmel_psif0_pclk = {
857 .name = "pclk",
858 .parent = &pba_clk,
859 .mode = pba_clk_mode,
860 .get_rate = pba_clk_get_rate,
861 .index = 15,
862};
863
864static struct resource atmel_psif1_resource[] __initdata = {
865 {
866 .start = 0xffe03d00,
867 .end = 0xffe03dff,
868 .flags = IORESOURCE_MEM,
869 },
870 IRQ(18),
871};
872static struct clk atmel_psif1_pclk = {
873 .name = "pclk",
874 .parent = &pba_clk,
875 .mode = pba_clk_mode,
876 .get_rate = pba_clk_get_rate,
877 .index = 15,
878};
879
880struct platform_device *__init at32_add_device_psif(unsigned int id)
881{
882 struct platform_device *pdev;
caf18f19 883 u32 pin_mask;
d86d314f
HCE
884
885 if (!(id == 0 || id == 1))
886 return NULL;
887
888 pdev = platform_device_alloc("atmel_psif", id);
889 if (!pdev)
890 return NULL;
891
892 switch (id) {
893 case 0:
caf18f19
JM
894 pin_mask = (1 << 8) | (1 << 9); /* CLOCK & DATA */
895
d86d314f
HCE
896 if (platform_device_add_resources(pdev, atmel_psif0_resource,
897 ARRAY_SIZE(atmel_psif0_resource)))
898 goto err_add_resources;
899 atmel_psif0_pclk.dev = &pdev->dev;
caf18f19 900 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
d86d314f
HCE
901 break;
902 case 1:
caf18f19
JM
903 pin_mask = (1 << 11) | (1 << 12); /* CLOCK & DATA */
904
d86d314f
HCE
905 if (platform_device_add_resources(pdev, atmel_psif1_resource,
906 ARRAY_SIZE(atmel_psif1_resource)))
907 goto err_add_resources;
908 atmel_psif1_pclk.dev = &pdev->dev;
caf18f19 909 select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
d86d314f
HCE
910 break;
911 default:
912 return NULL;
913 }
914
915 platform_device_add(pdev);
916 return pdev;
917
918err_add_resources:
919 platform_device_put(pdev);
920 return NULL;
921}
922
5f97f7f9
HS
923/* --------------------------------------------------------------------
924 * USART
925 * -------------------------------------------------------------------- */
926
75d35213
HS
927static struct atmel_uart_data atmel_usart0_data = {
928 .use_dma_tx = 1,
929 .use_dma_rx = 1,
930};
1e8ea802 931static struct resource atmel_usart0_resource[] = {
5f97f7f9 932 PBMEM(0xffe00c00),
a3d912c8 933 IRQ(6),
5f97f7f9 934};
75d35213 935DEFINE_DEV_DATA(atmel_usart, 0);
80f76c54 936DEV_CLK(usart, atmel_usart0, pba, 3);
5f97f7f9 937
75d35213
HS
938static struct atmel_uart_data atmel_usart1_data = {
939 .use_dma_tx = 1,
940 .use_dma_rx = 1,
941};
1e8ea802 942static struct resource atmel_usart1_resource[] = {
5f97f7f9
HS
943 PBMEM(0xffe01000),
944 IRQ(7),
945};
75d35213 946DEFINE_DEV_DATA(atmel_usart, 1);
1e8ea802 947DEV_CLK(usart, atmel_usart1, pba, 4);
5f97f7f9 948
75d35213
HS
949static struct atmel_uart_data atmel_usart2_data = {
950 .use_dma_tx = 1,
951 .use_dma_rx = 1,
952};
1e8ea802 953static struct resource atmel_usart2_resource[] = {
5f97f7f9
HS
954 PBMEM(0xffe01400),
955 IRQ(8),
956};
75d35213 957DEFINE_DEV_DATA(atmel_usart, 2);
1e8ea802 958DEV_CLK(usart, atmel_usart2, pba, 5);
5f97f7f9 959
75d35213
HS
960static struct atmel_uart_data atmel_usart3_data = {
961 .use_dma_tx = 1,
962 .use_dma_rx = 1,
963};
1e8ea802 964static struct resource atmel_usart3_resource[] = {
5f97f7f9
HS
965 PBMEM(0xffe01800),
966 IRQ(9),
967};
75d35213 968DEFINE_DEV_DATA(atmel_usart, 3);
1e8ea802 969DEV_CLK(usart, atmel_usart3, pba, 6);
5f97f7f9 970
bf4861cf 971static inline void configure_usart0_pins(int flags)
5f97f7f9 972{
caf18f19 973 u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
bf4861cf
PM
974 if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 6);
975 if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 7);
976 if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 10);
caf18f19 977
10546263 978 select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
5f97f7f9
HS
979}
980
bf4861cf 981static inline void configure_usart1_pins(int flags)
5f97f7f9 982{
caf18f19 983 u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
bf4861cf
PM
984 if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 19);
985 if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 20);
986 if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 16);
caf18f19 987
10546263 988 select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
5f97f7f9
HS
989}
990
bf4861cf 991static inline void configure_usart2_pins(int flags)
5f97f7f9 992{
caf18f19 993 u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
bf4861cf
PM
994 if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 30);
995 if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 29);
996 if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 28);
caf18f19 997
10546263 998 select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
5f97f7f9
HS
999}
1000
bf4861cf 1001static inline void configure_usart3_pins(int flags)
5f97f7f9 1002{
caf18f19 1003 u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
bf4861cf
PM
1004 if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 16);
1005 if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 15);
1006 if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 19);
caf18f19 1007
10546263 1008 select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
5f97f7f9
HS
1009}
1010
a3d912c8 1011static struct platform_device *__initdata at32_usarts[4];
c194588d 1012
bf4861cf 1013void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags)
5f97f7f9
HS
1014{
1015 struct platform_device *pdev;
1016
c194588d 1017 switch (hw_id) {
5f97f7f9 1018 case 0:
1e8ea802 1019 pdev = &atmel_usart0_device;
bf4861cf 1020 configure_usart0_pins(flags);
5f97f7f9
HS
1021 break;
1022 case 1:
1e8ea802 1023 pdev = &atmel_usart1_device;
bf4861cf 1024 configure_usart1_pins(flags);
5f97f7f9
HS
1025 break;
1026 case 2:
1e8ea802 1027 pdev = &atmel_usart2_device;
bf4861cf 1028 configure_usart2_pins(flags);
5f97f7f9
HS
1029 break;
1030 case 3:
1e8ea802 1031 pdev = &atmel_usart3_device;
bf4861cf 1032 configure_usart3_pins(flags);
5f97f7f9
HS
1033 break;
1034 default:
c194588d 1035 return;
75d35213
HS
1036 }
1037
1038 if (PXSEG(pdev->resource[0].start) == P4SEG) {
1039 /* Addresses in the P4 segment are permanently mapped 1:1 */
1040 struct atmel_uart_data *data = pdev->dev.platform_data;
1041 data->regs = (void __iomem *)pdev->resource[0].start;
5f97f7f9
HS
1042 }
1043
c194588d
HS
1044 pdev->id = line;
1045 at32_usarts[line] = pdev;
5f97f7f9
HS
1046}
1047
1048struct platform_device *__init at32_add_device_usart(unsigned int id)
1049{
c194588d
HS
1050 platform_device_register(at32_usarts[id]);
1051 return at32_usarts[id];
5f97f7f9
HS
1052}
1053
73e2798b 1054struct platform_device *atmel_default_console_device;
5f97f7f9
HS
1055
1056void __init at32_setup_serial_console(unsigned int usart_id)
1057{
c194588d 1058 atmel_default_console_device = at32_usarts[usart_id];
5f97f7f9
HS
1059}
1060
1061/* --------------------------------------------------------------------
1062 * Ethernet
1063 * -------------------------------------------------------------------- */
1064
438ff3f3 1065#ifdef CONFIG_CPU_AT32AP7000
5f97f7f9
HS
1066static struct eth_platform_data macb0_data;
1067static struct resource macb0_resource[] = {
1068 PBMEM(0xfff01800),
1069 IRQ(25),
1070};
1071DEFINE_DEV_DATA(macb, 0);
1072DEV_CLK(hclk, macb0, hsb, 8);
1073DEV_CLK(pclk, macb0, pbb, 6);
1074
cfcb3a89
HS
1075static struct eth_platform_data macb1_data;
1076static struct resource macb1_resource[] = {
1077 PBMEM(0xfff01c00),
1078 IRQ(26),
1079};
1080DEFINE_DEV_DATA(macb, 1);
1081DEV_CLK(hclk, macb1, hsb, 9);
1082DEV_CLK(pclk, macb1, pbb, 7);
1083
5f97f7f9
HS
1084struct platform_device *__init
1085at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
1086{
1087 struct platform_device *pdev;
caf18f19 1088 u32 pin_mask;
5f97f7f9
HS
1089
1090 switch (id) {
1091 case 0:
1092 pdev = &macb0_device;
1093
caf18f19
JM
1094 pin_mask = (1 << 3); /* TXD0 */
1095 pin_mask |= (1 << 4); /* TXD1 */
1096 pin_mask |= (1 << 7); /* TXEN */
1097 pin_mask |= (1 << 8); /* TXCK */
1098 pin_mask |= (1 << 9); /* RXD0 */
1099 pin_mask |= (1 << 10); /* RXD1 */
1100 pin_mask |= (1 << 13); /* RXER */
1101 pin_mask |= (1 << 15); /* RXDV */
1102 pin_mask |= (1 << 16); /* MDC */
1103 pin_mask |= (1 << 17); /* MDIO */
5f97f7f9
HS
1104
1105 if (!data->is_rmii) {
caf18f19
JM
1106 pin_mask |= (1 << 0); /* COL */
1107 pin_mask |= (1 << 1); /* CRS */
1108 pin_mask |= (1 << 2); /* TXER */
1109 pin_mask |= (1 << 5); /* TXD2 */
1110 pin_mask |= (1 << 6); /* TXD3 */
1111 pin_mask |= (1 << 11); /* RXD2 */
1112 pin_mask |= (1 << 12); /* RXD3 */
1113 pin_mask |= (1 << 14); /* RXCK */
198f2935 1114#ifndef CONFIG_BOARD_MIMC200
caf18f19 1115 pin_mask |= (1 << 18); /* SPD */
198f2935 1116#endif
5f97f7f9 1117 }
caf18f19
JM
1118
1119 select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
1120
5f97f7f9
HS
1121 break;
1122
cfcb3a89
HS
1123 case 1:
1124 pdev = &macb1_device;
1125
caf18f19
JM
1126 pin_mask = (1 << 13); /* TXD0 */
1127 pin_mask |= (1 << 14); /* TXD1 */
1128 pin_mask |= (1 << 11); /* TXEN */
1129 pin_mask |= (1 << 12); /* TXCK */
1130 pin_mask |= (1 << 10); /* RXD0 */
1131 pin_mask |= (1 << 6); /* RXD1 */
1132 pin_mask |= (1 << 5); /* RXER */
1133 pin_mask |= (1 << 4); /* RXDV */
1134 pin_mask |= (1 << 3); /* MDC */
1135 pin_mask |= (1 << 2); /* MDIO */
1136
198f2935 1137#ifndef CONFIG_BOARD_MIMC200
caf18f19
JM
1138 if (!data->is_rmii)
1139 pin_mask |= (1 << 15); /* SPD */
198f2935 1140#endif
caf18f19
JM
1141
1142 select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
cfcb3a89
HS
1143
1144 if (!data->is_rmii) {
caf18f19
JM
1145 pin_mask = (1 << 19); /* COL */
1146 pin_mask |= (1 << 23); /* CRS */
1147 pin_mask |= (1 << 26); /* TXER */
1148 pin_mask |= (1 << 27); /* TXD2 */
1149 pin_mask |= (1 << 28); /* TXD3 */
1150 pin_mask |= (1 << 29); /* RXD2 */
1151 pin_mask |= (1 << 30); /* RXD3 */
1152 pin_mask |= (1 << 24); /* RXCK */
1153
1154 select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
cfcb3a89
HS
1155 }
1156 break;
1157
5f97f7f9
HS
1158 default:
1159 return NULL;
1160 }
1161
1162 memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
1163 platform_device_register(pdev);
1164
1165 return pdev;
1166}
438ff3f3 1167#endif
5f97f7f9
HS
1168
1169/* --------------------------------------------------------------------
1170 * SPI
1171 * -------------------------------------------------------------------- */
3d60ee1b 1172static struct resource atmel_spi0_resource[] = {
5f97f7f9
HS
1173 PBMEM(0xffe00000),
1174 IRQ(3),
1175};
3d60ee1b
HS
1176DEFINE_DEV(atmel_spi, 0);
1177DEV_CLK(spi_clk, atmel_spi0, pba, 0);
1178
1179static struct resource atmel_spi1_resource[] = {
1180 PBMEM(0xffe00400),
1181 IRQ(4),
1182};
1183DEFINE_DEV(atmel_spi, 1);
1184DEV_CLK(spi_clk, atmel_spi1, pba, 1);
5f97f7f9 1185
07084203
PM
1186void __init
1187at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b, unsigned int n)
5f97f7f9 1188{
07084203
PM
1189 /*
1190 * Manage the chipselects as GPIOs, normally using the same pins
1191 * the SPI controller expects; but boards can use other pins.
1192 */
1193 static u8 __initdata spi_pins[][4] = {
1194 { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
1195 GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
1196 { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
1197 GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
1198 };
41d8ca45
HS
1199 unsigned int pin, mode;
1200
07084203
PM
1201 /* There are only 2 SPI controllers */
1202 if (bus_num > 1)
1203 return;
1204
41d8ca45
HS
1205 for (; n; n--, b++) {
1206 b->bus_num = bus_num;
1207 if (b->chip_select >= 4)
1208 continue;
1209 pin = (unsigned)b->controller_data;
1210 if (!pin) {
07084203 1211 pin = spi_pins[bus_num][b->chip_select];
41d8ca45
HS
1212 b->controller_data = (void *)pin;
1213 }
1214 mode = AT32_GPIOF_OUTPUT;
1215 if (!(b->mode & SPI_CS_HIGH))
1216 mode |= AT32_GPIOF_HIGH;
1217 at32_select_gpio(pin, mode);
1218 }
1219}
1220
1221struct platform_device *__init
1222at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
1223{
5f97f7f9 1224 struct platform_device *pdev;
caf18f19 1225 u32 pin_mask;
5f97f7f9
HS
1226
1227 switch (id) {
1228 case 0:
3d60ee1b 1229 pdev = &atmel_spi0_device;
caf18f19
JM
1230 pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
1231
9c2baf78 1232 /* pullup MISO so a level is always defined */
caf18f19
JM
1233 select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
1234 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
1235
07084203 1236 at32_spi_setup_slaves(0, b, n);
3d60ee1b
HS
1237 break;
1238
1239 case 1:
1240 pdev = &atmel_spi1_device;
caf18f19
JM
1241 pin_mask = (1 << 1) | (1 << 5); /* MOSI */
1242
9c2baf78 1243 /* pullup MISO so a level is always defined */
caf18f19
JM
1244 select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
1245 select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
1246
07084203 1247 at32_spi_setup_slaves(1, b, n);
5f97f7f9
HS
1248 break;
1249
1250 default:
1251 return NULL;
1252 }
1253
41d8ca45 1254 spi_register_board_info(b, n);
5f97f7f9
HS
1255 platform_device_register(pdev);
1256 return pdev;
1257}
1258
2042c1c4
HS
1259/* --------------------------------------------------------------------
1260 * TWI
1261 * -------------------------------------------------------------------- */
1262static struct resource atmel_twi0_resource[] __initdata = {
1263 PBMEM(0xffe00800),
1264 IRQ(5),
1265};
1266static struct clk atmel_twi0_pclk = {
1267 .name = "twi_pclk",
1268 .parent = &pba_clk,
1269 .mode = pba_clk_mode,
1270 .get_rate = pba_clk_get_rate,
1271 .index = 2,
1272};
1273
040b28fc
BN
1274struct platform_device *__init at32_add_device_twi(unsigned int id,
1275 struct i2c_board_info *b,
1276 unsigned int n)
2042c1c4
HS
1277{
1278 struct platform_device *pdev;
caf18f19 1279 u32 pin_mask;
2042c1c4
HS
1280
1281 if (id != 0)
1282 return NULL;
1283
1284 pdev = platform_device_alloc("atmel_twi", id);
1285 if (!pdev)
1286 return NULL;
1287
1288 if (platform_device_add_resources(pdev, atmel_twi0_resource,
1289 ARRAY_SIZE(atmel_twi0_resource)))
1290 goto err_add_resources;
1291
caf18f19
JM
1292 pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
1293
1294 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
2042c1c4
HS
1295
1296 atmel_twi0_pclk.dev = &pdev->dev;
1297
040b28fc
BN
1298 if (b)
1299 i2c_register_board_info(id, b, n);
1300
2042c1c4
HS
1301 platform_device_add(pdev);
1302 return pdev;
1303
1304err_add_resources:
1305 platform_device_put(pdev);
1306 return NULL;
1307}
1308
1309/* --------------------------------------------------------------------
1310 * MMC
1311 * -------------------------------------------------------------------- */
1312static struct resource atmel_mci0_resource[] __initdata = {
1313 PBMEM(0xfff02400),
1314 IRQ(28),
1315};
1316static struct clk atmel_mci0_pclk = {
1317 .name = "mci_clk",
1318 .parent = &pbb_clk,
1319 .mode = pbb_clk_mode,
1320 .get_rate = pbb_clk_get_rate,
1321 .index = 9,
1322};
1323
7d2be074
HS
1324struct platform_device *__init
1325at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
2042c1c4 1326{
7d2be074 1327 struct platform_device *pdev;
754a00ae 1328 struct mci_dma_data *slave;
caf18f19
JM
1329 u32 pioa_mask;
1330 u32 piob_mask;
2042c1c4 1331
6b918657
HS
1332 if (id != 0 || !data)
1333 return NULL;
1334
1335 /* Must have at least one usable slot */
1336 if (!data->slot[0].bus_width && !data->slot[1].bus_width)
2042c1c4
HS
1337 return NULL;
1338
1339 pdev = platform_device_alloc("atmel_mci", id);
1340 if (!pdev)
7d2be074 1341 goto fail;
2042c1c4
HS
1342
1343 if (platform_device_add_resources(pdev, atmel_mci0_resource,
1344 ARRAY_SIZE(atmel_mci0_resource)))
7d2be074
HS
1345 goto fail;
1346
754a00ae 1347 slave = kzalloc(sizeof(struct mci_dma_data), GFP_KERNEL);
cbf8de16
HCE
1348 if (!slave)
1349 goto fail;
2635d1ba
NF
1350
1351 slave->sdata.dma_dev = &dw_dmac0_device.dev;
1352 slave->sdata.reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
1353 slave->sdata.cfg_hi = (DWC_CFGH_SRC_PER(0)
65e8b083 1354 | DWC_CFGH_DST_PER(1));
2635d1ba 1355 slave->sdata.cfg_lo &= ~(DWC_CFGL_HS_DST_POL
65e8b083
HS
1356 | DWC_CFGL_HS_SRC_POL);
1357
2635d1ba
NF
1358 data->dma_slave = slave;
1359
7d2be074
HS
1360 if (platform_device_add_data(pdev, data,
1361 sizeof(struct mci_platform_data)))
cbf8de16 1362 goto fail_free;
2042c1c4 1363
6b918657 1364 /* CLK line is common to both slots */
caf18f19 1365 pioa_mask = 1 << 10;
6b918657
HS
1366
1367 switch (data->slot[0].bus_width) {
1368 case 4:
caf18f19
JM
1369 pioa_mask |= 1 << 13; /* DATA1 */
1370 pioa_mask |= 1 << 14; /* DATA2 */
1371 pioa_mask |= 1 << 15; /* DATA3 */
6b918657
HS
1372 /* fall through */
1373 case 1:
caf18f19
JM
1374 pioa_mask |= 1 << 11; /* CMD */
1375 pioa_mask |= 1 << 12; /* DATA0 */
6b918657
HS
1376
1377 if (gpio_is_valid(data->slot[0].detect_pin))
1378 at32_select_gpio(data->slot[0].detect_pin, 0);
1379 if (gpio_is_valid(data->slot[0].wp_pin))
1380 at32_select_gpio(data->slot[0].wp_pin, 0);
1381 break;
1382 case 0:
1383 /* Slot is unused */
1384 break;
1385 default:
cbf8de16 1386 goto fail_free;
6b918657
HS
1387 }
1388
caf18f19
JM
1389 select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
1390 piob_mask = 0;
1391
6b918657
HS
1392 switch (data->slot[1].bus_width) {
1393 case 4:
caf18f19
JM
1394 piob_mask |= 1 << 8; /* DATA1 */
1395 piob_mask |= 1 << 9; /* DATA2 */
1396 piob_mask |= 1 << 10; /* DATA3 */
6b918657
HS
1397 /* fall through */
1398 case 1:
caf18f19
JM
1399 piob_mask |= 1 << 6; /* CMD */
1400 piob_mask |= 1 << 7; /* DATA0 */
1401 select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
6b918657
HS
1402
1403 if (gpio_is_valid(data->slot[1].detect_pin))
1404 at32_select_gpio(data->slot[1].detect_pin, 0);
1405 if (gpio_is_valid(data->slot[1].wp_pin))
1406 at32_select_gpio(data->slot[1].wp_pin, 0);
1407 break;
1408 case 0:
1409 /* Slot is unused */
1410 break;
1411 default:
1412 if (!data->slot[0].bus_width)
cbf8de16 1413 goto fail_free;
6b918657
HS
1414
1415 data->slot[1].bus_width = 0;
1416 break;
1417 }
7d2be074 1418
2042c1c4
HS
1419 atmel_mci0_pclk.dev = &pdev->dev;
1420
1421 platform_device_add(pdev);
1422 return pdev;
1423
cbf8de16
HCE
1424fail_free:
1425 kfree(slave);
7d2be074 1426fail:
2635d1ba 1427 data->dma_slave = NULL;
2042c1c4
HS
1428 platform_device_put(pdev);
1429 return NULL;
1430}
1431
5f97f7f9
HS
1432/* --------------------------------------------------------------------
1433 * LCDC
1434 * -------------------------------------------------------------------- */
438ff3f3 1435#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
d0a2b7af
HS
1436static struct atmel_lcdfb_info atmel_lcdfb0_data;
1437static struct resource atmel_lcdfb0_resource[] = {
5f97f7f9
HS
1438 {
1439 .start = 0xff000000,
1440 .end = 0xff000fff,
1441 .flags = IORESOURCE_MEM,
1442 },
1443 IRQ(1),
d0a2b7af
HS
1444 {
1445 /* Placeholder for pre-allocated fb memory */
1446 .start = 0x00000000,
1447 .end = 0x00000000,
1448 .flags = 0,
1449 },
5f97f7f9 1450};
d0a2b7af
HS
1451DEFINE_DEV_DATA(atmel_lcdfb, 0);
1452DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
1453static struct clk atmel_lcdfb0_pixclk = {
1454 .name = "lcdc_clk",
1455 .dev = &atmel_lcdfb0_device.dev,
5f97f7f9
HS
1456 .mode = genclk_mode,
1457 .get_rate = genclk_get_rate,
1458 .set_rate = genclk_set_rate,
1459 .set_parent = genclk_set_parent,
1460 .index = 7,
1461};
1462
1463struct platform_device *__init
d0a2b7af 1464at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
47882cf6 1465 unsigned long fbmem_start, unsigned long fbmem_len,
70664124 1466 u64 pin_mask)
5f97f7f9
HS
1467{
1468 struct platform_device *pdev;
d0a2b7af
HS
1469 struct atmel_lcdfb_info *info;
1470 struct fb_monspecs *monspecs;
1471 struct fb_videomode *modedb;
1472 unsigned int modedb_size;
caf18f19 1473 u32 portc_mask, portd_mask, porte_mask;
d0a2b7af
HS
1474
1475 /*
1476 * Do a deep copy of the fb data, monspecs and modedb. Make
1477 * sure all allocations are done before setting up the
1478 * portmux.
1479 */
1480 monspecs = kmemdup(data->default_monspecs,
1481 sizeof(struct fb_monspecs), GFP_KERNEL);
1482 if (!monspecs)
1483 return NULL;
1484
1485 modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
1486 modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
1487 if (!modedb)
1488 goto err_dup_modedb;
1489 monspecs->modedb = modedb;
5f97f7f9
HS
1490
1491 switch (id) {
1492 case 0:
d0a2b7af 1493 pdev = &atmel_lcdfb0_device;
47882cf6 1494
70664124
JM
1495 if (pin_mask == 0ULL)
1496 /* Default to "full" lcdc control signals and 24bit */
1497 pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
1498
1499 /* LCDC on port C */
60900656 1500 portc_mask = pin_mask & 0xfff80000;
caf18f19 1501 select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
70664124
JM
1502
1503 /* LCDC on port D */
caf18f19
JM
1504 portd_mask = pin_mask & 0x0003ffff;
1505 select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
70664124
JM
1506
1507 /* LCDC on port E */
caf18f19
JM
1508 porte_mask = (pin_mask >> 32) & 0x0007ffff;
1509 select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
5f97f7f9 1510
d0a2b7af
HS
1511 clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
1512 clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
5f97f7f9
HS
1513 break;
1514
1515 default:
d0a2b7af 1516 goto err_invalid_id;
5f97f7f9
HS
1517 }
1518
d0a2b7af
HS
1519 if (fbmem_len) {
1520 pdev->resource[2].start = fbmem_start;
1521 pdev->resource[2].end = fbmem_start + fbmem_len - 1;
1522 pdev->resource[2].flags = IORESOURCE_MEM;
1523 }
1524
1525 info = pdev->dev.platform_data;
1526 memcpy(info, data, sizeof(struct atmel_lcdfb_info));
1527 info->default_monspecs = monspecs;
5f97f7f9
HS
1528
1529 platform_device_register(pdev);
1530 return pdev;
d0a2b7af
HS
1531
1532err_invalid_id:
1533 kfree(modedb);
1534err_dup_modedb:
1535 kfree(monspecs);
1536 return NULL;
5f97f7f9 1537}
438ff3f3 1538#endif
5f97f7f9 1539
9a1e8eb1
DB
1540/* --------------------------------------------------------------------
1541 * PWM
1542 * -------------------------------------------------------------------- */
1543static struct resource atmel_pwm0_resource[] __initdata = {
1544 PBMEM(0xfff01400),
1545 IRQ(24),
1546};
1547static struct clk atmel_pwm0_mck = {
8405996f 1548 .name = "pwm_clk",
9a1e8eb1
DB
1549 .parent = &pbb_clk,
1550 .mode = pbb_clk_mode,
1551 .get_rate = pbb_clk_get_rate,
1552 .index = 5,
1553};
1554
1555struct platform_device *__init at32_add_device_pwm(u32 mask)
1556{
1557 struct platform_device *pdev;
caf18f19 1558 u32 pin_mask;
9a1e8eb1
DB
1559
1560 if (!mask)
1561 return NULL;
1562
1563 pdev = platform_device_alloc("atmel_pwm", 0);
1564 if (!pdev)
1565 return NULL;
1566
1567 if (platform_device_add_resources(pdev, atmel_pwm0_resource,
1568 ARRAY_SIZE(atmel_pwm0_resource)))
1569 goto out_free_pdev;
1570
1571 if (platform_device_add_data(pdev, &mask, sizeof(mask)))
1572 goto out_free_pdev;
1573
caf18f19 1574 pin_mask = 0;
9a1e8eb1 1575 if (mask & (1 << 0))
caf18f19 1576 pin_mask |= (1 << 28);
9a1e8eb1 1577 if (mask & (1 << 1))
caf18f19
JM
1578 pin_mask |= (1 << 29);
1579 if (pin_mask > 0)
1580 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
1581
1582 pin_mask = 0;
9a1e8eb1 1583 if (mask & (1 << 2))
caf18f19 1584 pin_mask |= (1 << 21);
9a1e8eb1 1585 if (mask & (1 << 3))
caf18f19
JM
1586 pin_mask |= (1 << 22);
1587 if (pin_mask > 0)
1588 select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
9a1e8eb1
DB
1589
1590 atmel_pwm0_mck.dev = &pdev->dev;
1591
1592 platform_device_add(pdev);
1593
1594 return pdev;
1595
1596out_free_pdev:
1597 platform_device_put(pdev);
1598 return NULL;
1599}
1600
9cf6cf58
HCE
1601/* --------------------------------------------------------------------
1602 * SSC
1603 * -------------------------------------------------------------------- */
1604static struct resource ssc0_resource[] = {
1605 PBMEM(0xffe01c00),
1606 IRQ(10),
1607};
1608DEFINE_DEV(ssc, 0);
1609DEV_CLK(pclk, ssc0, pba, 7);
1610
1611static struct resource ssc1_resource[] = {
1612 PBMEM(0xffe02000),
1613 IRQ(11),
1614};
1615DEFINE_DEV(ssc, 1);
1616DEV_CLK(pclk, ssc1, pba, 8);
1617
1618static struct resource ssc2_resource[] = {
1619 PBMEM(0xffe02400),
1620 IRQ(12),
1621};
1622DEFINE_DEV(ssc, 2);
1623DEV_CLK(pclk, ssc2, pba, 9);
1624
1625struct platform_device *__init
1626at32_add_device_ssc(unsigned int id, unsigned int flags)
1627{
1628 struct platform_device *pdev;
caf18f19 1629 u32 pin_mask = 0;
9cf6cf58
HCE
1630
1631 switch (id) {
1632 case 0:
1633 pdev = &ssc0_device;
1634 if (flags & ATMEL_SSC_RF)
caf18f19 1635 pin_mask |= (1 << 21); /* RF */
9cf6cf58 1636 if (flags & ATMEL_SSC_RK)
caf18f19 1637 pin_mask |= (1 << 22); /* RK */
9cf6cf58 1638 if (flags & ATMEL_SSC_TK)
caf18f19 1639 pin_mask |= (1 << 23); /* TK */
9cf6cf58 1640 if (flags & ATMEL_SSC_TF)
caf18f19 1641 pin_mask |= (1 << 24); /* TF */
9cf6cf58 1642 if (flags & ATMEL_SSC_TD)
caf18f19 1643 pin_mask |= (1 << 25); /* TD */
9cf6cf58 1644 if (flags & ATMEL_SSC_RD)
caf18f19
JM
1645 pin_mask |= (1 << 26); /* RD */
1646
1647 if (pin_mask > 0)
1648 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
1649
9cf6cf58
HCE
1650 break;
1651 case 1:
1652 pdev = &ssc1_device;
1653 if (flags & ATMEL_SSC_RF)
caf18f19 1654 pin_mask |= (1 << 0); /* RF */
9cf6cf58 1655 if (flags & ATMEL_SSC_RK)
caf18f19 1656 pin_mask |= (1 << 1); /* RK */
9cf6cf58 1657 if (flags & ATMEL_SSC_TK)
caf18f19 1658 pin_mask |= (1 << 2); /* TK */
9cf6cf58 1659 if (flags & ATMEL_SSC_TF)
caf18f19 1660 pin_mask |= (1 << 3); /* TF */
9cf6cf58 1661 if (flags & ATMEL_SSC_TD)
caf18f19 1662 pin_mask |= (1 << 4); /* TD */
9cf6cf58 1663 if (flags & ATMEL_SSC_RD)
caf18f19
JM
1664 pin_mask |= (1 << 5); /* RD */
1665
1666 if (pin_mask > 0)
1667 select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
1668
9cf6cf58
HCE
1669 break;
1670 case 2:
1671 pdev = &ssc2_device;
1672 if (flags & ATMEL_SSC_TD)
caf18f19 1673 pin_mask |= (1 << 13); /* TD */
9cf6cf58 1674 if (flags & ATMEL_SSC_RD)
caf18f19 1675 pin_mask |= (1 << 14); /* RD */
9cf6cf58 1676 if (flags & ATMEL_SSC_TK)
caf18f19 1677 pin_mask |= (1 << 15); /* TK */
9cf6cf58 1678 if (flags & ATMEL_SSC_TF)
caf18f19 1679 pin_mask |= (1 << 16); /* TF */
9cf6cf58 1680 if (flags & ATMEL_SSC_RF)
caf18f19 1681 pin_mask |= (1 << 17); /* RF */
9cf6cf58 1682 if (flags & ATMEL_SSC_RK)
caf18f19
JM
1683 pin_mask |= (1 << 18); /* RK */
1684
1685 if (pin_mask > 0)
1686 select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
1687
9cf6cf58
HCE
1688 break;
1689 default:
1690 return NULL;
1691 }
1692
1693 platform_device_register(pdev);
1694 return pdev;
1695}
1696
6fcf0615
HS
1697/* --------------------------------------------------------------------
1698 * USB Device Controller
1699 * -------------------------------------------------------------------- */
1700static struct resource usba0_resource[] __initdata = {
1701 {
1702 .start = 0xff300000,
1703 .end = 0xff3fffff,
1704 .flags = IORESOURCE_MEM,
1705 }, {
1706 .start = 0xfff03000,
1707 .end = 0xfff033ff,
1708 .flags = IORESOURCE_MEM,
1709 },
1710 IRQ(31),
1711};
1712static struct clk usba0_pclk = {
1713 .name = "pclk",
1714 .parent = &pbb_clk,
1715 .mode = pbb_clk_mode,
1716 .get_rate = pbb_clk_get_rate,
1717 .index = 12,
1718};
1719static struct clk usba0_hclk = {
1720 .name = "hclk",
1721 .parent = &hsb_clk,
1722 .mode = hsb_clk_mode,
1723 .get_rate = hsb_clk_get_rate,
1724 .index = 6,
1725};
1726
8d855317
SP
1727#define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
1728 [idx] = { \
1729 .name = nam, \
1730 .index = idx, \
1731 .fifo_size = maxpkt, \
1732 .nr_banks = maxbk, \
1733 .can_dma = dma, \
1734 .can_isoc = isoc, \
1735 }
1736
1737static struct usba_ep_data at32_usba_ep[] __initdata = {
1738 EP("ep0", 0, 64, 1, 0, 0),
1739 EP("ep1", 1, 512, 2, 1, 1),
1740 EP("ep2", 2, 512, 2, 1, 1),
1741 EP("ep3-int", 3, 64, 3, 1, 0),
1742 EP("ep4-int", 4, 64, 3, 1, 0),
1743 EP("ep5", 5, 1024, 3, 1, 1),
1744 EP("ep6", 6, 1024, 3, 1, 1),
1745};
1746
1747#undef EP
1748
6fcf0615
HS
1749struct platform_device *__init
1750at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
1751{
8d855317
SP
1752 /*
1753 * pdata doesn't have room for any endpoints, so we need to
1754 * append room for the ones we need right after it.
1755 */
1756 struct {
1757 struct usba_platform_data pdata;
1758 struct usba_ep_data ep[7];
1759 } usba_data;
6fcf0615
HS
1760 struct platform_device *pdev;
1761
1762 if (id != 0)
1763 return NULL;
1764
1765 pdev = platform_device_alloc("atmel_usba_udc", 0);
1766 if (!pdev)
1767 return NULL;
1768
1769 if (platform_device_add_resources(pdev, usba0_resource,
1770 ARRAY_SIZE(usba0_resource)))
1771 goto out_free_pdev;
1772
640e95ab 1773 if (data) {
8d855317 1774 usba_data.pdata.vbus_pin = data->vbus_pin;
640e95ab
EA
1775 usba_data.pdata.vbus_pin_inverted = data->vbus_pin_inverted;
1776 } else {
8d855317 1777 usba_data.pdata.vbus_pin = -EINVAL;
640e95ab
EA
1778 usba_data.pdata.vbus_pin_inverted = -EINVAL;
1779 }
6fcf0615 1780
8d855317
SP
1781 data = &usba_data.pdata;
1782 data->num_ep = ARRAY_SIZE(at32_usba_ep);
1783 memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
1784
1785 if (platform_device_add_data(pdev, data, sizeof(usba_data)))
1786 goto out_free_pdev;
1787
9477ab2b 1788 if (gpio_is_valid(data->vbus_pin))
8d855317 1789 at32_select_gpio(data->vbus_pin, 0);
6fcf0615
HS
1790
1791 usba0_pclk.dev = &pdev->dev;
1792 usba0_hclk.dev = &pdev->dev;
1793
1794 platform_device_add(pdev);
1795
1796 return pdev;
1797
1798out_free_pdev:
1799 platform_device_put(pdev);
1800 return NULL;
1801}
1802
48021bd9 1803/* --------------------------------------------------------------------
eaf5f925 1804 * IDE / CompactFlash
48021bd9 1805 * -------------------------------------------------------------------- */
438ff3f3 1806#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
eaf5f925 1807static struct resource at32_smc_cs4_resource[] __initdata = {
48021bd9
KNG
1808 {
1809 .start = 0x04000000,
1810 .end = 0x07ffffff,
1811 .flags = IORESOURCE_MEM,
1812 },
1813 IRQ(~0UL), /* Magic IRQ will be overridden */
1814};
eaf5f925
HS
1815static struct resource at32_smc_cs5_resource[] __initdata = {
1816 {
1817 .start = 0x20000000,
1818 .end = 0x23ffffff,
1819 .flags = IORESOURCE_MEM,
1820 },
1821 IRQ(~0UL), /* Magic IRQ will be overridden */
1822};
48021bd9 1823
eaf5f925
HS
1824static int __init at32_init_ide_or_cf(struct platform_device *pdev,
1825 unsigned int cs, unsigned int extint)
48021bd9 1826{
eaf5f925 1827 static unsigned int extint_pin_map[4] __initdata = {
caf18f19
JM
1828 (1 << 25),
1829 (1 << 26),
1830 (1 << 27),
1831 (1 << 28),
eaf5f925
HS
1832 };
1833 static bool common_pins_initialized __initdata = false;
48021bd9 1834 unsigned int extint_pin;
eaf5f925 1835 int ret;
caf18f19 1836 u32 pin_mask;
48021bd9 1837
eaf5f925
HS
1838 if (extint >= ARRAY_SIZE(extint_pin_map))
1839 return -EINVAL;
1840 extint_pin = extint_pin_map[extint];
1841
1842 switch (cs) {
1843 case 4:
1844 ret = platform_device_add_resources(pdev,
1845 at32_smc_cs4_resource,
1846 ARRAY_SIZE(at32_smc_cs4_resource));
1847 if (ret)
1848 return ret;
1849
caf18f19
JM
1850 /* NCS4 -> OE_N */
1851 select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
b47eb409 1852 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
48021bd9 1853 break;
eaf5f925
HS
1854 case 5:
1855 ret = platform_device_add_resources(pdev,
1856 at32_smc_cs5_resource,
1857 ARRAY_SIZE(at32_smc_cs5_resource));
1858 if (ret)
1859 return ret;
1860
caf18f19
JM
1861 /* NCS5 -> OE_N */
1862 select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
b47eb409 1863 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
48021bd9
KNG
1864 break;
1865 default:
eaf5f925 1866 return -EINVAL;
48021bd9
KNG
1867 }
1868
eaf5f925 1869 if (!common_pins_initialized) {
caf18f19
JM
1870 pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
1871 pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
1872 pin_mask |= (1 << 23); /* CFRNW -> DIR */
1873 pin_mask |= (1 << 24); /* NWAIT <- IORDY */
1874
1875 select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
1876
eaf5f925 1877 common_pins_initialized = true;
48021bd9
KNG
1878 }
1879
caf18f19 1880 select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
48021bd9
KNG
1881
1882 pdev->resource[1].start = EIM_IRQ_BASE + extint;
1883 pdev->resource[1].end = pdev->resource[1].start;
1884
eaf5f925
HS
1885 return 0;
1886}
48021bd9 1887
eaf5f925
HS
1888struct platform_device *__init
1889at32_add_device_ide(unsigned int id, unsigned int extint,
1890 struct ide_platform_data *data)
1891{
1892 struct platform_device *pdev;
1893
1894 pdev = platform_device_alloc("at32_ide", id);
1895 if (!pdev)
1896 goto fail;
1897
1898 if (platform_device_add_data(pdev, data,
1899 sizeof(struct ide_platform_data)))
1900 goto fail;
1901
1902 if (at32_init_ide_or_cf(pdev, data->cs, extint))
1903 goto fail;
1904
1905 platform_device_add(pdev);
1906 return pdev;
1907
1908fail:
1909 platform_device_put(pdev);
1910 return NULL;
1911}
1912
1913struct platform_device *__init
1914at32_add_device_cf(unsigned int id, unsigned int extint,
1915 struct cf_platform_data *data)
1916{
1917 struct platform_device *pdev;
1918
1919 pdev = platform_device_alloc("at32_cf", id);
1920 if (!pdev)
1921 goto fail;
48021bd9 1922
eaf5f925
HS
1923 if (platform_device_add_data(pdev, data,
1924 sizeof(struct cf_platform_data)))
1925 goto fail;
1926
1927 if (at32_init_ide_or_cf(pdev, data->cs, extint))
1928 goto fail;
1929
3c26e170 1930 if (gpio_is_valid(data->detect_pin))
eaf5f925 1931 at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
3c26e170 1932 if (gpio_is_valid(data->reset_pin))
eaf5f925 1933 at32_select_gpio(data->reset_pin, 0);
3c26e170 1934 if (gpio_is_valid(data->vcc_pin))
eaf5f925
HS
1935 at32_select_gpio(data->vcc_pin, 0);
1936 /* READY is used as extint, so we can't select it as gpio */
1937
1938 platform_device_add(pdev);
48021bd9 1939 return pdev;
eaf5f925
HS
1940
1941fail:
1942 platform_device_put(pdev);
1943 return NULL;
48021bd9 1944}
438ff3f3 1945#endif
48021bd9 1946
62090a08
HS
1947/* --------------------------------------------------------------------
1948 * NAND Flash / SmartMedia
1949 * -------------------------------------------------------------------- */
1950static struct resource smc_cs3_resource[] __initdata = {
1951 {
1952 .start = 0x0c000000,
1953 .end = 0x0fffffff,
1954 .flags = IORESOURCE_MEM,
1955 }, {
1956 .start = 0xfff03c00,
1957 .end = 0xfff03fff,
1958 .flags = IORESOURCE_MEM,
1959 },
1960};
1961
1962struct platform_device *__init
1963at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
1964{
1965 struct platform_device *pdev;
1966
1967 if (id != 0 || !data)
1968 return NULL;
1969
1970 pdev = platform_device_alloc("atmel_nand", id);
1971 if (!pdev)
1972 goto fail;
1973
1974 if (platform_device_add_resources(pdev, smc_cs3_resource,
1975 ARRAY_SIZE(smc_cs3_resource)))
1976 goto fail;
1977
1978 if (platform_device_add_data(pdev, data,
1979 sizeof(struct atmel_nand_data)))
1980 goto fail;
1981
b47eb409 1982 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
62090a08
HS
1983 if (data->enable_pin)
1984 at32_select_gpio(data->enable_pin,
1985 AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
1986 if (data->rdy_pin)
1987 at32_select_gpio(data->rdy_pin, 0);
1988 if (data->det_pin)
1989 at32_select_gpio(data->det_pin, 0);
1990
1991 platform_device_add(pdev);
1992 return pdev;
1993
1994fail:
1995 platform_device_put(pdev);
1996 return NULL;
1997}
1998
2042c1c4
HS
1999/* --------------------------------------------------------------------
2000 * AC97C
2001 * -------------------------------------------------------------------- */
2002static struct resource atmel_ac97c0_resource[] __initdata = {
2003 PBMEM(0xfff02800),
2004 IRQ(29),
2005};
2006static struct clk atmel_ac97c0_pclk = {
2007 .name = "pclk",
2008 .parent = &pbb_clk,
2009 .mode = pbb_clk_mode,
2010 .get_rate = pbb_clk_get_rate,
2011 .index = 10,
2012};
2013
218df4a2 2014struct platform_device *__init
2f47c8c5
HCE
2015at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data,
2016 unsigned int flags)
2042c1c4 2017{
2f47c8c5
HCE
2018 struct platform_device *pdev;
2019 struct dw_dma_slave *rx_dws;
2020 struct dw_dma_slave *tx_dws;
2021 struct ac97c_platform_data _data;
2022 u32 pin_mask;
2042c1c4
HS
2023
2024 if (id != 0)
2025 return NULL;
2026
2027 pdev = platform_device_alloc("atmel_ac97c", id);
2028 if (!pdev)
2029 return NULL;
2030
2031 if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
2032 ARRAY_SIZE(atmel_ac97c0_resource)))
2f47c8c5 2033 goto out_free_resources;
218df4a2
HCE
2034
2035 if (!data) {
2036 data = &_data;
2037 memset(data, 0, sizeof(struct ac97c_platform_data));
2f47c8c5
HCE
2038 data->reset_pin = -ENODEV;
2039 }
2040
2041 rx_dws = &data->rx_dws;
2042 tx_dws = &data->tx_dws;
2043
2044 /* Check if DMA slave interface for capture should be configured. */
2045 if (flags & AC97C_CAPTURE) {
2046 rx_dws->dma_dev = &dw_dmac0_device.dev;
2047 rx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
2048 rx_dws->cfg_hi = DWC_CFGH_SRC_PER(3);
2049 rx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
218df4a2
HCE
2050 }
2051
2f47c8c5
HCE
2052 /* Check if DMA slave interface for playback should be configured. */
2053 if (flags & AC97C_PLAYBACK) {
2054 tx_dws->dma_dev = &dw_dmac0_device.dev;
2055 tx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
2056 tx_dws->cfg_hi = DWC_CFGH_DST_PER(4);
2057 tx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
2058 }
2042c1c4 2059
218df4a2
HCE
2060 if (platform_device_add_data(pdev, data,
2061 sizeof(struct ac97c_platform_data)))
2f47c8c5 2062 goto out_free_resources;
218df4a2 2063
2f47c8c5
HCE
2064 /* SDO | SYNC | SCLK | SDI */
2065 pin_mask = (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
caf18f19
JM
2066
2067 select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
218df4a2 2068
2f47c8c5
HCE
2069 if (gpio_is_valid(data->reset_pin))
2070 at32_select_gpio(data->reset_pin, AT32_GPIOF_OUTPUT
2071 | AT32_GPIOF_HIGH);
2042c1c4
HS
2072
2073 atmel_ac97c0_pclk.dev = &pdev->dev;
2074
2075 platform_device_add(pdev);
2076 return pdev;
2077
2f47c8c5 2078out_free_resources:
2042c1c4
HS
2079 platform_device_put(pdev);
2080 return NULL;
2081}
2082
2083/* --------------------------------------------------------------------
2084 * ABDAC
2085 * -------------------------------------------------------------------- */
2086static struct resource abdac0_resource[] __initdata = {
2087 PBMEM(0xfff02000),
2088 IRQ(27),
2089};
2090static struct clk abdac0_pclk = {
2091 .name = "pclk",
2092 .parent = &pbb_clk,
2093 .mode = pbb_clk_mode,
2094 .get_rate = pbb_clk_get_rate,
2095 .index = 8,
2096};
2097static struct clk abdac0_sample_clk = {
2098 .name = "sample_clk",
2099 .mode = genclk_mode,
2100 .get_rate = genclk_get_rate,
2101 .set_rate = genclk_set_rate,
2102 .set_parent = genclk_set_parent,
2103 .index = 6,
2104};
2105
6b0c9351
HCE
2106struct platform_device *__init
2107at32_add_device_abdac(unsigned int id, struct atmel_abdac_pdata *data)
2042c1c4 2108{
6b0c9351
HCE
2109 struct platform_device *pdev;
2110 struct dw_dma_slave *dws;
2111 u32 pin_mask;
2042c1c4 2112
6b0c9351 2113 if (id != 0 || !data)
2042c1c4
HS
2114 return NULL;
2115
6b0c9351 2116 pdev = platform_device_alloc("atmel_abdac", id);
2042c1c4
HS
2117 if (!pdev)
2118 return NULL;
2119
2120 if (platform_device_add_resources(pdev, abdac0_resource,
2121 ARRAY_SIZE(abdac0_resource)))
6b0c9351
HCE
2122 goto out_free_resources;
2123
2124 dws = &data->dws;
2125
2126 dws->dma_dev = &dw_dmac0_device.dev;
2127 dws->reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
2128 dws->cfg_hi = DWC_CFGH_DST_PER(2);
2129 dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
2130
2131 if (platform_device_add_data(pdev, data,
2132 sizeof(struct atmel_abdac_pdata)))
2133 goto out_free_resources;
2042c1c4 2134
caf18f19
JM
2135 pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
2136 pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
2137
2138 select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
2042c1c4
HS
2139
2140 abdac0_pclk.dev = &pdev->dev;
2141 abdac0_sample_clk.dev = &pdev->dev;
2142
2143 platform_device_add(pdev);
2144 return pdev;
2145
6b0c9351 2146out_free_resources:
2042c1c4
HS
2147 platform_device_put(pdev);
2148 return NULL;
2149}
2150
7a5fe238
HS
2151/* --------------------------------------------------------------------
2152 * GCLK
2153 * -------------------------------------------------------------------- */
2154static struct clk gclk0 = {
2155 .name = "gclk0",
2156 .mode = genclk_mode,
2157 .get_rate = genclk_get_rate,
2158 .set_rate = genclk_set_rate,
2159 .set_parent = genclk_set_parent,
2160 .index = 0,
2161};
2162static struct clk gclk1 = {
2163 .name = "gclk1",
2164 .mode = genclk_mode,
2165 .get_rate = genclk_get_rate,
2166 .set_rate = genclk_set_rate,
2167 .set_parent = genclk_set_parent,
2168 .index = 1,
2169};
2170static struct clk gclk2 = {
2171 .name = "gclk2",
2172 .mode = genclk_mode,
2173 .get_rate = genclk_get_rate,
2174 .set_rate = genclk_set_rate,
2175 .set_parent = genclk_set_parent,
2176 .index = 2,
2177};
2178static struct clk gclk3 = {
2179 .name = "gclk3",
2180 .mode = genclk_mode,
2181 .get_rate = genclk_get_rate,
2182 .set_rate = genclk_set_rate,
2183 .set_parent = genclk_set_parent,
2184 .index = 3,
2185};
2186static struct clk gclk4 = {
2187 .name = "gclk4",
2188 .mode = genclk_mode,
2189 .get_rate = genclk_get_rate,
2190 .set_rate = genclk_set_rate,
2191 .set_parent = genclk_set_parent,
2192 .index = 4,
2193};
2194
300bb762 2195static __initdata struct clk *init_clocks[] = {
5f97f7f9
HS
2196 &osc32k,
2197 &osc0,
2198 &osc1,
2199 &pll0,
2200 &pll1,
2201 &cpu_clk,
2202 &hsb_clk,
2203 &pba_clk,
2204 &pbb_clk,
7a5b8059 2205 &at32_pm_pclk,
5f97f7f9 2206 &at32_intc0_pclk,
b47eb409 2207 &at32_hmatrix_clk,
5f97f7f9
HS
2208 &ebi_clk,
2209 &hramc_clk,
7951f188 2210 &sdramc_clk,
bc157b75
HS
2211 &smc0_pclk,
2212 &smc0_mck,
5f97f7f9
HS
2213 &pdc_hclk,
2214 &pdc_pclk,
3bfb1d20 2215 &dw_dmac0_hclk,
5f97f7f9
HS
2216 &pico_clk,
2217 &pio0_mck,
2218 &pio1_mck,
2219 &pio2_mck,
2220 &pio3_mck,
7f9f4678 2221 &pio4_mck,
e723ff66
DB
2222 &at32_tcb0_t0_clk,
2223 &at32_tcb1_t0_clk,
d86d314f
HCE
2224 &atmel_psif0_pclk,
2225 &atmel_psif1_pclk,
1e8ea802
HS
2226 &atmel_usart0_usart,
2227 &atmel_usart1_usart,
2228 &atmel_usart2_usart,
2229 &atmel_usart3_usart,
9a1e8eb1 2230 &atmel_pwm0_mck,
438ff3f3 2231#if defined(CONFIG_CPU_AT32AP7000)
5f97f7f9
HS
2232 &macb0_hclk,
2233 &macb0_pclk,
cfcb3a89
HS
2234 &macb1_hclk,
2235 &macb1_pclk,
438ff3f3 2236#endif
3d60ee1b
HS
2237 &atmel_spi0_spi_clk,
2238 &atmel_spi1_spi_clk,
2042c1c4
HS
2239 &atmel_twi0_pclk,
2240 &atmel_mci0_pclk,
438ff3f3 2241#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
d0a2b7af
HS
2242 &atmel_lcdfb0_hck1,
2243 &atmel_lcdfb0_pixclk,
438ff3f3 2244#endif
9cf6cf58
HCE
2245 &ssc0_pclk,
2246 &ssc1_pclk,
2247 &ssc2_pclk,
6fcf0615
HS
2248 &usba0_hclk,
2249 &usba0_pclk,
2042c1c4
HS
2250 &atmel_ac97c0_pclk,
2251 &abdac0_pclk,
2252 &abdac0_sample_clk,
7a5fe238
HS
2253 &gclk0,
2254 &gclk1,
2255 &gclk2,
2256 &gclk3,
2257 &gclk4,
5f97f7f9 2258};
5f97f7f9 2259
65033ed7 2260void __init setup_platform(void)
5f97f7f9 2261{
5f97f7f9
HS
2262 u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
2263 int i;
2264
9e58e185 2265 if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
5f97f7f9 2266 main_clock = &pll0;
9e58e185
HCE
2267 cpu_clk.parent = &pll0;
2268 } else {
5f97f7f9 2269 main_clock = &osc0;
9e58e185
HCE
2270 cpu_clk.parent = &osc0;
2271 }
5f97f7f9 2272
7a5b8059 2273 if (pm_readl(PLL0) & PM_BIT(PLLOSC))
5f97f7f9 2274 pll0.parent = &osc1;
7a5b8059 2275 if (pm_readl(PLL1) & PM_BIT(PLLOSC))
5f97f7f9
HS
2276 pll1.parent = &osc1;
2277
7a5fe238
HS
2278 genclk_init_parent(&gclk0);
2279 genclk_init_parent(&gclk1);
2280 genclk_init_parent(&gclk2);
2281 genclk_init_parent(&gclk3);
2282 genclk_init_parent(&gclk4);
438ff3f3 2283#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
d0a2b7af 2284 genclk_init_parent(&atmel_lcdfb0_pixclk);
438ff3f3 2285#endif
2042c1c4 2286 genclk_init_parent(&abdac0_sample_clk);
7a5fe238 2287
5f97f7f9 2288 /*
300bb762
AR
2289 * Build initial dynamic clock list by registering all clocks
2290 * from the array.
2291 * At the same time, turn on all clocks that have at least one
2292 * user already, and turn off everything else. We only do this
2293 * for module clocks, and even though it isn't particularly
2294 * pretty to check the address of the mode function, it should
2295 * do the trick...
5f97f7f9 2296 */
300bb762
AR
2297 for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
2298 struct clk *clk = init_clocks[i];
2299
2300 /* first, register clock */
2301 at32_clk_register(clk);
5f97f7f9 2302
188ff65d
HS
2303 if (clk->users == 0)
2304 continue;
2305
5f97f7f9
HS
2306 if (clk->mode == &cpu_clk_mode)
2307 cpu_mask |= 1 << clk->index;
2308 else if (clk->mode == &hsb_clk_mode)
2309 hsb_mask |= 1 << clk->index;
2310 else if (clk->mode == &pba_clk_mode)
2311 pba_mask |= 1 << clk->index;
2312 else if (clk->mode == &pbb_clk_mode)
2313 pbb_mask |= 1 << clk->index;
2314 }
2315
7a5b8059
HS
2316 pm_writel(CPU_MASK, cpu_mask);
2317 pm_writel(HSB_MASK, hsb_mask);
2318 pm_writel(PBA_MASK, pba_mask);
2319 pm_writel(PBB_MASK, pbb_mask);
65033ed7
HS
2320
2321 /* Initialize the port muxes */
2322 at32_init_pio(&pio0_device);
2323 at32_init_pio(&pio1_device);
2324 at32_init_pio(&pio2_device);
2325 at32_init_pio(&pio3_device);
2326 at32_init_pio(&pio4_device);
5f97f7f9 2327}
b83d6ee1
HS
2328
2329struct gen_pool *sram_pool;
2330
2331static int __init sram_init(void)
2332{
2333 struct gen_pool *pool;
2334
2335 /* 1KiB granularity */
2336 pool = gen_pool_create(10, -1);
2337 if (!pool)
2338 goto fail;
2339
2340 if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
2341 goto err_pool_add;
2342
2343 sram_pool = pool;
2344 return 0;
2345
2346err_pool_add:
2347 gen_pool_destroy(pool);
2348fail:
2349 pr_err("Failed to create SRAM pool\n");
2350 return -ENOMEM;
2351}
2352core_initcall(sram_init);
This page took 0.394903 seconds and 5 git commands to generate.