at32ap700x.c 49 KB

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