at32ap7000.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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/at32ap7000.h>
  13. #include <asm/arch/board.h>
  14. #include <asm/arch/portmux.h>
  15. #include <asm/arch/sm.h>
  16. #include "clock.h"
  17. #include "pio.h"
  18. #include "sm.h"
  19. #define PBMEM(base) \
  20. { \
  21. .start = base, \
  22. .end = base + 0x3ff, \
  23. .flags = IORESOURCE_MEM, \
  24. }
  25. #define IRQ(num) \
  26. { \
  27. .start = num, \
  28. .end = num, \
  29. .flags = IORESOURCE_IRQ, \
  30. }
  31. #define NAMED_IRQ(num, _name) \
  32. { \
  33. .start = num, \
  34. .end = num, \
  35. .name = _name, \
  36. .flags = IORESOURCE_IRQ, \
  37. }
  38. #define DEFINE_DEV(_name, _id) \
  39. static struct platform_device _name##_id##_device = { \
  40. .name = #_name, \
  41. .id = _id, \
  42. .resource = _name##_id##_resource, \
  43. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  44. }
  45. #define DEFINE_DEV_DATA(_name, _id) \
  46. static struct platform_device _name##_id##_device = { \
  47. .name = #_name, \
  48. .id = _id, \
  49. .dev = { \
  50. .platform_data = &_name##_id##_data, \
  51. }, \
  52. .resource = _name##_id##_resource, \
  53. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  54. }
  55. #define select_peripheral(pin, periph, flags) \
  56. at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
  57. #define DEV_CLK(_name, devname, bus, _index) \
  58. static struct clk devname##_##_name = { \
  59. .name = #_name, \
  60. .dev = &devname##_device.dev, \
  61. .parent = &bus##_clk, \
  62. .mode = bus##_clk_mode, \
  63. .get_rate = bus##_clk_get_rate, \
  64. .index = _index, \
  65. }
  66. unsigned long at32ap7000_osc_rates[3] = {
  67. [0] = 32768,
  68. /* FIXME: these are ATSTK1002-specific */
  69. [1] = 20000000,
  70. [2] = 12000000,
  71. };
  72. static unsigned long osc_get_rate(struct clk *clk)
  73. {
  74. return at32ap7000_osc_rates[clk->index];
  75. }
  76. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  77. {
  78. unsigned long div, mul, rate;
  79. if (!(control & SM_BIT(PLLEN)))
  80. return 0;
  81. div = SM_BFEXT(PLLDIV, control) + 1;
  82. mul = SM_BFEXT(PLLMUL, control) + 1;
  83. rate = clk->parent->get_rate(clk->parent);
  84. rate = (rate + div / 2) / div;
  85. rate *= mul;
  86. return rate;
  87. }
  88. static unsigned long pll0_get_rate(struct clk *clk)
  89. {
  90. u32 control;
  91. control = sm_readl(&system_manager, PM_PLL0);
  92. return pll_get_rate(clk, control);
  93. }
  94. static unsigned long pll1_get_rate(struct clk *clk)
  95. {
  96. u32 control;
  97. control = sm_readl(&system_manager, PM_PLL1);
  98. return pll_get_rate(clk, control);
  99. }
  100. /*
  101. * The AT32AP7000 has five primary clock sources: One 32kHz
  102. * oscillator, two crystal oscillators and two PLLs.
  103. */
  104. static struct clk osc32k = {
  105. .name = "osc32k",
  106. .get_rate = osc_get_rate,
  107. .users = 1,
  108. .index = 0,
  109. };
  110. static struct clk osc0 = {
  111. .name = "osc0",
  112. .get_rate = osc_get_rate,
  113. .users = 1,
  114. .index = 1,
  115. };
  116. static struct clk osc1 = {
  117. .name = "osc1",
  118. .get_rate = osc_get_rate,
  119. .index = 2,
  120. };
  121. static struct clk pll0 = {
  122. .name = "pll0",
  123. .get_rate = pll0_get_rate,
  124. .parent = &osc0,
  125. };
  126. static struct clk pll1 = {
  127. .name = "pll1",
  128. .get_rate = pll1_get_rate,
  129. .parent = &osc0,
  130. };
  131. /*
  132. * The main clock can be either osc0 or pll0. The boot loader may
  133. * have chosen one for us, so we don't really know which one until we
  134. * have a look at the SM.
  135. */
  136. static struct clk *main_clock;
  137. /*
  138. * Synchronous clocks are generated from the main clock. The clocks
  139. * must satisfy the constraint
  140. * fCPU >= fHSB >= fPB
  141. * i.e. each clock must not be faster than its parent.
  142. */
  143. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  144. {
  145. return main_clock->get_rate(main_clock) >> shift;
  146. };
  147. static void cpu_clk_mode(struct clk *clk, int enabled)
  148. {
  149. struct at32_sm *sm = &system_manager;
  150. unsigned long flags;
  151. u32 mask;
  152. spin_lock_irqsave(&sm->lock, flags);
  153. mask = sm_readl(sm, PM_CPU_MASK);
  154. if (enabled)
  155. mask |= 1 << clk->index;
  156. else
  157. mask &= ~(1 << clk->index);
  158. sm_writel(sm, PM_CPU_MASK, mask);
  159. spin_unlock_irqrestore(&sm->lock, flags);
  160. }
  161. static unsigned long cpu_clk_get_rate(struct clk *clk)
  162. {
  163. unsigned long cksel, shift = 0;
  164. cksel = sm_readl(&system_manager, PM_CKSEL);
  165. if (cksel & SM_BIT(CPUDIV))
  166. shift = SM_BFEXT(CPUSEL, cksel) + 1;
  167. return bus_clk_get_rate(clk, shift);
  168. }
  169. static void hsb_clk_mode(struct clk *clk, int enabled)
  170. {
  171. struct at32_sm *sm = &system_manager;
  172. unsigned long flags;
  173. u32 mask;
  174. spin_lock_irqsave(&sm->lock, flags);
  175. mask = sm_readl(sm, PM_HSB_MASK);
  176. if (enabled)
  177. mask |= 1 << clk->index;
  178. else
  179. mask &= ~(1 << clk->index);
  180. sm_writel(sm, PM_HSB_MASK, mask);
  181. spin_unlock_irqrestore(&sm->lock, flags);
  182. }
  183. static unsigned long hsb_clk_get_rate(struct clk *clk)
  184. {
  185. unsigned long cksel, shift = 0;
  186. cksel = sm_readl(&system_manager, PM_CKSEL);
  187. if (cksel & SM_BIT(HSBDIV))
  188. shift = SM_BFEXT(HSBSEL, cksel) + 1;
  189. return bus_clk_get_rate(clk, shift);
  190. }
  191. static void pba_clk_mode(struct clk *clk, int enabled)
  192. {
  193. struct at32_sm *sm = &system_manager;
  194. unsigned long flags;
  195. u32 mask;
  196. spin_lock_irqsave(&sm->lock, flags);
  197. mask = sm_readl(sm, PM_PBA_MASK);
  198. if (enabled)
  199. mask |= 1 << clk->index;
  200. else
  201. mask &= ~(1 << clk->index);
  202. sm_writel(sm, PM_PBA_MASK, mask);
  203. spin_unlock_irqrestore(&sm->lock, flags);
  204. }
  205. static unsigned long pba_clk_get_rate(struct clk *clk)
  206. {
  207. unsigned long cksel, shift = 0;
  208. cksel = sm_readl(&system_manager, PM_CKSEL);
  209. if (cksel & SM_BIT(PBADIV))
  210. shift = SM_BFEXT(PBASEL, cksel) + 1;
  211. return bus_clk_get_rate(clk, shift);
  212. }
  213. static void pbb_clk_mode(struct clk *clk, int enabled)
  214. {
  215. struct at32_sm *sm = &system_manager;
  216. unsigned long flags;
  217. u32 mask;
  218. spin_lock_irqsave(&sm->lock, flags);
  219. mask = sm_readl(sm, PM_PBB_MASK);
  220. if (enabled)
  221. mask |= 1 << clk->index;
  222. else
  223. mask &= ~(1 << clk->index);
  224. sm_writel(sm, PM_PBB_MASK, mask);
  225. spin_unlock_irqrestore(&sm->lock, flags);
  226. }
  227. static unsigned long pbb_clk_get_rate(struct clk *clk)
  228. {
  229. unsigned long cksel, shift = 0;
  230. cksel = sm_readl(&system_manager, PM_CKSEL);
  231. if (cksel & SM_BIT(PBBDIV))
  232. shift = SM_BFEXT(PBBSEL, cksel) + 1;
  233. return bus_clk_get_rate(clk, shift);
  234. }
  235. static struct clk cpu_clk = {
  236. .name = "cpu",
  237. .get_rate = cpu_clk_get_rate,
  238. .users = 1,
  239. };
  240. static struct clk hsb_clk = {
  241. .name = "hsb",
  242. .parent = &cpu_clk,
  243. .get_rate = hsb_clk_get_rate,
  244. };
  245. static struct clk pba_clk = {
  246. .name = "pba",
  247. .parent = &hsb_clk,
  248. .mode = hsb_clk_mode,
  249. .get_rate = pba_clk_get_rate,
  250. .index = 1,
  251. };
  252. static struct clk pbb_clk = {
  253. .name = "pbb",
  254. .parent = &hsb_clk,
  255. .mode = hsb_clk_mode,
  256. .get_rate = pbb_clk_get_rate,
  257. .users = 1,
  258. .index = 2,
  259. };
  260. /* --------------------------------------------------------------------
  261. * Generic Clock operations
  262. * -------------------------------------------------------------------- */
  263. static void genclk_mode(struct clk *clk, int enabled)
  264. {
  265. u32 control;
  266. BUG_ON(clk->index > 7);
  267. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  268. if (enabled)
  269. control |= SM_BIT(CEN);
  270. else
  271. control &= ~SM_BIT(CEN);
  272. sm_writel(&system_manager, PM_GCCTRL + 4 * clk->index, control);
  273. }
  274. static unsigned long genclk_get_rate(struct clk *clk)
  275. {
  276. u32 control;
  277. unsigned long div = 1;
  278. BUG_ON(clk->index > 7);
  279. if (!clk->parent)
  280. return 0;
  281. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  282. if (control & SM_BIT(DIVEN))
  283. div = 2 * (SM_BFEXT(DIV, control) + 1);
  284. return clk->parent->get_rate(clk->parent) / div;
  285. }
  286. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  287. {
  288. u32 control;
  289. unsigned long parent_rate, actual_rate, div;
  290. BUG_ON(clk->index > 7);
  291. if (!clk->parent)
  292. return 0;
  293. parent_rate = clk->parent->get_rate(clk->parent);
  294. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  295. if (rate > 3 * parent_rate / 4) {
  296. actual_rate = parent_rate;
  297. control &= ~SM_BIT(DIVEN);
  298. } else {
  299. div = (parent_rate + rate) / (2 * rate) - 1;
  300. control = SM_BFINS(DIV, div, control) | SM_BIT(DIVEN);
  301. actual_rate = parent_rate / (2 * (div + 1));
  302. }
  303. printk("clk %s: new rate %lu (actual rate %lu)\n",
  304. clk->name, rate, actual_rate);
  305. if (apply)
  306. sm_writel(&system_manager, PM_GCCTRL + 4 * clk->index,
  307. control);
  308. return actual_rate;
  309. }
  310. int genclk_set_parent(struct clk *clk, struct clk *parent)
  311. {
  312. u32 control;
  313. BUG_ON(clk->index > 7);
  314. printk("clk %s: new parent %s (was %s)\n",
  315. clk->name, parent->name,
  316. clk->parent ? clk->parent->name : "(null)");
  317. control = sm_readl(&system_manager, PM_GCCTRL + 4 * clk->index);
  318. if (parent == &osc1 || parent == &pll1)
  319. control |= SM_BIT(OSCSEL);
  320. else if (parent == &osc0 || parent == &pll0)
  321. control &= ~SM_BIT(OSCSEL);
  322. else
  323. return -EINVAL;
  324. if (parent == &pll0 || parent == &pll1)
  325. control |= SM_BIT(PLLSEL);
  326. else
  327. control &= ~SM_BIT(PLLSEL);
  328. sm_writel(&system_manager, PM_GCCTRL + 4 * clk->index, control);
  329. clk->parent = parent;
  330. return 0;
  331. }
  332. /* --------------------------------------------------------------------
  333. * System peripherals
  334. * -------------------------------------------------------------------- */
  335. static struct resource sm_resource[] = {
  336. PBMEM(0xfff00000),
  337. NAMED_IRQ(19, "eim"),
  338. NAMED_IRQ(20, "pm"),
  339. NAMED_IRQ(21, "rtc"),
  340. };
  341. struct platform_device at32_sm_device = {
  342. .name = "sm",
  343. .id = 0,
  344. .resource = sm_resource,
  345. .num_resources = ARRAY_SIZE(sm_resource),
  346. };
  347. DEV_CLK(pclk, at32_sm, pbb, 0);
  348. static struct resource intc0_resource[] = {
  349. PBMEM(0xfff00400),
  350. };
  351. struct platform_device at32_intc0_device = {
  352. .name = "intc",
  353. .id = 0,
  354. .resource = intc0_resource,
  355. .num_resources = ARRAY_SIZE(intc0_resource),
  356. };
  357. DEV_CLK(pclk, at32_intc0, pbb, 1);
  358. static struct clk ebi_clk = {
  359. .name = "ebi",
  360. .parent = &hsb_clk,
  361. .mode = hsb_clk_mode,
  362. .get_rate = hsb_clk_get_rate,
  363. .users = 1,
  364. };
  365. static struct clk hramc_clk = {
  366. .name = "hramc",
  367. .parent = &hsb_clk,
  368. .mode = hsb_clk_mode,
  369. .get_rate = hsb_clk_get_rate,
  370. .users = 1,
  371. };
  372. static struct resource smc0_resource[] = {
  373. PBMEM(0xfff03400),
  374. };
  375. DEFINE_DEV(smc, 0);
  376. DEV_CLK(pclk, smc0, pbb, 13);
  377. DEV_CLK(mck, smc0, hsb, 0);
  378. static struct platform_device pdc_device = {
  379. .name = "pdc",
  380. .id = 0,
  381. };
  382. DEV_CLK(hclk, pdc, hsb, 4);
  383. DEV_CLK(pclk, pdc, pba, 16);
  384. static struct clk pico_clk = {
  385. .name = "pico",
  386. .parent = &cpu_clk,
  387. .mode = cpu_clk_mode,
  388. .get_rate = cpu_clk_get_rate,
  389. .users = 1,
  390. };
  391. /* --------------------------------------------------------------------
  392. * PIO
  393. * -------------------------------------------------------------------- */
  394. static struct resource pio0_resource[] = {
  395. PBMEM(0xffe02800),
  396. IRQ(13),
  397. };
  398. DEFINE_DEV(pio, 0);
  399. DEV_CLK(mck, pio0, pba, 10);
  400. static struct resource pio1_resource[] = {
  401. PBMEM(0xffe02c00),
  402. IRQ(14),
  403. };
  404. DEFINE_DEV(pio, 1);
  405. DEV_CLK(mck, pio1, pba, 11);
  406. static struct resource pio2_resource[] = {
  407. PBMEM(0xffe03000),
  408. IRQ(15),
  409. };
  410. DEFINE_DEV(pio, 2);
  411. DEV_CLK(mck, pio2, pba, 12);
  412. static struct resource pio3_resource[] = {
  413. PBMEM(0xffe03400),
  414. IRQ(16),
  415. };
  416. DEFINE_DEV(pio, 3);
  417. DEV_CLK(mck, pio3, pba, 13);
  418. static struct resource pio4_resource[] = {
  419. PBMEM(0xffe03800),
  420. IRQ(17),
  421. };
  422. DEFINE_DEV(pio, 4);
  423. DEV_CLK(mck, pio4, pba, 14);
  424. void __init at32_add_system_devices(void)
  425. {
  426. system_manager.eim_first_irq = EIM_IRQ_BASE;
  427. platform_device_register(&at32_sm_device);
  428. platform_device_register(&at32_intc0_device);
  429. platform_device_register(&smc0_device);
  430. platform_device_register(&pdc_device);
  431. platform_device_register(&pio0_device);
  432. platform_device_register(&pio1_device);
  433. platform_device_register(&pio2_device);
  434. platform_device_register(&pio3_device);
  435. platform_device_register(&pio4_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(6),
  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. select_peripheral(PA(8), PERIPH_B, 0); /* RXD */
  483. select_peripheral(PA(9), PERIPH_B, 0); /* TXD */
  484. }
  485. static inline void configure_usart1_pins(void)
  486. {
  487. select_peripheral(PA(17), PERIPH_A, 0); /* RXD */
  488. select_peripheral(PA(18), PERIPH_A, 0); /* TXD */
  489. }
  490. static inline void configure_usart2_pins(void)
  491. {
  492. select_peripheral(PB(26), PERIPH_B, 0); /* RXD */
  493. select_peripheral(PB(27), PERIPH_B, 0); /* TXD */
  494. }
  495. static inline void configure_usart3_pins(void)
  496. {
  497. select_peripheral(PB(18), PERIPH_B, 0); /* RXD */
  498. select_peripheral(PB(17), PERIPH_B, 0); /* TXD */
  499. }
  500. static struct platform_device *__initdata 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. static struct eth_platform_data macb1_data;
  554. static struct resource macb1_resource[] = {
  555. PBMEM(0xfff01c00),
  556. IRQ(26),
  557. };
  558. DEFINE_DEV_DATA(macb, 1);
  559. DEV_CLK(hclk, macb1, hsb, 9);
  560. DEV_CLK(pclk, macb1, pbb, 7);
  561. struct platform_device *__init
  562. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  563. {
  564. struct platform_device *pdev;
  565. switch (id) {
  566. case 0:
  567. pdev = &macb0_device;
  568. select_peripheral(PC(3), PERIPH_A, 0); /* TXD0 */
  569. select_peripheral(PC(4), PERIPH_A, 0); /* TXD1 */
  570. select_peripheral(PC(7), PERIPH_A, 0); /* TXEN */
  571. select_peripheral(PC(8), PERIPH_A, 0); /* TXCK */
  572. select_peripheral(PC(9), PERIPH_A, 0); /* RXD0 */
  573. select_peripheral(PC(10), PERIPH_A, 0); /* RXD1 */
  574. select_peripheral(PC(13), PERIPH_A, 0); /* RXER */
  575. select_peripheral(PC(15), PERIPH_A, 0); /* RXDV */
  576. select_peripheral(PC(16), PERIPH_A, 0); /* MDC */
  577. select_peripheral(PC(17), PERIPH_A, 0); /* MDIO */
  578. if (!data->is_rmii) {
  579. select_peripheral(PC(0), PERIPH_A, 0); /* COL */
  580. select_peripheral(PC(1), PERIPH_A, 0); /* CRS */
  581. select_peripheral(PC(2), PERIPH_A, 0); /* TXER */
  582. select_peripheral(PC(5), PERIPH_A, 0); /* TXD2 */
  583. select_peripheral(PC(6), PERIPH_A, 0); /* TXD3 */
  584. select_peripheral(PC(11), PERIPH_A, 0); /* RXD2 */
  585. select_peripheral(PC(12), PERIPH_A, 0); /* RXD3 */
  586. select_peripheral(PC(14), PERIPH_A, 0); /* RXCK */
  587. select_peripheral(PC(18), PERIPH_A, 0); /* SPD */
  588. }
  589. break;
  590. case 1:
  591. pdev = &macb1_device;
  592. select_peripheral(PD(13), PERIPH_B, 0); /* TXD0 */
  593. select_peripheral(PD(14), PERIPH_B, 0); /* TXD1 */
  594. select_peripheral(PD(11), PERIPH_B, 0); /* TXEN */
  595. select_peripheral(PD(12), PERIPH_B, 0); /* TXCK */
  596. select_peripheral(PD(10), PERIPH_B, 0); /* RXD0 */
  597. select_peripheral(PD(6), PERIPH_B, 0); /* RXD1 */
  598. select_peripheral(PD(5), PERIPH_B, 0); /* RXER */
  599. select_peripheral(PD(4), PERIPH_B, 0); /* RXDV */
  600. select_peripheral(PD(3), PERIPH_B, 0); /* MDC */
  601. select_peripheral(PD(2), PERIPH_B, 0); /* MDIO */
  602. if (!data->is_rmii) {
  603. select_peripheral(PC(19), PERIPH_B, 0); /* COL */
  604. select_peripheral(PC(23), PERIPH_B, 0); /* CRS */
  605. select_peripheral(PC(26), PERIPH_B, 0); /* TXER */
  606. select_peripheral(PC(27), PERIPH_B, 0); /* TXD2 */
  607. select_peripheral(PC(28), PERIPH_B, 0); /* TXD3 */
  608. select_peripheral(PC(29), PERIPH_B, 0); /* RXD2 */
  609. select_peripheral(PC(30), PERIPH_B, 0); /* RXD3 */
  610. select_peripheral(PC(24), PERIPH_B, 0); /* RXCK */
  611. select_peripheral(PD(15), PERIPH_B, 0); /* SPD */
  612. }
  613. break;
  614. default:
  615. return NULL;
  616. }
  617. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  618. platform_device_register(pdev);
  619. return pdev;
  620. }
  621. /* --------------------------------------------------------------------
  622. * SPI
  623. * -------------------------------------------------------------------- */
  624. static struct resource atmel_spi0_resource[] = {
  625. PBMEM(0xffe00000),
  626. IRQ(3),
  627. };
  628. DEFINE_DEV(atmel_spi, 0);
  629. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  630. static struct resource atmel_spi1_resource[] = {
  631. PBMEM(0xffe00400),
  632. IRQ(4),
  633. };
  634. DEFINE_DEV(atmel_spi, 1);
  635. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  636. struct platform_device *__init at32_add_device_spi(unsigned int id)
  637. {
  638. struct platform_device *pdev;
  639. switch (id) {
  640. case 0:
  641. pdev = &atmel_spi0_device;
  642. select_peripheral(PA(0), PERIPH_A, 0); /* MISO */
  643. select_peripheral(PA(1), PERIPH_A, 0); /* MOSI */
  644. select_peripheral(PA(2), PERIPH_A, 0); /* SCK */
  645. /* NPCS[2:0] */
  646. at32_select_gpio(GPIO_PIN_PA(3),
  647. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  648. at32_select_gpio(GPIO_PIN_PA(4),
  649. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  650. at32_select_gpio(GPIO_PIN_PA(5),
  651. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  652. break;
  653. case 1:
  654. pdev = &atmel_spi1_device;
  655. select_peripheral(PB(0), PERIPH_B, 0); /* MISO */
  656. select_peripheral(PB(1), PERIPH_B, 0); /* MOSI */
  657. select_peripheral(PB(5), PERIPH_B, 0); /* SCK */
  658. /* NPCS[2:0] */
  659. at32_select_gpio(GPIO_PIN_PB(2),
  660. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  661. at32_select_gpio(GPIO_PIN_PB(3),
  662. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  663. at32_select_gpio(GPIO_PIN_PB(4),
  664. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  665. break;
  666. default:
  667. return NULL;
  668. }
  669. platform_device_register(pdev);
  670. return pdev;
  671. }
  672. /* --------------------------------------------------------------------
  673. * LCDC
  674. * -------------------------------------------------------------------- */
  675. static struct lcdc_platform_data lcdc0_data;
  676. static struct resource lcdc0_resource[] = {
  677. {
  678. .start = 0xff000000,
  679. .end = 0xff000fff,
  680. .flags = IORESOURCE_MEM,
  681. },
  682. IRQ(1),
  683. };
  684. DEFINE_DEV_DATA(lcdc, 0);
  685. DEV_CLK(hclk, lcdc0, hsb, 7);
  686. static struct clk lcdc0_pixclk = {
  687. .name = "pixclk",
  688. .dev = &lcdc0_device.dev,
  689. .mode = genclk_mode,
  690. .get_rate = genclk_get_rate,
  691. .set_rate = genclk_set_rate,
  692. .set_parent = genclk_set_parent,
  693. .index = 7,
  694. };
  695. struct platform_device *__init
  696. at32_add_device_lcdc(unsigned int id, struct lcdc_platform_data *data)
  697. {
  698. struct platform_device *pdev;
  699. switch (id) {
  700. case 0:
  701. pdev = &lcdc0_device;
  702. select_peripheral(PC(19), PERIPH_A, 0); /* CC */
  703. select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
  704. select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
  705. select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
  706. select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
  707. select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
  708. select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
  709. select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
  710. select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
  711. select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
  712. select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
  713. select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
  714. select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
  715. select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
  716. select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
  717. select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
  718. select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
  719. select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
  720. select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
  721. select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
  722. select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
  723. select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
  724. select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
  725. select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
  726. select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
  727. select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
  728. select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
  729. select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
  730. select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
  731. select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
  732. select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
  733. clk_set_parent(&lcdc0_pixclk, &pll0);
  734. clk_set_rate(&lcdc0_pixclk, clk_get_rate(&pll0));
  735. break;
  736. default:
  737. return NULL;
  738. }
  739. memcpy(pdev->dev.platform_data, data,
  740. sizeof(struct lcdc_platform_data));
  741. platform_device_register(pdev);
  742. return pdev;
  743. }
  744. struct clk *at32_clock_list[] = {
  745. &osc32k,
  746. &osc0,
  747. &osc1,
  748. &pll0,
  749. &pll1,
  750. &cpu_clk,
  751. &hsb_clk,
  752. &pba_clk,
  753. &pbb_clk,
  754. &at32_sm_pclk,
  755. &at32_intc0_pclk,
  756. &ebi_clk,
  757. &hramc_clk,
  758. &smc0_pclk,
  759. &smc0_mck,
  760. &pdc_hclk,
  761. &pdc_pclk,
  762. &pico_clk,
  763. &pio0_mck,
  764. &pio1_mck,
  765. &pio2_mck,
  766. &pio3_mck,
  767. &pio4_mck,
  768. &atmel_usart0_usart,
  769. &atmel_usart1_usart,
  770. &atmel_usart2_usart,
  771. &atmel_usart3_usart,
  772. &macb0_hclk,
  773. &macb0_pclk,
  774. &macb1_hclk,
  775. &macb1_pclk,
  776. &atmel_spi0_spi_clk,
  777. &atmel_spi1_spi_clk,
  778. &lcdc0_hclk,
  779. &lcdc0_pixclk,
  780. };
  781. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  782. void __init at32_portmux_init(void)
  783. {
  784. at32_init_pio(&pio0_device);
  785. at32_init_pio(&pio1_device);
  786. at32_init_pio(&pio2_device);
  787. at32_init_pio(&pio3_device);
  788. at32_init_pio(&pio4_device);
  789. }
  790. void __init at32_clock_init(void)
  791. {
  792. struct at32_sm *sm = &system_manager;
  793. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  794. int i;
  795. if (sm_readl(sm, PM_MCCTRL) & SM_BIT(PLLSEL))
  796. main_clock = &pll0;
  797. else
  798. main_clock = &osc0;
  799. if (sm_readl(sm, PM_PLL0) & SM_BIT(PLLOSC))
  800. pll0.parent = &osc1;
  801. if (sm_readl(sm, PM_PLL1) & SM_BIT(PLLOSC))
  802. pll1.parent = &osc1;
  803. /*
  804. * Turn on all clocks that have at least one user already, and
  805. * turn off everything else. We only do this for module
  806. * clocks, and even though it isn't particularly pretty to
  807. * check the address of the mode function, it should do the
  808. * trick...
  809. */
  810. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  811. struct clk *clk = at32_clock_list[i];
  812. if (clk->mode == &cpu_clk_mode)
  813. cpu_mask |= 1 << clk->index;
  814. else if (clk->mode == &hsb_clk_mode)
  815. hsb_mask |= 1 << clk->index;
  816. else if (clk->mode == &pba_clk_mode)
  817. pba_mask |= 1 << clk->index;
  818. else if (clk->mode == &pbb_clk_mode)
  819. pbb_mask |= 1 << clk->index;
  820. }
  821. sm_writel(sm, PM_CPU_MASK, cpu_mask);
  822. sm_writel(sm, PM_HSB_MASK, hsb_mask);
  823. sm_writel(sm, PM_PBA_MASK, pba_mask);
  824. sm_writel(sm, PM_PBB_MASK, pbb_mask);
  825. }