at32ap700x.c 53 KB

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