at32ap7000.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/fb.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/spi/spi.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/at32ap7000.h>
  16. #include <asm/arch/board.h>
  17. #include <asm/arch/portmux.h>
  18. #include <video/atmel_lcdc.h>
  19. #include "clock.h"
  20. #include "hmatrix.h"
  21. #include "pio.h"
  22. #include "pm.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. /* REVISIT these assume *every* device supports DMA, but several
  43. * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
  44. */
  45. #define DEFINE_DEV(_name, _id) \
  46. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  47. static struct platform_device _name##_id##_device = { \
  48. .name = #_name, \
  49. .id = _id, \
  50. .dev = { \
  51. .dma_mask = &_name##_id##_dma_mask, \
  52. .coherent_dma_mask = DMA_32BIT_MASK, \
  53. }, \
  54. .resource = _name##_id##_resource, \
  55. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  56. }
  57. #define DEFINE_DEV_DATA(_name, _id) \
  58. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  59. static struct platform_device _name##_id##_device = { \
  60. .name = #_name, \
  61. .id = _id, \
  62. .dev = { \
  63. .dma_mask = &_name##_id##_dma_mask, \
  64. .platform_data = &_name##_id##_data, \
  65. .coherent_dma_mask = DMA_32BIT_MASK, \
  66. }, \
  67. .resource = _name##_id##_resource, \
  68. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  69. }
  70. #define select_peripheral(pin, periph, flags) \
  71. at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
  72. #define DEV_CLK(_name, devname, bus, _index) \
  73. static struct clk devname##_##_name = { \
  74. .name = #_name, \
  75. .dev = &devname##_device.dev, \
  76. .parent = &bus##_clk, \
  77. .mode = bus##_clk_mode, \
  78. .get_rate = bus##_clk_get_rate, \
  79. .index = _index, \
  80. }
  81. static DEFINE_SPINLOCK(pm_lock);
  82. unsigned long at32ap7000_osc_rates[3] = {
  83. [0] = 32768,
  84. /* FIXME: these are ATSTK1002-specific */
  85. [1] = 20000000,
  86. [2] = 12000000,
  87. };
  88. static unsigned long osc_get_rate(struct clk *clk)
  89. {
  90. return at32ap7000_osc_rates[clk->index];
  91. }
  92. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  93. {
  94. unsigned long div, mul, rate;
  95. if (!(control & PM_BIT(PLLEN)))
  96. return 0;
  97. div = PM_BFEXT(PLLDIV, control) + 1;
  98. mul = PM_BFEXT(PLLMUL, control) + 1;
  99. rate = clk->parent->get_rate(clk->parent);
  100. rate = (rate + div / 2) / div;
  101. rate *= mul;
  102. return rate;
  103. }
  104. static unsigned long pll0_get_rate(struct clk *clk)
  105. {
  106. u32 control;
  107. control = pm_readl(PLL0);
  108. return pll_get_rate(clk, control);
  109. }
  110. static unsigned long pll1_get_rate(struct clk *clk)
  111. {
  112. u32 control;
  113. control = pm_readl(PLL1);
  114. return pll_get_rate(clk, control);
  115. }
  116. /*
  117. * The AT32AP7000 has five primary clock sources: One 32kHz
  118. * oscillator, two crystal oscillators and two PLLs.
  119. */
  120. static struct clk osc32k = {
  121. .name = "osc32k",
  122. .get_rate = osc_get_rate,
  123. .users = 1,
  124. .index = 0,
  125. };
  126. static struct clk osc0 = {
  127. .name = "osc0",
  128. .get_rate = osc_get_rate,
  129. .users = 1,
  130. .index = 1,
  131. };
  132. static struct clk osc1 = {
  133. .name = "osc1",
  134. .get_rate = osc_get_rate,
  135. .index = 2,
  136. };
  137. static struct clk pll0 = {
  138. .name = "pll0",
  139. .get_rate = pll0_get_rate,
  140. .parent = &osc0,
  141. };
  142. static struct clk pll1 = {
  143. .name = "pll1",
  144. .get_rate = pll1_get_rate,
  145. .parent = &osc0,
  146. };
  147. /*
  148. * The main clock can be either osc0 or pll0. The boot loader may
  149. * have chosen one for us, so we don't really know which one until we
  150. * have a look at the SM.
  151. */
  152. static struct clk *main_clock;
  153. /*
  154. * Synchronous clocks are generated from the main clock. The clocks
  155. * must satisfy the constraint
  156. * fCPU >= fHSB >= fPB
  157. * i.e. each clock must not be faster than its parent.
  158. */
  159. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  160. {
  161. return main_clock->get_rate(main_clock) >> shift;
  162. };
  163. static void cpu_clk_mode(struct clk *clk, int enabled)
  164. {
  165. unsigned long flags;
  166. u32 mask;
  167. spin_lock_irqsave(&pm_lock, flags);
  168. mask = pm_readl(CPU_MASK);
  169. if (enabled)
  170. mask |= 1 << clk->index;
  171. else
  172. mask &= ~(1 << clk->index);
  173. pm_writel(CPU_MASK, mask);
  174. spin_unlock_irqrestore(&pm_lock, flags);
  175. }
  176. static unsigned long cpu_clk_get_rate(struct clk *clk)
  177. {
  178. unsigned long cksel, shift = 0;
  179. cksel = pm_readl(CKSEL);
  180. if (cksel & PM_BIT(CPUDIV))
  181. shift = PM_BFEXT(CPUSEL, cksel) + 1;
  182. return bus_clk_get_rate(clk, shift);
  183. }
  184. static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
  185. {
  186. u32 control;
  187. unsigned long parent_rate, child_div, actual_rate, div;
  188. parent_rate = clk->parent->get_rate(clk->parent);
  189. control = pm_readl(CKSEL);
  190. if (control & PM_BIT(HSBDIV))
  191. child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
  192. else
  193. child_div = 1;
  194. if (rate > 3 * (parent_rate / 4) || child_div == 1) {
  195. actual_rate = parent_rate;
  196. control &= ~PM_BIT(CPUDIV);
  197. } else {
  198. unsigned int cpusel;
  199. div = (parent_rate + rate / 2) / rate;
  200. if (div > child_div)
  201. div = child_div;
  202. cpusel = (div > 1) ? (fls(div) - 2) : 0;
  203. control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
  204. actual_rate = parent_rate / (1 << (cpusel + 1));
  205. }
  206. pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
  207. clk->name, rate, actual_rate);
  208. if (apply)
  209. pm_writel(CKSEL, control);
  210. return actual_rate;
  211. }
  212. static void hsb_clk_mode(struct clk *clk, int enabled)
  213. {
  214. unsigned long flags;
  215. u32 mask;
  216. spin_lock_irqsave(&pm_lock, flags);
  217. mask = pm_readl(HSB_MASK);
  218. if (enabled)
  219. mask |= 1 << clk->index;
  220. else
  221. mask &= ~(1 << clk->index);
  222. pm_writel(HSB_MASK, mask);
  223. spin_unlock_irqrestore(&pm_lock, flags);
  224. }
  225. static unsigned long hsb_clk_get_rate(struct clk *clk)
  226. {
  227. unsigned long cksel, shift = 0;
  228. cksel = pm_readl(CKSEL);
  229. if (cksel & PM_BIT(HSBDIV))
  230. shift = PM_BFEXT(HSBSEL, cksel) + 1;
  231. return bus_clk_get_rate(clk, shift);
  232. }
  233. static void pba_clk_mode(struct clk *clk, int enabled)
  234. {
  235. unsigned long flags;
  236. u32 mask;
  237. spin_lock_irqsave(&pm_lock, flags);
  238. mask = pm_readl(PBA_MASK);
  239. if (enabled)
  240. mask |= 1 << clk->index;
  241. else
  242. mask &= ~(1 << clk->index);
  243. pm_writel(PBA_MASK, mask);
  244. spin_unlock_irqrestore(&pm_lock, flags);
  245. }
  246. static unsigned long pba_clk_get_rate(struct clk *clk)
  247. {
  248. unsigned long cksel, shift = 0;
  249. cksel = pm_readl(CKSEL);
  250. if (cksel & PM_BIT(PBADIV))
  251. shift = PM_BFEXT(PBASEL, cksel) + 1;
  252. return bus_clk_get_rate(clk, shift);
  253. }
  254. static void pbb_clk_mode(struct clk *clk, int enabled)
  255. {
  256. unsigned long flags;
  257. u32 mask;
  258. spin_lock_irqsave(&pm_lock, flags);
  259. mask = pm_readl(PBB_MASK);
  260. if (enabled)
  261. mask |= 1 << clk->index;
  262. else
  263. mask &= ~(1 << clk->index);
  264. pm_writel(PBB_MASK, mask);
  265. spin_unlock_irqrestore(&pm_lock, flags);
  266. }
  267. static unsigned long pbb_clk_get_rate(struct clk *clk)
  268. {
  269. unsigned long cksel, shift = 0;
  270. cksel = pm_readl(CKSEL);
  271. if (cksel & PM_BIT(PBBDIV))
  272. shift = PM_BFEXT(PBBSEL, cksel) + 1;
  273. return bus_clk_get_rate(clk, shift);
  274. }
  275. static struct clk cpu_clk = {
  276. .name = "cpu",
  277. .get_rate = cpu_clk_get_rate,
  278. .set_rate = cpu_clk_set_rate,
  279. .users = 1,
  280. };
  281. static struct clk hsb_clk = {
  282. .name = "hsb",
  283. .parent = &cpu_clk,
  284. .get_rate = hsb_clk_get_rate,
  285. };
  286. static struct clk pba_clk = {
  287. .name = "pba",
  288. .parent = &hsb_clk,
  289. .mode = hsb_clk_mode,
  290. .get_rate = pba_clk_get_rate,
  291. .index = 1,
  292. };
  293. static struct clk pbb_clk = {
  294. .name = "pbb",
  295. .parent = &hsb_clk,
  296. .mode = hsb_clk_mode,
  297. .get_rate = pbb_clk_get_rate,
  298. .users = 1,
  299. .index = 2,
  300. };
  301. /* --------------------------------------------------------------------
  302. * Generic Clock operations
  303. * -------------------------------------------------------------------- */
  304. static void genclk_mode(struct clk *clk, int enabled)
  305. {
  306. u32 control;
  307. control = pm_readl(GCCTRL(clk->index));
  308. if (enabled)
  309. control |= PM_BIT(CEN);
  310. else
  311. control &= ~PM_BIT(CEN);
  312. pm_writel(GCCTRL(clk->index), control);
  313. }
  314. static unsigned long genclk_get_rate(struct clk *clk)
  315. {
  316. u32 control;
  317. unsigned long div = 1;
  318. control = pm_readl(GCCTRL(clk->index));
  319. if (control & PM_BIT(DIVEN))
  320. div = 2 * (PM_BFEXT(DIV, control) + 1);
  321. return clk->parent->get_rate(clk->parent) / div;
  322. }
  323. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  324. {
  325. u32 control;
  326. unsigned long parent_rate, actual_rate, div;
  327. parent_rate = clk->parent->get_rate(clk->parent);
  328. control = pm_readl(GCCTRL(clk->index));
  329. if (rate > 3 * parent_rate / 4) {
  330. actual_rate = parent_rate;
  331. control &= ~PM_BIT(DIVEN);
  332. } else {
  333. div = (parent_rate + rate) / (2 * rate) - 1;
  334. control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
  335. actual_rate = parent_rate / (2 * (div + 1));
  336. }
  337. dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
  338. clk->name, rate, actual_rate);
  339. if (apply)
  340. pm_writel(GCCTRL(clk->index), control);
  341. return actual_rate;
  342. }
  343. int genclk_set_parent(struct clk *clk, struct clk *parent)
  344. {
  345. u32 control;
  346. dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
  347. clk->name, parent->name, clk->parent->name);
  348. control = pm_readl(GCCTRL(clk->index));
  349. if (parent == &osc1 || parent == &pll1)
  350. control |= PM_BIT(OSCSEL);
  351. else if (parent == &osc0 || parent == &pll0)
  352. control &= ~PM_BIT(OSCSEL);
  353. else
  354. return -EINVAL;
  355. if (parent == &pll0 || parent == &pll1)
  356. control |= PM_BIT(PLLSEL);
  357. else
  358. control &= ~PM_BIT(PLLSEL);
  359. pm_writel(GCCTRL(clk->index), control);
  360. clk->parent = parent;
  361. return 0;
  362. }
  363. static void __init genclk_init_parent(struct clk *clk)
  364. {
  365. u32 control;
  366. struct clk *parent;
  367. BUG_ON(clk->index > 7);
  368. control = pm_readl(GCCTRL(clk->index));
  369. if (control & PM_BIT(OSCSEL))
  370. parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
  371. else
  372. parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
  373. clk->parent = parent;
  374. }
  375. /* --------------------------------------------------------------------
  376. * System peripherals
  377. * -------------------------------------------------------------------- */
  378. static struct resource at32_pm0_resource[] = {
  379. {
  380. .start = 0xfff00000,
  381. .end = 0xfff0007f,
  382. .flags = IORESOURCE_MEM,
  383. },
  384. IRQ(20),
  385. };
  386. static struct resource at32ap700x_rtc0_resource[] = {
  387. {
  388. .start = 0xfff00080,
  389. .end = 0xfff000af,
  390. .flags = IORESOURCE_MEM,
  391. },
  392. IRQ(21),
  393. };
  394. static struct resource at32_wdt0_resource[] = {
  395. {
  396. .start = 0xfff000b0,
  397. .end = 0xfff000bf,
  398. .flags = IORESOURCE_MEM,
  399. },
  400. };
  401. static struct resource at32_eic0_resource[] = {
  402. {
  403. .start = 0xfff00100,
  404. .end = 0xfff0013f,
  405. .flags = IORESOURCE_MEM,
  406. },
  407. IRQ(19),
  408. };
  409. DEFINE_DEV(at32_pm, 0);
  410. DEFINE_DEV(at32ap700x_rtc, 0);
  411. DEFINE_DEV(at32_wdt, 0);
  412. DEFINE_DEV(at32_eic, 0);
  413. /*
  414. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  415. * is always running.
  416. */
  417. static struct clk at32_pm_pclk = {
  418. .name = "pclk",
  419. .dev = &at32_pm0_device.dev,
  420. .parent = &pbb_clk,
  421. .mode = pbb_clk_mode,
  422. .get_rate = pbb_clk_get_rate,
  423. .users = 1,
  424. .index = 0,
  425. };
  426. static struct resource intc0_resource[] = {
  427. PBMEM(0xfff00400),
  428. };
  429. struct platform_device at32_intc0_device = {
  430. .name = "intc",
  431. .id = 0,
  432. .resource = intc0_resource,
  433. .num_resources = ARRAY_SIZE(intc0_resource),
  434. };
  435. DEV_CLK(pclk, at32_intc0, pbb, 1);
  436. static struct clk ebi_clk = {
  437. .name = "ebi",
  438. .parent = &hsb_clk,
  439. .mode = hsb_clk_mode,
  440. .get_rate = hsb_clk_get_rate,
  441. .users = 1,
  442. };
  443. static struct clk hramc_clk = {
  444. .name = "hramc",
  445. .parent = &hsb_clk,
  446. .mode = hsb_clk_mode,
  447. .get_rate = hsb_clk_get_rate,
  448. .users = 1,
  449. .index = 3,
  450. };
  451. static struct resource smc0_resource[] = {
  452. PBMEM(0xfff03400),
  453. };
  454. DEFINE_DEV(smc, 0);
  455. DEV_CLK(pclk, smc0, pbb, 13);
  456. DEV_CLK(mck, smc0, hsb, 0);
  457. static struct platform_device pdc_device = {
  458. .name = "pdc",
  459. .id = 0,
  460. };
  461. DEV_CLK(hclk, pdc, hsb, 4);
  462. DEV_CLK(pclk, pdc, pba, 16);
  463. static struct clk pico_clk = {
  464. .name = "pico",
  465. .parent = &cpu_clk,
  466. .mode = cpu_clk_mode,
  467. .get_rate = cpu_clk_get_rate,
  468. .users = 1,
  469. };
  470. /* --------------------------------------------------------------------
  471. * HMATRIX
  472. * -------------------------------------------------------------------- */
  473. static struct clk hmatrix_clk = {
  474. .name = "hmatrix_clk",
  475. .parent = &pbb_clk,
  476. .mode = pbb_clk_mode,
  477. .get_rate = pbb_clk_get_rate,
  478. .index = 2,
  479. .users = 1,
  480. };
  481. #define HMATRIX_BASE ((void __iomem *)0xfff00800)
  482. #define hmatrix_readl(reg) \
  483. __raw_readl((HMATRIX_BASE) + HMATRIX_##reg)
  484. #define hmatrix_writel(reg,value) \
  485. __raw_writel((value), (HMATRIX_BASE) + HMATRIX_##reg)
  486. /*
  487. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  488. * External Bus Interface (EBI). This can be used to enable special
  489. * features like CompactFlash support, NAND Flash support, etc. on
  490. * certain chipselects.
  491. */
  492. static inline void set_ebi_sfr_bits(u32 mask)
  493. {
  494. u32 sfr;
  495. clk_enable(&hmatrix_clk);
  496. sfr = hmatrix_readl(SFR4);
  497. sfr |= mask;
  498. hmatrix_writel(SFR4, sfr);
  499. clk_disable(&hmatrix_clk);
  500. }
  501. /* --------------------------------------------------------------------
  502. * System Timer/Counter (TC)
  503. * -------------------------------------------------------------------- */
  504. static struct resource at32_systc0_resource[] = {
  505. PBMEM(0xfff00c00),
  506. IRQ(22),
  507. };
  508. struct platform_device at32_systc0_device = {
  509. .name = "systc",
  510. .id = 0,
  511. .resource = at32_systc0_resource,
  512. .num_resources = ARRAY_SIZE(at32_systc0_resource),
  513. };
  514. DEV_CLK(pclk, at32_systc0, pbb, 3);
  515. /* --------------------------------------------------------------------
  516. * PIO
  517. * -------------------------------------------------------------------- */
  518. static struct resource pio0_resource[] = {
  519. PBMEM(0xffe02800),
  520. IRQ(13),
  521. };
  522. DEFINE_DEV(pio, 0);
  523. DEV_CLK(mck, pio0, pba, 10);
  524. static struct resource pio1_resource[] = {
  525. PBMEM(0xffe02c00),
  526. IRQ(14),
  527. };
  528. DEFINE_DEV(pio, 1);
  529. DEV_CLK(mck, pio1, pba, 11);
  530. static struct resource pio2_resource[] = {
  531. PBMEM(0xffe03000),
  532. IRQ(15),
  533. };
  534. DEFINE_DEV(pio, 2);
  535. DEV_CLK(mck, pio2, pba, 12);
  536. static struct resource pio3_resource[] = {
  537. PBMEM(0xffe03400),
  538. IRQ(16),
  539. };
  540. DEFINE_DEV(pio, 3);
  541. DEV_CLK(mck, pio3, pba, 13);
  542. static struct resource pio4_resource[] = {
  543. PBMEM(0xffe03800),
  544. IRQ(17),
  545. };
  546. DEFINE_DEV(pio, 4);
  547. DEV_CLK(mck, pio4, pba, 14);
  548. void __init at32_add_system_devices(void)
  549. {
  550. platform_device_register(&at32_pm0_device);
  551. platform_device_register(&at32_intc0_device);
  552. platform_device_register(&at32ap700x_rtc0_device);
  553. platform_device_register(&at32_wdt0_device);
  554. platform_device_register(&at32_eic0_device);
  555. platform_device_register(&smc0_device);
  556. platform_device_register(&pdc_device);
  557. platform_device_register(&at32_systc0_device);
  558. platform_device_register(&pio0_device);
  559. platform_device_register(&pio1_device);
  560. platform_device_register(&pio2_device);
  561. platform_device_register(&pio3_device);
  562. platform_device_register(&pio4_device);
  563. }
  564. /* --------------------------------------------------------------------
  565. * USART
  566. * -------------------------------------------------------------------- */
  567. static struct atmel_uart_data atmel_usart0_data = {
  568. .use_dma_tx = 1,
  569. .use_dma_rx = 1,
  570. };
  571. static struct resource atmel_usart0_resource[] = {
  572. PBMEM(0xffe00c00),
  573. IRQ(6),
  574. };
  575. DEFINE_DEV_DATA(atmel_usart, 0);
  576. DEV_CLK(usart, atmel_usart0, pba, 4);
  577. static struct atmel_uart_data atmel_usart1_data = {
  578. .use_dma_tx = 1,
  579. .use_dma_rx = 1,
  580. };
  581. static struct resource atmel_usart1_resource[] = {
  582. PBMEM(0xffe01000),
  583. IRQ(7),
  584. };
  585. DEFINE_DEV_DATA(atmel_usart, 1);
  586. DEV_CLK(usart, atmel_usart1, pba, 4);
  587. static struct atmel_uart_data atmel_usart2_data = {
  588. .use_dma_tx = 1,
  589. .use_dma_rx = 1,
  590. };
  591. static struct resource atmel_usart2_resource[] = {
  592. PBMEM(0xffe01400),
  593. IRQ(8),
  594. };
  595. DEFINE_DEV_DATA(atmel_usart, 2);
  596. DEV_CLK(usart, atmel_usart2, pba, 5);
  597. static struct atmel_uart_data atmel_usart3_data = {
  598. .use_dma_tx = 1,
  599. .use_dma_rx = 1,
  600. };
  601. static struct resource atmel_usart3_resource[] = {
  602. PBMEM(0xffe01800),
  603. IRQ(9),
  604. };
  605. DEFINE_DEV_DATA(atmel_usart, 3);
  606. DEV_CLK(usart, atmel_usart3, pba, 6);
  607. static inline void configure_usart0_pins(void)
  608. {
  609. select_peripheral(PA(8), PERIPH_B, 0); /* RXD */
  610. select_peripheral(PA(9), PERIPH_B, 0); /* TXD */
  611. }
  612. static inline void configure_usart1_pins(void)
  613. {
  614. select_peripheral(PA(17), PERIPH_A, 0); /* RXD */
  615. select_peripheral(PA(18), PERIPH_A, 0); /* TXD */
  616. }
  617. static inline void configure_usart2_pins(void)
  618. {
  619. select_peripheral(PB(26), PERIPH_B, 0); /* RXD */
  620. select_peripheral(PB(27), PERIPH_B, 0); /* TXD */
  621. }
  622. static inline void configure_usart3_pins(void)
  623. {
  624. select_peripheral(PB(18), PERIPH_B, 0); /* RXD */
  625. select_peripheral(PB(17), PERIPH_B, 0); /* TXD */
  626. }
  627. static struct platform_device *__initdata at32_usarts[4];
  628. void __init at32_map_usart(unsigned int hw_id, unsigned int line)
  629. {
  630. struct platform_device *pdev;
  631. switch (hw_id) {
  632. case 0:
  633. pdev = &atmel_usart0_device;
  634. configure_usart0_pins();
  635. break;
  636. case 1:
  637. pdev = &atmel_usart1_device;
  638. configure_usart1_pins();
  639. break;
  640. case 2:
  641. pdev = &atmel_usart2_device;
  642. configure_usart2_pins();
  643. break;
  644. case 3:
  645. pdev = &atmel_usart3_device;
  646. configure_usart3_pins();
  647. break;
  648. default:
  649. return;
  650. }
  651. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  652. /* Addresses in the P4 segment are permanently mapped 1:1 */
  653. struct atmel_uart_data *data = pdev->dev.platform_data;
  654. data->regs = (void __iomem *)pdev->resource[0].start;
  655. }
  656. pdev->id = line;
  657. at32_usarts[line] = pdev;
  658. }
  659. struct platform_device *__init at32_add_device_usart(unsigned int id)
  660. {
  661. platform_device_register(at32_usarts[id]);
  662. return at32_usarts[id];
  663. }
  664. struct platform_device *atmel_default_console_device;
  665. void __init at32_setup_serial_console(unsigned int usart_id)
  666. {
  667. atmel_default_console_device = at32_usarts[usart_id];
  668. }
  669. /* --------------------------------------------------------------------
  670. * Ethernet
  671. * -------------------------------------------------------------------- */
  672. static struct eth_platform_data macb0_data;
  673. static struct resource macb0_resource[] = {
  674. PBMEM(0xfff01800),
  675. IRQ(25),
  676. };
  677. DEFINE_DEV_DATA(macb, 0);
  678. DEV_CLK(hclk, macb0, hsb, 8);
  679. DEV_CLK(pclk, macb0, pbb, 6);
  680. static struct eth_platform_data macb1_data;
  681. static struct resource macb1_resource[] = {
  682. PBMEM(0xfff01c00),
  683. IRQ(26),
  684. };
  685. DEFINE_DEV_DATA(macb, 1);
  686. DEV_CLK(hclk, macb1, hsb, 9);
  687. DEV_CLK(pclk, macb1, pbb, 7);
  688. struct platform_device *__init
  689. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  690. {
  691. struct platform_device *pdev;
  692. switch (id) {
  693. case 0:
  694. pdev = &macb0_device;
  695. select_peripheral(PC(3), PERIPH_A, 0); /* TXD0 */
  696. select_peripheral(PC(4), PERIPH_A, 0); /* TXD1 */
  697. select_peripheral(PC(7), PERIPH_A, 0); /* TXEN */
  698. select_peripheral(PC(8), PERIPH_A, 0); /* TXCK */
  699. select_peripheral(PC(9), PERIPH_A, 0); /* RXD0 */
  700. select_peripheral(PC(10), PERIPH_A, 0); /* RXD1 */
  701. select_peripheral(PC(13), PERIPH_A, 0); /* RXER */
  702. select_peripheral(PC(15), PERIPH_A, 0); /* RXDV */
  703. select_peripheral(PC(16), PERIPH_A, 0); /* MDC */
  704. select_peripheral(PC(17), PERIPH_A, 0); /* MDIO */
  705. if (!data->is_rmii) {
  706. select_peripheral(PC(0), PERIPH_A, 0); /* COL */
  707. select_peripheral(PC(1), PERIPH_A, 0); /* CRS */
  708. select_peripheral(PC(2), PERIPH_A, 0); /* TXER */
  709. select_peripheral(PC(5), PERIPH_A, 0); /* TXD2 */
  710. select_peripheral(PC(6), PERIPH_A, 0); /* TXD3 */
  711. select_peripheral(PC(11), PERIPH_A, 0); /* RXD2 */
  712. select_peripheral(PC(12), PERIPH_A, 0); /* RXD3 */
  713. select_peripheral(PC(14), PERIPH_A, 0); /* RXCK */
  714. select_peripheral(PC(18), PERIPH_A, 0); /* SPD */
  715. }
  716. break;
  717. case 1:
  718. pdev = &macb1_device;
  719. select_peripheral(PD(13), PERIPH_B, 0); /* TXD0 */
  720. select_peripheral(PD(14), PERIPH_B, 0); /* TXD1 */
  721. select_peripheral(PD(11), PERIPH_B, 0); /* TXEN */
  722. select_peripheral(PD(12), PERIPH_B, 0); /* TXCK */
  723. select_peripheral(PD(10), PERIPH_B, 0); /* RXD0 */
  724. select_peripheral(PD(6), PERIPH_B, 0); /* RXD1 */
  725. select_peripheral(PD(5), PERIPH_B, 0); /* RXER */
  726. select_peripheral(PD(4), PERIPH_B, 0); /* RXDV */
  727. select_peripheral(PD(3), PERIPH_B, 0); /* MDC */
  728. select_peripheral(PD(2), PERIPH_B, 0); /* MDIO */
  729. if (!data->is_rmii) {
  730. select_peripheral(PC(19), PERIPH_B, 0); /* COL */
  731. select_peripheral(PC(23), PERIPH_B, 0); /* CRS */
  732. select_peripheral(PC(26), PERIPH_B, 0); /* TXER */
  733. select_peripheral(PC(27), PERIPH_B, 0); /* TXD2 */
  734. select_peripheral(PC(28), PERIPH_B, 0); /* TXD3 */
  735. select_peripheral(PC(29), PERIPH_B, 0); /* RXD2 */
  736. select_peripheral(PC(30), PERIPH_B, 0); /* RXD3 */
  737. select_peripheral(PC(24), PERIPH_B, 0); /* RXCK */
  738. select_peripheral(PD(15), PERIPH_B, 0); /* SPD */
  739. }
  740. break;
  741. default:
  742. return NULL;
  743. }
  744. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  745. platform_device_register(pdev);
  746. return pdev;
  747. }
  748. /* --------------------------------------------------------------------
  749. * SPI
  750. * -------------------------------------------------------------------- */
  751. static struct resource atmel_spi0_resource[] = {
  752. PBMEM(0xffe00000),
  753. IRQ(3),
  754. };
  755. DEFINE_DEV(atmel_spi, 0);
  756. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  757. static struct resource atmel_spi1_resource[] = {
  758. PBMEM(0xffe00400),
  759. IRQ(4),
  760. };
  761. DEFINE_DEV(atmel_spi, 1);
  762. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  763. static void __init
  764. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
  765. unsigned int n, const u8 *pins)
  766. {
  767. unsigned int pin, mode;
  768. for (; n; n--, b++) {
  769. b->bus_num = bus_num;
  770. if (b->chip_select >= 4)
  771. continue;
  772. pin = (unsigned)b->controller_data;
  773. if (!pin) {
  774. pin = pins[b->chip_select];
  775. b->controller_data = (void *)pin;
  776. }
  777. mode = AT32_GPIOF_OUTPUT;
  778. if (!(b->mode & SPI_CS_HIGH))
  779. mode |= AT32_GPIOF_HIGH;
  780. at32_select_gpio(pin, mode);
  781. }
  782. }
  783. struct platform_device *__init
  784. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  785. {
  786. /*
  787. * Manage the chipselects as GPIOs, normally using the same pins
  788. * the SPI controller expects; but boards can use other pins.
  789. */
  790. static u8 __initdata spi0_pins[] =
  791. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  792. GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
  793. static u8 __initdata spi1_pins[] =
  794. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  795. GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
  796. struct platform_device *pdev;
  797. switch (id) {
  798. case 0:
  799. pdev = &atmel_spi0_device;
  800. select_peripheral(PA(0), PERIPH_A, 0); /* MISO */
  801. select_peripheral(PA(1), PERIPH_A, 0); /* MOSI */
  802. select_peripheral(PA(2), PERIPH_A, 0); /* SCK */
  803. at32_spi_setup_slaves(0, b, n, spi0_pins);
  804. break;
  805. case 1:
  806. pdev = &atmel_spi1_device;
  807. select_peripheral(PB(0), PERIPH_B, 0); /* MISO */
  808. select_peripheral(PB(1), PERIPH_B, 0); /* MOSI */
  809. select_peripheral(PB(5), PERIPH_B, 0); /* SCK */
  810. at32_spi_setup_slaves(1, b, n, spi1_pins);
  811. break;
  812. default:
  813. return NULL;
  814. }
  815. spi_register_board_info(b, n);
  816. platform_device_register(pdev);
  817. return pdev;
  818. }
  819. /* --------------------------------------------------------------------
  820. * LCDC
  821. * -------------------------------------------------------------------- */
  822. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  823. static struct resource atmel_lcdfb0_resource[] = {
  824. {
  825. .start = 0xff000000,
  826. .end = 0xff000fff,
  827. .flags = IORESOURCE_MEM,
  828. },
  829. IRQ(1),
  830. {
  831. /* Placeholder for pre-allocated fb memory */
  832. .start = 0x00000000,
  833. .end = 0x00000000,
  834. .flags = 0,
  835. },
  836. };
  837. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  838. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  839. static struct clk atmel_lcdfb0_pixclk = {
  840. .name = "lcdc_clk",
  841. .dev = &atmel_lcdfb0_device.dev,
  842. .mode = genclk_mode,
  843. .get_rate = genclk_get_rate,
  844. .set_rate = genclk_set_rate,
  845. .set_parent = genclk_set_parent,
  846. .index = 7,
  847. };
  848. struct platform_device *__init
  849. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  850. unsigned long fbmem_start, unsigned long fbmem_len)
  851. {
  852. struct platform_device *pdev;
  853. struct atmel_lcdfb_info *info;
  854. struct fb_monspecs *monspecs;
  855. struct fb_videomode *modedb;
  856. unsigned int modedb_size;
  857. /*
  858. * Do a deep copy of the fb data, monspecs and modedb. Make
  859. * sure all allocations are done before setting up the
  860. * portmux.
  861. */
  862. monspecs = kmemdup(data->default_monspecs,
  863. sizeof(struct fb_monspecs), GFP_KERNEL);
  864. if (!monspecs)
  865. return NULL;
  866. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  867. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  868. if (!modedb)
  869. goto err_dup_modedb;
  870. monspecs->modedb = modedb;
  871. switch (id) {
  872. case 0:
  873. pdev = &atmel_lcdfb0_device;
  874. select_peripheral(PC(19), PERIPH_A, 0); /* CC */
  875. select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
  876. select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
  877. select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
  878. select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
  879. select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
  880. select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
  881. select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
  882. select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
  883. select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
  884. select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
  885. select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
  886. select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
  887. select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
  888. select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
  889. select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
  890. select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
  891. select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
  892. select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
  893. select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
  894. select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
  895. select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
  896. select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
  897. select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
  898. select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
  899. select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
  900. select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
  901. select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
  902. select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
  903. select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
  904. select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
  905. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  906. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  907. break;
  908. default:
  909. goto err_invalid_id;
  910. }
  911. if (fbmem_len) {
  912. pdev->resource[2].start = fbmem_start;
  913. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  914. pdev->resource[2].flags = IORESOURCE_MEM;
  915. }
  916. info = pdev->dev.platform_data;
  917. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  918. info->default_monspecs = monspecs;
  919. platform_device_register(pdev);
  920. return pdev;
  921. err_invalid_id:
  922. kfree(modedb);
  923. err_dup_modedb:
  924. kfree(monspecs);
  925. return NULL;
  926. }
  927. /* --------------------------------------------------------------------
  928. * SSC
  929. * -------------------------------------------------------------------- */
  930. static struct resource ssc0_resource[] = {
  931. PBMEM(0xffe01c00),
  932. IRQ(10),
  933. };
  934. DEFINE_DEV(ssc, 0);
  935. DEV_CLK(pclk, ssc0, pba, 7);
  936. static struct resource ssc1_resource[] = {
  937. PBMEM(0xffe02000),
  938. IRQ(11),
  939. };
  940. DEFINE_DEV(ssc, 1);
  941. DEV_CLK(pclk, ssc1, pba, 8);
  942. static struct resource ssc2_resource[] = {
  943. PBMEM(0xffe02400),
  944. IRQ(12),
  945. };
  946. DEFINE_DEV(ssc, 2);
  947. DEV_CLK(pclk, ssc2, pba, 9);
  948. struct platform_device *__init
  949. at32_add_device_ssc(unsigned int id, unsigned int flags)
  950. {
  951. struct platform_device *pdev;
  952. switch (id) {
  953. case 0:
  954. pdev = &ssc0_device;
  955. if (flags & ATMEL_SSC_RF)
  956. select_peripheral(PA(21), PERIPH_A, 0); /* RF */
  957. if (flags & ATMEL_SSC_RK)
  958. select_peripheral(PA(22), PERIPH_A, 0); /* RK */
  959. if (flags & ATMEL_SSC_TK)
  960. select_peripheral(PA(23), PERIPH_A, 0); /* TK */
  961. if (flags & ATMEL_SSC_TF)
  962. select_peripheral(PA(24), PERIPH_A, 0); /* TF */
  963. if (flags & ATMEL_SSC_TD)
  964. select_peripheral(PA(25), PERIPH_A, 0); /* TD */
  965. if (flags & ATMEL_SSC_RD)
  966. select_peripheral(PA(26), PERIPH_A, 0); /* RD */
  967. break;
  968. case 1:
  969. pdev = &ssc1_device;
  970. if (flags & ATMEL_SSC_RF)
  971. select_peripheral(PA(0), PERIPH_B, 0); /* RF */
  972. if (flags & ATMEL_SSC_RK)
  973. select_peripheral(PA(1), PERIPH_B, 0); /* RK */
  974. if (flags & ATMEL_SSC_TK)
  975. select_peripheral(PA(2), PERIPH_B, 0); /* TK */
  976. if (flags & ATMEL_SSC_TF)
  977. select_peripheral(PA(3), PERIPH_B, 0); /* TF */
  978. if (flags & ATMEL_SSC_TD)
  979. select_peripheral(PA(4), PERIPH_B, 0); /* TD */
  980. if (flags & ATMEL_SSC_RD)
  981. select_peripheral(PA(5), PERIPH_B, 0); /* RD */
  982. break;
  983. case 2:
  984. pdev = &ssc2_device;
  985. if (flags & ATMEL_SSC_TD)
  986. select_peripheral(PB(13), PERIPH_A, 0); /* TD */
  987. if (flags & ATMEL_SSC_RD)
  988. select_peripheral(PB(14), PERIPH_A, 0); /* RD */
  989. if (flags & ATMEL_SSC_TK)
  990. select_peripheral(PB(15), PERIPH_A, 0); /* TK */
  991. if (flags & ATMEL_SSC_TF)
  992. select_peripheral(PB(16), PERIPH_A, 0); /* TF */
  993. if (flags & ATMEL_SSC_RF)
  994. select_peripheral(PB(17), PERIPH_A, 0); /* RF */
  995. if (flags & ATMEL_SSC_RK)
  996. select_peripheral(PB(18), PERIPH_A, 0); /* RK */
  997. break;
  998. default:
  999. return NULL;
  1000. }
  1001. platform_device_register(pdev);
  1002. return pdev;
  1003. }
  1004. /* --------------------------------------------------------------------
  1005. * USB Device Controller
  1006. * -------------------------------------------------------------------- */
  1007. static struct resource usba0_resource[] __initdata = {
  1008. {
  1009. .start = 0xff300000,
  1010. .end = 0xff3fffff,
  1011. .flags = IORESOURCE_MEM,
  1012. }, {
  1013. .start = 0xfff03000,
  1014. .end = 0xfff033ff,
  1015. .flags = IORESOURCE_MEM,
  1016. },
  1017. IRQ(31),
  1018. };
  1019. static struct clk usba0_pclk = {
  1020. .name = "pclk",
  1021. .parent = &pbb_clk,
  1022. .mode = pbb_clk_mode,
  1023. .get_rate = pbb_clk_get_rate,
  1024. .index = 12,
  1025. };
  1026. static struct clk usba0_hclk = {
  1027. .name = "hclk",
  1028. .parent = &hsb_clk,
  1029. .mode = hsb_clk_mode,
  1030. .get_rate = hsb_clk_get_rate,
  1031. .index = 6,
  1032. };
  1033. struct platform_device *__init
  1034. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1035. {
  1036. struct platform_device *pdev;
  1037. if (id != 0)
  1038. return NULL;
  1039. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1040. if (!pdev)
  1041. return NULL;
  1042. if (platform_device_add_resources(pdev, usba0_resource,
  1043. ARRAY_SIZE(usba0_resource)))
  1044. goto out_free_pdev;
  1045. if (data) {
  1046. if (platform_device_add_data(pdev, data, sizeof(*data)))
  1047. goto out_free_pdev;
  1048. if (data->vbus_pin != GPIO_PIN_NONE)
  1049. at32_select_gpio(data->vbus_pin, 0);
  1050. }
  1051. usba0_pclk.dev = &pdev->dev;
  1052. usba0_hclk.dev = &pdev->dev;
  1053. platform_device_add(pdev);
  1054. return pdev;
  1055. out_free_pdev:
  1056. platform_device_put(pdev);
  1057. return NULL;
  1058. }
  1059. /* --------------------------------------------------------------------
  1060. * GCLK
  1061. * -------------------------------------------------------------------- */
  1062. static struct clk gclk0 = {
  1063. .name = "gclk0",
  1064. .mode = genclk_mode,
  1065. .get_rate = genclk_get_rate,
  1066. .set_rate = genclk_set_rate,
  1067. .set_parent = genclk_set_parent,
  1068. .index = 0,
  1069. };
  1070. static struct clk gclk1 = {
  1071. .name = "gclk1",
  1072. .mode = genclk_mode,
  1073. .get_rate = genclk_get_rate,
  1074. .set_rate = genclk_set_rate,
  1075. .set_parent = genclk_set_parent,
  1076. .index = 1,
  1077. };
  1078. static struct clk gclk2 = {
  1079. .name = "gclk2",
  1080. .mode = genclk_mode,
  1081. .get_rate = genclk_get_rate,
  1082. .set_rate = genclk_set_rate,
  1083. .set_parent = genclk_set_parent,
  1084. .index = 2,
  1085. };
  1086. static struct clk gclk3 = {
  1087. .name = "gclk3",
  1088. .mode = genclk_mode,
  1089. .get_rate = genclk_get_rate,
  1090. .set_rate = genclk_set_rate,
  1091. .set_parent = genclk_set_parent,
  1092. .index = 3,
  1093. };
  1094. static struct clk gclk4 = {
  1095. .name = "gclk4",
  1096. .mode = genclk_mode,
  1097. .get_rate = genclk_get_rate,
  1098. .set_rate = genclk_set_rate,
  1099. .set_parent = genclk_set_parent,
  1100. .index = 4,
  1101. };
  1102. struct clk *at32_clock_list[] = {
  1103. &osc32k,
  1104. &osc0,
  1105. &osc1,
  1106. &pll0,
  1107. &pll1,
  1108. &cpu_clk,
  1109. &hsb_clk,
  1110. &pba_clk,
  1111. &pbb_clk,
  1112. &at32_pm_pclk,
  1113. &at32_intc0_pclk,
  1114. &hmatrix_clk,
  1115. &ebi_clk,
  1116. &hramc_clk,
  1117. &smc0_pclk,
  1118. &smc0_mck,
  1119. &pdc_hclk,
  1120. &pdc_pclk,
  1121. &pico_clk,
  1122. &pio0_mck,
  1123. &pio1_mck,
  1124. &pio2_mck,
  1125. &pio3_mck,
  1126. &pio4_mck,
  1127. &at32_systc0_pclk,
  1128. &atmel_usart0_usart,
  1129. &atmel_usart1_usart,
  1130. &atmel_usart2_usart,
  1131. &atmel_usart3_usart,
  1132. &macb0_hclk,
  1133. &macb0_pclk,
  1134. &macb1_hclk,
  1135. &macb1_pclk,
  1136. &atmel_spi0_spi_clk,
  1137. &atmel_spi1_spi_clk,
  1138. &atmel_lcdfb0_hck1,
  1139. &atmel_lcdfb0_pixclk,
  1140. &ssc0_pclk,
  1141. &ssc1_pclk,
  1142. &ssc2_pclk,
  1143. &usba0_hclk,
  1144. &usba0_pclk,
  1145. &gclk0,
  1146. &gclk1,
  1147. &gclk2,
  1148. &gclk3,
  1149. &gclk4,
  1150. };
  1151. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  1152. void __init at32_portmux_init(void)
  1153. {
  1154. at32_init_pio(&pio0_device);
  1155. at32_init_pio(&pio1_device);
  1156. at32_init_pio(&pio2_device);
  1157. at32_init_pio(&pio3_device);
  1158. at32_init_pio(&pio4_device);
  1159. }
  1160. void __init at32_clock_init(void)
  1161. {
  1162. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1163. int i;
  1164. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1165. main_clock = &pll0;
  1166. cpu_clk.parent = &pll0;
  1167. } else {
  1168. main_clock = &osc0;
  1169. cpu_clk.parent = &osc0;
  1170. }
  1171. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1172. pll0.parent = &osc1;
  1173. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1174. pll1.parent = &osc1;
  1175. genclk_init_parent(&gclk0);
  1176. genclk_init_parent(&gclk1);
  1177. genclk_init_parent(&gclk2);
  1178. genclk_init_parent(&gclk3);
  1179. genclk_init_parent(&gclk4);
  1180. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1181. /*
  1182. * Turn on all clocks that have at least one user already, and
  1183. * turn off everything else. We only do this for module
  1184. * clocks, and even though it isn't particularly pretty to
  1185. * check the address of the mode function, it should do the
  1186. * trick...
  1187. */
  1188. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  1189. struct clk *clk = at32_clock_list[i];
  1190. if (clk->users == 0)
  1191. continue;
  1192. if (clk->mode == &cpu_clk_mode)
  1193. cpu_mask |= 1 << clk->index;
  1194. else if (clk->mode == &hsb_clk_mode)
  1195. hsb_mask |= 1 << clk->index;
  1196. else if (clk->mode == &pba_clk_mode)
  1197. pba_mask |= 1 << clk->index;
  1198. else if (clk->mode == &pbb_clk_mode)
  1199. pbb_mask |= 1 << clk->index;
  1200. }
  1201. pm_writel(CPU_MASK, cpu_mask);
  1202. pm_writel(HSB_MASK, hsb_mask);
  1203. pm_writel(PBA_MASK, pba_mask);
  1204. pm_writel(PBB_MASK, pbb_mask);
  1205. }