at32ap7000.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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/init.h>
  10. #include <linux/platform_device.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/board.h>
  13. #include <asm/arch/portmux.h>
  14. #include <asm/arch/sm.h>
  15. #include "clock.h"
  16. #include "pio.h"
  17. #include "sm.h"
  18. #define PBMEM(base) \
  19. { \
  20. .start = base, \
  21. .end = base + 0x3ff, \
  22. .flags = IORESOURCE_MEM, \
  23. }
  24. #define IRQ(num) \
  25. { \
  26. .start = num, \
  27. .end = num, \
  28. .flags = IORESOURCE_IRQ, \
  29. }
  30. #define NAMED_IRQ(num, _name) \
  31. { \
  32. .start = num, \
  33. .end = num, \
  34. .name = _name, \
  35. .flags = IORESOURCE_IRQ, \
  36. }
  37. #define DEFINE_DEV(_name, _id) \
  38. static struct platform_device _name##_id##_device = { \
  39. .name = #_name, \
  40. .id = _id, \
  41. .resource = _name##_id##_resource, \
  42. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  43. }
  44. #define DEFINE_DEV_DATA(_name, _id) \
  45. static struct platform_device _name##_id##_device = { \
  46. .name = #_name, \
  47. .id = _id, \
  48. .dev = { \
  49. .platform_data = &_name##_id##_data, \
  50. }, \
  51. .resource = _name##_id##_resource, \
  52. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  53. }
  54. #define DEV_CLK(_name, devname, bus, _index) \
  55. static struct clk devname##_##_name = { \
  56. .name = #_name, \
  57. .dev = &devname##_device.dev, \
  58. .parent = &bus##_clk, \
  59. .mode = bus##_clk_mode, \
  60. .get_rate = bus##_clk_get_rate, \
  61. .index = _index, \
  62. }
  63. enum {
  64. PIOA,
  65. PIOB,
  66. PIOC,
  67. PIOD,
  68. };
  69. enum {
  70. FUNC_A,
  71. FUNC_B,
  72. };
  73. unsigned long at32ap7000_osc_rates[3] = {
  74. [0] = 32768,
  75. /* FIXME: these are ATSTK1002-specific */
  76. [1] = 20000000,
  77. [2] = 12000000,
  78. };
  79. static unsigned long osc_get_rate(struct clk *clk)
  80. {
  81. return at32ap7000_osc_rates[clk->index];
  82. }
  83. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  84. {
  85. unsigned long div, mul, rate;
  86. if (!(control & SM_BIT(PLLEN)))
  87. return 0;
  88. div = SM_BFEXT(PLLDIV, control) + 1;
  89. mul = SM_BFEXT(PLLMUL, control) + 1;
  90. rate = clk->parent->get_rate(clk->parent);
  91. rate = (rate + div / 2) / div;
  92. rate *= mul;
  93. return rate;
  94. }
  95. static unsigned long pll0_get_rate(struct clk *clk)
  96. {
  97. u32 control;
  98. control = sm_readl(&system_manager, PM_PLL0);
  99. return pll_get_rate(clk, control);
  100. }
  101. static unsigned long pll1_get_rate(struct clk *clk)
  102. {
  103. u32 control;
  104. control = sm_readl(&system_manager, PM_PLL1);
  105. return pll_get_rate(clk, control);
  106. }
  107. /*
  108. * The AT32AP7000 has five primary clock sources: One 32kHz
  109. * oscillator, two crystal oscillators and two PLLs.
  110. */
  111. static struct clk osc32k = {
  112. .name = "osc32k",
  113. .get_rate = osc_get_rate,
  114. .users = 1,
  115. .index = 0,
  116. };
  117. static struct clk osc0 = {
  118. .name = "osc0",
  119. .get_rate = osc_get_rate,
  120. .users = 1,
  121. .index = 1,
  122. };
  123. static struct clk osc1 = {
  124. .name = "osc1",
  125. .get_rate = osc_get_rate,
  126. .index = 2,
  127. };
  128. static struct clk pll0 = {
  129. .name = "pll0",
  130. .get_rate = pll0_get_rate,
  131. .parent = &osc0,
  132. };
  133. static struct clk pll1 = {
  134. .name = "pll1",
  135. .get_rate = pll1_get_rate,
  136. .parent = &osc0,
  137. };
  138. /*
  139. * The main clock can be either osc0 or pll0. The boot loader may
  140. * have chosen one for us, so we don't really know which one until we
  141. * have a look at the SM.
  142. */
  143. static struct clk *main_clock;
  144. /*
  145. * Synchronous clocks are generated from the main clock. The clocks
  146. * must satisfy the constraint
  147. * fCPU >= fHSB >= fPB
  148. * i.e. each clock must not be faster than its parent.
  149. */
  150. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  151. {
  152. return main_clock->get_rate(main_clock) >> shift;
  153. };
  154. static void cpu_clk_mode(struct clk *clk, int enabled)
  155. {
  156. struct at32_sm *sm = &system_manager;
  157. unsigned long flags;
  158. u32 mask;
  159. spin_lock_irqsave(&sm->lock, flags);
  160. mask = sm_readl(sm, PM_CPU_MASK);
  161. if (enabled)
  162. mask |= 1 << clk->index;
  163. else
  164. mask &= ~(1 << clk->index);
  165. sm_writel(sm, PM_CPU_MASK, mask);
  166. spin_unlock_irqrestore(&sm->lock, flags);
  167. }
  168. static unsigned long cpu_clk_get_rate(struct clk *clk)
  169. {
  170. unsigned long cksel, shift = 0;
  171. cksel = sm_readl(&system_manager, PM_CKSEL);
  172. if (cksel & SM_BIT(CPUDIV))
  173. shift = SM_BFEXT(CPUSEL, cksel) + 1;
  174. return bus_clk_get_rate(clk, shift);
  175. }
  176. static void hsb_clk_mode(struct clk *clk, int enabled)
  177. {
  178. struct at32_sm *sm = &system_manager;
  179. unsigned long flags;
  180. u32 mask;
  181. spin_lock_irqsave(&sm->lock, flags);
  182. mask = sm_readl(sm, PM_HSB_MASK);
  183. if (enabled)
  184. mask |= 1 << clk->index;
  185. else
  186. mask &= ~(1 << clk->index);
  187. sm_writel(sm, PM_HSB_MASK, mask);
  188. spin_unlock_irqrestore(&sm->lock, flags);
  189. }
  190. static unsigned long hsb_clk_get_rate(struct clk *clk)
  191. {
  192. unsigned long cksel, shift = 0;
  193. cksel = sm_readl(&system_manager, PM_CKSEL);
  194. if (cksel & SM_BIT(HSBDIV))
  195. shift = SM_BFEXT(HSBSEL, cksel) + 1;
  196. return bus_clk_get_rate(clk, shift);
  197. }
  198. static void pba_clk_mode(struct clk *clk, int enabled)
  199. {
  200. struct at32_sm *sm = &system_manager;
  201. unsigned long flags;
  202. u32 mask;
  203. spin_lock_irqsave(&sm->lock, flags);
  204. mask = sm_readl(sm, PM_PBA_MASK);
  205. if (enabled)
  206. mask |= 1 << clk->index;
  207. else
  208. mask &= ~(1 << clk->index);
  209. sm_writel(sm, PM_PBA_MASK, mask);
  210. spin_unlock_irqrestore(&sm->lock, flags);
  211. }
  212. static unsigned long pba_clk_get_rate(struct clk *clk)
  213. {
  214. unsigned long cksel, shift = 0;
  215. cksel = sm_readl(&system_manager, PM_CKSEL);
  216. if (cksel & SM_BIT(PBADIV))
  217. shift = SM_BFEXT(PBASEL, cksel) + 1;
  218. return bus_clk_get_rate(clk, shift);
  219. }
  220. static void pbb_clk_mode(struct clk *clk, int enabled)
  221. {
  222. struct at32_sm *sm = &system_manager;
  223. unsigned long flags;
  224. u32 mask;
  225. spin_lock_irqsave(&sm->lock, flags);
  226. mask = sm_readl(sm, PM_PBB_MASK);
  227. if (enabled)
  228. mask |= 1 << clk->index;
  229. else
  230. mask &= ~(1 << clk->index);
  231. sm_writel(sm, PM_PBB_MASK, mask);
  232. spin_unlock_irqrestore(&sm->lock, flags);
  233. }
  234. static unsigned long pbb_clk_get_rate(struct clk *clk)
  235. {
  236. unsigned long cksel, shift = 0;
  237. cksel = sm_readl(&system_manager, PM_CKSEL);
  238. if (cksel & SM_BIT(PBBDIV))
  239. shift = SM_BFEXT(PBBSEL, cksel) + 1;
  240. return bus_clk_get_rate(clk, shift);
  241. }
  242. static struct clk cpu_clk = {
  243. .name = "cpu",
  244. .get_rate = cpu_clk_get_rate,
  245. .users = 1,
  246. };
  247. static struct clk hsb_clk = {
  248. .name = "hsb",
  249. .parent = &cpu_clk,
  250. .get_rate = hsb_clk_get_rate,
  251. };
  252. static struct clk pba_clk = {
  253. .name = "pba",
  254. .parent = &hsb_clk,
  255. .mode = hsb_clk_mode,
  256. .get_rate = pba_clk_get_rate,
  257. .index = 1,
  258. };
  259. static struct clk pbb_clk = {
  260. .name = "pbb",
  261. .parent = &hsb_clk,
  262. .mode = hsb_clk_mode,
  263. .get_rate = pbb_clk_get_rate,
  264. .users = 1,
  265. .index = 2,
  266. };
  267. /* --------------------------------------------------------------------
  268. * Generic Clock operations
  269. * -------------------------------------------------------------------- */
  270. static void genclk_mode(struct clk *clk, int enabled)
  271. {
  272. u32 control;
  273. BUG_ON(clk->index > 7);
  274. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  275. if (enabled)
  276. control |= SM_BIT(CEN);
  277. else
  278. control &= ~SM_BIT(CEN);
  279. sm_writel(&system_manager, PM_GCCTRL + 4 * clk->index, control);
  280. }
  281. static unsigned long genclk_get_rate(struct clk *clk)
  282. {
  283. u32 control;
  284. unsigned long div = 1;
  285. BUG_ON(clk->index > 7);
  286. if (!clk->parent)
  287. return 0;
  288. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  289. if (control & SM_BIT(DIVEN))
  290. div = 2 * (SM_BFEXT(DIV, control) + 1);
  291. return clk->parent->get_rate(clk->parent) / div;
  292. }
  293. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  294. {
  295. u32 control;
  296. unsigned long parent_rate, actual_rate, div;
  297. BUG_ON(clk->index > 7);
  298. if (!clk->parent)
  299. return 0;
  300. parent_rate = clk->parent->get_rate(clk->parent);
  301. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  302. if (rate > 3 * parent_rate / 4) {
  303. actual_rate = parent_rate;
  304. control &= ~SM_BIT(DIVEN);
  305. } else {
  306. div = (parent_rate + rate) / (2 * rate) - 1;
  307. control = SM_BFINS(DIV, div, control) | SM_BIT(DIVEN);
  308. actual_rate = parent_rate / (2 * (div + 1));
  309. }
  310. printk("clk %s: new rate %lu (actual rate %lu)\n",
  311. clk->name, rate, actual_rate);
  312. if (apply)
  313. sm_writel(&system_manager, PM_GCCTRL + 4 * clk->index,
  314. control);
  315. return actual_rate;
  316. }
  317. int genclk_set_parent(struct clk *clk, struct clk *parent)
  318. {
  319. u32 control;
  320. BUG_ON(clk->index > 7);
  321. printk("clk %s: new parent %s (was %s)\n",
  322. clk->name, parent->name,
  323. clk->parent ? clk->parent->name : "(null)");
  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. /* --------------------------------------------------------------------
  340. * System peripherals
  341. * -------------------------------------------------------------------- */
  342. static struct resource sm_resource[] = {
  343. PBMEM(0xfff00000),
  344. NAMED_IRQ(19, "eim"),
  345. NAMED_IRQ(20, "pm"),
  346. NAMED_IRQ(21, "rtc"),
  347. };
  348. struct platform_device at32_sm_device = {
  349. .name = "sm",
  350. .id = 0,
  351. .resource = sm_resource,
  352. .num_resources = ARRAY_SIZE(sm_resource),
  353. };
  354. DEV_CLK(pclk, at32_sm, pbb, 0);
  355. static struct resource intc0_resource[] = {
  356. PBMEM(0xfff00400),
  357. };
  358. struct platform_device at32_intc0_device = {
  359. .name = "intc",
  360. .id = 0,
  361. .resource = intc0_resource,
  362. .num_resources = ARRAY_SIZE(intc0_resource),
  363. };
  364. DEV_CLK(pclk, at32_intc0, pbb, 1);
  365. static struct clk ebi_clk = {
  366. .name = "ebi",
  367. .parent = &hsb_clk,
  368. .mode = hsb_clk_mode,
  369. .get_rate = hsb_clk_get_rate,
  370. .users = 1,
  371. };
  372. static struct clk hramc_clk = {
  373. .name = "hramc",
  374. .parent = &hsb_clk,
  375. .mode = hsb_clk_mode,
  376. .get_rate = hsb_clk_get_rate,
  377. .users = 1,
  378. };
  379. static struct resource smc0_resource[] = {
  380. PBMEM(0xfff03400),
  381. };
  382. DEFINE_DEV(smc, 0);
  383. DEV_CLK(pclk, smc0, pbb, 13);
  384. DEV_CLK(mck, smc0, hsb, 0);
  385. static struct platform_device pdc_device = {
  386. .name = "pdc",
  387. .id = 0,
  388. };
  389. DEV_CLK(hclk, pdc, hsb, 4);
  390. DEV_CLK(pclk, pdc, pba, 16);
  391. static struct clk pico_clk = {
  392. .name = "pico",
  393. .parent = &cpu_clk,
  394. .mode = cpu_clk_mode,
  395. .get_rate = cpu_clk_get_rate,
  396. .users = 1,
  397. };
  398. /* --------------------------------------------------------------------
  399. * PIO
  400. * -------------------------------------------------------------------- */
  401. static struct resource pio0_resource[] = {
  402. PBMEM(0xffe02800),
  403. IRQ(13),
  404. };
  405. DEFINE_DEV(pio, 0);
  406. DEV_CLK(mck, pio0, pba, 10);
  407. static struct resource pio1_resource[] = {
  408. PBMEM(0xffe02c00),
  409. IRQ(14),
  410. };
  411. DEFINE_DEV(pio, 1);
  412. DEV_CLK(mck, pio1, pba, 11);
  413. static struct resource pio2_resource[] = {
  414. PBMEM(0xffe03000),
  415. IRQ(15),
  416. };
  417. DEFINE_DEV(pio, 2);
  418. DEV_CLK(mck, pio2, pba, 12);
  419. static struct resource pio3_resource[] = {
  420. PBMEM(0xffe03400),
  421. IRQ(16),
  422. };
  423. DEFINE_DEV(pio, 3);
  424. DEV_CLK(mck, pio3, pba, 13);
  425. void __init at32_add_system_devices(void)
  426. {
  427. system_manager.eim_first_irq = NR_INTERNAL_IRQS;
  428. platform_device_register(&at32_sm_device);
  429. platform_device_register(&at32_intc0_device);
  430. platform_device_register(&smc0_device);
  431. platform_device_register(&pdc_device);
  432. platform_device_register(&pio0_device);
  433. platform_device_register(&pio1_device);
  434. platform_device_register(&pio2_device);
  435. platform_device_register(&pio3_device);
  436. }
  437. /* --------------------------------------------------------------------
  438. * USART
  439. * -------------------------------------------------------------------- */
  440. static struct atmel_uart_data atmel_usart0_data = {
  441. .use_dma_tx = 1,
  442. .use_dma_rx = 1,
  443. };
  444. static struct resource atmel_usart0_resource[] = {
  445. PBMEM(0xffe00c00),
  446. IRQ(7),
  447. };
  448. DEFINE_DEV_DATA(atmel_usart, 0);
  449. DEV_CLK(usart, atmel_usart0, pba, 4);
  450. static struct atmel_uart_data atmel_usart1_data = {
  451. .use_dma_tx = 1,
  452. .use_dma_rx = 1,
  453. };
  454. static struct resource atmel_usart1_resource[] = {
  455. PBMEM(0xffe01000),
  456. IRQ(7),
  457. };
  458. DEFINE_DEV_DATA(atmel_usart, 1);
  459. DEV_CLK(usart, atmel_usart1, pba, 4);
  460. static struct atmel_uart_data atmel_usart2_data = {
  461. .use_dma_tx = 1,
  462. .use_dma_rx = 1,
  463. };
  464. static struct resource atmel_usart2_resource[] = {
  465. PBMEM(0xffe01400),
  466. IRQ(8),
  467. };
  468. DEFINE_DEV_DATA(atmel_usart, 2);
  469. DEV_CLK(usart, atmel_usart2, pba, 5);
  470. static struct atmel_uart_data atmel_usart3_data = {
  471. .use_dma_tx = 1,
  472. .use_dma_rx = 1,
  473. };
  474. static struct resource atmel_usart3_resource[] = {
  475. PBMEM(0xffe01800),
  476. IRQ(9),
  477. };
  478. DEFINE_DEV_DATA(atmel_usart, 3);
  479. DEV_CLK(usart, atmel_usart3, pba, 6);
  480. static inline void configure_usart0_pins(void)
  481. {
  482. portmux_set_func(PIOA, 8, FUNC_B); /* RXD */
  483. portmux_set_func(PIOA, 9, FUNC_B); /* TXD */
  484. }
  485. static inline void configure_usart1_pins(void)
  486. {
  487. portmux_set_func(PIOA, 17, FUNC_A); /* RXD */
  488. portmux_set_func(PIOA, 18, FUNC_A); /* TXD */
  489. }
  490. static inline void configure_usart2_pins(void)
  491. {
  492. portmux_set_func(PIOB, 26, FUNC_B); /* RXD */
  493. portmux_set_func(PIOB, 27, FUNC_B); /* TXD */
  494. }
  495. static inline void configure_usart3_pins(void)
  496. {
  497. portmux_set_func(PIOB, 18, FUNC_B); /* RXD */
  498. portmux_set_func(PIOB, 17, FUNC_B); /* TXD */
  499. }
  500. static struct platform_device *at32_usarts[4];
  501. void __init at32_map_usart(unsigned int hw_id, unsigned int line)
  502. {
  503. struct platform_device *pdev;
  504. switch (hw_id) {
  505. case 0:
  506. pdev = &atmel_usart0_device;
  507. configure_usart0_pins();
  508. break;
  509. case 1:
  510. pdev = &atmel_usart1_device;
  511. configure_usart1_pins();
  512. break;
  513. case 2:
  514. pdev = &atmel_usart2_device;
  515. configure_usart2_pins();
  516. break;
  517. case 3:
  518. pdev = &atmel_usart3_device;
  519. configure_usart3_pins();
  520. break;
  521. default:
  522. return;
  523. }
  524. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  525. /* Addresses in the P4 segment are permanently mapped 1:1 */
  526. struct atmel_uart_data *data = pdev->dev.platform_data;
  527. data->regs = (void __iomem *)pdev->resource[0].start;
  528. }
  529. pdev->id = line;
  530. at32_usarts[line] = pdev;
  531. }
  532. struct platform_device *__init at32_add_device_usart(unsigned int id)
  533. {
  534. platform_device_register(at32_usarts[id]);
  535. return at32_usarts[id];
  536. }
  537. struct platform_device *atmel_default_console_device;
  538. void __init at32_setup_serial_console(unsigned int usart_id)
  539. {
  540. atmel_default_console_device = at32_usarts[usart_id];
  541. }
  542. /* --------------------------------------------------------------------
  543. * Ethernet
  544. * -------------------------------------------------------------------- */
  545. static struct eth_platform_data macb0_data;
  546. static struct resource macb0_resource[] = {
  547. PBMEM(0xfff01800),
  548. IRQ(25),
  549. };
  550. DEFINE_DEV_DATA(macb, 0);
  551. DEV_CLK(hclk, macb0, hsb, 8);
  552. DEV_CLK(pclk, macb0, pbb, 6);
  553. struct platform_device *__init
  554. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  555. {
  556. struct platform_device *pdev;
  557. switch (id) {
  558. case 0:
  559. pdev = &macb0_device;
  560. portmux_set_func(PIOC, 3, FUNC_A); /* TXD0 */
  561. portmux_set_func(PIOC, 4, FUNC_A); /* TXD1 */
  562. portmux_set_func(PIOC, 7, FUNC_A); /* TXEN */
  563. portmux_set_func(PIOC, 8, FUNC_A); /* TXCK */
  564. portmux_set_func(PIOC, 9, FUNC_A); /* RXD0 */
  565. portmux_set_func(PIOC, 10, FUNC_A); /* RXD1 */
  566. portmux_set_func(PIOC, 13, FUNC_A); /* RXER */
  567. portmux_set_func(PIOC, 15, FUNC_A); /* RXDV */
  568. portmux_set_func(PIOC, 16, FUNC_A); /* MDC */
  569. portmux_set_func(PIOC, 17, FUNC_A); /* MDIO */
  570. if (!data->is_rmii) {
  571. portmux_set_func(PIOC, 0, FUNC_A); /* COL */
  572. portmux_set_func(PIOC, 1, FUNC_A); /* CRS */
  573. portmux_set_func(PIOC, 2, FUNC_A); /* TXER */
  574. portmux_set_func(PIOC, 5, FUNC_A); /* TXD2 */
  575. portmux_set_func(PIOC, 6, FUNC_A); /* TXD3 */
  576. portmux_set_func(PIOC, 11, FUNC_A); /* RXD2 */
  577. portmux_set_func(PIOC, 12, FUNC_A); /* RXD3 */
  578. portmux_set_func(PIOC, 14, FUNC_A); /* RXCK */
  579. portmux_set_func(PIOC, 18, FUNC_A); /* SPD */
  580. }
  581. break;
  582. default:
  583. return NULL;
  584. }
  585. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  586. platform_device_register(pdev);
  587. return pdev;
  588. }
  589. /* --------------------------------------------------------------------
  590. * SPI
  591. * -------------------------------------------------------------------- */
  592. static struct resource spi0_resource[] = {
  593. PBMEM(0xffe00000),
  594. IRQ(3),
  595. };
  596. DEFINE_DEV(spi, 0);
  597. DEV_CLK(mck, spi0, pba, 0);
  598. struct platform_device *__init at32_add_device_spi(unsigned int id)
  599. {
  600. struct platform_device *pdev;
  601. switch (id) {
  602. case 0:
  603. pdev = &spi0_device;
  604. portmux_set_func(PIOA, 0, FUNC_A); /* MISO */
  605. portmux_set_func(PIOA, 1, FUNC_A); /* MOSI */
  606. portmux_set_func(PIOA, 2, FUNC_A); /* SCK */
  607. portmux_set_func(PIOA, 3, FUNC_A); /* NPCS0 */
  608. portmux_set_func(PIOA, 4, FUNC_A); /* NPCS1 */
  609. portmux_set_func(PIOA, 5, FUNC_A); /* NPCS2 */
  610. break;
  611. default:
  612. return NULL;
  613. }
  614. platform_device_register(pdev);
  615. return pdev;
  616. }
  617. /* --------------------------------------------------------------------
  618. * LCDC
  619. * -------------------------------------------------------------------- */
  620. static struct lcdc_platform_data lcdc0_data;
  621. static struct resource lcdc0_resource[] = {
  622. {
  623. .start = 0xff000000,
  624. .end = 0xff000fff,
  625. .flags = IORESOURCE_MEM,
  626. },
  627. IRQ(1),
  628. };
  629. DEFINE_DEV_DATA(lcdc, 0);
  630. DEV_CLK(hclk, lcdc0, hsb, 7);
  631. static struct clk lcdc0_pixclk = {
  632. .name = "pixclk",
  633. .dev = &lcdc0_device.dev,
  634. .mode = genclk_mode,
  635. .get_rate = genclk_get_rate,
  636. .set_rate = genclk_set_rate,
  637. .set_parent = genclk_set_parent,
  638. .index = 7,
  639. };
  640. struct platform_device *__init
  641. at32_add_device_lcdc(unsigned int id, struct lcdc_platform_data *data)
  642. {
  643. struct platform_device *pdev;
  644. switch (id) {
  645. case 0:
  646. pdev = &lcdc0_device;
  647. portmux_set_func(PIOC, 19, FUNC_A); /* CC */
  648. portmux_set_func(PIOC, 20, FUNC_A); /* HSYNC */
  649. portmux_set_func(PIOC, 21, FUNC_A); /* PCLK */
  650. portmux_set_func(PIOC, 22, FUNC_A); /* VSYNC */
  651. portmux_set_func(PIOC, 23, FUNC_A); /* DVAL */
  652. portmux_set_func(PIOC, 24, FUNC_A); /* MODE */
  653. portmux_set_func(PIOC, 25, FUNC_A); /* PWR */
  654. portmux_set_func(PIOC, 26, FUNC_A); /* DATA0 */
  655. portmux_set_func(PIOC, 27, FUNC_A); /* DATA1 */
  656. portmux_set_func(PIOC, 28, FUNC_A); /* DATA2 */
  657. portmux_set_func(PIOC, 29, FUNC_A); /* DATA3 */
  658. portmux_set_func(PIOC, 30, FUNC_A); /* DATA4 */
  659. portmux_set_func(PIOC, 31, FUNC_A); /* DATA5 */
  660. portmux_set_func(PIOD, 0, FUNC_A); /* DATA6 */
  661. portmux_set_func(PIOD, 1, FUNC_A); /* DATA7 */
  662. portmux_set_func(PIOD, 2, FUNC_A); /* DATA8 */
  663. portmux_set_func(PIOD, 3, FUNC_A); /* DATA9 */
  664. portmux_set_func(PIOD, 4, FUNC_A); /* DATA10 */
  665. portmux_set_func(PIOD, 5, FUNC_A); /* DATA11 */
  666. portmux_set_func(PIOD, 6, FUNC_A); /* DATA12 */
  667. portmux_set_func(PIOD, 7, FUNC_A); /* DATA13 */
  668. portmux_set_func(PIOD, 8, FUNC_A); /* DATA14 */
  669. portmux_set_func(PIOD, 9, FUNC_A); /* DATA15 */
  670. portmux_set_func(PIOD, 10, FUNC_A); /* DATA16 */
  671. portmux_set_func(PIOD, 11, FUNC_A); /* DATA17 */
  672. portmux_set_func(PIOD, 12, FUNC_A); /* DATA18 */
  673. portmux_set_func(PIOD, 13, FUNC_A); /* DATA19 */
  674. portmux_set_func(PIOD, 14, FUNC_A); /* DATA20 */
  675. portmux_set_func(PIOD, 15, FUNC_A); /* DATA21 */
  676. portmux_set_func(PIOD, 16, FUNC_A); /* DATA22 */
  677. portmux_set_func(PIOD, 17, FUNC_A); /* DATA23 */
  678. clk_set_parent(&lcdc0_pixclk, &pll0);
  679. clk_set_rate(&lcdc0_pixclk, clk_get_rate(&pll0));
  680. break;
  681. default:
  682. return NULL;
  683. }
  684. memcpy(pdev->dev.platform_data, data,
  685. sizeof(struct lcdc_platform_data));
  686. platform_device_register(pdev);
  687. return pdev;
  688. }
  689. struct clk *at32_clock_list[] = {
  690. &osc32k,
  691. &osc0,
  692. &osc1,
  693. &pll0,
  694. &pll1,
  695. &cpu_clk,
  696. &hsb_clk,
  697. &pba_clk,
  698. &pbb_clk,
  699. &at32_sm_pclk,
  700. &at32_intc0_pclk,
  701. &ebi_clk,
  702. &hramc_clk,
  703. &smc0_pclk,
  704. &smc0_mck,
  705. &pdc_hclk,
  706. &pdc_pclk,
  707. &pico_clk,
  708. &pio0_mck,
  709. &pio1_mck,
  710. &pio2_mck,
  711. &pio3_mck,
  712. &atmel_usart0_usart,
  713. &atmel_usart1_usart,
  714. &atmel_usart2_usart,
  715. &atmel_usart3_usart,
  716. &macb0_hclk,
  717. &macb0_pclk,
  718. &spi0_mck,
  719. &lcdc0_hclk,
  720. &lcdc0_pixclk,
  721. };
  722. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  723. void __init at32_portmux_init(void)
  724. {
  725. at32_init_pio(&pio0_device);
  726. at32_init_pio(&pio1_device);
  727. at32_init_pio(&pio2_device);
  728. at32_init_pio(&pio3_device);
  729. }
  730. void __init at32_clock_init(void)
  731. {
  732. struct at32_sm *sm = &system_manager;
  733. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  734. int i;
  735. if (sm_readl(sm, PM_MCCTRL) & SM_BIT(PLLSEL))
  736. main_clock = &pll0;
  737. else
  738. main_clock = &osc0;
  739. if (sm_readl(sm, PM_PLL0) & SM_BIT(PLLOSC))
  740. pll0.parent = &osc1;
  741. if (sm_readl(sm, PM_PLL1) & SM_BIT(PLLOSC))
  742. pll1.parent = &osc1;
  743. /*
  744. * Turn on all clocks that have at least one user already, and
  745. * turn off everything else. We only do this for module
  746. * clocks, and even though it isn't particularly pretty to
  747. * check the address of the mode function, it should do the
  748. * trick...
  749. */
  750. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  751. struct clk *clk = at32_clock_list[i];
  752. if (clk->mode == &cpu_clk_mode)
  753. cpu_mask |= 1 << clk->index;
  754. else if (clk->mode == &hsb_clk_mode)
  755. hsb_mask |= 1 << clk->index;
  756. else if (clk->mode == &pba_clk_mode)
  757. pba_mask |= 1 << clk->index;
  758. else if (clk->mode == &pbb_clk_mode)
  759. pbb_mask |= 1 << clk->index;
  760. }
  761. sm_writel(sm, PM_CPU_MASK, cpu_mask);
  762. sm_writel(sm, PM_HSB_MASK, hsb_mask);
  763. sm_writel(sm, PM_PBA_MASK, pba_mask);
  764. sm_writel(sm, PM_PBB_MASK, pbb_mask);
  765. }