at32ap700x.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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/delay.h>
  10. #include <linux/fb.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/usb/atmel_usba_udc.h>
  16. #include <asm/io.h>
  17. #include <asm/irq.h>
  18. #include <asm/arch/at32ap700x.h>
  19. #include <asm/arch/board.h>
  20. #include <asm/arch/portmux.h>
  21. #include <video/atmel_lcdc.h>
  22. #include "clock.h"
  23. #include "hmatrix.h"
  24. #include "pio.h"
  25. #include "pm.h"
  26. #define PBMEM(base) \
  27. { \
  28. .start = base, \
  29. .end = base + 0x3ff, \
  30. .flags = IORESOURCE_MEM, \
  31. }
  32. #define IRQ(num) \
  33. { \
  34. .start = num, \
  35. .end = num, \
  36. .flags = IORESOURCE_IRQ, \
  37. }
  38. #define NAMED_IRQ(num, _name) \
  39. { \
  40. .start = num, \
  41. .end = num, \
  42. .name = _name, \
  43. .flags = IORESOURCE_IRQ, \
  44. }
  45. /* REVISIT these assume *every* device supports DMA, but several
  46. * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more.
  47. */
  48. #define DEFINE_DEV(_name, _id) \
  49. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  50. static struct platform_device _name##_id##_device = { \
  51. .name = #_name, \
  52. .id = _id, \
  53. .dev = { \
  54. .dma_mask = &_name##_id##_dma_mask, \
  55. .coherent_dma_mask = DMA_32BIT_MASK, \
  56. }, \
  57. .resource = _name##_id##_resource, \
  58. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  59. }
  60. #define DEFINE_DEV_DATA(_name, _id) \
  61. static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \
  62. static struct platform_device _name##_id##_device = { \
  63. .name = #_name, \
  64. .id = _id, \
  65. .dev = { \
  66. .dma_mask = &_name##_id##_dma_mask, \
  67. .platform_data = &_name##_id##_data, \
  68. .coherent_dma_mask = DMA_32BIT_MASK, \
  69. }, \
  70. .resource = _name##_id##_resource, \
  71. .num_resources = ARRAY_SIZE(_name##_id##_resource), \
  72. }
  73. #define select_peripheral(pin, periph, flags) \
  74. at32_select_periph(GPIO_PIN_##pin, GPIO_##periph, flags)
  75. #define DEV_CLK(_name, devname, bus, _index) \
  76. static struct clk devname##_##_name = { \
  77. .name = #_name, \
  78. .dev = &devname##_device.dev, \
  79. .parent = &bus##_clk, \
  80. .mode = bus##_clk_mode, \
  81. .get_rate = bus##_clk_get_rate, \
  82. .index = _index, \
  83. }
  84. static DEFINE_SPINLOCK(pm_lock);
  85. static struct clk osc0;
  86. static struct clk osc1;
  87. static unsigned long osc_get_rate(struct clk *clk)
  88. {
  89. return at32_board_osc_rates[clk->index];
  90. }
  91. static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
  92. {
  93. unsigned long div, mul, rate;
  94. div = PM_BFEXT(PLLDIV, control) + 1;
  95. mul = PM_BFEXT(PLLMUL, control) + 1;
  96. rate = clk->parent->get_rate(clk->parent);
  97. rate = (rate + div / 2) / div;
  98. rate *= mul;
  99. return rate;
  100. }
  101. static long pll_set_rate(struct clk *clk, unsigned long rate,
  102. u32 *pll_ctrl)
  103. {
  104. unsigned long mul;
  105. unsigned long mul_best_fit = 0;
  106. unsigned long div;
  107. unsigned long div_min;
  108. unsigned long div_max;
  109. unsigned long div_best_fit = 0;
  110. unsigned long base;
  111. unsigned long pll_in;
  112. unsigned long actual = 0;
  113. unsigned long rate_error;
  114. unsigned long rate_error_prev = ~0UL;
  115. u32 ctrl;
  116. /* Rate must be between 80 MHz and 200 Mhz. */
  117. if (rate < 80000000UL || rate > 200000000UL)
  118. return -EINVAL;
  119. ctrl = PM_BF(PLLOPT, 4);
  120. base = clk->parent->get_rate(clk->parent);
  121. /* PLL input frequency must be between 6 MHz and 32 MHz. */
  122. div_min = DIV_ROUND_UP(base, 32000000UL);
  123. div_max = base / 6000000UL;
  124. if (div_max < div_min)
  125. return -EINVAL;
  126. for (div = div_min; div <= div_max; div++) {
  127. pll_in = (base + div / 2) / div;
  128. mul = (rate + pll_in / 2) / pll_in;
  129. if (mul == 0)
  130. continue;
  131. actual = pll_in * mul;
  132. rate_error = abs(actual - rate);
  133. if (rate_error < rate_error_prev) {
  134. mul_best_fit = mul;
  135. div_best_fit = div;
  136. rate_error_prev = rate_error;
  137. }
  138. if (rate_error == 0)
  139. break;
  140. }
  141. if (div_best_fit == 0)
  142. return -EINVAL;
  143. ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
  144. ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
  145. ctrl |= PM_BF(PLLCOUNT, 16);
  146. if (clk->parent == &osc1)
  147. ctrl |= PM_BIT(PLLOSC);
  148. *pll_ctrl = ctrl;
  149. return actual;
  150. }
  151. static unsigned long pll0_get_rate(struct clk *clk)
  152. {
  153. u32 control;
  154. control = pm_readl(PLL0);
  155. return pll_get_rate(clk, control);
  156. }
  157. static void pll1_mode(struct clk *clk, int enabled)
  158. {
  159. unsigned long timeout;
  160. u32 status;
  161. u32 ctrl;
  162. ctrl = pm_readl(PLL1);
  163. if (enabled) {
  164. if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
  165. pr_debug("clk %s: failed to enable, rate not set\n",
  166. clk->name);
  167. return;
  168. }
  169. ctrl |= PM_BIT(PLLEN);
  170. pm_writel(PLL1, ctrl);
  171. /* Wait for PLL lock. */
  172. for (timeout = 10000; timeout; timeout--) {
  173. status = pm_readl(ISR);
  174. if (status & PM_BIT(LOCK1))
  175. break;
  176. udelay(10);
  177. }
  178. if (!(status & PM_BIT(LOCK1)))
  179. printk(KERN_ERR "clk %s: timeout waiting for lock\n",
  180. clk->name);
  181. } else {
  182. ctrl &= ~PM_BIT(PLLEN);
  183. pm_writel(PLL1, ctrl);
  184. }
  185. }
  186. static unsigned long pll1_get_rate(struct clk *clk)
  187. {
  188. u32 control;
  189. control = pm_readl(PLL1);
  190. return pll_get_rate(clk, control);
  191. }
  192. static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
  193. {
  194. u32 ctrl = 0;
  195. unsigned long actual_rate;
  196. actual_rate = pll_set_rate(clk, rate, &ctrl);
  197. if (apply) {
  198. if (actual_rate != rate)
  199. return -EINVAL;
  200. if (clk->users > 0)
  201. return -EBUSY;
  202. pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
  203. clk->name, rate, actual_rate);
  204. pm_writel(PLL1, ctrl);
  205. }
  206. return actual_rate;
  207. }
  208. static int pll1_set_parent(struct clk *clk, struct clk *parent)
  209. {
  210. u32 ctrl;
  211. if (clk->users > 0)
  212. return -EBUSY;
  213. ctrl = pm_readl(PLL1);
  214. WARN_ON(ctrl & PM_BIT(PLLEN));
  215. if (parent == &osc0)
  216. ctrl &= ~PM_BIT(PLLOSC);
  217. else if (parent == &osc1)
  218. ctrl |= PM_BIT(PLLOSC);
  219. else
  220. return -EINVAL;
  221. pm_writel(PLL1, ctrl);
  222. clk->parent = parent;
  223. return 0;
  224. }
  225. /*
  226. * The AT32AP7000 has five primary clock sources: One 32kHz
  227. * oscillator, two crystal oscillators and two PLLs.
  228. */
  229. static struct clk osc32k = {
  230. .name = "osc32k",
  231. .get_rate = osc_get_rate,
  232. .users = 1,
  233. .index = 0,
  234. };
  235. static struct clk osc0 = {
  236. .name = "osc0",
  237. .get_rate = osc_get_rate,
  238. .users = 1,
  239. .index = 1,
  240. };
  241. static struct clk osc1 = {
  242. .name = "osc1",
  243. .get_rate = osc_get_rate,
  244. .index = 2,
  245. };
  246. static struct clk pll0 = {
  247. .name = "pll0",
  248. .get_rate = pll0_get_rate,
  249. .parent = &osc0,
  250. };
  251. static struct clk pll1 = {
  252. .name = "pll1",
  253. .mode = pll1_mode,
  254. .get_rate = pll1_get_rate,
  255. .set_rate = pll1_set_rate,
  256. .set_parent = pll1_set_parent,
  257. .parent = &osc0,
  258. };
  259. /*
  260. * The main clock can be either osc0 or pll0. The boot loader may
  261. * have chosen one for us, so we don't really know which one until we
  262. * have a look at the SM.
  263. */
  264. static struct clk *main_clock;
  265. /*
  266. * Synchronous clocks are generated from the main clock. The clocks
  267. * must satisfy the constraint
  268. * fCPU >= fHSB >= fPB
  269. * i.e. each clock must not be faster than its parent.
  270. */
  271. static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift)
  272. {
  273. return main_clock->get_rate(main_clock) >> shift;
  274. };
  275. static void cpu_clk_mode(struct clk *clk, int enabled)
  276. {
  277. unsigned long flags;
  278. u32 mask;
  279. spin_lock_irqsave(&pm_lock, flags);
  280. mask = pm_readl(CPU_MASK);
  281. if (enabled)
  282. mask |= 1 << clk->index;
  283. else
  284. mask &= ~(1 << clk->index);
  285. pm_writel(CPU_MASK, mask);
  286. spin_unlock_irqrestore(&pm_lock, flags);
  287. }
  288. static unsigned long cpu_clk_get_rate(struct clk *clk)
  289. {
  290. unsigned long cksel, shift = 0;
  291. cksel = pm_readl(CKSEL);
  292. if (cksel & PM_BIT(CPUDIV))
  293. shift = PM_BFEXT(CPUSEL, cksel) + 1;
  294. return bus_clk_get_rate(clk, shift);
  295. }
  296. static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply)
  297. {
  298. u32 control;
  299. unsigned long parent_rate, child_div, actual_rate, div;
  300. parent_rate = clk->parent->get_rate(clk->parent);
  301. control = pm_readl(CKSEL);
  302. if (control & PM_BIT(HSBDIV))
  303. child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);
  304. else
  305. child_div = 1;
  306. if (rate > 3 * (parent_rate / 4) || child_div == 1) {
  307. actual_rate = parent_rate;
  308. control &= ~PM_BIT(CPUDIV);
  309. } else {
  310. unsigned int cpusel;
  311. div = (parent_rate + rate / 2) / rate;
  312. if (div > child_div)
  313. div = child_div;
  314. cpusel = (div > 1) ? (fls(div) - 2) : 0;
  315. control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);
  316. actual_rate = parent_rate / (1 << (cpusel + 1));
  317. }
  318. pr_debug("clk %s: new rate %lu (actual rate %lu)\n",
  319. clk->name, rate, actual_rate);
  320. if (apply)
  321. pm_writel(CKSEL, control);
  322. return actual_rate;
  323. }
  324. static void hsb_clk_mode(struct clk *clk, int enabled)
  325. {
  326. unsigned long flags;
  327. u32 mask;
  328. spin_lock_irqsave(&pm_lock, flags);
  329. mask = pm_readl(HSB_MASK);
  330. if (enabled)
  331. mask |= 1 << clk->index;
  332. else
  333. mask &= ~(1 << clk->index);
  334. pm_writel(HSB_MASK, mask);
  335. spin_unlock_irqrestore(&pm_lock, flags);
  336. }
  337. static unsigned long hsb_clk_get_rate(struct clk *clk)
  338. {
  339. unsigned long cksel, shift = 0;
  340. cksel = pm_readl(CKSEL);
  341. if (cksel & PM_BIT(HSBDIV))
  342. shift = PM_BFEXT(HSBSEL, cksel) + 1;
  343. return bus_clk_get_rate(clk, shift);
  344. }
  345. static void pba_clk_mode(struct clk *clk, int enabled)
  346. {
  347. unsigned long flags;
  348. u32 mask;
  349. spin_lock_irqsave(&pm_lock, flags);
  350. mask = pm_readl(PBA_MASK);
  351. if (enabled)
  352. mask |= 1 << clk->index;
  353. else
  354. mask &= ~(1 << clk->index);
  355. pm_writel(PBA_MASK, mask);
  356. spin_unlock_irqrestore(&pm_lock, flags);
  357. }
  358. static unsigned long pba_clk_get_rate(struct clk *clk)
  359. {
  360. unsigned long cksel, shift = 0;
  361. cksel = pm_readl(CKSEL);
  362. if (cksel & PM_BIT(PBADIV))
  363. shift = PM_BFEXT(PBASEL, cksel) + 1;
  364. return bus_clk_get_rate(clk, shift);
  365. }
  366. static void pbb_clk_mode(struct clk *clk, int enabled)
  367. {
  368. unsigned long flags;
  369. u32 mask;
  370. spin_lock_irqsave(&pm_lock, flags);
  371. mask = pm_readl(PBB_MASK);
  372. if (enabled)
  373. mask |= 1 << clk->index;
  374. else
  375. mask &= ~(1 << clk->index);
  376. pm_writel(PBB_MASK, mask);
  377. spin_unlock_irqrestore(&pm_lock, flags);
  378. }
  379. static unsigned long pbb_clk_get_rate(struct clk *clk)
  380. {
  381. unsigned long cksel, shift = 0;
  382. cksel = pm_readl(CKSEL);
  383. if (cksel & PM_BIT(PBBDIV))
  384. shift = PM_BFEXT(PBBSEL, cksel) + 1;
  385. return bus_clk_get_rate(clk, shift);
  386. }
  387. static struct clk cpu_clk = {
  388. .name = "cpu",
  389. .get_rate = cpu_clk_get_rate,
  390. .set_rate = cpu_clk_set_rate,
  391. .users = 1,
  392. };
  393. static struct clk hsb_clk = {
  394. .name = "hsb",
  395. .parent = &cpu_clk,
  396. .get_rate = hsb_clk_get_rate,
  397. };
  398. static struct clk pba_clk = {
  399. .name = "pba",
  400. .parent = &hsb_clk,
  401. .mode = hsb_clk_mode,
  402. .get_rate = pba_clk_get_rate,
  403. .index = 1,
  404. };
  405. static struct clk pbb_clk = {
  406. .name = "pbb",
  407. .parent = &hsb_clk,
  408. .mode = hsb_clk_mode,
  409. .get_rate = pbb_clk_get_rate,
  410. .users = 1,
  411. .index = 2,
  412. };
  413. /* --------------------------------------------------------------------
  414. * Generic Clock operations
  415. * -------------------------------------------------------------------- */
  416. static void genclk_mode(struct clk *clk, int enabled)
  417. {
  418. u32 control;
  419. control = pm_readl(GCCTRL(clk->index));
  420. if (enabled)
  421. control |= PM_BIT(CEN);
  422. else
  423. control &= ~PM_BIT(CEN);
  424. pm_writel(GCCTRL(clk->index), control);
  425. }
  426. static unsigned long genclk_get_rate(struct clk *clk)
  427. {
  428. u32 control;
  429. unsigned long div = 1;
  430. control = pm_readl(GCCTRL(clk->index));
  431. if (control & PM_BIT(DIVEN))
  432. div = 2 * (PM_BFEXT(DIV, control) + 1);
  433. return clk->parent->get_rate(clk->parent) / div;
  434. }
  435. static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
  436. {
  437. u32 control;
  438. unsigned long parent_rate, actual_rate, div;
  439. parent_rate = clk->parent->get_rate(clk->parent);
  440. control = pm_readl(GCCTRL(clk->index));
  441. if (rate > 3 * parent_rate / 4) {
  442. actual_rate = parent_rate;
  443. control &= ~PM_BIT(DIVEN);
  444. } else {
  445. div = (parent_rate + rate) / (2 * rate) - 1;
  446. control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);
  447. actual_rate = parent_rate / (2 * (div + 1));
  448. }
  449. dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",
  450. clk->name, rate, actual_rate);
  451. if (apply)
  452. pm_writel(GCCTRL(clk->index), control);
  453. return actual_rate;
  454. }
  455. int genclk_set_parent(struct clk *clk, struct clk *parent)
  456. {
  457. u32 control;
  458. dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",
  459. clk->name, parent->name, clk->parent->name);
  460. control = pm_readl(GCCTRL(clk->index));
  461. if (parent == &osc1 || parent == &pll1)
  462. control |= PM_BIT(OSCSEL);
  463. else if (parent == &osc0 || parent == &pll0)
  464. control &= ~PM_BIT(OSCSEL);
  465. else
  466. return -EINVAL;
  467. if (parent == &pll0 || parent == &pll1)
  468. control |= PM_BIT(PLLSEL);
  469. else
  470. control &= ~PM_BIT(PLLSEL);
  471. pm_writel(GCCTRL(clk->index), control);
  472. clk->parent = parent;
  473. return 0;
  474. }
  475. static void __init genclk_init_parent(struct clk *clk)
  476. {
  477. u32 control;
  478. struct clk *parent;
  479. BUG_ON(clk->index > 7);
  480. control = pm_readl(GCCTRL(clk->index));
  481. if (control & PM_BIT(OSCSEL))
  482. parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;
  483. else
  484. parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;
  485. clk->parent = parent;
  486. }
  487. /* --------------------------------------------------------------------
  488. * System peripherals
  489. * -------------------------------------------------------------------- */
  490. static struct resource at32_pm0_resource[] = {
  491. {
  492. .start = 0xfff00000,
  493. .end = 0xfff0007f,
  494. .flags = IORESOURCE_MEM,
  495. },
  496. IRQ(20),
  497. };
  498. static struct resource at32ap700x_rtc0_resource[] = {
  499. {
  500. .start = 0xfff00080,
  501. .end = 0xfff000af,
  502. .flags = IORESOURCE_MEM,
  503. },
  504. IRQ(21),
  505. };
  506. static struct resource at32_wdt0_resource[] = {
  507. {
  508. .start = 0xfff000b0,
  509. .end = 0xfff000cf,
  510. .flags = IORESOURCE_MEM,
  511. },
  512. };
  513. static struct resource at32_eic0_resource[] = {
  514. {
  515. .start = 0xfff00100,
  516. .end = 0xfff0013f,
  517. .flags = IORESOURCE_MEM,
  518. },
  519. IRQ(19),
  520. };
  521. DEFINE_DEV(at32_pm, 0);
  522. DEFINE_DEV(at32ap700x_rtc, 0);
  523. DEFINE_DEV(at32_wdt, 0);
  524. DEFINE_DEV(at32_eic, 0);
  525. /*
  526. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  527. * is always running.
  528. */
  529. static struct clk at32_pm_pclk = {
  530. .name = "pclk",
  531. .dev = &at32_pm0_device.dev,
  532. .parent = &pbb_clk,
  533. .mode = pbb_clk_mode,
  534. .get_rate = pbb_clk_get_rate,
  535. .users = 1,
  536. .index = 0,
  537. };
  538. static struct resource intc0_resource[] = {
  539. PBMEM(0xfff00400),
  540. };
  541. struct platform_device at32_intc0_device = {
  542. .name = "intc",
  543. .id = 0,
  544. .resource = intc0_resource,
  545. .num_resources = ARRAY_SIZE(intc0_resource),
  546. };
  547. DEV_CLK(pclk, at32_intc0, pbb, 1);
  548. static struct clk ebi_clk = {
  549. .name = "ebi",
  550. .parent = &hsb_clk,
  551. .mode = hsb_clk_mode,
  552. .get_rate = hsb_clk_get_rate,
  553. .users = 1,
  554. };
  555. static struct clk hramc_clk = {
  556. .name = "hramc",
  557. .parent = &hsb_clk,
  558. .mode = hsb_clk_mode,
  559. .get_rate = hsb_clk_get_rate,
  560. .users = 1,
  561. .index = 3,
  562. };
  563. static struct resource smc0_resource[] = {
  564. PBMEM(0xfff03400),
  565. };
  566. DEFINE_DEV(smc, 0);
  567. DEV_CLK(pclk, smc0, pbb, 13);
  568. DEV_CLK(mck, smc0, hsb, 0);
  569. static struct platform_device pdc_device = {
  570. .name = "pdc",
  571. .id = 0,
  572. };
  573. DEV_CLK(hclk, pdc, hsb, 4);
  574. DEV_CLK(pclk, pdc, pba, 16);
  575. static struct clk pico_clk = {
  576. .name = "pico",
  577. .parent = &cpu_clk,
  578. .mode = cpu_clk_mode,
  579. .get_rate = cpu_clk_get_rate,
  580. .users = 1,
  581. };
  582. static struct resource dmaca0_resource[] = {
  583. {
  584. .start = 0xff200000,
  585. .end = 0xff20ffff,
  586. .flags = IORESOURCE_MEM,
  587. },
  588. IRQ(2),
  589. };
  590. DEFINE_DEV(dmaca, 0);
  591. DEV_CLK(hclk, dmaca0, hsb, 10);
  592. /* --------------------------------------------------------------------
  593. * HMATRIX
  594. * -------------------------------------------------------------------- */
  595. static struct clk hmatrix_clk = {
  596. .name = "hmatrix_clk",
  597. .parent = &pbb_clk,
  598. .mode = pbb_clk_mode,
  599. .get_rate = pbb_clk_get_rate,
  600. .index = 2,
  601. .users = 1,
  602. };
  603. #define HMATRIX_BASE ((void __iomem *)0xfff00800)
  604. #define hmatrix_readl(reg) \
  605. __raw_readl((HMATRIX_BASE) + HMATRIX_##reg)
  606. #define hmatrix_writel(reg,value) \
  607. __raw_writel((value), (HMATRIX_BASE) + HMATRIX_##reg)
  608. /*
  609. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  610. * External Bus Interface (EBI). This can be used to enable special
  611. * features like CompactFlash support, NAND Flash support, etc. on
  612. * certain chipselects.
  613. */
  614. static inline void set_ebi_sfr_bits(u32 mask)
  615. {
  616. u32 sfr;
  617. clk_enable(&hmatrix_clk);
  618. sfr = hmatrix_readl(SFR4);
  619. sfr |= mask;
  620. hmatrix_writel(SFR4, sfr);
  621. clk_disable(&hmatrix_clk);
  622. }
  623. /* --------------------------------------------------------------------
  624. * Timer/Counter (TC)
  625. * -------------------------------------------------------------------- */
  626. static struct resource at32_tcb0_resource[] = {
  627. PBMEM(0xfff00c00),
  628. IRQ(22),
  629. };
  630. static struct platform_device at32_tcb0_device = {
  631. .name = "atmel_tcb",
  632. .id = 0,
  633. .resource = at32_tcb0_resource,
  634. .num_resources = ARRAY_SIZE(at32_tcb0_resource),
  635. };
  636. DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
  637. static struct resource at32_tcb1_resource[] = {
  638. PBMEM(0xfff01000),
  639. IRQ(23),
  640. };
  641. static struct platform_device at32_tcb1_device = {
  642. .name = "atmel_tcb",
  643. .id = 1,
  644. .resource = at32_tcb1_resource,
  645. .num_resources = ARRAY_SIZE(at32_tcb1_resource),
  646. };
  647. DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
  648. /* --------------------------------------------------------------------
  649. * PIO
  650. * -------------------------------------------------------------------- */
  651. static struct resource pio0_resource[] = {
  652. PBMEM(0xffe02800),
  653. IRQ(13),
  654. };
  655. DEFINE_DEV(pio, 0);
  656. DEV_CLK(mck, pio0, pba, 10);
  657. static struct resource pio1_resource[] = {
  658. PBMEM(0xffe02c00),
  659. IRQ(14),
  660. };
  661. DEFINE_DEV(pio, 1);
  662. DEV_CLK(mck, pio1, pba, 11);
  663. static struct resource pio2_resource[] = {
  664. PBMEM(0xffe03000),
  665. IRQ(15),
  666. };
  667. DEFINE_DEV(pio, 2);
  668. DEV_CLK(mck, pio2, pba, 12);
  669. static struct resource pio3_resource[] = {
  670. PBMEM(0xffe03400),
  671. IRQ(16),
  672. };
  673. DEFINE_DEV(pio, 3);
  674. DEV_CLK(mck, pio3, pba, 13);
  675. static struct resource pio4_resource[] = {
  676. PBMEM(0xffe03800),
  677. IRQ(17),
  678. };
  679. DEFINE_DEV(pio, 4);
  680. DEV_CLK(mck, pio4, pba, 14);
  681. void __init at32_add_system_devices(void)
  682. {
  683. platform_device_register(&at32_pm0_device);
  684. platform_device_register(&at32_intc0_device);
  685. platform_device_register(&at32ap700x_rtc0_device);
  686. platform_device_register(&at32_wdt0_device);
  687. platform_device_register(&at32_eic0_device);
  688. platform_device_register(&smc0_device);
  689. platform_device_register(&pdc_device);
  690. platform_device_register(&dmaca0_device);
  691. platform_device_register(&at32_tcb0_device);
  692. platform_device_register(&at32_tcb1_device);
  693. platform_device_register(&pio0_device);
  694. platform_device_register(&pio1_device);
  695. platform_device_register(&pio2_device);
  696. platform_device_register(&pio3_device);
  697. platform_device_register(&pio4_device);
  698. }
  699. /* --------------------------------------------------------------------
  700. * USART
  701. * -------------------------------------------------------------------- */
  702. static struct atmel_uart_data atmel_usart0_data = {
  703. .use_dma_tx = 1,
  704. .use_dma_rx = 1,
  705. };
  706. static struct resource atmel_usart0_resource[] = {
  707. PBMEM(0xffe00c00),
  708. IRQ(6),
  709. };
  710. DEFINE_DEV_DATA(atmel_usart, 0);
  711. DEV_CLK(usart, atmel_usart0, pba, 3);
  712. static struct atmel_uart_data atmel_usart1_data = {
  713. .use_dma_tx = 1,
  714. .use_dma_rx = 1,
  715. };
  716. static struct resource atmel_usart1_resource[] = {
  717. PBMEM(0xffe01000),
  718. IRQ(7),
  719. };
  720. DEFINE_DEV_DATA(atmel_usart, 1);
  721. DEV_CLK(usart, atmel_usart1, pba, 4);
  722. static struct atmel_uart_data atmel_usart2_data = {
  723. .use_dma_tx = 1,
  724. .use_dma_rx = 1,
  725. };
  726. static struct resource atmel_usart2_resource[] = {
  727. PBMEM(0xffe01400),
  728. IRQ(8),
  729. };
  730. DEFINE_DEV_DATA(atmel_usart, 2);
  731. DEV_CLK(usart, atmel_usart2, pba, 5);
  732. static struct atmel_uart_data atmel_usart3_data = {
  733. .use_dma_tx = 1,
  734. .use_dma_rx = 1,
  735. };
  736. static struct resource atmel_usart3_resource[] = {
  737. PBMEM(0xffe01800),
  738. IRQ(9),
  739. };
  740. DEFINE_DEV_DATA(atmel_usart, 3);
  741. DEV_CLK(usart, atmel_usart3, pba, 6);
  742. static inline void configure_usart0_pins(void)
  743. {
  744. select_peripheral(PA(8), PERIPH_B, 0); /* RXD */
  745. select_peripheral(PA(9), PERIPH_B, 0); /* TXD */
  746. }
  747. static inline void configure_usart1_pins(void)
  748. {
  749. select_peripheral(PA(17), PERIPH_A, 0); /* RXD */
  750. select_peripheral(PA(18), PERIPH_A, 0); /* TXD */
  751. }
  752. static inline void configure_usart2_pins(void)
  753. {
  754. select_peripheral(PB(26), PERIPH_B, 0); /* RXD */
  755. select_peripheral(PB(27), PERIPH_B, 0); /* TXD */
  756. }
  757. static inline void configure_usart3_pins(void)
  758. {
  759. select_peripheral(PB(18), PERIPH_B, 0); /* RXD */
  760. select_peripheral(PB(17), PERIPH_B, 0); /* TXD */
  761. }
  762. static struct platform_device *__initdata at32_usarts[4];
  763. void __init at32_map_usart(unsigned int hw_id, unsigned int line)
  764. {
  765. struct platform_device *pdev;
  766. switch (hw_id) {
  767. case 0:
  768. pdev = &atmel_usart0_device;
  769. configure_usart0_pins();
  770. break;
  771. case 1:
  772. pdev = &atmel_usart1_device;
  773. configure_usart1_pins();
  774. break;
  775. case 2:
  776. pdev = &atmel_usart2_device;
  777. configure_usart2_pins();
  778. break;
  779. case 3:
  780. pdev = &atmel_usart3_device;
  781. configure_usart3_pins();
  782. break;
  783. default:
  784. return;
  785. }
  786. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  787. /* Addresses in the P4 segment are permanently mapped 1:1 */
  788. struct atmel_uart_data *data = pdev->dev.platform_data;
  789. data->regs = (void __iomem *)pdev->resource[0].start;
  790. }
  791. pdev->id = line;
  792. at32_usarts[line] = pdev;
  793. }
  794. struct platform_device *__init at32_add_device_usart(unsigned int id)
  795. {
  796. platform_device_register(at32_usarts[id]);
  797. return at32_usarts[id];
  798. }
  799. struct platform_device *atmel_default_console_device;
  800. void __init at32_setup_serial_console(unsigned int usart_id)
  801. {
  802. atmel_default_console_device = at32_usarts[usart_id];
  803. }
  804. /* --------------------------------------------------------------------
  805. * Ethernet
  806. * -------------------------------------------------------------------- */
  807. #ifdef CONFIG_CPU_AT32AP7000
  808. static struct eth_platform_data macb0_data;
  809. static struct resource macb0_resource[] = {
  810. PBMEM(0xfff01800),
  811. IRQ(25),
  812. };
  813. DEFINE_DEV_DATA(macb, 0);
  814. DEV_CLK(hclk, macb0, hsb, 8);
  815. DEV_CLK(pclk, macb0, pbb, 6);
  816. static struct eth_platform_data macb1_data;
  817. static struct resource macb1_resource[] = {
  818. PBMEM(0xfff01c00),
  819. IRQ(26),
  820. };
  821. DEFINE_DEV_DATA(macb, 1);
  822. DEV_CLK(hclk, macb1, hsb, 9);
  823. DEV_CLK(pclk, macb1, pbb, 7);
  824. struct platform_device *__init
  825. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  826. {
  827. struct platform_device *pdev;
  828. switch (id) {
  829. case 0:
  830. pdev = &macb0_device;
  831. select_peripheral(PC(3), PERIPH_A, 0); /* TXD0 */
  832. select_peripheral(PC(4), PERIPH_A, 0); /* TXD1 */
  833. select_peripheral(PC(7), PERIPH_A, 0); /* TXEN */
  834. select_peripheral(PC(8), PERIPH_A, 0); /* TXCK */
  835. select_peripheral(PC(9), PERIPH_A, 0); /* RXD0 */
  836. select_peripheral(PC(10), PERIPH_A, 0); /* RXD1 */
  837. select_peripheral(PC(13), PERIPH_A, 0); /* RXER */
  838. select_peripheral(PC(15), PERIPH_A, 0); /* RXDV */
  839. select_peripheral(PC(16), PERIPH_A, 0); /* MDC */
  840. select_peripheral(PC(17), PERIPH_A, 0); /* MDIO */
  841. if (!data->is_rmii) {
  842. select_peripheral(PC(0), PERIPH_A, 0); /* COL */
  843. select_peripheral(PC(1), PERIPH_A, 0); /* CRS */
  844. select_peripheral(PC(2), PERIPH_A, 0); /* TXER */
  845. select_peripheral(PC(5), PERIPH_A, 0); /* TXD2 */
  846. select_peripheral(PC(6), PERIPH_A, 0); /* TXD3 */
  847. select_peripheral(PC(11), PERIPH_A, 0); /* RXD2 */
  848. select_peripheral(PC(12), PERIPH_A, 0); /* RXD3 */
  849. select_peripheral(PC(14), PERIPH_A, 0); /* RXCK */
  850. select_peripheral(PC(18), PERIPH_A, 0); /* SPD */
  851. }
  852. break;
  853. case 1:
  854. pdev = &macb1_device;
  855. select_peripheral(PD(13), PERIPH_B, 0); /* TXD0 */
  856. select_peripheral(PD(14), PERIPH_B, 0); /* TXD1 */
  857. select_peripheral(PD(11), PERIPH_B, 0); /* TXEN */
  858. select_peripheral(PD(12), PERIPH_B, 0); /* TXCK */
  859. select_peripheral(PD(10), PERIPH_B, 0); /* RXD0 */
  860. select_peripheral(PD(6), PERIPH_B, 0); /* RXD1 */
  861. select_peripheral(PD(5), PERIPH_B, 0); /* RXER */
  862. select_peripheral(PD(4), PERIPH_B, 0); /* RXDV */
  863. select_peripheral(PD(3), PERIPH_B, 0); /* MDC */
  864. select_peripheral(PD(2), PERIPH_B, 0); /* MDIO */
  865. if (!data->is_rmii) {
  866. select_peripheral(PC(19), PERIPH_B, 0); /* COL */
  867. select_peripheral(PC(23), PERIPH_B, 0); /* CRS */
  868. select_peripheral(PC(26), PERIPH_B, 0); /* TXER */
  869. select_peripheral(PC(27), PERIPH_B, 0); /* TXD2 */
  870. select_peripheral(PC(28), PERIPH_B, 0); /* TXD3 */
  871. select_peripheral(PC(29), PERIPH_B, 0); /* RXD2 */
  872. select_peripheral(PC(30), PERIPH_B, 0); /* RXD3 */
  873. select_peripheral(PC(24), PERIPH_B, 0); /* RXCK */
  874. select_peripheral(PD(15), PERIPH_B, 0); /* SPD */
  875. }
  876. break;
  877. default:
  878. return NULL;
  879. }
  880. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  881. platform_device_register(pdev);
  882. return pdev;
  883. }
  884. #endif
  885. /* --------------------------------------------------------------------
  886. * SPI
  887. * -------------------------------------------------------------------- */
  888. static struct resource atmel_spi0_resource[] = {
  889. PBMEM(0xffe00000),
  890. IRQ(3),
  891. };
  892. DEFINE_DEV(atmel_spi, 0);
  893. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  894. static struct resource atmel_spi1_resource[] = {
  895. PBMEM(0xffe00400),
  896. IRQ(4),
  897. };
  898. DEFINE_DEV(atmel_spi, 1);
  899. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  900. static void __init
  901. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
  902. unsigned int n, const u8 *pins)
  903. {
  904. unsigned int pin, mode;
  905. for (; n; n--, b++) {
  906. b->bus_num = bus_num;
  907. if (b->chip_select >= 4)
  908. continue;
  909. pin = (unsigned)b->controller_data;
  910. if (!pin) {
  911. pin = pins[b->chip_select];
  912. b->controller_data = (void *)pin;
  913. }
  914. mode = AT32_GPIOF_OUTPUT;
  915. if (!(b->mode & SPI_CS_HIGH))
  916. mode |= AT32_GPIOF_HIGH;
  917. at32_select_gpio(pin, mode);
  918. }
  919. }
  920. struct platform_device *__init
  921. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  922. {
  923. /*
  924. * Manage the chipselects as GPIOs, normally using the same pins
  925. * the SPI controller expects; but boards can use other pins.
  926. */
  927. static u8 __initdata spi0_pins[] =
  928. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  929. GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
  930. static u8 __initdata spi1_pins[] =
  931. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  932. GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
  933. struct platform_device *pdev;
  934. switch (id) {
  935. case 0:
  936. pdev = &atmel_spi0_device;
  937. select_peripheral(PA(0), PERIPH_A, 0); /* MISO */
  938. select_peripheral(PA(1), PERIPH_A, 0); /* MOSI */
  939. select_peripheral(PA(2), PERIPH_A, 0); /* SCK */
  940. at32_spi_setup_slaves(0, b, n, spi0_pins);
  941. break;
  942. case 1:
  943. pdev = &atmel_spi1_device;
  944. select_peripheral(PB(0), PERIPH_B, 0); /* MISO */
  945. select_peripheral(PB(1), PERIPH_B, 0); /* MOSI */
  946. select_peripheral(PB(5), PERIPH_B, 0); /* SCK */
  947. at32_spi_setup_slaves(1, b, n, spi1_pins);
  948. break;
  949. default:
  950. return NULL;
  951. }
  952. spi_register_board_info(b, n);
  953. platform_device_register(pdev);
  954. return pdev;
  955. }
  956. /* --------------------------------------------------------------------
  957. * TWI
  958. * -------------------------------------------------------------------- */
  959. static struct resource atmel_twi0_resource[] __initdata = {
  960. PBMEM(0xffe00800),
  961. IRQ(5),
  962. };
  963. static struct clk atmel_twi0_pclk = {
  964. .name = "twi_pclk",
  965. .parent = &pba_clk,
  966. .mode = pba_clk_mode,
  967. .get_rate = pba_clk_get_rate,
  968. .index = 2,
  969. };
  970. struct platform_device *__init at32_add_device_twi(unsigned int id,
  971. struct i2c_board_info *b,
  972. unsigned int n)
  973. {
  974. struct platform_device *pdev;
  975. if (id != 0)
  976. return NULL;
  977. pdev = platform_device_alloc("atmel_twi", id);
  978. if (!pdev)
  979. return NULL;
  980. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  981. ARRAY_SIZE(atmel_twi0_resource)))
  982. goto err_add_resources;
  983. select_peripheral(PA(6), PERIPH_A, 0); /* SDA */
  984. select_peripheral(PA(7), PERIPH_A, 0); /* SDL */
  985. atmel_twi0_pclk.dev = &pdev->dev;
  986. if (b)
  987. i2c_register_board_info(id, b, n);
  988. platform_device_add(pdev);
  989. return pdev;
  990. err_add_resources:
  991. platform_device_put(pdev);
  992. return NULL;
  993. }
  994. /* --------------------------------------------------------------------
  995. * MMC
  996. * -------------------------------------------------------------------- */
  997. static struct resource atmel_mci0_resource[] __initdata = {
  998. PBMEM(0xfff02400),
  999. IRQ(28),
  1000. };
  1001. static struct clk atmel_mci0_pclk = {
  1002. .name = "mci_clk",
  1003. .parent = &pbb_clk,
  1004. .mode = pbb_clk_mode,
  1005. .get_rate = pbb_clk_get_rate,
  1006. .index = 9,
  1007. };
  1008. struct platform_device *__init at32_add_device_mci(unsigned int id)
  1009. {
  1010. struct platform_device *pdev;
  1011. if (id != 0)
  1012. return NULL;
  1013. pdev = platform_device_alloc("atmel_mci", id);
  1014. if (!pdev)
  1015. return NULL;
  1016. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  1017. ARRAY_SIZE(atmel_mci0_resource)))
  1018. goto err_add_resources;
  1019. select_peripheral(PA(10), PERIPH_A, 0); /* CLK */
  1020. select_peripheral(PA(11), PERIPH_A, 0); /* CMD */
  1021. select_peripheral(PA(12), PERIPH_A, 0); /* DATA0 */
  1022. select_peripheral(PA(13), PERIPH_A, 0); /* DATA1 */
  1023. select_peripheral(PA(14), PERIPH_A, 0); /* DATA2 */
  1024. select_peripheral(PA(15), PERIPH_A, 0); /* DATA3 */
  1025. atmel_mci0_pclk.dev = &pdev->dev;
  1026. platform_device_add(pdev);
  1027. return pdev;
  1028. err_add_resources:
  1029. platform_device_put(pdev);
  1030. return NULL;
  1031. }
  1032. /* --------------------------------------------------------------------
  1033. * LCDC
  1034. * -------------------------------------------------------------------- */
  1035. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1036. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  1037. static struct resource atmel_lcdfb0_resource[] = {
  1038. {
  1039. .start = 0xff000000,
  1040. .end = 0xff000fff,
  1041. .flags = IORESOURCE_MEM,
  1042. },
  1043. IRQ(1),
  1044. {
  1045. /* Placeholder for pre-allocated fb memory */
  1046. .start = 0x00000000,
  1047. .end = 0x00000000,
  1048. .flags = 0,
  1049. },
  1050. };
  1051. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  1052. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  1053. static struct clk atmel_lcdfb0_pixclk = {
  1054. .name = "lcdc_clk",
  1055. .dev = &atmel_lcdfb0_device.dev,
  1056. .mode = genclk_mode,
  1057. .get_rate = genclk_get_rate,
  1058. .set_rate = genclk_set_rate,
  1059. .set_parent = genclk_set_parent,
  1060. .index = 7,
  1061. };
  1062. struct platform_device *__init
  1063. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  1064. unsigned long fbmem_start, unsigned long fbmem_len)
  1065. {
  1066. struct platform_device *pdev;
  1067. struct atmel_lcdfb_info *info;
  1068. struct fb_monspecs *monspecs;
  1069. struct fb_videomode *modedb;
  1070. unsigned int modedb_size;
  1071. /*
  1072. * Do a deep copy of the fb data, monspecs and modedb. Make
  1073. * sure all allocations are done before setting up the
  1074. * portmux.
  1075. */
  1076. monspecs = kmemdup(data->default_monspecs,
  1077. sizeof(struct fb_monspecs), GFP_KERNEL);
  1078. if (!monspecs)
  1079. return NULL;
  1080. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  1081. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  1082. if (!modedb)
  1083. goto err_dup_modedb;
  1084. monspecs->modedb = modedb;
  1085. switch (id) {
  1086. case 0:
  1087. pdev = &atmel_lcdfb0_device;
  1088. select_peripheral(PC(19), PERIPH_A, 0); /* CC */
  1089. select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
  1090. select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
  1091. select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
  1092. select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
  1093. select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
  1094. select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
  1095. select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
  1096. select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
  1097. select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
  1098. select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
  1099. select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
  1100. select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
  1101. select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
  1102. select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
  1103. select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
  1104. select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
  1105. select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
  1106. select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
  1107. select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
  1108. select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
  1109. select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
  1110. select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
  1111. select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
  1112. select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
  1113. select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
  1114. select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
  1115. select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
  1116. select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
  1117. select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
  1118. select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
  1119. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  1120. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  1121. break;
  1122. default:
  1123. goto err_invalid_id;
  1124. }
  1125. if (fbmem_len) {
  1126. pdev->resource[2].start = fbmem_start;
  1127. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1128. pdev->resource[2].flags = IORESOURCE_MEM;
  1129. }
  1130. info = pdev->dev.platform_data;
  1131. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  1132. info->default_monspecs = monspecs;
  1133. platform_device_register(pdev);
  1134. return pdev;
  1135. err_invalid_id:
  1136. kfree(modedb);
  1137. err_dup_modedb:
  1138. kfree(monspecs);
  1139. return NULL;
  1140. }
  1141. #endif
  1142. /* --------------------------------------------------------------------
  1143. * PWM
  1144. * -------------------------------------------------------------------- */
  1145. static struct resource atmel_pwm0_resource[] __initdata = {
  1146. PBMEM(0xfff01400),
  1147. IRQ(24),
  1148. };
  1149. static struct clk atmel_pwm0_mck = {
  1150. .name = "mck",
  1151. .parent = &pbb_clk,
  1152. .mode = pbb_clk_mode,
  1153. .get_rate = pbb_clk_get_rate,
  1154. .index = 5,
  1155. };
  1156. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1157. {
  1158. struct platform_device *pdev;
  1159. if (!mask)
  1160. return NULL;
  1161. pdev = platform_device_alloc("atmel_pwm", 0);
  1162. if (!pdev)
  1163. return NULL;
  1164. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1165. ARRAY_SIZE(atmel_pwm0_resource)))
  1166. goto out_free_pdev;
  1167. if (platform_device_add_data(pdev, &mask, sizeof(mask)))
  1168. goto out_free_pdev;
  1169. if (mask & (1 << 0))
  1170. select_peripheral(PA(28), PERIPH_A, 0);
  1171. if (mask & (1 << 1))
  1172. select_peripheral(PA(29), PERIPH_A, 0);
  1173. if (mask & (1 << 2))
  1174. select_peripheral(PA(21), PERIPH_B, 0);
  1175. if (mask & (1 << 3))
  1176. select_peripheral(PA(22), PERIPH_B, 0);
  1177. atmel_pwm0_mck.dev = &pdev->dev;
  1178. platform_device_add(pdev);
  1179. return pdev;
  1180. out_free_pdev:
  1181. platform_device_put(pdev);
  1182. return NULL;
  1183. }
  1184. /* --------------------------------------------------------------------
  1185. * SSC
  1186. * -------------------------------------------------------------------- */
  1187. static struct resource ssc0_resource[] = {
  1188. PBMEM(0xffe01c00),
  1189. IRQ(10),
  1190. };
  1191. DEFINE_DEV(ssc, 0);
  1192. DEV_CLK(pclk, ssc0, pba, 7);
  1193. static struct resource ssc1_resource[] = {
  1194. PBMEM(0xffe02000),
  1195. IRQ(11),
  1196. };
  1197. DEFINE_DEV(ssc, 1);
  1198. DEV_CLK(pclk, ssc1, pba, 8);
  1199. static struct resource ssc2_resource[] = {
  1200. PBMEM(0xffe02400),
  1201. IRQ(12),
  1202. };
  1203. DEFINE_DEV(ssc, 2);
  1204. DEV_CLK(pclk, ssc2, pba, 9);
  1205. struct platform_device *__init
  1206. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1207. {
  1208. struct platform_device *pdev;
  1209. switch (id) {
  1210. case 0:
  1211. pdev = &ssc0_device;
  1212. if (flags & ATMEL_SSC_RF)
  1213. select_peripheral(PA(21), PERIPH_A, 0); /* RF */
  1214. if (flags & ATMEL_SSC_RK)
  1215. select_peripheral(PA(22), PERIPH_A, 0); /* RK */
  1216. if (flags & ATMEL_SSC_TK)
  1217. select_peripheral(PA(23), PERIPH_A, 0); /* TK */
  1218. if (flags & ATMEL_SSC_TF)
  1219. select_peripheral(PA(24), PERIPH_A, 0); /* TF */
  1220. if (flags & ATMEL_SSC_TD)
  1221. select_peripheral(PA(25), PERIPH_A, 0); /* TD */
  1222. if (flags & ATMEL_SSC_RD)
  1223. select_peripheral(PA(26), PERIPH_A, 0); /* RD */
  1224. break;
  1225. case 1:
  1226. pdev = &ssc1_device;
  1227. if (flags & ATMEL_SSC_RF)
  1228. select_peripheral(PA(0), PERIPH_B, 0); /* RF */
  1229. if (flags & ATMEL_SSC_RK)
  1230. select_peripheral(PA(1), PERIPH_B, 0); /* RK */
  1231. if (flags & ATMEL_SSC_TK)
  1232. select_peripheral(PA(2), PERIPH_B, 0); /* TK */
  1233. if (flags & ATMEL_SSC_TF)
  1234. select_peripheral(PA(3), PERIPH_B, 0); /* TF */
  1235. if (flags & ATMEL_SSC_TD)
  1236. select_peripheral(PA(4), PERIPH_B, 0); /* TD */
  1237. if (flags & ATMEL_SSC_RD)
  1238. select_peripheral(PA(5), PERIPH_B, 0); /* RD */
  1239. break;
  1240. case 2:
  1241. pdev = &ssc2_device;
  1242. if (flags & ATMEL_SSC_TD)
  1243. select_peripheral(PB(13), PERIPH_A, 0); /* TD */
  1244. if (flags & ATMEL_SSC_RD)
  1245. select_peripheral(PB(14), PERIPH_A, 0); /* RD */
  1246. if (flags & ATMEL_SSC_TK)
  1247. select_peripheral(PB(15), PERIPH_A, 0); /* TK */
  1248. if (flags & ATMEL_SSC_TF)
  1249. select_peripheral(PB(16), PERIPH_A, 0); /* TF */
  1250. if (flags & ATMEL_SSC_RF)
  1251. select_peripheral(PB(17), PERIPH_A, 0); /* RF */
  1252. if (flags & ATMEL_SSC_RK)
  1253. select_peripheral(PB(18), PERIPH_A, 0); /* RK */
  1254. break;
  1255. default:
  1256. return NULL;
  1257. }
  1258. platform_device_register(pdev);
  1259. return pdev;
  1260. }
  1261. /* --------------------------------------------------------------------
  1262. * USB Device Controller
  1263. * -------------------------------------------------------------------- */
  1264. static struct resource usba0_resource[] __initdata = {
  1265. {
  1266. .start = 0xff300000,
  1267. .end = 0xff3fffff,
  1268. .flags = IORESOURCE_MEM,
  1269. }, {
  1270. .start = 0xfff03000,
  1271. .end = 0xfff033ff,
  1272. .flags = IORESOURCE_MEM,
  1273. },
  1274. IRQ(31),
  1275. };
  1276. static struct clk usba0_pclk = {
  1277. .name = "pclk",
  1278. .parent = &pbb_clk,
  1279. .mode = pbb_clk_mode,
  1280. .get_rate = pbb_clk_get_rate,
  1281. .index = 12,
  1282. };
  1283. static struct clk usba0_hclk = {
  1284. .name = "hclk",
  1285. .parent = &hsb_clk,
  1286. .mode = hsb_clk_mode,
  1287. .get_rate = hsb_clk_get_rate,
  1288. .index = 6,
  1289. };
  1290. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1291. [idx] = { \
  1292. .name = nam, \
  1293. .index = idx, \
  1294. .fifo_size = maxpkt, \
  1295. .nr_banks = maxbk, \
  1296. .can_dma = dma, \
  1297. .can_isoc = isoc, \
  1298. }
  1299. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1300. EP("ep0", 0, 64, 1, 0, 0),
  1301. EP("ep1", 1, 512, 2, 1, 1),
  1302. EP("ep2", 2, 512, 2, 1, 1),
  1303. EP("ep3-int", 3, 64, 3, 1, 0),
  1304. EP("ep4-int", 4, 64, 3, 1, 0),
  1305. EP("ep5", 5, 1024, 3, 1, 1),
  1306. EP("ep6", 6, 1024, 3, 1, 1),
  1307. };
  1308. #undef EP
  1309. struct platform_device *__init
  1310. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1311. {
  1312. /*
  1313. * pdata doesn't have room for any endpoints, so we need to
  1314. * append room for the ones we need right after it.
  1315. */
  1316. struct {
  1317. struct usba_platform_data pdata;
  1318. struct usba_ep_data ep[7];
  1319. } usba_data;
  1320. struct platform_device *pdev;
  1321. if (id != 0)
  1322. return NULL;
  1323. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1324. if (!pdev)
  1325. return NULL;
  1326. if (platform_device_add_resources(pdev, usba0_resource,
  1327. ARRAY_SIZE(usba0_resource)))
  1328. goto out_free_pdev;
  1329. if (data)
  1330. usba_data.pdata.vbus_pin = data->vbus_pin;
  1331. else
  1332. usba_data.pdata.vbus_pin = -EINVAL;
  1333. data = &usba_data.pdata;
  1334. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1335. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1336. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1337. goto out_free_pdev;
  1338. if (data->vbus_pin >= 0)
  1339. at32_select_gpio(data->vbus_pin, 0);
  1340. usba0_pclk.dev = &pdev->dev;
  1341. usba0_hclk.dev = &pdev->dev;
  1342. platform_device_add(pdev);
  1343. return pdev;
  1344. out_free_pdev:
  1345. platform_device_put(pdev);
  1346. return NULL;
  1347. }
  1348. /* --------------------------------------------------------------------
  1349. * IDE / CompactFlash
  1350. * -------------------------------------------------------------------- */
  1351. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1352. static struct resource at32_smc_cs4_resource[] __initdata = {
  1353. {
  1354. .start = 0x04000000,
  1355. .end = 0x07ffffff,
  1356. .flags = IORESOURCE_MEM,
  1357. },
  1358. IRQ(~0UL), /* Magic IRQ will be overridden */
  1359. };
  1360. static struct resource at32_smc_cs5_resource[] __initdata = {
  1361. {
  1362. .start = 0x20000000,
  1363. .end = 0x23ffffff,
  1364. .flags = IORESOURCE_MEM,
  1365. },
  1366. IRQ(~0UL), /* Magic IRQ will be overridden */
  1367. };
  1368. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1369. unsigned int cs, unsigned int extint)
  1370. {
  1371. static unsigned int extint_pin_map[4] __initdata = {
  1372. GPIO_PIN_PB(25),
  1373. GPIO_PIN_PB(26),
  1374. GPIO_PIN_PB(27),
  1375. GPIO_PIN_PB(28),
  1376. };
  1377. static bool common_pins_initialized __initdata = false;
  1378. unsigned int extint_pin;
  1379. int ret;
  1380. if (extint >= ARRAY_SIZE(extint_pin_map))
  1381. return -EINVAL;
  1382. extint_pin = extint_pin_map[extint];
  1383. switch (cs) {
  1384. case 4:
  1385. ret = platform_device_add_resources(pdev,
  1386. at32_smc_cs4_resource,
  1387. ARRAY_SIZE(at32_smc_cs4_resource));
  1388. if (ret)
  1389. return ret;
  1390. select_peripheral(PE(21), PERIPH_A, 0); /* NCS4 -> OE_N */
  1391. set_ebi_sfr_bits(HMATRIX_BIT(CS4A));
  1392. break;
  1393. case 5:
  1394. ret = platform_device_add_resources(pdev,
  1395. at32_smc_cs5_resource,
  1396. ARRAY_SIZE(at32_smc_cs5_resource));
  1397. if (ret)
  1398. return ret;
  1399. select_peripheral(PE(22), PERIPH_A, 0); /* NCS5 -> OE_N */
  1400. set_ebi_sfr_bits(HMATRIX_BIT(CS5A));
  1401. break;
  1402. default:
  1403. return -EINVAL;
  1404. }
  1405. if (!common_pins_initialized) {
  1406. select_peripheral(PE(19), PERIPH_A, 0); /* CFCE1 -> CS0_N */
  1407. select_peripheral(PE(20), PERIPH_A, 0); /* CFCE2 -> CS1_N */
  1408. select_peripheral(PE(23), PERIPH_A, 0); /* CFRNW -> DIR */
  1409. select_peripheral(PE(24), PERIPH_A, 0); /* NWAIT <- IORDY */
  1410. common_pins_initialized = true;
  1411. }
  1412. at32_select_periph(extint_pin, GPIO_PERIPH_A, AT32_GPIOF_DEGLITCH);
  1413. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1414. pdev->resource[1].end = pdev->resource[1].start;
  1415. return 0;
  1416. }
  1417. struct platform_device *__init
  1418. at32_add_device_ide(unsigned int id, unsigned int extint,
  1419. struct ide_platform_data *data)
  1420. {
  1421. struct platform_device *pdev;
  1422. pdev = platform_device_alloc("at32_ide", id);
  1423. if (!pdev)
  1424. goto fail;
  1425. if (platform_device_add_data(pdev, data,
  1426. sizeof(struct ide_platform_data)))
  1427. goto fail;
  1428. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1429. goto fail;
  1430. platform_device_add(pdev);
  1431. return pdev;
  1432. fail:
  1433. platform_device_put(pdev);
  1434. return NULL;
  1435. }
  1436. struct platform_device *__init
  1437. at32_add_device_cf(unsigned int id, unsigned int extint,
  1438. struct cf_platform_data *data)
  1439. {
  1440. struct platform_device *pdev;
  1441. pdev = platform_device_alloc("at32_cf", id);
  1442. if (!pdev)
  1443. goto fail;
  1444. if (platform_device_add_data(pdev, data,
  1445. sizeof(struct cf_platform_data)))
  1446. goto fail;
  1447. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1448. goto fail;
  1449. if (data->detect_pin != GPIO_PIN_NONE)
  1450. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1451. if (data->reset_pin != GPIO_PIN_NONE)
  1452. at32_select_gpio(data->reset_pin, 0);
  1453. if (data->vcc_pin != GPIO_PIN_NONE)
  1454. at32_select_gpio(data->vcc_pin, 0);
  1455. /* READY is used as extint, so we can't select it as gpio */
  1456. platform_device_add(pdev);
  1457. return pdev;
  1458. fail:
  1459. platform_device_put(pdev);
  1460. return NULL;
  1461. }
  1462. #endif
  1463. /* --------------------------------------------------------------------
  1464. * AC97C
  1465. * -------------------------------------------------------------------- */
  1466. static struct resource atmel_ac97c0_resource[] __initdata = {
  1467. PBMEM(0xfff02800),
  1468. IRQ(29),
  1469. };
  1470. static struct clk atmel_ac97c0_pclk = {
  1471. .name = "pclk",
  1472. .parent = &pbb_clk,
  1473. .mode = pbb_clk_mode,
  1474. .get_rate = pbb_clk_get_rate,
  1475. .index = 10,
  1476. };
  1477. struct platform_device *__init at32_add_device_ac97c(unsigned int id)
  1478. {
  1479. struct platform_device *pdev;
  1480. if (id != 0)
  1481. return NULL;
  1482. pdev = platform_device_alloc("atmel_ac97c", id);
  1483. if (!pdev)
  1484. return NULL;
  1485. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1486. ARRAY_SIZE(atmel_ac97c0_resource)))
  1487. goto err_add_resources;
  1488. select_peripheral(PB(20), PERIPH_B, 0); /* SYNC */
  1489. select_peripheral(PB(21), PERIPH_B, 0); /* SDO */
  1490. select_peripheral(PB(22), PERIPH_B, 0); /* SDI */
  1491. select_peripheral(PB(23), PERIPH_B, 0); /* SCLK */
  1492. atmel_ac97c0_pclk.dev = &pdev->dev;
  1493. platform_device_add(pdev);
  1494. return pdev;
  1495. err_add_resources:
  1496. platform_device_put(pdev);
  1497. return NULL;
  1498. }
  1499. /* --------------------------------------------------------------------
  1500. * ABDAC
  1501. * -------------------------------------------------------------------- */
  1502. static struct resource abdac0_resource[] __initdata = {
  1503. PBMEM(0xfff02000),
  1504. IRQ(27),
  1505. };
  1506. static struct clk abdac0_pclk = {
  1507. .name = "pclk",
  1508. .parent = &pbb_clk,
  1509. .mode = pbb_clk_mode,
  1510. .get_rate = pbb_clk_get_rate,
  1511. .index = 8,
  1512. };
  1513. static struct clk abdac0_sample_clk = {
  1514. .name = "sample_clk",
  1515. .mode = genclk_mode,
  1516. .get_rate = genclk_get_rate,
  1517. .set_rate = genclk_set_rate,
  1518. .set_parent = genclk_set_parent,
  1519. .index = 6,
  1520. };
  1521. struct platform_device *__init at32_add_device_abdac(unsigned int id)
  1522. {
  1523. struct platform_device *pdev;
  1524. if (id != 0)
  1525. return NULL;
  1526. pdev = platform_device_alloc("abdac", id);
  1527. if (!pdev)
  1528. return NULL;
  1529. if (platform_device_add_resources(pdev, abdac0_resource,
  1530. ARRAY_SIZE(abdac0_resource)))
  1531. goto err_add_resources;
  1532. select_peripheral(PB(20), PERIPH_A, 0); /* DATA1 */
  1533. select_peripheral(PB(21), PERIPH_A, 0); /* DATA0 */
  1534. select_peripheral(PB(22), PERIPH_A, 0); /* DATAN1 */
  1535. select_peripheral(PB(23), PERIPH_A, 0); /* DATAN0 */
  1536. abdac0_pclk.dev = &pdev->dev;
  1537. abdac0_sample_clk.dev = &pdev->dev;
  1538. platform_device_add(pdev);
  1539. return pdev;
  1540. err_add_resources:
  1541. platform_device_put(pdev);
  1542. return NULL;
  1543. }
  1544. /* --------------------------------------------------------------------
  1545. * GCLK
  1546. * -------------------------------------------------------------------- */
  1547. static struct clk gclk0 = {
  1548. .name = "gclk0",
  1549. .mode = genclk_mode,
  1550. .get_rate = genclk_get_rate,
  1551. .set_rate = genclk_set_rate,
  1552. .set_parent = genclk_set_parent,
  1553. .index = 0,
  1554. };
  1555. static struct clk gclk1 = {
  1556. .name = "gclk1",
  1557. .mode = genclk_mode,
  1558. .get_rate = genclk_get_rate,
  1559. .set_rate = genclk_set_rate,
  1560. .set_parent = genclk_set_parent,
  1561. .index = 1,
  1562. };
  1563. static struct clk gclk2 = {
  1564. .name = "gclk2",
  1565. .mode = genclk_mode,
  1566. .get_rate = genclk_get_rate,
  1567. .set_rate = genclk_set_rate,
  1568. .set_parent = genclk_set_parent,
  1569. .index = 2,
  1570. };
  1571. static struct clk gclk3 = {
  1572. .name = "gclk3",
  1573. .mode = genclk_mode,
  1574. .get_rate = genclk_get_rate,
  1575. .set_rate = genclk_set_rate,
  1576. .set_parent = genclk_set_parent,
  1577. .index = 3,
  1578. };
  1579. static struct clk gclk4 = {
  1580. .name = "gclk4",
  1581. .mode = genclk_mode,
  1582. .get_rate = genclk_get_rate,
  1583. .set_rate = genclk_set_rate,
  1584. .set_parent = genclk_set_parent,
  1585. .index = 4,
  1586. };
  1587. struct clk *at32_clock_list[] = {
  1588. &osc32k,
  1589. &osc0,
  1590. &osc1,
  1591. &pll0,
  1592. &pll1,
  1593. &cpu_clk,
  1594. &hsb_clk,
  1595. &pba_clk,
  1596. &pbb_clk,
  1597. &at32_pm_pclk,
  1598. &at32_intc0_pclk,
  1599. &hmatrix_clk,
  1600. &ebi_clk,
  1601. &hramc_clk,
  1602. &smc0_pclk,
  1603. &smc0_mck,
  1604. &pdc_hclk,
  1605. &pdc_pclk,
  1606. &dmaca0_hclk,
  1607. &pico_clk,
  1608. &pio0_mck,
  1609. &pio1_mck,
  1610. &pio2_mck,
  1611. &pio3_mck,
  1612. &pio4_mck,
  1613. &at32_tcb0_t0_clk,
  1614. &at32_tcb1_t0_clk,
  1615. &atmel_usart0_usart,
  1616. &atmel_usart1_usart,
  1617. &atmel_usart2_usart,
  1618. &atmel_usart3_usart,
  1619. &atmel_pwm0_mck,
  1620. #if defined(CONFIG_CPU_AT32AP7000)
  1621. &macb0_hclk,
  1622. &macb0_pclk,
  1623. &macb1_hclk,
  1624. &macb1_pclk,
  1625. #endif
  1626. &atmel_spi0_spi_clk,
  1627. &atmel_spi1_spi_clk,
  1628. &atmel_twi0_pclk,
  1629. &atmel_mci0_pclk,
  1630. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1631. &atmel_lcdfb0_hck1,
  1632. &atmel_lcdfb0_pixclk,
  1633. #endif
  1634. &ssc0_pclk,
  1635. &ssc1_pclk,
  1636. &ssc2_pclk,
  1637. &usba0_hclk,
  1638. &usba0_pclk,
  1639. &atmel_ac97c0_pclk,
  1640. &abdac0_pclk,
  1641. &abdac0_sample_clk,
  1642. &gclk0,
  1643. &gclk1,
  1644. &gclk2,
  1645. &gclk3,
  1646. &gclk4,
  1647. };
  1648. unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
  1649. void __init at32_portmux_init(void)
  1650. {
  1651. at32_init_pio(&pio0_device);
  1652. at32_init_pio(&pio1_device);
  1653. at32_init_pio(&pio2_device);
  1654. at32_init_pio(&pio3_device);
  1655. at32_init_pio(&pio4_device);
  1656. }
  1657. void __init at32_clock_init(void)
  1658. {
  1659. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1660. int i;
  1661. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1662. main_clock = &pll0;
  1663. cpu_clk.parent = &pll0;
  1664. } else {
  1665. main_clock = &osc0;
  1666. cpu_clk.parent = &osc0;
  1667. }
  1668. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1669. pll0.parent = &osc1;
  1670. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1671. pll1.parent = &osc1;
  1672. genclk_init_parent(&gclk0);
  1673. genclk_init_parent(&gclk1);
  1674. genclk_init_parent(&gclk2);
  1675. genclk_init_parent(&gclk3);
  1676. genclk_init_parent(&gclk4);
  1677. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1678. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1679. #endif
  1680. genclk_init_parent(&abdac0_sample_clk);
  1681. /*
  1682. * Turn on all clocks that have at least one user already, and
  1683. * turn off everything else. We only do this for module
  1684. * clocks, and even though it isn't particularly pretty to
  1685. * check the address of the mode function, it should do the
  1686. * trick...
  1687. */
  1688. for (i = 0; i < ARRAY_SIZE(at32_clock_list); i++) {
  1689. struct clk *clk = at32_clock_list[i];
  1690. if (clk->users == 0)
  1691. continue;
  1692. if (clk->mode == &cpu_clk_mode)
  1693. cpu_mask |= 1 << clk->index;
  1694. else if (clk->mode == &hsb_clk_mode)
  1695. hsb_mask |= 1 << clk->index;
  1696. else if (clk->mode == &pba_clk_mode)
  1697. pba_mask |= 1 << clk->index;
  1698. else if (clk->mode == &pbb_clk_mode)
  1699. pbb_mask |= 1 << clk->index;
  1700. }
  1701. pm_writel(CPU_MASK, cpu_mask);
  1702. pm_writel(HSB_MASK, hsb_mask);
  1703. pm_writel(PBA_MASK, pba_mask);
  1704. pm_writel(PBB_MASK, pbb_mask);
  1705. }