at32ap700x.c 51 KB

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