at32ap700x.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  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/fb.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/usb/atmel_usba_udc.h>
  16. #include <asm/atmel-mci.h>
  17. #include <asm/io.h>
  18. #include <asm/irq.h>
  19. #include <asm/arch/at32ap700x.h>
  20. #include <asm/arch/board.h>
  21. #include <asm/arch/portmux.h>
  22. #include <asm/arch/sram.h>
  23. #include <video/atmel_lcdc.h>
  24. #include "clock.h"
  25. #include "hmatrix.h"
  26. #include "pio.h"
  27. #include "pm.h"
  28. #define PBMEM(base) \
  29. { \
  30. .start = base, \
  31. .end = base + 0x3ff, \
  32. .flags = IORESOURCE_MEM, \
  33. }
  34. #define IRQ(num) \
  35. { \
  36. .start = num, \
  37. .end = num, \
  38. .flags = IORESOURCE_IRQ, \
  39. }
  40. #define NAMED_IRQ(num, _name) \
  41. { \
  42. .start = num, \
  43. .end = num, \
  44. .name = _name, \
  45. .flags = IORESOURCE_IRQ, \
  46. }
  47. /* REVISIT these assume *every* device supports DMA, but several
  48. * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
  49. */
  50. #define DEFINE_DEV(_name, _id) \
  51. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  52. static struct platform_device _name##_id##_device = { \
  53. .name = #_name, \
  54. .id = _id, \
  55. .dev = { \
  56. .dma_mask = &_name##_id##_dma_mask, \
  57. .coherent_dma_mask = DMA_32BIT_MASK, \
  58. }, \
  59. .resource = _name##_id##_resource, \
  60. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  61. }
  62. #define DEFINE_DEV_DATA(_name, _id) \
  63. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  64. static struct platform_device _name##_id##_device = { \
  65. .name = #_name, \
  66. .id = _id, \
  67. .dev = { \
  68. .dma_mask = &_name##_id##_dma_mask, \
  69. .platform_data = &_name##_id##_data, \
  70. .coherent_dma_mask = DMA_32BIT_MASK, \
  71. }, \
  72. .resource = _name##_id##_resource, \
  73. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  74. }
  75. #define select_peripheral(pin, periph, flags) \
  76. at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
  77. #define DEV_CLK(_name, devname, bus, _index) \
  78. static struct clk devname##_##_name = { \
  79. .name = #_name, \
  80. .dev = &devname##_device.dev, \
  81. .parent = &bus##_clk, \
  82. .mode = bus##_clk_mode, \
  83. .get_rate = bus##_clk_get_rate, \
  84. .index = _index, \
  85. }
  86. static DEFINE_SPINLOCK(pm_lock);
  87. static struct clk osc0;
  88. static struct clk osc1;
  89. static unsigned long osc_get_rate(struct clk *clk)
  90. {
  91. return at32_board_osc_rates[clk->index];
  92. }
  93. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  94. {
  95. unsigned long div, mul, rate;
  96. div = PM_BFEXT(PLLDIV, control) + 1;
  97. mul = PM_BFEXT(PLLMUL, control) + 1;
  98. rate = clk->parent->get_rate(clk->parent);
  99. rate = (rate + div / 2) / div;
  100. rate *= mul;
  101. return rate;
  102. }
  103. static long pll_set_rate(struct clk *clk, unsigned long rate,
  104. u32 *pll_ctrl)
  105. {
  106. unsigned long mul;
  107. unsigned long mul_best_fit = 0;
  108. unsigned long div;
  109. unsigned long div_min;
  110. unsigned long div_max;
  111. unsigned long div_best_fit = 0;
  112. unsigned long base;
  113. unsigned long pll_in;
  114. unsigned long actual = 0;
  115. unsigned long rate_error;
  116. unsigned long rate_error_prev = ~0UL;
  117. u32 ctrl;
  118. /* Rate must be between 80 MHz and 200 Mhz. */
  119. if (rate < 80000000UL || rate > 200000000UL)
  120. return -EINVAL;
  121. ctrl = PM_BF(PLLOPT, 4);
  122. base = clk->parent->get_rate(clk->parent);
  123. /* PLL input frequency must be between 6 MHz and 32 MHz. */
  124. div_min = DIV_ROUND_UP(base, 32000000UL);
  125. div_max = base / 6000000UL;
  126. if (div_max < div_min)
  127. return -EINVAL;
  128. for (div = div_min; div <= div_max; div++) {
  129. pll_in = (base + div / 2) / div;
  130. mul = (rate + pll_in / 2) / pll_in;
  131. if (mul == 0)
  132. continue;
  133. actual = pll_in * mul;
  134. rate_error = abs(actual - rate);
  135. if (rate_error < rate_error_prev) {
  136. mul_best_fit = mul;
  137. div_best_fit = div;
  138. rate_error_prev = rate_error;
  139. }
  140. if (rate_error == 0)
  141. break;
  142. }
  143. if (div_best_fit == 0)
  144. return -EINVAL;
  145. ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
  146. ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
  147. ctrl |= PM_BF(PLLCOUNT, 16);
  148. if (clk->parent == &osc1)
  149. ctrl |= PM_BIT(PLLOSC);
  150. *pll_ctrl = ctrl;
  151. return actual;
  152. }
  153. static unsigned long pll0_get_rate(struct clk *clk)
  154. {
  155. u32 control;
  156. control = pm_readl(PLL0);
  157. return pll_get_rate(clk, control);
  158. }
  159. static void pll1_mode(struct clk *clk, int enabled)
  160. {
  161. unsigned long timeout;
  162. u32 status;
  163. u32 ctrl;
  164. ctrl = pm_readl(PLL1);
  165. if (enabled) {
  166. if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
  167. pr_debug("clk %s: failed to enable, rate not set\n",
  168. clk->name);
  169. return;
  170. }
  171. ctrl |= PM_BIT(PLLEN);
  172. pm_writel(PLL1, ctrl);
  173. /* Wait for PLL lock. */
  174. for (timeout = 10000; timeout; timeout--) {
  175. status = pm_readl(ISR);
  176. if (status & PM_BIT(LOCK1))
  177. break;
  178. udelay(10);
  179. }
  180. if (!(status & PM_BIT(LOCK1)))
  181. printk(KERN_ERR "clk %s: timeout waiting for lock\n",
  182. clk->name);
  183. } else {
  184. ctrl &= ~PM_BIT(PLLEN);
  185. pm_writel(PLL1, ctrl);
  186. }
  187. }
  188. static unsigned long pll1_get_rate(struct clk *clk)
  189. {
  190. u32 control;
  191. control = pm_readl(PLL1);
  192. return pll_get_rate(clk, control);
  193. }
  194. static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
  195. {
  196. u32 ctrl = 0;
  197. unsigned long actual_rate;
  198. actual_rate = pll_set_rate(clk, rate, &ctrl);
  199. if (apply) {
  200. if (actual_rate != rate)
  201. return -EINVAL;
  202. if (clk->users > 0)
  203. return -EBUSY;
  204. pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
  205. clk->name, rate, actual_rate);
  206. pm_writel(PLL1, ctrl);
  207. }
  208. return actual_rate;
  209. }
  210. static int pll1_set_parent(struct clk *clk, struct clk *parent)
  211. {
  212. u32 ctrl;
  213. if (clk->users > 0)
  214. return -EBUSY;
  215. ctrl = pm_readl(PLL1);
  216. WARN_ON(ctrl & PM_BIT(PLLEN));
  217. if (parent == &osc0)
  218. ctrl &= ~PM_BIT(PLLOSC);
  219. else if (parent == &osc1)
  220. ctrl |= PM_BIT(PLLOSC);
  221. else
  222. return -EINVAL;
  223. pm_writel(PLL1, ctrl);
  224. clk->parent = parent;
  225. return 0;
  226. }
  227. /*
  228. * The AT32AP7000 has five primary clock sources: One 32kHz
  229. * oscillator, two crystal oscillators and two PLLs.
  230. */
  231. static struct clk osc32k = {
  232. .name = "osc32k",
  233. .get_rate = osc_get_rate,
  234. .users = 1,
  235. .index = 0,
  236. };
  237. static struct clk osc0 = {
  238. .name = "osc0",
  239. .get_rate = osc_get_rate,
  240. .users = 1,
  241. .index = 1,
  242. };
  243. static struct clk osc1 = {
  244. .name = "osc1",
  245. .get_rate = osc_get_rate,
  246. .index = 2,
  247. };
  248. static struct clk pll0 = {
  249. .name = "pll0",
  250. .get_rate = pll0_get_rate,
  251. .parent = &osc0,
  252. };
  253. static struct clk pll1 = {
  254. .name = "pll1",
  255. .mode = pll1_mode,
  256. .get_rate = pll1_get_rate,
  257. .set_rate = pll1_set_rate,
  258. .set_parent = pll1_set_parent,
  259. .parent = &osc0,
  260. };
  261. /*
  262. * The main clock can be either osc0 or pll0. The boot loader may
  263. * have chosen one for us, so we don't really know which one until we
  264. * have a look at the SM.
  265. */
  266. static struct clk *main_clock;
  267. /*
  268. * Synchronous clocks are generated from the main clock. The clocks
  269. * must satisfy the constraint
  270. * fCPU >= fHSB >= fPB
  271. * i.e. each clock must not be faster than its parent.
  272. */
  273. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  274. {
  275. return main_clock->get_rate(main_clock) >> shift;
  276. };
  277. static void cpu_clk_mode(struct clk *clk, int enabled)
  278. {
  279. unsigned long flags;
  280. u32 mask;
  281. spin_lock_irqsave(&pm_lock, flags);
  282. mask = pm_readl(CPU_MASK);
  283. if (enabled)
  284. mask |= 1 << clk->index;
  285. else
  286. mask &= ~(1 << clk->index);
  287. pm_writel(CPU_MASK, mask);
  288. spin_unlock_irqrestore(&pm_lock, flags);
  289. }
  290. static unsigned long cpu_clk_get_rate(struct clk *clk)
  291. {
  292. unsigned long cksel, shift = 0;
  293. cksel = pm_readl(CKSEL);
  294. if (cksel & PM_BIT(CPUDIV))
  295. shift = PM_BFEXT(CPUSEL, cksel) + 1;
  296. return bus_clk_get_rate(clk, shift);
  297. }
  298. static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
  299. {
  300. u32 control;
  301. unsigned long parent_rate, child_div, actual_rate, div;
  302. parent_rate = clk->parent->get_rate(clk->parent);
  303. control = pm_readl(CKSEL);
  304. if (control & PM_BIT(HSBDIV))
  305. child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
  306. else
  307. child_div = 1;
  308. if (rate > 3 * (parent_rate / 4) || child_div == 1) {
  309. actual_rate = parent_rate;
  310. control &= ~PM_BIT(CPUDIV);
  311. } else {
  312. unsigned int cpusel;
  313. div = (parent_rate + rate / 2) / rate;
  314. if (div > child_div)
  315. div = child_div;
  316. cpusel = (div > 1) ? (fls(div) - 2) : 0;
  317. control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
  318. actual_rate = parent_rate / (1 << (cpusel + 1));
  319. }
  320. pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
  321. clk->name, rate, actual_rate);
  322. if (apply)
  323. pm_writel(CKSEL, control);
  324. return actual_rate;
  325. }
  326. static void hsb_clk_mode(struct clk *clk, int enabled)
  327. {
  328. unsigned long flags;
  329. u32 mask;
  330. spin_lock_irqsave(&pm_lock, flags);
  331. mask = pm_readl(HSB_MASK);
  332. if (enabled)
  333. mask |= 1 << clk->index;
  334. else
  335. mask &= ~(1 << clk->index);
  336. pm_writel(HSB_MASK, mask);
  337. spin_unlock_irqrestore(&pm_lock, flags);
  338. }
  339. static unsigned long hsb_clk_get_rate(struct clk *clk)
  340. {
  341. unsigned long cksel, shift = 0;
  342. cksel = pm_readl(CKSEL);
  343. if (cksel & PM_BIT(HSBDIV))
  344. shift = PM_BFEXT(HSBSEL, cksel) + 1;
  345. return bus_clk_get_rate(clk, shift);
  346. }
  347. static void pba_clk_mode(struct clk *clk, int enabled)
  348. {
  349. unsigned long flags;
  350. u32 mask;
  351. spin_lock_irqsave(&pm_lock, flags);
  352. mask = pm_readl(PBA_MASK);
  353. if (enabled)
  354. mask |= 1 << clk->index;
  355. else
  356. mask &= ~(1 << clk->index);
  357. pm_writel(PBA_MASK, mask);
  358. spin_unlock_irqrestore(&pm_lock, flags);
  359. }
  360. static unsigned long pba_clk_get_rate(struct clk *clk)
  361. {
  362. unsigned long cksel, shift = 0;
  363. cksel = pm_readl(CKSEL);
  364. if (cksel & PM_BIT(PBADIV))
  365. shift = PM_BFEXT(PBASEL, cksel) + 1;
  366. return bus_clk_get_rate(clk, shift);
  367. }
  368. static void pbb_clk_mode(struct clk *clk, int enabled)
  369. {
  370. unsigned long flags;
  371. u32 mask;
  372. spin_lock_irqsave(&pm_lock, flags);
  373. mask = pm_readl(PBB_MASK);
  374. if (enabled)
  375. mask |= 1 << clk->index;
  376. else
  377. mask &= ~(1 << clk->index);
  378. pm_writel(PBB_MASK, mask);
  379. spin_unlock_irqrestore(&pm_lock, flags);
  380. }
  381. static unsigned long pbb_clk_get_rate(struct clk *clk)
  382. {
  383. unsigned long cksel, shift = 0;
  384. cksel = pm_readl(CKSEL);
  385. if (cksel & PM_BIT(PBBDIV))
  386. shift = PM_BFEXT(PBBSEL, cksel) + 1;
  387. return bus_clk_get_rate(clk, shift);
  388. }
  389. static struct clk cpu_clk = {
  390. .name = "cpu",
  391. .get_rate = cpu_clk_get_rate,
  392. .set_rate = cpu_clk_set_rate,
  393. .users = 1,
  394. };
  395. static struct clk hsb_clk = {
  396. .name = "hsb",
  397. .parent = &cpu_clk,
  398. .get_rate = hsb_clk_get_rate,
  399. };
  400. static struct clk pba_clk = {
  401. .name = "pba",
  402. .parent = &hsb_clk,
  403. .mode = hsb_clk_mode,
  404. .get_rate = pba_clk_get_rate,
  405. .index = 1,
  406. };
  407. static struct clk pbb_clk = {
  408. .name = "pbb",
  409. .parent = &hsb_clk,
  410. .mode = hsb_clk_mode,
  411. .get_rate = pbb_clk_get_rate,
  412. .users = 1,
  413. .index = 2,
  414. };
  415. /* --------------------------------------------------------------------
  416. * Generic Clock operations
  417. * -------------------------------------------------------------------- */
  418. static void genclk_mode(struct clk *clk, int enabled)
  419. {
  420. u32 control;
  421. control = pm_readl(GCCTRL(clk->index));
  422. if (enabled)
  423. control |= PM_BIT(CEN);
  424. else
  425. control &= ~PM_BIT(CEN);
  426. pm_writel(GCCTRL(clk->index), control);
  427. }
  428. static unsigned long genclk_get_rate(struct clk *clk)
  429. {
  430. u32 control;
  431. unsigned long div = 1;
  432. control = pm_readl(GCCTRL(clk->index));
  433. if (control & PM_BIT(DIVEN))
  434. div = 2 * (PM_BFEXT(DIV, control) + 1);
  435. return clk->parent->get_rate(clk->parent) / div;
  436. }
  437. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  438. {
  439. u32 control;
  440. unsigned long parent_rate, actual_rate, div;
  441. parent_rate = clk->parent->get_rate(clk->parent);
  442. control = pm_readl(GCCTRL(clk->index));
  443. if (rate > 3 * parent_rate / 4) {
  444. actual_rate = parent_rate;
  445. control &= ~PM_BIT(DIVEN);
  446. } else {
  447. div = (parent_rate + rate) / (2 * rate) - 1;
  448. control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
  449. actual_rate = parent_rate / (2 * (div + 1));
  450. }
  451. dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
  452. clk->name, rate, actual_rate);
  453. if (apply)
  454. pm_writel(GCCTRL(clk->index), control);
  455. return actual_rate;
  456. }
  457. int genclk_set_parent(struct clk *clk, struct clk *parent)
  458. {
  459. u32 control;
  460. dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
  461. clk->name, parent->name, clk->parent->name);
  462. control = pm_readl(GCCTRL(clk->index));
  463. if (parent == &osc1 || parent == &pll1)
  464. control |= PM_BIT(OSCSEL);
  465. else if (parent == &osc0 || parent == &pll0)
  466. control &= ~PM_BIT(OSCSEL);
  467. else
  468. return -EINVAL;
  469. if (parent == &pll0 || parent == &pll1)
  470. control |= PM_BIT(PLLSEL);
  471. else
  472. control &= ~PM_BIT(PLLSEL);
  473. pm_writel(GCCTRL(clk->index), control);
  474. clk->parent = parent;
  475. return 0;
  476. }
  477. static void __init genclk_init_parent(struct clk *clk)
  478. {
  479. u32 control;
  480. struct clk *parent;
  481. BUG_ON(clk->index > 7);
  482. control = pm_readl(GCCTRL(clk->index));
  483. if (control & PM_BIT(OSCSEL))
  484. parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
  485. else
  486. parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
  487. clk->parent = parent;
  488. }
  489. /* --------------------------------------------------------------------
  490. * System peripherals
  491. * -------------------------------------------------------------------- */
  492. static struct resource at32_pm0_resource[] = {
  493. {
  494. .start = 0xfff00000,
  495. .end = 0xfff0007f,
  496. .flags = IORESOURCE_MEM,
  497. },
  498. IRQ(20),
  499. };
  500. static struct resource at32ap700x_rtc0_resource[] = {
  501. {
  502. .start = 0xfff00080,
  503. .end = 0xfff000af,
  504. .flags = IORESOURCE_MEM,
  505. },
  506. IRQ(21),
  507. };
  508. static struct resource at32_wdt0_resource[] = {
  509. {
  510. .start = 0xfff000b0,
  511. .end = 0xfff000cf,
  512. .flags = IORESOURCE_MEM,
  513. },
  514. };
  515. static struct resource at32_eic0_resource[] = {
  516. {
  517. .start = 0xfff00100,
  518. .end = 0xfff0013f,
  519. .flags = IORESOURCE_MEM,
  520. },
  521. IRQ(19),
  522. };
  523. DEFINE_DEV(at32_pm, 0);
  524. DEFINE_DEV(at32ap700x_rtc, 0);
  525. DEFINE_DEV(at32_wdt, 0);
  526. DEFINE_DEV(at32_eic, 0);
  527. /*
  528. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  529. * is always running.
  530. */
  531. static struct clk at32_pm_pclk = {
  532. .name = "pclk",
  533. .dev = &at32_pm0_device.dev,
  534. .parent = &pbb_clk,
  535. .mode = pbb_clk_mode,
  536. .get_rate = pbb_clk_get_rate,
  537. .users = 1,
  538. .index = 0,
  539. };
  540. static struct resource intc0_resource[] = {
  541. PBMEM(0xfff00400),
  542. };
  543. struct platform_device at32_intc0_device = {
  544. .name = "intc",
  545. .id = 0,
  546. .resource = intc0_resource,
  547. .num_resources = ARRAY_SIZE(intc0_resource),
  548. };
  549. DEV_CLK(pclk, at32_intc0, pbb, 1);
  550. static struct clk ebi_clk = {
  551. .name = "ebi",
  552. .parent = &hsb_clk,
  553. .mode = hsb_clk_mode,
  554. .get_rate = hsb_clk_get_rate,
  555. .users = 1,
  556. };
  557. static struct clk hramc_clk = {
  558. .name = "hramc",
  559. .parent = &hsb_clk,
  560. .mode = hsb_clk_mode,
  561. .get_rate = hsb_clk_get_rate,
  562. .users = 1,
  563. .index = 3,
  564. };
  565. static struct clk sdramc_clk = {
  566. .name = "sdramc_clk",
  567. .parent = &pbb_clk,
  568. .mode = pbb_clk_mode,
  569. .get_rate = pbb_clk_get_rate,
  570. .users = 1,
  571. .index = 14,
  572. };
  573. static struct resource smc0_resource[] = {
  574. PBMEM(0xfff03400),
  575. };
  576. DEFINE_DEV(smc, 0);
  577. DEV_CLK(pclk, smc0, pbb, 13);
  578. DEV_CLK(mck, smc0, hsb, 0);
  579. static struct platform_device pdc_device = {
  580. .name = "pdc",
  581. .id = 0,
  582. };
  583. DEV_CLK(hclk, pdc, hsb, 4);
  584. DEV_CLK(pclk, pdc, pba, 16);
  585. static struct clk pico_clk = {
  586. .name = "pico",
  587. .parent = &cpu_clk,
  588. .mode = cpu_clk_mode,
  589. .get_rate = cpu_clk_get_rate,
  590. .users = 1,
  591. };
  592. static struct resource dmaca0_resource[] = {
  593. {
  594. .start = 0xff200000,
  595. .end = 0xff20ffff,
  596. .flags = IORESOURCE_MEM,
  597. },
  598. IRQ(2),
  599. };
  600. DEFINE_DEV(dmaca, 0);
  601. DEV_CLK(hclk, dmaca0, hsb, 10);
  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(&dmaca0_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. * AC97C
  1600. * -------------------------------------------------------------------- */
  1601. static struct resource atmel_ac97c0_resource[] __initdata = {
  1602. PBMEM(0xfff02800),
  1603. IRQ(29),
  1604. };
  1605. static struct clk atmel_ac97c0_pclk = {
  1606. .name = "pclk",
  1607. .parent = &pbb_clk,
  1608. .mode = pbb_clk_mode,
  1609. .get_rate = pbb_clk_get_rate,
  1610. .index = 10,
  1611. };
  1612. struct platform_device *__init at32_add_device_ac97c(unsigned int id)
  1613. {
  1614. struct platform_device *pdev;
  1615. if (id != 0)
  1616. return NULL;
  1617. pdev = platform_device_alloc("atmel_ac97c", id);
  1618. if (!pdev)
  1619. return NULL;
  1620. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1621. ARRAY_SIZE(atmel_ac97c0_resource)))
  1622. goto err_add_resources;
  1623. select_peripheral(PB(20), PERIPH_B, 0); /* SYNC */
  1624. select_peripheral(PB(21), PERIPH_B, 0); /* SDO */
  1625. select_peripheral(PB(22), PERIPH_B, 0); /* SDI */
  1626. select_peripheral(PB(23), PERIPH_B, 0); /* SCLK */
  1627. atmel_ac97c0_pclk.dev = &pdev->dev;
  1628. platform_device_add(pdev);
  1629. return pdev;
  1630. err_add_resources:
  1631. platform_device_put(pdev);
  1632. return NULL;
  1633. }
  1634. /* --------------------------------------------------------------------
  1635. * ABDAC
  1636. * -------------------------------------------------------------------- */
  1637. static struct resource abdac0_resource[] __initdata = {
  1638. PBMEM(0xfff02000),
  1639. IRQ(27),
  1640. };
  1641. static struct clk abdac0_pclk = {
  1642. .name = "pclk",
  1643. .parent = &pbb_clk,
  1644. .mode = pbb_clk_mode,
  1645. .get_rate = pbb_clk_get_rate,
  1646. .index = 8,
  1647. };
  1648. static struct clk abdac0_sample_clk = {
  1649. .name = "sample_clk",
  1650. .mode = genclk_mode,
  1651. .get_rate = genclk_get_rate,
  1652. .set_rate = genclk_set_rate,
  1653. .set_parent = genclk_set_parent,
  1654. .index = 6,
  1655. };
  1656. struct platform_device *__init at32_add_device_abdac(unsigned int id)
  1657. {
  1658. struct platform_device *pdev;
  1659. if (id != 0)
  1660. return NULL;
  1661. pdev = platform_device_alloc("abdac", id);
  1662. if (!pdev)
  1663. return NULL;
  1664. if (platform_device_add_resources(pdev, abdac0_resource,
  1665. ARRAY_SIZE(abdac0_resource)))
  1666. goto err_add_resources;
  1667. select_peripheral(PB(20), PERIPH_A, 0); /* DATA1 */
  1668. select_peripheral(PB(21), PERIPH_A, 0); /* DATA0 */
  1669. select_peripheral(PB(22), PERIPH_A, 0); /* DATAN1 */
  1670. select_peripheral(PB(23), PERIPH_A, 0); /* DATAN0 */
  1671. abdac0_pclk.dev = &pdev->dev;
  1672. abdac0_sample_clk.dev = &pdev->dev;
  1673. platform_device_add(pdev);
  1674. return pdev;
  1675. err_add_resources:
  1676. platform_device_put(pdev);
  1677. return NULL;
  1678. }
  1679. /* --------------------------------------------------------------------
  1680. * GCLK
  1681. * -------------------------------------------------------------------- */
  1682. static struct clk gclk0 = {
  1683. .name = "gclk0",
  1684. .mode = genclk_mode,
  1685. .get_rate = genclk_get_rate,
  1686. .set_rate = genclk_set_rate,
  1687. .set_parent = genclk_set_parent,
  1688. .index = 0,
  1689. };
  1690. static struct clk gclk1 = {
  1691. .name = "gclk1",
  1692. .mode = genclk_mode,
  1693. .get_rate = genclk_get_rate,
  1694. .set_rate = genclk_set_rate,
  1695. .set_parent = genclk_set_parent,
  1696. .index = 1,
  1697. };
  1698. static struct clk gclk2 = {
  1699. .name = "gclk2",
  1700. .mode = genclk_mode,
  1701. .get_rate = genclk_get_rate,
  1702. .set_rate = genclk_set_rate,
  1703. .set_parent = genclk_set_parent,
  1704. .index = 2,
  1705. };
  1706. static struct clk gclk3 = {
  1707. .name = "gclk3",
  1708. .mode = genclk_mode,
  1709. .get_rate = genclk_get_rate,
  1710. .set_rate = genclk_set_rate,
  1711. .set_parent = genclk_set_parent,
  1712. .index = 3,
  1713. };
  1714. static struct clk gclk4 = {
  1715. .name = "gclk4",
  1716. .mode = genclk_mode,
  1717. .get_rate = genclk_get_rate,
  1718. .set_rate = genclk_set_rate,
  1719. .set_parent = genclk_set_parent,
  1720. .index = 4,
  1721. };
  1722. struct clk *at32_clock_list[] = {
  1723. &osc32k,
  1724. &osc0,
  1725. &osc1,
  1726. &pll0,
  1727. &pll1,
  1728. &cpu_clk,
  1729. &hsb_clk,
  1730. &pba_clk,
  1731. &pbb_clk,
  1732. &at32_pm_pclk,
  1733. &at32_intc0_pclk,
  1734. &hmatrix_clk,
  1735. &ebi_clk,
  1736. &hramc_clk,
  1737. &sdramc_clk,
  1738. &smc0_pclk,
  1739. &smc0_mck,
  1740. &pdc_hclk,
  1741. &pdc_pclk,
  1742. &dmaca0_hclk,
  1743. &pico_clk,
  1744. &pio0_mck,
  1745. &pio1_mck,
  1746. &pio2_mck,
  1747. &pio3_mck,
  1748. &pio4_mck,
  1749. &at32_tcb0_t0_clk,
  1750. &at32_tcb1_t0_clk,
  1751. &atmel_psif0_pclk,
  1752. &atmel_psif1_pclk,
  1753. &atmel_usart0_usart,
  1754. &atmel_usart1_usart,
  1755. &atmel_usart2_usart,
  1756. &atmel_usart3_usart,
  1757. &atmel_pwm0_mck,
  1758. #if defined(CONFIG_CPU_AT32AP7000)
  1759. &macb0_hclk,
  1760. &macb0_pclk,
  1761. &macb1_hclk,
  1762. &macb1_pclk,
  1763. #endif
  1764. &atmel_spi0_spi_clk,
  1765. &atmel_spi1_spi_clk,
  1766. &atmel_twi0_pclk,
  1767. &atmel_mci0_pclk,
  1768. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1769. &atmel_lcdfb0_hck1,
  1770. &atmel_lcdfb0_pixclk,
  1771. #endif
  1772. &ssc0_pclk,
  1773. &ssc1_pclk,
  1774. &ssc2_pclk,
  1775. &usba0_hclk,
  1776. &usba0_pclk,
  1777. &atmel_ac97c0_pclk,
  1778. &abdac0_pclk,
  1779. &abdac0_sample_clk,
  1780. &gclk0,
  1781. &gclk1,
  1782. &gclk2,
  1783. &gclk3,
  1784. &gclk4,
  1785. };
  1786. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  1787. void __init setup_platform(void)
  1788. {
  1789. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1790. int i;
  1791. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1792. main_clock = &pll0;
  1793. cpu_clk.parent = &pll0;
  1794. } else {
  1795. main_clock = &osc0;
  1796. cpu_clk.parent = &osc0;
  1797. }
  1798. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1799. pll0.parent = &osc1;
  1800. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1801. pll1.parent = &osc1;
  1802. genclk_init_parent(&gclk0);
  1803. genclk_init_parent(&gclk1);
  1804. genclk_init_parent(&gclk2);
  1805. genclk_init_parent(&gclk3);
  1806. genclk_init_parent(&gclk4);
  1807. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1808. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1809. #endif
  1810. genclk_init_parent(&abdac0_sample_clk);
  1811. /*
  1812. * Turn on all clocks that have at least one user already, and
  1813. * turn off everything else. We only do this for module
  1814. * clocks, and even though it isn't particularly pretty to
  1815. * check the address of the mode function, it should do the
  1816. * trick...
  1817. */
  1818. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  1819. struct clk *clk = at32_clock_list[i];
  1820. if (clk->users == 0)
  1821. continue;
  1822. if (clk->mode == &cpu_clk_mode)
  1823. cpu_mask |= 1 << clk->index;
  1824. else if (clk->mode == &hsb_clk_mode)
  1825. hsb_mask |= 1 << clk->index;
  1826. else if (clk->mode == &pba_clk_mode)
  1827. pba_mask |= 1 << clk->index;
  1828. else if (clk->mode == &pbb_clk_mode)
  1829. pbb_mask |= 1 << clk->index;
  1830. }
  1831. pm_writel(CPU_MASK, cpu_mask);
  1832. pm_writel(HSB_MASK, hsb_mask);
  1833. pm_writel(PBA_MASK, pba_mask);
  1834. pm_writel(PBB_MASK, pbb_mask);
  1835. /* Initialize the port muxes */
  1836. at32_init_pio(&pio0_device);
  1837. at32_init_pio(&pio1_device);
  1838. at32_init_pio(&pio2_device);
  1839. at32_init_pio(&pio3_device);
  1840. at32_init_pio(&pio4_device);
  1841. }
  1842. struct gen_pool *sram_pool;
  1843. static int __init sram_init(void)
  1844. {
  1845. struct gen_pool *pool;
  1846. /* 1KiB granularity */
  1847. pool = gen_pool_create(10, -1);
  1848. if (!pool)
  1849. goto fail;
  1850. if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
  1851. goto err_pool_add;
  1852. sram_pool = pool;
  1853. return 0;
  1854. err_pool_add:
  1855. gen_pool_destroy(pool);
  1856. fail:
  1857. pr_err("Failed to create SRAM pool\n");
  1858. return -ENOMEM;
  1859. }
  1860. core_initcall(sram_init);