at32ap7000.c 28 KB

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