at32ap700x.c 51 KB

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