at32ap700x.c 53 KB

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