commandProcessing.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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>
  9. #include <linux/delay.h>
  10. #include <linux/dw_dmac.h>
  11. #include <linux/fb.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/slab.h>
  16. #include <linux/gpio.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/usb/atmel_usba_udc.h>
  19. #include <mach/atmel-mci.h>
  20. #include <linux/atmel-mci.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <mach/at32ap700x.h>
  24. #include <mach/board.h>
  25. #include <mach/hmatrix.h>
  26. #include <mach/portmux.h>
  27. #include <mach/sram.h>
  28. #include <sound/atmel-abdac.h>
  29. #include <sound/atmel-ac97c.h>
  30. #include <video/atmel_lcdc.h>
  31. #include "clock.h"
  32. #include "pio.h"
  33. #include "pm.h"
  34. #define PBMEM(base) \
  35. { \
  36. .start = base, \
  37. .end = base + 0x3ff, \
  38. .flags = IORESOURCE_MEM, \
  39. }
  40. #define IRQ(num) \
  41. { \
  42. .start = num, \
  43. .end = num, \
  44. .flags = IORESOURCE_IRQ, \
  45. }
  46. #define NAMED_IRQ(num, _name) \
  47. { \
  48. .start = num, \
  49. .end = num, \
  50. .name = _name, \
  51. .flags = IORESOURCE_IRQ, \
  52. }
  53. /* REVISIT these assume *every* device supports DMA, but several
  54. * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
  55. */
  56. #define DEFINE_DEV(_name, _id) \
  57. static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
  58. static struct platform_device _name##_id##_device = { \
  59. .name = #_name, \
  60. .id = _id, \
  61. .dev = { \
  62. .dma_mask = &_name##_id##_dma_mask, \
  63. .coherent_dma_mask = DMA_BIT_MASK(32), \
  64. }, \
  65. .resource = _name##_id##_resource, \
  66. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  67. }
  68. #define DEFINE_DEV_DATA(_name, _id) \
  69. static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32); \
  70. static struct platform_device _name##_id##_device = { \
  71. .name = #_name, \
  72. .id = _id, \
  73. .dev = { \
  74. .dma_mask = &_name##_id##_dma_mask, \
  75. .platform_data = &_name##_id##_data, \
  76. .coherent_dma_mask = DMA_BIT_MASK(32), \
  77. }, \
  78. .resource = _name##_id##_resource, \
  79. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  80. }
  81. #define select_peripheral(port, pin_mask, periph, flags) \
  82. at32_select_periph(GPIO_##port##_BASE, pin_mask, \
  83. GPIO_##periph, flags)
  84. #define DEV_CLK(_name, devname, bus, _index) \
  85. static struct clk devname##_##_name = { \
  86. .name = #_name, \
  87. .dev = &devname##_device.dev, \
  88. .parent = &bus##_clk, \
  89. .mode = bus##_clk_mode, \
  90. .get_rate = bus##_clk_get_rate, \
  91. .index = _index, \
  92. }
  93. static DEFINE_SPINLOCK(pm_lock);
  94. static struct clk osc0;
  95. static struct clk osc1;
  96. static unsigned long osc_get_rate(struct clk *clk)
  97. {
  98. return at32_board_osc_rates[clk->index];
  99. }
  100. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  101. {
  102. unsigned long div, mul, rate;
  103. div = PM_BFEXT(PLLDIV, control) + 1;
  104. mul = PM_BFEXT(PLLMUL, control) + 1;
  105. rate = clk->parent->get_rate(clk->parent);
  106. rate = (rate + div / 2) / div;
  107. rate *= mul;
  108. return rate;
  109. }
  110. static long pll_set_rate(struct clk *clk, unsigned long rate,
  111. u32 *pll_ctrl)
  112. {
  113. unsigned long mul;
  114. unsigned long mul_best_fit = 0;
  115. unsigned long div;
  116. unsigned long div_min;
  117. unsigned long div_max;
  118. unsigned long div_best_fit = 0;
  119. unsigned long base;
  120. unsigned long pll_in;
  121. unsigned long actual = 0;
  122. unsigned long rate_error;
  123. unsigned long rate_error_prev = ~0UL;
  124. u32 ctrl;
  125. /* Rate must be between 80 MHz and 200 Mhz. */
  126. if (rate < 80000000UL || rate > 200000000UL)
  127. return -EINVAL;
  128. ctrl = PM_BF(PLLOPT, 4);
  129. base = clk->parent->get_rate(clk->parent);
  130. /* PLL input frequency must be between 6 MHz and 32 MHz. */
  131. div_min = DIV_ROUND_UP(base, 32000000UL);
  132. div_max = base / 6000000UL;
  133. if (div_max < div_min)
  134. return -EINVAL;
  135. for (div = div_min; div <= div_max; div++) {
  136. pll_in = (base + div / 2) / div;
  137. mul = (rate + pll_in / 2) / pll_in;
  138. if (mul == 0)
  139. continue;
  140. actual = pll_in * mul;
  141. rate_error = abs(actual - rate);
  142. if (rate_error < rate_error_prev) {
  143. mul_best_fit = mul;
  144. div_best_fit = div;
  145. rate_error_prev = rate_error;
  146. }
  147. if (rate_error == 0)
  148. break;
  149. }
  150. if (div_best_fit == 0)
  151. return -EINVAL;
  152. ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
  153. ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
  154. ctrl |= PM_BF(PLLCOUNT, 16);
  155. if (clk->parent == &osc1)
  156. ctrl |= PM_BIT(PLLOSC);
  157. *pll_ctrl = ctrl;
  158. return actual;
  159. }
  160. static unsigned long pll0_get_rate(struct clk *clk)
  161. {
  162. u32 control;
  163. control = pm_readl(PLL0);
  164. return pll_get_rate(clk, control);
  165. }
  166. static void pll1_mode(struct clk *clk, int enabled)
  167. {
  168. unsigned long timeout;
  169. u32 status;
  170. u32 ctrl;
  171. ctrl = pm_readl(PLL1);
  172. if (enabled) {
  173. if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
  174. pr_debug("clk %s: failed to enable, rate not set\n",
  175. clk->name);
  176. return;
  177. }
  178. ctrl |= PM_BIT(PLLEN);
  179. pm_writel(PLL1, ctrl);
  180. /* Wait for PLL lock. */
  181. for (timeout = 10000; timeout; timeout--) {
  182. status = pm_readl(ISR);
  183. if (status & PM_BIT(LOCK1))
  184. break;
  185. udelay(10);
  186. }
  187. if (!(status & PM_BIT(LOCK1)))
  188. printk(KERN_ERR "clk %s: timeout waiting for lock\n",
  189. clk->name);
  190. } else {
  191. ctrl &= ~PM_BIT(PLLEN);
  192. pm_writel(PLL1, ctrl);
  193. }
  194. }
  195. static unsigned long pll1_get_rate(struct clk *clk)
  196. {
  197. u32 control;
  198. control = pm_readl(PLL1);
  199. return pll_get_rate(clk, control);
  200. }
  201. static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
  202. {
  203. u32 ctrl = 0;
  204. unsigned long actual_rate;
  205. actual_rate = pll_set_rate(clk, rate, &ctrl);
  206. if (apply) {
  207. if (actual_rate != rate)
  208. return -EINVAL;
  209. if (clk->users > 0)
  210. return -EBUSY;
  211. pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
  212. clk->name, rate, actual_rate);
  213. pm_writel(PLL1, ctrl);
  214. }
  215. return actual_rate;
  216. }
  217. static int pll1_set_parent(struct clk *clk, struct clk *parent)
  218. {
  219. u32 ctrl;
  220. if (clk->users > 0)
  221. return -EBUSY;
  222. ctrl = pm_readl(PLL1);
  223. WARN_ON(ctrl & PM_BIT(PLLEN));
  224. if (parent == &osc0)
  225. ctrl &= ~PM_BIT(PLLOSC);
  226. else if (parent == &osc1)
  227. ctrl |= PM_BIT(PLLOSC);
  228. else
  229. return -EINVAL;
  230. pm_writel(PLL1, ctrl);
  231. clk->parent = parent;
  232. return 0;
  233. }
  234. /*
  235. * The AT32AP7000 has five primary clock sources: One 32kHz
  236. * oscillator, two crystal oscillators and two PLLs.
  237. */
  238. static struct clk osc32k = {
  239. .name = "osc32k",
  240. .get_rate = osc_get_rate,
  241. .users = 1,
  242. .index = 0,
  243. };
  244. static struct clk osc0 = {
  245. .name = "osc0",
  246. .get_rate = osc_get_rate,
  247. .users = 1,
  248. .index = 1,
  249. };
  250. static struct clk osc1 = {
  251. .name = "osc1",
  252. .get_rate = osc_get_rate,
  253. .index = 2,
  254. };
  255. static struct clk pll0 = {
  256. .name = "pll0",
  257. .get_rate = pll0_get_rate,
  258. .parent = &osc0,
  259. };
  260. static struct clk pll1 = {
  261. .name = "pll1",
  262. .mode = pll1_mode,
  263. .get_rate = pll1_get_rate,
  264. .set_rate = pll1_set_rate,
  265. .set_parent = pll1_set_parent,
  266. .parent = &osc0,
  267. };
  268. /*
  269. * The main clock can be either osc0 or pll0. The boot loader may
  270. * have chosen one for us, so we don't really know which one until we
  271. * have a look at the SM.
  272. */
  273. static struct clk *main_clock;
  274. /*
  275. * Synchronous clocks are generated from the main clock. The clocks
  276. * must satisfy the constraint
  277. * fCPU >= fHSB >= fPB
  278. * i.e. each clock must not be faster than its parent.
  279. */
  280. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  281. {
  282. return main_clock->get_rate(main_clock) >> shift;
  283. };
  284. static void cpu_clk_mode(struct clk *clk, int enabled)
  285. {
  286. unsigned long flags;
  287. u32 mask;
  288. spin_lock_irqsave(&pm_lock, flags);
  289. mask = pm_readl(CPU_MASK);
  290. if (enabled)
  291. mask |= 1 << clk->index;
  292. else
  293. mask &= ~(1 << clk->index);
  294. pm_writel(CPU_MASK, mask);
  295. spin_unlock_irqrestore(&pm_lock, flags);
  296. }
  297. static unsigned long cpu_clk_get_rate(struct clk *clk)
  298. {
  299. unsigned long cksel, shift = 0;
  300. cksel = pm_readl(CKSEL);
  301. if (cksel & PM_BIT(CPUDIV))
  302. shift = PM_BFEXT(CPUSEL, cksel) + 1;
  303. return bus_clk_get_rate(clk, shift);
  304. }
  305. static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
  306. {
  307. u32 control;
  308. unsigned long parent_rate, child_div, actual_rate, div;
  309. parent_rate = clk->parent->get_rate(clk->parent);
  310. control = pm_readl(CKSEL);
  311. if (control & PM_BIT(HSBDIV))
  312. child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
  313. else
  314. child_div = 1;
  315. if (rate > 3 * (parent_rate / 4) || child_div == 1) {
  316. actual_rate = parent_rate;
  317. control &= ~PM_BIT(CPUDIV);
  318. } else {
  319. unsigned int cpusel;
  320. div = (parent_rate + rate / 2) / rate;
  321. if (div > child_div)
  322. div = child_div;
  323. cpusel = (div > 1) ? (fls(div) - 2) : 0;
  324. control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
  325. actual_rate = parent_rate / (1 << (cpusel + 1));
  326. }
  327. pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
  328. clk->name, rate, actual_rate);
  329. if (apply)
  330. pm_writel(CKSEL, control);
  331. return actual_rate;
  332. }
  333. static void hsb_clk_mode(struct clk *clk, int enabled)
  334. {
  335. unsigned long flags;
  336. u32 mask;
  337. spin_lock_irqsave(&pm_lock, flags);
  338. mask = pm_readl(HSB_MASK);
  339. if (enabled)
  340. mask |= 1 << clk->index;
  341. else
  342. mask &= ~(1 << clk->index);
  343. pm_writel(HSB_MASK, mask);
  344. spin_unlock_irqrestore(&pm_lock, flags);
  345. }
  346. static unsigned long hsb_clk_get_rate(struct clk *clk)
  347. {
  348. unsigned long cksel, shift = 0;
  349. cksel = pm_readl(CKSEL);
  350. if (cksel & PM_BIT(HSBDIV))
  351. shift = PM_BFEXT(HSBSEL, cksel) + 1;
  352. return bus_clk_get_rate(clk, shift);
  353. }
  354. void pba_clk_mode(struct clk *clk, int enabled)
  355. {
  356. unsigned long flags;
  357. u32 mask;
  358. spin_lock_irqsave(&pm_lock, flags);
  359. mask = pm_readl(PBA_MASK);
  360. if (enabled)
  361. mask |= 1 << clk->index;
  362. else
  363. mask &= ~(1 << clk->index);
  364. pm_writel(PBA_MASK, mask);
  365. spin_unlock_irqrestore(&pm_lock, flags);
  366. }
  367. unsigned long pba_clk_get_rate(struct clk *clk)
  368. {
  369. unsigned long cksel, shift = 0;
  370. cksel = pm_readl(CKSEL);
  371. if (cksel & PM_BIT(PBADIV))
  372. shift = PM_BFEXT(PBASEL, cksel) + 1;
  373. return bus_clk_get_rate(clk, shift);
  374. }
  375. static void pbb_clk_mode(struct clk *clk, int enabled)
  376. {
  377. unsigned long flags;
  378. u32 mask;
  379. spin_lock_irqsave(&pm_lock, flags);
  380. mask = pm_readl(PBB_MASK);
  381. if (enabled)
  382. mask |= 1 << clk->index;
  383. else
  384. mask &= ~(1 << clk->index);
  385. pm_writel(PBB_MASK, mask);
  386. spin_unlock_irqrestore(&pm_lock, flags);
  387. }
  388. static unsigned long pbb_clk_get_rate(struct clk *clk)
  389. {
  390. unsigned long cksel, shift = 0;
  391. cksel = pm_readl(CKSEL);
  392. if (cksel & PM_BIT(PBBDIV))
  393. shift = PM_BFEXT(PBBSEL, cksel) + 1;
  394. return bus_clk_get_rate(clk, shift);
  395. }
  396. static struct clk cpu_clk = {
  397. .name = "cpu",
  398. .get_rate = cpu_clk_get_rate,
  399. .set_rate = cpu_clk_set_rate,
  400. .users = 1,
  401. };
  402. static struct clk hsb_clk = {
  403. .name = "hsb",
  404. .parent = &cpu_clk,
  405. .get_rate = hsb_clk_get_rate,
  406. };
  407. static struct clk pba_clk = {
  408. .name = "pba",
  409. .parent = &hsb_clk,
  410. .mode = hsb_clk_mode,
  411. .get_rate = pba_clk_get_rate,
  412. .index = 1,
  413. };
  414. static struct clk pbb_clk = {
  415. .name = "pbb",
  416. .parent = &hsb_clk,
  417. .mode = hsb_clk_mode,
  418. .get_rate = pbb_clk_get_rate,
  419. .users = 1,
  420. .index = 2,
  421. };
  422. /* --------------------------------------------------------------------
  423. * Generic Clock operations
  424. * -------------------------------------------------------------------- */
  425. static void genclk_mode(struct clk *clk, int enabled)
  426. {
  427. u32 control;
  428. control = pm_readl(GCCTRL(clk->index));
  429. if (enabled)
  430. control |= PM_BIT(CEN);
  431. else
  432. control &= ~PM_BIT(CEN);
  433. pm_writel(GCCTRL(clk->index), control);
  434. }
  435. static unsigned long genclk_get_rate(struct clk *clk)
  436. {
  437. u32 control;
  438. unsigned long div = 1;
  439. control = pm_readl(GCCTRL(clk->index));
  440. if (control & PM_BIT(DIVEN))
  441. div = 2 * (PM_BFEXT(DIV, control) + 1);
  442. return clk->parent->get_rate(clk->parent) / div;
  443. }
  444. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  445. {
  446. u32 control;
  447. unsigned long parent_rate, actual_rate, div;
  448. parent_rate = clk->parent->get_rate(clk->parent);
  449. control = pm_readl(GCCTRL(clk->index));
  450. if (rate > 3 * parent_rate / 4) {
  451. actual_rate = parent_rate;
  452. control &= ~PM_BIT(DIVEN);
  453. } else {
  454. div = (parent_rate + rate) / (2 * rate) - 1;
  455. control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
  456. actual_rate = parent_rate / (2 * (div + 1));
  457. }
  458. dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
  459. clk->name, rate, actual_rate);
  460. if (apply)
  461. pm_writel(GCCTRL(clk->index), control);
  462. return actual_rate;
  463. }
  464. int genclk_set_parent(struct clk *clk, struct clk *parent)
  465. {
  466. u32 control;
  467. dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
  468. clk->name, parent->name, clk->parent->name);
  469. control = pm_readl(GCCTRL(clk->index));
  470. if (parent == &osc1 || parent == &pll1)
  471. control |= PM_BIT(OSCSEL);
  472. else if (parent == &osc0 || parent == &pll0)
  473. control &= ~PM_BIT(OSCSEL);
  474. else
  475. return -EINVAL;
  476. if (parent == &pll0 || parent == &pll1)
  477. control |= PM_BIT(PLLSEL);
  478. else
  479. control &= ~PM_BIT(PLLSEL);
  480. pm_writel(GCCTRL(clk->index), control);
  481. clk->parent = parent;
  482. return 0;
  483. }
  484. static void __init genclk_init_parent(struct clk *clk)
  485. {
  486. u32 control;
  487. struct clk *parent;
  488. BUG_ON(clk->index > 7);
  489. control = pm_readl(GCCTRL(clk->index));
  490. if (control & PM_BIT(OSCSEL))
  491. parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
  492. else
  493. parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
  494. clk->parent = parent;
  495. }
  496. static struct dw_dma_platform_data dw_dmac0_data = {
  497. .nr_channels = 3,
  498. .block_size = 4095U,
  499. .nr_masters = 2,
  500. .data_width = { 2, 2, 0, 0 },
  501. };
  502. static struct resource dw_dmac0_resource[] = {
  503. PBMEM(0xff200000),
  504. IRQ(2),
  505. };
  506. DEFINE_DEV_DATA(dw_dmac, 0);
  507. DEV_CLK(hclk, dw_dmac0, hsb, 10);
  508. /* --------------------------------------------------------------------
  509. * System peripherals
  510. * -------------------------------------------------------------------- */
  511. static struct resource at32_pm0_resource[] = {
  512. {
  513. .start = 0xfff00000,
  514. .end = 0xfff0007f,
  515. .flags = IORESOURCE_MEM,
  516. },
  517. IRQ(20),
  518. };
  519. static struct resource at32ap700x_rtc0_resource[] = {
  520. {
  521. .start = 0xfff00080,
  522. .end = 0xfff000af,
  523. .flags = IORESOURCE_MEM,
  524. },
  525. IRQ(21),
  526. };
  527. static struct resource at32_wdt0_resource[] = {
  528. {
  529. .start = 0xfff000b0,
  530. .end = 0xfff000cf,
  531. .flags = IORESOURCE_MEM,
  532. },
  533. };
  534. static struct resource at32_eic0_resource[] = {
  535. {
  536. .start = 0xfff00100,
  537. .end = 0xfff0013f,
  538. .flags = IORESOURCE_MEM,
  539. },
  540. IRQ(19),
  541. };
  542. DEFINE_DEV(at32_pm, 0);
  543. DEFINE_DEV(at32ap700x_rtc, 0);
  544. DEFINE_DEV(at32_wdt, 0);
  545. DEFINE_DEV(at32_eic, 0);
  546. /*
  547. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  548. * is always running.
  549. */
  550. static struct clk at32_pm_pclk = {
  551. .name = "pclk",
  552. .dev = &at32_pm0_device.dev,
  553. .parent = &pbb_clk,
  554. .mode = pbb_clk_mode,
  555. .get_rate = pbb_clk_get_rate,
  556. .users = 1,
  557. .index = 0,
  558. };
  559. static struct resource intc0_resource[] = {
  560. PBMEM(0xfff00400),
  561. };
  562. struct platform_device at32_intc0_device = {
  563. .name = "intc",
  564. .id = 0,
  565. .resource = intc0_resource,
  566. .num_resources = ARRAY_SIZE(intc0_resource),
  567. };
  568. DEV_CLK(pclk, at32_intc0, pbb, 1);
  569. static struct clk ebi_clk = {
  570. .name = "ebi",
  571. .parent = &hsb_clk,
  572. .mode = hsb_clk_mode,
  573. .get_rate = hsb_clk_get_rate,
  574. .users = 1,
  575. };
  576. static struct clk hramc_clk = {
  577. .name = "hramc",
  578. .parent = &hsb_clk,
  579. .mode = hsb_clk_mode,
  580. .get_rate = hsb_clk_get_rate,
  581. .users = 1,
  582. .index = 3,
  583. };
  584. static struct clk sdramc_clk = {
  585. .name = "sdramc_clk",
  586. .parent = &pbb_clk,
  587. .mode = pbb_clk_mode,
  588. .get_rate = pbb_clk_get_rate,
  589. .users = 1,
  590. .index = 14,
  591. };
  592. static struct resource smc0_resource[] = {
  593. PBMEM(0xfff03400),
  594. };
  595. DEFINE_DEV(smc, 0);
  596. DEV_CLK(pclk, smc0, pbb, 13);
  597. DEV_CLK(mck, smc0, hsb, 0);
  598. static struct platform_device pdc_device = {
  599. .name = "pdc",
  600. .id = 0,
  601. };
  602. DEV_CLK(hclk, pdc, hsb, 4);
  603. DEV_CLK(pclk, pdc, pba, 16);
  604. static struct clk pico_clk = {
  605. .name = "pico",
  606. .parent = &cpu_clk,
  607. .mode = cpu_clk_mode,
  608. .get_rate = cpu_clk_get_rate,
  609. .users = 1,
  610. };
  611. /* --------------------------------------------------------------------
  612. * HMATRIX
  613. * -------------------------------------------------------------------- */
  614. struct clk at32_hmatrix_clk = {
  615. .name = "hmatrix_clk",
  616. .parent = &pbb_clk,
  617. .mode = pbb_clk_mode,
  618. .get_rate = pbb_clk_get_rate,
  619. .index = 2,
  620. .users = 1,
  621. };
  622. /*
  623. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  624. * External Bus Interface (EBI). This can be used to enable special
  625. * features like CompactFlash support, NAND Flash support, etc. on
  626. * certain chipselects.
  627. */
  628. static inline void set_ebi_sfr_bits(u32 mask)
  629. {
  630. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);
  631. }
  632. /* --------------------------------------------------------------------
  633. * Timer/Counter (TC)
  634. * -------------------------------------------------------------------- */
  635. static struct resource at32_tcb0_resource[] = {
  636. PBMEM(0xfff00c00),
  637. IRQ(22),
  638. };
  639. static struct platform_device at32_tcb0_device = {
  640. .name = "atmel_tcb",
  641. .id = 0,
  642. .resource = at32_tcb0_resource,
  643. .num_resources = ARRAY_SIZE(at32_tcb0_resource),
  644. };
  645. DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
  646. static struct resource at32_tcb1_resource[] = {
  647. PBMEM(0xfff01000),
  648. IRQ(23),
  649. };
  650. static struct platform_device at32_tcb1_device = {
  651. .name = "atmel_tcb",
  652. .id = 1,
  653. .resource = at32_tcb1_resource,
  654. .num_resources = ARRAY_SIZE(at32_tcb1_resource),
  655. };
  656. DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
  657. /* --------------------------------------------------------------------
  658. * PIO
  659. * -------------------------------------------------------------------- */
  660. static struct resource pio0_resource[] = {
  661. PBMEM(0xffe02800),
  662. IRQ(13),
  663. };
  664. DEFINE_DEV(pio, 0);
  665. DEV_CLK(mck, pio0, pba, 10);
  666. static struct resource pio1_resource[] = {
  667. PBMEM(0xffe02c00),
  668. IRQ(14),
  669. };
  670. DEFINE_DEV(pio, 1);
  671. DEV_CLK(mck, pio1, pba, 11);
  672. static struct resource pio2_resource[] = {
  673. PBMEM(0xffe03000),
  674. IRQ(15),
  675. };
  676. DEFINE_DEV(pio, 2);
  677. DEV_CLK(mck, pio2, pba, 12);
  678. static struct resource pio3_resource[] = {
  679. PBMEM(0xffe03400),
  680. IRQ(16),
  681. };
  682. DEFINE_DEV(pio, 3);
  683. DEV_CLK(mck, pio3, pba, 13);
  684. static struct resource pio4_resource[] = {
  685. PBMEM(0xffe03800),
  686. IRQ(17),
  687. };
  688. DEFINE_DEV(pio, 4);
  689. DEV_CLK(mck, pio4, pba, 14);
  690. static int __init system_device_init(void)
  691. {
  692. platform_device_register(&at32_pm0_device);
  693. platform_device_register(&at32_intc0_device);
  694. platform_device_register(&at32ap700x_rtc0_device);
  695. platform_device_register(&at32_wdt0_device);
  696. platform_device_register(&at32_eic0_device);
  697. platform_device_register(&smc0_device);
  698. platform_device_register(&pdc_device);
  699. platform_device_register(&dw_dmac0_device);
  700. platform_device_register(&at32_tcb0_device);
  701. platform_device_register(&at32_tcb1_device);
  702. platform_device_register(&pio0_device);
  703. platform_device_register(&pio1_device);
  704. platform_device_register(&pio2_device);
  705. platform_device_register(&pio3_device);
  706. platform_device_register(&pio4_device);
  707. return 0;
  708. }
  709. core_initcall(system_device_init);
  710. /* --------------------------------------------------------------------
  711. * PSIF
  712. * -------------------------------------------------------------------- */
  713. static struct resource atmel_psif0_resource[] __initdata = {
  714. {
  715. .start = 0xffe03c00,
  716. .end = 0xffe03cff,
  717. .flags = IORESOURCE_MEM,
  718. },
  719. IRQ(18),
  720. };
  721. static struct clk atmel_psif0_pclk = {
  722. .name = "pclk",
  723. .parent = &pba_clk,
  724. .mode = pba_clk_mode,
  725. .get_rate = pba_clk_get_rate,
  726. .index = 15,
  727. };
  728. static struct resource atmel_psif1_resource[] __initdata = {
  729. {
  730. .start = 0xffe03d00,
  731. .end = 0xffe03dff,
  732. .flags = IORESOURCE_MEM,
  733. },
  734. IRQ(18),
  735. };
  736. static struct clk atmel_psif1_pclk = {
  737. .name = "pclk",
  738. .parent = &pba_clk,
  739. .mode = pba_clk_mode,
  740. .get_rate = pba_clk_get_rate,
  741. .index = 15,
  742. };
  743. struct platform_device *__init at32_add_device_psif(unsigned int id)
  744. {
  745. struct platform_device *pdev;
  746. u32 pin_mask;
  747. if (!(id == 0 || id == 1))
  748. return NULL;
  749. pdev = platform_device_alloc("atmel_psif", id);
  750. if (!pdev)
  751. return NULL;
  752. switch (id) {
  753. case 0:
  754. pin_mask = (1 << 8) | (1 << 9); /* CLOCK & DATA */
  755. if (platform_device_add_resources(pdev, atmel_psif0_resource,
  756. ARRAY_SIZE(atmel_psif0_resource)))
  757. goto err_add_resources;
  758. atmel_psif0_pclk.dev = &pdev->dev;
  759. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  760. break;
  761. case 1:
  762. pin_mask = (1 << 11) | (1 << 12); /* CLOCK & DATA */
  763. if (platform_device_add_resources(pdev, atmel_psif1_resource,
  764. ARRAY_SIZE(atmel_psif1_resource)))
  765. goto err_add_resources;
  766. atmel_psif1_pclk.dev = &pdev->dev;
  767. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  768. break;
  769. default:
  770. return NULL;
  771. }
  772. platform_device_add(pdev);
  773. return pdev;
  774. err_add_resources:
  775. platform_device_put(pdev);
  776. return NULL;
  777. }
  778. /* --------------------------------------------------------------------
  779. * USART
  780. * -------------------------------------------------------------------- */
  781. static struct atmel_uart_data atmel_usart0_data = {
  782. .use_dma_tx = 1,
  783. .use_dma_rx = 1,
  784. };
  785. static struct resource atmel_usart0_resource[] = {
  786. PBMEM(0xffe00c00),
  787. IRQ(6),
  788. };
  789. DEFINE_DEV_DATA(atmel_usart, 0);
  790. DEV_CLK(usart, atmel_usart0, pba, 3);
  791. static struct atmel_uart_data atmel_usart1_data = {
  792. .use_dma_tx = 1,
  793. .use_dma_rx = 1,
  794. };
  795. static struct resource atmel_usart1_resource[] = {
  796. PBMEM(0xffe01000),
  797. IRQ(7),
  798. };
  799. DEFINE_DEV_DATA(atmel_usart, 1);
  800. DEV_CLK(usart, atmel_usart1, pba, 4);
  801. static struct atmel_uart_data atmel_usart2_data = {
  802. .use_dma_tx = 1,
  803. .use_dma_rx = 1,
  804. };
  805. static struct resource atmel_usart2_resource[] = {
  806. PBMEM(0xffe01400),
  807. IRQ(8),
  808. };
  809. DEFINE_DEV_DATA(atmel_usart, 2);
  810. DEV_CLK(usart, atmel_usart2, pba, 5);
  811. static struct atmel_uart_data atmel_usart3_data = {
  812. .use_dma_tx = 1,
  813. .use_dma_rx = 1,
  814. };
  815. static struct resource atmel_usart3_resource[] = {
  816. PBMEM(0xffe01800),
  817. IRQ(9),
  818. };
  819. DEFINE_DEV_DATA(atmel_usart, 3);
  820. DEV_CLK(usart, atmel_usart3, pba, 6);
  821. static inline void configure_usart0_pins(int flags)
  822. {
  823. u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
  824. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 6);
  825. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 7);
  826. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 10);
  827. select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  828. }
  829. static inline void configure_usart1_pins(int flags)
  830. {
  831. u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
  832. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 19);
  833. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 20);
  834. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 16);
  835. select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
  836. }
  837. static inline void configure_usart2_pins(int flags)
  838. {
  839. u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
  840. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 30);
  841. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 29);
  842. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 28);
  843. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  844. }
  845. static inline void configure_usart3_pins(int flags)
  846. {
  847. u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
  848. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 16);
  849. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 15);
  850. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 19);
  851. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  852. }
  853. static struct platform_device *__initdata at32_usarts[4];
  854. void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags)
  855. {
  856. struct platform_device *pdev;
  857. struct atmel_uart_data *pdata;
  858. switch (hw_id) {
  859. case 0:
  860. pdev = &atmel_usart0_device;
  861. configure_usart0_pins(flags);
  862. break;
  863. case 1:
  864. pdev = &atmel_usart1_device;
  865. configure_usart1_pins(flags);
  866. break;
  867. case 2:
  868. pdev = &atmel_usart2_device;
  869. configure_usart2_pins(flags);
  870. break;
  871. case 3:
  872. pdev = &atmel_usart3_device;
  873. configure_usart3_pins(flags);
  874. break;
  875. default:
  876. return;
  877. }
  878. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  879. /* Addresses in the P4 segment are permanently mapped 1:1 */
  880. struct atmel_uart_data *data = pdev->dev.platform_data;
  881. data->regs = (void __iomem *)pdev->resource[0].start;
  882. }
  883. pdev->id = line;
  884. pdata = pdev->dev.platform_data;
  885. pdata->num = line;
  886. at32_usarts[line] = pdev;
  887. }
  888. struct platform_device *__init at32_add_device_usart(unsigned int id)
  889. {
  890. platform_device_register(at32_usarts[id]);
  891. return at32_usarts[id];
  892. }
  893. void __init at32_setup_serial_console(unsigned int usart_id)
  894. {
  895. atmel_default_console_device = at32_usarts[usart_id];
  896. }
  897. /* --------------------------------------------------------------------
  898. * Ethernet
  899. * -------------------------------------------------------------------- */
  900. #ifdef CONFIG_CPU_AT32AP7000
  901. static struct macb_platform_data macb0_data;
  902. static struct resource macb0_resource[] = {
  903. PBMEM(0xfff01800),
  904. IRQ(25),
  905. };
  906. DEFINE_DEV_DATA(macb, 0);
  907. DEV_CLK(hclk, macb0, hsb, 8);
  908. DEV_CLK(pclk, macb0, pbb, 6);
  909. static struct macb_platform_data macb1_data;
  910. static struct resource macb1_resource[] = {
  911. PBMEM(0xfff01c00),
  912. IRQ(26),
  913. };
  914. DEFINE_DEV_DATA(macb, 1);
  915. DEV_CLK(hclk, macb1, hsb, 9);
  916. DEV_CLK(pclk, macb1, pbb, 7);
  917. struct platform_device *__init
  918. at32_add_device_eth(unsigned int id, struct macb_platform_data *data)
  919. {
  920. struct platform_device *pdev;
  921. u32 pin_mask;
  922. switch (id) {
  923. case 0:
  924. pdev = &macb0_device;
  925. pin_mask = (1 << 3); /* TXD0 */
  926. pin_mask |= (1 << 4); /* TXD1 */
  927. pin_mask |= (1 << 7); /* TXEN */
  928. pin_mask |= (1 << 8); /* TXCK */
  929. pin_mask |= (1 << 9); /* RXD0 */
  930. pin_mask |= (1 << 10); /* RXD1 */
  931. pin_mask |= (1 << 13); /* RXER */
  932. pin_mask |= (1 << 15); /* RXDV */
  933. pin_mask |= (1 << 16); /* MDC */
  934. pin_mask |= (1 << 17); /* MDIO */
  935. if (!data->is_rmii) {
  936. pin_mask |= (1 << 0); /* COL */
  937. pin_mask |= (1 << 1); /* CRS */
  938. pin_mask |= (1 << 2); /* TXER */
  939. pin_mask |= (1 << 5); /* TXD2 */
  940. pin_mask |= (1 << 6); /* TXD3 */
  941. pin_mask |= (1 << 11); /* RXD2 */
  942. pin_mask |= (1 << 12); /* RXD3 */
  943. pin_mask |= (1 << 14); /* RXCK */
  944. #ifndef CONFIG_BOARD_MIMC200
  945. pin_mask |= (1 << 18); /* SPD */
  946. #endif
  947. }
  948. select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
  949. break;
  950. case 1:
  951. pdev = &macb1_device;
  952. pin_mask = (1 << 13); /* TXD0 */
  953. pin_mask |= (1 << 14); /* TXD1 */
  954. pin_mask |= (1 << 11); /* TXEN */
  955. pin_mask |= (1 << 12); /* TXCK */
  956. pin_mask |= (1 << 10); /* RXD0 */
  957. pin_mask |= (1 << 6); /* RXD1 */
  958. pin_mask |= (1 << 5); /* RXER */
  959. pin_mask |= (1 << 4); /* RXDV */
  960. pin_mask |= (1 << 3); /* MDC */
  961. pin_mask |= (1 << 2); /* MDIO */
  962. #ifndef CONFIG_BOARD_MIMC200
  963. if (!data->is_rmii)
  964. pin_mask |= (1 << 15); /* SPD */
  965. #endif
  966. select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
  967. if (!data->is_rmii) {
  968. pin_mask = (1 << 19); /* COL */
  969. pin_mask |= (1 << 23); /* CRS */
  970. pin_mask |= (1 << 26); /* TXER */
  971. pin_mask |= (1 << 27); /* TXD2 */
  972. pin_mask |= (1 << 28); /* TXD3 */
  973. pin_mask |= (1 << 29); /* RXD2 */
  974. pin_mask |= (1 << 30); /* RXD3 */
  975. pin_mask |= (1 << 24); /* RXCK */
  976. select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
  977. }
  978. break;
  979. default:
  980. return NULL;
  981. }
  982. memcpy(pdev->dev.platform_data, data, sizeof(struct macb_platform_data));
  983. platform_device_register(pdev);
  984. return pdev;
  985. }
  986. #endif
  987. /* --------------------------------------------------------------------
  988. * SPI
  989. * -------------------------------------------------------------------- */
  990. static struct resource atmel_spi0_resource[] = {
  991. PBMEM(0xffe00000),
  992. IRQ(3),
  993. };
  994. DEFINE_DEV(atmel_spi, 0);
  995. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  996. static struct resource atmel_spi1_resource[] = {
  997. PBMEM(0xffe00400),
  998. IRQ(4),
  999. };
  1000. DEFINE_DEV(atmel_spi, 1);
  1001. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  1002. void __init
  1003. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b, unsigned int n)
  1004. {
  1005. /*
  1006. * Manage the chipselects as GPIOs, normally using the same pins
  1007. * the SPI controller expects; but boards can use other pins.
  1008. */
  1009. static u8 __initdata spi_pins[][4] = {
  1010. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  1011. GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
  1012. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  1013. GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
  1014. };
  1015. unsigned int pin, mode;
  1016. /* There are only 2 SPI controllers */
  1017. if (bus_num > 1)
  1018. return;
  1019. for (; n; n--, b++) {
  1020. b->bus_num = bus_num;
  1021. if (b->chip_select >= 4)
  1022. continue;
  1023. pin = (unsigned)b->controller_data;
  1024. if (!pin) {
  1025. pin = spi_pins[bus_num][b->chip_select];
  1026. b->controller_data = (void *)pin;
  1027. }
  1028. mode = AT32_GPIOF_OUTPUT;
  1029. if (!(b->mode & SPI_CS_HIGH))
  1030. mode |= AT32_GPIOF_HIGH;
  1031. at32_select_gpio(pin, mode);
  1032. }
  1033. }
  1034. struct platform_device *__init
  1035. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  1036. {
  1037. struct platform_device *pdev;
  1038. u32 pin_mask;
  1039. switch (id) {
  1040. case 0:
  1041. pdev = &atmel_spi0_device;
  1042. pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
  1043. /* pullup MISO so a level is always defined */
  1044. select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
  1045. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1046. at32_spi_setup_slaves(0, b, n);
  1047. break;
  1048. case 1:
  1049. pdev = &atmel_spi1_device;
  1050. pin_mask = (1 << 1) | (1 << 5); /* MOSI */
  1051. /* pullup MISO so a level is always defined */
  1052. select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
  1053. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1054. at32_spi_setup_slaves(1, b, n);
  1055. break;
  1056. default:
  1057. return NULL;
  1058. }
  1059. spi_register_board_info(b, n);
  1060. platform_device_register(pdev);
  1061. return pdev;
  1062. }
  1063. /* --------------------------------------------------------------------
  1064. * TWI
  1065. * -------------------------------------------------------------------- */
  1066. static struct resource atmel_twi0_resource[] __initdata = {
  1067. PBMEM(0xffe00800),
  1068. IRQ(5),
  1069. };
  1070. static struct clk atmel_twi0_pclk = {
  1071. .name = "twi_pclk",
  1072. .parent = &pba_clk,
  1073. .mode = pba_clk_mode,
  1074. .get_rate = pba_clk_get_rate,
  1075. .index = 2,
  1076. };
  1077. struct platform_device *__init at32_add_device_twi(unsigned int id,
  1078. struct i2c_board_info *b,
  1079. unsigned int n)
  1080. {
  1081. struct platform_device *pdev;
  1082. u32 pin_mask;
  1083. if (id != 0)
  1084. return NULL;
  1085. pdev = platform_device_alloc("atmel_twi", id);
  1086. if (!pdev)
  1087. return NULL;
  1088. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  1089. ARRAY_SIZE(atmel_twi0_resource)))
  1090. goto err_add_resources;
  1091. pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
  1092. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1093. atmel_twi0_pclk.dev = &pdev->dev;
  1094. if (b)
  1095. i2c_register_board_info(id, b, n);
  1096. platform_device_add(pdev);
  1097. return pdev;
  1098. err_add_resources:
  1099. platform_device_put(pdev);
  1100. return NULL;
  1101. }
  1102. /* --------------------------------------------------------------------
  1103. * MMC
  1104. * -------------------------------------------------------------------- */
  1105. static struct resource atmel_mci0_resource[] __initdata = {
  1106. PBMEM(0xfff02400),
  1107. IRQ(28),
  1108. };
  1109. static struct clk atmel_mci0_pclk = {
  1110. .name = "mci_clk",
  1111. .parent = &pbb_clk,
  1112. .mode = pbb_clk_mode,
  1113. .get_rate = pbb_clk_get_rate,
  1114. .index = 9,
  1115. };
  1116. struct platform_device *__init
  1117. at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
  1118. {
  1119. struct platform_device *pdev;
  1120. struct mci_dma_data *slave;
  1121. u32 pioa_mask;
  1122. u32 piob_mask;
  1123. if (id != 0 || !data)
  1124. return NULL;
  1125. /* Must have at least one usable slot */
  1126. if (!data->slot[0].bus_width && !data->slot[1].bus_width)
  1127. return NULL;
  1128. pdev = platform_device_alloc("atmel_mci", id);
  1129. if (!pdev)
  1130. goto fail;
  1131. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  1132. ARRAY_SIZE(atmel_mci0_resource)))
  1133. goto fail;
  1134. slave = kzalloc(sizeof(struct mci_dma_data), GFP_KERNEL);
  1135. if (!slave)
  1136. goto fail;
  1137. slave->sdata.dma_dev = &dw_dmac0_device.dev;
  1138. slave->sdata.cfg_hi = (DWC_CFGH_SRC_PER(0)
  1139. | DWC_CFGH_DST_PER(1));
  1140. slave->sdata.cfg_lo &= ~(DWC_CFGL_HS_DST_POL
  1141. | DWC_CFGL_HS_SRC_POL);
  1142. data->dma_slave = slave;
  1143. if (platform_device_add_data(pdev, data,
  1144. sizeof(struct mci_platform_data)))
  1145. goto fail_free;
  1146. /* CLK line is common to both slots */
  1147. pioa_mask = 1 << 10;
  1148. switch (data->slot[0].bus_width) {
  1149. case 4:
  1150. pioa_mask |= 1 << 13; /* DATA1 */
  1151. pioa_mask |= 1 << 14; /* DATA2 */
  1152. pioa_mask |= 1 << 15; /* DATA3 */
  1153. /* fall through */
  1154. case 1:
  1155. pioa_mask |= 1 << 11; /* CMD */
  1156. pioa_mask |= 1 << 12; /* DATA0 */
  1157. if (gpio_is_valid(data->slot[0].detect_pin))
  1158. at32_select_gpio(data->slot[0].detect_pin, 0);
  1159. if (gpio_is_valid(data->slot[0].wp_pin))
  1160. at32_select_gpio(data->slot[0].wp_pin, 0);
  1161. break;
  1162. case 0:
  1163. /* Slot is unused */
  1164. break;
  1165. default:
  1166. goto fail_free;
  1167. }
  1168. select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
  1169. piob_mask = 0;
  1170. switch (data->slot[1].bus_width) {
  1171. case 4:
  1172. piob_mask |= 1 << 8; /* DATA1 */
  1173. piob_mask |= 1 << 9; /* DATA2 */
  1174. piob_mask |= 1 << 10; /* DATA3 */
  1175. /* fall through */
  1176. case 1:
  1177. piob_mask |= 1 << 6; /* CMD */
  1178. piob_mask |= 1 << 7; /* DATA0 */
  1179. select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
  1180. if (gpio_is_valid(data->slot[1].detect_pin))
  1181. at32_select_gpio(data->slot[1].detect_pin, 0);
  1182. if (gpio_is_valid(data->slot[1].wp_pin))
  1183. at32_select_gpio(data->slot[1].wp_pin, 0);
  1184. break;
  1185. case 0:
  1186. /* Slot is unused */
  1187. break;