at32ap700x.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  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. };
  499. static struct resource dw_dmac0_resource[] = {
  500. PBMEM(0xff200000),
  501. IRQ(2),
  502. };
  503. DEFINE_DEV_DATA(dw_dmac, 0);
  504. DEV_CLK(hclk, dw_dmac0, hsb, 10);
  505. /* --------------------------------------------------------------------
  506. * System peripherals
  507. * -------------------------------------------------------------------- */
  508. static struct resource at32_pm0_resource[] = {
  509. {
  510. .start = 0xfff00000,
  511. .end = 0xfff0007f,
  512. .flags = IORESOURCE_MEM,
  513. },
  514. IRQ(20),
  515. };
  516. static struct resource at32ap700x_rtc0_resource[] = {
  517. {
  518. .start = 0xfff00080,
  519. .end = 0xfff000af,
  520. .flags = IORESOURCE_MEM,
  521. },
  522. IRQ(21),
  523. };
  524. static struct resource at32_wdt0_resource[] = {
  525. {
  526. .start = 0xfff000b0,
  527. .end = 0xfff000cf,
  528. .flags = IORESOURCE_MEM,
  529. },
  530. };
  531. static struct resource at32_eic0_resource[] = {
  532. {
  533. .start = 0xfff00100,
  534. .end = 0xfff0013f,
  535. .flags = IORESOURCE_MEM,
  536. },
  537. IRQ(19),
  538. };
  539. DEFINE_DEV(at32_pm, 0);
  540. DEFINE_DEV(at32ap700x_rtc, 0);
  541. DEFINE_DEV(at32_wdt, 0);
  542. DEFINE_DEV(at32_eic, 0);
  543. /*
  544. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  545. * is always running.
  546. */
  547. static struct clk at32_pm_pclk = {
  548. .name = "pclk",
  549. .dev = &at32_pm0_device.dev,
  550. .parent = &pbb_clk,
  551. .mode = pbb_clk_mode,
  552. .get_rate = pbb_clk_get_rate,
  553. .users = 1,
  554. .index = 0,
  555. };
  556. static struct resource intc0_resource[] = {
  557. PBMEM(0xfff00400),
  558. };
  559. struct platform_device at32_intc0_device = {
  560. .name = "intc",
  561. .id = 0,
  562. .resource = intc0_resource,
  563. .num_resources = ARRAY_SIZE(intc0_resource),
  564. };
  565. DEV_CLK(pclk, at32_intc0, pbb, 1);
  566. static struct clk ebi_clk = {
  567. .name = "ebi",
  568. .parent = &hsb_clk,
  569. .mode = hsb_clk_mode,
  570. .get_rate = hsb_clk_get_rate,
  571. .users = 1,
  572. };
  573. static struct clk hramc_clk = {
  574. .name = "hramc",
  575. .parent = &hsb_clk,
  576. .mode = hsb_clk_mode,
  577. .get_rate = hsb_clk_get_rate,
  578. .users = 1,
  579. .index = 3,
  580. };
  581. static struct clk sdramc_clk = {
  582. .name = "sdramc_clk",
  583. .parent = &pbb_clk,
  584. .mode = pbb_clk_mode,
  585. .get_rate = pbb_clk_get_rate,
  586. .users = 1,
  587. .index = 14,
  588. };
  589. static struct resource smc0_resource[] = {
  590. PBMEM(0xfff03400),
  591. };
  592. DEFINE_DEV(smc, 0);
  593. DEV_CLK(pclk, smc0, pbb, 13);
  594. DEV_CLK(mck, smc0, hsb, 0);
  595. static struct platform_device pdc_device = {
  596. .name = "pdc",
  597. .id = 0,
  598. };
  599. DEV_CLK(hclk, pdc, hsb, 4);
  600. DEV_CLK(pclk, pdc, pba, 16);
  601. static struct clk pico_clk = {
  602. .name = "pico",
  603. .parent = &cpu_clk,
  604. .mode = cpu_clk_mode,
  605. .get_rate = cpu_clk_get_rate,
  606. .users = 1,
  607. };
  608. /* --------------------------------------------------------------------
  609. * HMATRIX
  610. * -------------------------------------------------------------------- */
  611. struct clk at32_hmatrix_clk = {
  612. .name = "hmatrix_clk",
  613. .parent = &pbb_clk,
  614. .mode = pbb_clk_mode,
  615. .get_rate = pbb_clk_get_rate,
  616. .index = 2,
  617. .users = 1,
  618. };
  619. /*
  620. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  621. * External Bus Interface (EBI). This can be used to enable special
  622. * features like CompactFlash support, NAND Flash support, etc. on
  623. * certain chipselects.
  624. */
  625. static inline void set_ebi_sfr_bits(u32 mask)
  626. {
  627. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);
  628. }
  629. /* --------------------------------------------------------------------
  630. * Timer/Counter (TC)
  631. * -------------------------------------------------------------------- */
  632. static struct resource at32_tcb0_resource[] = {
  633. PBMEM(0xfff00c00),
  634. IRQ(22),
  635. };
  636. static struct platform_device at32_tcb0_device = {
  637. .name = "atmel_tcb",
  638. .id = 0,
  639. .resource = at32_tcb0_resource,
  640. .num_resources = ARRAY_SIZE(at32_tcb0_resource),
  641. };
  642. DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
  643. static struct resource at32_tcb1_resource[] = {
  644. PBMEM(0xfff01000),
  645. IRQ(23),
  646. };
  647. static struct platform_device at32_tcb1_device = {
  648. .name = "atmel_tcb",
  649. .id = 1,
  650. .resource = at32_tcb1_resource,
  651. .num_resources = ARRAY_SIZE(at32_tcb1_resource),
  652. };
  653. DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
  654. /* --------------------------------------------------------------------
  655. * PIO
  656. * -------------------------------------------------------------------- */
  657. static struct resource pio0_resource[] = {
  658. PBMEM(0xffe02800),
  659. IRQ(13),
  660. };
  661. DEFINE_DEV(pio, 0);
  662. DEV_CLK(mck, pio0, pba, 10);
  663. static struct resource pio1_resource[] = {
  664. PBMEM(0xffe02c00),
  665. IRQ(14),
  666. };
  667. DEFINE_DEV(pio, 1);
  668. DEV_CLK(mck, pio1, pba, 11);
  669. static struct resource pio2_resource[] = {
  670. PBMEM(0xffe03000),
  671. IRQ(15),
  672. };
  673. DEFINE_DEV(pio, 2);
  674. DEV_CLK(mck, pio2, pba, 12);
  675. static struct resource pio3_resource[] = {
  676. PBMEM(0xffe03400),
  677. IRQ(16),
  678. };
  679. DEFINE_DEV(pio, 3);
  680. DEV_CLK(mck, pio3, pba, 13);
  681. static struct resource pio4_resource[] = {
  682. PBMEM(0xffe03800),
  683. IRQ(17),
  684. };
  685. DEFINE_DEV(pio, 4);
  686. DEV_CLK(mck, pio4, pba, 14);
  687. static int __init system_device_init(void)
  688. {
  689. platform_device_register(&at32_pm0_device);
  690. platform_device_register(&at32_intc0_device);
  691. platform_device_register(&at32ap700x_rtc0_device);
  692. platform_device_register(&at32_wdt0_device);
  693. platform_device_register(&at32_eic0_device);
  694. platform_device_register(&smc0_device);
  695. platform_device_register(&pdc_device);
  696. platform_device_register(&dw_dmac0_device);
  697. platform_device_register(&at32_tcb0_device);
  698. platform_device_register(&at32_tcb1_device);
  699. platform_device_register(&pio0_device);
  700. platform_device_register(&pio1_device);
  701. platform_device_register(&pio2_device);
  702. platform_device_register(&pio3_device);
  703. platform_device_register(&pio4_device);
  704. return 0;
  705. }
  706. core_initcall(system_device_init);
  707. /* --------------------------------------------------------------------
  708. * PSIF
  709. * -------------------------------------------------------------------- */
  710. static struct resource atmel_psif0_resource[] __initdata = {
  711. {
  712. .start = 0xffe03c00,
  713. .end = 0xffe03cff,
  714. .flags = IORESOURCE_MEM,
  715. },
  716. IRQ(18),
  717. };
  718. static struct clk atmel_psif0_pclk = {
  719. .name = "pclk",
  720. .parent = &pba_clk,
  721. .mode = pba_clk_mode,
  722. .get_rate = pba_clk_get_rate,
  723. .index = 15,
  724. };
  725. static struct resource atmel_psif1_resource[] __initdata = {
  726. {
  727. .start = 0xffe03d00,
  728. .end = 0xffe03dff,
  729. .flags = IORESOURCE_MEM,
  730. },
  731. IRQ(18),
  732. };
  733. static struct clk atmel_psif1_pclk = {
  734. .name = "pclk",
  735. .parent = &pba_clk,
  736. .mode = pba_clk_mode,
  737. .get_rate = pba_clk_get_rate,
  738. .index = 15,
  739. };
  740. struct platform_device *__init at32_add_device_psif(unsigned int id)
  741. {
  742. struct platform_device *pdev;
  743. u32 pin_mask;
  744. if (!(id == 0 || id == 1))
  745. return NULL;
  746. pdev = platform_device_alloc("atmel_psif", id);
  747. if (!pdev)
  748. return NULL;
  749. switch (id) {
  750. case 0:
  751. pin_mask = (1 << 8) | (1 << 9); /* CLOCK & DATA */
  752. if (platform_device_add_resources(pdev, atmel_psif0_resource,
  753. ARRAY_SIZE(atmel_psif0_resource)))
  754. goto err_add_resources;
  755. atmel_psif0_pclk.dev = &pdev->dev;
  756. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  757. break;
  758. case 1:
  759. pin_mask = (1 << 11) | (1 << 12); /* CLOCK & DATA */
  760. if (platform_device_add_resources(pdev, atmel_psif1_resource,
  761. ARRAY_SIZE(atmel_psif1_resource)))
  762. goto err_add_resources;
  763. atmel_psif1_pclk.dev = &pdev->dev;
  764. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  765. break;
  766. default:
  767. return NULL;
  768. }
  769. platform_device_add(pdev);
  770. return pdev;
  771. err_add_resources:
  772. platform_device_put(pdev);
  773. return NULL;
  774. }
  775. /* --------------------------------------------------------------------
  776. * USART
  777. * -------------------------------------------------------------------- */
  778. static struct atmel_uart_data atmel_usart0_data = {
  779. .use_dma_tx = 1,
  780. .use_dma_rx = 1,
  781. };
  782. static struct resource atmel_usart0_resource[] = {
  783. PBMEM(0xffe00c00),
  784. IRQ(6),
  785. };
  786. DEFINE_DEV_DATA(atmel_usart, 0);
  787. DEV_CLK(usart, atmel_usart0, pba, 3);
  788. static struct atmel_uart_data atmel_usart1_data = {
  789. .use_dma_tx = 1,
  790. .use_dma_rx = 1,
  791. };
  792. static struct resource atmel_usart1_resource[] = {
  793. PBMEM(0xffe01000),
  794. IRQ(7),
  795. };
  796. DEFINE_DEV_DATA(atmel_usart, 1);
  797. DEV_CLK(usart, atmel_usart1, pba, 4);
  798. static struct atmel_uart_data atmel_usart2_data = {
  799. .use_dma_tx = 1,
  800. .use_dma_rx = 1,
  801. };
  802. static struct resource atmel_usart2_resource[] = {
  803. PBMEM(0xffe01400),
  804. IRQ(8),
  805. };
  806. DEFINE_DEV_DATA(atmel_usart, 2);
  807. DEV_CLK(usart, atmel_usart2, pba, 5);
  808. static struct atmel_uart_data atmel_usart3_data = {
  809. .use_dma_tx = 1,
  810. .use_dma_rx = 1,
  811. };
  812. static struct resource atmel_usart3_resource[] = {
  813. PBMEM(0xffe01800),
  814. IRQ(9),
  815. };
  816. DEFINE_DEV_DATA(atmel_usart, 3);
  817. DEV_CLK(usart, atmel_usart3, pba, 6);
  818. static inline void configure_usart0_pins(int flags)
  819. {
  820. u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
  821. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 6);
  822. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 7);
  823. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 10);
  824. select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  825. }
  826. static inline void configure_usart1_pins(int flags)
  827. {
  828. u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
  829. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 19);
  830. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 20);
  831. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 16);
  832. select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
  833. }
  834. static inline void configure_usart2_pins(int flags)
  835. {
  836. u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
  837. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 30);
  838. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 29);
  839. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 28);
  840. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  841. }
  842. static inline void configure_usart3_pins(int flags)
  843. {
  844. u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
  845. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 16);
  846. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 15);
  847. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 19);
  848. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  849. }
  850. static struct platform_device *__initdata at32_usarts[4];
  851. void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags)
  852. {
  853. struct platform_device *pdev;
  854. struct atmel_uart_data *pdata;
  855. switch (hw_id) {
  856. case 0:
  857. pdev = &atmel_usart0_device;
  858. configure_usart0_pins(flags);
  859. break;
  860. case 1:
  861. pdev = &atmel_usart1_device;
  862. configure_usart1_pins(flags);
  863. break;
  864. case 2:
  865. pdev = &atmel_usart2_device;
  866. configure_usart2_pins(flags);
  867. break;
  868. case 3:
  869. pdev = &atmel_usart3_device;
  870. configure_usart3_pins(flags);
  871. break;
  872. default:
  873. return;
  874. }
  875. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  876. /* Addresses in the P4 segment are permanently mapped 1:1 */
  877. struct atmel_uart_data *data = pdev->dev.platform_data;
  878. data->regs = (void __iomem *)pdev->resource[0].start;
  879. }
  880. pdev->id = line;
  881. pdata = pdev->dev.platform_data;
  882. pdata->num = line;
  883. at32_usarts[line] = pdev;
  884. }
  885. struct platform_device *__init at32_add_device_usart(unsigned int id)
  886. {
  887. platform_device_register(at32_usarts[id]);
  888. return at32_usarts[id];
  889. }
  890. void __init at32_setup_serial_console(unsigned int usart_id)
  891. {
  892. atmel_default_console_device = at32_usarts[usart_id];
  893. }
  894. /* --------------------------------------------------------------------
  895. * Ethernet
  896. * -------------------------------------------------------------------- */
  897. #ifdef CONFIG_CPU_AT32AP7000
  898. static struct macb_platform_data macb0_data;
  899. static struct resource macb0_resource[] = {
  900. PBMEM(0xfff01800),
  901. IRQ(25),
  902. };
  903. DEFINE_DEV_DATA(macb, 0);
  904. DEV_CLK(hclk, macb0, hsb, 8);
  905. DEV_CLK(pclk, macb0, pbb, 6);
  906. static struct macb_platform_data macb1_data;
  907. static struct resource macb1_resource[] = {
  908. PBMEM(0xfff01c00),
  909. IRQ(26),
  910. };
  911. DEFINE_DEV_DATA(macb, 1);
  912. DEV_CLK(hclk, macb1, hsb, 9);
  913. DEV_CLK(pclk, macb1, pbb, 7);
  914. struct platform_device *__init
  915. at32_add_device_eth(unsigned int id, struct macb_platform_data *data)
  916. {
  917. struct platform_device *pdev;
  918. u32 pin_mask;
  919. switch (id) {
  920. case 0:
  921. pdev = &macb0_device;
  922. pin_mask = (1 << 3); /* TXD0 */
  923. pin_mask |= (1 << 4); /* TXD1 */
  924. pin_mask |= (1 << 7); /* TXEN */
  925. pin_mask |= (1 << 8); /* TXCK */
  926. pin_mask |= (1 << 9); /* RXD0 */
  927. pin_mask |= (1 << 10); /* RXD1 */
  928. pin_mask |= (1 << 13); /* RXER */
  929. pin_mask |= (1 << 15); /* RXDV */
  930. pin_mask |= (1 << 16); /* MDC */
  931. pin_mask |= (1 << 17); /* MDIO */
  932. if (!data->is_rmii) {
  933. pin_mask |= (1 << 0); /* COL */
  934. pin_mask |= (1 << 1); /* CRS */
  935. pin_mask |= (1 << 2); /* TXER */
  936. pin_mask |= (1 << 5); /* TXD2 */
  937. pin_mask |= (1 << 6); /* TXD3 */
  938. pin_mask |= (1 << 11); /* RXD2 */
  939. pin_mask |= (1 << 12); /* RXD3 */
  940. pin_mask |= (1 << 14); /* RXCK */
  941. #ifndef CONFIG_BOARD_MIMC200
  942. pin_mask |= (1 << 18); /* SPD */
  943. #endif
  944. }
  945. select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
  946. break;
  947. case 1:
  948. pdev = &macb1_device;
  949. pin_mask = (1 << 13); /* TXD0 */
  950. pin_mask |= (1 << 14); /* TXD1 */
  951. pin_mask |= (1 << 11); /* TXEN */
  952. pin_mask |= (1 << 12); /* TXCK */
  953. pin_mask |= (1 << 10); /* RXD0 */
  954. pin_mask |= (1 << 6); /* RXD1 */
  955. pin_mask |= (1 << 5); /* RXER */
  956. pin_mask |= (1 << 4); /* RXDV */
  957. pin_mask |= (1 << 3); /* MDC */
  958. pin_mask |= (1 << 2); /* MDIO */
  959. #ifndef CONFIG_BOARD_MIMC200
  960. if (!data->is_rmii)
  961. pin_mask |= (1 << 15); /* SPD */
  962. #endif
  963. select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
  964. if (!data->is_rmii) {
  965. pin_mask = (1 << 19); /* COL */
  966. pin_mask |= (1 << 23); /* CRS */
  967. pin_mask |= (1 << 26); /* TXER */
  968. pin_mask |= (1 << 27); /* TXD2 */
  969. pin_mask |= (1 << 28); /* TXD3 */
  970. pin_mask |= (1 << 29); /* RXD2 */
  971. pin_mask |= (1 << 30); /* RXD3 */
  972. pin_mask |= (1 << 24); /* RXCK */
  973. select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
  974. }
  975. break;
  976. default:
  977. return NULL;
  978. }
  979. memcpy(pdev->dev.platform_data, data, sizeof(struct macb_platform_data));
  980. platform_device_register(pdev);
  981. return pdev;
  982. }
  983. #endif
  984. /* --------------------------------------------------------------------
  985. * SPI
  986. * -------------------------------------------------------------------- */
  987. static struct resource atmel_spi0_resource[] = {
  988. PBMEM(0xffe00000),
  989. IRQ(3),
  990. };
  991. DEFINE_DEV(atmel_spi, 0);
  992. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  993. static struct resource atmel_spi1_resource[] = {
  994. PBMEM(0xffe00400),
  995. IRQ(4),
  996. };
  997. DEFINE_DEV(atmel_spi, 1);
  998. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  999. void __init
  1000. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b, unsigned int n)
  1001. {
  1002. /*
  1003. * Manage the chipselects as GPIOs, normally using the same pins
  1004. * the SPI controller expects; but boards can use other pins.
  1005. */
  1006. static u8 __initdata spi_pins[][4] = {
  1007. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  1008. GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
  1009. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  1010. GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
  1011. };
  1012. unsigned int pin, mode;
  1013. /* There are only 2 SPI controllers */
  1014. if (bus_num > 1)
  1015. return;
  1016. for (; n; n--, b++) {
  1017. b->bus_num = bus_num;
  1018. if (b->chip_select >= 4)
  1019. continue;
  1020. pin = (unsigned)b->controller_data;
  1021. if (!pin) {
  1022. pin = spi_pins[bus_num][b->chip_select];
  1023. b->controller_data = (void *)pin;
  1024. }
  1025. mode = AT32_GPIOF_OUTPUT;
  1026. if (!(b->mode & SPI_CS_HIGH))
  1027. mode |= AT32_GPIOF_HIGH;
  1028. at32_select_gpio(pin, mode);
  1029. }
  1030. }
  1031. struct platform_device *__init
  1032. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  1033. {
  1034. struct platform_device *pdev;
  1035. u32 pin_mask;
  1036. switch (id) {
  1037. case 0:
  1038. pdev = &atmel_spi0_device;
  1039. pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
  1040. /* pullup MISO so a level is always defined */
  1041. select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
  1042. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1043. at32_spi_setup_slaves(0, b, n);
  1044. break;
  1045. case 1:
  1046. pdev = &atmel_spi1_device;
  1047. pin_mask = (1 << 1) | (1 << 5); /* MOSI */
  1048. /* pullup MISO so a level is always defined */
  1049. select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
  1050. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1051. at32_spi_setup_slaves(1, b, n);
  1052. break;
  1053. default:
  1054. return NULL;
  1055. }
  1056. spi_register_board_info(b, n);
  1057. platform_device_register(pdev);
  1058. return pdev;
  1059. }
  1060. /* --------------------------------------------------------------------
  1061. * TWI
  1062. * -------------------------------------------------------------------- */
  1063. static struct resource atmel_twi0_resource[] __initdata = {
  1064. PBMEM(0xffe00800),
  1065. IRQ(5),
  1066. };
  1067. static struct clk atmel_twi0_pclk = {
  1068. .name = "twi_pclk",
  1069. .parent = &pba_clk,
  1070. .mode = pba_clk_mode,
  1071. .get_rate = pba_clk_get_rate,
  1072. .index = 2,
  1073. };
  1074. struct platform_device *__init at32_add_device_twi(unsigned int id,
  1075. struct i2c_board_info *b,
  1076. unsigned int n)
  1077. {
  1078. struct platform_device *pdev;
  1079. u32 pin_mask;
  1080. if (id != 0)
  1081. return NULL;
  1082. pdev = platform_device_alloc("atmel_twi", id);
  1083. if (!pdev)
  1084. return NULL;
  1085. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  1086. ARRAY_SIZE(atmel_twi0_resource)))
  1087. goto err_add_resources;
  1088. pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
  1089. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1090. atmel_twi0_pclk.dev = &pdev->dev;
  1091. if (b)
  1092. i2c_register_board_info(id, b, n);
  1093. platform_device_add(pdev);
  1094. return pdev;
  1095. err_add_resources:
  1096. platform_device_put(pdev);
  1097. return NULL;
  1098. }
  1099. /* --------------------------------------------------------------------
  1100. * MMC
  1101. * -------------------------------------------------------------------- */
  1102. static struct resource atmel_mci0_resource[] __initdata = {
  1103. PBMEM(0xfff02400),
  1104. IRQ(28),
  1105. };
  1106. static struct clk atmel_mci0_pclk = {
  1107. .name = "mci_clk",
  1108. .parent = &pbb_clk,
  1109. .mode = pbb_clk_mode,
  1110. .get_rate = pbb_clk_get_rate,
  1111. .index = 9,
  1112. };
  1113. struct platform_device *__init
  1114. at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
  1115. {
  1116. struct platform_device *pdev;
  1117. struct mci_dma_data *slave;
  1118. u32 pioa_mask;
  1119. u32 piob_mask;
  1120. if (id != 0 || !data)
  1121. return NULL;
  1122. /* Must have at least one usable slot */
  1123. if (!data->slot[0].bus_width && !data->slot[1].bus_width)
  1124. return NULL;
  1125. pdev = platform_device_alloc("atmel_mci", id);
  1126. if (!pdev)
  1127. goto fail;
  1128. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  1129. ARRAY_SIZE(atmel_mci0_resource)))
  1130. goto fail;
  1131. slave = kzalloc(sizeof(struct mci_dma_data), GFP_KERNEL);
  1132. if (!slave)
  1133. goto fail;
  1134. slave->sdata.dma_dev = &dw_dmac0_device.dev;
  1135. slave->sdata.cfg_hi = (DWC_CFGH_SRC_PER(0)
  1136. | DWC_CFGH_DST_PER(1));
  1137. slave->sdata.cfg_lo &= ~(DWC_CFGL_HS_DST_POL
  1138. | DWC_CFGL_HS_SRC_POL);
  1139. data->dma_slave = slave;
  1140. if (platform_device_add_data(pdev, data,
  1141. sizeof(struct mci_platform_data)))
  1142. goto fail_free;
  1143. /* CLK line is common to both slots */
  1144. pioa_mask = 1 << 10;
  1145. switch (data->slot[0].bus_width) {
  1146. case 4:
  1147. pioa_mask |= 1 << 13; /* DATA1 */
  1148. pioa_mask |= 1 << 14; /* DATA2 */
  1149. pioa_mask |= 1 << 15; /* DATA3 */
  1150. /* fall through */
  1151. case 1:
  1152. pioa_mask |= 1 << 11; /* CMD */
  1153. pioa_mask |= 1 << 12; /* DATA0 */
  1154. if (gpio_is_valid(data->slot[0].detect_pin))
  1155. at32_select_gpio(data->slot[0].detect_pin, 0);
  1156. if (gpio_is_valid(data->slot[0].wp_pin))
  1157. at32_select_gpio(data->slot[0].wp_pin, 0);
  1158. break;
  1159. case 0:
  1160. /* Slot is unused */
  1161. break;
  1162. default:
  1163. goto fail_free;
  1164. }
  1165. select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
  1166. piob_mask = 0;
  1167. switch (data->slot[1].bus_width) {
  1168. case 4:
  1169. piob_mask |= 1 << 8; /* DATA1 */
  1170. piob_mask |= 1 << 9; /* DATA2 */
  1171. piob_mask |= 1 << 10; /* DATA3 */
  1172. /* fall through */
  1173. case 1:
  1174. piob_mask |= 1 << 6; /* CMD */
  1175. piob_mask |= 1 << 7; /* DATA0 */
  1176. select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
  1177. if (gpio_is_valid(data->slot[1].detect_pin))
  1178. at32_select_gpio(data->slot[1].detect_pin, 0);
  1179. if (gpio_is_valid(data->slot[1].wp_pin))
  1180. at32_select_gpio(data->slot[1].wp_pin, 0);
  1181. break;
  1182. case 0:
  1183. /* Slot is unused */
  1184. break;
  1185. default:
  1186. if (!data->slot[0].bus_width)
  1187. goto fail_free;
  1188. data->slot[1].bus_width = 0;
  1189. break;
  1190. }
  1191. atmel_mci0_pclk.dev = &pdev->dev;
  1192. platform_device_add(pdev);
  1193. return pdev;
  1194. fail_free:
  1195. kfree(slave);
  1196. fail:
  1197. data->dma_slave = NULL;
  1198. platform_device_put(pdev);
  1199. return NULL;
  1200. }
  1201. /* --------------------------------------------------------------------
  1202. * LCDC
  1203. * -------------------------------------------------------------------- */
  1204. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1205. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  1206. static struct resource atmel_lcdfb0_resource[] = {
  1207. {
  1208. .start = 0xff000000,
  1209. .end = 0xff000fff,
  1210. .flags = IORESOURCE_MEM,
  1211. },
  1212. IRQ(1),
  1213. {
  1214. /* Placeholder for pre-allocated fb memory */
  1215. .start = 0x00000000,
  1216. .end = 0x00000000,
  1217. .flags = 0,
  1218. },
  1219. };
  1220. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  1221. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  1222. static struct clk atmel_lcdfb0_pixclk = {
  1223. .name = "lcdc_clk",
  1224. .dev = &atmel_lcdfb0_device.dev,
  1225. .mode = genclk_mode,
  1226. .get_rate = genclk_get_rate,
  1227. .set_rate = genclk_set_rate,
  1228. .set_parent = genclk_set_parent,
  1229. .index = 7,
  1230. };
  1231. struct platform_device *__init
  1232. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  1233. unsigned long fbmem_start, unsigned long fbmem_len,
  1234. u64 pin_mask)
  1235. {
  1236. struct platform_device *pdev;
  1237. struct atmel_lcdfb_info *info;
  1238. struct fb_monspecs *monspecs;
  1239. struct fb_videomode *modedb;
  1240. unsigned int modedb_size;
  1241. u32 portc_mask, portd_mask, porte_mask;
  1242. /*
  1243. * Do a deep copy of the fb data, monspecs and modedb. Make
  1244. * sure all allocations are done before setting up the
  1245. * portmux.
  1246. */
  1247. monspecs = kmemdup(data->default_monspecs,
  1248. sizeof(struct fb_monspecs), GFP_KERNEL);
  1249. if (!monspecs)
  1250. return NULL;
  1251. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  1252. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  1253. if (!modedb)
  1254. goto err_dup_modedb;
  1255. monspecs->modedb = modedb;
  1256. switch (id) {
  1257. case 0:
  1258. pdev = &atmel_lcdfb0_device;
  1259. if (pin_mask == 0ULL)
  1260. /* Default to "full" lcdc control signals and 24bit */
  1261. pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
  1262. /* LCDC on port C */
  1263. portc_mask = pin_mask & 0xfff80000;
  1264. select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
  1265. /* LCDC on port D */
  1266. portd_mask = pin_mask & 0x0003ffff;
  1267. select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
  1268. /* LCDC on port E */
  1269. porte_mask = (pin_mask >> 32) & 0x0007ffff;
  1270. select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
  1271. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  1272. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  1273. break;
  1274. default:
  1275. goto err_invalid_id;
  1276. }
  1277. if (fbmem_len) {
  1278. pdev->resource[2].start = fbmem_start;
  1279. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1280. pdev->resource[2].flags = IORESOURCE_MEM;
  1281. }
  1282. info = pdev->dev.platform_data;
  1283. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  1284. info->default_monspecs = monspecs;
  1285. platform_device_register(pdev);
  1286. return pdev;
  1287. err_invalid_id:
  1288. kfree(modedb);
  1289. err_dup_modedb:
  1290. kfree(monspecs);
  1291. return NULL;
  1292. }
  1293. #endif
  1294. /* --------------------------------------------------------------------
  1295. * PWM
  1296. * -------------------------------------------------------------------- */
  1297. static struct resource atmel_pwm0_resource[] __initdata = {
  1298. PBMEM(0xfff01400),
  1299. IRQ(24),
  1300. };
  1301. static struct clk atmel_pwm0_mck = {
  1302. .name = "pwm_clk",
  1303. .parent = &pbb_clk,
  1304. .mode = pbb_clk_mode,
  1305. .get_rate = pbb_clk_get_rate,
  1306. .index = 5,
  1307. };
  1308. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1309. {
  1310. struct platform_device *pdev;
  1311. u32 pin_mask;
  1312. if (!mask)
  1313. return NULL;
  1314. pdev = platform_device_alloc("atmel_pwm", 0);
  1315. if (!pdev)
  1316. return NULL;
  1317. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1318. ARRAY_SIZE(atmel_pwm0_resource)))
  1319. goto out_free_pdev;
  1320. if (platform_device_add_data(pdev, &mask, sizeof(mask)))
  1321. goto out_free_pdev;
  1322. pin_mask = 0;
  1323. if (mask & (1 << 0))
  1324. pin_mask |= (1 << 28);
  1325. if (mask & (1 << 1))
  1326. pin_mask |= (1 << 29);
  1327. if (pin_mask > 0)
  1328. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1329. pin_mask = 0;
  1330. if (mask & (1 << 2))
  1331. pin_mask |= (1 << 21);
  1332. if (mask & (1 << 3))
  1333. pin_mask |= (1 << 22);
  1334. if (pin_mask > 0)
  1335. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1336. atmel_pwm0_mck.dev = &pdev->dev;
  1337. platform_device_add(pdev);
  1338. return pdev;
  1339. out_free_pdev:
  1340. platform_device_put(pdev);
  1341. return NULL;
  1342. }
  1343. /* --------------------------------------------------------------------
  1344. * SSC
  1345. * -------------------------------------------------------------------- */
  1346. static struct resource ssc0_resource[] = {
  1347. PBMEM(0xffe01c00),
  1348. IRQ(10),
  1349. };
  1350. DEFINE_DEV(ssc, 0);
  1351. DEV_CLK(pclk, ssc0, pba, 7);
  1352. static struct resource ssc1_resource[] = {
  1353. PBMEM(0xffe02000),
  1354. IRQ(11),
  1355. };
  1356. DEFINE_DEV(ssc, 1);
  1357. DEV_CLK(pclk, ssc1, pba, 8);
  1358. static struct resource ssc2_resource[] = {
  1359. PBMEM(0xffe02400),
  1360. IRQ(12),
  1361. };
  1362. DEFINE_DEV(ssc, 2);
  1363. DEV_CLK(pclk, ssc2, pba, 9);
  1364. struct platform_device *__init
  1365. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1366. {
  1367. struct platform_device *pdev;
  1368. u32 pin_mask = 0;
  1369. switch (id) {
  1370. case 0:
  1371. pdev = &ssc0_device;
  1372. if (flags & ATMEL_SSC_RF)
  1373. pin_mask |= (1 << 21); /* RF */
  1374. if (flags & ATMEL_SSC_RK)
  1375. pin_mask |= (1 << 22); /* RK */
  1376. if (flags & ATMEL_SSC_TK)
  1377. pin_mask |= (1 << 23); /* TK */
  1378. if (flags & ATMEL_SSC_TF)
  1379. pin_mask |= (1 << 24); /* TF */
  1380. if (flags & ATMEL_SSC_TD)
  1381. pin_mask |= (1 << 25); /* TD */
  1382. if (flags & ATMEL_SSC_RD)
  1383. pin_mask |= (1 << 26); /* RD */
  1384. if (pin_mask > 0)
  1385. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1386. break;
  1387. case 1:
  1388. pdev = &ssc1_device;
  1389. if (flags & ATMEL_SSC_RF)
  1390. pin_mask |= (1 << 0); /* RF */
  1391. if (flags & ATMEL_SSC_RK)
  1392. pin_mask |= (1 << 1); /* RK */
  1393. if (flags & ATMEL_SSC_TK)
  1394. pin_mask |= (1 << 2); /* TK */
  1395. if (flags & ATMEL_SSC_TF)
  1396. pin_mask |= (1 << 3); /* TF */
  1397. if (flags & ATMEL_SSC_TD)
  1398. pin_mask |= (1 << 4); /* TD */
  1399. if (flags & ATMEL_SSC_RD)
  1400. pin_mask |= (1 << 5); /* RD */
  1401. if (pin_mask > 0)
  1402. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1403. break;
  1404. case 2:
  1405. pdev = &ssc2_device;
  1406. if (flags & ATMEL_SSC_TD)
  1407. pin_mask |= (1 << 13); /* TD */
  1408. if (flags & ATMEL_SSC_RD)
  1409. pin_mask |= (1 << 14); /* RD */
  1410. if (flags & ATMEL_SSC_TK)
  1411. pin_mask |= (1 << 15); /* TK */
  1412. if (flags & ATMEL_SSC_TF)
  1413. pin_mask |= (1 << 16); /* TF */
  1414. if (flags & ATMEL_SSC_RF)
  1415. pin_mask |= (1 << 17); /* RF */
  1416. if (flags & ATMEL_SSC_RK)
  1417. pin_mask |= (1 << 18); /* RK */
  1418. if (pin_mask > 0)
  1419. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1420. break;
  1421. default:
  1422. return NULL;
  1423. }
  1424. platform_device_register(pdev);
  1425. return pdev;
  1426. }
  1427. /* --------------------------------------------------------------------
  1428. * USB Device Controller
  1429. * -------------------------------------------------------------------- */
  1430. static struct resource usba0_resource[] __initdata = {
  1431. {
  1432. .start = 0xff300000,
  1433. .end = 0xff3fffff,
  1434. .flags = IORESOURCE_MEM,
  1435. }, {
  1436. .start = 0xfff03000,
  1437. .end = 0xfff033ff,
  1438. .flags = IORESOURCE_MEM,
  1439. },
  1440. IRQ(31),
  1441. };
  1442. static struct clk usba0_pclk = {
  1443. .name = "pclk",
  1444. .parent = &pbb_clk,
  1445. .mode = pbb_clk_mode,
  1446. .get_rate = pbb_clk_get_rate,
  1447. .index = 12,
  1448. };
  1449. static struct clk usba0_hclk = {
  1450. .name = "hclk",
  1451. .parent = &hsb_clk,
  1452. .mode = hsb_clk_mode,
  1453. .get_rate = hsb_clk_get_rate,
  1454. .index = 6,
  1455. };
  1456. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1457. [idx] = { \
  1458. .name = nam, \
  1459. .index = idx, \
  1460. .fifo_size = maxpkt, \
  1461. .nr_banks = maxbk, \
  1462. .can_dma = dma, \
  1463. .can_isoc = isoc, \
  1464. }
  1465. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1466. EP("ep0", 0, 64, 1, 0, 0),
  1467. EP("ep1", 1, 512, 2, 1, 1),
  1468. EP("ep2", 2, 512, 2, 1, 1),
  1469. EP("ep3-int", 3, 64, 3, 1, 0),
  1470. EP("ep4-int", 4, 64, 3, 1, 0),
  1471. EP("ep5", 5, 1024, 3, 1, 1),
  1472. EP("ep6", 6, 1024, 3, 1, 1),
  1473. };
  1474. #undef EP
  1475. struct platform_device *__init
  1476. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1477. {
  1478. /*
  1479. * pdata doesn't have room for any endpoints, so we need to
  1480. * append room for the ones we need right after it.
  1481. */
  1482. struct {
  1483. struct usba_platform_data pdata;
  1484. struct usba_ep_data ep[7];
  1485. } usba_data;
  1486. struct platform_device *pdev;
  1487. if (id != 0)
  1488. return NULL;
  1489. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1490. if (!pdev)
  1491. return NULL;
  1492. if (platform_device_add_resources(pdev, usba0_resource,
  1493. ARRAY_SIZE(usba0_resource)))
  1494. goto out_free_pdev;
  1495. if (data) {
  1496. usba_data.pdata.vbus_pin = data->vbus_pin;
  1497. usba_data.pdata.vbus_pin_inverted = data->vbus_pin_inverted;
  1498. } else {
  1499. usba_data.pdata.vbus_pin = -EINVAL;
  1500. usba_data.pdata.vbus_pin_inverted = -EINVAL;
  1501. }
  1502. data = &usba_data.pdata;
  1503. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1504. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1505. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1506. goto out_free_pdev;
  1507. if (gpio_is_valid(data->vbus_pin))
  1508. at32_select_gpio(data->vbus_pin, 0);
  1509. usba0_pclk.dev = &pdev->dev;
  1510. usba0_hclk.dev = &pdev->dev;
  1511. platform_device_add(pdev);
  1512. return pdev;
  1513. out_free_pdev:
  1514. platform_device_put(pdev);
  1515. return NULL;
  1516. }
  1517. /* --------------------------------------------------------------------
  1518. * IDE / CompactFlash
  1519. * -------------------------------------------------------------------- */
  1520. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1521. static struct resource at32_smc_cs4_resource[] __initdata = {
  1522. {
  1523. .start = 0x04000000,
  1524. .end = 0x07ffffff,
  1525. .flags = IORESOURCE_MEM,
  1526. },
  1527. IRQ(~0UL), /* Magic IRQ will be overridden */
  1528. };
  1529. static struct resource at32_smc_cs5_resource[] __initdata = {
  1530. {
  1531. .start = 0x20000000,
  1532. .end = 0x23ffffff,
  1533. .flags = IORESOURCE_MEM,
  1534. },
  1535. IRQ(~0UL), /* Magic IRQ will be overridden */
  1536. };
  1537. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1538. unsigned int cs, unsigned int extint)
  1539. {
  1540. static unsigned int extint_pin_map[4] __initdata = {
  1541. (1 << 25),
  1542. (1 << 26),
  1543. (1 << 27),
  1544. (1 << 28),
  1545. };
  1546. static bool common_pins_initialized __initdata = false;
  1547. unsigned int extint_pin;
  1548. int ret;
  1549. u32 pin_mask;
  1550. if (extint >= ARRAY_SIZE(extint_pin_map))
  1551. return -EINVAL;
  1552. extint_pin = extint_pin_map[extint];
  1553. switch (cs) {
  1554. case 4:
  1555. ret = platform_device_add_resources(pdev,
  1556. at32_smc_cs4_resource,
  1557. ARRAY_SIZE(at32_smc_cs4_resource));
  1558. if (ret)
  1559. return ret;
  1560. /* NCS4 -> OE_N */
  1561. select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
  1562. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
  1563. break;
  1564. case 5:
  1565. ret = platform_device_add_resources(pdev,
  1566. at32_smc_cs5_resource,
  1567. ARRAY_SIZE(at32_smc_cs5_resource));
  1568. if (ret)
  1569. return ret;
  1570. /* NCS5 -> OE_N */
  1571. select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
  1572. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
  1573. break;
  1574. default:
  1575. return -EINVAL;
  1576. }
  1577. if (!common_pins_initialized) {
  1578. pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
  1579. pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
  1580. pin_mask |= (1 << 23); /* CFRNW -> DIR */
  1581. pin_mask |= (1 << 24); /* NWAIT <- IORDY */
  1582. select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
  1583. common_pins_initialized = true;
  1584. }
  1585. select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
  1586. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1587. pdev->resource[1].end = pdev->resource[1].start;
  1588. return 0;
  1589. }
  1590. struct platform_device *__init
  1591. at32_add_device_ide(unsigned int id, unsigned int extint,
  1592. struct ide_platform_data *data)
  1593. {
  1594. struct platform_device *pdev;
  1595. pdev = platform_device_alloc("at32_ide", id);
  1596. if (!pdev)
  1597. goto fail;
  1598. if (platform_device_add_data(pdev, data,
  1599. sizeof(struct ide_platform_data)))
  1600. goto fail;
  1601. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1602. goto fail;
  1603. platform_device_add(pdev);
  1604. return pdev;
  1605. fail:
  1606. platform_device_put(pdev);
  1607. return NULL;
  1608. }
  1609. struct platform_device *__init
  1610. at32_add_device_cf(unsigned int id, unsigned int extint,
  1611. struct cf_platform_data *data)
  1612. {
  1613. struct platform_device *pdev;
  1614. pdev = platform_device_alloc("at32_cf", id);
  1615. if (!pdev)
  1616. goto fail;
  1617. if (platform_device_add_data(pdev, data,
  1618. sizeof(struct cf_platform_data)))
  1619. goto fail;
  1620. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1621. goto fail;
  1622. if (gpio_is_valid(data->detect_pin))
  1623. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1624. if (gpio_is_valid(data->reset_pin))
  1625. at32_select_gpio(data->reset_pin, 0);
  1626. if (gpio_is_valid(data->vcc_pin))
  1627. at32_select_gpio(data->vcc_pin, 0);
  1628. /* READY is used as extint, so we can't select it as gpio */
  1629. platform_device_add(pdev);
  1630. return pdev;
  1631. fail:
  1632. platform_device_put(pdev);
  1633. return NULL;
  1634. }
  1635. #endif
  1636. /* --------------------------------------------------------------------
  1637. * NAND Flash / SmartMedia
  1638. * -------------------------------------------------------------------- */
  1639. static struct resource smc_cs3_resource[] __initdata = {
  1640. {
  1641. .start = 0x0c000000,
  1642. .end = 0x0fffffff,
  1643. .flags = IORESOURCE_MEM,
  1644. }, {
  1645. .start = 0xfff03c00,
  1646. .end = 0xfff03fff,
  1647. .flags = IORESOURCE_MEM,
  1648. },
  1649. };
  1650. struct platform_device *__init
  1651. at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
  1652. {
  1653. struct platform_device *pdev;
  1654. if (id != 0 || !data)
  1655. return NULL;
  1656. pdev = platform_device_alloc("atmel_nand", id);
  1657. if (!pdev)
  1658. goto fail;
  1659. if (platform_device_add_resources(pdev, smc_cs3_resource,
  1660. ARRAY_SIZE(smc_cs3_resource)))
  1661. goto fail;
  1662. if (platform_device_add_data(pdev, data,
  1663. sizeof(struct atmel_nand_data)))
  1664. goto fail;
  1665. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
  1666. if (data->enable_pin)
  1667. at32_select_gpio(data->enable_pin,
  1668. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  1669. if (data->rdy_pin)
  1670. at32_select_gpio(data->rdy_pin, 0);
  1671. if (data->det_pin)
  1672. at32_select_gpio(data->det_pin, 0);
  1673. platform_device_add(pdev);
  1674. return pdev;
  1675. fail:
  1676. platform_device_put(pdev);
  1677. return NULL;
  1678. }
  1679. /* --------------------------------------------------------------------
  1680. * AC97C
  1681. * -------------------------------------------------------------------- */
  1682. static struct resource atmel_ac97c0_resource[] __initdata = {
  1683. PBMEM(0xfff02800),
  1684. IRQ(29),
  1685. };
  1686. static struct clk atmel_ac97c0_pclk = {
  1687. .name = "pclk",
  1688. .parent = &pbb_clk,
  1689. .mode = pbb_clk_mode,
  1690. .get_rate = pbb_clk_get_rate,
  1691. .index = 10,
  1692. };
  1693. struct platform_device *__init
  1694. at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data,
  1695. unsigned int flags)
  1696. {
  1697. struct platform_device *pdev;
  1698. struct dw_dma_slave *rx_dws;
  1699. struct dw_dma_slave *tx_dws;
  1700. struct ac97c_platform_data _data;
  1701. u32 pin_mask;
  1702. if (id != 0)
  1703. return NULL;
  1704. pdev = platform_device_alloc("atmel_ac97c", id);
  1705. if (!pdev)
  1706. return NULL;
  1707. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1708. ARRAY_SIZE(atmel_ac97c0_resource)))
  1709. goto out_free_resources;
  1710. if (!data) {
  1711. data = &_data;
  1712. memset(data, 0, sizeof(struct ac97c_platform_data));
  1713. data->reset_pin = -ENODEV;
  1714. }
  1715. rx_dws = &data->rx_dws;
  1716. tx_dws = &data->tx_dws;
  1717. /* Check if DMA slave interface for capture should be configured. */
  1718. if (flags & AC97C_CAPTURE) {
  1719. rx_dws->dma_dev = &dw_dmac0_device.dev;
  1720. rx_dws->cfg_hi = DWC_CFGH_SRC_PER(3);
  1721. rx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1722. rx_dws->src_master = 0;
  1723. rx_dws->dst_master = 1;
  1724. }
  1725. /* Check if DMA slave interface for playback should be configured. */
  1726. if (flags & AC97C_PLAYBACK) {
  1727. tx_dws->dma_dev = &dw_dmac0_device.dev;
  1728. tx_dws->cfg_hi = DWC_CFGH_DST_PER(4);
  1729. tx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1730. tx_dws->src_master = 0;
  1731. tx_dws->dst_master = 1;
  1732. }
  1733. if (platform_device_add_data(pdev, data,
  1734. sizeof(struct ac97c_platform_data)))
  1735. goto out_free_resources;
  1736. /* SDO | SYNC | SCLK | SDI */
  1737. pin_mask = (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
  1738. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1739. if (gpio_is_valid(data->reset_pin))
  1740. at32_select_gpio(data->reset_pin, AT32_GPIOF_OUTPUT
  1741. | AT32_GPIOF_HIGH);
  1742. atmel_ac97c0_pclk.dev = &pdev->dev;
  1743. platform_device_add(pdev);
  1744. return pdev;
  1745. out_free_resources:
  1746. platform_device_put(pdev);
  1747. return NULL;
  1748. }
  1749. /* --------------------------------------------------------------------
  1750. * ABDAC
  1751. * -------------------------------------------------------------------- */
  1752. static struct resource abdac0_resource[] __initdata = {
  1753. PBMEM(0xfff02000),
  1754. IRQ(27),
  1755. };
  1756. static struct clk abdac0_pclk = {
  1757. .name = "pclk",
  1758. .parent = &pbb_clk,
  1759. .mode = pbb_clk_mode,
  1760. .get_rate = pbb_clk_get_rate,
  1761. .index = 8,
  1762. };
  1763. static struct clk abdac0_sample_clk = {
  1764. .name = "sample_clk",
  1765. .mode = genclk_mode,
  1766. .get_rate = genclk_get_rate,
  1767. .set_rate = genclk_set_rate,
  1768. .set_parent = genclk_set_parent,
  1769. .index = 6,
  1770. };
  1771. struct platform_device *__init
  1772. at32_add_device_abdac(unsigned int id, struct atmel_abdac_pdata *data)
  1773. {
  1774. struct platform_device *pdev;
  1775. struct dw_dma_slave *dws;
  1776. u32 pin_mask;
  1777. if (id != 0 || !data)
  1778. return NULL;
  1779. pdev = platform_device_alloc("atmel_abdac", id);
  1780. if (!pdev)
  1781. return NULL;
  1782. if (platform_device_add_resources(pdev, abdac0_resource,
  1783. ARRAY_SIZE(abdac0_resource)))
  1784. goto out_free_resources;
  1785. dws = &data->dws;
  1786. dws->dma_dev = &dw_dmac0_device.dev;
  1787. dws->cfg_hi = DWC_CFGH_DST_PER(2);
  1788. dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1789. dws->src_master = 0;
  1790. dws->dst_master = 1;
  1791. if (platform_device_add_data(pdev, data,
  1792. sizeof(struct atmel_abdac_pdata)))
  1793. goto out_free_resources;
  1794. pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
  1795. pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
  1796. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1797. abdac0_pclk.dev = &pdev->dev;
  1798. abdac0_sample_clk.dev = &pdev->dev;
  1799. platform_device_add(pdev);
  1800. return pdev;
  1801. out_free_resources:
  1802. platform_device_put(pdev);
  1803. return NULL;
  1804. }
  1805. /* --------------------------------------------------------------------
  1806. * GCLK
  1807. * -------------------------------------------------------------------- */
  1808. static struct clk gclk0 = {
  1809. .name = "gclk0",
  1810. .mode = genclk_mode,
  1811. .get_rate = genclk_get_rate,
  1812. .set_rate = genclk_set_rate,
  1813. .set_parent = genclk_set_parent,
  1814. .index = 0,
  1815. };
  1816. static struct clk gclk1 = {
  1817. .name = "gclk1",
  1818. .mode = genclk_mode,
  1819. .get_rate = genclk_get_rate,
  1820. .set_rate = genclk_set_rate,
  1821. .set_parent = genclk_set_parent,
  1822. .index = 1,
  1823. };
  1824. static struct clk gclk2 = {
  1825. .name = "gclk2",
  1826. .mode = genclk_mode,
  1827. .get_rate = genclk_get_rate,
  1828. .set_rate = genclk_set_rate,
  1829. .set_parent = genclk_set_parent,
  1830. .index = 2,
  1831. };
  1832. static struct clk gclk3 = {
  1833. .name = "gclk3",
  1834. .mode = genclk_mode,
  1835. .get_rate = genclk_get_rate,
  1836. .set_rate = genclk_set_rate,
  1837. .set_parent = genclk_set_parent,
  1838. .index = 3,
  1839. };
  1840. static struct clk gclk4 = {
  1841. .name = "gclk4",
  1842. .mode = genclk_mode,
  1843. .get_rate = genclk_get_rate,
  1844. .set_rate = genclk_set_rate,
  1845. .set_parent = genclk_set_parent,
  1846. .index = 4,
  1847. };
  1848. static __initdata struct clk *init_clocks[] = {
  1849. &osc32k,
  1850. &osc0,
  1851. &osc1,
  1852. &pll0,
  1853. &pll1,
  1854. &cpu_clk,
  1855. &hsb_clk,
  1856. &pba_clk,
  1857. &pbb_clk,
  1858. &at32_pm_pclk,
  1859. &at32_intc0_pclk,
  1860. &at32_hmatrix_clk,
  1861. &ebi_clk,
  1862. &hramc_clk,
  1863. &sdramc_clk,
  1864. &smc0_pclk,
  1865. &smc0_mck,
  1866. &pdc_hclk,
  1867. &pdc_pclk,
  1868. &dw_dmac0_hclk,
  1869. &pico_clk,
  1870. &pio0_mck,
  1871. &pio1_mck,
  1872. &pio2_mck,
  1873. &pio3_mck,
  1874. &pio4_mck,
  1875. &at32_tcb0_t0_clk,
  1876. &at32_tcb1_t0_clk,
  1877. &atmel_psif0_pclk,
  1878. &atmel_psif1_pclk,
  1879. &atmel_usart0_usart,
  1880. &atmel_usart1_usart,
  1881. &atmel_usart2_usart,
  1882. &atmel_usart3_usart,
  1883. &atmel_pwm0_mck,
  1884. #if defined(CONFIG_CPU_AT32AP7000)
  1885. &macb0_hclk,
  1886. &macb0_pclk,
  1887. &macb1_hclk,
  1888. &macb1_pclk,
  1889. #endif
  1890. &atmel_spi0_spi_clk,
  1891. &atmel_spi1_spi_clk,
  1892. &atmel_twi0_pclk,
  1893. &atmel_mci0_pclk,
  1894. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1895. &atmel_lcdfb0_hck1,
  1896. &atmel_lcdfb0_pixclk,
  1897. #endif
  1898. &ssc0_pclk,
  1899. &ssc1_pclk,
  1900. &ssc2_pclk,
  1901. &usba0_hclk,
  1902. &usba0_pclk,
  1903. &atmel_ac97c0_pclk,
  1904. &abdac0_pclk,
  1905. &abdac0_sample_clk,
  1906. &gclk0,
  1907. &gclk1,
  1908. &gclk2,
  1909. &gclk3,
  1910. &gclk4,
  1911. };
  1912. void __init setup_platform(void)
  1913. {
  1914. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1915. int i;
  1916. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1917. main_clock = &pll0;
  1918. cpu_clk.parent = &pll0;
  1919. } else {
  1920. main_clock = &osc0;
  1921. cpu_clk.parent = &osc0;
  1922. }
  1923. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1924. pll0.parent = &osc1;
  1925. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1926. pll1.parent = &osc1;
  1927. genclk_init_parent(&gclk0);
  1928. genclk_init_parent(&gclk1);
  1929. genclk_init_parent(&gclk2);
  1930. genclk_init_parent(&gclk3);
  1931. genclk_init_parent(&gclk4);
  1932. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1933. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1934. #endif
  1935. genclk_init_parent(&abdac0_sample_clk);
  1936. /*
  1937. * Build initial dynamic clock list by registering all clocks
  1938. * from the array.
  1939. * At the same time, turn on all clocks that have at least one
  1940. * user already, and turn off everything else. We only do this
  1941. * for module clocks, and even though it isn't particularly
  1942. * pretty to check the address of the mode function, it should
  1943. * do the trick...
  1944. */
  1945. for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
  1946. struct clk *clk = init_clocks[i];
  1947. /* first, register clock */
  1948. at32_clk_register(clk);
  1949. if (clk->users == 0)
  1950. continue;
  1951. if (clk->mode == &cpu_clk_mode)
  1952. cpu_mask |= 1 << clk->index;
  1953. else if (clk->mode == &hsb_clk_mode)
  1954. hsb_mask |= 1 << clk->index;
  1955. else if (clk->mode == &pba_clk_mode)
  1956. pba_mask |= 1 << clk->index;
  1957. else if (clk->mode == &pbb_clk_mode)
  1958. pbb_mask |= 1 << clk->index;
  1959. }
  1960. pm_writel(CPU_MASK, cpu_mask);
  1961. pm_writel(HSB_MASK, hsb_mask);
  1962. pm_writel(PBA_MASK, pba_mask);
  1963. pm_writel(PBB_MASK, pbb_mask);
  1964. /* Initialize the port muxes */
  1965. at32_init_pio(&pio0_device);
  1966. at32_init_pio(&pio1_device);
  1967. at32_init_pio(&pio2_device);
  1968. at32_init_pio(&pio3_device);
  1969. at32_init_pio(&pio4_device);
  1970. }
  1971. struct gen_pool *sram_pool;
  1972. static int __init sram_init(void)
  1973. {
  1974. struct gen_pool *pool;
  1975. /* 1KiB granularity */
  1976. pool = gen_pool_create(10, -1);
  1977. if (!pool)
  1978. goto fail;
  1979. if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
  1980. goto err_pool_add;
  1981. sram_pool = pool;
  1982. return 0;
  1983. err_pool_add:
  1984. gen_pool_destroy(pool);
  1985. fail:
  1986. pr_err("Failed to create SRAM pool\n");
  1987. return -ENOMEM;
  1988. }
  1989. core_initcall(sram_init);