at32ap7000.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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 resource usart0_resource[] = {
  441. PBMEM(0xffe00c00),
  442. IRQ(7),
  443. };
  444. DEFINE_DEV(usart, 0);
  445. DEV_CLK(usart, usart0, pba, 4);
  446. static struct resource usart1_resource[] = {
  447. PBMEM(0xffe01000),
  448. IRQ(7),
  449. };
  450. DEFINE_DEV(usart, 1);
  451. DEV_CLK(usart, usart1, pba, 4);
  452. static struct resource usart2_resource[] = {
  453. PBMEM(0xffe01400),
  454. IRQ(8),
  455. };
  456. DEFINE_DEV(usart, 2);
  457. DEV_CLK(usart, usart2, pba, 5);
  458. static struct resource usart3_resource[] = {
  459. PBMEM(0xffe01800),
  460. IRQ(9),
  461. };
  462. DEFINE_DEV(usart, 3);
  463. DEV_CLK(usart, usart3, pba, 6);
  464. static inline void configure_usart0_pins(void)
  465. {
  466. portmux_set_func(PIOA, 8, FUNC_B); /* RXD */
  467. portmux_set_func(PIOA, 9, FUNC_B); /* TXD */
  468. }
  469. static inline void configure_usart1_pins(void)
  470. {
  471. portmux_set_func(PIOA, 17, FUNC_A); /* RXD */
  472. portmux_set_func(PIOA, 18, FUNC_A); /* TXD */
  473. }
  474. static inline void configure_usart2_pins(void)
  475. {
  476. portmux_set_func(PIOB, 26, FUNC_B); /* RXD */
  477. portmux_set_func(PIOB, 27, FUNC_B); /* TXD */
  478. }
  479. static inline void configure_usart3_pins(void)
  480. {
  481. portmux_set_func(PIOB, 18, FUNC_B); /* RXD */
  482. portmux_set_func(PIOB, 17, FUNC_B); /* TXD */
  483. }
  484. static struct platform_device *setup_usart(unsigned int id)
  485. {
  486. struct platform_device *pdev;
  487. switch (id) {
  488. case 0:
  489. pdev = &usart0_device;
  490. configure_usart0_pins();
  491. break;
  492. case 1:
  493. pdev = &usart1_device;
  494. configure_usart1_pins();
  495. break;
  496. case 2:
  497. pdev = &usart2_device;
  498. configure_usart2_pins();
  499. break;
  500. case 3:
  501. pdev = &usart3_device;
  502. configure_usart3_pins();
  503. break;
  504. default:
  505. pdev = NULL;
  506. break;
  507. }
  508. return pdev;
  509. }
  510. struct platform_device *__init at32_add_device_usart(unsigned int id)
  511. {
  512. struct platform_device *pdev;
  513. pdev = setup_usart(id);
  514. if (pdev)
  515. platform_device_register(pdev);
  516. return pdev;
  517. }
  518. struct platform_device *at91_default_console_device;
  519. void __init at32_setup_serial_console(unsigned int usart_id)
  520. {
  521. at91_default_console_device = setup_usart(usart_id);
  522. }
  523. /* --------------------------------------------------------------------
  524. * Ethernet
  525. * -------------------------------------------------------------------- */
  526. static struct eth_platform_data macb0_data;
  527. static struct resource macb0_resource[] = {
  528. PBMEM(0xfff01800),
  529. IRQ(25),
  530. };
  531. DEFINE_DEV_DATA(macb, 0);
  532. DEV_CLK(hclk, macb0, hsb, 8);
  533. DEV_CLK(pclk, macb0, pbb, 6);
  534. struct platform_device *__init
  535. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  536. {
  537. struct platform_device *pdev;
  538. switch (id) {
  539. case 0:
  540. pdev = &macb0_device;
  541. portmux_set_func(PIOC, 3, FUNC_A); /* TXD0 */
  542. portmux_set_func(PIOC, 4, FUNC_A); /* TXD1 */
  543. portmux_set_func(PIOC, 7, FUNC_A); /* TXEN */
  544. portmux_set_func(PIOC, 8, FUNC_A); /* TXCK */
  545. portmux_set_func(PIOC, 9, FUNC_A); /* RXD0 */
  546. portmux_set_func(PIOC, 10, FUNC_A); /* RXD1 */
  547. portmux_set_func(PIOC, 13, FUNC_A); /* RXER */
  548. portmux_set_func(PIOC, 15, FUNC_A); /* RXDV */
  549. portmux_set_func(PIOC, 16, FUNC_A); /* MDC */
  550. portmux_set_func(PIOC, 17, FUNC_A); /* MDIO */
  551. if (!data->is_rmii) {
  552. portmux_set_func(PIOC, 0, FUNC_A); /* COL */
  553. portmux_set_func(PIOC, 1, FUNC_A); /* CRS */
  554. portmux_set_func(PIOC, 2, FUNC_A); /* TXER */
  555. portmux_set_func(PIOC, 5, FUNC_A); /* TXD2 */
  556. portmux_set_func(PIOC, 6, FUNC_A); /* TXD3 */
  557. portmux_set_func(PIOC, 11, FUNC_A); /* RXD2 */
  558. portmux_set_func(PIOC, 12, FUNC_A); /* RXD3 */
  559. portmux_set_func(PIOC, 14, FUNC_A); /* RXCK */
  560. portmux_set_func(PIOC, 18, FUNC_A); /* SPD */
  561. }
  562. break;
  563. default:
  564. return NULL;
  565. }
  566. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  567. platform_device_register(pdev);
  568. return pdev;
  569. }
  570. /* --------------------------------------------------------------------
  571. * SPI
  572. * -------------------------------------------------------------------- */
  573. static struct resource spi0_resource[] = {
  574. PBMEM(0xffe00000),
  575. IRQ(3),
  576. };
  577. DEFINE_DEV(spi, 0);
  578. DEV_CLK(mck, spi0, pba, 0);
  579. struct platform_device *__init at32_add_device_spi(unsigned int id)
  580. {
  581. struct platform_device *pdev;
  582. switch (id) {
  583. case 0:
  584. pdev = &spi0_device;
  585. portmux_set_func(PIOA, 0, FUNC_A); /* MISO */
  586. portmux_set_func(PIOA, 1, FUNC_A); /* MOSI */
  587. portmux_set_func(PIOA, 2, FUNC_A); /* SCK */
  588. portmux_set_func(PIOA, 3, FUNC_A); /* NPCS0 */
  589. portmux_set_func(PIOA, 4, FUNC_A); /* NPCS1 */
  590. portmux_set_func(PIOA, 5, FUNC_A); /* NPCS2 */
  591. break;
  592. default:
  593. return NULL;
  594. }
  595. platform_device_register(pdev);
  596. return pdev;
  597. }
  598. /* --------------------------------------------------------------------
  599. * LCDC
  600. * -------------------------------------------------------------------- */
  601. static struct lcdc_platform_data lcdc0_data;
  602. static struct resource lcdc0_resource[] = {
  603. {
  604. .start = 0xff000000,
  605. .end = 0xff000fff,
  606. .flags = IORESOURCE_MEM,
  607. },
  608. IRQ(1),
  609. };
  610. DEFINE_DEV_DATA(lcdc, 0);
  611. DEV_CLK(hclk, lcdc0, hsb, 7);
  612. static struct clk lcdc0_pixclk = {
  613. .name = "pixclk",
  614. .dev = &lcdc0_device.dev,
  615. .mode = genclk_mode,
  616. .get_rate = genclk_get_rate,
  617. .set_rate = genclk_set_rate,
  618. .set_parent = genclk_set_parent,
  619. .index = 7,
  620. };
  621. struct platform_device *__init
  622. at32_add_device_lcdc(unsigned int id, struct lcdc_platform_data *data)
  623. {
  624. struct platform_device *pdev;
  625. switch (id) {
  626. case 0:
  627. pdev = &lcdc0_device;
  628. portmux_set_func(PIOC, 19, FUNC_A); /* CC */
  629. portmux_set_func(PIOC, 20, FUNC_A); /* HSYNC */
  630. portmux_set_func(PIOC, 21, FUNC_A); /* PCLK */
  631. portmux_set_func(PIOC, 22, FUNC_A); /* VSYNC */
  632. portmux_set_func(PIOC, 23, FUNC_A); /* DVAL */
  633. portmux_set_func(PIOC, 24, FUNC_A); /* MODE */
  634. portmux_set_func(PIOC, 25, FUNC_A); /* PWR */
  635. portmux_set_func(PIOC, 26, FUNC_A); /* DATA0 */
  636. portmux_set_func(PIOC, 27, FUNC_A); /* DATA1 */
  637. portmux_set_func(PIOC, 28, FUNC_A); /* DATA2 */
  638. portmux_set_func(PIOC, 29, FUNC_A); /* DATA3 */
  639. portmux_set_func(PIOC, 30, FUNC_A); /* DATA4 */
  640. portmux_set_func(PIOC, 31, FUNC_A); /* DATA5 */
  641. portmux_set_func(PIOD, 0, FUNC_A); /* DATA6 */
  642. portmux_set_func(PIOD, 1, FUNC_A); /* DATA7 */
  643. portmux_set_func(PIOD, 2, FUNC_A); /* DATA8 */
  644. portmux_set_func(PIOD, 3, FUNC_A); /* DATA9 */
  645. portmux_set_func(PIOD, 4, FUNC_A); /* DATA10 */
  646. portmux_set_func(PIOD, 5, FUNC_A); /* DATA11 */
  647. portmux_set_func(PIOD, 6, FUNC_A); /* DATA12 */
  648. portmux_set_func(PIOD, 7, FUNC_A); /* DATA13 */
  649. portmux_set_func(PIOD, 8, FUNC_A); /* DATA14 */
  650. portmux_set_func(PIOD, 9, FUNC_A); /* DATA15 */
  651. portmux_set_func(PIOD, 10, FUNC_A); /* DATA16 */
  652. portmux_set_func(PIOD, 11, FUNC_A); /* DATA17 */
  653. portmux_set_func(PIOD, 12, FUNC_A); /* DATA18 */
  654. portmux_set_func(PIOD, 13, FUNC_A); /* DATA19 */
  655. portmux_set_func(PIOD, 14, FUNC_A); /* DATA20 */
  656. portmux_set_func(PIOD, 15, FUNC_A); /* DATA21 */
  657. portmux_set_func(PIOD, 16, FUNC_A); /* DATA22 */
  658. portmux_set_func(PIOD, 17, FUNC_A); /* DATA23 */
  659. clk_set_parent(&lcdc0_pixclk, &pll0);
  660. clk_set_rate(&lcdc0_pixclk, clk_get_rate(&pll0));
  661. break;
  662. default:
  663. return NULL;
  664. }
  665. memcpy(pdev->dev.platform_data, data,
  666. sizeof(struct lcdc_platform_data));
  667. platform_device_register(pdev);
  668. return pdev;
  669. }
  670. struct clk *at32_clock_list[] = {
  671. &osc32k,
  672. &osc0,
  673. &osc1,
  674. &pll0,
  675. &pll1,
  676. &cpu_clk,
  677. &hsb_clk,
  678. &pba_clk,
  679. &pbb_clk,
  680. &at32_sm_pclk,
  681. &at32_intc0_pclk,
  682. &ebi_clk,
  683. &hramc_clk,
  684. &smc0_pclk,
  685. &smc0_mck,
  686. &pdc_hclk,
  687. &pdc_pclk,
  688. &pico_clk,
  689. &pio0_mck,
  690. &pio1_mck,
  691. &pio2_mck,
  692. &pio3_mck,
  693. &usart0_usart,
  694. &usart1_usart,
  695. &usart2_usart,
  696. &usart3_usart,
  697. &macb0_hclk,
  698. &macb0_pclk,
  699. &spi0_mck,
  700. &lcdc0_hclk,
  701. &lcdc0_pixclk,
  702. };
  703. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  704. void __init at32_portmux_init(void)
  705. {
  706. at32_init_pio(&pio0_device);
  707. at32_init_pio(&pio1_device);
  708. at32_init_pio(&pio2_device);
  709. at32_init_pio(&pio3_device);
  710. }
  711. void __init at32_clock_init(void)
  712. {
  713. struct at32_sm *sm = &system_manager;
  714. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  715. int i;
  716. if (sm_readl(sm, PM_MCCTRL) & SM_BIT(PLLSEL))
  717. main_clock = &pll0;
  718. else
  719. main_clock = &osc0;
  720. if (sm_readl(sm, PM_PLL0) & SM_BIT(PLLOSC))
  721. pll0.parent = &osc1;
  722. if (sm_readl(sm, PM_PLL1) & SM_BIT(PLLOSC))
  723. pll1.parent = &osc1;
  724. /*
  725. * Turn on all clocks that have at least one user already, and
  726. * turn off everything else. We only do this for module
  727. * clocks, and even though it isn't particularly pretty to
  728. * check the address of the mode function, it should do the
  729. * trick...
  730. */
  731. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  732. struct clk *clk = at32_clock_list[i];
  733. if (clk->mode == &cpu_clk_mode)
  734. cpu_mask |= 1 << clk->index;
  735. else if (clk->mode == &hsb_clk_mode)
  736. hsb_mask |= 1 << clk->index;
  737. else if (clk->mode == &pba_clk_mode)
  738. pba_mask |= 1 << clk->index;
  739. else if (clk->mode == &pbb_clk_mode)
  740. pbb_mask |= 1 << clk->index;
  741. }
  742. sm_writel(sm, PM_CPU_MASK, cpu_mask);
  743. sm_writel(sm, PM_HSB_MASK, hsb_mask);
  744. sm_writel(sm, PM_PBA_MASK, pba_mask);
  745. sm_writel(sm, PM_PBB_MASK, pbb_mask);
  746. }