at32ap700x.c 44 KB

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