at32ap700x.c 52 KB

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