at32ap700x.c 51 KB

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