at32ap7000.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. /*
  24. * We can reduce the code size a bit by using a constant here. Since
  25. * this file is completely chip-specific, it's safe to not use
  26. * ioremap. Generic drivers should of course never do this.
  27. */
  28. #define AT32_PM_BASE 0xfff00000
  29. #define PBMEM(base) \
  30. { \
  31. .start = base, \
  32. .end = base + 0x3ff, \
  33. .flags = IORESOURCE_MEM, \
  34. }
  35. #define IRQ(num) \
  36. { \
  37. .start = num, \
  38. .end = num, \
  39. .flags = IORESOURCE_IRQ, \
  40. }
  41. #define NAMED_IRQ(num, _name) \
  42. { \
  43. .start = num, \
  44. .end = num, \
  45. .name = _name, \
  46. .flags = IORESOURCE_IRQ, \
  47. }
  48. /* REVISIT these assume *every* device supports DMA, but several
  49. * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
  50. */
  51. #define DEFINE_DEV(_name, _id) \
  52. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  53. static struct platform_device _name##_id##_device = { \
  54. .name = #_name, \
  55. .id = _id, \
  56. .dev = { \
  57. .dma_mask = &_name##_id##_dma_mask, \
  58. .coherent_dma_mask = DMA_32BIT_MASK, \
  59. }, \
  60. .resource = _name##_id##_resource, \
  61. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  62. }
  63. #define DEFINE_DEV_DATA(_name, _id) \
  64. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  65. static struct platform_device _name##_id##_device = { \
  66. .name = #_name, \
  67. .id = _id, \
  68. .dev = { \
  69. .dma_mask = &_name##_id##_dma_mask, \
  70. .platform_data = &_name##_id##_data, \
  71. .coherent_dma_mask = DMA_32BIT_MASK, \
  72. }, \
  73. .resource = _name##_id##_resource, \
  74. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  75. }
  76. #define select_peripheral(pin, periph, flags) \
  77. at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
  78. #define DEV_CLK(_name, devname, bus, _index) \
  79. static struct clk devname##_##_name = { \
  80. .name = #_name, \
  81. .dev = &devname##_device.dev, \
  82. .parent = &bus##_clk, \
  83. .mode = bus##_clk_mode, \
  84. .get_rate = bus##_clk_get_rate, \
  85. .index = _index, \
  86. }
  87. static DEFINE_SPINLOCK(pm_lock);
  88. unsigned long at32ap7000_osc_rates[3] = {
  89. [0] = 32768,
  90. /* FIXME: these are ATSTK1002-specific */
  91. [1] = 20000000,
  92. [2] = 12000000,
  93. };
  94. static unsigned long osc_get_rate(struct clk *clk)
  95. {
  96. return at32ap7000_osc_rates[clk->index];
  97. }
  98. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  99. {
  100. unsigned long div, mul, rate;
  101. if (!(control & PM_BIT(PLLEN)))
  102. return 0;
  103. div = PM_BFEXT(PLLDIV, control) + 1;
  104. mul = PM_BFEXT(PLLMUL, control) + 1;
  105. rate = clk->parent->get_rate(clk->parent);
  106. rate = (rate + div / 2) / div;
  107. rate *= mul;
  108. return rate;
  109. }
  110. static unsigned long pll0_get_rate(struct clk *clk)
  111. {
  112. u32 control;
  113. control = pm_readl(PLL0);
  114. return pll_get_rate(clk, control);
  115. }
  116. static unsigned long pll1_get_rate(struct clk *clk)
  117. {
  118. u32 control;
  119. control = pm_readl(PLL1);
  120. return pll_get_rate(clk, control);
  121. }
  122. /*
  123. * The AT32AP7000 has five primary clock sources: One 32kHz
  124. * oscillator, two crystal oscillators and two PLLs.
  125. */
  126. static struct clk osc32k = {
  127. .name = "osc32k",
  128. .get_rate = osc_get_rate,
  129. .users = 1,
  130. .index = 0,
  131. };
  132. static struct clk osc0 = {
  133. .name = "osc0",
  134. .get_rate = osc_get_rate,
  135. .users = 1,
  136. .index = 1,
  137. };
  138. static struct clk osc1 = {
  139. .name = "osc1",
  140. .get_rate = osc_get_rate,
  141. .index = 2,
  142. };
  143. static struct clk pll0 = {
  144. .name = "pll0",
  145. .get_rate = pll0_get_rate,
  146. .parent = &osc0,
  147. };
  148. static struct clk pll1 = {
  149. .name = "pll1",
  150. .get_rate = pll1_get_rate,
  151. .parent = &osc0,
  152. };
  153. /*
  154. * The main clock can be either osc0 or pll0. The boot loader may
  155. * have chosen one for us, so we don't really know which one until we
  156. * have a look at the SM.
  157. */
  158. static struct clk *main_clock;
  159. /*
  160. * Synchronous clocks are generated from the main clock. The clocks
  161. * must satisfy the constraint
  162. * fCPU >= fHSB >= fPB
  163. * i.e. each clock must not be faster than its parent.
  164. */
  165. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  166. {
  167. return main_clock->get_rate(main_clock) >> shift;
  168. };
  169. static void cpu_clk_mode(struct clk *clk, int enabled)
  170. {
  171. unsigned long flags;
  172. u32 mask;
  173. spin_lock_irqsave(&pm_lock, flags);
  174. mask = pm_readl(CPU_MASK);
  175. if (enabled)
  176. mask |= 1 << clk->index;
  177. else
  178. mask &= ~(1 << clk->index);
  179. pm_writel(CPU_MASK, mask);
  180. spin_unlock_irqrestore(&pm_lock, flags);
  181. }
  182. static unsigned long cpu_clk_get_rate(struct clk *clk)
  183. {
  184. unsigned long cksel, shift = 0;
  185. cksel = pm_readl(CKSEL);
  186. if (cksel & PM_BIT(CPUDIV))
  187. shift = PM_BFEXT(CPUSEL, cksel) + 1;
  188. return bus_clk_get_rate(clk, shift);
  189. }
  190. static void hsb_clk_mode(struct clk *clk, int enabled)
  191. {
  192. unsigned long flags;
  193. u32 mask;
  194. spin_lock_irqsave(&pm_lock, flags);
  195. mask = pm_readl(HSB_MASK);
  196. if (enabled)
  197. mask |= 1 << clk->index;
  198. else
  199. mask &= ~(1 << clk->index);
  200. pm_writel(HSB_MASK, mask);
  201. spin_unlock_irqrestore(&pm_lock, flags);
  202. }
  203. static unsigned long hsb_clk_get_rate(struct clk *clk)
  204. {
  205. unsigned long cksel, shift = 0;
  206. cksel = pm_readl(CKSEL);
  207. if (cksel & PM_BIT(HSBDIV))
  208. shift = PM_BFEXT(HSBSEL, cksel) + 1;
  209. return bus_clk_get_rate(clk, shift);
  210. }
  211. static void pba_clk_mode(struct clk *clk, int enabled)
  212. {
  213. unsigned long flags;
  214. u32 mask;
  215. spin_lock_irqsave(&pm_lock, flags);
  216. mask = pm_readl(PBA_MASK);
  217. if (enabled)
  218. mask |= 1 << clk->index;
  219. else
  220. mask &= ~(1 << clk->index);
  221. pm_writel(PBA_MASK, mask);
  222. spin_unlock_irqrestore(&pm_lock, flags);
  223. }
  224. static unsigned long pba_clk_get_rate(struct clk *clk)
  225. {
  226. unsigned long cksel, shift = 0;
  227. cksel = pm_readl(CKSEL);
  228. if (cksel & PM_BIT(PBADIV))
  229. shift = PM_BFEXT(PBASEL, cksel) + 1;
  230. return bus_clk_get_rate(clk, shift);
  231. }
  232. static void pbb_clk_mode(struct clk *clk, int enabled)
  233. {
  234. unsigned long flags;
  235. u32 mask;
  236. spin_lock_irqsave(&pm_lock, flags);
  237. mask = pm_readl(PBB_MASK);
  238. if (enabled)
  239. mask |= 1 << clk->index;
  240. else
  241. mask &= ~(1 << clk->index);
  242. pm_writel(PBB_MASK, mask);
  243. spin_unlock_irqrestore(&pm_lock, flags);
  244. }
  245. static unsigned long pbb_clk_get_rate(struct clk *clk)
  246. {
  247. unsigned long cksel, shift = 0;
  248. cksel = pm_readl(CKSEL);
  249. if (cksel & PM_BIT(PBBDIV))
  250. shift = PM_BFEXT(PBBSEL, cksel) + 1;
  251. return bus_clk_get_rate(clk, shift);
  252. }
  253. static struct clk cpu_clk = {
  254. .name = "cpu",
  255. .get_rate = cpu_clk_get_rate,
  256. .users = 1,
  257. };
  258. static struct clk hsb_clk = {
  259. .name = "hsb",
  260. .parent = &cpu_clk,
  261. .get_rate = hsb_clk_get_rate,
  262. };
  263. static struct clk pba_clk = {
  264. .name = "pba",
  265. .parent = &hsb_clk,
  266. .mode = hsb_clk_mode,
  267. .get_rate = pba_clk_get_rate,
  268. .index = 1,
  269. };
  270. static struct clk pbb_clk = {
  271. .name = "pbb",
  272. .parent = &hsb_clk,
  273. .mode = hsb_clk_mode,
  274. .get_rate = pbb_clk_get_rate,
  275. .users = 1,
  276. .index = 2,
  277. };
  278. /* --------------------------------------------------------------------
  279. * Generic Clock operations
  280. * -------------------------------------------------------------------- */
  281. static void genclk_mode(struct clk *clk, int enabled)
  282. {
  283. u32 control;
  284. control = pm_readl(GCCTRL(clk->index));
  285. if (enabled)
  286. control |= PM_BIT(CEN);
  287. else
  288. control &= ~PM_BIT(CEN);
  289. pm_writel(GCCTRL(clk->index), control);
  290. }
  291. static unsigned long genclk_get_rate(struct clk *clk)
  292. {
  293. u32 control;
  294. unsigned long div = 1;
  295. control = pm_readl(GCCTRL(clk->index));
  296. if (control & PM_BIT(DIVEN))
  297. div = 2 * (PM_BFEXT(DIV, control) + 1);
  298. return clk->parent->get_rate(clk->parent) / div;
  299. }
  300. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  301. {
  302. u32 control;
  303. unsigned long parent_rate, actual_rate, div;
  304. parent_rate = clk->parent->get_rate(clk->parent);
  305. control = pm_readl(GCCTRL(clk->index));
  306. if (rate > 3 * parent_rate / 4) {
  307. actual_rate = parent_rate;
  308. control &= ~PM_BIT(DIVEN);
  309. } else {
  310. div = (parent_rate + rate) / (2 * rate) - 1;
  311. control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
  312. actual_rate = parent_rate / (2 * (div + 1));
  313. }
  314. dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
  315. clk->name, rate, actual_rate);
  316. if (apply)
  317. pm_writel(GCCTRL(clk->index), control);
  318. return actual_rate;
  319. }
  320. int genclk_set_parent(struct clk *clk, struct clk *parent)
  321. {
  322. u32 control;
  323. dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
  324. clk->name, parent->name, clk->parent->name);
  325. control = pm_readl(GCCTRL(clk->index));
  326. if (parent == &osc1 || parent == &pll1)
  327. control |= PM_BIT(OSCSEL);
  328. else if (parent == &osc0 || parent == &pll0)
  329. control &= ~PM_BIT(OSCSEL);
  330. else
  331. return -EINVAL;
  332. if (parent == &pll0 || parent == &pll1)
  333. control |= PM_BIT(PLLSEL);
  334. else
  335. control &= ~PM_BIT(PLLSEL);
  336. pm_writel(GCCTRL(clk->index), control);
  337. clk->parent = parent;
  338. return 0;
  339. }
  340. static void __init genclk_init_parent(struct clk *clk)
  341. {
  342. u32 control;
  343. struct clk *parent;
  344. BUG_ON(clk->index > 7);
  345. control = pm_readl(GCCTRL(clk->index));
  346. if (control & PM_BIT(OSCSEL))
  347. parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
  348. else
  349. parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
  350. clk->parent = parent;
  351. }
  352. /* --------------------------------------------------------------------
  353. * System peripherals
  354. * -------------------------------------------------------------------- */
  355. static struct resource at32_pm0_resource[] = {
  356. {
  357. .start = 0xfff00000,
  358. .end = 0xfff0007f,
  359. .flags = IORESOURCE_MEM,
  360. },
  361. IRQ(20),
  362. };
  363. static struct resource at32ap700x_rtc0_resource[] = {
  364. {
  365. .start = 0xfff00080,
  366. .end = 0xfff000af,
  367. .flags = IORESOURCE_MEM,
  368. },
  369. IRQ(21),
  370. };
  371. static struct resource at32_wdt0_resource[] = {
  372. {
  373. .start = 0xfff000b0,
  374. .end = 0xfff000bf,
  375. .flags = IORESOURCE_MEM,
  376. },
  377. };
  378. static struct resource at32_eic0_resource[] = {
  379. {
  380. .start = 0xfff00100,
  381. .end = 0xfff0013f,
  382. .flags = IORESOURCE_MEM,
  383. },
  384. IRQ(19),
  385. };
  386. DEFINE_DEV(at32_pm, 0);
  387. DEFINE_DEV(at32ap700x_rtc, 0);
  388. DEFINE_DEV(at32_wdt, 0);
  389. DEFINE_DEV(at32_eic, 0);
  390. /*
  391. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  392. * is always running.
  393. */
  394. static struct clk at32_pm_pclk = {
  395. .name = "pclk",
  396. .dev = &at32_pm0_device.dev,
  397. .parent = &pbb_clk,
  398. .mode = pbb_clk_mode,
  399. .get_rate = pbb_clk_get_rate,
  400. .users = 1,
  401. .index = 0,
  402. };
  403. static struct resource intc0_resource[] = {
  404. PBMEM(0xfff00400),
  405. };
  406. struct platform_device at32_intc0_device = {
  407. .name = "intc",
  408. .id = 0,
  409. .resource = intc0_resource,
  410. .num_resources = ARRAY_SIZE(intc0_resource),
  411. };
  412. DEV_CLK(pclk, at32_intc0, pbb, 1);
  413. static struct clk ebi_clk = {
  414. .name = "ebi",
  415. .parent = &hsb_clk,
  416. .mode = hsb_clk_mode,
  417. .get_rate = hsb_clk_get_rate,
  418. .users = 1,
  419. };
  420. static struct clk hramc_clk = {
  421. .name = "hramc",
  422. .parent = &hsb_clk,
  423. .mode = hsb_clk_mode,
  424. .get_rate = hsb_clk_get_rate,
  425. .users = 1,
  426. .index = 3,
  427. };
  428. static struct resource smc0_resource[] = {
  429. PBMEM(0xfff03400),
  430. };
  431. DEFINE_DEV(smc, 0);
  432. DEV_CLK(pclk, smc0, pbb, 13);
  433. DEV_CLK(mck, smc0, hsb, 0);
  434. static struct platform_device pdc_device = {
  435. .name = "pdc",
  436. .id = 0,
  437. };
  438. DEV_CLK(hclk, pdc, hsb, 4);
  439. DEV_CLK(pclk, pdc, pba, 16);
  440. static struct clk pico_clk = {
  441. .name = "pico",
  442. .parent = &cpu_clk,
  443. .mode = cpu_clk_mode,
  444. .get_rate = cpu_clk_get_rate,
  445. .users = 1,
  446. };
  447. /* --------------------------------------------------------------------
  448. * HMATRIX
  449. * -------------------------------------------------------------------- */
  450. static struct clk hmatrix_clk = {
  451. .name = "hmatrix_clk",
  452. .parent = &pbb_clk,
  453. .mode = pbb_clk_mode,
  454. .get_rate = pbb_clk_get_rate,
  455. .index = 2,
  456. .users = 1,
  457. };
  458. #define HMATRIX_BASE ((void __iomem *)0xfff00800)
  459. #define hmatrix_readl(reg) \
  460. __raw_readl((HMATRIX_BASE) + HMATRIX_##reg)
  461. #define hmatrix_writel(reg,value) \
  462. __raw_writel((value), (HMATRIX_BASE) + HMATRIX_##reg)
  463. /*
  464. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  465. * External Bus Interface (EBI). This can be used to enable special
  466. * features like CompactFlash support, NAND Flash support, etc. on
  467. * certain chipselects.
  468. */
  469. static inline void set_ebi_sfr_bits(u32 mask)
  470. {
  471. u32 sfr;
  472. clk_enable(&hmatrix_clk);
  473. sfr = hmatrix_readl(SFR4);
  474. sfr |= mask;
  475. hmatrix_writel(SFR4, sfr);
  476. clk_disable(&hmatrix_clk);
  477. }
  478. /* --------------------------------------------------------------------
  479. * System Timer/Counter (TC)
  480. * -------------------------------------------------------------------- */
  481. static struct resource at32_systc0_resource[] = {
  482. PBMEM(0xfff00c00),
  483. IRQ(22),
  484. };
  485. struct platform_device at32_systc0_device = {
  486. .name = "systc",
  487. .id = 0,
  488. .resource = at32_systc0_resource,
  489. .num_resources = ARRAY_SIZE(at32_systc0_resource),
  490. };
  491. DEV_CLK(pclk, at32_systc0, pbb, 3);
  492. /* --------------------------------------------------------------------
  493. * PIO
  494. * -------------------------------------------------------------------- */
  495. static struct resource pio0_resource[] = {
  496. PBMEM(0xffe02800),
  497. IRQ(13),
  498. };
  499. DEFINE_DEV(pio, 0);
  500. DEV_CLK(mck, pio0, pba, 10);
  501. static struct resource pio1_resource[] = {
  502. PBMEM(0xffe02c00),
  503. IRQ(14),
  504. };
  505. DEFINE_DEV(pio, 1);
  506. DEV_CLK(mck, pio1, pba, 11);
  507. static struct resource pio2_resource[] = {
  508. PBMEM(0xffe03000),
  509. IRQ(15),
  510. };
  511. DEFINE_DEV(pio, 2);
  512. DEV_CLK(mck, pio2, pba, 12);
  513. static struct resource pio3_resource[] = {
  514. PBMEM(0xffe03400),
  515. IRQ(16),
  516. };
  517. DEFINE_DEV(pio, 3);
  518. DEV_CLK(mck, pio3, pba, 13);
  519. static struct resource pio4_resource[] = {
  520. PBMEM(0xffe03800),
  521. IRQ(17),
  522. };
  523. DEFINE_DEV(pio, 4);
  524. DEV_CLK(mck, pio4, pba, 14);
  525. void __init at32_add_system_devices(void)
  526. {
  527. platform_device_register(&at32_pm0_device);
  528. platform_device_register(&at32_intc0_device);
  529. platform_device_register(&at32ap700x_rtc0_device);
  530. platform_device_register(&at32_wdt0_device);
  531. platform_device_register(&at32_eic0_device);
  532. platform_device_register(&smc0_device);
  533. platform_device_register(&pdc_device);
  534. platform_device_register(&at32_systc0_device);
  535. platform_device_register(&pio0_device);
  536. platform_device_register(&pio1_device);
  537. platform_device_register(&pio2_device);
  538. platform_device_register(&pio3_device);
  539. platform_device_register(&pio4_device);
  540. }
  541. /* --------------------------------------------------------------------
  542. * USART
  543. * -------------------------------------------------------------------- */
  544. static struct atmel_uart_data atmel_usart0_data = {
  545. .use_dma_tx = 1,
  546. .use_dma_rx = 1,
  547. };
  548. static struct resource atmel_usart0_resource[] = {
  549. PBMEM(0xffe00c00),
  550. IRQ(6),
  551. };
  552. DEFINE_DEV_DATA(atmel_usart, 0);
  553. DEV_CLK(usart, atmel_usart0, pba, 4);
  554. static struct atmel_uart_data atmel_usart1_data = {
  555. .use_dma_tx = 1,
  556. .use_dma_rx = 1,
  557. };
  558. static struct resource atmel_usart1_resource[] = {
  559. PBMEM(0xffe01000),
  560. IRQ(7),
  561. };
  562. DEFINE_DEV_DATA(atmel_usart, 1);
  563. DEV_CLK(usart, atmel_usart1, pba, 4);
  564. static struct atmel_uart_data atmel_usart2_data = {
  565. .use_dma_tx = 1,
  566. .use_dma_rx = 1,
  567. };
  568. static struct resource atmel_usart2_resource[] = {
  569. PBMEM(0xffe01400),
  570. IRQ(8),
  571. };
  572. DEFINE_DEV_DATA(atmel_usart, 2);
  573. DEV_CLK(usart, atmel_usart2, pba, 5);
  574. static struct atmel_uart_data atmel_usart3_data = {
  575. .use_dma_tx = 1,
  576. .use_dma_rx = 1,
  577. };
  578. static struct resource atmel_usart3_resource[] = {
  579. PBMEM(0xffe01800),
  580. IRQ(9),
  581. };
  582. DEFINE_DEV_DATA(atmel_usart, 3);
  583. DEV_CLK(usart, atmel_usart3, pba, 6);
  584. static inline void configure_usart0_pins(void)
  585. {
  586. select_peripheral(PA(8), PERIPH_B, 0); /* RXD */
  587. select_peripheral(PA(9), PERIPH_B, 0); /* TXD */
  588. }
  589. static inline void configure_usart1_pins(void)
  590. {
  591. select_peripheral(PA(17), PERIPH_A, 0); /* RXD */
  592. select_peripheral(PA(18), PERIPH_A, 0); /* TXD */
  593. }
  594. static inline void configure_usart2_pins(void)
  595. {
  596. select_peripheral(PB(26), PERIPH_B, 0); /* RXD */
  597. select_peripheral(PB(27), PERIPH_B, 0); /* TXD */
  598. }
  599. static inline void configure_usart3_pins(void)
  600. {
  601. select_peripheral(PB(18), PERIPH_B, 0); /* RXD */
  602. select_peripheral(PB(17), PERIPH_B, 0); /* TXD */
  603. }
  604. static struct platform_device *__initdata at32_usarts[4];
  605. void __init at32_map_usart(unsigned int hw_id, unsigned int line)
  606. {
  607. struct platform_device *pdev;
  608. switch (hw_id) {
  609. case 0:
  610. pdev = &atmel_usart0_device;
  611. configure_usart0_pins();
  612. break;
  613. case 1:
  614. pdev = &atmel_usart1_device;
  615. configure_usart1_pins();
  616. break;
  617. case 2:
  618. pdev = &atmel_usart2_device;
  619. configure_usart2_pins();
  620. break;
  621. case 3:
  622. pdev = &atmel_usart3_device;
  623. configure_usart3_pins();
  624. break;
  625. default:
  626. return;
  627. }
  628. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  629. /* Addresses in the P4 segment are permanently mapped 1:1 */
  630. struct atmel_uart_data *data = pdev->dev.platform_data;
  631. data->regs = (void __iomem *)pdev->resource[0].start;
  632. }
  633. pdev->id = line;
  634. at32_usarts[line] = pdev;
  635. }
  636. struct platform_device *__init at32_add_device_usart(unsigned int id)
  637. {
  638. platform_device_register(at32_usarts[id]);
  639. return at32_usarts[id];
  640. }
  641. struct platform_device *atmel_default_console_device;
  642. void __init at32_setup_serial_console(unsigned int usart_id)
  643. {
  644. atmel_default_console_device = at32_usarts[usart_id];
  645. }
  646. /* --------------------------------------------------------------------
  647. * Ethernet
  648. * -------------------------------------------------------------------- */
  649. static struct eth_platform_data macb0_data;
  650. static struct resource macb0_resource[] = {
  651. PBMEM(0xfff01800),
  652. IRQ(25),
  653. };
  654. DEFINE_DEV_DATA(macb, 0);
  655. DEV_CLK(hclk, macb0, hsb, 8);
  656. DEV_CLK(pclk, macb0, pbb, 6);
  657. static struct eth_platform_data macb1_data;
  658. static struct resource macb1_resource[] = {
  659. PBMEM(0xfff01c00),
  660. IRQ(26),
  661. };
  662. DEFINE_DEV_DATA(macb, 1);
  663. DEV_CLK(hclk, macb1, hsb, 9);
  664. DEV_CLK(pclk, macb1, pbb, 7);
  665. struct platform_device *__init
  666. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  667. {
  668. struct platform_device *pdev;
  669. switch (id) {
  670. case 0:
  671. pdev = &macb0_device;
  672. select_peripheral(PC(3), PERIPH_A, 0); /* TXD0 */
  673. select_peripheral(PC(4), PERIPH_A, 0); /* TXD1 */
  674. select_peripheral(PC(7), PERIPH_A, 0); /* TXEN */
  675. select_peripheral(PC(8), PERIPH_A, 0); /* TXCK */
  676. select_peripheral(PC(9), PERIPH_A, 0); /* RXD0 */
  677. select_peripheral(PC(10), PERIPH_A, 0); /* RXD1 */
  678. select_peripheral(PC(13), PERIPH_A, 0); /* RXER */
  679. select_peripheral(PC(15), PERIPH_A, 0); /* RXDV */
  680. select_peripheral(PC(16), PERIPH_A, 0); /* MDC */
  681. select_peripheral(PC(17), PERIPH_A, 0); /* MDIO */
  682. if (!data->is_rmii) {
  683. select_peripheral(PC(0), PERIPH_A, 0); /* COL */
  684. select_peripheral(PC(1), PERIPH_A, 0); /* CRS */
  685. select_peripheral(PC(2), PERIPH_A, 0); /* TXER */
  686. select_peripheral(PC(5), PERIPH_A, 0); /* TXD2 */
  687. select_peripheral(PC(6), PERIPH_A, 0); /* TXD3 */
  688. select_peripheral(PC(11), PERIPH_A, 0); /* RXD2 */
  689. select_peripheral(PC(12), PERIPH_A, 0); /* RXD3 */
  690. select_peripheral(PC(14), PERIPH_A, 0); /* RXCK */
  691. select_peripheral(PC(18), PERIPH_A, 0); /* SPD */
  692. }
  693. break;
  694. case 1:
  695. pdev = &macb1_device;
  696. select_peripheral(PD(13), PERIPH_B, 0); /* TXD0 */
  697. select_peripheral(PD(14), PERIPH_B, 0); /* TXD1 */
  698. select_peripheral(PD(11), PERIPH_B, 0); /* TXEN */
  699. select_peripheral(PD(12), PERIPH_B, 0); /* TXCK */
  700. select_peripheral(PD(10), PERIPH_B, 0); /* RXD0 */
  701. select_peripheral(PD(6), PERIPH_B, 0); /* RXD1 */
  702. select_peripheral(PD(5), PERIPH_B, 0); /* RXER */
  703. select_peripheral(PD(4), PERIPH_B, 0); /* RXDV */
  704. select_peripheral(PD(3), PERIPH_B, 0); /* MDC */
  705. select_peripheral(PD(2), PERIPH_B, 0); /* MDIO */
  706. if (!data->is_rmii) {
  707. select_peripheral(PC(19), PERIPH_B, 0); /* COL */
  708. select_peripheral(PC(23), PERIPH_B, 0); /* CRS */
  709. select_peripheral(PC(26), PERIPH_B, 0); /* TXER */
  710. select_peripheral(PC(27), PERIPH_B, 0); /* TXD2 */
  711. select_peripheral(PC(28), PERIPH_B, 0); /* TXD3 */
  712. select_peripheral(PC(29), PERIPH_B, 0); /* RXD2 */
  713. select_peripheral(PC(30), PERIPH_B, 0); /* RXD3 */
  714. select_peripheral(PC(24), PERIPH_B, 0); /* RXCK */
  715. select_peripheral(PD(15), PERIPH_B, 0); /* SPD */
  716. }
  717. break;
  718. default:
  719. return NULL;
  720. }
  721. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  722. platform_device_register(pdev);
  723. return pdev;
  724. }
  725. /* --------------------------------------------------------------------
  726. * SPI
  727. * -------------------------------------------------------------------- */
  728. static struct resource atmel_spi0_resource[] = {
  729. PBMEM(0xffe00000),
  730. IRQ(3),
  731. };
  732. DEFINE_DEV(atmel_spi, 0);
  733. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  734. static struct resource atmel_spi1_resource[] = {
  735. PBMEM(0xffe00400),
  736. IRQ(4),
  737. };
  738. DEFINE_DEV(atmel_spi, 1);
  739. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  740. static void __init
  741. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
  742. unsigned int n, const u8 *pins)
  743. {
  744. unsigned int pin, mode;
  745. for (; n; n--, b++) {
  746. b->bus_num = bus_num;
  747. if (b->chip_select >= 4)
  748. continue;
  749. pin = (unsigned)b->controller_data;
  750. if (!pin) {
  751. pin = pins[b->chip_select];
  752. b->controller_data = (void *)pin;
  753. }
  754. mode = AT32_GPIOF_OUTPUT;
  755. if (!(b->mode & SPI_CS_HIGH))
  756. mode |= AT32_GPIOF_HIGH;
  757. at32_select_gpio(pin, mode);
  758. }
  759. }
  760. struct platform_device *__init
  761. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  762. {
  763. /*
  764. * Manage the chipselects as GPIOs, normally using the same pins
  765. * the SPI controller expects; but boards can use other pins.
  766. */
  767. static u8 __initdata spi0_pins[] =
  768. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  769. GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
  770. static u8 __initdata spi1_pins[] =
  771. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  772. GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
  773. struct platform_device *pdev;
  774. switch (id) {
  775. case 0:
  776. pdev = &atmel_spi0_device;
  777. select_peripheral(PA(0), PERIPH_A, 0); /* MISO */
  778. select_peripheral(PA(1), PERIPH_A, 0); /* MOSI */
  779. select_peripheral(PA(2), PERIPH_A, 0); /* SCK */
  780. at32_spi_setup_slaves(0, b, n, spi0_pins);
  781. break;
  782. case 1:
  783. pdev = &atmel_spi1_device;
  784. select_peripheral(PB(0), PERIPH_B, 0); /* MISO */
  785. select_peripheral(PB(1), PERIPH_B, 0); /* MOSI */
  786. select_peripheral(PB(5), PERIPH_B, 0); /* SCK */
  787. at32_spi_setup_slaves(1, b, n, spi1_pins);
  788. break;
  789. default:
  790. return NULL;
  791. }
  792. spi_register_board_info(b, n);
  793. platform_device_register(pdev);
  794. return pdev;
  795. }
  796. /* --------------------------------------------------------------------
  797. * LCDC
  798. * -------------------------------------------------------------------- */
  799. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  800. static struct resource atmel_lcdfb0_resource[] = {
  801. {
  802. .start = 0xff000000,
  803. .end = 0xff000fff,
  804. .flags = IORESOURCE_MEM,
  805. },
  806. IRQ(1),
  807. {
  808. /* Placeholder for pre-allocated fb memory */
  809. .start = 0x00000000,
  810. .end = 0x00000000,
  811. .flags = 0,
  812. },
  813. };
  814. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  815. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  816. static struct clk atmel_lcdfb0_pixclk = {
  817. .name = "lcdc_clk",
  818. .dev = &atmel_lcdfb0_device.dev,
  819. .mode = genclk_mode,
  820. .get_rate = genclk_get_rate,
  821. .set_rate = genclk_set_rate,
  822. .set_parent = genclk_set_parent,
  823. .index = 7,
  824. };
  825. struct platform_device *__init
  826. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  827. unsigned long fbmem_start, unsigned long fbmem_len)
  828. {
  829. struct platform_device *pdev;
  830. struct atmel_lcdfb_info *info;
  831. struct fb_monspecs *monspecs;
  832. struct fb_videomode *modedb;
  833. unsigned int modedb_size;
  834. /*
  835. * Do a deep copy of the fb data, monspecs and modedb. Make
  836. * sure all allocations are done before setting up the
  837. * portmux.
  838. */
  839. monspecs = kmemdup(data->default_monspecs,
  840. sizeof(struct fb_monspecs), GFP_KERNEL);
  841. if (!monspecs)
  842. return NULL;
  843. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  844. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  845. if (!modedb)
  846. goto err_dup_modedb;
  847. monspecs->modedb = modedb;
  848. switch (id) {
  849. case 0:
  850. pdev = &atmel_lcdfb0_device;
  851. select_peripheral(PC(19), PERIPH_A, 0); /* CC */
  852. select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
  853. select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
  854. select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
  855. select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
  856. select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
  857. select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
  858. select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
  859. select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
  860. select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
  861. select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
  862. select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
  863. select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
  864. select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
  865. select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
  866. select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
  867. select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
  868. select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
  869. select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
  870. select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
  871. select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
  872. select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
  873. select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
  874. select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
  875. select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
  876. select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
  877. select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
  878. select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
  879. select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
  880. select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
  881. select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
  882. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  883. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  884. break;
  885. default:
  886. goto err_invalid_id;
  887. }
  888. if (fbmem_len) {
  889. pdev->resource[2].start = fbmem_start;
  890. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  891. pdev->resource[2].flags = IORESOURCE_MEM;
  892. }
  893. info = pdev->dev.platform_data;
  894. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  895. info->default_monspecs = monspecs;
  896. platform_device_register(pdev);
  897. return pdev;
  898. err_invalid_id:
  899. kfree(modedb);
  900. err_dup_modedb:
  901. kfree(monspecs);
  902. return NULL;
  903. }
  904. /* --------------------------------------------------------------------
  905. * GCLK
  906. * -------------------------------------------------------------------- */
  907. static struct clk gclk0 = {
  908. .name = "gclk0",
  909. .mode = genclk_mode,
  910. .get_rate = genclk_get_rate,
  911. .set_rate = genclk_set_rate,
  912. .set_parent = genclk_set_parent,
  913. .index = 0,
  914. };
  915. static struct clk gclk1 = {
  916. .name = "gclk1",
  917. .mode = genclk_mode,
  918. .get_rate = genclk_get_rate,
  919. .set_rate = genclk_set_rate,
  920. .set_parent = genclk_set_parent,
  921. .index = 1,
  922. };
  923. static struct clk gclk2 = {
  924. .name = "gclk2",
  925. .mode = genclk_mode,
  926. .get_rate = genclk_get_rate,
  927. .set_rate = genclk_set_rate,
  928. .set_parent = genclk_set_parent,
  929. .index = 2,
  930. };
  931. static struct clk gclk3 = {
  932. .name = "gclk3",
  933. .mode = genclk_mode,
  934. .get_rate = genclk_get_rate,
  935. .set_rate = genclk_set_rate,
  936. .set_parent = genclk_set_parent,
  937. .index = 3,
  938. };
  939. static struct clk gclk4 = {
  940. .name = "gclk4",
  941. .mode = genclk_mode,
  942. .get_rate = genclk_get_rate,
  943. .set_rate = genclk_set_rate,
  944. .set_parent = genclk_set_parent,
  945. .index = 4,
  946. };
  947. struct clk *at32_clock_list[] = {
  948. &osc32k,
  949. &osc0,
  950. &osc1,
  951. &pll0,
  952. &pll1,
  953. &cpu_clk,
  954. &hsb_clk,
  955. &pba_clk,
  956. &pbb_clk,
  957. &at32_pm_pclk,
  958. &at32_intc0_pclk,
  959. &hmatrix_clk,
  960. &ebi_clk,
  961. &hramc_clk,
  962. &smc0_pclk,
  963. &smc0_mck,
  964. &pdc_hclk,
  965. &pdc_pclk,
  966. &pico_clk,
  967. &pio0_mck,
  968. &pio1_mck,
  969. &pio2_mck,
  970. &pio3_mck,
  971. &pio4_mck,
  972. &at32_systc0_pclk,
  973. &atmel_usart0_usart,
  974. &atmel_usart1_usart,
  975. &atmel_usart2_usart,
  976. &atmel_usart3_usart,
  977. &macb0_hclk,
  978. &macb0_pclk,
  979. &macb1_hclk,
  980. &macb1_pclk,
  981. &atmel_spi0_spi_clk,
  982. &atmel_spi1_spi_clk,
  983. &atmel_lcdfb0_hck1,
  984. &atmel_lcdfb0_pixclk,
  985. &gclk0,
  986. &gclk1,
  987. &gclk2,
  988. &gclk3,
  989. &gclk4,
  990. };
  991. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  992. void __init at32_portmux_init(void)
  993. {
  994. at32_init_pio(&pio0_device);
  995. at32_init_pio(&pio1_device);
  996. at32_init_pio(&pio2_device);
  997. at32_init_pio(&pio3_device);
  998. at32_init_pio(&pio4_device);
  999. }
  1000. void __init at32_clock_init(void)
  1001. {
  1002. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1003. int i;
  1004. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL))
  1005. main_clock = &pll0;
  1006. else
  1007. main_clock = &osc0;
  1008. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1009. pll0.parent = &osc1;
  1010. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1011. pll1.parent = &osc1;
  1012. genclk_init_parent(&gclk0);
  1013. genclk_init_parent(&gclk1);
  1014. genclk_init_parent(&gclk2);
  1015. genclk_init_parent(&gclk3);
  1016. genclk_init_parent(&gclk4);
  1017. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1018. /*
  1019. * Turn on all clocks that have at least one user already, and
  1020. * turn off everything else. We only do this for module
  1021. * clocks, and even though it isn't particularly pretty to
  1022. * check the address of the mode function, it should do the
  1023. * trick...
  1024. */
  1025. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  1026. struct clk *clk = at32_clock_list[i];
  1027. if (clk->users == 0)
  1028. continue;
  1029. if (clk->mode == &cpu_clk_mode)
  1030. cpu_mask |= 1 << clk->index;
  1031. else if (clk->mode == &hsb_clk_mode)
  1032. hsb_mask |= 1 << clk->index;
  1033. else if (clk->mode == &pba_clk_mode)
  1034. pba_mask |= 1 << clk->index;
  1035. else if (clk->mode == &pbb_clk_mode)
  1036. pbb_mask |= 1 << clk->index;
  1037. }
  1038. pm_writel(CPU_MASK, cpu_mask);
  1039. pm_writel(HSB_MASK, hsb_mask);
  1040. pm_writel(PBA_MASK, pba_mask);
  1041. pm_writel(PBB_MASK, pbb_mask);
  1042. }