at32ap700x.c 54 KB

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