at32ap700x.c 47 KB

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