at32ap700x.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  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/platform_data/dma-dw.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 <linux/platform_data/mmc-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. .block_size = 4095U,
  499. .nr_masters = 2,
  500. .data_width = { 2, 2 },
  501. };
  502. static struct resource dw_dmac0_resource[] = {
  503. PBMEM(0xff200000),
  504. IRQ(2),
  505. };
  506. DEFINE_DEV_DATA(dw_dmac, 0);
  507. DEV_CLK(hclk, dw_dmac0, hsb, 10);
  508. /* --------------------------------------------------------------------
  509. * System peripherals
  510. * -------------------------------------------------------------------- */
  511. static struct resource at32_pm0_resource[] = {
  512. {
  513. .start = 0xfff00000,
  514. .end = 0xfff0007f,
  515. .flags = IORESOURCE_MEM,
  516. },
  517. IRQ(20),
  518. };
  519. static struct resource at32ap700x_rtc0_resource[] = {
  520. {
  521. .start = 0xfff00080,
  522. .end = 0xfff000af,
  523. .flags = IORESOURCE_MEM,
  524. },
  525. IRQ(21),
  526. };
  527. static struct resource at32_wdt0_resource[] = {
  528. {
  529. .start = 0xfff000b0,
  530. .end = 0xfff000cf,
  531. .flags = IORESOURCE_MEM,
  532. },
  533. };
  534. static struct resource at32_eic0_resource[] = {
  535. {
  536. .start = 0xfff00100,
  537. .end = 0xfff0013f,
  538. .flags = IORESOURCE_MEM,
  539. },
  540. IRQ(19),
  541. };
  542. DEFINE_DEV(at32_pm, 0);
  543. DEFINE_DEV(at32ap700x_rtc, 0);
  544. DEFINE_DEV(at32_wdt, 0);
  545. DEFINE_DEV(at32_eic, 0);
  546. /*
  547. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  548. * is always running.
  549. */
  550. static struct clk at32_pm_pclk = {
  551. .name = "pclk",
  552. .dev = &at32_pm0_device.dev,
  553. .parent = &pbb_clk,
  554. .mode = pbb_clk_mode,
  555. .get_rate = pbb_clk_get_rate,
  556. .users = 1,
  557. .index = 0,
  558. };
  559. static struct resource intc0_resource[] = {
  560. PBMEM(0xfff00400),
  561. };
  562. struct platform_device at32_intc0_device = {
  563. .name = "intc",
  564. .id = 0,
  565. .resource = intc0_resource,
  566. .num_resources = ARRAY_SIZE(intc0_resource),
  567. };
  568. DEV_CLK(pclk, at32_intc0, pbb, 1);
  569. static struct clk ebi_clk = {
  570. .name = "ebi",
  571. .parent = &hsb_clk,
  572. .mode = hsb_clk_mode,
  573. .get_rate = hsb_clk_get_rate,
  574. .users = 1,
  575. };
  576. static struct clk hramc_clk = {
  577. .name = "hramc",
  578. .parent = &hsb_clk,
  579. .mode = hsb_clk_mode,
  580. .get_rate = hsb_clk_get_rate,
  581. .users = 1,
  582. .index = 3,
  583. };
  584. static struct clk sdramc_clk = {
  585. .name = "sdramc_clk",
  586. .parent = &pbb_clk,
  587. .mode = pbb_clk_mode,
  588. .get_rate = pbb_clk_get_rate,
  589. .users = 1,
  590. .index = 14,
  591. };
  592. static struct resource smc0_resource[] = {
  593. PBMEM(0xfff03400),
  594. };
  595. DEFINE_DEV(smc, 0);
  596. DEV_CLK(pclk, smc0, pbb, 13);
  597. DEV_CLK(mck, smc0, hsb, 0);
  598. static struct platform_device pdc_device = {
  599. .name = "pdc",
  600. .id = 0,
  601. };
  602. DEV_CLK(hclk, pdc, hsb, 4);
  603. DEV_CLK(pclk, pdc, pba, 16);
  604. static struct clk pico_clk = {
  605. .name = "pico",
  606. .parent = &cpu_clk,
  607. .mode = cpu_clk_mode,
  608. .get_rate = cpu_clk_get_rate,
  609. .users = 1,
  610. };
  611. /* --------------------------------------------------------------------
  612. * HMATRIX
  613. * -------------------------------------------------------------------- */
  614. struct clk at32_hmatrix_clk = {
  615. .name = "hmatrix_clk",
  616. .parent = &pbb_clk,
  617. .mode = pbb_clk_mode,
  618. .get_rate = pbb_clk_get_rate,
  619. .index = 2,
  620. .users = 1,
  621. };
  622. /*
  623. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  624. * External Bus Interface (EBI). This can be used to enable special
  625. * features like CompactFlash support, NAND Flash support, etc. on
  626. * certain chipselects.
  627. */
  628. static inline void set_ebi_sfr_bits(u32 mask)
  629. {
  630. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);
  631. }
  632. /* --------------------------------------------------------------------
  633. * Timer/Counter (TC)
  634. * -------------------------------------------------------------------- */
  635. static struct resource at32_tcb0_resource[] = {
  636. PBMEM(0xfff00c00),
  637. IRQ(22),
  638. };
  639. static struct platform_device at32_tcb0_device = {
  640. .name = "atmel_tcb",
  641. .id = 0,
  642. .resource = at32_tcb0_resource,
  643. .num_resources = ARRAY_SIZE(at32_tcb0_resource),
  644. };
  645. DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
  646. static struct resource at32_tcb1_resource[] = {
  647. PBMEM(0xfff01000),
  648. IRQ(23),
  649. };
  650. static struct platform_device at32_tcb1_device = {
  651. .name = "atmel_tcb",
  652. .id = 1,
  653. .resource = at32_tcb1_resource,
  654. .num_resources = ARRAY_SIZE(at32_tcb1_resource),
  655. };
  656. DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
  657. /* --------------------------------------------------------------------
  658. * PIO
  659. * -------------------------------------------------------------------- */
  660. static struct resource pio0_resource[] = {
  661. PBMEM(0xffe02800),
  662. IRQ(13),
  663. };
  664. DEFINE_DEV(pio, 0);
  665. DEV_CLK(mck, pio0, pba, 10);
  666. static struct resource pio1_resource[] = {
  667. PBMEM(0xffe02c00),
  668. IRQ(14),
  669. };
  670. DEFINE_DEV(pio, 1);
  671. DEV_CLK(mck, pio1, pba, 11);
  672. static struct resource pio2_resource[] = {
  673. PBMEM(0xffe03000),
  674. IRQ(15),
  675. };
  676. DEFINE_DEV(pio, 2);
  677. DEV_CLK(mck, pio2, pba, 12);
  678. static struct resource pio3_resource[] = {
  679. PBMEM(0xffe03400),
  680. IRQ(16),
  681. };
  682. DEFINE_DEV(pio, 3);
  683. DEV_CLK(mck, pio3, pba, 13);
  684. static struct resource pio4_resource[] = {
  685. PBMEM(0xffe03800),
  686. IRQ(17),
  687. };
  688. DEFINE_DEV(pio, 4);
  689. DEV_CLK(mck, pio4, pba, 14);
  690. static int __init system_device_init(void)
  691. {
  692. platform_device_register(&at32_pm0_device);
  693. platform_device_register(&at32_intc0_device);
  694. platform_device_register(&at32ap700x_rtc0_device);
  695. platform_device_register(&at32_wdt0_device);
  696. platform_device_register(&at32_eic0_device);
  697. platform_device_register(&smc0_device);
  698. platform_device_register(&pdc_device);
  699. platform_device_register(&dw_dmac0_device);
  700. platform_device_register(&at32_tcb0_device);
  701. platform_device_register(&at32_tcb1_device);
  702. platform_device_register(&pio0_device);
  703. platform_device_register(&pio1_device);
  704. platform_device_register(&pio2_device);
  705. platform_device_register(&pio3_device);
  706. platform_device_register(&pio4_device);
  707. return 0;
  708. }
  709. core_initcall(system_device_init);
  710. /* --------------------------------------------------------------------
  711. * PSIF
  712. * -------------------------------------------------------------------- */
  713. static struct resource atmel_psif0_resource[] __initdata = {
  714. {
  715. .start = 0xffe03c00,
  716. .end = 0xffe03cff,
  717. .flags = IORESOURCE_MEM,
  718. },
  719. IRQ(18),
  720. };
  721. static struct clk atmel_psif0_pclk = {
  722. .name = "pclk",
  723. .parent = &pba_clk,
  724. .mode = pba_clk_mode,
  725. .get_rate = pba_clk_get_rate,
  726. .index = 15,
  727. };
  728. static struct resource atmel_psif1_resource[] __initdata = {
  729. {
  730. .start = 0xffe03d00,
  731. .end = 0xffe03dff,
  732. .flags = IORESOURCE_MEM,
  733. },
  734. IRQ(18),
  735. };
  736. static struct clk atmel_psif1_pclk = {
  737. .name = "pclk",
  738. .parent = &pba_clk,
  739. .mode = pba_clk_mode,
  740. .get_rate = pba_clk_get_rate,
  741. .index = 15,
  742. };
  743. struct platform_device *__init at32_add_device_psif(unsigned int id)
  744. {
  745. struct platform_device *pdev;
  746. u32 pin_mask;
  747. if (!(id == 0 || id == 1))
  748. return NULL;
  749. pdev = platform_device_alloc("atmel_psif", id);
  750. if (!pdev)
  751. return NULL;
  752. switch (id) {
  753. case 0:
  754. pin_mask = (1 << 8) | (1 << 9); /* CLOCK & DATA */
  755. if (platform_device_add_resources(pdev, atmel_psif0_resource,
  756. ARRAY_SIZE(atmel_psif0_resource)))
  757. goto err_add_resources;
  758. atmel_psif0_pclk.dev = &pdev->dev;
  759. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  760. break;
  761. case 1:
  762. pin_mask = (1 << 11) | (1 << 12); /* CLOCK & DATA */
  763. if (platform_device_add_resources(pdev, atmel_psif1_resource,
  764. ARRAY_SIZE(atmel_psif1_resource)))
  765. goto err_add_resources;
  766. atmel_psif1_pclk.dev = &pdev->dev;
  767. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  768. break;
  769. default:
  770. return NULL;
  771. }
  772. platform_device_add(pdev);
  773. return pdev;
  774. err_add_resources:
  775. platform_device_put(pdev);
  776. return NULL;
  777. }
  778. /* --------------------------------------------------------------------
  779. * USART
  780. * -------------------------------------------------------------------- */
  781. static struct atmel_uart_data atmel_usart0_data = {
  782. .use_dma_tx = 1,
  783. .use_dma_rx = 1,
  784. };
  785. static struct resource atmel_usart0_resource[] = {
  786. PBMEM(0xffe00c00),
  787. IRQ(6),
  788. };
  789. DEFINE_DEV_DATA(atmel_usart, 0);
  790. DEV_CLK(usart, atmel_usart0, pba, 3);
  791. static struct atmel_uart_data atmel_usart1_data = {
  792. .use_dma_tx = 1,
  793. .use_dma_rx = 1,
  794. };
  795. static struct resource atmel_usart1_resource[] = {
  796. PBMEM(0xffe01000),
  797. IRQ(7),
  798. };
  799. DEFINE_DEV_DATA(atmel_usart, 1);
  800. DEV_CLK(usart, atmel_usart1, pba, 4);
  801. static struct atmel_uart_data atmel_usart2_data = {
  802. .use_dma_tx = 1,
  803. .use_dma_rx = 1,
  804. };
  805. static struct resource atmel_usart2_resource[] = {
  806. PBMEM(0xffe01400),
  807. IRQ(8),
  808. };
  809. DEFINE_DEV_DATA(atmel_usart, 2);
  810. DEV_CLK(usart, atmel_usart2, pba, 5);
  811. static struct atmel_uart_data atmel_usart3_data = {
  812. .use_dma_tx = 1,
  813. .use_dma_rx = 1,
  814. };
  815. static struct resource atmel_usart3_resource[] = {
  816. PBMEM(0xffe01800),
  817. IRQ(9),
  818. };
  819. DEFINE_DEV_DATA(atmel_usart, 3);
  820. DEV_CLK(usart, atmel_usart3, pba, 6);
  821. static inline void configure_usart0_pins(int flags)
  822. {
  823. u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
  824. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 6);
  825. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 7);
  826. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 10);
  827. select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  828. }
  829. static inline void configure_usart1_pins(int flags)
  830. {
  831. u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
  832. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 19);
  833. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 20);
  834. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 16);
  835. select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
  836. }
  837. static inline void configure_usart2_pins(int flags)
  838. {
  839. u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
  840. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 30);
  841. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 29);
  842. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 28);
  843. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  844. }
  845. static inline void configure_usart3_pins(int flags)
  846. {
  847. u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
  848. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 16);
  849. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 15);
  850. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 19);
  851. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  852. }
  853. static struct platform_device *__initdata at32_usarts[4];
  854. void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags)
  855. {
  856. struct platform_device *pdev;
  857. struct atmel_uart_data *pdata;
  858. switch (hw_id) {
  859. case 0:
  860. pdev = &atmel_usart0_device;
  861. configure_usart0_pins(flags);
  862. break;
  863. case 1:
  864. pdev = &atmel_usart1_device;
  865. configure_usart1_pins(flags);
  866. break;
  867. case 2:
  868. pdev = &atmel_usart2_device;
  869. configure_usart2_pins(flags);
  870. break;
  871. case 3:
  872. pdev = &atmel_usart3_device;
  873. configure_usart3_pins(flags);
  874. break;
  875. default:
  876. return;
  877. }
  878. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  879. /* Addresses in the P4 segment are permanently mapped 1:1 */
  880. struct atmel_uart_data *data = pdev->dev.platform_data;
  881. data->regs = (void __iomem *)pdev->resource[0].start;
  882. }
  883. pdev->id = line;
  884. pdata = pdev->dev.platform_data;
  885. pdata->num = line;
  886. at32_usarts[line] = pdev;
  887. }
  888. struct platform_device *__init at32_add_device_usart(unsigned int id)
  889. {
  890. platform_device_register(at32_usarts[id]);
  891. return at32_usarts[id];
  892. }
  893. void __init at32_setup_serial_console(unsigned int usart_id)
  894. {
  895. #ifdef CONFIG_SERIAL_ATMEL
  896. atmel_default_console_device = at32_usarts[usart_id];
  897. #endif
  898. }
  899. /* --------------------------------------------------------------------
  900. * Ethernet
  901. * -------------------------------------------------------------------- */
  902. #ifdef CONFIG_CPU_AT32AP7000
  903. static struct macb_platform_data macb0_data;
  904. static struct resource macb0_resource[] = {
  905. PBMEM(0xfff01800),
  906. IRQ(25),
  907. };
  908. DEFINE_DEV_DATA(macb, 0);
  909. DEV_CLK(hclk, macb0, hsb, 8);
  910. DEV_CLK(pclk, macb0, pbb, 6);
  911. static struct macb_platform_data macb1_data;
  912. static struct resource macb1_resource[] = {
  913. PBMEM(0xfff01c00),
  914. IRQ(26),
  915. };
  916. DEFINE_DEV_DATA(macb, 1);
  917. DEV_CLK(hclk, macb1, hsb, 9);
  918. DEV_CLK(pclk, macb1, pbb, 7);
  919. struct platform_device *__init
  920. at32_add_device_eth(unsigned int id, struct macb_platform_data *data)
  921. {
  922. struct platform_device *pdev;
  923. u32 pin_mask;
  924. switch (id) {
  925. case 0:
  926. pdev = &macb0_device;
  927. pin_mask = (1 << 3); /* TXD0 */
  928. pin_mask |= (1 << 4); /* TXD1 */
  929. pin_mask |= (1 << 7); /* TXEN */
  930. pin_mask |= (1 << 8); /* TXCK */
  931. pin_mask |= (1 << 9); /* RXD0 */
  932. pin_mask |= (1 << 10); /* RXD1 */
  933. pin_mask |= (1 << 13); /* RXER */
  934. pin_mask |= (1 << 15); /* RXDV */
  935. pin_mask |= (1 << 16); /* MDC */
  936. pin_mask |= (1 << 17); /* MDIO */
  937. if (!data->is_rmii) {
  938. pin_mask |= (1 << 0); /* COL */
  939. pin_mask |= (1 << 1); /* CRS */
  940. pin_mask |= (1 << 2); /* TXER */
  941. pin_mask |= (1 << 5); /* TXD2 */
  942. pin_mask |= (1 << 6); /* TXD3 */
  943. pin_mask |= (1 << 11); /* RXD2 */
  944. pin_mask |= (1 << 12); /* RXD3 */
  945. pin_mask |= (1 << 14); /* RXCK */
  946. #ifndef CONFIG_BOARD_MIMC200
  947. pin_mask |= (1 << 18); /* SPD */
  948. #endif
  949. }
  950. select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
  951. break;
  952. case 1:
  953. pdev = &macb1_device;
  954. pin_mask = (1 << 13); /* TXD0 */
  955. pin_mask |= (1 << 14); /* TXD1 */
  956. pin_mask |= (1 << 11); /* TXEN */
  957. pin_mask |= (1 << 12); /* TXCK */
  958. pin_mask |= (1 << 10); /* RXD0 */
  959. pin_mask |= (1 << 6); /* RXD1 */
  960. pin_mask |= (1 << 5); /* RXER */
  961. pin_mask |= (1 << 4); /* RXDV */
  962. pin_mask |= (1 << 3); /* MDC */
  963. pin_mask |= (1 << 2); /* MDIO */
  964. #ifndef CONFIG_BOARD_MIMC200
  965. if (!data->is_rmii)
  966. pin_mask |= (1 << 15); /* SPD */
  967. #endif
  968. select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
  969. if (!data->is_rmii) {
  970. pin_mask = (1 << 19); /* COL */
  971. pin_mask |= (1 << 23); /* CRS */
  972. pin_mask |= (1 << 26); /* TXER */
  973. pin_mask |= (1 << 27); /* TXD2 */
  974. pin_mask |= (1 << 28); /* TXD3 */
  975. pin_mask |= (1 << 29); /* RXD2 */
  976. pin_mask |= (1 << 30); /* RXD3 */
  977. pin_mask |= (1 << 24); /* RXCK */
  978. select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
  979. }
  980. break;
  981. default:
  982. return NULL;
  983. }
  984. memcpy(pdev->dev.platform_data, data, sizeof(struct macb_platform_data));
  985. platform_device_register(pdev);
  986. return pdev;
  987. }
  988. #endif
  989. /* --------------------------------------------------------------------
  990. * SPI
  991. * -------------------------------------------------------------------- */
  992. static struct resource atmel_spi0_resource[] = {
  993. PBMEM(0xffe00000),
  994. IRQ(3),
  995. };
  996. DEFINE_DEV(atmel_spi, 0);
  997. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  998. static struct resource atmel_spi1_resource[] = {
  999. PBMEM(0xffe00400),
  1000. IRQ(4),
  1001. };
  1002. DEFINE_DEV(atmel_spi, 1);
  1003. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  1004. void __init
  1005. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b, unsigned int n)
  1006. {
  1007. /*
  1008. * Manage the chipselects as GPIOs, normally using the same pins
  1009. * the SPI controller expects; but boards can use other pins.
  1010. */
  1011. static u8 __initdata spi_pins[][4] = {
  1012. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  1013. GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
  1014. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  1015. GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
  1016. };
  1017. unsigned int pin, mode;
  1018. /* There are only 2 SPI controllers */
  1019. if (bus_num > 1)
  1020. return;
  1021. for (; n; n--, b++) {
  1022. b->bus_num = bus_num;
  1023. if (b->chip_select >= 4)
  1024. continue;
  1025. pin = (unsigned)b->controller_data;
  1026. if (!pin) {
  1027. pin = spi_pins[bus_num][b->chip_select];
  1028. b->controller_data = (void *)pin;
  1029. }
  1030. mode = AT32_GPIOF_OUTPUT;
  1031. if (!(b->mode & SPI_CS_HIGH))
  1032. mode |= AT32_GPIOF_HIGH;
  1033. at32_select_gpio(pin, mode);
  1034. }
  1035. }
  1036. struct platform_device *__init
  1037. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  1038. {
  1039. struct platform_device *pdev;
  1040. u32 pin_mask;
  1041. switch (id) {
  1042. case 0:
  1043. pdev = &atmel_spi0_device;
  1044. pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
  1045. /* pullup MISO so a level is always defined */
  1046. select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
  1047. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1048. at32_spi_setup_slaves(0, b, n);
  1049. break;
  1050. case 1:
  1051. pdev = &atmel_spi1_device;
  1052. pin_mask = (1 << 1) | (1 << 5); /* MOSI */
  1053. /* pullup MISO so a level is always defined */
  1054. select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
  1055. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1056. at32_spi_setup_slaves(1, b, n);
  1057. break;
  1058. default:
  1059. return NULL;
  1060. }
  1061. spi_register_board_info(b, n);
  1062. platform_device_register(pdev);
  1063. return pdev;
  1064. }
  1065. /* --------------------------------------------------------------------
  1066. * TWI
  1067. * -------------------------------------------------------------------- */
  1068. static struct resource atmel_twi0_resource[] __initdata = {
  1069. PBMEM(0xffe00800),
  1070. IRQ(5),
  1071. };
  1072. static struct clk atmel_twi0_pclk = {
  1073. .name = "twi_pclk",
  1074. .parent = &pba_clk,
  1075. .mode = pba_clk_mode,
  1076. .get_rate = pba_clk_get_rate,
  1077. .index = 2,
  1078. };
  1079. struct platform_device *__init at32_add_device_twi(unsigned int id,
  1080. struct i2c_board_info *b,
  1081. unsigned int n)
  1082. {
  1083. struct platform_device *pdev;
  1084. u32 pin_mask;
  1085. if (id != 0)
  1086. return NULL;
  1087. pdev = platform_device_alloc("atmel_twi", id);
  1088. if (!pdev)
  1089. return NULL;
  1090. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  1091. ARRAY_SIZE(atmel_twi0_resource)))
  1092. goto err_add_resources;
  1093. pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
  1094. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1095. atmel_twi0_pclk.dev = &pdev->dev;
  1096. if (b)
  1097. i2c_register_board_info(id, b, n);
  1098. platform_device_add(pdev);
  1099. return pdev;
  1100. err_add_resources:
  1101. platform_device_put(pdev);
  1102. return NULL;
  1103. }
  1104. /* --------------------------------------------------------------------
  1105. * MMC
  1106. * -------------------------------------------------------------------- */
  1107. static struct resource atmel_mci0_resource[] __initdata = {
  1108. PBMEM(0xfff02400),
  1109. IRQ(28),
  1110. };
  1111. static struct clk atmel_mci0_pclk = {
  1112. .name = "mci_clk",
  1113. .parent = &pbb_clk,
  1114. .mode = pbb_clk_mode,
  1115. .get_rate = pbb_clk_get_rate,
  1116. .index = 9,
  1117. };
  1118. struct platform_device *__init
  1119. at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
  1120. {
  1121. struct platform_device *pdev;
  1122. struct mci_dma_data *slave;
  1123. u32 pioa_mask;
  1124. u32 piob_mask;
  1125. if (id != 0 || !data)
  1126. return NULL;
  1127. /* Must have at least one usable slot */
  1128. if (!data->slot[0].bus_width && !data->slot[1].bus_width)
  1129. return NULL;
  1130. pdev = platform_device_alloc("atmel_mci", id);
  1131. if (!pdev)
  1132. goto fail;
  1133. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  1134. ARRAY_SIZE(atmel_mci0_resource)))
  1135. goto fail;
  1136. slave = kzalloc(sizeof(struct mci_dma_data), GFP_KERNEL);
  1137. if (!slave)
  1138. goto fail;
  1139. slave->sdata.dma_dev = &dw_dmac0_device.dev;
  1140. slave->sdata.src_id = 0;
  1141. slave->sdata.dst_id = 1;
  1142. slave->sdata.src_master = 1;
  1143. slave->sdata.dst_master = 0;
  1144. data->dma_slave = slave;
  1145. if (platform_device_add_data(pdev, data,
  1146. sizeof(struct mci_platform_data)))
  1147. goto fail_free;
  1148. /* CLK line is common to both slots */
  1149. pioa_mask = 1 << 10;
  1150. switch (data->slot[0].bus_width) {
  1151. case 4:
  1152. pioa_mask |= 1 << 13; /* DATA1 */
  1153. pioa_mask |= 1 << 14; /* DATA2 */
  1154. pioa_mask |= 1 << 15; /* DATA3 */
  1155. /* fall through */
  1156. case 1:
  1157. pioa_mask |= 1 << 11; /* CMD */
  1158. pioa_mask |= 1 << 12; /* DATA0 */
  1159. if (gpio_is_valid(data->slot[0].detect_pin))
  1160. at32_select_gpio(data->slot[0].detect_pin, 0);
  1161. if (gpio_is_valid(data->slot[0].wp_pin))
  1162. at32_select_gpio(data->slot[0].wp_pin, 0);
  1163. break;
  1164. case 0:
  1165. /* Slot is unused */
  1166. break;
  1167. default:
  1168. goto fail_free;
  1169. }
  1170. select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
  1171. piob_mask = 0;
  1172. switch (data->slot[1].bus_width) {
  1173. case 4:
  1174. piob_mask |= 1 << 8; /* DATA1 */
  1175. piob_mask |= 1 << 9; /* DATA2 */
  1176. piob_mask |= 1 << 10; /* DATA3 */
  1177. /* fall through */
  1178. case 1:
  1179. piob_mask |= 1 << 6; /* CMD */
  1180. piob_mask |= 1 << 7; /* DATA0 */
  1181. select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
  1182. if (gpio_is_valid(data->slot[1].detect_pin))
  1183. at32_select_gpio(data->slot[1].detect_pin, 0);
  1184. if (gpio_is_valid(data->slot[1].wp_pin))
  1185. at32_select_gpio(data->slot[1].wp_pin, 0);
  1186. break;
  1187. case 0:
  1188. /* Slot is unused */
  1189. break;
  1190. default:
  1191. if (!data->slot[0].bus_width)
  1192. goto fail_free;
  1193. data->slot[1].bus_width = 0;
  1194. break;
  1195. }
  1196. atmel_mci0_pclk.dev = &pdev->dev;
  1197. platform_device_add(pdev);
  1198. return pdev;
  1199. fail_free:
  1200. kfree(slave);
  1201. fail:
  1202. data->dma_slave = NULL;
  1203. platform_device_put(pdev);
  1204. return NULL;
  1205. }
  1206. /* --------------------------------------------------------------------
  1207. * LCDC
  1208. * -------------------------------------------------------------------- */
  1209. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1210. static struct atmel_lcdfb_pdata atmel_lcdfb0_data;
  1211. static struct resource atmel_lcdfb0_resource[] = {
  1212. {
  1213. .start = 0xff000000,
  1214. .end = 0xff000fff,
  1215. .flags = IORESOURCE_MEM,
  1216. },
  1217. IRQ(1),
  1218. {
  1219. /* Placeholder for pre-allocated fb memory */
  1220. .start = 0x00000000,
  1221. .end = 0x00000000,
  1222. .flags = 0,
  1223. },
  1224. };
  1225. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  1226. DEV_CLK(hclk, atmel_lcdfb0, hsb, 7);
  1227. static struct clk atmel_lcdfb0_pixclk = {
  1228. .name = "lcdc_clk",
  1229. .dev = &atmel_lcdfb0_device.dev,
  1230. .mode = genclk_mode,
  1231. .get_rate = genclk_get_rate,
  1232. .set_rate = genclk_set_rate,
  1233. .set_parent = genclk_set_parent,
  1234. .index = 7,
  1235. };
  1236. struct platform_device *__init
  1237. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_pdata *data,
  1238. unsigned long fbmem_start, unsigned long fbmem_len,
  1239. u64 pin_mask)
  1240. {
  1241. struct platform_device *pdev;
  1242. struct atmel_lcdfb_pdata *info;
  1243. struct fb_monspecs *monspecs;
  1244. struct fb_videomode *modedb;
  1245. unsigned int modedb_size;
  1246. u32 portc_mask, portd_mask, porte_mask;
  1247. /*
  1248. * Do a deep copy of the fb data, monspecs and modedb. Make
  1249. * sure all allocations are done before setting up the
  1250. * portmux.
  1251. */
  1252. monspecs = kmemdup(data->default_monspecs,
  1253. sizeof(struct fb_monspecs), GFP_KERNEL);
  1254. if (!monspecs)
  1255. return NULL;
  1256. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  1257. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  1258. if (!modedb)
  1259. goto err_dup_modedb;
  1260. monspecs->modedb = modedb;
  1261. switch (id) {
  1262. case 0:
  1263. pdev = &atmel_lcdfb0_device;
  1264. if (pin_mask == 0ULL)
  1265. /* Default to "full" lcdc control signals and 24bit */
  1266. pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
  1267. /* LCDC on port C */
  1268. portc_mask = pin_mask & 0xfff80000;
  1269. select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
  1270. /* LCDC on port D */
  1271. portd_mask = pin_mask & 0x0003ffff;
  1272. select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
  1273. /* LCDC on port E */
  1274. porte_mask = (pin_mask >> 32) & 0x0007ffff;
  1275. select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
  1276. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  1277. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  1278. break;
  1279. default:
  1280. goto err_invalid_id;
  1281. }
  1282. if (fbmem_len) {
  1283. pdev->resource[2].start = fbmem_start;
  1284. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1285. pdev->resource[2].flags = IORESOURCE_MEM;
  1286. }
  1287. info = pdev->dev.platform_data;
  1288. memcpy(info, data, sizeof(struct atmel_lcdfb_pdata));
  1289. info->default_monspecs = monspecs;
  1290. pdev->name = "at32ap-lcdfb";
  1291. platform_device_register(pdev);
  1292. return pdev;
  1293. err_invalid_id:
  1294. kfree(modedb);
  1295. err_dup_modedb:
  1296. kfree(monspecs);
  1297. return NULL;
  1298. }
  1299. #endif
  1300. /* --------------------------------------------------------------------
  1301. * PWM
  1302. * -------------------------------------------------------------------- */
  1303. static struct resource atmel_pwm0_resource[] __initdata = {
  1304. PBMEM(0xfff01400),
  1305. IRQ(24),
  1306. };
  1307. static struct clk atmel_pwm0_mck = {
  1308. .name = "at91sam9rl-pwm",
  1309. .parent = &pbb_clk,
  1310. .mode = pbb_clk_mode,
  1311. .get_rate = pbb_clk_get_rate,
  1312. .index = 5,
  1313. };
  1314. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1315. {
  1316. struct platform_device *pdev;
  1317. u32 pin_mask;
  1318. if (!mask)
  1319. return NULL;
  1320. pdev = platform_device_alloc("at91sam9rl-pwm", 0);
  1321. if (!pdev)
  1322. return NULL;
  1323. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1324. ARRAY_SIZE(atmel_pwm0_resource)))
  1325. goto out_free_pdev;
  1326. pin_mask = 0;
  1327. if (mask & (1 << 0))
  1328. pin_mask |= (1 << 28);
  1329. if (mask & (1 << 1))
  1330. pin_mask |= (1 << 29);
  1331. if (pin_mask > 0)
  1332. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1333. pin_mask = 0;
  1334. if (mask & (1 << 2))
  1335. pin_mask |= (1 << 21);
  1336. if (mask & (1 << 3))
  1337. pin_mask |= (1 << 22);
  1338. if (pin_mask > 0)
  1339. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1340. atmel_pwm0_mck.dev = &pdev->dev;
  1341. platform_device_add(pdev);
  1342. return pdev;
  1343. out_free_pdev:
  1344. platform_device_put(pdev);
  1345. return NULL;
  1346. }
  1347. /* --------------------------------------------------------------------
  1348. * SSC
  1349. * -------------------------------------------------------------------- */
  1350. static struct resource ssc0_resource[] = {
  1351. PBMEM(0xffe01c00),
  1352. IRQ(10),
  1353. };
  1354. DEFINE_DEV(ssc, 0);
  1355. DEV_CLK(pclk, ssc0, pba, 7);
  1356. static struct resource ssc1_resource[] = {
  1357. PBMEM(0xffe02000),
  1358. IRQ(11),
  1359. };
  1360. DEFINE_DEV(ssc, 1);
  1361. DEV_CLK(pclk, ssc1, pba, 8);
  1362. static struct resource ssc2_resource[] = {
  1363. PBMEM(0xffe02400),
  1364. IRQ(12),
  1365. };
  1366. DEFINE_DEV(ssc, 2);
  1367. DEV_CLK(pclk, ssc2, pba, 9);
  1368. struct platform_device *__init
  1369. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1370. {
  1371. struct platform_device *pdev;
  1372. u32 pin_mask = 0;
  1373. switch (id) {
  1374. case 0:
  1375. pdev = &ssc0_device;
  1376. if (flags & ATMEL_SSC_RF)
  1377. pin_mask |= (1 << 21); /* RF */
  1378. if (flags & ATMEL_SSC_RK)
  1379. pin_mask |= (1 << 22); /* RK */
  1380. if (flags & ATMEL_SSC_TK)
  1381. pin_mask |= (1 << 23); /* TK */
  1382. if (flags & ATMEL_SSC_TF)
  1383. pin_mask |= (1 << 24); /* TF */
  1384. if (flags & ATMEL_SSC_TD)
  1385. pin_mask |= (1 << 25); /* TD */
  1386. if (flags & ATMEL_SSC_RD)
  1387. pin_mask |= (1 << 26); /* RD */
  1388. if (pin_mask > 0)
  1389. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1390. break;
  1391. case 1:
  1392. pdev = &ssc1_device;
  1393. if (flags & ATMEL_SSC_RF)
  1394. pin_mask |= (1 << 0); /* RF */
  1395. if (flags & ATMEL_SSC_RK)
  1396. pin_mask |= (1 << 1); /* RK */
  1397. if (flags & ATMEL_SSC_TK)
  1398. pin_mask |= (1 << 2); /* TK */
  1399. if (flags & ATMEL_SSC_TF)
  1400. pin_mask |= (1 << 3); /* TF */
  1401. if (flags & ATMEL_SSC_TD)
  1402. pin_mask |= (1 << 4); /* TD */
  1403. if (flags & ATMEL_SSC_RD)
  1404. pin_mask |= (1 << 5); /* RD */
  1405. if (pin_mask > 0)
  1406. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1407. break;
  1408. case 2:
  1409. pdev = &ssc2_device;
  1410. if (flags & ATMEL_SSC_TD)
  1411. pin_mask |= (1 << 13); /* TD */
  1412. if (flags & ATMEL_SSC_RD)
  1413. pin_mask |= (1 << 14); /* RD */
  1414. if (flags & ATMEL_SSC_TK)
  1415. pin_mask |= (1 << 15); /* TK */
  1416. if (flags & ATMEL_SSC_TF)
  1417. pin_mask |= (1 << 16); /* TF */
  1418. if (flags & ATMEL_SSC_RF)
  1419. pin_mask |= (1 << 17); /* RF */
  1420. if (flags & ATMEL_SSC_RK)
  1421. pin_mask |= (1 << 18); /* RK */
  1422. if (pin_mask > 0)
  1423. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1424. break;
  1425. default:
  1426. return NULL;
  1427. }
  1428. platform_device_register(pdev);
  1429. return pdev;
  1430. }
  1431. /* --------------------------------------------------------------------
  1432. * USB Device Controller
  1433. * -------------------------------------------------------------------- */
  1434. static struct resource usba0_resource[] __initdata = {
  1435. {
  1436. .start = 0xff300000,
  1437. .end = 0xff3fffff,
  1438. .flags = IORESOURCE_MEM,
  1439. }, {
  1440. .start = 0xfff03000,
  1441. .end = 0xfff033ff,
  1442. .flags = IORESOURCE_MEM,
  1443. },
  1444. IRQ(31),
  1445. };
  1446. static struct clk usba0_pclk = {
  1447. .name = "pclk",
  1448. .parent = &pbb_clk,
  1449. .mode = pbb_clk_mode,
  1450. .get_rate = pbb_clk_get_rate,
  1451. .index = 12,
  1452. };
  1453. static struct clk usba0_hclk = {
  1454. .name = "hclk",
  1455. .parent = &hsb_clk,
  1456. .mode = hsb_clk_mode,
  1457. .get_rate = hsb_clk_get_rate,
  1458. .index = 6,
  1459. };
  1460. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1461. [idx] = { \
  1462. .name = nam, \
  1463. .index = idx, \
  1464. .fifo_size = maxpkt, \
  1465. .nr_banks = maxbk, \
  1466. .can_dma = dma, \
  1467. .can_isoc = isoc, \
  1468. }
  1469. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1470. EP("ep0", 0, 64, 1, 0, 0),
  1471. EP("ep1", 1, 512, 2, 1, 1),
  1472. EP("ep2", 2, 512, 2, 1, 1),
  1473. EP("ep3-int", 3, 64, 3, 1, 0),
  1474. EP("ep4-int", 4, 64, 3, 1, 0),
  1475. EP("ep5", 5, 1024, 3, 1, 1),
  1476. EP("ep6", 6, 1024, 3, 1, 1),
  1477. };
  1478. #undef EP
  1479. struct platform_device *__init
  1480. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1481. {
  1482. /*
  1483. * pdata doesn't have room for any endpoints, so we need to
  1484. * append room for the ones we need right after it.
  1485. */
  1486. struct {
  1487. struct usba_platform_data pdata;
  1488. struct usba_ep_data ep[7];
  1489. } usba_data;
  1490. struct platform_device *pdev;
  1491. if (id != 0)
  1492. return NULL;
  1493. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1494. if (!pdev)
  1495. return NULL;
  1496. if (platform_device_add_resources(pdev, usba0_resource,
  1497. ARRAY_SIZE(usba0_resource)))
  1498. goto out_free_pdev;
  1499. if (data) {
  1500. usba_data.pdata.vbus_pin = data->vbus_pin;
  1501. usba_data.pdata.vbus_pin_inverted = data->vbus_pin_inverted;
  1502. } else {
  1503. usba_data.pdata.vbus_pin = -EINVAL;
  1504. usba_data.pdata.vbus_pin_inverted = -EINVAL;
  1505. }
  1506. data = &usba_data.pdata;
  1507. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1508. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1509. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1510. goto out_free_pdev;
  1511. if (gpio_is_valid(data->vbus_pin))
  1512. at32_select_gpio(data->vbus_pin, 0);
  1513. usba0_pclk.dev = &pdev->dev;
  1514. usba0_hclk.dev = &pdev->dev;
  1515. platform_device_add(pdev);
  1516. return pdev;
  1517. out_free_pdev:
  1518. platform_device_put(pdev);
  1519. return NULL;
  1520. }
  1521. /* --------------------------------------------------------------------
  1522. * IDE / CompactFlash
  1523. * -------------------------------------------------------------------- */
  1524. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1525. static struct resource at32_smc_cs4_resource[] __initdata = {
  1526. {
  1527. .start = 0x04000000,
  1528. .end = 0x07ffffff,
  1529. .flags = IORESOURCE_MEM,
  1530. },
  1531. IRQ(~0UL), /* Magic IRQ will be overridden */
  1532. };
  1533. static struct resource at32_smc_cs5_resource[] __initdata = {
  1534. {
  1535. .start = 0x20000000,
  1536. .end = 0x23ffffff,
  1537. .flags = IORESOURCE_MEM,
  1538. },
  1539. IRQ(~0UL), /* Magic IRQ will be overridden */
  1540. };
  1541. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1542. unsigned int cs, unsigned int extint)
  1543. {
  1544. static unsigned int extint_pin_map[4] __initdata = {
  1545. (1 << 25),
  1546. (1 << 26),
  1547. (1 << 27),
  1548. (1 << 28),
  1549. };
  1550. static bool common_pins_initialized __initdata = false;
  1551. unsigned int extint_pin;
  1552. int ret;
  1553. u32 pin_mask;
  1554. if (extint >= ARRAY_SIZE(extint_pin_map))
  1555. return -EINVAL;
  1556. extint_pin = extint_pin_map[extint];
  1557. switch (cs) {
  1558. case 4:
  1559. ret = platform_device_add_resources(pdev,
  1560. at32_smc_cs4_resource,
  1561. ARRAY_SIZE(at32_smc_cs4_resource));
  1562. if (ret)
  1563. return ret;
  1564. /* NCS4 -> OE_N */
  1565. select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
  1566. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
  1567. break;
  1568. case 5:
  1569. ret = platform_device_add_resources(pdev,
  1570. at32_smc_cs5_resource,
  1571. ARRAY_SIZE(at32_smc_cs5_resource));
  1572. if (ret)
  1573. return ret;
  1574. /* NCS5 -> OE_N */
  1575. select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
  1576. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
  1577. break;
  1578. default:
  1579. return -EINVAL;
  1580. }
  1581. if (!common_pins_initialized) {
  1582. pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
  1583. pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
  1584. pin_mask |= (1 << 23); /* CFRNW -> DIR */
  1585. pin_mask |= (1 << 24); /* NWAIT <- IORDY */
  1586. select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
  1587. common_pins_initialized = true;
  1588. }
  1589. select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
  1590. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1591. pdev->resource[1].end = pdev->resource[1].start;
  1592. return 0;
  1593. }
  1594. struct platform_device *__init
  1595. at32_add_device_ide(unsigned int id, unsigned int extint,
  1596. struct ide_platform_data *data)
  1597. {
  1598. struct platform_device *pdev;
  1599. pdev = platform_device_alloc("at32_ide", id);
  1600. if (!pdev)
  1601. goto fail;
  1602. if (platform_device_add_data(pdev, data,
  1603. sizeof(struct ide_platform_data)))
  1604. goto fail;
  1605. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1606. goto fail;
  1607. platform_device_add(pdev);
  1608. return pdev;
  1609. fail:
  1610. platform_device_put(pdev);
  1611. return NULL;
  1612. }
  1613. struct platform_device *__init
  1614. at32_add_device_cf(unsigned int id, unsigned int extint,
  1615. struct cf_platform_data *data)
  1616. {
  1617. struct platform_device *pdev;
  1618. pdev = platform_device_alloc("at32_cf", id);
  1619. if (!pdev)
  1620. goto fail;
  1621. if (platform_device_add_data(pdev, data,
  1622. sizeof(struct cf_platform_data)))
  1623. goto fail;
  1624. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1625. goto fail;
  1626. if (gpio_is_valid(data->detect_pin))
  1627. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1628. if (gpio_is_valid(data->reset_pin))
  1629. at32_select_gpio(data->reset_pin, 0);
  1630. if (gpio_is_valid(data->vcc_pin))
  1631. at32_select_gpio(data->vcc_pin, 0);
  1632. /* READY is used as extint, so we can't select it as gpio */
  1633. platform_device_add(pdev);
  1634. return pdev;
  1635. fail:
  1636. platform_device_put(pdev);
  1637. return NULL;
  1638. }
  1639. #endif
  1640. /* --------------------------------------------------------------------
  1641. * NAND Flash / SmartMedia
  1642. * -------------------------------------------------------------------- */
  1643. static struct resource smc_cs3_resource[] __initdata = {
  1644. {
  1645. .start = 0x0c000000,
  1646. .end = 0x0fffffff,
  1647. .flags = IORESOURCE_MEM,
  1648. }, {
  1649. .start = 0xfff03c00,
  1650. .end = 0xfff03fff,
  1651. .flags = IORESOURCE_MEM,
  1652. },
  1653. };
  1654. struct platform_device *__init
  1655. at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
  1656. {
  1657. struct platform_device *pdev;
  1658. if (id != 0 || !data)
  1659. return NULL;
  1660. pdev = platform_device_alloc("atmel_nand", id);
  1661. if (!pdev)
  1662. goto fail;
  1663. if (platform_device_add_resources(pdev, smc_cs3_resource,
  1664. ARRAY_SIZE(smc_cs3_resource)))
  1665. goto fail;
  1666. /* For at32ap7000, we use the reset workaround for nand driver */
  1667. data->need_reset_workaround = true;
  1668. if (platform_device_add_data(pdev, data,
  1669. sizeof(struct atmel_nand_data)))
  1670. goto fail;
  1671. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
  1672. if (data->enable_pin)
  1673. at32_select_gpio(data->enable_pin,
  1674. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  1675. if (data->rdy_pin)
  1676. at32_select_gpio(data->rdy_pin, 0);
  1677. if (data->det_pin)
  1678. at32_select_gpio(data->det_pin, 0);
  1679. platform_device_add(pdev);
  1680. return pdev;
  1681. fail:
  1682. platform_device_put(pdev);
  1683. return NULL;
  1684. }
  1685. /* --------------------------------------------------------------------
  1686. * AC97C
  1687. * -------------------------------------------------------------------- */
  1688. static struct resource atmel_ac97c0_resource[] __initdata = {
  1689. PBMEM(0xfff02800),
  1690. IRQ(29),
  1691. };
  1692. static struct clk atmel_ac97c0_pclk = {
  1693. .name = "pclk",
  1694. .parent = &pbb_clk,
  1695. .mode = pbb_clk_mode,
  1696. .get_rate = pbb_clk_get_rate,
  1697. .index = 10,
  1698. };
  1699. struct platform_device *__init
  1700. at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data,
  1701. unsigned int flags)
  1702. {
  1703. struct platform_device *pdev;
  1704. struct dw_dma_slave *rx_dws;
  1705. struct dw_dma_slave *tx_dws;
  1706. struct ac97c_platform_data _data;
  1707. u32 pin_mask;
  1708. if (id != 0)
  1709. return NULL;
  1710. pdev = platform_device_alloc("atmel_ac97c", id);
  1711. if (!pdev)
  1712. return NULL;
  1713. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1714. ARRAY_SIZE(atmel_ac97c0_resource)))
  1715. goto out_free_resources;
  1716. if (!data) {
  1717. data = &_data;
  1718. memset(data, 0, sizeof(struct ac97c_platform_data));
  1719. data->reset_pin = -ENODEV;
  1720. }
  1721. rx_dws = &data->rx_dws;
  1722. tx_dws = &data->tx_dws;
  1723. /* Check if DMA slave interface for capture should be configured. */
  1724. if (flags & AC97C_CAPTURE) {
  1725. rx_dws->dma_dev = &dw_dmac0_device.dev;
  1726. rx_dws->src_id = 3;
  1727. rx_dws->src_master = 0;
  1728. rx_dws->dst_master = 1;
  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->dst_id = 4;
  1734. tx_dws->src_master = 0;
  1735. tx_dws->dst_master = 1;
  1736. }
  1737. if (platform_device_add_data(pdev, data,
  1738. sizeof(struct ac97c_platform_data)))
  1739. goto out_free_resources;
  1740. /* SDO | SYNC | SCLK | SDI */
  1741. pin_mask = (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
  1742. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1743. if (gpio_is_valid(data->reset_pin))
  1744. at32_select_gpio(data->reset_pin, AT32_GPIOF_OUTPUT
  1745. | AT32_GPIOF_HIGH);
  1746. atmel_ac97c0_pclk.dev = &pdev->dev;
  1747. platform_device_add(pdev);
  1748. return pdev;
  1749. out_free_resources:
  1750. platform_device_put(pdev);
  1751. return NULL;
  1752. }
  1753. /* --------------------------------------------------------------------
  1754. * ABDAC
  1755. * -------------------------------------------------------------------- */
  1756. static struct resource abdac0_resource[] __initdata = {
  1757. PBMEM(0xfff02000),
  1758. IRQ(27),
  1759. };
  1760. static struct clk abdac0_pclk = {
  1761. .name = "pclk",
  1762. .parent = &pbb_clk,
  1763. .mode = pbb_clk_mode,
  1764. .get_rate = pbb_clk_get_rate,
  1765. .index = 8,
  1766. };
  1767. static struct clk abdac0_sample_clk = {
  1768. .name = "sample_clk",
  1769. .mode = genclk_mode,
  1770. .get_rate = genclk_get_rate,
  1771. .set_rate = genclk_set_rate,
  1772. .set_parent = genclk_set_parent,
  1773. .index = 6,
  1774. };
  1775. struct platform_device *__init
  1776. at32_add_device_abdac(unsigned int id, struct atmel_abdac_pdata *data)
  1777. {
  1778. struct platform_device *pdev;
  1779. struct dw_dma_slave *dws;
  1780. u32 pin_mask;
  1781. if (id != 0 || !data)
  1782. return NULL;
  1783. pdev = platform_device_alloc("atmel_abdac", id);
  1784. if (!pdev)
  1785. return NULL;
  1786. if (platform_device_add_resources(pdev, abdac0_resource,
  1787. ARRAY_SIZE(abdac0_resource)))
  1788. goto out_free_resources;
  1789. dws = &data->dws;
  1790. dws->dma_dev = &dw_dmac0_device.dev;
  1791. dws->dst_id = 2;
  1792. dws->src_master = 0;
  1793. dws->dst_master = 1;
  1794. if (platform_device_add_data(pdev, data,
  1795. sizeof(struct atmel_abdac_pdata)))
  1796. goto out_free_resources;
  1797. pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
  1798. pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
  1799. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1800. abdac0_pclk.dev = &pdev->dev;
  1801. abdac0_sample_clk.dev = &pdev->dev;
  1802. platform_device_add(pdev);
  1803. return pdev;
  1804. out_free_resources:
  1805. platform_device_put(pdev);
  1806. return NULL;
  1807. }
  1808. /* --------------------------------------------------------------------
  1809. * GCLK
  1810. * -------------------------------------------------------------------- */
  1811. static struct clk gclk0 = {
  1812. .name = "gclk0",
  1813. .mode = genclk_mode,
  1814. .get_rate = genclk_get_rate,
  1815. .set_rate = genclk_set_rate,
  1816. .set_parent = genclk_set_parent,
  1817. .index = 0,
  1818. };
  1819. static struct clk gclk1 = {
  1820. .name = "gclk1",
  1821. .mode = genclk_mode,
  1822. .get_rate = genclk_get_rate,
  1823. .set_rate = genclk_set_rate,
  1824. .set_parent = genclk_set_parent,
  1825. .index = 1,
  1826. };
  1827. static struct clk gclk2 = {
  1828. .name = "gclk2",
  1829. .mode = genclk_mode,
  1830. .get_rate = genclk_get_rate,
  1831. .set_rate = genclk_set_rate,
  1832. .set_parent = genclk_set_parent,
  1833. .index = 2,
  1834. };
  1835. static struct clk gclk3 = {
  1836. .name = "gclk3",
  1837. .mode = genclk_mode,
  1838. .get_rate = genclk_get_rate,
  1839. .set_rate = genclk_set_rate,
  1840. .set_parent = genclk_set_parent,
  1841. .index = 3,
  1842. };
  1843. static struct clk gclk4 = {
  1844. .name = "gclk4",
  1845. .mode = genclk_mode,
  1846. .get_rate = genclk_get_rate,
  1847. .set_rate = genclk_set_rate,
  1848. .set_parent = genclk_set_parent,
  1849. .index = 4,
  1850. };
  1851. static __initdata struct clk *init_clocks[] = {
  1852. &osc32k,
  1853. &osc0,
  1854. &osc1,
  1855. &pll0,
  1856. &pll1,
  1857. &cpu_clk,
  1858. &hsb_clk,
  1859. &pba_clk,
  1860. &pbb_clk,
  1861. &at32_pm_pclk,
  1862. &at32_intc0_pclk,
  1863. &at32_hmatrix_clk,
  1864. &ebi_clk,
  1865. &hramc_clk,
  1866. &sdramc_clk,
  1867. &smc0_pclk,
  1868. &smc0_mck,
  1869. &pdc_hclk,
  1870. &pdc_pclk,
  1871. &dw_dmac0_hclk,
  1872. &pico_clk,
  1873. &pio0_mck,
  1874. &pio1_mck,
  1875. &pio2_mck,
  1876. &pio3_mck,
  1877. &pio4_mck,
  1878. &at32_tcb0_t0_clk,
  1879. &at32_tcb1_t0_clk,
  1880. &atmel_psif0_pclk,
  1881. &atmel_psif1_pclk,
  1882. &atmel_usart0_usart,
  1883. &atmel_usart1_usart,
  1884. &atmel_usart2_usart,
  1885. &atmel_usart3_usart,
  1886. &atmel_pwm0_mck,
  1887. #if defined(CONFIG_CPU_AT32AP7000)
  1888. &macb0_hclk,
  1889. &macb0_pclk,
  1890. &macb1_hclk,
  1891. &macb1_pclk,
  1892. #endif
  1893. &atmel_spi0_spi_clk,
  1894. &atmel_spi1_spi_clk,
  1895. &atmel_twi0_pclk,
  1896. &atmel_mci0_pclk,
  1897. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1898. &atmel_lcdfb0_hclk,
  1899. &atmel_lcdfb0_pixclk,
  1900. #endif
  1901. &ssc0_pclk,
  1902. &ssc1_pclk,
  1903. &ssc2_pclk,
  1904. &usba0_hclk,
  1905. &usba0_pclk,
  1906. &atmel_ac97c0_pclk,
  1907. &abdac0_pclk,
  1908. &abdac0_sample_clk,
  1909. &gclk0,
  1910. &gclk1,
  1911. &gclk2,
  1912. &gclk3,
  1913. &gclk4,
  1914. };
  1915. void __init setup_platform(void)
  1916. {
  1917. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1918. int i;
  1919. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1920. main_clock = &pll0;
  1921. cpu_clk.parent = &pll0;
  1922. } else {
  1923. main_clock = &osc0;
  1924. cpu_clk.parent = &osc0;
  1925. }
  1926. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1927. pll0.parent = &osc1;
  1928. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1929. pll1.parent = &osc1;
  1930. genclk_init_parent(&gclk0);
  1931. genclk_init_parent(&gclk1);
  1932. genclk_init_parent(&gclk2);
  1933. genclk_init_parent(&gclk3);
  1934. genclk_init_parent(&gclk4);
  1935. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1936. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1937. #endif
  1938. genclk_init_parent(&abdac0_sample_clk);
  1939. /*
  1940. * Build initial dynamic clock list by registering all clocks
  1941. * from the array.
  1942. * At the same time, turn on all clocks that have at least one
  1943. * user already, and turn off everything else. We only do this
  1944. * for module clocks, and even though it isn't particularly
  1945. * pretty to check the address of the mode function, it should
  1946. * do the trick...
  1947. */
  1948. for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
  1949. struct clk *clk = init_clocks[i];
  1950. /* first, register clock */
  1951. at32_clk_register(clk);
  1952. if (clk->users == 0)
  1953. continue;
  1954. if (clk->mode == &cpu_clk_mode)
  1955. cpu_mask |= 1 << clk->index;
  1956. else if (clk->mode == &hsb_clk_mode)
  1957. hsb_mask |= 1 << clk->index;
  1958. else if (clk->mode == &pba_clk_mode)
  1959. pba_mask |= 1 << clk->index;
  1960. else if (clk->mode == &pbb_clk_mode)
  1961. pbb_mask |= 1 << clk->index;
  1962. }
  1963. pm_writel(CPU_MASK, cpu_mask);
  1964. pm_writel(HSB_MASK, hsb_mask);
  1965. pm_writel(PBA_MASK, pba_mask);
  1966. pm_writel(PBB_MASK, pbb_mask);
  1967. /* Initialize the port muxes */
  1968. at32_init_pio(&pio0_device);
  1969. at32_init_pio(&pio1_device);
  1970. at32_init_pio(&pio2_device);
  1971. at32_init_pio(&pio3_device);
  1972. at32_init_pio(&pio4_device);
  1973. }
  1974. struct gen_pool *sram_pool;
  1975. static int __init sram_init(void)
  1976. {
  1977. struct gen_pool *pool;
  1978. /* 1KiB granularity */
  1979. pool = gen_pool_create(10, -1);
  1980. if (!pool)
  1981. goto fail;
  1982. if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
  1983. goto err_pool_add;
  1984. sram_pool = pool;
  1985. return 0;
  1986. err_pool_add:
  1987. gen_pool_destroy(pool);
  1988. fail:
  1989. pr_err("Failed to create SRAM pool\n");
  1990. return -ENOMEM;
  1991. }
  1992. core_initcall(sram_init);