at32ap700x.c 52 KB

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