at32ap700x.c 51 KB

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