at32ap700x.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  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_data *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_data), GFP_KERNEL);
  1129. if (!slave)
  1130. goto fail;
  1131. slave->sdata.dma_dev = &dw_dmac0_device.dev;
  1132. slave->sdata.reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
  1133. slave->sdata.cfg_hi = (DWC_CFGH_SRC_PER(0)
  1134. | DWC_CFGH_DST_PER(1));
  1135. slave->sdata.cfg_lo &= ~(DWC_CFGL_HS_DST_POL
  1136. | DWC_CFGL_HS_SRC_POL);
  1137. data->dma_slave = slave;
  1138. if (platform_device_add_data(pdev, data,
  1139. sizeof(struct mci_platform_data)))
  1140. goto fail_free;
  1141. /* CLK line is common to both slots */
  1142. pioa_mask = 1 << 10;
  1143. switch (data->slot[0].bus_width) {
  1144. case 4:
  1145. pioa_mask |= 1 << 13; /* DATA1 */
  1146. pioa_mask |= 1 << 14; /* DATA2 */
  1147. pioa_mask |= 1 << 15; /* DATA3 */
  1148. /* fall through */
  1149. case 1:
  1150. pioa_mask |= 1 << 11; /* CMD */
  1151. pioa_mask |= 1 << 12; /* DATA0 */
  1152. if (gpio_is_valid(data->slot[0].detect_pin))
  1153. at32_select_gpio(data->slot[0].detect_pin, 0);
  1154. if (gpio_is_valid(data->slot[0].wp_pin))
  1155. at32_select_gpio(data->slot[0].wp_pin, 0);
  1156. break;
  1157. case 0:
  1158. /* Slot is unused */
  1159. break;
  1160. default:
  1161. goto fail_free;
  1162. }
  1163. select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
  1164. piob_mask = 0;
  1165. switch (data->slot[1].bus_width) {
  1166. case 4:
  1167. piob_mask |= 1 << 8; /* DATA1 */
  1168. piob_mask |= 1 << 9; /* DATA2 */
  1169. piob_mask |= 1 << 10; /* DATA3 */
  1170. /* fall through */
  1171. case 1:
  1172. piob_mask |= 1 << 6; /* CMD */
  1173. piob_mask |= 1 << 7; /* DATA0 */
  1174. select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
  1175. if (gpio_is_valid(data->slot[1].detect_pin))
  1176. at32_select_gpio(data->slot[1].detect_pin, 0);
  1177. if (gpio_is_valid(data->slot[1].wp_pin))
  1178. at32_select_gpio(data->slot[1].wp_pin, 0);
  1179. break;
  1180. case 0:
  1181. /* Slot is unused */
  1182. break;
  1183. default:
  1184. if (!data->slot[0].bus_width)
  1185. goto fail_free;
  1186. data->slot[1].bus_width = 0;
  1187. break;
  1188. }
  1189. atmel_mci0_pclk.dev = &pdev->dev;
  1190. platform_device_add(pdev);
  1191. return pdev;
  1192. fail_free:
  1193. kfree(slave);
  1194. fail:
  1195. data->dma_slave = NULL;
  1196. platform_device_put(pdev);
  1197. return NULL;
  1198. }
  1199. /* --------------------------------------------------------------------
  1200. * LCDC
  1201. * -------------------------------------------------------------------- */
  1202. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1203. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  1204. static struct resource atmel_lcdfb0_resource[] = {
  1205. {
  1206. .start = 0xff000000,
  1207. .end = 0xff000fff,
  1208. .flags = IORESOURCE_MEM,
  1209. },
  1210. IRQ(1),
  1211. {
  1212. /* Placeholder for pre-allocated fb memory */
  1213. .start = 0x00000000,
  1214. .end = 0x00000000,
  1215. .flags = 0,
  1216. },
  1217. };
  1218. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  1219. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  1220. static struct clk atmel_lcdfb0_pixclk = {
  1221. .name = "lcdc_clk",
  1222. .dev = &atmel_lcdfb0_device.dev,
  1223. .mode = genclk_mode,
  1224. .get_rate = genclk_get_rate,
  1225. .set_rate = genclk_set_rate,
  1226. .set_parent = genclk_set_parent,
  1227. .index = 7,
  1228. };
  1229. struct platform_device *__init
  1230. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  1231. unsigned long fbmem_start, unsigned long fbmem_len,
  1232. u64 pin_mask)
  1233. {
  1234. struct platform_device *pdev;
  1235. struct atmel_lcdfb_info *info;
  1236. struct fb_monspecs *monspecs;
  1237. struct fb_videomode *modedb;
  1238. unsigned int modedb_size;
  1239. u32 portc_mask, portd_mask, porte_mask;
  1240. /*
  1241. * Do a deep copy of the fb data, monspecs and modedb. Make
  1242. * sure all allocations are done before setting up the
  1243. * portmux.
  1244. */
  1245. monspecs = kmemdup(data->default_monspecs,
  1246. sizeof(struct fb_monspecs), GFP_KERNEL);
  1247. if (!monspecs)
  1248. return NULL;
  1249. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  1250. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  1251. if (!modedb)
  1252. goto err_dup_modedb;
  1253. monspecs->modedb = modedb;
  1254. switch (id) {
  1255. case 0:
  1256. pdev = &atmel_lcdfb0_device;
  1257. if (pin_mask == 0ULL)
  1258. /* Default to "full" lcdc control signals and 24bit */
  1259. pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
  1260. /* LCDC on port C */
  1261. portc_mask = pin_mask & 0xfff80000;
  1262. select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
  1263. /* LCDC on port D */
  1264. portd_mask = pin_mask & 0x0003ffff;
  1265. select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
  1266. /* LCDC on port E */
  1267. porte_mask = (pin_mask >> 32) & 0x0007ffff;
  1268. select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
  1269. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  1270. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  1271. break;
  1272. default:
  1273. goto err_invalid_id;
  1274. }
  1275. if (fbmem_len) {
  1276. pdev->resource[2].start = fbmem_start;
  1277. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1278. pdev->resource[2].flags = IORESOURCE_MEM;
  1279. }
  1280. info = pdev->dev.platform_data;
  1281. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  1282. info->default_monspecs = monspecs;
  1283. platform_device_register(pdev);
  1284. return pdev;
  1285. err_invalid_id:
  1286. kfree(modedb);
  1287. err_dup_modedb:
  1288. kfree(monspecs);
  1289. return NULL;
  1290. }
  1291. #endif
  1292. /* --------------------------------------------------------------------
  1293. * PWM
  1294. * -------------------------------------------------------------------- */
  1295. static struct resource atmel_pwm0_resource[] __initdata = {
  1296. PBMEM(0xfff01400),
  1297. IRQ(24),
  1298. };
  1299. static struct clk atmel_pwm0_mck = {
  1300. .name = "pwm_clk",
  1301. .parent = &pbb_clk,
  1302. .mode = pbb_clk_mode,
  1303. .get_rate = pbb_clk_get_rate,
  1304. .index = 5,
  1305. };
  1306. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1307. {
  1308. struct platform_device *pdev;
  1309. u32 pin_mask;
  1310. if (!mask)
  1311. return NULL;
  1312. pdev = platform_device_alloc("atmel_pwm", 0);
  1313. if (!pdev)
  1314. return NULL;
  1315. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1316. ARRAY_SIZE(atmel_pwm0_resource)))
  1317. goto out_free_pdev;
  1318. if (platform_device_add_data(pdev, &mask, sizeof(mask)))
  1319. goto out_free_pdev;
  1320. pin_mask = 0;
  1321. if (mask & (1 << 0))
  1322. pin_mask |= (1 << 28);
  1323. if (mask & (1 << 1))
  1324. pin_mask |= (1 << 29);
  1325. if (pin_mask > 0)
  1326. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1327. pin_mask = 0;
  1328. if (mask & (1 << 2))
  1329. pin_mask |= (1 << 21);
  1330. if (mask & (1 << 3))
  1331. pin_mask |= (1 << 22);
  1332. if (pin_mask > 0)
  1333. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1334. atmel_pwm0_mck.dev = &pdev->dev;
  1335. platform_device_add(pdev);
  1336. return pdev;
  1337. out_free_pdev:
  1338. platform_device_put(pdev);
  1339. return NULL;
  1340. }
  1341. /* --------------------------------------------------------------------
  1342. * SSC
  1343. * -------------------------------------------------------------------- */
  1344. static struct resource ssc0_resource[] = {
  1345. PBMEM(0xffe01c00),
  1346. IRQ(10),
  1347. };
  1348. DEFINE_DEV(ssc, 0);
  1349. DEV_CLK(pclk, ssc0, pba, 7);
  1350. static struct resource ssc1_resource[] = {
  1351. PBMEM(0xffe02000),
  1352. IRQ(11),
  1353. };
  1354. DEFINE_DEV(ssc, 1);
  1355. DEV_CLK(pclk, ssc1, pba, 8);
  1356. static struct resource ssc2_resource[] = {
  1357. PBMEM(0xffe02400),
  1358. IRQ(12),
  1359. };
  1360. DEFINE_DEV(ssc, 2);
  1361. DEV_CLK(pclk, ssc2, pba, 9);
  1362. struct platform_device *__init
  1363. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1364. {
  1365. struct platform_device *pdev;
  1366. u32 pin_mask = 0;
  1367. switch (id) {
  1368. case 0:
  1369. pdev = &ssc0_device;
  1370. if (flags & ATMEL_SSC_RF)
  1371. pin_mask |= (1 << 21); /* RF */
  1372. if (flags & ATMEL_SSC_RK)
  1373. pin_mask |= (1 << 22); /* RK */
  1374. if (flags & ATMEL_SSC_TK)
  1375. pin_mask |= (1 << 23); /* TK */
  1376. if (flags & ATMEL_SSC_TF)
  1377. pin_mask |= (1 << 24); /* TF */
  1378. if (flags & ATMEL_SSC_TD)
  1379. pin_mask |= (1 << 25); /* TD */
  1380. if (flags & ATMEL_SSC_RD)
  1381. pin_mask |= (1 << 26); /* RD */
  1382. if (pin_mask > 0)
  1383. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1384. break;
  1385. case 1:
  1386. pdev = &ssc1_device;
  1387. if (flags & ATMEL_SSC_RF)
  1388. pin_mask |= (1 << 0); /* RF */
  1389. if (flags & ATMEL_SSC_RK)
  1390. pin_mask |= (1 << 1); /* RK */
  1391. if (flags & ATMEL_SSC_TK)
  1392. pin_mask |= (1 << 2); /* TK */
  1393. if (flags & ATMEL_SSC_TF)
  1394. pin_mask |= (1 << 3); /* TF */
  1395. if (flags & ATMEL_SSC_TD)
  1396. pin_mask |= (1 << 4); /* TD */
  1397. if (flags & ATMEL_SSC_RD)
  1398. pin_mask |= (1 << 5); /* RD */
  1399. if (pin_mask > 0)
  1400. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1401. break;
  1402. case 2:
  1403. pdev = &ssc2_device;
  1404. if (flags & ATMEL_SSC_TD)
  1405. pin_mask |= (1 << 13); /* TD */
  1406. if (flags & ATMEL_SSC_RD)
  1407. pin_mask |= (1 << 14); /* RD */
  1408. if (flags & ATMEL_SSC_TK)
  1409. pin_mask |= (1 << 15); /* TK */
  1410. if (flags & ATMEL_SSC_TF)
  1411. pin_mask |= (1 << 16); /* TF */
  1412. if (flags & ATMEL_SSC_RF)
  1413. pin_mask |= (1 << 17); /* RF */
  1414. if (flags & ATMEL_SSC_RK)
  1415. pin_mask |= (1 << 18); /* RK */
  1416. if (pin_mask > 0)
  1417. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1418. break;
  1419. default:
  1420. return NULL;
  1421. }
  1422. platform_device_register(pdev);
  1423. return pdev;
  1424. }
  1425. /* --------------------------------------------------------------------
  1426. * USB Device Controller
  1427. * -------------------------------------------------------------------- */
  1428. static struct resource usba0_resource[] __initdata = {
  1429. {
  1430. .start = 0xff300000,
  1431. .end = 0xff3fffff,
  1432. .flags = IORESOURCE_MEM,
  1433. }, {
  1434. .start = 0xfff03000,
  1435. .end = 0xfff033ff,
  1436. .flags = IORESOURCE_MEM,
  1437. },
  1438. IRQ(31),
  1439. };
  1440. static struct clk usba0_pclk = {
  1441. .name = "pclk",
  1442. .parent = &pbb_clk,
  1443. .mode = pbb_clk_mode,
  1444. .get_rate = pbb_clk_get_rate,
  1445. .index = 12,
  1446. };
  1447. static struct clk usba0_hclk = {
  1448. .name = "hclk",
  1449. .parent = &hsb_clk,
  1450. .mode = hsb_clk_mode,
  1451. .get_rate = hsb_clk_get_rate,
  1452. .index = 6,
  1453. };
  1454. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1455. [idx] = { \
  1456. .name = nam, \
  1457. .index = idx, \
  1458. .fifo_size = maxpkt, \
  1459. .nr_banks = maxbk, \
  1460. .can_dma = dma, \
  1461. .can_isoc = isoc, \
  1462. }
  1463. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1464. EP("ep0", 0, 64, 1, 0, 0),
  1465. EP("ep1", 1, 512, 2, 1, 1),
  1466. EP("ep2", 2, 512, 2, 1, 1),
  1467. EP("ep3-int", 3, 64, 3, 1, 0),
  1468. EP("ep4-int", 4, 64, 3, 1, 0),
  1469. EP("ep5", 5, 1024, 3, 1, 1),
  1470. EP("ep6", 6, 1024, 3, 1, 1),
  1471. };
  1472. #undef EP
  1473. struct platform_device *__init
  1474. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1475. {
  1476. /*
  1477. * pdata doesn't have room for any endpoints, so we need to
  1478. * append room for the ones we need right after it.
  1479. */
  1480. struct {
  1481. struct usba_platform_data pdata;
  1482. struct usba_ep_data ep[7];
  1483. } usba_data;
  1484. struct platform_device *pdev;
  1485. if (id != 0)
  1486. return NULL;
  1487. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1488. if (!pdev)
  1489. return NULL;
  1490. if (platform_device_add_resources(pdev, usba0_resource,
  1491. ARRAY_SIZE(usba0_resource)))
  1492. goto out_free_pdev;
  1493. if (data) {
  1494. usba_data.pdata.vbus_pin = data->vbus_pin;
  1495. usba_data.pdata.vbus_pin_inverted = data->vbus_pin_inverted;
  1496. } else {
  1497. usba_data.pdata.vbus_pin = -EINVAL;
  1498. usba_data.pdata.vbus_pin_inverted = -EINVAL;
  1499. }
  1500. data = &usba_data.pdata;
  1501. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1502. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1503. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1504. goto out_free_pdev;
  1505. if (gpio_is_valid(data->vbus_pin))
  1506. at32_select_gpio(data->vbus_pin, 0);
  1507. usba0_pclk.dev = &pdev->dev;
  1508. usba0_hclk.dev = &pdev->dev;
  1509. platform_device_add(pdev);
  1510. return pdev;
  1511. out_free_pdev:
  1512. platform_device_put(pdev);
  1513. return NULL;
  1514. }
  1515. /* --------------------------------------------------------------------
  1516. * IDE / CompactFlash
  1517. * -------------------------------------------------------------------- */
  1518. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1519. static struct resource at32_smc_cs4_resource[] __initdata = {
  1520. {
  1521. .start = 0x04000000,
  1522. .end = 0x07ffffff,
  1523. .flags = IORESOURCE_MEM,
  1524. },
  1525. IRQ(~0UL), /* Magic IRQ will be overridden */
  1526. };
  1527. static struct resource at32_smc_cs5_resource[] __initdata = {
  1528. {
  1529. .start = 0x20000000,
  1530. .end = 0x23ffffff,
  1531. .flags = IORESOURCE_MEM,
  1532. },
  1533. IRQ(~0UL), /* Magic IRQ will be overridden */
  1534. };
  1535. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1536. unsigned int cs, unsigned int extint)
  1537. {
  1538. static unsigned int extint_pin_map[4] __initdata = {
  1539. (1 << 25),
  1540. (1 << 26),
  1541. (1 << 27),
  1542. (1 << 28),
  1543. };
  1544. static bool common_pins_initialized __initdata = false;
  1545. unsigned int extint_pin;
  1546. int ret;
  1547. u32 pin_mask;
  1548. if (extint >= ARRAY_SIZE(extint_pin_map))
  1549. return -EINVAL;
  1550. extint_pin = extint_pin_map[extint];
  1551. switch (cs) {
  1552. case 4:
  1553. ret = platform_device_add_resources(pdev,
  1554. at32_smc_cs4_resource,
  1555. ARRAY_SIZE(at32_smc_cs4_resource));
  1556. if (ret)
  1557. return ret;
  1558. /* NCS4 -> OE_N */
  1559. select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
  1560. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
  1561. break;
  1562. case 5:
  1563. ret = platform_device_add_resources(pdev,
  1564. at32_smc_cs5_resource,
  1565. ARRAY_SIZE(at32_smc_cs5_resource));
  1566. if (ret)
  1567. return ret;
  1568. /* NCS5 -> OE_N */
  1569. select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
  1570. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
  1571. break;
  1572. default:
  1573. return -EINVAL;
  1574. }
  1575. if (!common_pins_initialized) {
  1576. pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
  1577. pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
  1578. pin_mask |= (1 << 23); /* CFRNW -> DIR */
  1579. pin_mask |= (1 << 24); /* NWAIT <- IORDY */
  1580. select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
  1581. common_pins_initialized = true;
  1582. }
  1583. select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
  1584. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1585. pdev->resource[1].end = pdev->resource[1].start;
  1586. return 0;
  1587. }
  1588. struct platform_device *__init
  1589. at32_add_device_ide(unsigned int id, unsigned int extint,
  1590. struct ide_platform_data *data)
  1591. {
  1592. struct platform_device *pdev;
  1593. pdev = platform_device_alloc("at32_ide", id);
  1594. if (!pdev)
  1595. goto fail;
  1596. if (platform_device_add_data(pdev, data,
  1597. sizeof(struct ide_platform_data)))
  1598. goto fail;
  1599. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1600. goto fail;
  1601. platform_device_add(pdev);
  1602. return pdev;
  1603. fail:
  1604. platform_device_put(pdev);
  1605. return NULL;
  1606. }
  1607. struct platform_device *__init
  1608. at32_add_device_cf(unsigned int id, unsigned int extint,
  1609. struct cf_platform_data *data)
  1610. {
  1611. struct platform_device *pdev;
  1612. pdev = platform_device_alloc("at32_cf", id);
  1613. if (!pdev)
  1614. goto fail;
  1615. if (platform_device_add_data(pdev, data,
  1616. sizeof(struct cf_platform_data)))
  1617. goto fail;
  1618. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1619. goto fail;
  1620. if (gpio_is_valid(data->detect_pin))
  1621. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1622. if (gpio_is_valid(data->reset_pin))
  1623. at32_select_gpio(data->reset_pin, 0);
  1624. if (gpio_is_valid(data->vcc_pin))
  1625. at32_select_gpio(data->vcc_pin, 0);
  1626. /* READY is used as extint, so we can't select it as gpio */
  1627. platform_device_add(pdev);
  1628. return pdev;
  1629. fail:
  1630. platform_device_put(pdev);
  1631. return NULL;
  1632. }
  1633. #endif
  1634. /* --------------------------------------------------------------------
  1635. * NAND Flash / SmartMedia
  1636. * -------------------------------------------------------------------- */
  1637. static struct resource smc_cs3_resource[] __initdata = {
  1638. {
  1639. .start = 0x0c000000,
  1640. .end = 0x0fffffff,
  1641. .flags = IORESOURCE_MEM,
  1642. }, {
  1643. .start = 0xfff03c00,
  1644. .end = 0xfff03fff,
  1645. .flags = IORESOURCE_MEM,
  1646. },
  1647. };
  1648. struct platform_device *__init
  1649. at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
  1650. {
  1651. struct platform_device *pdev;
  1652. if (id != 0 || !data)
  1653. return NULL;
  1654. pdev = platform_device_alloc("atmel_nand", id);
  1655. if (!pdev)
  1656. goto fail;
  1657. if (platform_device_add_resources(pdev, smc_cs3_resource,
  1658. ARRAY_SIZE(smc_cs3_resource)))
  1659. goto fail;
  1660. if (platform_device_add_data(pdev, data,
  1661. sizeof(struct atmel_nand_data)))
  1662. goto fail;
  1663. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
  1664. if (data->enable_pin)
  1665. at32_select_gpio(data->enable_pin,
  1666. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  1667. if (data->rdy_pin)
  1668. at32_select_gpio(data->rdy_pin, 0);
  1669. if (data->det_pin)
  1670. at32_select_gpio(data->det_pin, 0);
  1671. platform_device_add(pdev);
  1672. return pdev;
  1673. fail:
  1674. platform_device_put(pdev);
  1675. return NULL;
  1676. }
  1677. /* --------------------------------------------------------------------
  1678. * AC97C
  1679. * -------------------------------------------------------------------- */
  1680. static struct resource atmel_ac97c0_resource[] __initdata = {
  1681. PBMEM(0xfff02800),
  1682. IRQ(29),
  1683. };
  1684. static struct clk atmel_ac97c0_pclk = {
  1685. .name = "pclk",
  1686. .parent = &pbb_clk,
  1687. .mode = pbb_clk_mode,
  1688. .get_rate = pbb_clk_get_rate,
  1689. .index = 10,
  1690. };
  1691. struct platform_device *__init
  1692. at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data,
  1693. unsigned int flags)
  1694. {
  1695. struct platform_device *pdev;
  1696. struct dw_dma_slave *rx_dws;
  1697. struct dw_dma_slave *tx_dws;
  1698. struct ac97c_platform_data _data;
  1699. u32 pin_mask;
  1700. if (id != 0)
  1701. return NULL;
  1702. pdev = platform_device_alloc("atmel_ac97c", id);
  1703. if (!pdev)
  1704. return NULL;
  1705. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1706. ARRAY_SIZE(atmel_ac97c0_resource)))
  1707. goto out_free_resources;
  1708. if (!data) {
  1709. data = &_data;
  1710. memset(data, 0, sizeof(struct ac97c_platform_data));
  1711. data->reset_pin = -ENODEV;
  1712. }
  1713. rx_dws = &data->rx_dws;
  1714. tx_dws = &data->tx_dws;
  1715. /* Check if DMA slave interface for capture should be configured. */
  1716. if (flags & AC97C_CAPTURE) {
  1717. rx_dws->dma_dev = &dw_dmac0_device.dev;
  1718. rx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
  1719. rx_dws->cfg_hi = DWC_CFGH_SRC_PER(3);
  1720. rx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1721. }
  1722. /* Check if DMA slave interface for playback should be configured. */
  1723. if (flags & AC97C_PLAYBACK) {
  1724. tx_dws->dma_dev = &dw_dmac0_device.dev;
  1725. tx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
  1726. tx_dws->cfg_hi = DWC_CFGH_DST_PER(4);
  1727. tx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1728. }
  1729. if (platform_device_add_data(pdev, data,
  1730. sizeof(struct ac97c_platform_data)))
  1731. goto out_free_resources;
  1732. /* SDO | SYNC | SCLK | SDI */
  1733. pin_mask = (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
  1734. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1735. if (gpio_is_valid(data->reset_pin))
  1736. at32_select_gpio(data->reset_pin, AT32_GPIOF_OUTPUT
  1737. | AT32_GPIOF_HIGH);
  1738. atmel_ac97c0_pclk.dev = &pdev->dev;
  1739. platform_device_add(pdev);
  1740. return pdev;
  1741. out_free_resources:
  1742. platform_device_put(pdev);
  1743. return NULL;
  1744. }
  1745. /* --------------------------------------------------------------------
  1746. * ABDAC
  1747. * -------------------------------------------------------------------- */
  1748. static struct resource abdac0_resource[] __initdata = {
  1749. PBMEM(0xfff02000),
  1750. IRQ(27),
  1751. };
  1752. static struct clk abdac0_pclk = {
  1753. .name = "pclk",
  1754. .parent = &pbb_clk,
  1755. .mode = pbb_clk_mode,
  1756. .get_rate = pbb_clk_get_rate,
  1757. .index = 8,
  1758. };
  1759. static struct clk abdac0_sample_clk = {
  1760. .name = "sample_clk",
  1761. .mode = genclk_mode,
  1762. .get_rate = genclk_get_rate,
  1763. .set_rate = genclk_set_rate,
  1764. .set_parent = genclk_set_parent,
  1765. .index = 6,
  1766. };
  1767. struct platform_device *__init
  1768. at32_add_device_abdac(unsigned int id, struct atmel_abdac_pdata *data)
  1769. {
  1770. struct platform_device *pdev;
  1771. struct dw_dma_slave *dws;
  1772. u32 pin_mask;
  1773. if (id != 0 || !data)
  1774. return NULL;
  1775. pdev = platform_device_alloc("atmel_abdac", id);
  1776. if (!pdev)
  1777. return NULL;
  1778. if (platform_device_add_resources(pdev, abdac0_resource,
  1779. ARRAY_SIZE(abdac0_resource)))
  1780. goto out_free_resources;
  1781. dws = &data->dws;
  1782. dws->dma_dev = &dw_dmac0_device.dev;
  1783. dws->reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
  1784. dws->cfg_hi = DWC_CFGH_DST_PER(2);
  1785. dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1786. if (platform_device_add_data(pdev, data,
  1787. sizeof(struct atmel_abdac_pdata)))
  1788. goto out_free_resources;
  1789. pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
  1790. pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
  1791. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1792. abdac0_pclk.dev = &pdev->dev;
  1793. abdac0_sample_clk.dev = &pdev->dev;
  1794. platform_device_add(pdev);
  1795. return pdev;
  1796. out_free_resources:
  1797. platform_device_put(pdev);
  1798. return NULL;
  1799. }
  1800. /* --------------------------------------------------------------------
  1801. * GCLK
  1802. * -------------------------------------------------------------------- */
  1803. static struct clk gclk0 = {
  1804. .name = "gclk0",
  1805. .mode = genclk_mode,
  1806. .get_rate = genclk_get_rate,
  1807. .set_rate = genclk_set_rate,
  1808. .set_parent = genclk_set_parent,
  1809. .index = 0,
  1810. };
  1811. static struct clk gclk1 = {
  1812. .name = "gclk1",
  1813. .mode = genclk_mode,
  1814. .get_rate = genclk_get_rate,
  1815. .set_rate = genclk_set_rate,
  1816. .set_parent = genclk_set_parent,
  1817. .index = 1,
  1818. };
  1819. static struct clk gclk2 = {
  1820. .name = "gclk2",
  1821. .mode = genclk_mode,
  1822. .get_rate = genclk_get_rate,
  1823. .set_rate = genclk_set_rate,
  1824. .set_parent = genclk_set_parent,
  1825. .index = 2,
  1826. };
  1827. static struct clk gclk3 = {
  1828. .name = "gclk3",
  1829. .mode = genclk_mode,
  1830. .get_rate = genclk_get_rate,
  1831. .set_rate = genclk_set_rate,
  1832. .set_parent = genclk_set_parent,
  1833. .index = 3,
  1834. };
  1835. static struct clk gclk4 = {
  1836. .name = "gclk4",
  1837. .mode = genclk_mode,
  1838. .get_rate = genclk_get_rate,
  1839. .set_rate = genclk_set_rate,
  1840. .set_parent = genclk_set_parent,
  1841. .index = 4,
  1842. };
  1843. static __initdata struct clk *init_clocks[] = {
  1844. &osc32k,
  1845. &osc0,
  1846. &osc1,
  1847. &pll0,
  1848. &pll1,
  1849. &cpu_clk,
  1850. &hsb_clk,
  1851. &pba_clk,
  1852. &pbb_clk,
  1853. &at32_pm_pclk,
  1854. &at32_intc0_pclk,
  1855. &at32_hmatrix_clk,
  1856. &ebi_clk,
  1857. &hramc_clk,
  1858. &sdramc_clk,
  1859. &smc0_pclk,
  1860. &smc0_mck,
  1861. &pdc_hclk,
  1862. &pdc_pclk,
  1863. &dw_dmac0_hclk,
  1864. &pico_clk,
  1865. &pio0_mck,
  1866. &pio1_mck,
  1867. &pio2_mck,
  1868. &pio3_mck,
  1869. &pio4_mck,
  1870. &at32_tcb0_t0_clk,
  1871. &at32_tcb1_t0_clk,
  1872. &atmel_psif0_pclk,
  1873. &atmel_psif1_pclk,
  1874. &atmel_usart0_usart,
  1875. &atmel_usart1_usart,
  1876. &atmel_usart2_usart,
  1877. &atmel_usart3_usart,
  1878. &atmel_pwm0_mck,
  1879. #if defined(CONFIG_CPU_AT32AP7000)
  1880. &macb0_hclk,
  1881. &macb0_pclk,
  1882. &macb1_hclk,
  1883. &macb1_pclk,
  1884. #endif
  1885. &atmel_spi0_spi_clk,
  1886. &atmel_spi1_spi_clk,
  1887. &atmel_twi0_pclk,
  1888. &atmel_mci0_pclk,
  1889. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1890. &atmel_lcdfb0_hck1,
  1891. &atmel_lcdfb0_pixclk,
  1892. #endif
  1893. &ssc0_pclk,
  1894. &ssc1_pclk,
  1895. &ssc2_pclk,
  1896. &usba0_hclk,
  1897. &usba0_pclk,
  1898. &atmel_ac97c0_pclk,
  1899. &abdac0_pclk,
  1900. &abdac0_sample_clk,
  1901. &gclk0,
  1902. &gclk1,
  1903. &gclk2,
  1904. &gclk3,
  1905. &gclk4,
  1906. };
  1907. void __init setup_platform(void)
  1908. {
  1909. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1910. int i;
  1911. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1912. main_clock = &pll0;
  1913. cpu_clk.parent = &pll0;
  1914. } else {
  1915. main_clock = &osc0;
  1916. cpu_clk.parent = &osc0;
  1917. }
  1918. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1919. pll0.parent = &osc1;
  1920. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1921. pll1.parent = &osc1;
  1922. genclk_init_parent(&gclk0);
  1923. genclk_init_parent(&gclk1);
  1924. genclk_init_parent(&gclk2);
  1925. genclk_init_parent(&gclk3);
  1926. genclk_init_parent(&gclk4);
  1927. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1928. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1929. #endif
  1930. genclk_init_parent(&abdac0_sample_clk);
  1931. /*
  1932. * Build initial dynamic clock list by registering all clocks
  1933. * from the array.
  1934. * At the same time, turn on all clocks that have at least one
  1935. * user already, and turn off everything else. We only do this
  1936. * for module clocks, and even though it isn't particularly
  1937. * pretty to check the address of the mode function, it should
  1938. * do the trick...
  1939. */
  1940. for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
  1941. struct clk *clk = init_clocks[i];
  1942. /* first, register clock */
  1943. at32_clk_register(clk);
  1944. if (clk->users == 0)
  1945. continue;
  1946. if (clk->mode == &cpu_clk_mode)
  1947. cpu_mask |= 1 << clk->index;
  1948. else if (clk->mode == &hsb_clk_mode)
  1949. hsb_mask |= 1 << clk->index;
  1950. else if (clk->mode == &pba_clk_mode)
  1951. pba_mask |= 1 << clk->index;
  1952. else if (clk->mode == &pbb_clk_mode)
  1953. pbb_mask |= 1 << clk->index;
  1954. }
  1955. pm_writel(CPU_MASK, cpu_mask);
  1956. pm_writel(HSB_MASK, hsb_mask);
  1957. pm_writel(PBA_MASK, pba_mask);
  1958. pm_writel(PBB_MASK, pbb_mask);
  1959. /* Initialize the port muxes */
  1960. at32_init_pio(&pio0_device);
  1961. at32_init_pio(&pio1_device);
  1962. at32_init_pio(&pio2_device);
  1963. at32_init_pio(&pio3_device);
  1964. at32_init_pio(&pio4_device);
  1965. }
  1966. struct gen_pool *sram_pool;
  1967. static int __init sram_init(void)
  1968. {
  1969. struct gen_pool *pool;
  1970. /* 1KiB granularity */
  1971. pool = gen_pool_create(10, -1);
  1972. if (!pool)
  1973. goto fail;
  1974. if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
  1975. goto err_pool_add;
  1976. sram_pool = pool;
  1977. return 0;
  1978. err_pool_add:
  1979. gen_pool_destroy(pool);
  1980. fail:
  1981. pr_err("Failed to create SRAM pool\n");
  1982. return -ENOMEM;
  1983. }
  1984. core_initcall(sram_init);