at32ap7000.c 29 KB

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