at32ap700x.c 53 KB

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