blob: 066252eebf614e98ffb96c1c58705cbb9e35840b [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
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>
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +01009#include <linux/delay.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070010#include <linux/dw_dmac.h>
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +010011#include <linux/fb.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070012#include <linux/init.h>
13#include <linux/platform_device.h>
David Brownell6b84bbf2007-06-22 19:17:57 -070014#include <linux/dma-mapping.h>
David Brownell3c26e172008-07-27 02:34:45 -070015#include <linux/gpio.h>
Haavard Skinnemoen41d8ca42007-02-16 13:56:11 +010016#include <linux/spi/spi.h>
Stelian Pop8d855312008-03-05 00:00:00 +010017#include <linux/usb/atmel_usba_udc.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070018
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020019#include <asm/atmel-mci.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070020#include <asm/io.h>
Haavard Skinnemoene7ba1762007-10-10 14:58:29 +020021#include <asm/irq.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070022
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020023#include <mach/at32ap700x.h>
24#include <mach/board.h>
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +020025#include <mach/hmatrix.h>
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020026#include <mach/portmux.h>
27#include <mach/sram.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070028
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +010029#include <video/atmel_lcdc.h>
30
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070031#include "clock.h"
32#include "pio.h"
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +020033#include "pm.h"
34
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070035
36#define PBMEM(base) \
37 { \
38 .start = base, \
39 .end = base + 0x3ff, \
40 .flags = IORESOURCE_MEM, \
41 }
42#define IRQ(num) \
43 { \
44 .start = num, \
45 .end = num, \
46 .flags = IORESOURCE_IRQ, \
47 }
48#define NAMED_IRQ(num, _name) \
49 { \
50 .start = num, \
51 .end = num, \
52 .name = _name, \
53 .flags = IORESOURCE_IRQ, \
54 }
55
David Brownell6b84bbf2007-06-22 19:17:57 -070056/* REVISIT these assume *every* device supports DMA, but several
57 * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
58 */
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070059#define DEFINE_DEV(_name, _id) \
David Brownell6b84bbf2007-06-22 19:17:57 -070060static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070061static struct platform_device _name##_id##_device = { \
62 .name = #_name, \
63 .id = _id, \
64 .dev = { \
David Brownell6b84bbf2007-06-22 19:17:57 -070065 .dma_mask = &_name##_id##_dma_mask, \
66 .coherent_dma_mask = DMA_32BIT_MASK, \
67 }, \
68 .resource = _name##_id##_resource, \
69 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
70}
71#define DEFINE_DEV_DATA(_name, _id) \
72static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
73static struct platform_device _name##_id##_device = { \
74 .name = #_name, \
75 .id = _id, \
76 .dev = { \
77 .dma_mask = &_name##_id##_dma_mask, \
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070078 .platform_data = &_name##_id##_data, \
David Brownell6b84bbf2007-06-22 19:17:57 -070079 .coherent_dma_mask = DMA_32BIT_MASK, \
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070080 }, \
81 .resource = _name##_id##_resource, \
82 .num_resources = ARRAY_SIZE(_name##_id##_resource), \
83}
84
Julien Maycaf18f12008-09-24 10:30:47 +020085#define select_peripheral(port, pin_mask, periph, flags) \
86 at32_select_periph(GPIO_##port##_BASE, pin_mask, \
87 GPIO_##periph, flags)
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010088
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070089#define DEV_CLK(_name, devname, bus, _index) \
90static struct clk devname##_##_name = { \
91 .name = #_name, \
92 .dev = &devname##_device.dev, \
93 .parent = &bus##_clk, \
94 .mode = bus##_clk_mode, \
95 .get_rate = bus##_clk_get_rate, \
96 .index = _index, \
97}
98
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +020099static DEFINE_SPINLOCK(pm_lock);
100
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +0100101static struct clk osc0;
102static struct clk osc1;
103
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700104static unsigned long osc_get_rate(struct clk *clk)
105{
Alex60ed7952008-03-17 14:55:06 +0100106 return at32_board_osc_rates[clk->index];
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700107}
108
109static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
110{
111 unsigned long div, mul, rate;
112
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200113 div = PM_BFEXT(PLLDIV, control) + 1;
114 mul = PM_BFEXT(PLLMUL, control) + 1;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700115
116 rate = clk->parent->get_rate(clk->parent);
117 rate = (rate + div / 2) / div;
118 rate *= mul;
119
120 return rate;
121}
122
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +0100123static long pll_set_rate(struct clk *clk, unsigned long rate,
124 u32 *pll_ctrl)
125{
126 unsigned long mul;
127 unsigned long mul_best_fit = 0;
128 unsigned long div;
129 unsigned long div_min;
130 unsigned long div_max;
131 unsigned long div_best_fit = 0;
132 unsigned long base;
133 unsigned long pll_in;
134 unsigned long actual = 0;
135 unsigned long rate_error;
136 unsigned long rate_error_prev = ~0UL;
137 u32 ctrl;
138
139 /* Rate must be between 80 MHz and 200 Mhz. */
140 if (rate < 80000000UL || rate > 200000000UL)
141 return -EINVAL;
142
143 ctrl = PM_BF(PLLOPT, 4);
144 base = clk->parent->get_rate(clk->parent);
145
146 /* PLL input frequency must be between 6 MHz and 32 MHz. */
147 div_min = DIV_ROUND_UP(base, 32000000UL);
148 div_max = base / 6000000UL;
149
150 if (div_max < div_min)
151 return -EINVAL;
152
153 for (div = div_min; div <= div_max; div++) {
154 pll_in = (base + div / 2) / div;
155 mul = (rate + pll_in / 2) / pll_in;
156
157 if (mul == 0)
158 continue;
159
160 actual = pll_in * mul;
161 rate_error = abs(actual - rate);
162
163 if (rate_error < rate_error_prev) {
164 mul_best_fit = mul;
165 div_best_fit = div;
166 rate_error_prev = rate_error;
167 }
168
169 if (rate_error == 0)
170 break;
171 }
172
173 if (div_best_fit == 0)
174 return -EINVAL;
175
176 ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
177 ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
178 ctrl |= PM_BF(PLLCOUNT, 16);
179
180 if (clk->parent == &osc1)
181 ctrl |= PM_BIT(PLLOSC);
182
183 *pll_ctrl = ctrl;
184
185 return actual;
186}
187
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700188static unsigned long pll0_get_rate(struct clk *clk)
189{
190 u32 control;
191
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200192 control = pm_readl(PLL0);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700193
194 return pll_get_rate(clk, control);
195}
196
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +0100197static void pll1_mode(struct clk *clk, int enabled)
198{
199 unsigned long timeout;
200 u32 status;
201 u32 ctrl;
202
203 ctrl = pm_readl(PLL1);
204
205 if (enabled) {
206 if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
207 pr_debug("clk %s: failed to enable, rate not set\n",
208 clk->name);
209 return;
210 }
211
212 ctrl |= PM_BIT(PLLEN);
213 pm_writel(PLL1, ctrl);
214
215 /* Wait for PLL lock. */
216 for (timeout = 10000; timeout; timeout--) {
217 status = pm_readl(ISR);
218 if (status & PM_BIT(LOCK1))
219 break;
220 udelay(10);
221 }
222
223 if (!(status & PM_BIT(LOCK1)))
224 printk(KERN_ERR "clk %s: timeout waiting for lock\n",
225 clk->name);
226 } else {
227 ctrl &= ~PM_BIT(PLLEN);
228 pm_writel(PLL1, ctrl);
229 }
230}
231
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700232static unsigned long pll1_get_rate(struct clk *clk)
233{
234 u32 control;
235
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200236 control = pm_readl(PLL1);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700237
238 return pll_get_rate(clk, control);
239}
240
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +0100241static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
242{
243 u32 ctrl = 0;
244 unsigned long actual_rate;
245
246 actual_rate = pll_set_rate(clk, rate, &ctrl);
247
248 if (apply) {
249 if (actual_rate != rate)
250 return -EINVAL;
251 if (clk->users > 0)
252 return -EBUSY;
253 pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
254 clk->name, rate, actual_rate);
255 pm_writel(PLL1, ctrl);
256 }
257
258 return actual_rate;
259}
260
261static int pll1_set_parent(struct clk *clk, struct clk *parent)
262{
263 u32 ctrl;
264
265 if (clk->users > 0)
266 return -EBUSY;
267
268 ctrl = pm_readl(PLL1);
269 WARN_ON(ctrl & PM_BIT(PLLEN));
270
271 if (parent == &osc0)
272 ctrl &= ~PM_BIT(PLLOSC);
273 else if (parent == &osc1)
274 ctrl |= PM_BIT(PLLOSC);
275 else
276 return -EINVAL;
277
278 pm_writel(PLL1, ctrl);
279 clk->parent = parent;
280
281 return 0;
282}
283
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700284/*
285 * The AT32AP7000 has five primary clock sources: One 32kHz
286 * oscillator, two crystal oscillators and two PLLs.
287 */
288static struct clk osc32k = {
289 .name = "osc32k",
290 .get_rate = osc_get_rate,
291 .users = 1,
292 .index = 0,
293};
294static struct clk osc0 = {
295 .name = "osc0",
296 .get_rate = osc_get_rate,
297 .users = 1,
298 .index = 1,
299};
300static struct clk osc1 = {
301 .name = "osc1",
302 .get_rate = osc_get_rate,
303 .index = 2,
304};
305static struct clk pll0 = {
306 .name = "pll0",
307 .get_rate = pll0_get_rate,
308 .parent = &osc0,
309};
310static struct clk pll1 = {
311 .name = "pll1",
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +0100312 .mode = pll1_mode,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700313 .get_rate = pll1_get_rate,
Hans-Christian Egtvedt35bf50c2007-12-19 09:29:19 +0100314 .set_rate = pll1_set_rate,
315 .set_parent = pll1_set_parent,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700316 .parent = &osc0,
317};
318
319/*
320 * The main clock can be either osc0 or pll0. The boot loader may
321 * have chosen one for us, so we don't really know which one until we
322 * have a look at the SM.
323 */
324static struct clk *main_clock;
325
326/*
327 * Synchronous clocks are generated from the main clock. The clocks
328 * must satisfy the constraint
329 * fCPU >= fHSB >= fPB
330 * i.e. each clock must not be faster than its parent.
331 */
332static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
333{
334 return main_clock->get_rate(main_clock) >> shift;
335};
336
337static void cpu_clk_mode(struct clk *clk, int enabled)
338{
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700339 unsigned long flags;
340 u32 mask;
341
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200342 spin_lock_irqsave(&pm_lock, flags);
343 mask = pm_readl(CPU_MASK);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700344 if (enabled)
345 mask |= 1 << clk->index;
346 else
347 mask &= ~(1 << clk->index);
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200348 pm_writel(CPU_MASK, mask);
349 spin_unlock_irqrestore(&pm_lock, flags);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700350}
351
352static unsigned long cpu_clk_get_rate(struct clk *clk)
353{
354 unsigned long cksel, shift = 0;
355
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200356 cksel = pm_readl(CKSEL);
357 if (cksel & PM_BIT(CPUDIV))
358 shift = PM_BFEXT(CPUSEL, cksel) + 1;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700359
360 return bus_clk_get_rate(clk, shift);
361}
362
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200363static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
364{
365 u32 control;
366 unsigned long parent_rate, child_div, actual_rate, div;
367
368 parent_rate = clk->parent->get_rate(clk->parent);
369 control = pm_readl(CKSEL);
370
371 if (control & PM_BIT(HSBDIV))
372 child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
373 else
374 child_div = 1;
375
376 if (rate > 3 * (parent_rate / 4) || child_div == 1) {
377 actual_rate = parent_rate;
378 control &= ~PM_BIT(CPUDIV);
379 } else {
380 unsigned int cpusel;
381 div = (parent_rate + rate / 2) / rate;
382 if (div > child_div)
383 div = child_div;
384 cpusel = (div > 1) ? (fls(div) - 2) : 0;
385 control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
386 actual_rate = parent_rate / (1 << (cpusel + 1));
387 }
388
389 pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
390 clk->name, rate, actual_rate);
391
392 if (apply)
393 pm_writel(CKSEL, control);
394
395 return actual_rate;
396}
397
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700398static void hsb_clk_mode(struct clk *clk, int enabled)
399{
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700400 unsigned long flags;
401 u32 mask;
402
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200403 spin_lock_irqsave(&pm_lock, flags);
404 mask = pm_readl(HSB_MASK);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700405 if (enabled)
406 mask |= 1 << clk->index;
407 else
408 mask &= ~(1 << clk->index);
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200409 pm_writel(HSB_MASK, mask);
410 spin_unlock_irqrestore(&pm_lock, flags);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700411}
412
413static unsigned long hsb_clk_get_rate(struct clk *clk)
414{
415 unsigned long cksel, shift = 0;
416
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200417 cksel = pm_readl(CKSEL);
418 if (cksel & PM_BIT(HSBDIV))
419 shift = PM_BFEXT(HSBSEL, cksel) + 1;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700420
421 return bus_clk_get_rate(clk, shift);
422}
423
424static void pba_clk_mode(struct clk *clk, int enabled)
425{
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700426 unsigned long flags;
427 u32 mask;
428
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200429 spin_lock_irqsave(&pm_lock, flags);
430 mask = pm_readl(PBA_MASK);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700431 if (enabled)
432 mask |= 1 << clk->index;
433 else
434 mask &= ~(1 << clk->index);
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200435 pm_writel(PBA_MASK, mask);
436 spin_unlock_irqrestore(&pm_lock, flags);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700437}
438
439static unsigned long pba_clk_get_rate(struct clk *clk)
440{
441 unsigned long cksel, shift = 0;
442
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200443 cksel = pm_readl(CKSEL);
444 if (cksel & PM_BIT(PBADIV))
445 shift = PM_BFEXT(PBASEL, cksel) + 1;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700446
447 return bus_clk_get_rate(clk, shift);
448}
449
450static void pbb_clk_mode(struct clk *clk, int enabled)
451{
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700452 unsigned long flags;
453 u32 mask;
454
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200455 spin_lock_irqsave(&pm_lock, flags);
456 mask = pm_readl(PBB_MASK);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700457 if (enabled)
458 mask |= 1 << clk->index;
459 else
460 mask &= ~(1 << clk->index);
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200461 pm_writel(PBB_MASK, mask);
462 spin_unlock_irqrestore(&pm_lock, flags);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700463}
464
465static unsigned long pbb_clk_get_rate(struct clk *clk)
466{
467 unsigned long cksel, shift = 0;
468
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200469 cksel = pm_readl(CKSEL);
470 if (cksel & PM_BIT(PBBDIV))
471 shift = PM_BFEXT(PBBSEL, cksel) + 1;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700472
473 return bus_clk_get_rate(clk, shift);
474}
475
476static struct clk cpu_clk = {
477 .name = "cpu",
478 .get_rate = cpu_clk_get_rate,
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200479 .set_rate = cpu_clk_set_rate,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700480 .users = 1,
481};
482static struct clk hsb_clk = {
483 .name = "hsb",
484 .parent = &cpu_clk,
485 .get_rate = hsb_clk_get_rate,
486};
487static struct clk pba_clk = {
488 .name = "pba",
489 .parent = &hsb_clk,
490 .mode = hsb_clk_mode,
491 .get_rate = pba_clk_get_rate,
492 .index = 1,
493};
494static struct clk pbb_clk = {
495 .name = "pbb",
496 .parent = &hsb_clk,
497 .mode = hsb_clk_mode,
498 .get_rate = pbb_clk_get_rate,
499 .users = 1,
500 .index = 2,
501};
502
503/* --------------------------------------------------------------------
504 * Generic Clock operations
505 * -------------------------------------------------------------------- */
506
507static void genclk_mode(struct clk *clk, int enabled)
508{
509 u32 control;
510
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200511 control = pm_readl(GCCTRL(clk->index));
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700512 if (enabled)
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200513 control |= PM_BIT(CEN);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700514 else
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200515 control &= ~PM_BIT(CEN);
516 pm_writel(GCCTRL(clk->index), control);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700517}
518
519static unsigned long genclk_get_rate(struct clk *clk)
520{
521 u32 control;
522 unsigned long div = 1;
523
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200524 control = pm_readl(GCCTRL(clk->index));
525 if (control & PM_BIT(DIVEN))
526 div = 2 * (PM_BFEXT(DIV, control) + 1);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700527
528 return clk->parent->get_rate(clk->parent) / div;
529}
530
531static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
532{
533 u32 control;
534 unsigned long parent_rate, actual_rate, div;
535
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700536 parent_rate = clk->parent->get_rate(clk->parent);
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200537 control = pm_readl(GCCTRL(clk->index));
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700538
539 if (rate > 3 * parent_rate / 4) {
540 actual_rate = parent_rate;
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200541 control &= ~PM_BIT(DIVEN);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700542 } else {
543 div = (parent_rate + rate) / (2 * rate) - 1;
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200544 control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700545 actual_rate = parent_rate / (2 * (div + 1));
546 }
547
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200548 dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
549 clk->name, rate, actual_rate);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700550
551 if (apply)
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200552 pm_writel(GCCTRL(clk->index), control);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700553
554 return actual_rate;
555}
556
557int genclk_set_parent(struct clk *clk, struct clk *parent)
558{
559 u32 control;
560
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200561 dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
562 clk->name, parent->name, clk->parent->name);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700563
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200564 control = pm_readl(GCCTRL(clk->index));
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700565
566 if (parent == &osc1 || parent == &pll1)
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200567 control |= PM_BIT(OSCSEL);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700568 else if (parent == &osc0 || parent == &pll0)
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200569 control &= ~PM_BIT(OSCSEL);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700570 else
571 return -EINVAL;
572
573 if (parent == &pll0 || parent == &pll1)
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200574 control |= PM_BIT(PLLSEL);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700575 else
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200576 control &= ~PM_BIT(PLLSEL);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700577
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200578 pm_writel(GCCTRL(clk->index), control);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700579 clk->parent = parent;
580
581 return 0;
582}
583
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +0100584static void __init genclk_init_parent(struct clk *clk)
585{
586 u32 control;
587 struct clk *parent;
588
589 BUG_ON(clk->index > 7);
590
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200591 control = pm_readl(GCCTRL(clk->index));
592 if (control & PM_BIT(OSCSEL))
593 parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +0100594 else
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200595 parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +0100596
597 clk->parent = parent;
598}
599
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700600static struct dw_dma_platform_data dw_dmac0_data = {
601 .nr_channels = 3,
602};
603
604static struct resource dw_dmac0_resource[] = {
605 PBMEM(0xff200000),
606 IRQ(2),
607};
608DEFINE_DEV_DATA(dw_dmac, 0);
609DEV_CLK(hclk, dw_dmac0, hsb, 10);
610
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700611/* --------------------------------------------------------------------
612 * System peripherals
613 * -------------------------------------------------------------------- */
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200614static struct resource at32_pm0_resource[] = {
615 {
616 .start = 0xfff00000,
617 .end = 0xfff0007f,
618 .flags = IORESOURCE_MEM,
619 },
620 IRQ(20),
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700621};
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200622
623static struct resource at32ap700x_rtc0_resource[] = {
624 {
625 .start = 0xfff00080,
626 .end = 0xfff000af,
627 .flags = IORESOURCE_MEM,
628 },
629 IRQ(21),
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700630};
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200631
632static struct resource at32_wdt0_resource[] = {
633 {
634 .start = 0xfff000b0,
Hans-Christian Egtvedt9797bed2007-10-30 14:29:50 +0100635 .end = 0xfff000cf,
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200636 .flags = IORESOURCE_MEM,
637 },
638};
639
640static struct resource at32_eic0_resource[] = {
641 {
642 .start = 0xfff00100,
643 .end = 0xfff0013f,
644 .flags = IORESOURCE_MEM,
645 },
646 IRQ(19),
647};
648
649DEFINE_DEV(at32_pm, 0);
650DEFINE_DEV(at32ap700x_rtc, 0);
651DEFINE_DEV(at32_wdt, 0);
652DEFINE_DEV(at32_eic, 0);
653
654/*
655 * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
656 * is always running.
657 */
658static struct clk at32_pm_pclk = {
Haavard Skinnemoen188ff652007-03-14 13:23:44 +0100659 .name = "pclk",
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200660 .dev = &at32_pm0_device.dev,
Haavard Skinnemoen188ff652007-03-14 13:23:44 +0100661 .parent = &pbb_clk,
662 .mode = pbb_clk_mode,
663 .get_rate = pbb_clk_get_rate,
664 .users = 1,
665 .index = 0,
666};
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700667
668static struct resource intc0_resource[] = {
669 PBMEM(0xfff00400),
670};
671struct platform_device at32_intc0_device = {
672 .name = "intc",
673 .id = 0,
674 .resource = intc0_resource,
675 .num_resources = ARRAY_SIZE(intc0_resource),
676};
677DEV_CLK(pclk, at32_intc0, pbb, 1);
678
679static struct clk ebi_clk = {
680 .name = "ebi",
681 .parent = &hsb_clk,
682 .mode = hsb_clk_mode,
683 .get_rate = hsb_clk_get_rate,
684 .users = 1,
685};
686static struct clk hramc_clk = {
687 .name = "hramc",
688 .parent = &hsb_clk,
689 .mode = hsb_clk_mode,
690 .get_rate = hsb_clk_get_rate,
691 .users = 1,
Haavard Skinnemoen188ff652007-03-14 13:23:44 +0100692 .index = 3,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700693};
Haavard Skinnemoen7951f182008-03-05 15:08:27 +0100694static struct clk sdramc_clk = {
695 .name = "sdramc_clk",
696 .parent = &pbb_clk,
697 .mode = pbb_clk_mode,
698 .get_rate = pbb_clk_get_rate,
699 .users = 1,
700 .index = 14,
701};
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700702
Haavard Skinnemoenbc157b72006-09-25 23:32:16 -0700703static struct resource smc0_resource[] = {
704 PBMEM(0xfff03400),
705};
706DEFINE_DEV(smc, 0);
707DEV_CLK(pclk, smc0, pbb, 13);
708DEV_CLK(mck, smc0, hsb, 0);
709
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700710static struct platform_device pdc_device = {
711 .name = "pdc",
712 .id = 0,
713};
714DEV_CLK(hclk, pdc, hsb, 4);
715DEV_CLK(pclk, pdc, pba, 16);
716
717static struct clk pico_clk = {
718 .name = "pico",
719 .parent = &cpu_clk,
720 .mode = cpu_clk_mode,
721 .get_rate = cpu_clk_get_rate,
722 .users = 1,
723};
724
725/* --------------------------------------------------------------------
Haavard Skinnemoen9c8f8e72007-02-01 16:34:10 +0100726 * HMATRIX
727 * -------------------------------------------------------------------- */
728
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +0200729struct clk at32_hmatrix_clk = {
Haavard Skinnemoen9c8f8e72007-02-01 16:34:10 +0100730 .name = "hmatrix_clk",
731 .parent = &pbb_clk,
732 .mode = pbb_clk_mode,
733 .get_rate = pbb_clk_get_rate,
734 .index = 2,
735 .users = 1,
736};
Haavard Skinnemoen9c8f8e72007-02-01 16:34:10 +0100737
738/*
739 * Set bits in the HMATRIX Special Function Register (SFR) used by the
740 * External Bus Interface (EBI). This can be used to enable special
741 * features like CompactFlash support, NAND Flash support, etc. on
742 * certain chipselects.
743 */
744static inline void set_ebi_sfr_bits(u32 mask)
745{
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +0200746 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);
Haavard Skinnemoen9c8f8e72007-02-01 16:34:10 +0100747}
748
749/* --------------------------------------------------------------------
David Brownelle723ff62008-02-14 11:24:02 -0800750 * Timer/Counter (TC)
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100751 * -------------------------------------------------------------------- */
David Brownelle723ff62008-02-14 11:24:02 -0800752
753static struct resource at32_tcb0_resource[] = {
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100754 PBMEM(0xfff00c00),
755 IRQ(22),
756};
David Brownelle723ff62008-02-14 11:24:02 -0800757static struct platform_device at32_tcb0_device = {
758 .name = "atmel_tcb",
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100759 .id = 0,
David Brownelle723ff62008-02-14 11:24:02 -0800760 .resource = at32_tcb0_resource,
761 .num_resources = ARRAY_SIZE(at32_tcb0_resource),
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100762};
David Brownelle723ff62008-02-14 11:24:02 -0800763DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
764
765static struct resource at32_tcb1_resource[] = {
766 PBMEM(0xfff01000),
767 IRQ(23),
768};
769static struct platform_device at32_tcb1_device = {
770 .name = "atmel_tcb",
771 .id = 1,
772 .resource = at32_tcb1_resource,
773 .num_resources = ARRAY_SIZE(at32_tcb1_resource),
774};
775DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100776
777/* --------------------------------------------------------------------
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700778 * PIO
779 * -------------------------------------------------------------------- */
780
781static struct resource pio0_resource[] = {
782 PBMEM(0xffe02800),
783 IRQ(13),
784};
785DEFINE_DEV(pio, 0);
786DEV_CLK(mck, pio0, pba, 10);
787
788static struct resource pio1_resource[] = {
789 PBMEM(0xffe02c00),
790 IRQ(14),
791};
792DEFINE_DEV(pio, 1);
793DEV_CLK(mck, pio1, pba, 11);
794
795static struct resource pio2_resource[] = {
796 PBMEM(0xffe03000),
797 IRQ(15),
798};
799DEFINE_DEV(pio, 2);
800DEV_CLK(mck, pio2, pba, 12);
801
802static struct resource pio3_resource[] = {
803 PBMEM(0xffe03400),
804 IRQ(16),
805};
806DEFINE_DEV(pio, 3);
807DEV_CLK(mck, pio3, pba, 13);
808
Haavard Skinnemoen7f9f4672007-01-30 11:16:16 +0100809static struct resource pio4_resource[] = {
810 PBMEM(0xffe03800),
811 IRQ(17),
812};
813DEFINE_DEV(pio, 4);
814DEV_CLK(mck, pio4, pba, 14);
815
Haavard Skinnemoene82c6102008-10-23 14:42:19 +0200816static int __init system_device_init(void)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700817{
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200818 platform_device_register(&at32_pm0_device);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700819 platform_device_register(&at32_intc0_device);
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +0200820 platform_device_register(&at32ap700x_rtc0_device);
821 platform_device_register(&at32_wdt0_device);
822 platform_device_register(&at32_eic0_device);
Haavard Skinnemoenbc157b72006-09-25 23:32:16 -0700823 platform_device_register(&smc0_device);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700824 platform_device_register(&pdc_device);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700825 platform_device_register(&dw_dmac0_device);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700826
David Brownelle723ff62008-02-14 11:24:02 -0800827 platform_device_register(&at32_tcb0_device);
828 platform_device_register(&at32_tcb1_device);
Hans-Christian Egtvedt77609892007-03-12 18:15:16 +0100829
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700830 platform_device_register(&pio0_device);
831 platform_device_register(&pio1_device);
832 platform_device_register(&pio2_device);
833 platform_device_register(&pio3_device);
Haavard Skinnemoen7f9f4672007-01-30 11:16:16 +0100834 platform_device_register(&pio4_device);
Haavard Skinnemoene82c6102008-10-23 14:42:19 +0200835
836 return 0;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700837}
Haavard Skinnemoene82c6102008-10-23 14:42:19 +0200838core_initcall(system_device_init);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700839
840/* --------------------------------------------------------------------
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +0100841 * PSIF
842 * -------------------------------------------------------------------- */
843static struct resource atmel_psif0_resource[] __initdata = {
844 {
845 .start = 0xffe03c00,
846 .end = 0xffe03cff,
847 .flags = IORESOURCE_MEM,
848 },
849 IRQ(18),
850};
851static struct clk atmel_psif0_pclk = {
852 .name = "pclk",
853 .parent = &pba_clk,
854 .mode = pba_clk_mode,
855 .get_rate = pba_clk_get_rate,
856 .index = 15,
857};
858
859static struct resource atmel_psif1_resource[] __initdata = {
860 {
861 .start = 0xffe03d00,
862 .end = 0xffe03dff,
863 .flags = IORESOURCE_MEM,
864 },
865 IRQ(18),
866};
867static struct clk atmel_psif1_pclk = {
868 .name = "pclk",
869 .parent = &pba_clk,
870 .mode = pba_clk_mode,
871 .get_rate = pba_clk_get_rate,
872 .index = 15,
873};
874
875struct platform_device *__init at32_add_device_psif(unsigned int id)
876{
877 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +0200878 u32 pin_mask;
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +0100879
880 if (!(id == 0 || id == 1))
881 return NULL;
882
883 pdev = platform_device_alloc("atmel_psif", id);
884 if (!pdev)
885 return NULL;
886
887 switch (id) {
888 case 0:
Julien Maycaf18f12008-09-24 10:30:47 +0200889 pin_mask = (1 << 8) | (1 << 9); /* CLOCK & DATA */
890
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +0100891 if (platform_device_add_resources(pdev, atmel_psif0_resource,
892 ARRAY_SIZE(atmel_psif0_resource)))
893 goto err_add_resources;
894 atmel_psif0_pclk.dev = &pdev->dev;
Julien Maycaf18f12008-09-24 10:30:47 +0200895 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +0100896 break;
897 case 1:
Julien Maycaf18f12008-09-24 10:30:47 +0200898 pin_mask = (1 << 11) | (1 << 12); /* CLOCK & DATA */
899
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +0100900 if (platform_device_add_resources(pdev, atmel_psif1_resource,
901 ARRAY_SIZE(atmel_psif1_resource)))
902 goto err_add_resources;
903 atmel_psif1_pclk.dev = &pdev->dev;
Julien Maycaf18f12008-09-24 10:30:47 +0200904 select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +0100905 break;
906 default:
907 return NULL;
908 }
909
910 platform_device_add(pdev);
911 return pdev;
912
913err_add_resources:
914 platform_device_put(pdev);
915 return NULL;
916}
917
918/* --------------------------------------------------------------------
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700919 * USART
920 * -------------------------------------------------------------------- */
921
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200922static struct atmel_uart_data atmel_usart0_data = {
923 .use_dma_tx = 1,
924 .use_dma_rx = 1,
925};
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200926static struct resource atmel_usart0_resource[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700927 PBMEM(0xffe00c00),
David Brownella3d912c82007-01-23 20:14:02 -0800928 IRQ(6),
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700929};
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200930DEFINE_DEV_DATA(atmel_usart, 0);
ben.nizette@iinet.net.au80f76c52007-11-07 16:16:22 +0900931DEV_CLK(usart, atmel_usart0, pba, 3);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700932
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200933static struct atmel_uart_data atmel_usart1_data = {
934 .use_dma_tx = 1,
935 .use_dma_rx = 1,
936};
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200937static struct resource atmel_usart1_resource[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700938 PBMEM(0xffe01000),
939 IRQ(7),
940};
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200941DEFINE_DEV_DATA(atmel_usart, 1);
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200942DEV_CLK(usart, atmel_usart1, pba, 4);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700943
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200944static struct atmel_uart_data atmel_usart2_data = {
945 .use_dma_tx = 1,
946 .use_dma_rx = 1,
947};
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200948static struct resource atmel_usart2_resource[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700949 PBMEM(0xffe01400),
950 IRQ(8),
951};
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200952DEFINE_DEV_DATA(atmel_usart, 2);
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200953DEV_CLK(usart, atmel_usart2, pba, 5);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700954
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200955static struct atmel_uart_data atmel_usart3_data = {
956 .use_dma_tx = 1,
957 .use_dma_rx = 1,
958};
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200959static struct resource atmel_usart3_resource[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700960 PBMEM(0xffe01800),
961 IRQ(9),
962};
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200963DEFINE_DEV_DATA(atmel_usart, 3);
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +0200964DEV_CLK(usart, atmel_usart3, pba, 6);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700965
966static inline void configure_usart0_pins(void)
967{
Julien Maycaf18f12008-09-24 10:30:47 +0200968 u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
969
Anders Blomdell10546262008-10-24 14:54:08 +0200970 select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700971}
972
973static inline void configure_usart1_pins(void)
974{
Julien Maycaf18f12008-09-24 10:30:47 +0200975 u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
976
Anders Blomdell10546262008-10-24 14:54:08 +0200977 select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700978}
979
980static inline void configure_usart2_pins(void)
981{
Julien Maycaf18f12008-09-24 10:30:47 +0200982 u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
983
Anders Blomdell10546262008-10-24 14:54:08 +0200984 select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700985}
986
987static inline void configure_usart3_pins(void)
988{
Julien Maycaf18f12008-09-24 10:30:47 +0200989 u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
990
Anders Blomdell10546262008-10-24 14:54:08 +0200991 select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700992}
993
David Brownella3d912c82007-01-23 20:14:02 -0800994static struct platform_device *__initdata at32_usarts[4];
Haavard Skinnemoenc1945882006-10-04 16:02:10 +0200995
996void __init at32_map_usart(unsigned int hw_id, unsigned int line)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700997{
998 struct platform_device *pdev;
999
Haavard Skinnemoenc1945882006-10-04 16:02:10 +02001000 switch (hw_id) {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001001 case 0:
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001002 pdev = &atmel_usart0_device;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001003 configure_usart0_pins();
1004 break;
1005 case 1:
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001006 pdev = &atmel_usart1_device;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001007 configure_usart1_pins();
1008 break;
1009 case 2:
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001010 pdev = &atmel_usart2_device;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001011 configure_usart2_pins();
1012 break;
1013 case 3:
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001014 pdev = &atmel_usart3_device;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001015 configure_usart3_pins();
1016 break;
1017 default:
Haavard Skinnemoenc1945882006-10-04 16:02:10 +02001018 return;
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02001019 }
1020
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;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001025 }
1026
Haavard Skinnemoenc1945882006-10-04 16:02:10 +02001027 pdev->id = line;
1028 at32_usarts[line] = pdev;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001029}
1030
1031struct platform_device *__init at32_add_device_usart(unsigned int id)
1032{
Haavard Skinnemoenc1945882006-10-04 16:02:10 +02001033 platform_device_register(at32_usarts[id]);
1034 return at32_usarts[id];
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001035}
1036
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02001037struct platform_device *atmel_default_console_device;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001038
1039void __init at32_setup_serial_console(unsigned int usart_id)
1040{
Haavard Skinnemoenc1945882006-10-04 16:02:10 +02001041 atmel_default_console_device = at32_usarts[usart_id];
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001042}
1043
1044/* --------------------------------------------------------------------
1045 * Ethernet
1046 * -------------------------------------------------------------------- */
1047
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01001048#ifdef CONFIG_CPU_AT32AP7000
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001049static struct eth_platform_data macb0_data;
1050static struct resource macb0_resource[] = {
1051 PBMEM(0xfff01800),
1052 IRQ(25),
1053};
1054DEFINE_DEV_DATA(macb, 0);
1055DEV_CLK(hclk, macb0, hsb, 8);
1056DEV_CLK(pclk, macb0, pbb, 6);
1057
Haavard Skinnemoencfcb3a82006-10-30 09:23:12 +01001058static struct eth_platform_data macb1_data;
1059static struct resource macb1_resource[] = {
1060 PBMEM(0xfff01c00),
1061 IRQ(26),
1062};
1063DEFINE_DEV_DATA(macb, 1);
1064DEV_CLK(hclk, macb1, hsb, 9);
1065DEV_CLK(pclk, macb1, pbb, 7);
1066
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001067struct platform_device *__init
1068at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
1069{
1070 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +02001071 u32 pin_mask;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001072
1073 switch (id) {
1074 case 0:
1075 pdev = &macb0_device;
1076
Julien Maycaf18f12008-09-24 10:30:47 +02001077 pin_mask = (1 << 3); /* TXD0 */
1078 pin_mask |= (1 << 4); /* TXD1 */
1079 pin_mask |= (1 << 7); /* TXEN */
1080 pin_mask |= (1 << 8); /* TXCK */
1081 pin_mask |= (1 << 9); /* RXD0 */
1082 pin_mask |= (1 << 10); /* RXD1 */
1083 pin_mask |= (1 << 13); /* RXER */
1084 pin_mask |= (1 << 15); /* RXDV */
1085 pin_mask |= (1 << 16); /* MDC */
1086 pin_mask |= (1 << 17); /* MDIO */
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001087
1088 if (!data->is_rmii) {
Julien Maycaf18f12008-09-24 10:30:47 +02001089 pin_mask |= (1 << 0); /* COL */
1090 pin_mask |= (1 << 1); /* CRS */
1091 pin_mask |= (1 << 2); /* TXER */
1092 pin_mask |= (1 << 5); /* TXD2 */
1093 pin_mask |= (1 << 6); /* TXD3 */
1094 pin_mask |= (1 << 11); /* RXD2 */
1095 pin_mask |= (1 << 12); /* RXD3 */
1096 pin_mask |= (1 << 14); /* RXCK */
Mark Jackson198f2932008-10-13 10:46:27 +00001097#ifndef CONFIG_BOARD_MIMC200
Julien Maycaf18f12008-09-24 10:30:47 +02001098 pin_mask |= (1 << 18); /* SPD */
Mark Jackson198f2932008-10-13 10:46:27 +00001099#endif
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001100 }
Julien Maycaf18f12008-09-24 10:30:47 +02001101
1102 select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
1103
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001104 break;
1105
Haavard Skinnemoencfcb3a82006-10-30 09:23:12 +01001106 case 1:
1107 pdev = &macb1_device;
1108
Julien Maycaf18f12008-09-24 10:30:47 +02001109 pin_mask = (1 << 13); /* TXD0 */
1110 pin_mask |= (1 << 14); /* TXD1 */
1111 pin_mask |= (1 << 11); /* TXEN */
1112 pin_mask |= (1 << 12); /* TXCK */
1113 pin_mask |= (1 << 10); /* RXD0 */
1114 pin_mask |= (1 << 6); /* RXD1 */
1115 pin_mask |= (1 << 5); /* RXER */
1116 pin_mask |= (1 << 4); /* RXDV */
1117 pin_mask |= (1 << 3); /* MDC */
1118 pin_mask |= (1 << 2); /* MDIO */
1119
Mark Jackson198f2932008-10-13 10:46:27 +00001120#ifndef CONFIG_BOARD_MIMC200
Julien Maycaf18f12008-09-24 10:30:47 +02001121 if (!data->is_rmii)
1122 pin_mask |= (1 << 15); /* SPD */
Mark Jackson198f2932008-10-13 10:46:27 +00001123#endif
Julien Maycaf18f12008-09-24 10:30:47 +02001124
1125 select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
Haavard Skinnemoencfcb3a82006-10-30 09:23:12 +01001126
1127 if (!data->is_rmii) {
Julien Maycaf18f12008-09-24 10:30:47 +02001128 pin_mask = (1 << 19); /* COL */
1129 pin_mask |= (1 << 23); /* CRS */
1130 pin_mask |= (1 << 26); /* TXER */
1131 pin_mask |= (1 << 27); /* TXD2 */
1132 pin_mask |= (1 << 28); /* TXD3 */
1133 pin_mask |= (1 << 29); /* RXD2 */
1134 pin_mask |= (1 << 30); /* RXD3 */
1135 pin_mask |= (1 << 24); /* RXCK */
1136
1137 select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
Haavard Skinnemoencfcb3a82006-10-30 09:23:12 +01001138 }
1139 break;
1140
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001141 default:
1142 return NULL;
1143 }
1144
1145 memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
1146 platform_device_register(pdev);
1147
1148 return pdev;
1149}
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01001150#endif
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001151
1152/* --------------------------------------------------------------------
1153 * SPI
1154 * -------------------------------------------------------------------- */
Haavard Skinnemoen3d60ee12007-01-10 20:20:02 +01001155static struct resource atmel_spi0_resource[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001156 PBMEM(0xffe00000),
1157 IRQ(3),
1158};
Haavard Skinnemoen3d60ee12007-01-10 20:20:02 +01001159DEFINE_DEV(atmel_spi, 0);
1160DEV_CLK(spi_clk, atmel_spi0, pba, 0);
1161
1162static struct resource atmel_spi1_resource[] = {
1163 PBMEM(0xffe00400),
1164 IRQ(4),
1165};
1166DEFINE_DEV(atmel_spi, 1);
1167DEV_CLK(spi_clk, atmel_spi1, pba, 1);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001168
Haavard Skinnemoen9a596a62007-02-19 10:38:04 +01001169static void __init
Haavard Skinnemoen41d8ca42007-02-16 13:56:11 +01001170at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
1171 unsigned int n, const u8 *pins)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001172{
Haavard Skinnemoen41d8ca42007-02-16 13:56:11 +01001173 unsigned int pin, mode;
1174
1175 for (; n; n--, b++) {
1176 b->bus_num = bus_num;
1177 if (b->chip_select >= 4)
1178 continue;
1179 pin = (unsigned)b->controller_data;
1180 if (!pin) {
1181 pin = pins[b->chip_select];
1182 b->controller_data = (void *)pin;
1183 }
1184 mode = AT32_GPIOF_OUTPUT;
1185 if (!(b->mode & SPI_CS_HIGH))
1186 mode |= AT32_GPIOF_HIGH;
1187 at32_select_gpio(pin, mode);
1188 }
1189}
1190
1191struct platform_device *__init
1192at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
1193{
1194 /*
1195 * Manage the chipselects as GPIOs, normally using the same pins
1196 * the SPI controller expects; but boards can use other pins.
1197 */
1198 static u8 __initdata spi0_pins[] =
1199 { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
1200 GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
1201 static u8 __initdata spi1_pins[] =
1202 { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
1203 GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001204 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +02001205 u32 pin_mask;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001206
1207 switch (id) {
1208 case 0:
Haavard Skinnemoen3d60ee12007-01-10 20:20:02 +01001209 pdev = &atmel_spi0_device;
Julien Maycaf18f12008-09-24 10:30:47 +02001210 pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
1211
David Brownell9c2baf72008-06-18 02:31:43 -07001212 /* pullup MISO so a level is always defined */
Julien Maycaf18f12008-09-24 10:30:47 +02001213 select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
1214 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
1215
Haavard Skinnemoen41d8ca42007-02-16 13:56:11 +01001216 at32_spi_setup_slaves(0, b, n, spi0_pins);
Haavard Skinnemoen3d60ee12007-01-10 20:20:02 +01001217 break;
1218
1219 case 1:
1220 pdev = &atmel_spi1_device;
Julien Maycaf18f12008-09-24 10:30:47 +02001221 pin_mask = (1 << 1) | (1 << 5); /* MOSI */
1222
David Brownell9c2baf72008-06-18 02:31:43 -07001223 /* pullup MISO so a level is always defined */
Julien Maycaf18f12008-09-24 10:30:47 +02001224 select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
1225 select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
1226
Haavard Skinnemoen41d8ca42007-02-16 13:56:11 +01001227 at32_spi_setup_slaves(1, b, n, spi1_pins);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001228 break;
1229
1230 default:
1231 return NULL;
1232 }
1233
Haavard Skinnemoen41d8ca42007-02-16 13:56:11 +01001234 spi_register_board_info(b, n);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001235 platform_device_register(pdev);
1236 return pdev;
1237}
1238
1239/* --------------------------------------------------------------------
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001240 * TWI
1241 * -------------------------------------------------------------------- */
1242static struct resource atmel_twi0_resource[] __initdata = {
1243 PBMEM(0xffe00800),
1244 IRQ(5),
1245};
1246static struct clk atmel_twi0_pclk = {
1247 .name = "twi_pclk",
1248 .parent = &pba_clk,
1249 .mode = pba_clk_mode,
1250 .get_rate = pba_clk_get_rate,
1251 .index = 2,
1252};
1253
Ben Nizette040b28f2008-02-07 15:28:57 +11001254struct platform_device *__init at32_add_device_twi(unsigned int id,
1255 struct i2c_board_info *b,
1256 unsigned int n)
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001257{
1258 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +02001259 u32 pin_mask;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001260
1261 if (id != 0)
1262 return NULL;
1263
1264 pdev = platform_device_alloc("atmel_twi", id);
1265 if (!pdev)
1266 return NULL;
1267
1268 if (platform_device_add_resources(pdev, atmel_twi0_resource,
1269 ARRAY_SIZE(atmel_twi0_resource)))
1270 goto err_add_resources;
1271
Julien Maycaf18f12008-09-24 10:30:47 +02001272 pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
1273
1274 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001275
1276 atmel_twi0_pclk.dev = &pdev->dev;
1277
Ben Nizette040b28f2008-02-07 15:28:57 +11001278 if (b)
1279 i2c_register_board_info(id, b, n);
1280
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001281 platform_device_add(pdev);
1282 return pdev;
1283
1284err_add_resources:
1285 platform_device_put(pdev);
1286 return NULL;
1287}
1288
1289/* --------------------------------------------------------------------
1290 * MMC
1291 * -------------------------------------------------------------------- */
1292static struct resource atmel_mci0_resource[] __initdata = {
1293 PBMEM(0xfff02400),
1294 IRQ(28),
1295};
1296static struct clk atmel_mci0_pclk = {
1297 .name = "mci_clk",
1298 .parent = &pbb_clk,
1299 .mode = pbb_clk_mode,
1300 .get_rate = pbb_clk_get_rate,
1301 .index = 9,
1302};
1303
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001304struct platform_device *__init
1305at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001306{
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001307 struct platform_device *pdev;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001308 struct dw_dma_slave *dws;
Julien Maycaf18f12008-09-24 10:30:47 +02001309 u32 pioa_mask;
1310 u32 piob_mask;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001311
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001312 if (id != 0 || !data)
1313 return NULL;
1314
1315 /* Must have at least one usable slot */
1316 if (!data->slot[0].bus_width && !data->slot[1].bus_width)
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001317 return NULL;
1318
1319 pdev = platform_device_alloc("atmel_mci", id);
1320 if (!pdev)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001321 goto fail;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001322
1323 if (platform_device_add_resources(pdev, atmel_mci0_resource,
1324 ARRAY_SIZE(atmel_mci0_resource)))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001325 goto fail;
1326
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001327 if (data->dma_slave)
1328 dws = kmemdup(to_dw_dma_slave(data->dma_slave),
1329 sizeof(struct dw_dma_slave), GFP_KERNEL);
1330 else
1331 dws = kzalloc(sizeof(struct dw_dma_slave), GFP_KERNEL);
1332
1333 dws->slave.dev = &pdev->dev;
1334 dws->slave.dma_dev = &dw_dmac0_device.dev;
1335 dws->slave.reg_width = DMA_SLAVE_WIDTH_32BIT;
1336 dws->cfg_hi = (DWC_CFGH_SRC_PER(0)
1337 | DWC_CFGH_DST_PER(1));
1338 dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL
1339 | DWC_CFGL_HS_SRC_POL);
1340
1341 data->dma_slave = &dws->slave;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001342
1343 if (platform_device_add_data(pdev, data,
1344 sizeof(struct mci_platform_data)))
1345 goto fail;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001346
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001347 /* CLK line is common to both slots */
Julien Maycaf18f12008-09-24 10:30:47 +02001348 pioa_mask = 1 << 10;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001349
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001350 switch (data->slot[0].bus_width) {
1351 case 4:
Julien Maycaf18f12008-09-24 10:30:47 +02001352 pioa_mask |= 1 << 13; /* DATA1 */
1353 pioa_mask |= 1 << 14; /* DATA2 */
1354 pioa_mask |= 1 << 15; /* DATA3 */
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001355 /* fall through */
1356 case 1:
Julien Maycaf18f12008-09-24 10:30:47 +02001357 pioa_mask |= 1 << 11; /* CMD */
1358 pioa_mask |= 1 << 12; /* DATA0 */
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001359
1360 if (gpio_is_valid(data->slot[0].detect_pin))
1361 at32_select_gpio(data->slot[0].detect_pin, 0);
1362 if (gpio_is_valid(data->slot[0].wp_pin))
1363 at32_select_gpio(data->slot[0].wp_pin, 0);
1364 break;
1365 case 0:
1366 /* Slot is unused */
1367 break;
1368 default:
1369 goto fail;
1370 }
1371
Julien Maycaf18f12008-09-24 10:30:47 +02001372 select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
1373 piob_mask = 0;
1374
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001375 switch (data->slot[1].bus_width) {
1376 case 4:
Julien Maycaf18f12008-09-24 10:30:47 +02001377 piob_mask |= 1 << 8; /* DATA1 */
1378 piob_mask |= 1 << 9; /* DATA2 */
1379 piob_mask |= 1 << 10; /* DATA3 */
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001380 /* fall through */
1381 case 1:
Julien Maycaf18f12008-09-24 10:30:47 +02001382 piob_mask |= 1 << 6; /* CMD */
1383 piob_mask |= 1 << 7; /* DATA0 */
1384 select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
Haavard Skinnemoen6b918652008-08-07 14:08:49 +02001385
1386 if (gpio_is_valid(data->slot[1].detect_pin))
1387 at32_select_gpio(data->slot[1].detect_pin, 0);
1388 if (gpio_is_valid(data->slot[1].wp_pin))
1389 at32_select_gpio(data->slot[1].wp_pin, 0);
1390 break;
1391 case 0:
1392 /* Slot is unused */
1393 break;
1394 default:
1395 if (!data->slot[0].bus_width)
1396 goto fail;
1397
1398 data->slot[1].bus_width = 0;
1399 break;
1400 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001401
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001402 atmel_mci0_pclk.dev = &pdev->dev;
1403
1404 platform_device_add(pdev);
1405 return pdev;
1406
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001407fail:
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001408 platform_device_put(pdev);
1409 return NULL;
1410}
1411
1412/* --------------------------------------------------------------------
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001413 * LCDC
1414 * -------------------------------------------------------------------- */
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01001415#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001416static struct atmel_lcdfb_info atmel_lcdfb0_data;
1417static struct resource atmel_lcdfb0_resource[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001418 {
1419 .start = 0xff000000,
1420 .end = 0xff000fff,
1421 .flags = IORESOURCE_MEM,
1422 },
1423 IRQ(1),
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001424 {
1425 /* Placeholder for pre-allocated fb memory */
1426 .start = 0x00000000,
1427 .end = 0x00000000,
1428 .flags = 0,
1429 },
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001430};
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001431DEFINE_DEV_DATA(atmel_lcdfb, 0);
1432DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
1433static struct clk atmel_lcdfb0_pixclk = {
1434 .name = "lcdc_clk",
1435 .dev = &atmel_lcdfb0_device.dev,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001436 .mode = genclk_mode,
1437 .get_rate = genclk_get_rate,
1438 .set_rate = genclk_set_rate,
1439 .set_parent = genclk_set_parent,
1440 .index = 7,
1441};
1442
1443struct platform_device *__init
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001444at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
Hans-Christian Egtvedt47882cf2008-02-05 15:27:16 +01001445 unsigned long fbmem_start, unsigned long fbmem_len,
Julien May70664122008-08-04 14:27:38 +02001446 u64 pin_mask)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001447{
1448 struct platform_device *pdev;
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001449 struct atmel_lcdfb_info *info;
1450 struct fb_monspecs *monspecs;
1451 struct fb_videomode *modedb;
1452 unsigned int modedb_size;
Julien Maycaf18f12008-09-24 10:30:47 +02001453 u32 portc_mask, portd_mask, porte_mask;
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001454
1455 /*
1456 * Do a deep copy of the fb data, monspecs and modedb. Make
1457 * sure all allocations are done before setting up the
1458 * portmux.
1459 */
1460 monspecs = kmemdup(data->default_monspecs,
1461 sizeof(struct fb_monspecs), GFP_KERNEL);
1462 if (!monspecs)
1463 return NULL;
1464
1465 modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
1466 modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
1467 if (!modedb)
1468 goto err_dup_modedb;
1469 monspecs->modedb = modedb;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001470
1471 switch (id) {
1472 case 0:
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001473 pdev = &atmel_lcdfb0_device;
Hans-Christian Egtvedt47882cf2008-02-05 15:27:16 +01001474
Julien May70664122008-08-04 14:27:38 +02001475 if (pin_mask == 0ULL)
1476 /* Default to "full" lcdc control signals and 24bit */
1477 pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
1478
1479 /* LCDC on port C */
Alex Raimondi60900652008-10-13 16:03:45 +02001480 portc_mask = pin_mask & 0xfff80000;
Julien Maycaf18f12008-09-24 10:30:47 +02001481 select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
Julien May70664122008-08-04 14:27:38 +02001482
1483 /* LCDC on port D */
Julien Maycaf18f12008-09-24 10:30:47 +02001484 portd_mask = pin_mask & 0x0003ffff;
1485 select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
Julien May70664122008-08-04 14:27:38 +02001486
1487 /* LCDC on port E */
Julien Maycaf18f12008-09-24 10:30:47 +02001488 porte_mask = (pin_mask >> 32) & 0x0007ffff;
1489 select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001490
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001491 clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
1492 clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001493 break;
1494
1495 default:
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001496 goto err_invalid_id;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001497 }
1498
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001499 if (fbmem_len) {
1500 pdev->resource[2].start = fbmem_start;
1501 pdev->resource[2].end = fbmem_start + fbmem_len - 1;
1502 pdev->resource[2].flags = IORESOURCE_MEM;
1503 }
1504
1505 info = pdev->dev.platform_data;
1506 memcpy(info, data, sizeof(struct atmel_lcdfb_info));
1507 info->default_monspecs = monspecs;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001508
1509 platform_device_register(pdev);
1510 return pdev;
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01001511
1512err_invalid_id:
1513 kfree(modedb);
1514err_dup_modedb:
1515 kfree(monspecs);
1516 return NULL;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001517}
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01001518#endif
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001519
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +01001520/* --------------------------------------------------------------------
David Brownell9a1e8eb2008-02-08 04:21:21 -08001521 * PWM
1522 * -------------------------------------------------------------------- */
1523static struct resource atmel_pwm0_resource[] __initdata = {
1524 PBMEM(0xfff01400),
1525 IRQ(24),
1526};
1527static struct clk atmel_pwm0_mck = {
Sedji Gaouaou84059962008-06-25 10:32:50 +02001528 .name = "pwm_clk",
David Brownell9a1e8eb2008-02-08 04:21:21 -08001529 .parent = &pbb_clk,
1530 .mode = pbb_clk_mode,
1531 .get_rate = pbb_clk_get_rate,
1532 .index = 5,
1533};
1534
1535struct platform_device *__init at32_add_device_pwm(u32 mask)
1536{
1537 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +02001538 u32 pin_mask;
David Brownell9a1e8eb2008-02-08 04:21:21 -08001539
1540 if (!mask)
1541 return NULL;
1542
1543 pdev = platform_device_alloc("atmel_pwm", 0);
1544 if (!pdev)
1545 return NULL;
1546
1547 if (platform_device_add_resources(pdev, atmel_pwm0_resource,
1548 ARRAY_SIZE(atmel_pwm0_resource)))
1549 goto out_free_pdev;
1550
1551 if (platform_device_add_data(pdev, &mask, sizeof(mask)))
1552 goto out_free_pdev;
1553
Julien Maycaf18f12008-09-24 10:30:47 +02001554 pin_mask = 0;
David Brownell9a1e8eb2008-02-08 04:21:21 -08001555 if (mask & (1 << 0))
Julien Maycaf18f12008-09-24 10:30:47 +02001556 pin_mask |= (1 << 28);
David Brownell9a1e8eb2008-02-08 04:21:21 -08001557 if (mask & (1 << 1))
Julien Maycaf18f12008-09-24 10:30:47 +02001558 pin_mask |= (1 << 29);
1559 if (pin_mask > 0)
1560 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
1561
1562 pin_mask = 0;
David Brownell9a1e8eb2008-02-08 04:21:21 -08001563 if (mask & (1 << 2))
Julien Maycaf18f12008-09-24 10:30:47 +02001564 pin_mask |= (1 << 21);
David Brownell9a1e8eb2008-02-08 04:21:21 -08001565 if (mask & (1 << 3))
Julien Maycaf18f12008-09-24 10:30:47 +02001566 pin_mask |= (1 << 22);
1567 if (pin_mask > 0)
1568 select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
David Brownell9a1e8eb2008-02-08 04:21:21 -08001569
1570 atmel_pwm0_mck.dev = &pdev->dev;
1571
1572 platform_device_add(pdev);
1573
1574 return pdev;
1575
1576out_free_pdev:
1577 platform_device_put(pdev);
1578 return NULL;
1579}
1580
1581/* --------------------------------------------------------------------
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001582 * SSC
1583 * -------------------------------------------------------------------- */
1584static struct resource ssc0_resource[] = {
1585 PBMEM(0xffe01c00),
1586 IRQ(10),
1587};
1588DEFINE_DEV(ssc, 0);
1589DEV_CLK(pclk, ssc0, pba, 7);
1590
1591static struct resource ssc1_resource[] = {
1592 PBMEM(0xffe02000),
1593 IRQ(11),
1594};
1595DEFINE_DEV(ssc, 1);
1596DEV_CLK(pclk, ssc1, pba, 8);
1597
1598static struct resource ssc2_resource[] = {
1599 PBMEM(0xffe02400),
1600 IRQ(12),
1601};
1602DEFINE_DEV(ssc, 2);
1603DEV_CLK(pclk, ssc2, pba, 9);
1604
1605struct platform_device *__init
1606at32_add_device_ssc(unsigned int id, unsigned int flags)
1607{
1608 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +02001609 u32 pin_mask = 0;
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001610
1611 switch (id) {
1612 case 0:
1613 pdev = &ssc0_device;
1614 if (flags & ATMEL_SSC_RF)
Julien Maycaf18f12008-09-24 10:30:47 +02001615 pin_mask |= (1 << 21); /* RF */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001616 if (flags & ATMEL_SSC_RK)
Julien Maycaf18f12008-09-24 10:30:47 +02001617 pin_mask |= (1 << 22); /* RK */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001618 if (flags & ATMEL_SSC_TK)
Julien Maycaf18f12008-09-24 10:30:47 +02001619 pin_mask |= (1 << 23); /* TK */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001620 if (flags & ATMEL_SSC_TF)
Julien Maycaf18f12008-09-24 10:30:47 +02001621 pin_mask |= (1 << 24); /* TF */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001622 if (flags & ATMEL_SSC_TD)
Julien Maycaf18f12008-09-24 10:30:47 +02001623 pin_mask |= (1 << 25); /* TD */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001624 if (flags & ATMEL_SSC_RD)
Julien Maycaf18f12008-09-24 10:30:47 +02001625 pin_mask |= (1 << 26); /* RD */
1626
1627 if (pin_mask > 0)
1628 select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
1629
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001630 break;
1631 case 1:
1632 pdev = &ssc1_device;
1633 if (flags & ATMEL_SSC_RF)
Julien Maycaf18f12008-09-24 10:30:47 +02001634 pin_mask |= (1 << 0); /* RF */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001635 if (flags & ATMEL_SSC_RK)
Julien Maycaf18f12008-09-24 10:30:47 +02001636 pin_mask |= (1 << 1); /* RK */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001637 if (flags & ATMEL_SSC_TK)
Julien Maycaf18f12008-09-24 10:30:47 +02001638 pin_mask |= (1 << 2); /* TK */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001639 if (flags & ATMEL_SSC_TF)
Julien Maycaf18f12008-09-24 10:30:47 +02001640 pin_mask |= (1 << 3); /* TF */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001641 if (flags & ATMEL_SSC_TD)
Julien Maycaf18f12008-09-24 10:30:47 +02001642 pin_mask |= (1 << 4); /* TD */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001643 if (flags & ATMEL_SSC_RD)
Julien Maycaf18f12008-09-24 10:30:47 +02001644 pin_mask |= (1 << 5); /* RD */
1645
1646 if (pin_mask > 0)
1647 select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
1648
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001649 break;
1650 case 2:
1651 pdev = &ssc2_device;
1652 if (flags & ATMEL_SSC_TD)
Julien Maycaf18f12008-09-24 10:30:47 +02001653 pin_mask |= (1 << 13); /* TD */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001654 if (flags & ATMEL_SSC_RD)
Julien Maycaf18f12008-09-24 10:30:47 +02001655 pin_mask |= (1 << 14); /* RD */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001656 if (flags & ATMEL_SSC_TK)
Julien Maycaf18f12008-09-24 10:30:47 +02001657 pin_mask |= (1 << 15); /* TK */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001658 if (flags & ATMEL_SSC_TF)
Julien Maycaf18f12008-09-24 10:30:47 +02001659 pin_mask |= (1 << 16); /* TF */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001660 if (flags & ATMEL_SSC_RF)
Julien Maycaf18f12008-09-24 10:30:47 +02001661 pin_mask |= (1 << 17); /* RF */
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001662 if (flags & ATMEL_SSC_RK)
Julien Maycaf18f12008-09-24 10:30:47 +02001663 pin_mask |= (1 << 18); /* RK */
1664
1665 if (pin_mask > 0)
1666 select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
1667
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02001668 break;
1669 default:
1670 return NULL;
1671 }
1672
1673 platform_device_register(pdev);
1674 return pdev;
1675}
1676
1677/* --------------------------------------------------------------------
Haavard Skinnemoen6fcf0612007-06-14 17:37:31 +02001678 * USB Device Controller
1679 * -------------------------------------------------------------------- */
1680static struct resource usba0_resource[] __initdata = {
1681 {
1682 .start = 0xff300000,
1683 .end = 0xff3fffff,
1684 .flags = IORESOURCE_MEM,
1685 }, {
1686 .start = 0xfff03000,
1687 .end = 0xfff033ff,
1688 .flags = IORESOURCE_MEM,
1689 },
1690 IRQ(31),
1691};
1692static struct clk usba0_pclk = {
1693 .name = "pclk",
1694 .parent = &pbb_clk,
1695 .mode = pbb_clk_mode,
1696 .get_rate = pbb_clk_get_rate,
1697 .index = 12,
1698};
1699static struct clk usba0_hclk = {
1700 .name = "hclk",
1701 .parent = &hsb_clk,
1702 .mode = hsb_clk_mode,
1703 .get_rate = hsb_clk_get_rate,
1704 .index = 6,
1705};
1706
Stelian Pop8d855312008-03-05 00:00:00 +01001707#define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
1708 [idx] = { \
1709 .name = nam, \
1710 .index = idx, \
1711 .fifo_size = maxpkt, \
1712 .nr_banks = maxbk, \
1713 .can_dma = dma, \
1714 .can_isoc = isoc, \
1715 }
1716
1717static struct usba_ep_data at32_usba_ep[] __initdata = {
1718 EP("ep0", 0, 64, 1, 0, 0),
1719 EP("ep1", 1, 512, 2, 1, 1),
1720 EP("ep2", 2, 512, 2, 1, 1),
1721 EP("ep3-int", 3, 64, 3, 1, 0),
1722 EP("ep4-int", 4, 64, 3, 1, 0),
1723 EP("ep5", 5, 1024, 3, 1, 1),
1724 EP("ep6", 6, 1024, 3, 1, 1),
1725};
1726
1727#undef EP
1728
Haavard Skinnemoen6fcf0612007-06-14 17:37:31 +02001729struct platform_device *__init
1730at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
1731{
Stelian Pop8d855312008-03-05 00:00:00 +01001732 /*
1733 * pdata doesn't have room for any endpoints, so we need to
1734 * append room for the ones we need right after it.
1735 */
1736 struct {
1737 struct usba_platform_data pdata;
1738 struct usba_ep_data ep[7];
1739 } usba_data;
Haavard Skinnemoen6fcf0612007-06-14 17:37:31 +02001740 struct platform_device *pdev;
1741
1742 if (id != 0)
1743 return NULL;
1744
1745 pdev = platform_device_alloc("atmel_usba_udc", 0);
1746 if (!pdev)
1747 return NULL;
1748
1749 if (platform_device_add_resources(pdev, usba0_resource,
1750 ARRAY_SIZE(usba0_resource)))
1751 goto out_free_pdev;
1752
Stelian Pop8d855312008-03-05 00:00:00 +01001753 if (data)
1754 usba_data.pdata.vbus_pin = data->vbus_pin;
1755 else
1756 usba_data.pdata.vbus_pin = -EINVAL;
Haavard Skinnemoen6fcf0612007-06-14 17:37:31 +02001757
Stelian Pop8d855312008-03-05 00:00:00 +01001758 data = &usba_data.pdata;
1759 data->num_ep = ARRAY_SIZE(at32_usba_ep);
1760 memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
1761
1762 if (platform_device_add_data(pdev, data, sizeof(usba_data)))
1763 goto out_free_pdev;
1764
1765 if (data->vbus_pin >= 0)
1766 at32_select_gpio(data->vbus_pin, 0);
Haavard Skinnemoen6fcf0612007-06-14 17:37:31 +02001767
1768 usba0_pclk.dev = &pdev->dev;
1769 usba0_hclk.dev = &pdev->dev;
1770
1771 platform_device_add(pdev);
1772
1773 return pdev;
1774
1775out_free_pdev:
1776 platform_device_put(pdev);
1777 return NULL;
1778}
1779
1780/* --------------------------------------------------------------------
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001781 * IDE / CompactFlash
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001782 * -------------------------------------------------------------------- */
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01001783#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001784static struct resource at32_smc_cs4_resource[] __initdata = {
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001785 {
1786 .start = 0x04000000,
1787 .end = 0x07ffffff,
1788 .flags = IORESOURCE_MEM,
1789 },
1790 IRQ(~0UL), /* Magic IRQ will be overridden */
1791};
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001792static struct resource at32_smc_cs5_resource[] __initdata = {
1793 {
1794 .start = 0x20000000,
1795 .end = 0x23ffffff,
1796 .flags = IORESOURCE_MEM,
1797 },
1798 IRQ(~0UL), /* Magic IRQ will be overridden */
1799};
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001800
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001801static int __init at32_init_ide_or_cf(struct platform_device *pdev,
1802 unsigned int cs, unsigned int extint)
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001803{
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001804 static unsigned int extint_pin_map[4] __initdata = {
Julien Maycaf18f12008-09-24 10:30:47 +02001805 (1 << 25),
1806 (1 << 26),
1807 (1 << 27),
1808 (1 << 28),
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001809 };
1810 static bool common_pins_initialized __initdata = false;
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001811 unsigned int extint_pin;
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001812 int ret;
Julien Maycaf18f12008-09-24 10:30:47 +02001813 u32 pin_mask;
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001814
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001815 if (extint >= ARRAY_SIZE(extint_pin_map))
1816 return -EINVAL;
1817 extint_pin = extint_pin_map[extint];
1818
1819 switch (cs) {
1820 case 4:
1821 ret = platform_device_add_resources(pdev,
1822 at32_smc_cs4_resource,
1823 ARRAY_SIZE(at32_smc_cs4_resource));
1824 if (ret)
1825 return ret;
1826
Julien Maycaf18f12008-09-24 10:30:47 +02001827 /* NCS4 -> OE_N */
1828 select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +02001829 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001830 break;
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001831 case 5:
1832 ret = platform_device_add_resources(pdev,
1833 at32_smc_cs5_resource,
1834 ARRAY_SIZE(at32_smc_cs5_resource));
1835 if (ret)
1836 return ret;
1837
Julien Maycaf18f12008-09-24 10:30:47 +02001838 /* NCS5 -> OE_N */
1839 select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +02001840 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001841 break;
1842 default:
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001843 return -EINVAL;
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001844 }
1845
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001846 if (!common_pins_initialized) {
Julien Maycaf18f12008-09-24 10:30:47 +02001847 pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
1848 pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
1849 pin_mask |= (1 << 23); /* CFRNW -> DIR */
1850 pin_mask |= (1 << 24); /* NWAIT <- IORDY */
1851
1852 select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
1853
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001854 common_pins_initialized = true;
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001855 }
1856
Julien Maycaf18f12008-09-24 10:30:47 +02001857 select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001858
1859 pdev->resource[1].start = EIM_IRQ_BASE + extint;
1860 pdev->resource[1].end = pdev->resource[1].start;
1861
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001862 return 0;
1863}
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001864
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001865struct platform_device *__init
1866at32_add_device_ide(unsigned int id, unsigned int extint,
1867 struct ide_platform_data *data)
1868{
1869 struct platform_device *pdev;
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001870
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001871 pdev = platform_device_alloc("at32_ide", id);
1872 if (!pdev)
1873 goto fail;
1874
1875 if (platform_device_add_data(pdev, data,
1876 sizeof(struct ide_platform_data)))
1877 goto fail;
1878
1879 if (at32_init_ide_or_cf(pdev, data->cs, extint))
1880 goto fail;
1881
1882 platform_device_add(pdev);
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001883 return pdev;
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001884
1885fail:
1886 platform_device_put(pdev);
1887 return NULL;
1888}
1889
1890struct platform_device *__init
1891at32_add_device_cf(unsigned int id, unsigned int extint,
1892 struct cf_platform_data *data)
1893{
1894 struct platform_device *pdev;
1895
1896 pdev = platform_device_alloc("at32_cf", id);
1897 if (!pdev)
1898 goto fail;
1899
1900 if (platform_device_add_data(pdev, data,
1901 sizeof(struct cf_platform_data)))
1902 goto fail;
1903
1904 if (at32_init_ide_or_cf(pdev, data->cs, extint))
1905 goto fail;
1906
David Brownell3c26e172008-07-27 02:34:45 -07001907 if (gpio_is_valid(data->detect_pin))
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001908 at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
David Brownell3c26e172008-07-27 02:34:45 -07001909 if (gpio_is_valid(data->reset_pin))
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001910 at32_select_gpio(data->reset_pin, 0);
David Brownell3c26e172008-07-27 02:34:45 -07001911 if (gpio_is_valid(data->vcc_pin))
Haavard Skinnemoeneaf5f922007-10-22 18:32:14 +02001912 at32_select_gpio(data->vcc_pin, 0);
1913 /* READY is used as extint, so we can't select it as gpio */
1914
1915 platform_device_add(pdev);
1916 return pdev;
1917
1918fail:
1919 platform_device_put(pdev);
1920 return NULL;
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001921}
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01001922#endif
Kristoffer Nyborg Gregertsen48021bd2007-08-16 13:45:00 +02001923
1924/* --------------------------------------------------------------------
HÃ¥vard Skinnemoen62090a02008-06-06 18:04:56 +02001925 * NAND Flash / SmartMedia
1926 * -------------------------------------------------------------------- */
1927static struct resource smc_cs3_resource[] __initdata = {
1928 {
1929 .start = 0x0c000000,
1930 .end = 0x0fffffff,
1931 .flags = IORESOURCE_MEM,
1932 }, {
1933 .start = 0xfff03c00,
1934 .end = 0xfff03fff,
1935 .flags = IORESOURCE_MEM,
1936 },
1937};
1938
1939struct platform_device *__init
1940at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
1941{
1942 struct platform_device *pdev;
1943
1944 if (id != 0 || !data)
1945 return NULL;
1946
1947 pdev = platform_device_alloc("atmel_nand", id);
1948 if (!pdev)
1949 goto fail;
1950
1951 if (platform_device_add_resources(pdev, smc_cs3_resource,
1952 ARRAY_SIZE(smc_cs3_resource)))
1953 goto fail;
1954
1955 if (platform_device_add_data(pdev, data,
1956 sizeof(struct atmel_nand_data)))
1957 goto fail;
1958
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +02001959 hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
HÃ¥vard Skinnemoen62090a02008-06-06 18:04:56 +02001960 if (data->enable_pin)
1961 at32_select_gpio(data->enable_pin,
1962 AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
1963 if (data->rdy_pin)
1964 at32_select_gpio(data->rdy_pin, 0);
1965 if (data->det_pin)
1966 at32_select_gpio(data->det_pin, 0);
1967
1968 platform_device_add(pdev);
1969 return pdev;
1970
1971fail:
1972 platform_device_put(pdev);
1973 return NULL;
1974}
1975
1976/* --------------------------------------------------------------------
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001977 * AC97C
1978 * -------------------------------------------------------------------- */
1979static struct resource atmel_ac97c0_resource[] __initdata = {
1980 PBMEM(0xfff02800),
1981 IRQ(29),
1982};
1983static struct clk atmel_ac97c0_pclk = {
1984 .name = "pclk",
1985 .parent = &pbb_clk,
1986 .mode = pbb_clk_mode,
1987 .get_rate = pbb_clk_get_rate,
1988 .index = 10,
1989};
1990
Hans-Christian Egtvedt218df4a2008-07-01 14:26:45 +02001991struct platform_device *__init
1992at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data)
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001993{
1994 struct platform_device *pdev;
Hans-Christian Egtvedt218df4a2008-07-01 14:26:45 +02001995 struct ac97c_platform_data _data;
Julien Maycaf18f12008-09-24 10:30:47 +02001996 u32 pin_mask;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02001997
1998 if (id != 0)
1999 return NULL;
2000
2001 pdev = platform_device_alloc("atmel_ac97c", id);
2002 if (!pdev)
2003 return NULL;
2004
2005 if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
2006 ARRAY_SIZE(atmel_ac97c0_resource)))
Hans-Christian Egtvedt218df4a2008-07-01 14:26:45 +02002007 goto fail;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002008
Hans-Christian Egtvedt218df4a2008-07-01 14:26:45 +02002009 if (!data) {
2010 data = &_data;
2011 memset(data, 0, sizeof(struct ac97c_platform_data));
2012 data->reset_pin = GPIO_PIN_NONE;
2013 }
2014
2015 data->dma_rx_periph_id = 3;
2016 data->dma_tx_periph_id = 4;
2017 data->dma_controller_id = 0;
2018
2019 if (platform_device_add_data(pdev, data,
2020 sizeof(struct ac97c_platform_data)))
2021 goto fail;
2022
Julien Maycaf18f12008-09-24 10:30:47 +02002023 pin_mask = (1 << 20) | (1 << 21); /* SDO & SYNC */
2024 pin_mask |= (1 << 22) | (1 << 23); /* SCLK & SDI */
2025
2026 select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
Hans-Christian Egtvedt218df4a2008-07-01 14:26:45 +02002027
2028 /* TODO: gpio_is_valid(data->reset_pin) with kernel 2.6.26. */
2029 if (data->reset_pin != GPIO_PIN_NONE)
2030 at32_select_gpio(data->reset_pin, 0);
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002031
2032 atmel_ac97c0_pclk.dev = &pdev->dev;
2033
2034 platform_device_add(pdev);
2035 return pdev;
2036
Hans-Christian Egtvedt218df4a2008-07-01 14:26:45 +02002037fail:
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002038 platform_device_put(pdev);
2039 return NULL;
2040}
2041
2042/* --------------------------------------------------------------------
2043 * ABDAC
2044 * -------------------------------------------------------------------- */
2045static struct resource abdac0_resource[] __initdata = {
2046 PBMEM(0xfff02000),
2047 IRQ(27),
2048};
2049static struct clk abdac0_pclk = {
2050 .name = "pclk",
2051 .parent = &pbb_clk,
2052 .mode = pbb_clk_mode,
2053 .get_rate = pbb_clk_get_rate,
2054 .index = 8,
2055};
2056static struct clk abdac0_sample_clk = {
2057 .name = "sample_clk",
2058 .mode = genclk_mode,
2059 .get_rate = genclk_get_rate,
2060 .set_rate = genclk_set_rate,
2061 .set_parent = genclk_set_parent,
2062 .index = 6,
2063};
2064
2065struct platform_device *__init at32_add_device_abdac(unsigned int id)
2066{
2067 struct platform_device *pdev;
Julien Maycaf18f12008-09-24 10:30:47 +02002068 u32 pin_mask;
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002069
2070 if (id != 0)
2071 return NULL;
2072
2073 pdev = platform_device_alloc("abdac", id);
2074 if (!pdev)
2075 return NULL;
2076
2077 if (platform_device_add_resources(pdev, abdac0_resource,
2078 ARRAY_SIZE(abdac0_resource)))
2079 goto err_add_resources;
2080
Julien Maycaf18f12008-09-24 10:30:47 +02002081 pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
2082 pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
2083
2084 select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002085
2086 abdac0_pclk.dev = &pdev->dev;
2087 abdac0_sample_clk.dev = &pdev->dev;
2088
2089 platform_device_add(pdev);
2090 return pdev;
2091
2092err_add_resources:
2093 platform_device_put(pdev);
2094 return NULL;
2095}
2096
2097/* --------------------------------------------------------------------
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +01002098 * GCLK
2099 * -------------------------------------------------------------------- */
2100static struct clk gclk0 = {
2101 .name = "gclk0",
2102 .mode = genclk_mode,
2103 .get_rate = genclk_get_rate,
2104 .set_rate = genclk_set_rate,
2105 .set_parent = genclk_set_parent,
2106 .index = 0,
2107};
2108static struct clk gclk1 = {
2109 .name = "gclk1",
2110 .mode = genclk_mode,
2111 .get_rate = genclk_get_rate,
2112 .set_rate = genclk_set_rate,
2113 .set_parent = genclk_set_parent,
2114 .index = 1,
2115};
2116static struct clk gclk2 = {
2117 .name = "gclk2",
2118 .mode = genclk_mode,
2119 .get_rate = genclk_get_rate,
2120 .set_rate = genclk_set_rate,
2121 .set_parent = genclk_set_parent,
2122 .index = 2,
2123};
2124static struct clk gclk3 = {
2125 .name = "gclk3",
2126 .mode = genclk_mode,
2127 .get_rate = genclk_get_rate,
2128 .set_rate = genclk_set_rate,
2129 .set_parent = genclk_set_parent,
2130 .index = 3,
2131};
2132static struct clk gclk4 = {
2133 .name = "gclk4",
2134 .mode = genclk_mode,
2135 .get_rate = genclk_get_rate,
2136 .set_rate = genclk_set_rate,
2137 .set_parent = genclk_set_parent,
2138 .index = 4,
2139};
2140
Alex Raimondi300bb762008-09-22 21:40:55 +02002141static __initdata struct clk *init_clocks[] = {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002142 &osc32k,
2143 &osc0,
2144 &osc1,
2145 &pll0,
2146 &pll1,
2147 &cpu_clk,
2148 &hsb_clk,
2149 &pba_clk,
2150 &pbb_clk,
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +02002151 &at32_pm_pclk,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002152 &at32_intc0_pclk,
Haavard Skinnemoenb47eb402008-07-31 15:56:36 +02002153 &at32_hmatrix_clk,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002154 &ebi_clk,
2155 &hramc_clk,
Haavard Skinnemoen7951f182008-03-05 15:08:27 +01002156 &sdramc_clk,
Haavard Skinnemoenbc157b72006-09-25 23:32:16 -07002157 &smc0_pclk,
2158 &smc0_mck,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002159 &pdc_hclk,
2160 &pdc_pclk,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07002161 &dw_dmac0_hclk,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002162 &pico_clk,
2163 &pio0_mck,
2164 &pio1_mck,
2165 &pio2_mck,
2166 &pio3_mck,
Haavard Skinnemoen7f9f4672007-01-30 11:16:16 +01002167 &pio4_mck,
David Brownelle723ff62008-02-14 11:24:02 -08002168 &at32_tcb0_t0_clk,
2169 &at32_tcb1_t0_clk,
Hans-Christian Egtvedtd86d3142008-02-25 11:24:30 +01002170 &atmel_psif0_pclk,
2171 &atmel_psif1_pclk,
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002172 &atmel_usart0_usart,
2173 &atmel_usart1_usart,
2174 &atmel_usart2_usart,
2175 &atmel_usart3_usart,
David Brownell9a1e8eb2008-02-08 04:21:21 -08002176 &atmel_pwm0_mck,
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01002177#if defined(CONFIG_CPU_AT32AP7000)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002178 &macb0_hclk,
2179 &macb0_pclk,
Haavard Skinnemoencfcb3a82006-10-30 09:23:12 +01002180 &macb1_hclk,
2181 &macb1_pclk,
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01002182#endif
Haavard Skinnemoen3d60ee12007-01-10 20:20:02 +01002183 &atmel_spi0_spi_clk,
2184 &atmel_spi1_spi_clk,
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002185 &atmel_twi0_pclk,
2186 &atmel_mci0_pclk,
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01002187#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01002188 &atmel_lcdfb0_hck1,
2189 &atmel_lcdfb0_pixclk,
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01002190#endif
Hans-Christian Egtvedt9cf6cf52007-07-06 14:31:55 +02002191 &ssc0_pclk,
2192 &ssc1_pclk,
2193 &ssc2_pclk,
Haavard Skinnemoen6fcf0612007-06-14 17:37:31 +02002194 &usba0_hclk,
2195 &usba0_pclk,
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002196 &atmel_ac97c0_pclk,
2197 &abdac0_pclk,
2198 &abdac0_sample_clk,
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +01002199 &gclk0,
2200 &gclk1,
2201 &gclk2,
2202 &gclk3,
2203 &gclk4,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002204};
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002205
Haavard Skinnemoen65033ed2008-03-04 15:15:00 +01002206void __init setup_platform(void)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002207{
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002208 u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
2209 int i;
2210
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +02002211 if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002212 main_clock = &pll0;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +02002213 cpu_clk.parent = &pll0;
2214 } else {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002215 main_clock = &osc0;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +02002216 cpu_clk.parent = &osc0;
2217 }
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002218
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +02002219 if (pm_readl(PLL0) & PM_BIT(PLLOSC))
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002220 pll0.parent = &osc1;
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +02002221 if (pm_readl(PLL1) & PM_BIT(PLLOSC))
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002222 pll1.parent = &osc1;
2223
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +01002224 genclk_init_parent(&gclk0);
2225 genclk_init_parent(&gclk1);
2226 genclk_init_parent(&gclk2);
2227 genclk_init_parent(&gclk3);
2228 genclk_init_parent(&gclk4);
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01002229#if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
Haavard Skinnemoend0a2b7a2007-03-21 18:08:49 +01002230 genclk_init_parent(&atmel_lcdfb0_pixclk);
Haavard Skinnemoen438ff3f2007-10-29 15:28:07 +01002231#endif
Haavard Skinnemoen2042c1c2007-10-22 17:42:35 +02002232 genclk_init_parent(&abdac0_sample_clk);
Haavard Skinnemoen7a5fe232007-02-16 13:14:33 +01002233
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002234 /*
Alex Raimondi300bb762008-09-22 21:40:55 +02002235 * Build initial dynamic clock list by registering all clocks
2236 * from the array.
2237 * At the same time, turn on all clocks that have at least one
2238 * user already, and turn off everything else. We only do this
2239 * for module clocks, and even though it isn't particularly
2240 * pretty to check the address of the mode function, it should
2241 * do the trick...
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002242 */
Alex Raimondi300bb762008-09-22 21:40:55 +02002243 for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
2244 struct clk *clk = init_clocks[i];
2245
2246 /* first, register clock */
2247 at32_clk_register(clk);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002248
Haavard Skinnemoen188ff652007-03-14 13:23:44 +01002249 if (clk->users == 0)
2250 continue;
2251
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002252 if (clk->mode == &cpu_clk_mode)
2253 cpu_mask |= 1 << clk->index;
2254 else if (clk->mode == &hsb_clk_mode)
2255 hsb_mask |= 1 << clk->index;
2256 else if (clk->mode == &pba_clk_mode)
2257 pba_mask |= 1 << clk->index;
2258 else if (clk->mode == &pbb_clk_mode)
2259 pbb_mask |= 1 << clk->index;
2260 }
2261
Haavard Skinnemoen7a5b8052007-06-04 12:58:30 +02002262 pm_writel(CPU_MASK, cpu_mask);
2263 pm_writel(HSB_MASK, hsb_mask);
2264 pm_writel(PBA_MASK, pba_mask);
2265 pm_writel(PBB_MASK, pbb_mask);
Haavard Skinnemoen65033ed2008-03-04 15:15:00 +01002266
2267 /* Initialize the port muxes */
2268 at32_init_pio(&pio0_device);
2269 at32_init_pio(&pio1_device);
2270 at32_init_pio(&pio2_device);
2271 at32_init_pio(&pio3_device);
2272 at32_init_pio(&pio4_device);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07002273}
Haavard Skinnemoenb83d6ee12008-03-05 10:00:28 +01002274
2275struct gen_pool *sram_pool;
2276
2277static int __init sram_init(void)
2278{
2279 struct gen_pool *pool;
2280
2281 /* 1KiB granularity */
2282 pool = gen_pool_create(10, -1);
2283 if (!pool)
2284 goto fail;
2285
2286 if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
2287 goto err_pool_add;
2288
2289 sram_pool = pool;
2290 return 0;
2291
2292err_pool_add:
2293 gen_pool_destroy(pool);
2294fail:
2295 pr_err("Failed to create SRAM pool\n");
2296 return -ENOMEM;
2297}
2298core_initcall(sram_init);