at32ap700x.c 53 KB

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