at32ap700x.c 51 KB

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