at32ap700x.c 53 KB

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