at32ap7000.c 41 KB

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