at32ap700x.c 47 KB

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