at32ap700x.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335
  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_BIT_MASK(32); \
  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_BIT_MASK(32), \
  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_BIT_MASK(32); \
  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_BIT_MASK(32), \
  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(int flags)
  817. {
  818. u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
  819. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 6);
  820. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 7);
  821. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 10);
  822. select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  823. }
  824. static inline void configure_usart1_pins(int flags)
  825. {
  826. u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
  827. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 19);
  828. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 20);
  829. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 16);
  830. select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
  831. }
  832. static inline void configure_usart2_pins(int flags)
  833. {
  834. u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
  835. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 30);
  836. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 29);
  837. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 28);
  838. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  839. }
  840. static inline void configure_usart3_pins(int flags)
  841. {
  842. u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
  843. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 16);
  844. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 15);
  845. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 19);
  846. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  847. }
  848. static struct platform_device *__initdata at32_usarts[4];
  849. void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags)
  850. {
  851. struct platform_device *pdev;
  852. switch (hw_id) {
  853. case 0:
  854. pdev = &atmel_usart0_device;
  855. configure_usart0_pins(flags);
  856. break;
  857. case 1:
  858. pdev = &atmel_usart1_device;
  859. configure_usart1_pins(flags);
  860. break;
  861. case 2:
  862. pdev = &atmel_usart2_device;
  863. configure_usart2_pins(flags);
  864. break;
  865. case 3:
  866. pdev = &atmel_usart3_device;
  867. configure_usart3_pins(flags);
  868. break;
  869. default:
  870. return;
  871. }
  872. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  873. /* Addresses in the P4 segment are permanently mapped 1:1 */
  874. struct atmel_uart_data *data = pdev->dev.platform_data;
  875. data->regs = (void __iomem *)pdev->resource[0].start;
  876. }
  877. pdev->id = line;
  878. at32_usarts[line] = pdev;
  879. }
  880. struct platform_device *__init at32_add_device_usart(unsigned int id)
  881. {
  882. platform_device_register(at32_usarts[id]);
  883. return at32_usarts[id];
  884. }
  885. struct platform_device *atmel_default_console_device;
  886. void __init at32_setup_serial_console(unsigned int usart_id)
  887. {
  888. atmel_default_console_device = at32_usarts[usart_id];
  889. }
  890. /* --------------------------------------------------------------------
  891. * Ethernet
  892. * -------------------------------------------------------------------- */
  893. #ifdef CONFIG_CPU_AT32AP7000
  894. static struct eth_platform_data macb0_data;
  895. static struct resource macb0_resource[] = {
  896. PBMEM(0xfff01800),
  897. IRQ(25),
  898. };
  899. DEFINE_DEV_DATA(macb, 0);
  900. DEV_CLK(hclk, macb0, hsb, 8);
  901. DEV_CLK(pclk, macb0, pbb, 6);
  902. static struct eth_platform_data macb1_data;
  903. static struct resource macb1_resource[] = {
  904. PBMEM(0xfff01c00),
  905. IRQ(26),
  906. };
  907. DEFINE_DEV_DATA(macb, 1);
  908. DEV_CLK(hclk, macb1, hsb, 9);
  909. DEV_CLK(pclk, macb1, pbb, 7);
  910. struct platform_device *__init
  911. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  912. {
  913. struct platform_device *pdev;
  914. u32 pin_mask;
  915. switch (id) {
  916. case 0:
  917. pdev = &macb0_device;
  918. pin_mask = (1 << 3); /* TXD0 */
  919. pin_mask |= (1 << 4); /* TXD1 */
  920. pin_mask |= (1 << 7); /* TXEN */
  921. pin_mask |= (1 << 8); /* TXCK */
  922. pin_mask |= (1 << 9); /* RXD0 */
  923. pin_mask |= (1 << 10); /* RXD1 */
  924. pin_mask |= (1 << 13); /* RXER */
  925. pin_mask |= (1 << 15); /* RXDV */
  926. pin_mask |= (1 << 16); /* MDC */
  927. pin_mask |= (1 << 17); /* MDIO */
  928. if (!data->is_rmii) {
  929. pin_mask |= (1 << 0); /* COL */
  930. pin_mask |= (1 << 1); /* CRS */
  931. pin_mask |= (1 << 2); /* TXER */
  932. pin_mask |= (1 << 5); /* TXD2 */
  933. pin_mask |= (1 << 6); /* TXD3 */
  934. pin_mask |= (1 << 11); /* RXD2 */
  935. pin_mask |= (1 << 12); /* RXD3 */
  936. pin_mask |= (1 << 14); /* RXCK */
  937. #ifndef CONFIG_BOARD_MIMC200
  938. pin_mask |= (1 << 18); /* SPD */
  939. #endif
  940. }
  941. select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
  942. break;
  943. case 1:
  944. pdev = &macb1_device;
  945. pin_mask = (1 << 13); /* TXD0 */
  946. pin_mask |= (1 << 14); /* TXD1 */
  947. pin_mask |= (1 << 11); /* TXEN */
  948. pin_mask |= (1 << 12); /* TXCK */
  949. pin_mask |= (1 << 10); /* RXD0 */
  950. pin_mask |= (1 << 6); /* RXD1 */
  951. pin_mask |= (1 << 5); /* RXER */
  952. pin_mask |= (1 << 4); /* RXDV */
  953. pin_mask |= (1 << 3); /* MDC */
  954. pin_mask |= (1 << 2); /* MDIO */
  955. #ifndef CONFIG_BOARD_MIMC200
  956. if (!data->is_rmii)
  957. pin_mask |= (1 << 15); /* SPD */
  958. #endif
  959. select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
  960. if (!data->is_rmii) {
  961. pin_mask = (1 << 19); /* COL */
  962. pin_mask |= (1 << 23); /* CRS */
  963. pin_mask |= (1 << 26); /* TXER */
  964. pin_mask |= (1 << 27); /* TXD2 */
  965. pin_mask |= (1 << 28); /* TXD3 */
  966. pin_mask |= (1 << 29); /* RXD2 */
  967. pin_mask |= (1 << 30); /* RXD3 */
  968. pin_mask |= (1 << 24); /* RXCK */
  969. select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
  970. }
  971. break;
  972. default:
  973. return NULL;
  974. }
  975. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  976. platform_device_register(pdev);
  977. return pdev;
  978. }
  979. #endif
  980. /* --------------------------------------------------------------------
  981. * SPI
  982. * -------------------------------------------------------------------- */
  983. static struct resource atmel_spi0_resource[] = {
  984. PBMEM(0xffe00000),
  985. IRQ(3),
  986. };
  987. DEFINE_DEV(atmel_spi, 0);
  988. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  989. static struct resource atmel_spi1_resource[] = {
  990. PBMEM(0xffe00400),
  991. IRQ(4),
  992. };
  993. DEFINE_DEV(atmel_spi, 1);
  994. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  995. static void __init
  996. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b,
  997. unsigned int n, const u8 *pins)
  998. {
  999. unsigned int pin, mode;
  1000. for (; n; n--, b++) {
  1001. b->bus_num = bus_num;
  1002. if (b->chip_select >= 4)
  1003. continue;
  1004. pin = (unsigned)b->controller_data;
  1005. if (!pin) {
  1006. pin = pins[b->chip_select];
  1007. b->controller_data = (void *)pin;
  1008. }
  1009. mode = AT32_GPIOF_OUTPUT;
  1010. if (!(b->mode & SPI_CS_HIGH))
  1011. mode |= AT32_GPIOF_HIGH;
  1012. at32_select_gpio(pin, mode);
  1013. }
  1014. }
  1015. struct platform_device *__init
  1016. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  1017. {
  1018. /*
  1019. * Manage the chipselects as GPIOs, normally using the same pins
  1020. * the SPI controller expects; but boards can use other pins.
  1021. */
  1022. static u8 __initdata spi0_pins[] =
  1023. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  1024. GPIO_PIN_PA(5), GPIO_PIN_PA(20), };
  1025. static u8 __initdata spi1_pins[] =
  1026. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  1027. GPIO_PIN_PB(4), GPIO_PIN_PA(27), };
  1028. struct platform_device *pdev;
  1029. u32 pin_mask;
  1030. switch (id) {
  1031. case 0:
  1032. pdev = &atmel_spi0_device;
  1033. pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
  1034. /* pullup MISO so a level is always defined */
  1035. select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
  1036. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1037. at32_spi_setup_slaves(0, b, n, spi0_pins);
  1038. break;
  1039. case 1:
  1040. pdev = &atmel_spi1_device;
  1041. pin_mask = (1 << 1) | (1 << 5); /* MOSI */
  1042. /* pullup MISO so a level is always defined */
  1043. select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
  1044. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1045. at32_spi_setup_slaves(1, b, n, spi1_pins);
  1046. break;
  1047. default:
  1048. return NULL;
  1049. }
  1050. spi_register_board_info(b, n);
  1051. platform_device_register(pdev);
  1052. return pdev;
  1053. }
  1054. /* --------------------------------------------------------------------
  1055. * TWI
  1056. * -------------------------------------------------------------------- */
  1057. static struct resource atmel_twi0_resource[] __initdata = {
  1058. PBMEM(0xffe00800),
  1059. IRQ(5),
  1060. };
  1061. static struct clk atmel_twi0_pclk = {
  1062. .name = "twi_pclk",
  1063. .parent = &pba_clk,
  1064. .mode = pba_clk_mode,
  1065. .get_rate = pba_clk_get_rate,
  1066. .index = 2,
  1067. };
  1068. struct platform_device *__init at32_add_device_twi(unsigned int id,
  1069. struct i2c_board_info *b,
  1070. unsigned int n)
  1071. {
  1072. struct platform_device *pdev;
  1073. u32 pin_mask;
  1074. if (id != 0)
  1075. return NULL;
  1076. pdev = platform_device_alloc("atmel_twi", id);
  1077. if (!pdev)
  1078. return NULL;
  1079. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  1080. ARRAY_SIZE(atmel_twi0_resource)))
  1081. goto err_add_resources;
  1082. pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
  1083. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1084. atmel_twi0_pclk.dev = &pdev->dev;
  1085. if (b)
  1086. i2c_register_board_info(id, b, n);
  1087. platform_device_add(pdev);
  1088. return pdev;
  1089. err_add_resources:
  1090. platform_device_put(pdev);
  1091. return NULL;
  1092. }
  1093. /* --------------------------------------------------------------------
  1094. * MMC
  1095. * -------------------------------------------------------------------- */
  1096. static struct resource atmel_mci0_resource[] __initdata = {
  1097. PBMEM(0xfff02400),
  1098. IRQ(28),
  1099. };
  1100. static struct clk atmel_mci0_pclk = {
  1101. .name = "mci_clk",
  1102. .parent = &pbb_clk,
  1103. .mode = pbb_clk_mode,
  1104. .get_rate = pbb_clk_get_rate,
  1105. .index = 9,
  1106. };
  1107. struct platform_device *__init
  1108. at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
  1109. {
  1110. struct platform_device *pdev;
  1111. struct dw_dma_slave *dws = &data->dma_slave;
  1112. u32 pioa_mask;
  1113. u32 piob_mask;
  1114. if (id != 0 || !data)
  1115. return NULL;
  1116. /* Must have at least one usable slot */
  1117. if (!data->slot[0].bus_width && !data->slot[1].bus_width)
  1118. return NULL;
  1119. pdev = platform_device_alloc("atmel_mci", id);
  1120. if (!pdev)
  1121. goto fail;
  1122. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  1123. ARRAY_SIZE(atmel_mci0_resource)))
  1124. goto fail;
  1125. dws->dma_dev = &dw_dmac0_device.dev;
  1126. dws->reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
  1127. dws->cfg_hi = (DWC_CFGH_SRC_PER(0)
  1128. | DWC_CFGH_DST_PER(1));
  1129. dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL
  1130. | DWC_CFGL_HS_SRC_POL);
  1131. if (platform_device_add_data(pdev, data,
  1132. sizeof(struct mci_platform_data)))
  1133. goto fail;
  1134. /* CLK line is common to both slots */
  1135. pioa_mask = 1 << 10;
  1136. switch (data->slot[0].bus_width) {
  1137. case 4:
  1138. pioa_mask |= 1 << 13; /* DATA1 */
  1139. pioa_mask |= 1 << 14; /* DATA2 */
  1140. pioa_mask |= 1 << 15; /* DATA3 */
  1141. /* fall through */
  1142. case 1:
  1143. pioa_mask |= 1 << 11; /* CMD */
  1144. pioa_mask |= 1 << 12; /* DATA0 */
  1145. if (gpio_is_valid(data->slot[0].detect_pin))
  1146. at32_select_gpio(data->slot[0].detect_pin, 0);
  1147. if (gpio_is_valid(data->slot[0].wp_pin))
  1148. at32_select_gpio(data->slot[0].wp_pin, 0);
  1149. break;
  1150. case 0:
  1151. /* Slot is unused */
  1152. break;
  1153. default:
  1154. goto fail;
  1155. }
  1156. select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
  1157. piob_mask = 0;
  1158. switch (data->slot[1].bus_width) {
  1159. case 4:
  1160. piob_mask |= 1 << 8; /* DATA1 */
  1161. piob_mask |= 1 << 9; /* DATA2 */
  1162. piob_mask |= 1 << 10; /* DATA3 */
  1163. /* fall through */
  1164. case 1:
  1165. piob_mask |= 1 << 6; /* CMD */
  1166. piob_mask |= 1 << 7; /* DATA0 */
  1167. select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
  1168. if (gpio_is_valid(data->slot[1].detect_pin))
  1169. at32_select_gpio(data->slot[1].detect_pin, 0);
  1170. if (gpio_is_valid(data->slot[1].wp_pin))
  1171. at32_select_gpio(data->slot[1].wp_pin, 0);
  1172. break;
  1173. case 0:
  1174. /* Slot is unused */
  1175. break;
  1176. default:
  1177. if (!data->slot[0].bus_width)
  1178. goto fail;
  1179. data->slot[1].bus_width = 0;
  1180. break;
  1181. }
  1182. atmel_mci0_pclk.dev = &pdev->dev;
  1183. platform_device_add(pdev);
  1184. return pdev;
  1185. fail:
  1186. platform_device_put(pdev);
  1187. return NULL;
  1188. }
  1189. /* --------------------------------------------------------------------
  1190. * LCDC
  1191. * -------------------------------------------------------------------- */
  1192. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1193. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  1194. static struct resource atmel_lcdfb0_resource[] = {
  1195. {
  1196. .start = 0xff000000,
  1197. .end = 0xff000fff,
  1198. .flags = IORESOURCE_MEM,
  1199. },
  1200. IRQ(1),
  1201. {
  1202. /* Placeholder for pre-allocated fb memory */
  1203. .start = 0x00000000,
  1204. .end = 0x00000000,
  1205. .flags = 0,
  1206. },
  1207. };
  1208. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  1209. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  1210. static struct clk atmel_lcdfb0_pixclk = {
  1211. .name = "lcdc_clk",
  1212. .dev = &atmel_lcdfb0_device.dev,
  1213. .mode = genclk_mode,
  1214. .get_rate = genclk_get_rate,
  1215. .set_rate = genclk_set_rate,
  1216. .set_parent = genclk_set_parent,
  1217. .index = 7,
  1218. };
  1219. struct platform_device *__init
  1220. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  1221. unsigned long fbmem_start, unsigned long fbmem_len,
  1222. u64 pin_mask)
  1223. {
  1224. struct platform_device *pdev;
  1225. struct atmel_lcdfb_info *info;
  1226. struct fb_monspecs *monspecs;
  1227. struct fb_videomode *modedb;
  1228. unsigned int modedb_size;
  1229. u32 portc_mask, portd_mask, porte_mask;
  1230. /*
  1231. * Do a deep copy of the fb data, monspecs and modedb. Make
  1232. * sure all allocations are done before setting up the
  1233. * portmux.
  1234. */
  1235. monspecs = kmemdup(data->default_monspecs,
  1236. sizeof(struct fb_monspecs), GFP_KERNEL);
  1237. if (!monspecs)
  1238. return NULL;
  1239. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  1240. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  1241. if (!modedb)
  1242. goto err_dup_modedb;
  1243. monspecs->modedb = modedb;
  1244. switch (id) {
  1245. case 0:
  1246. pdev = &atmel_lcdfb0_device;
  1247. if (pin_mask == 0ULL)
  1248. /* Default to "full" lcdc control signals and 24bit */
  1249. pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
  1250. /* LCDC on port C */
  1251. portc_mask = pin_mask & 0xfff80000;
  1252. select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
  1253. /* LCDC on port D */
  1254. portd_mask = pin_mask & 0x0003ffff;
  1255. select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
  1256. /* LCDC on port E */
  1257. porte_mask = (pin_mask >> 32) & 0x0007ffff;
  1258. select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
  1259. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  1260. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  1261. break;
  1262. default:
  1263. goto err_invalid_id;
  1264. }
  1265. if (fbmem_len) {
  1266. pdev->resource[2].start = fbmem_start;
  1267. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1268. pdev->resource[2].flags = IORESOURCE_MEM;
  1269. }
  1270. info = pdev->dev.platform_data;
  1271. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  1272. info->default_monspecs = monspecs;
  1273. platform_device_register(pdev);
  1274. return pdev;
  1275. err_invalid_id:
  1276. kfree(modedb);
  1277. err_dup_modedb:
  1278. kfree(monspecs);
  1279. return NULL;
  1280. }
  1281. #endif
  1282. /* --------------------------------------------------------------------
  1283. * PWM
  1284. * -------------------------------------------------------------------- */
  1285. static struct resource atmel_pwm0_resource[] __initdata = {
  1286. PBMEM(0xfff01400),
  1287. IRQ(24),
  1288. };
  1289. static struct clk atmel_pwm0_mck = {
  1290. .name = "pwm_clk",
  1291. .parent = &pbb_clk,
  1292. .mode = pbb_clk_mode,
  1293. .get_rate = pbb_clk_get_rate,
  1294. .index = 5,
  1295. };
  1296. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1297. {
  1298. struct platform_device *pdev;
  1299. u32 pin_mask;
  1300. if (!mask)
  1301. return NULL;
  1302. pdev = platform_device_alloc("atmel_pwm", 0);
  1303. if (!pdev)
  1304. return NULL;
  1305. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1306. ARRAY_SIZE(atmel_pwm0_resource)))
  1307. goto out_free_pdev;
  1308. if (platform_device_add_data(pdev, &mask, sizeof(mask)))
  1309. goto out_free_pdev;
  1310. pin_mask = 0;
  1311. if (mask & (1 << 0))
  1312. pin_mask |= (1 << 28);
  1313. if (mask & (1 << 1))
  1314. pin_mask |= (1 << 29);
  1315. if (pin_mask > 0)
  1316. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1317. pin_mask = 0;
  1318. if (mask & (1 << 2))
  1319. pin_mask |= (1 << 21);
  1320. if (mask & (1 << 3))
  1321. pin_mask |= (1 << 22);
  1322. if (pin_mask > 0)
  1323. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1324. atmel_pwm0_mck.dev = &pdev->dev;
  1325. platform_device_add(pdev);
  1326. return pdev;
  1327. out_free_pdev:
  1328. platform_device_put(pdev);
  1329. return NULL;
  1330. }
  1331. /* --------------------------------------------------------------------
  1332. * SSC
  1333. * -------------------------------------------------------------------- */
  1334. static struct resource ssc0_resource[] = {
  1335. PBMEM(0xffe01c00),
  1336. IRQ(10),
  1337. };
  1338. DEFINE_DEV(ssc, 0);
  1339. DEV_CLK(pclk, ssc0, pba, 7);
  1340. static struct resource ssc1_resource[] = {
  1341. PBMEM(0xffe02000),
  1342. IRQ(11),
  1343. };
  1344. DEFINE_DEV(ssc, 1);
  1345. DEV_CLK(pclk, ssc1, pba, 8);
  1346. static struct resource ssc2_resource[] = {
  1347. PBMEM(0xffe02400),
  1348. IRQ(12),
  1349. };
  1350. DEFINE_DEV(ssc, 2);
  1351. DEV_CLK(pclk, ssc2, pba, 9);
  1352. struct platform_device *__init
  1353. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1354. {
  1355. struct platform_device *pdev;
  1356. u32 pin_mask = 0;
  1357. switch (id) {
  1358. case 0:
  1359. pdev = &ssc0_device;
  1360. if (flags & ATMEL_SSC_RF)
  1361. pin_mask |= (1 << 21); /* RF */
  1362. if (flags & ATMEL_SSC_RK)
  1363. pin_mask |= (1 << 22); /* RK */
  1364. if (flags & ATMEL_SSC_TK)
  1365. pin_mask |= (1 << 23); /* TK */
  1366. if (flags & ATMEL_SSC_TF)
  1367. pin_mask |= (1 << 24); /* TF */
  1368. if (flags & ATMEL_SSC_TD)
  1369. pin_mask |= (1 << 25); /* TD */
  1370. if (flags & ATMEL_SSC_RD)
  1371. pin_mask |= (1 << 26); /* RD */
  1372. if (pin_mask > 0)
  1373. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1374. break;
  1375. case 1:
  1376. pdev = &ssc1_device;
  1377. if (flags & ATMEL_SSC_RF)
  1378. pin_mask |= (1 << 0); /* RF */
  1379. if (flags & ATMEL_SSC_RK)
  1380. pin_mask |= (1 << 1); /* RK */
  1381. if (flags & ATMEL_SSC_TK)
  1382. pin_mask |= (1 << 2); /* TK */
  1383. if (flags & ATMEL_SSC_TF)
  1384. pin_mask |= (1 << 3); /* TF */
  1385. if (flags & ATMEL_SSC_TD)
  1386. pin_mask |= (1 << 4); /* TD */
  1387. if (flags & ATMEL_SSC_RD)
  1388. pin_mask |= (1 << 5); /* RD */
  1389. if (pin_mask > 0)
  1390. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1391. break;
  1392. case 2:
  1393. pdev = &ssc2_device;
  1394. if (flags & ATMEL_SSC_TD)
  1395. pin_mask |= (1 << 13); /* TD */
  1396. if (flags & ATMEL_SSC_RD)
  1397. pin_mask |= (1 << 14); /* RD */
  1398. if (flags & ATMEL_SSC_TK)
  1399. pin_mask |= (1 << 15); /* TK */
  1400. if (flags & ATMEL_SSC_TF)
  1401. pin_mask |= (1 << 16); /* TF */
  1402. if (flags & ATMEL_SSC_RF)
  1403. pin_mask |= (1 << 17); /* RF */
  1404. if (flags & ATMEL_SSC_RK)
  1405. pin_mask |= (1 << 18); /* RK */
  1406. if (pin_mask > 0)
  1407. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1408. break;
  1409. default:
  1410. return NULL;
  1411. }
  1412. platform_device_register(pdev);
  1413. return pdev;
  1414. }
  1415. /* --------------------------------------------------------------------
  1416. * USB Device Controller
  1417. * -------------------------------------------------------------------- */
  1418. static struct resource usba0_resource[] __initdata = {
  1419. {
  1420. .start = 0xff300000,
  1421. .end = 0xff3fffff,
  1422. .flags = IORESOURCE_MEM,
  1423. }, {
  1424. .start = 0xfff03000,
  1425. .end = 0xfff033ff,
  1426. .flags = IORESOURCE_MEM,
  1427. },
  1428. IRQ(31),
  1429. };
  1430. static struct clk usba0_pclk = {
  1431. .name = "pclk",
  1432. .parent = &pbb_clk,
  1433. .mode = pbb_clk_mode,
  1434. .get_rate = pbb_clk_get_rate,
  1435. .index = 12,
  1436. };
  1437. static struct clk usba0_hclk = {
  1438. .name = "hclk",
  1439. .parent = &hsb_clk,
  1440. .mode = hsb_clk_mode,
  1441. .get_rate = hsb_clk_get_rate,
  1442. .index = 6,
  1443. };
  1444. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1445. [idx] = { \
  1446. .name = nam, \
  1447. .index = idx, \
  1448. .fifo_size = maxpkt, \
  1449. .nr_banks = maxbk, \
  1450. .can_dma = dma, \
  1451. .can_isoc = isoc, \
  1452. }
  1453. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1454. EP("ep0", 0, 64, 1, 0, 0),
  1455. EP("ep1", 1, 512, 2, 1, 1),
  1456. EP("ep2", 2, 512, 2, 1, 1),
  1457. EP("ep3-int", 3, 64, 3, 1, 0),
  1458. EP("ep4-int", 4, 64, 3, 1, 0),
  1459. EP("ep5", 5, 1024, 3, 1, 1),
  1460. EP("ep6", 6, 1024, 3, 1, 1),
  1461. };
  1462. #undef EP
  1463. struct platform_device *__init
  1464. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1465. {
  1466. /*
  1467. * pdata doesn't have room for any endpoints, so we need to
  1468. * append room for the ones we need right after it.
  1469. */
  1470. struct {
  1471. struct usba_platform_data pdata;
  1472. struct usba_ep_data ep[7];
  1473. } usba_data;
  1474. struct platform_device *pdev;
  1475. if (id != 0)
  1476. return NULL;
  1477. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1478. if (!pdev)
  1479. return NULL;
  1480. if (platform_device_add_resources(pdev, usba0_resource,
  1481. ARRAY_SIZE(usba0_resource)))
  1482. goto out_free_pdev;
  1483. if (data)
  1484. usba_data.pdata.vbus_pin = data->vbus_pin;
  1485. else
  1486. usba_data.pdata.vbus_pin = -EINVAL;
  1487. data = &usba_data.pdata;
  1488. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1489. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1490. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1491. goto out_free_pdev;
  1492. if (gpio_is_valid(data->vbus_pin))
  1493. at32_select_gpio(data->vbus_pin, 0);
  1494. usba0_pclk.dev = &pdev->dev;
  1495. usba0_hclk.dev = &pdev->dev;
  1496. platform_device_add(pdev);
  1497. return pdev;
  1498. out_free_pdev:
  1499. platform_device_put(pdev);
  1500. return NULL;
  1501. }
  1502. /* --------------------------------------------------------------------
  1503. * IDE / CompactFlash
  1504. * -------------------------------------------------------------------- */
  1505. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1506. static struct resource at32_smc_cs4_resource[] __initdata = {
  1507. {
  1508. .start = 0x04000000,
  1509. .end = 0x07ffffff,
  1510. .flags = IORESOURCE_MEM,
  1511. },
  1512. IRQ(~0UL), /* Magic IRQ will be overridden */
  1513. };
  1514. static struct resource at32_smc_cs5_resource[] __initdata = {
  1515. {
  1516. .start = 0x20000000,
  1517. .end = 0x23ffffff,
  1518. .flags = IORESOURCE_MEM,
  1519. },
  1520. IRQ(~0UL), /* Magic IRQ will be overridden */
  1521. };
  1522. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1523. unsigned int cs, unsigned int extint)
  1524. {
  1525. static unsigned int extint_pin_map[4] __initdata = {
  1526. (1 << 25),
  1527. (1 << 26),
  1528. (1 << 27),
  1529. (1 << 28),
  1530. };
  1531. static bool common_pins_initialized __initdata = false;
  1532. unsigned int extint_pin;
  1533. int ret;
  1534. u32 pin_mask;
  1535. if (extint >= ARRAY_SIZE(extint_pin_map))
  1536. return -EINVAL;
  1537. extint_pin = extint_pin_map[extint];
  1538. switch (cs) {
  1539. case 4:
  1540. ret = platform_device_add_resources(pdev,
  1541. at32_smc_cs4_resource,
  1542. ARRAY_SIZE(at32_smc_cs4_resource));
  1543. if (ret)
  1544. return ret;
  1545. /* NCS4 -> OE_N */
  1546. select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
  1547. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
  1548. break;
  1549. case 5:
  1550. ret = platform_device_add_resources(pdev,
  1551. at32_smc_cs5_resource,
  1552. ARRAY_SIZE(at32_smc_cs5_resource));
  1553. if (ret)
  1554. return ret;
  1555. /* NCS5 -> OE_N */
  1556. select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
  1557. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
  1558. break;
  1559. default:
  1560. return -EINVAL;
  1561. }
  1562. if (!common_pins_initialized) {
  1563. pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
  1564. pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
  1565. pin_mask |= (1 << 23); /* CFRNW -> DIR */
  1566. pin_mask |= (1 << 24); /* NWAIT <- IORDY */
  1567. select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
  1568. common_pins_initialized = true;
  1569. }
  1570. select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
  1571. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1572. pdev->resource[1].end = pdev->resource[1].start;
  1573. return 0;
  1574. }
  1575. struct platform_device *__init
  1576. at32_add_device_ide(unsigned int id, unsigned int extint,
  1577. struct ide_platform_data *data)
  1578. {
  1579. struct platform_device *pdev;
  1580. pdev = platform_device_alloc("at32_ide", id);
  1581. if (!pdev)
  1582. goto fail;
  1583. if (platform_device_add_data(pdev, data,
  1584. sizeof(struct ide_platform_data)))
  1585. goto fail;
  1586. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1587. goto fail;
  1588. platform_device_add(pdev);
  1589. return pdev;
  1590. fail:
  1591. platform_device_put(pdev);
  1592. return NULL;
  1593. }
  1594. struct platform_device *__init
  1595. at32_add_device_cf(unsigned int id, unsigned int extint,
  1596. struct cf_platform_data *data)
  1597. {
  1598. struct platform_device *pdev;
  1599. pdev = platform_device_alloc("at32_cf", id);
  1600. if (!pdev)
  1601. goto fail;
  1602. if (platform_device_add_data(pdev, data,
  1603. sizeof(struct cf_platform_data)))
  1604. goto fail;
  1605. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1606. goto fail;
  1607. if (gpio_is_valid(data->detect_pin))
  1608. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1609. if (gpio_is_valid(data->reset_pin))
  1610. at32_select_gpio(data->reset_pin, 0);
  1611. if (gpio_is_valid(data->vcc_pin))
  1612. at32_select_gpio(data->vcc_pin, 0);
  1613. /* READY is used as extint, so we can't select it as gpio */
  1614. platform_device_add(pdev);
  1615. return pdev;
  1616. fail:
  1617. platform_device_put(pdev);
  1618. return NULL;
  1619. }
  1620. #endif
  1621. /* --------------------------------------------------------------------
  1622. * NAND Flash / SmartMedia
  1623. * -------------------------------------------------------------------- */
  1624. static struct resource smc_cs3_resource[] __initdata = {
  1625. {
  1626. .start = 0x0c000000,
  1627. .end = 0x0fffffff,
  1628. .flags = IORESOURCE_MEM,
  1629. }, {
  1630. .start = 0xfff03c00,
  1631. .end = 0xfff03fff,
  1632. .flags = IORESOURCE_MEM,
  1633. },
  1634. };
  1635. struct platform_device *__init
  1636. at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
  1637. {
  1638. struct platform_device *pdev;
  1639. if (id != 0 || !data)
  1640. return NULL;
  1641. pdev = platform_device_alloc("atmel_nand", id);
  1642. if (!pdev)
  1643. goto fail;
  1644. if (platform_device_add_resources(pdev, smc_cs3_resource,
  1645. ARRAY_SIZE(smc_cs3_resource)))
  1646. goto fail;
  1647. if (platform_device_add_data(pdev, data,
  1648. sizeof(struct atmel_nand_data)))
  1649. goto fail;
  1650. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
  1651. if (data->enable_pin)
  1652. at32_select_gpio(data->enable_pin,
  1653. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  1654. if (data->rdy_pin)
  1655. at32_select_gpio(data->rdy_pin, 0);
  1656. if (data->det_pin)
  1657. at32_select_gpio(data->det_pin, 0);
  1658. platform_device_add(pdev);
  1659. return pdev;
  1660. fail:
  1661. platform_device_put(pdev);
  1662. return NULL;
  1663. }
  1664. /* --------------------------------------------------------------------
  1665. * AC97C
  1666. * -------------------------------------------------------------------- */
  1667. static struct resource atmel_ac97c0_resource[] __initdata = {
  1668. PBMEM(0xfff02800),
  1669. IRQ(29),
  1670. };
  1671. static struct clk atmel_ac97c0_pclk = {
  1672. .name = "pclk",
  1673. .parent = &pbb_clk,
  1674. .mode = pbb_clk_mode,
  1675. .get_rate = pbb_clk_get_rate,
  1676. .index = 10,
  1677. };
  1678. struct platform_device *__init
  1679. at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data,
  1680. unsigned int flags)
  1681. {
  1682. struct platform_device *pdev;
  1683. struct dw_dma_slave *rx_dws;
  1684. struct dw_dma_slave *tx_dws;
  1685. struct ac97c_platform_data _data;
  1686. u32 pin_mask;
  1687. if (id != 0)
  1688. return NULL;
  1689. pdev = platform_device_alloc("atmel_ac97c", id);
  1690. if (!pdev)
  1691. return NULL;
  1692. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1693. ARRAY_SIZE(atmel_ac97c0_resource)))
  1694. goto out_free_resources;
  1695. if (!data) {
  1696. data = &_data;
  1697. memset(data, 0, sizeof(struct ac97c_platform_data));
  1698. data->reset_pin = -ENODEV;
  1699. }
  1700. rx_dws = &data->rx_dws;
  1701. tx_dws = &data->tx_dws;
  1702. /* Check if DMA slave interface for capture should be configured. */
  1703. if (flags & AC97C_CAPTURE) {
  1704. rx_dws->dma_dev = &dw_dmac0_device.dev;
  1705. rx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
  1706. rx_dws->cfg_hi = DWC_CFGH_SRC_PER(3);
  1707. rx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1708. }
  1709. /* Check if DMA slave interface for playback should be configured. */
  1710. if (flags & AC97C_PLAYBACK) {
  1711. tx_dws->dma_dev = &dw_dmac0_device.dev;
  1712. tx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
  1713. tx_dws->cfg_hi = DWC_CFGH_DST_PER(4);
  1714. tx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1715. }
  1716. if (platform_device_add_data(pdev, data,
  1717. sizeof(struct ac97c_platform_data)))
  1718. goto out_free_resources;
  1719. /* SDO | SYNC | SCLK | SDI */
  1720. pin_mask = (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
  1721. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1722. if (gpio_is_valid(data->reset_pin))
  1723. at32_select_gpio(data->reset_pin, AT32_GPIOF_OUTPUT
  1724. | AT32_GPIOF_HIGH);
  1725. atmel_ac97c0_pclk.dev = &pdev->dev;
  1726. platform_device_add(pdev);
  1727. return pdev;
  1728. out_free_resources:
  1729. platform_device_put(pdev);
  1730. return NULL;
  1731. }
  1732. /* --------------------------------------------------------------------
  1733. * ABDAC
  1734. * -------------------------------------------------------------------- */
  1735. static struct resource abdac0_resource[] __initdata = {
  1736. PBMEM(0xfff02000),
  1737. IRQ(27),
  1738. };
  1739. static struct clk abdac0_pclk = {
  1740. .name = "pclk",
  1741. .parent = &pbb_clk,
  1742. .mode = pbb_clk_mode,
  1743. .get_rate = pbb_clk_get_rate,
  1744. .index = 8,
  1745. };
  1746. static struct clk abdac0_sample_clk = {
  1747. .name = "sample_clk",
  1748. .mode = genclk_mode,
  1749. .get_rate = genclk_get_rate,
  1750. .set_rate = genclk_set_rate,
  1751. .set_parent = genclk_set_parent,
  1752. .index = 6,
  1753. };
  1754. struct platform_device *__init
  1755. at32_add_device_abdac(unsigned int id, struct atmel_abdac_pdata *data)
  1756. {
  1757. struct platform_device *pdev;
  1758. struct dw_dma_slave *dws;
  1759. u32 pin_mask;
  1760. if (id != 0 || !data)
  1761. return NULL;
  1762. pdev = platform_device_alloc("atmel_abdac", id);
  1763. if (!pdev)
  1764. return NULL;
  1765. if (platform_device_add_resources(pdev, abdac0_resource,
  1766. ARRAY_SIZE(abdac0_resource)))
  1767. goto out_free_resources;
  1768. dws = &data->dws;
  1769. dws->dma_dev = &dw_dmac0_device.dev;
  1770. dws->reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
  1771. dws->cfg_hi = DWC_CFGH_DST_PER(2);
  1772. dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1773. if (platform_device_add_data(pdev, data,
  1774. sizeof(struct atmel_abdac_pdata)))
  1775. goto out_free_resources;
  1776. pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
  1777. pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
  1778. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1779. abdac0_pclk.dev = &pdev->dev;
  1780. abdac0_sample_clk.dev = &pdev->dev;
  1781. platform_device_add(pdev);
  1782. return pdev;
  1783. out_free_resources:
  1784. platform_device_put(pdev);
  1785. return NULL;
  1786. }
  1787. /* --------------------------------------------------------------------
  1788. * GCLK
  1789. * -------------------------------------------------------------------- */
  1790. static struct clk gclk0 = {
  1791. .name = "gclk0",
  1792. .mode = genclk_mode,
  1793. .get_rate = genclk_get_rate,
  1794. .set_rate = genclk_set_rate,
  1795. .set_parent = genclk_set_parent,
  1796. .index = 0,
  1797. };
  1798. static struct clk gclk1 = {
  1799. .name = "gclk1",
  1800. .mode = genclk_mode,
  1801. .get_rate = genclk_get_rate,
  1802. .set_rate = genclk_set_rate,
  1803. .set_parent = genclk_set_parent,
  1804. .index = 1,
  1805. };
  1806. static struct clk gclk2 = {
  1807. .name = "gclk2",
  1808. .mode = genclk_mode,
  1809. .get_rate = genclk_get_rate,
  1810. .set_rate = genclk_set_rate,
  1811. .set_parent = genclk_set_parent,
  1812. .index = 2,
  1813. };
  1814. static struct clk gclk3 = {
  1815. .name = "gclk3",
  1816. .mode = genclk_mode,
  1817. .get_rate = genclk_get_rate,
  1818. .set_rate = genclk_set_rate,
  1819. .set_parent = genclk_set_parent,
  1820. .index = 3,
  1821. };
  1822. static struct clk gclk4 = {
  1823. .name = "gclk4",
  1824. .mode = genclk_mode,
  1825. .get_rate = genclk_get_rate,
  1826. .set_rate = genclk_set_rate,
  1827. .set_parent = genclk_set_parent,
  1828. .index = 4,
  1829. };
  1830. static __initdata struct clk *init_clocks[] = {
  1831. &osc32k,
  1832. &osc0,
  1833. &osc1,
  1834. &pll0,
  1835. &pll1,
  1836. &cpu_clk,
  1837. &hsb_clk,
  1838. &pba_clk,
  1839. &pbb_clk,
  1840. &at32_pm_pclk,
  1841. &at32_intc0_pclk,
  1842. &at32_hmatrix_clk,
  1843. &ebi_clk,
  1844. &hramc_clk,
  1845. &sdramc_clk,
  1846. &smc0_pclk,
  1847. &smc0_mck,
  1848. &pdc_hclk,
  1849. &pdc_pclk,
  1850. &dw_dmac0_hclk,
  1851. &pico_clk,
  1852. &pio0_mck,
  1853. &pio1_mck,
  1854. &pio2_mck,
  1855. &pio3_mck,
  1856. &pio4_mck,
  1857. &at32_tcb0_t0_clk,
  1858. &at32_tcb1_t0_clk,
  1859. &atmel_psif0_pclk,
  1860. &atmel_psif1_pclk,
  1861. &atmel_usart0_usart,
  1862. &atmel_usart1_usart,
  1863. &atmel_usart2_usart,
  1864. &atmel_usart3_usart,
  1865. &atmel_pwm0_mck,
  1866. #if defined(CONFIG_CPU_AT32AP7000)
  1867. &macb0_hclk,
  1868. &macb0_pclk,
  1869. &macb1_hclk,
  1870. &macb1_pclk,
  1871. #endif
  1872. &atmel_spi0_spi_clk,
  1873. &atmel_spi1_spi_clk,
  1874. &atmel_twi0_pclk,
  1875. &atmel_mci0_pclk,
  1876. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1877. &atmel_lcdfb0_hck1,
  1878. &atmel_lcdfb0_pixclk,
  1879. #endif
  1880. &ssc0_pclk,
  1881. &ssc1_pclk,
  1882. &ssc2_pclk,
  1883. &usba0_hclk,
  1884. &usba0_pclk,
  1885. &atmel_ac97c0_pclk,
  1886. &abdac0_pclk,
  1887. &abdac0_sample_clk,
  1888. &gclk0,
  1889. &gclk1,
  1890. &gclk2,
  1891. &gclk3,
  1892. &gclk4,
  1893. };
  1894. void __init setup_platform(void)
  1895. {
  1896. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1897. int i;
  1898. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1899. main_clock = &pll0;
  1900. cpu_clk.parent = &pll0;
  1901. } else {
  1902. main_clock = &osc0;
  1903. cpu_clk.parent = &osc0;
  1904. }
  1905. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1906. pll0.parent = &osc1;
  1907. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1908. pll1.parent = &osc1;
  1909. genclk_init_parent(&gclk0);
  1910. genclk_init_parent(&gclk1);
  1911. genclk_init_parent(&gclk2);
  1912. genclk_init_parent(&gclk3);
  1913. genclk_init_parent(&gclk4);
  1914. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1915. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1916. #endif
  1917. genclk_init_parent(&abdac0_sample_clk);
  1918. /*
  1919. * Build initial dynamic clock list by registering all clocks
  1920. * from the array.
  1921. * At the same time, turn on all clocks that have at least one
  1922. * user already, and turn off everything else. We only do this
  1923. * for module clocks, and even though it isn't particularly
  1924. * pretty to check the address of the mode function, it should
  1925. * do the trick...
  1926. */
  1927. for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
  1928. struct clk *clk = init_clocks[i];
  1929. /* first, register clock */
  1930. at32_clk_register(clk);
  1931. if (clk->users == 0)
  1932. continue;
  1933. if (clk->mode == &cpu_clk_mode)
  1934. cpu_mask |= 1 << clk->index;
  1935. else if (clk->mode == &hsb_clk_mode)
  1936. hsb_mask |= 1 << clk->index;
  1937. else if (clk->mode == &pba_clk_mode)
  1938. pba_mask |= 1 << clk->index;
  1939. else if (clk->mode == &pbb_clk_mode)
  1940. pbb_mask |= 1 << clk->index;
  1941. }
  1942. pm_writel(CPU_MASK, cpu_mask);
  1943. pm_writel(HSB_MASK, hsb_mask);
  1944. pm_writel(PBA_MASK, pba_mask);
  1945. pm_writel(PBB_MASK, pbb_mask);
  1946. /* Initialize the port muxes */
  1947. at32_init_pio(&pio0_device);
  1948. at32_init_pio(&pio1_device);
  1949. at32_init_pio(&pio2_device);
  1950. at32_init_pio(&pio3_device);
  1951. at32_init_pio(&pio4_device);
  1952. }
  1953. struct gen_pool *sram_pool;
  1954. static int __init sram_init(void)
  1955. {
  1956. struct gen_pool *pool;
  1957. /* 1KiB granularity */
  1958. pool = gen_pool_create(10, -1);
  1959. if (!pool)
  1960. goto fail;
  1961. if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
  1962. goto err_pool_add;
  1963. sram_pool = pool;
  1964. return 0;
  1965. err_pool_add:
  1966. gen_pool_destroy(pool);
  1967. fail:
  1968. pr_err("Failed to create SRAM pool\n");
  1969. return -ENOMEM;
  1970. }
  1971. core_initcall(sram_init);