at32ap700x.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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/fb.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/usb/atmel_usba_udc.h>
  15. #include <asm/io.h>
  16. #include <asm/irq.h>
  17. #include <asm/arch/at32ap700x.h>
  18. #include <asm/arch/board.h>
  19. #include <asm/arch/portmux.h>
  20. #include <video/atmel_lcdc.h>
  21. #include "clock.h"
  22. #include "hmatrix.h"
  23. #include "pio.h"
  24. #include "pm.h"
  25. #define PBMEM(base) \
  26. { \
  27. .start = base, \
  28. .end = base + 0x3ff, \
  29. .flags = IORESOURCE_MEM, \
  30. }
  31. #define IRQ(num) \
  32. { \
  33. .start = num, \
  34. .end = num, \
  35. .flags = IORESOURCE_IRQ, \
  36. }
  37. #define NAMED_IRQ(num, _name) \
  38. { \
  39. .start = num, \
  40. .end = num, \
  41. .name = _name, \
  42. .flags = IORESOURCE_IRQ, \
  43. }
  44. /* REVISIT these assume *every* device supports DMA, but several
  45. * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
  46. */
  47. #define DEFINE_DEV(_name, _id) \
  48. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  49. static struct platform_device _name##_id##_device = { \
  50. .name = #_name, \
  51. .id = _id, \
  52. .dev = { \
  53. .dma_mask = &_name##_id##_dma_mask, \
  54. .coherent_dma_mask = DMA_32BIT_MASK, \
  55. }, \
  56. .resource = _name##_id##_resource, \
  57. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  58. }
  59. #define DEFINE_DEV_DATA(_name, _id) \
  60. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  61. static struct platform_device _name##_id##_device = { \
  62. .name = #_name, \
  63. .id = _id, \
  64. .dev = { \
  65. .dma_mask = &_name##_id##_dma_mask, \
  66. .platform_data = &_name##_id##_data, \
  67. .coherent_dma_mask = DMA_32BIT_MASK, \
  68. }, \
  69. .resource = _name##_id##_resource, \
  70. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  71. }
  72. #define select_peripheral(pin, periph, flags) \
  73. at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
  74. #define DEV_CLK(_name, devname, bus, _index) \
  75. static struct clk devname##_##_name = { \
  76. .name = #_name, \
  77. .dev = &devname##_device.dev, \
  78. .parent = &bus##_clk, \
  79. .mode = bus##_clk_mode, \
  80. .get_rate = bus##_clk_get_rate, \
  81. .index = _index, \
  82. }
  83. static DEFINE_SPINLOCK(pm_lock);
  84. unsigned long at32ap7000_osc_rates[3] = {
  85. [0] = 32768,
  86. /* FIXME: these are ATSTK1002-specific */
  87. [1] = 20000000,
  88. [2] = 12000000,
  89. };
  90. static unsigned long osc_get_rate(struct clk *clk)
  91. {
  92. return at32ap7000_osc_rates[clk->index];
  93. }
  94. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  95. {
  96. unsigned long div, mul, rate;
  97. if (!(control & PM_BIT(PLLEN)))
  98. return 0;
  99. div = PM_BFEXT(PLLDIV, control) + 1;
  100. mul = PM_BFEXT(PLLMUL, control) + 1;
  101. rate = clk->parent->get_rate(clk->parent);
  102. rate = (rate + div / 2) / div;
  103. rate *= mul;
  104. return rate;
  105. }
  106. static unsigned long pll0_get_rate(struct clk *clk)
  107. {
  108. u32 control;
  109. control = pm_readl(PLL0);
  110. return pll_get_rate(clk, control);
  111. }
  112. static unsigned long pll1_get_rate(struct clk *clk)
  113. {
  114. u32 control;
  115. control = pm_readl(PLL1);
  116. return pll_get_rate(clk, control);
  117. }
  118. /*
  119. * The AT32AP7000 has five primary clock sources: One 32kHz
  120. * oscillator, two crystal oscillators and two PLLs.
  121. */
  122. static struct clk osc32k = {
  123. .name = "osc32k",
  124. .get_rate = osc_get_rate,
  125. .users = 1,
  126. .index = 0,
  127. };
  128. static struct clk osc0 = {
  129. .name = "osc0",
  130. .get_rate = osc_get_rate,
  131. .users = 1,
  132. .index = 1,
  133. };
  134. static struct clk osc1 = {
  135. .name = "osc1",
  136. .get_rate = osc_get_rate,
  137. .index = 2,
  138. };
  139. static struct clk pll0 = {
  140. .name = "pll0",
  141. .get_rate = pll0_get_rate,
  142. .parent = &osc0,
  143. };
  144. static struct clk pll1 = {
  145. .name = "pll1",
  146. .get_rate = pll1_get_rate,
  147. .parent = &osc0,
  148. };
  149. /*
  150. * The main clock can be either osc0 or pll0. The boot loader may
  151. * have chosen one for us, so we don't really know which one until we
  152. * have a look at the SM.
  153. */
  154. static struct clk *main_clock;
  155. /*
  156. * Synchronous clocks are generated from the main clock. The clocks
  157. * must satisfy the constraint
  158. * fCPU >= fHSB >= fPB
  159. * i.e. each clock must not be faster than its parent.
  160. */
  161. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  162. {
  163. return main_clock->get_rate(main_clock) >> shift;
  164. };
  165. static void cpu_clk_mode(struct clk *clk, int enabled)
  166. {
  167. unsigned long flags;
  168. u32 mask;
  169. spin_lock_irqsave(&pm_lock, flags);
  170. mask = pm_readl(CPU_MASK);
  171. if (enabled)
  172. mask |= 1 << clk->index;
  173. else
  174. mask &= ~(1 << clk->index);
  175. pm_writel(CPU_MASK, mask);
  176. spin_unlock_irqrestore(&pm_lock, flags);
  177. }
  178. static unsigned long cpu_clk_get_rate(struct clk *clk)
  179. {
  180. unsigned long cksel, shift = 0;
  181. cksel = pm_readl(CKSEL);
  182. if (cksel & PM_BIT(CPUDIV))
  183. shift = PM_BFEXT(CPUSEL, cksel) + 1;
  184. return bus_clk_get_rate(clk, shift);
  185. }
  186. static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
  187. {
  188. u32 control;
  189. unsigned long parent_rate, child_div, actual_rate, div;
  190. parent_rate = clk->parent->get_rate(clk->parent);
  191. control = pm_readl(CKSEL);
  192. if (control & PM_BIT(HSBDIV))
  193. child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
  194. else
  195. child_div = 1;
  196. if (rate > 3 * (parent_rate / 4) || child_div == 1) {
  197. actual_rate = parent_rate;
  198. control &= ~PM_BIT(CPUDIV);
  199. } else {
  200. unsigned int cpusel;
  201. div = (parent_rate + rate / 2) / rate;
  202. if (div > child_div)
  203. div = child_div;
  204. cpusel = (div > 1) ? (fls(div) - 2) : 0;
  205. control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
  206. actual_rate = parent_rate / (1 << (cpusel + 1));
  207. }
  208. pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
  209. clk->name, rate, actual_rate);
  210. if (apply)
  211. pm_writel(CKSEL, control);
  212. return actual_rate;
  213. }
  214. static void hsb_clk_mode(struct clk *clk, int enabled)
  215. {
  216. unsigned long flags;
  217. u32 mask;
  218. spin_lock_irqsave(&pm_lock, flags);
  219. mask = pm_readl(HSB_MASK);
  220. if (enabled)
  221. mask |= 1 << clk->index;
  222. else
  223. mask &= ~(1 << clk->index);
  224. pm_writel(HSB_MASK, mask);
  225. spin_unlock_irqrestore(&pm_lock, flags);
  226. }
  227. static unsigned long hsb_clk_get_rate(struct clk *clk)
  228. {
  229. unsigned long cksel, shift = 0;
  230. cksel = pm_readl(CKSEL);
  231. if (cksel & PM_BIT(HSBDIV))
  232. shift = PM_BFEXT(HSBSEL, cksel) + 1;
  233. return bus_clk_get_rate(clk, shift);
  234. }
  235. static void pba_clk_mode(struct clk *clk, int enabled)
  236. {
  237. unsigned long flags;
  238. u32 mask;
  239. spin_lock_irqsave(&pm_lock, flags);
  240. mask = pm_readl(PBA_MASK);
  241. if (enabled)
  242. mask |= 1 << clk->index;
  243. else
  244. mask &= ~(1 << clk->index);
  245. pm_writel(PBA_MASK, mask);
  246. spin_unlock_irqrestore(&pm_lock, flags);
  247. }
  248. static unsigned long pba_clk_get_rate(struct clk *clk)
  249. {
  250. unsigned long cksel, shift = 0;
  251. cksel = pm_readl(CKSEL);
  252. if (cksel & PM_BIT(PBADIV))
  253. shift = PM_BFEXT(PBASEL, cksel) + 1;
  254. return bus_clk_get_rate(clk, shift);
  255. }
  256. static void pbb_clk_mode(struct clk *clk, int enabled)
  257. {
  258. unsigned long flags;
  259. u32 mask;
  260. spin_lock_irqsave(&pm_lock, flags);
  261. mask = pm_readl(PBB_MASK);
  262. if (enabled)
  263. mask |= 1 << clk->index;
  264. else
  265. mask &= ~(1 << clk->index);
  266. pm_writel(PBB_MASK, mask);
  267. spin_unlock_irqrestore(&pm_lock, flags);
  268. }
  269. static unsigned long pbb_clk_get_rate(struct clk *clk)
  270. {
  271. unsigned long cksel, shift = 0;
  272. cksel = pm_readl(CKSEL);
  273. if (cksel & PM_BIT(PBBDIV))
  274. shift = PM_BFEXT(PBBSEL, cksel) + 1;
  275. return bus_clk_get_rate(clk, shift);
  276. }
  277. static struct clk cpu_clk = {
  278. .name = "cpu",
  279. .get_rate = cpu_clk_get_rate,
  280. .set_rate = cpu_clk_set_rate,
  281. .users = 1,
  282. };
  283. static struct clk hsb_clk = {
  284. .name = "hsb",
  285. .parent = &cpu_clk,
  286. .get_rate = hsb_clk_get_rate,
  287. };
  288. static struct clk pba_clk = {
  289. .name = "pba",
  290. .parent = &hsb_clk,
  291. .mode = hsb_clk_mode,
  292. .get_rate = pba_clk_get_rate,
  293. .index = 1,
  294. };
  295. static struct clk pbb_clk = {
  296. .name = "pbb",
  297. .parent = &hsb_clk,
  298. .mode = hsb_clk_mode,
  299. .get_rate = pbb_clk_get_rate,
  300. .users = 1,
  301. .index = 2,
  302. };
  303. /* --------------------------------------------------------------------
  304. * Generic Clock operations
  305. * -------------------------------------------------------------------- */
  306. static void genclk_mode(struct clk *clk, int enabled)
  307. {
  308. u32 control;
  309. control = pm_readl(GCCTRL(clk->index));
  310. if (enabled)
  311. control |= PM_BIT(CEN);
  312. else
  313. control &= ~PM_BIT(CEN);
  314. pm_writel(GCCTRL(clk->index), control);
  315. }
  316. static unsigned long genclk_get_rate(struct clk *clk)
  317. {
  318. u32 control;
  319. unsigned long div = 1;
  320. control = pm_readl(GCCTRL(clk->index));
  321. if (control & PM_BIT(DIVEN))
  322. div = 2 * (PM_BFEXT(DIV, control) + 1);
  323. return clk->parent->get_rate(clk->parent) / div;
  324. }
  325. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  326. {
  327. u32 control;
  328. unsigned long parent_rate, actual_rate, div;
  329. parent_rate = clk->parent->get_rate(clk->parent);
  330. control = pm_readl(GCCTRL(clk->index));
  331. if (rate > 3 * parent_rate / 4) {
  332. actual_rate = parent_rate;
  333. control &= ~PM_BIT(DIVEN);
  334. } else {
  335. div = (parent_rate + rate) / (2 * rate) - 1;
  336. control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
  337. actual_rate = parent_rate / (2 * (div + 1));
  338. }
  339. dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
  340. clk->name, rate, actual_rate);
  341. if (apply)
  342. pm_writel(GCCTRL(clk->index), control);
  343. return actual_rate;
  344. }
  345. int genclk_set_parent(struct clk *clk, struct clk *parent)
  346. {
  347. u32 control;
  348. dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
  349. clk->name, parent->name, clk->parent->name);
  350. control = pm_readl(GCCTRL(clk->index));
  351. if (parent == &osc1 || parent == &pll1)
  352. control |= PM_BIT(OSCSEL);
  353. else if (parent == &osc0 || parent == &pll0)
  354. control &= ~PM_BIT(OSCSEL);
  355. else
  356. return -EINVAL;
  357. if (parent == &pll0 || parent == &pll1)
  358. control |= PM_BIT(PLLSEL);
  359. else
  360. control &= ~PM_BIT(PLLSEL);
  361. pm_writel(GCCTRL(clk->index), control);
  362. clk->parent = parent;
  363. return 0;
  364. }
  365. static void __init genclk_init_parent(struct clk *clk)
  366. {
  367. u32 control;
  368. struct clk *parent;
  369. BUG_ON(clk->index > 7);
  370. control = pm_readl(GCCTRL(clk->index));
  371. if (control & PM_BIT(OSCSEL))
  372. parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
  373. else
  374. parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
  375. clk->parent = parent;
  376. }
  377. /* --------------------------------------------------------------------
  378. * System peripherals
  379. * -------------------------------------------------------------------- */
  380. static struct resource at32_pm0_resource[] = {
  381. {
  382. .start = 0xfff00000,
  383. .end = 0xfff0007f,
  384. .flags = IORESOURCE_MEM,
  385. },
  386. IRQ(20),
  387. };
  388. static struct resource at32ap700x_rtc0_resource[] = {
  389. {
  390. .start = 0xfff00080,
  391. .end = 0xfff000af,
  392. .flags = IORESOURCE_MEM,
  393. },
  394. IRQ(21),
  395. };
  396. static struct resource at32_wdt0_resource[] = {
  397. {
  398. .start = 0xfff000b0,
  399. .end = 0xfff000cf,
  400. .flags = IORESOURCE_MEM,
  401. },
  402. };
  403. static struct resource at32_eic0_resource[] = {
  404. {
  405. .start = 0xfff00100,
  406. .end = 0xfff0013f,
  407. .flags = IORESOURCE_MEM,
  408. },
  409. IRQ(19),
  410. };
  411. DEFINE_DEV(at32_pm, 0);
  412. DEFINE_DEV(at32ap700x_rtc, 0);
  413. DEFINE_DEV(at32_wdt, 0);
  414. DEFINE_DEV(at32_eic, 0);
  415. /*
  416. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  417. * is always running.
  418. */
  419. static struct clk at32_pm_pclk = {
  420. .name = "pclk",
  421. .dev = &at32_pm0_device.dev,
  422. .parent = &pbb_clk,
  423. .mode = pbb_clk_mode,
  424. .get_rate = pbb_clk_get_rate,
  425. .users = 1,
  426. .index = 0,
  427. };
  428. static struct resource intc0_resource[] = {
  429. PBMEM(0xfff00400),
  430. };
  431. struct platform_device at32_intc0_device = {
  432. .name = "intc",
  433. .id = 0,
  434. .resource = intc0_resource,
  435. .num_resources = ARRAY_SIZE(intc0_resource),
  436. };
  437. DEV_CLK(pclk, at32_intc0, pbb, 1);
  438. static struct clk ebi_clk = {
  439. .name = "ebi",
  440. .parent = &hsb_clk,
  441. .mode = hsb_clk_mode,
  442. .get_rate = hsb_clk_get_rate,
  443. .users = 1,
  444. };
  445. static struct clk hramc_clk = {
  446. .name = "hramc",
  447. .parent = &hsb_clk,
  448. .mode = hsb_clk_mode,
  449. .get_rate = hsb_clk_get_rate,
  450. .users = 1,
  451. .index = 3,
  452. };
  453. static struct resource smc0_resource[] = {
  454. PBMEM(0xfff03400),
  455. };
  456. DEFINE_DEV(smc, 0);
  457. DEV_CLK(pclk, smc0, pbb, 13);
  458. DEV_CLK(mck, smc0, hsb, 0);
  459. static struct platform_device pdc_device = {
  460. .name = "pdc",
  461. .id = 0,
  462. };
  463. DEV_CLK(hclk, pdc, hsb, 4);
  464. DEV_CLK(pclk, pdc, pba, 16);
  465. static struct clk pico_clk = {
  466. .name = "pico",
  467. .parent = &cpu_clk,
  468. .mode = cpu_clk_mode,
  469. .get_rate = cpu_clk_get_rate,
  470. .users = 1,
  471. };
  472. static struct resource dmaca0_resource[] = {
  473. {
  474. .start = 0xff200000,
  475. .end = 0xff20ffff,
  476. .flags = IORESOURCE_MEM,
  477. },
  478. IRQ(2),
  479. };
  480. DEFINE_DEV(dmaca, 0);
  481. DEV_CLK(hclk, dmaca0, hsb, 10);
  482. /* --------------------------------------------------------------------
  483. * HMATRIX
  484. * -------------------------------------------------------------------- */
  485. static struct clk hmatrix_clk = {
  486. .name = "hmatrix_clk",
  487. .parent = &pbb_clk,
  488. .mode = pbb_clk_mode,
  489. .get_rate = pbb_clk_get_rate,
  490. .index = 2,
  491. .users = 1,
  492. };
  493. #define HMATRIX_BASE ((void __iomem *)0xfff00800)
  494. #define hmatrix_readl(reg) \
  495. __raw_readl((HMATRIX_BASE) + HMATRIX_##reg)
  496. #define hmatrix_writel(reg,value) \
  497. __raw_writel((value), (HMATRIX_BASE) + HMATRIX_##reg)
  498. /*
  499. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  500. * External Bus Interface (EBI). This can be used to enable special
  501. * features like CompactFlash support, NAND Flash support, etc. on
  502. * certain chipselects.
  503. */
  504. static inline void set_ebi_sfr_bits(u32 mask)
  505. {
  506. u32 sfr;
  507. clk_enable(&hmatrix_clk);
  508. sfr = hmatrix_readl(SFR4);
  509. sfr |= mask;
  510. hmatrix_writel(SFR4, sfr);
  511. clk_disable(&hmatrix_clk);
  512. }
  513. /* --------------------------------------------------------------------
  514. * System Timer/Counter (TC)
  515. * -------------------------------------------------------------------- */
  516. static struct resource at32_systc0_resource[] = {
  517. PBMEM(0xfff00c00),
  518. IRQ(22),
  519. };
  520. struct platform_device at32_systc0_device = {
  521. .name = "systc",
  522. .id = 0,
  523. .resource = at32_systc0_resource,
  524. .num_resources = ARRAY_SIZE(at32_systc0_resource),
  525. };
  526. DEV_CLK(pclk, at32_systc0, pbb, 3);
  527. /* --------------------------------------------------------------------
  528. * PIO
  529. * -------------------------------------------------------------------- */
  530. static struct resource pio0_resource[] = {
  531. PBMEM(0xffe02800),
  532. IRQ(13),
  533. };
  534. DEFINE_DEV(pio, 0);
  535. DEV_CLK(mck, pio0, pba, 10);
  536. static struct resource pio1_resource[] = {
  537. PBMEM(0xffe02c00),
  538. IRQ(14),
  539. };
  540. DEFINE_DEV(pio, 1);
  541. DEV_CLK(mck, pio1, pba, 11);
  542. static struct resource pio2_resource[] = {
  543. PBMEM(0xffe03000),
  544. IRQ(15),
  545. };
  546. DEFINE_DEV(pio, 2);
  547. DEV_CLK(mck, pio2, pba, 12);
  548. static struct resource pio3_resource[] = {
  549. PBMEM(0xffe03400),
  550. IRQ(16),
  551. };
  552. DEFINE_DEV(pio, 3);
  553. DEV_CLK(mck, pio3, pba, 13);
  554. static struct resource pio4_resource[] = {
  555. PBMEM(0xffe03800),
  556. IRQ(17),
  557. };
  558. DEFINE_DEV(pio, 4);
  559. DEV_CLK(mck, pio4, pba, 14);
  560. void __init at32_add_system_devices(void)
  561. {
  562. platform_device_register(&at32_pm0_device);
  563. platform_device_register(&at32_intc0_device);
  564. platform_device_register(&at32ap700x_rtc0_device);
  565. platform_device_register(&at32_wdt0_device);
  566. platform_device_register(&at32_eic0_device);
  567. platform_device_register(&smc0_device);
  568. platform_device_register(&pdc_device);
  569. platform_device_register(&dmaca0_device);
  570. platform_device_register(&at32_systc0_device);
  571. platform_device_register(&pio0_device);
  572. platform_device_register(&pio1_device);
  573. platform_device_register(&pio2_device);
  574. platform_device_register(&pio3_device);
  575. platform_device_register(&pio4_device);
  576. }
  577. /* --------------------------------------------------------------------
  578. * USART
  579. * -------------------------------------------------------------------- */
  580. static struct atmel_uart_data atmel_usart0_data = {
  581. .use_dma_tx = 1,
  582. .use_dma_rx = 1,
  583. };
  584. static struct resource atmel_usart0_resource[] = {
  585. PBMEM(0xffe00c00),
  586. IRQ(6),
  587. };
  588. DEFINE_DEV_DATA(atmel_usart, 0);
  589. DEV_CLK(usart, atmel_usart0, pba, 3);
  590. static struct atmel_uart_data atmel_usart1_data = {
  591. .use_dma_tx = 1,
  592. .use_dma_rx = 1,
  593. };
  594. static struct resource atmel_usart1_resource[] = {
  595. PBMEM(0xffe01000),
  596. IRQ(7),
  597. };
  598. DEFINE_DEV_DATA(atmel_usart, 1);
  599. DEV_CLK(usart, atmel_usart1, pba, 4);
  600. static struct atmel_uart_data atmel_usart2_data = {
  601. .use_dma_tx = 1,
  602. .use_dma_rx = 1,
  603. };
  604. static struct resource atmel_usart2_resource[] = {
  605. PBMEM(0xffe01400),
  606. IRQ(8),
  607. };
  608. DEFINE_DEV_DATA(atmel_usart, 2);
  609. DEV_CLK(usart, atmel_usart2, pba, 5);
  610. static struct atmel_uart_data atmel_usart3_data = {
  611. .use_dma_tx = 1,
  612. .use_dma_rx = 1,
  613. };
  614. static struct resource atmel_usart3_resource[] = {
  615. PBMEM(0xffe01800),
  616. IRQ(9),
  617. };
  618. DEFINE_DEV_DATA(atmel_usart, 3);
  619. DEV_CLK(usart, atmel_usart3, pba, 6);
  620. static inline void configure_usart0_pins(void)
  621. {
  622. select_peripheral(PA(8), PERIPH_B, 0); /* RXD */
  623. select_peripheral(PA(9), PERIPH_B, 0); /* TXD */
  624. }
  625. static inline void configure_usart1_pins(void)
  626. {
  627. select_peripheral(PA(17), PERIPH_A, 0); /* RXD */
  628. select_peripheral(PA(18), PERIPH_A, 0); /* TXD */
  629. }
  630. static inline void configure_usart2_pins(void)
  631. {
  632. select_peripheral(PB(26), PERIPH_B, 0); /* RXD */
  633. select_peripheral(PB(27), PERIPH_B, 0); /* TXD */
  634. }
  635. static inline void configure_usart3_pins(void)
  636. {
  637. select_peripheral(PB(18), PERIPH_B, 0); /* RXD */
  638. select_peripheral(PB(17), PERIPH_B, 0); /* TXD */
  639. }
  640. static struct platform_device *__initdata at32_usarts[4];
  641. void __init at32_map_usart(unsigned int hw_id, unsigned int line)
  642. {
  643. struct platform_device *pdev;
  644. switch (hw_id) {
  645. case 0:
  646. pdev = &atmel_usart0_device;
  647. configure_usart0_pins();
  648. break;
  649. case 1:
  650. pdev = &atmel_usart1_device;
  651. configure_usart1_pins();
  652. break;
  653. case 2:
  654. pdev = &atmel_usart2_device;
  655. configure_usart2_pins();
  656. break;
  657. case 3:
  658. pdev = &atmel_usart3_device;
  659. configure_usart3_pins();
  660. break;
  661. default:
  662. return;
  663. }
  664. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  665. /* Addresses in the P4 segment are permanently mapped 1:1 */
  666. struct atmel_uart_data *data = pdev->dev.platform_data;
  667. data->regs = (void __iomem *)pdev->resource[0].start;
  668. }
  669. pdev->id = line;
  670. at32_usarts[line] = pdev;
  671. }
  672. struct platform_device *__init at32_add_device_usart(unsigned int id)
  673. {
  674. platform_device_register(at32_usarts[id]);
  675. return at32_usarts[id];
  676. }
  677. struct platform_device *atmel_default_console_device;
  678. void __init at32_setup_serial_console(unsigned int usart_id)
  679. {
  680. atmel_default_console_device = at32_usarts[usart_id];
  681. }
  682. /* --------------------------------------------------------------------
  683. * Ethernet
  684. * -------------------------------------------------------------------- */
  685. #ifdef CONFIG_CPU_AT32AP7000
  686. static struct eth_platform_data macb0_data;
  687. static struct resource macb0_resource[] = {
  688. PBMEM(0xfff01800),
  689. IRQ(25),
  690. };
  691. DEFINE_DEV_DATA(macb, 0);
  692. DEV_CLK(hclk, macb0, hsb, 8);
  693. DEV_CLK(pclk, macb0, pbb, 6);
  694. static struct eth_platform_data macb1_data;
  695. static struct resource macb1_resource[] = {
  696. PBMEM(0xfff01c00),
  697. IRQ(26),
  698. };
  699. DEFINE_DEV_DATA(macb, 1);
  700. DEV_CLK(hclk, macb1, hsb, 9);
  701. DEV_CLK(pclk, macb1, pbb, 7);
  702. struct platform_device *__init
  703. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  704. {
  705. struct platform_device *pdev;
  706. switch (id) {
  707. case 0:
  708. pdev = &macb0_device;
  709. select_peripheral(PC(3), PERIPH_A, 0); /* TXD0 */
  710. select_peripheral(PC(4), PERIPH_A, 0); /* TXD1 */
  711. select_peripheral(PC(7), PERIPH_A, 0); /* TXEN */
  712. select_peripheral(PC(8), PERIPH_A, 0); /* TXCK */
  713. select_peripheral(PC(9), PERIPH_A, 0); /* RXD0 */
  714. select_peripheral(PC(10), PERIPH_A, 0); /* RXD1 */
  715. select_peripheral(PC(13), PERIPH_A, 0); /* RXER */
  716. select_peripheral(PC(15), PERIPH_A, 0); /* RXDV */
  717. select_peripheral(PC(16), PERIPH_A, 0); /* MDC */
  718. select_peripheral(PC(17), PERIPH_A, 0); /* MDIO */
  719. if (!data->is_rmii) {
  720. select_peripheral(PC(0), PERIPH_A, 0); /* COL */
  721. select_peripheral(PC(1), PERIPH_A, 0); /* CRS */
  722. select_peripheral(PC(2), PERIPH_A, 0); /* TXER */
  723. select_peripheral(PC(5), PERIPH_A, 0); /* TXD2 */
  724. select_peripheral(PC(6), PERIPH_A, 0); /* TXD3 */
  725. select_peripheral(PC(11), PERIPH_A, 0); /* RXD2 */
  726. select_peripheral(PC(12), PERIPH_A, 0); /* RXD3 */
  727. select_peripheral(PC(14), PERIPH_A, 0); /* RXCK */
  728. select_peripheral(PC(18), PERIPH_A, 0); /* SPD */
  729. }
  730. break;
  731. case 1:
  732. pdev = &macb1_device;
  733. select_peripheral(PD(13), PERIPH_B, 0); /* TXD0 */
  734. select_peripheral(PD(14), PERIPH_B, 0); /* TXD1 */
  735. select_peripheral(PD(11), PERIPH_B, 0); /* TXEN */
  736. select_peripheral(PD(12), PERIPH_B, 0); /* TXCK */
  737. select_peripheral(PD(10), PERIPH_B, 0); /* RXD0 */
  738. select_peripheral(PD(6), PERIPH_B, 0); /* RXD1 */
  739. select_peripheral(PD(5), PERIPH_B, 0); /* RXER */
  740. select_peripheral(PD(4), PERIPH_B, 0); /* RXDV */
  741. select_peripheral(PD(3), PERIPH_B, 0); /* MDC */
  742. select_peripheral(PD(2), PERIPH_B, 0); /* MDIO */
  743. if (!data->is_rmii) {
  744. select_peripheral(PC(19), PERIPH_B, 0); /* COL */
  745. select_peripheral(PC(23), PERIPH_B, 0); /* CRS */
  746. select_peripheral(PC(26), PERIPH_B, 0); /* TXER */
  747. select_peripheral(PC(27), PERIPH_B, 0); /* TXD2 */
  748. select_peripheral(PC(28), PERIPH_B, 0); /* TXD3 */
  749. select_peripheral(PC(29), PERIPH_B, 0); /* RXD2 */
  750. select_peripheral(PC(30), PERIPH_B, 0); /* RXD3 */
  751. select_peripheral(PC(24), PERIPH_B, 0); /* RXCK */
  752. select_peripheral(PD(15), PERIPH_B, 0); /* SPD */
  753. }
  754. break;
  755. default:
  756. return NULL;
  757. }
  758. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  759. platform_device_register(pdev);
  760. return pdev;
  761. }
  762. #endif
  763. /* --------------------------------------------------------------------
  764. * SPI
  765. * -------------------------------------------------------------------- */
  766. static struct resource atmel_spi0_resource[] = {
  767. PBMEM(0xffe00000),
  768. IRQ(3),
  769. };
  770. DEFINE_DEV(atmel_spi, 0);
  771. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  772. static struct resource atmel_spi1_resource[] = {
  773. PBMEM(0xffe00400),
  774. IRQ(4),
  775. };
  776. DEFINE_DEV(atmel_spi, 1);
  777. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  778. static void __init
  779. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
  780. unsigned int n, const u8 *pins)
  781. {
  782. unsigned int pin, mode;
  783. for (; n; n--, b++) {
  784. b->bus_num = bus_num;
  785. if (b->chip_select >= 4)
  786. continue;
  787. pin = (unsigned)b->controller_data;
  788. if (!pin) {
  789. pin = pins[b->chip_select];
  790. b->controller_data = (void *)pin;
  791. }
  792. mode = AT32_GPIOF_OUTPUT;
  793. if (!(b->mode & SPI_CS_HIGH))
  794. mode |= AT32_GPIOF_HIGH;
  795. at32_select_gpio(pin, mode);
  796. }
  797. }
  798. struct platform_device *__init
  799. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  800. {
  801. /*
  802. * Manage the chipselects as GPIOs, normally using the same pins
  803. * the SPI controller expects; but boards can use other pins.
  804. */
  805. static u8 __initdata spi0_pins[] =
  806. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  807. GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
  808. static u8 __initdata spi1_pins[] =
  809. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  810. GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
  811. struct platform_device *pdev;
  812. switch (id) {
  813. case 0:
  814. pdev = &atmel_spi0_device;
  815. select_peripheral(PA(0), PERIPH_A, 0); /* MISO */
  816. select_peripheral(PA(1), PERIPH_A, 0); /* MOSI */
  817. select_peripheral(PA(2), PERIPH_A, 0); /* SCK */
  818. at32_spi_setup_slaves(0, b, n, spi0_pins);
  819. break;
  820. case 1:
  821. pdev = &atmel_spi1_device;
  822. select_peripheral(PB(0), PERIPH_B, 0); /* MISO */
  823. select_peripheral(PB(1), PERIPH_B, 0); /* MOSI */
  824. select_peripheral(PB(5), PERIPH_B, 0); /* SCK */
  825. at32_spi_setup_slaves(1, b, n, spi1_pins);
  826. break;
  827. default:
  828. return NULL;
  829. }
  830. spi_register_board_info(b, n);
  831. platform_device_register(pdev);
  832. return pdev;
  833. }
  834. /* --------------------------------------------------------------------
  835. * TWI
  836. * -------------------------------------------------------------------- */
  837. static struct resource atmel_twi0_resource[] __initdata = {
  838. PBMEM(0xffe00800),
  839. IRQ(5),
  840. };
  841. static struct clk atmel_twi0_pclk = {
  842. .name = "twi_pclk",
  843. .parent = &pba_clk,
  844. .mode = pba_clk_mode,
  845. .get_rate = pba_clk_get_rate,
  846. .index = 2,
  847. };
  848. struct platform_device *__init at32_add_device_twi(unsigned int id,
  849. struct i2c_board_info *b,
  850. unsigned int n)
  851. {
  852. struct platform_device *pdev;
  853. if (id != 0)
  854. return NULL;
  855. pdev = platform_device_alloc("atmel_twi", id);
  856. if (!pdev)
  857. return NULL;
  858. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  859. ARRAY_SIZE(atmel_twi0_resource)))
  860. goto err_add_resources;
  861. select_peripheral(PA(6), PERIPH_A, 0); /* SDA */
  862. select_peripheral(PA(7), PERIPH_A, 0); /* SDL */
  863. atmel_twi0_pclk.dev = &pdev->dev;
  864. if (b)
  865. i2c_register_board_info(id, b, n);
  866. platform_device_add(pdev);
  867. return pdev;
  868. err_add_resources:
  869. platform_device_put(pdev);
  870. return NULL;
  871. }
  872. /* --------------------------------------------------------------------
  873. * MMC
  874. * -------------------------------------------------------------------- */
  875. static struct resource atmel_mci0_resource[] __initdata = {
  876. PBMEM(0xfff02400),
  877. IRQ(28),
  878. };
  879. static struct clk atmel_mci0_pclk = {
  880. .name = "mci_clk",
  881. .parent = &pbb_clk,
  882. .mode = pbb_clk_mode,
  883. .get_rate = pbb_clk_get_rate,
  884. .index = 9,
  885. };
  886. struct platform_device *__init at32_add_device_mci(unsigned int id)
  887. {
  888. struct platform_device *pdev;
  889. if (id != 0)
  890. return NULL;
  891. pdev = platform_device_alloc("atmel_mci", id);
  892. if (!pdev)
  893. return NULL;
  894. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  895. ARRAY_SIZE(atmel_mci0_resource)))
  896. goto err_add_resources;
  897. select_peripheral(PA(10), PERIPH_A, 0); /* CLK */
  898. select_peripheral(PA(11), PERIPH_A, 0); /* CMD */
  899. select_peripheral(PA(12), PERIPH_A, 0); /* DATA0 */
  900. select_peripheral(PA(13), PERIPH_A, 0); /* DATA1 */
  901. select_peripheral(PA(14), PERIPH_A, 0); /* DATA2 */
  902. select_peripheral(PA(15), PERIPH_A, 0); /* DATA3 */
  903. atmel_mci0_pclk.dev = &pdev->dev;
  904. platform_device_add(pdev);
  905. return pdev;
  906. err_add_resources:
  907. platform_device_put(pdev);
  908. return NULL;
  909. }
  910. /* --------------------------------------------------------------------
  911. * LCDC
  912. * -------------------------------------------------------------------- */
  913. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  914. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  915. static struct resource atmel_lcdfb0_resource[] = {
  916. {
  917. .start = 0xff000000,
  918. .end = 0xff000fff,
  919. .flags = IORESOURCE_MEM,
  920. },
  921. IRQ(1),
  922. {
  923. /* Placeholder for pre-allocated fb memory */
  924. .start = 0x00000000,
  925. .end = 0x00000000,
  926. .flags = 0,
  927. },
  928. };
  929. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  930. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  931. static struct clk atmel_lcdfb0_pixclk = {
  932. .name = "lcdc_clk",
  933. .dev = &atmel_lcdfb0_device.dev,
  934. .mode = genclk_mode,
  935. .get_rate = genclk_get_rate,
  936. .set_rate = genclk_set_rate,
  937. .set_parent = genclk_set_parent,
  938. .index = 7,
  939. };
  940. struct platform_device *__init
  941. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  942. unsigned long fbmem_start, unsigned long fbmem_len)
  943. {
  944. struct platform_device *pdev;
  945. struct atmel_lcdfb_info *info;
  946. struct fb_monspecs *monspecs;
  947. struct fb_videomode *modedb;
  948. unsigned int modedb_size;
  949. /*
  950. * Do a deep copy of the fb data, monspecs and modedb. Make
  951. * sure all allocations are done before setting up the
  952. * portmux.
  953. */
  954. monspecs = kmemdup(data->default_monspecs,
  955. sizeof(struct fb_monspecs), GFP_KERNEL);
  956. if (!monspecs)
  957. return NULL;
  958. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  959. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  960. if (!modedb)
  961. goto err_dup_modedb;
  962. monspecs->modedb = modedb;
  963. switch (id) {
  964. case 0:
  965. pdev = &atmel_lcdfb0_device;
  966. select_peripheral(PC(19), PERIPH_A, 0); /* CC */
  967. select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
  968. select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
  969. select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
  970. select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
  971. select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
  972. select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
  973. select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
  974. select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
  975. select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
  976. select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
  977. select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
  978. select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
  979. select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
  980. select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
  981. select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
  982. select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
  983. select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
  984. select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
  985. select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
  986. select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
  987. select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
  988. select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
  989. select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
  990. select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
  991. select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
  992. select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
  993. select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
  994. select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
  995. select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
  996. select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
  997. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  998. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  999. break;
  1000. default:
  1001. goto err_invalid_id;
  1002. }
  1003. if (fbmem_len) {
  1004. pdev->resource[2].start = fbmem_start;
  1005. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1006. pdev->resource[2].flags = IORESOURCE_MEM;
  1007. }
  1008. info = pdev->dev.platform_data;
  1009. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  1010. info->default_monspecs = monspecs;
  1011. platform_device_register(pdev);
  1012. return pdev;
  1013. err_invalid_id:
  1014. kfree(modedb);
  1015. err_dup_modedb:
  1016. kfree(monspecs);
  1017. return NULL;
  1018. }
  1019. #endif
  1020. /* --------------------------------------------------------------------
  1021. * PWM
  1022. * -------------------------------------------------------------------- */
  1023. static struct resource atmel_pwm0_resource[] __initdata = {
  1024. PBMEM(0xfff01400),
  1025. IRQ(24),
  1026. };
  1027. static struct clk atmel_pwm0_mck = {
  1028. .name = "mck",
  1029. .parent = &pbb_clk,
  1030. .mode = pbb_clk_mode,
  1031. .get_rate = pbb_clk_get_rate,
  1032. .index = 5,
  1033. };
  1034. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1035. {
  1036. struct platform_device *pdev;
  1037. if (!mask)
  1038. return NULL;
  1039. pdev = platform_device_alloc("atmel_pwm", 0);
  1040. if (!pdev)
  1041. return NULL;
  1042. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1043. ARRAY_SIZE(atmel_pwm0_resource)))
  1044. goto out_free_pdev;
  1045. if (platform_device_add_data(pdev, &mask, sizeof(mask)))
  1046. goto out_free_pdev;
  1047. if (mask & (1 << 0))
  1048. select_peripheral(PA(28), PERIPH_A, 0);
  1049. if (mask & (1 << 1))
  1050. select_peripheral(PA(29), PERIPH_A, 0);
  1051. if (mask & (1 << 2))
  1052. select_peripheral(PA(21), PERIPH_B, 0);
  1053. if (mask & (1 << 3))
  1054. select_peripheral(PA(22), PERIPH_B, 0);
  1055. atmel_pwm0_mck.dev = &pdev->dev;
  1056. platform_device_add(pdev);
  1057. return pdev;
  1058. out_free_pdev:
  1059. platform_device_put(pdev);
  1060. return NULL;
  1061. }
  1062. /* --------------------------------------------------------------------
  1063. * SSC
  1064. * -------------------------------------------------------------------- */
  1065. static struct resource ssc0_resource[] = {
  1066. PBMEM(0xffe01c00),
  1067. IRQ(10),
  1068. };
  1069. DEFINE_DEV(ssc, 0);
  1070. DEV_CLK(pclk, ssc0, pba, 7);
  1071. static struct resource ssc1_resource[] = {
  1072. PBMEM(0xffe02000),
  1073. IRQ(11),
  1074. };
  1075. DEFINE_DEV(ssc, 1);
  1076. DEV_CLK(pclk, ssc1, pba, 8);
  1077. static struct resource ssc2_resource[] = {
  1078. PBMEM(0xffe02400),
  1079. IRQ(12),
  1080. };
  1081. DEFINE_DEV(ssc, 2);
  1082. DEV_CLK(pclk, ssc2, pba, 9);
  1083. struct platform_device *__init
  1084. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1085. {
  1086. struct platform_device *pdev;
  1087. switch (id) {
  1088. case 0:
  1089. pdev = &ssc0_device;
  1090. if (flags & ATMEL_SSC_RF)
  1091. select_peripheral(PA(21), PERIPH_A, 0); /* RF */
  1092. if (flags & ATMEL_SSC_RK)
  1093. select_peripheral(PA(22), PERIPH_A, 0); /* RK */
  1094. if (flags & ATMEL_SSC_TK)
  1095. select_peripheral(PA(23), PERIPH_A, 0); /* TK */
  1096. if (flags & ATMEL_SSC_TF)
  1097. select_peripheral(PA(24), PERIPH_A, 0); /* TF */
  1098. if (flags & ATMEL_SSC_TD)
  1099. select_peripheral(PA(25), PERIPH_A, 0); /* TD */
  1100. if (flags & ATMEL_SSC_RD)
  1101. select_peripheral(PA(26), PERIPH_A, 0); /* RD */
  1102. break;
  1103. case 1:
  1104. pdev = &ssc1_device;
  1105. if (flags & ATMEL_SSC_RF)
  1106. select_peripheral(PA(0), PERIPH_B, 0); /* RF */
  1107. if (flags & ATMEL_SSC_RK)
  1108. select_peripheral(PA(1), PERIPH_B, 0); /* RK */
  1109. if (flags & ATMEL_SSC_TK)
  1110. select_peripheral(PA(2), PERIPH_B, 0); /* TK */
  1111. if (flags & ATMEL_SSC_TF)
  1112. select_peripheral(PA(3), PERIPH_B, 0); /* TF */
  1113. if (flags & ATMEL_SSC_TD)
  1114. select_peripheral(PA(4), PERIPH_B, 0); /* TD */
  1115. if (flags & ATMEL_SSC_RD)
  1116. select_peripheral(PA(5), PERIPH_B, 0); /* RD */
  1117. break;
  1118. case 2:
  1119. pdev = &ssc2_device;
  1120. if (flags & ATMEL_SSC_TD)
  1121. select_peripheral(PB(13), PERIPH_A, 0); /* TD */
  1122. if (flags & ATMEL_SSC_RD)
  1123. select_peripheral(PB(14), PERIPH_A, 0); /* RD */
  1124. if (flags & ATMEL_SSC_TK)
  1125. select_peripheral(PB(15), PERIPH_A, 0); /* TK */
  1126. if (flags & ATMEL_SSC_TF)
  1127. select_peripheral(PB(16), PERIPH_A, 0); /* TF */
  1128. if (flags & ATMEL_SSC_RF)
  1129. select_peripheral(PB(17), PERIPH_A, 0); /* RF */
  1130. if (flags & ATMEL_SSC_RK)
  1131. select_peripheral(PB(18), PERIPH_A, 0); /* RK */
  1132. break;
  1133. default:
  1134. return NULL;
  1135. }
  1136. platform_device_register(pdev);
  1137. return pdev;
  1138. }
  1139. /* --------------------------------------------------------------------
  1140. * USB Device Controller
  1141. * -------------------------------------------------------------------- */
  1142. static struct resource usba0_resource[] __initdata = {
  1143. {
  1144. .start = 0xff300000,
  1145. .end = 0xff3fffff,
  1146. .flags = IORESOURCE_MEM,
  1147. }, {
  1148. .start = 0xfff03000,
  1149. .end = 0xfff033ff,
  1150. .flags = IORESOURCE_MEM,
  1151. },
  1152. IRQ(31),
  1153. };
  1154. static struct clk usba0_pclk = {
  1155. .name = "pclk",
  1156. .parent = &pbb_clk,
  1157. .mode = pbb_clk_mode,
  1158. .get_rate = pbb_clk_get_rate,
  1159. .index = 12,
  1160. };
  1161. static struct clk usba0_hclk = {
  1162. .name = "hclk",
  1163. .parent = &hsb_clk,
  1164. .mode = hsb_clk_mode,
  1165. .get_rate = hsb_clk_get_rate,
  1166. .index = 6,
  1167. };
  1168. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1169. [idx] = { \
  1170. .name = nam, \
  1171. .index = idx, \
  1172. .fifo_size = maxpkt, \
  1173. .nr_banks = maxbk, \
  1174. .can_dma = dma, \
  1175. .can_isoc = isoc, \
  1176. }
  1177. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1178. EP("ep0", 0, 64, 1, 0, 0),
  1179. EP("ep1", 1, 512, 2, 1, 1),
  1180. EP("ep2", 2, 512, 2, 1, 1),
  1181. EP("ep3-int", 3, 64, 3, 1, 0),
  1182. EP("ep4-int", 4, 64, 3, 1, 0),
  1183. EP("ep5", 5, 1024, 3, 1, 1),
  1184. EP("ep6", 6, 1024, 3, 1, 1),
  1185. };
  1186. #undef EP
  1187. struct platform_device *__init
  1188. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1189. {
  1190. /*
  1191. * pdata doesn't have room for any endpoints, so we need to
  1192. * append room for the ones we need right after it.
  1193. */
  1194. struct {
  1195. struct usba_platform_data pdata;
  1196. struct usba_ep_data ep[7];
  1197. } usba_data;
  1198. struct platform_device *pdev;
  1199. if (id != 0)
  1200. return NULL;
  1201. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1202. if (!pdev)
  1203. return NULL;
  1204. if (platform_device_add_resources(pdev, usba0_resource,
  1205. ARRAY_SIZE(usba0_resource)))
  1206. goto out_free_pdev;
  1207. if (data)
  1208. usba_data.pdata.vbus_pin = data->vbus_pin;
  1209. else
  1210. usba_data.pdata.vbus_pin = -EINVAL;
  1211. data = &usba_data.pdata;
  1212. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1213. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1214. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1215. goto out_free_pdev;
  1216. if (data->vbus_pin >= 0)
  1217. at32_select_gpio(data->vbus_pin, 0);
  1218. usba0_pclk.dev = &pdev->dev;
  1219. usba0_hclk.dev = &pdev->dev;
  1220. platform_device_add(pdev);
  1221. return pdev;
  1222. out_free_pdev:
  1223. platform_device_put(pdev);
  1224. return NULL;
  1225. }
  1226. /* --------------------------------------------------------------------
  1227. * IDE / CompactFlash
  1228. * -------------------------------------------------------------------- */
  1229. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1230. static struct resource at32_smc_cs4_resource[] __initdata = {
  1231. {
  1232. .start = 0x04000000,
  1233. .end = 0x07ffffff,
  1234. .flags = IORESOURCE_MEM,
  1235. },
  1236. IRQ(~0UL), /* Magic IRQ will be overridden */
  1237. };
  1238. static struct resource at32_smc_cs5_resource[] __initdata = {
  1239. {
  1240. .start = 0x20000000,
  1241. .end = 0x23ffffff,
  1242. .flags = IORESOURCE_MEM,
  1243. },
  1244. IRQ(~0UL), /* Magic IRQ will be overridden */
  1245. };
  1246. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1247. unsigned int cs, unsigned int extint)
  1248. {
  1249. static unsigned int extint_pin_map[4] __initdata = {
  1250. GPIO_PIN_PB(25),
  1251. GPIO_PIN_PB(26),
  1252. GPIO_PIN_PB(27),
  1253. GPIO_PIN_PB(28),
  1254. };
  1255. static bool common_pins_initialized __initdata = false;
  1256. unsigned int extint_pin;
  1257. int ret;
  1258. if (extint >= ARRAY_SIZE(extint_pin_map))
  1259. return -EINVAL;
  1260. extint_pin = extint_pin_map[extint];
  1261. switch (cs) {
  1262. case 4:
  1263. ret = platform_device_add_resources(pdev,
  1264. at32_smc_cs4_resource,
  1265. ARRAY_SIZE(at32_smc_cs4_resource));
  1266. if (ret)
  1267. return ret;
  1268. select_peripheral(PE(21), PERIPH_A, 0); /* NCS4 -> OE_N */
  1269. set_ebi_sfr_bits(HMATRIX_BIT(CS4A));
  1270. break;
  1271. case 5:
  1272. ret = platform_device_add_resources(pdev,
  1273. at32_smc_cs5_resource,
  1274. ARRAY_SIZE(at32_smc_cs5_resource));
  1275. if (ret)
  1276. return ret;
  1277. select_peripheral(PE(22), PERIPH_A, 0); /* NCS5 -> OE_N */
  1278. set_ebi_sfr_bits(HMATRIX_BIT(CS5A));
  1279. break;
  1280. default:
  1281. return -EINVAL;
  1282. }
  1283. if (!common_pins_initialized) {
  1284. select_peripheral(PE(19), PERIPH_A, 0); /* CFCE1 -> CS0_N */
  1285. select_peripheral(PE(20), PERIPH_A, 0); /* CFCE2 -> CS1_N */
  1286. select_peripheral(PE(23), PERIPH_A, 0); /* CFRNW -> DIR */
  1287. select_peripheral(PE(24), PERIPH_A, 0); /* NWAIT <- IORDY */
  1288. common_pins_initialized = true;
  1289. }
  1290. at32_select_periph(extint_pin, GPIO_PERIPH_A, AT32_GPIOF_DEGLITCH);
  1291. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1292. pdev->resource[1].end = pdev->resource[1].start;
  1293. return 0;
  1294. }
  1295. struct platform_device *__init
  1296. at32_add_device_ide(unsigned int id, unsigned int extint,
  1297. struct ide_platform_data *data)
  1298. {
  1299. struct platform_device *pdev;
  1300. pdev = platform_device_alloc("at32_ide", id);
  1301. if (!pdev)
  1302. goto fail;
  1303. if (platform_device_add_data(pdev, data,
  1304. sizeof(struct ide_platform_data)))
  1305. goto fail;
  1306. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1307. goto fail;
  1308. platform_device_add(pdev);
  1309. return pdev;
  1310. fail:
  1311. platform_device_put(pdev);
  1312. return NULL;
  1313. }
  1314. struct platform_device *__init
  1315. at32_add_device_cf(unsigned int id, unsigned int extint,
  1316. struct cf_platform_data *data)
  1317. {
  1318. struct platform_device *pdev;
  1319. pdev = platform_device_alloc("at32_cf", id);
  1320. if (!pdev)
  1321. goto fail;
  1322. if (platform_device_add_data(pdev, data,
  1323. sizeof(struct cf_platform_data)))
  1324. goto fail;
  1325. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1326. goto fail;
  1327. if (data->detect_pin != GPIO_PIN_NONE)
  1328. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1329. if (data->reset_pin != GPIO_PIN_NONE)
  1330. at32_select_gpio(data->reset_pin, 0);
  1331. if (data->vcc_pin != GPIO_PIN_NONE)
  1332. at32_select_gpio(data->vcc_pin, 0);
  1333. /* READY is used as extint, so we can't select it as gpio */
  1334. platform_device_add(pdev);
  1335. return pdev;
  1336. fail:
  1337. platform_device_put(pdev);
  1338. return NULL;
  1339. }
  1340. #endif
  1341. /* --------------------------------------------------------------------
  1342. * AC97C
  1343. * -------------------------------------------------------------------- */
  1344. static struct resource atmel_ac97c0_resource[] __initdata = {
  1345. PBMEM(0xfff02800),
  1346. IRQ(29),
  1347. };
  1348. static struct clk atmel_ac97c0_pclk = {
  1349. .name = "pclk",
  1350. .parent = &pbb_clk,
  1351. .mode = pbb_clk_mode,
  1352. .get_rate = pbb_clk_get_rate,
  1353. .index = 10,
  1354. };
  1355. struct platform_device *__init at32_add_device_ac97c(unsigned int id)
  1356. {
  1357. struct platform_device *pdev;
  1358. if (id != 0)
  1359. return NULL;
  1360. pdev = platform_device_alloc("atmel_ac97c", id);
  1361. if (!pdev)
  1362. return NULL;
  1363. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1364. ARRAY_SIZE(atmel_ac97c0_resource)))
  1365. goto err_add_resources;
  1366. select_peripheral(PB(20), PERIPH_B, 0); /* SYNC */
  1367. select_peripheral(PB(21), PERIPH_B, 0); /* SDO */
  1368. select_peripheral(PB(22), PERIPH_B, 0); /* SDI */
  1369. select_peripheral(PB(23), PERIPH_B, 0); /* SCLK */
  1370. atmel_ac97c0_pclk.dev = &pdev->dev;
  1371. platform_device_add(pdev);
  1372. return pdev;
  1373. err_add_resources:
  1374. platform_device_put(pdev);
  1375. return NULL;
  1376. }
  1377. /* --------------------------------------------------------------------
  1378. * ABDAC
  1379. * -------------------------------------------------------------------- */
  1380. static struct resource abdac0_resource[] __initdata = {
  1381. PBMEM(0xfff02000),
  1382. IRQ(27),
  1383. };
  1384. static struct clk abdac0_pclk = {
  1385. .name = "pclk",
  1386. .parent = &pbb_clk,
  1387. .mode = pbb_clk_mode,
  1388. .get_rate = pbb_clk_get_rate,
  1389. .index = 8,
  1390. };
  1391. static struct clk abdac0_sample_clk = {
  1392. .name = "sample_clk",
  1393. .mode = genclk_mode,
  1394. .get_rate = genclk_get_rate,
  1395. .set_rate = genclk_set_rate,
  1396. .set_parent = genclk_set_parent,
  1397. .index = 6,
  1398. };
  1399. struct platform_device *__init at32_add_device_abdac(unsigned int id)
  1400. {
  1401. struct platform_device *pdev;
  1402. if (id != 0)
  1403. return NULL;
  1404. pdev = platform_device_alloc("abdac", id);
  1405. if (!pdev)
  1406. return NULL;
  1407. if (platform_device_add_resources(pdev, abdac0_resource,
  1408. ARRAY_SIZE(abdac0_resource)))
  1409. goto err_add_resources;
  1410. select_peripheral(PB(20), PERIPH_A, 0); /* DATA1 */
  1411. select_peripheral(PB(21), PERIPH_A, 0); /* DATA0 */
  1412. select_peripheral(PB(22), PERIPH_A, 0); /* DATAN1 */
  1413. select_peripheral(PB(23), PERIPH_A, 0); /* DATAN0 */
  1414. abdac0_pclk.dev = &pdev->dev;
  1415. abdac0_sample_clk.dev = &pdev->dev;
  1416. platform_device_add(pdev);
  1417. return pdev;
  1418. err_add_resources:
  1419. platform_device_put(pdev);
  1420. return NULL;
  1421. }
  1422. /* --------------------------------------------------------------------
  1423. * GCLK
  1424. * -------------------------------------------------------------------- */
  1425. static struct clk gclk0 = {
  1426. .name = "gclk0",
  1427. .mode = genclk_mode,
  1428. .get_rate = genclk_get_rate,
  1429. .set_rate = genclk_set_rate,
  1430. .set_parent = genclk_set_parent,
  1431. .index = 0,
  1432. };
  1433. static struct clk gclk1 = {
  1434. .name = "gclk1",
  1435. .mode = genclk_mode,
  1436. .get_rate = genclk_get_rate,
  1437. .set_rate = genclk_set_rate,
  1438. .set_parent = genclk_set_parent,
  1439. .index = 1,
  1440. };
  1441. static struct clk gclk2 = {
  1442. .name = "gclk2",
  1443. .mode = genclk_mode,
  1444. .get_rate = genclk_get_rate,
  1445. .set_rate = genclk_set_rate,
  1446. .set_parent = genclk_set_parent,
  1447. .index = 2,
  1448. };
  1449. static struct clk gclk3 = {
  1450. .name = "gclk3",
  1451. .mode = genclk_mode,
  1452. .get_rate = genclk_get_rate,
  1453. .set_rate = genclk_set_rate,
  1454. .set_parent = genclk_set_parent,
  1455. .index = 3,
  1456. };
  1457. static struct clk gclk4 = {
  1458. .name = "gclk4",
  1459. .mode = genclk_mode,
  1460. .get_rate = genclk_get_rate,
  1461. .set_rate = genclk_set_rate,
  1462. .set_parent = genclk_set_parent,
  1463. .index = 4,
  1464. };
  1465. struct clk *at32_clock_list[] = {
  1466. &osc32k,
  1467. &osc0,
  1468. &osc1,
  1469. &pll0,
  1470. &pll1,
  1471. &cpu_clk,
  1472. &hsb_clk,
  1473. &pba_clk,
  1474. &pbb_clk,
  1475. &at32_pm_pclk,
  1476. &at32_intc0_pclk,
  1477. &hmatrix_clk,
  1478. &ebi_clk,
  1479. &hramc_clk,
  1480. &smc0_pclk,
  1481. &smc0_mck,
  1482. &pdc_hclk,
  1483. &pdc_pclk,
  1484. &dmaca0_hclk,
  1485. &pico_clk,
  1486. &pio0_mck,
  1487. &pio1_mck,
  1488. &pio2_mck,
  1489. &pio3_mck,
  1490. &pio4_mck,
  1491. &at32_systc0_pclk,
  1492. &atmel_usart0_usart,
  1493. &atmel_usart1_usart,
  1494. &atmel_usart2_usart,
  1495. &atmel_usart3_usart,
  1496. &atmel_pwm0_mck,
  1497. #if defined(CONFIG_CPU_AT32AP7000)
  1498. &macb0_hclk,
  1499. &macb0_pclk,
  1500. &macb1_hclk,
  1501. &macb1_pclk,
  1502. #endif
  1503. &atmel_spi0_spi_clk,
  1504. &atmel_spi1_spi_clk,
  1505. &atmel_twi0_pclk,
  1506. &atmel_mci0_pclk,
  1507. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1508. &atmel_lcdfb0_hck1,
  1509. &atmel_lcdfb0_pixclk,
  1510. #endif
  1511. &ssc0_pclk,
  1512. &ssc1_pclk,
  1513. &ssc2_pclk,
  1514. &usba0_hclk,
  1515. &usba0_pclk,
  1516. &atmel_ac97c0_pclk,
  1517. &abdac0_pclk,
  1518. &abdac0_sample_clk,
  1519. &gclk0,
  1520. &gclk1,
  1521. &gclk2,
  1522. &gclk3,
  1523. &gclk4,
  1524. };
  1525. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  1526. void __init at32_portmux_init(void)
  1527. {
  1528. at32_init_pio(&pio0_device);
  1529. at32_init_pio(&pio1_device);
  1530. at32_init_pio(&pio2_device);
  1531. at32_init_pio(&pio3_device);
  1532. at32_init_pio(&pio4_device);
  1533. }
  1534. void __init at32_clock_init(void)
  1535. {
  1536. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1537. int i;
  1538. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1539. main_clock = &pll0;
  1540. cpu_clk.parent = &pll0;
  1541. } else {
  1542. main_clock = &osc0;
  1543. cpu_clk.parent = &osc0;
  1544. }
  1545. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1546. pll0.parent = &osc1;
  1547. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1548. pll1.parent = &osc1;
  1549. genclk_init_parent(&gclk0);
  1550. genclk_init_parent(&gclk1);
  1551. genclk_init_parent(&gclk2);
  1552. genclk_init_parent(&gclk3);
  1553. genclk_init_parent(&gclk4);
  1554. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1555. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1556. #endif
  1557. genclk_init_parent(&abdac0_sample_clk);
  1558. /*
  1559. * Turn on all clocks that have at least one user already, and
  1560. * turn off everything else. We only do this for module
  1561. * clocks, and even though it isn't particularly pretty to
  1562. * check the address of the mode function, it should do the
  1563. * trick...
  1564. */
  1565. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  1566. struct clk *clk = at32_clock_list[i];
  1567. if (clk->users == 0)
  1568. continue;
  1569. if (clk->mode == &cpu_clk_mode)
  1570. cpu_mask |= 1 << clk->index;
  1571. else if (clk->mode == &hsb_clk_mode)
  1572. hsb_mask |= 1 << clk->index;
  1573. else if (clk->mode == &pba_clk_mode)
  1574. pba_mask |= 1 << clk->index;
  1575. else if (clk->mode == &pbb_clk_mode)
  1576. pbb_mask |= 1 << clk->index;
  1577. }
  1578. pm_writel(CPU_MASK, cpu_mask);
  1579. pm_writel(HSB_MASK, hsb_mask);
  1580. pm_writel(PBA_MASK, pba_mask);
  1581. pm_writel(PBB_MASK, pbb_mask);
  1582. }