at32ap700x.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  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. };
  499. static struct resource dw_dmac0_resource[] = {
  500. PBMEM(0xff200000),
  501. IRQ(2),
  502. };
  503. DEFINE_DEV_DATA(dw_dmac, 0);
  504. DEV_CLK(hclk, dw_dmac0, hsb, 10);
  505. /* --------------------------------------------------------------------
  506. * System peripherals
  507. * -------------------------------------------------------------------- */
  508. static struct resource at32_pm0_resource[] = {
  509. {
  510. .start = 0xfff00000,
  511. .end = 0xfff0007f,
  512. .flags = IORESOURCE_MEM,
  513. },
  514. IRQ(20),
  515. };
  516. static struct resource at32ap700x_rtc0_resource[] = {
  517. {
  518. .start = 0xfff00080,
  519. .end = 0xfff000af,
  520. .flags = IORESOURCE_MEM,
  521. },
  522. IRQ(21),
  523. };
  524. static struct resource at32_wdt0_resource[] = {
  525. {
  526. .start = 0xfff000b0,
  527. .end = 0xfff000cf,
  528. .flags = IORESOURCE_MEM,
  529. },
  530. };
  531. static struct resource at32_eic0_resource[] = {
  532. {
  533. .start = 0xfff00100,
  534. .end = 0xfff0013f,
  535. .flags = IORESOURCE_MEM,
  536. },
  537. IRQ(19),
  538. };
  539. DEFINE_DEV(at32_pm, 0);
  540. DEFINE_DEV(at32ap700x_rtc, 0);
  541. DEFINE_DEV(at32_wdt, 0);
  542. DEFINE_DEV(at32_eic, 0);
  543. /*
  544. * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this
  545. * is always running.
  546. */
  547. static struct clk at32_pm_pclk = {
  548. .name = "pclk",
  549. .dev = &at32_pm0_device.dev,
  550. .parent = &pbb_clk,
  551. .mode = pbb_clk_mode,
  552. .get_rate = pbb_clk_get_rate,
  553. .users = 1,
  554. .index = 0,
  555. };
  556. static struct resource intc0_resource[] = {
  557. PBMEM(0xfff00400),
  558. };
  559. struct platform_device at32_intc0_device = {
  560. .name = "intc",
  561. .id = 0,
  562. .resource = intc0_resource,
  563. .num_resources = ARRAY_SIZE(intc0_resource),
  564. };
  565. DEV_CLK(pclk, at32_intc0, pbb, 1);
  566. static struct clk ebi_clk = {
  567. .name = "ebi",
  568. .parent = &hsb_clk,
  569. .mode = hsb_clk_mode,
  570. .get_rate = hsb_clk_get_rate,
  571. .users = 1,
  572. };
  573. static struct clk hramc_clk = {
  574. .name = "hramc",
  575. .parent = &hsb_clk,
  576. .mode = hsb_clk_mode,
  577. .get_rate = hsb_clk_get_rate,
  578. .users = 1,
  579. .index = 3,
  580. };
  581. static struct clk sdramc_clk = {
  582. .name = "sdramc_clk",
  583. .parent = &pbb_clk,
  584. .mode = pbb_clk_mode,
  585. .get_rate = pbb_clk_get_rate,
  586. .users = 1,
  587. .index = 14,
  588. };
  589. static struct resource smc0_resource[] = {
  590. PBMEM(0xfff03400),
  591. };
  592. DEFINE_DEV(smc, 0);
  593. DEV_CLK(pclk, smc0, pbb, 13);
  594. DEV_CLK(mck, smc0, hsb, 0);
  595. static struct platform_device pdc_device = {
  596. .name = "pdc",
  597. .id = 0,
  598. };
  599. DEV_CLK(hclk, pdc, hsb, 4);
  600. DEV_CLK(pclk, pdc, pba, 16);
  601. static struct clk pico_clk = {
  602. .name = "pico",
  603. .parent = &cpu_clk,
  604. .mode = cpu_clk_mode,
  605. .get_rate = cpu_clk_get_rate,
  606. .users = 1,
  607. };
  608. /* --------------------------------------------------------------------
  609. * HMATRIX
  610. * -------------------------------------------------------------------- */
  611. struct clk at32_hmatrix_clk = {
  612. .name = "hmatrix_clk",
  613. .parent = &pbb_clk,
  614. .mode = pbb_clk_mode,
  615. .get_rate = pbb_clk_get_rate,
  616. .index = 2,
  617. .users = 1,
  618. };
  619. /*
  620. * Set bits in the HMATRIX Special Function Register (SFR) used by the
  621. * External Bus Interface (EBI). This can be used to enable special
  622. * features like CompactFlash support, NAND Flash support, etc. on
  623. * certain chipselects.
  624. */
  625. static inline void set_ebi_sfr_bits(u32 mask)
  626. {
  627. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);
  628. }
  629. /* --------------------------------------------------------------------
  630. * Timer/Counter (TC)
  631. * -------------------------------------------------------------------- */
  632. static struct resource at32_tcb0_resource[] = {
  633. PBMEM(0xfff00c00),
  634. IRQ(22),
  635. };
  636. static struct platform_device at32_tcb0_device = {
  637. .name = "atmel_tcb",
  638. .id = 0,
  639. .resource = at32_tcb0_resource,
  640. .num_resources = ARRAY_SIZE(at32_tcb0_resource),
  641. };
  642. DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
  643. static struct resource at32_tcb1_resource[] = {
  644. PBMEM(0xfff01000),
  645. IRQ(23),
  646. };
  647. static struct platform_device at32_tcb1_device = {
  648. .name = "atmel_tcb",
  649. .id = 1,
  650. .resource = at32_tcb1_resource,
  651. .num_resources = ARRAY_SIZE(at32_tcb1_resource),
  652. };
  653. DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
  654. /* --------------------------------------------------------------------
  655. * PIO
  656. * -------------------------------------------------------------------- */
  657. static struct resource pio0_resource[] = {
  658. PBMEM(0xffe02800),
  659. IRQ(13),
  660. };
  661. DEFINE_DEV(pio, 0);
  662. DEV_CLK(mck, pio0, pba, 10);
  663. static struct resource pio1_resource[] = {
  664. PBMEM(0xffe02c00),
  665. IRQ(14),
  666. };
  667. DEFINE_DEV(pio, 1);
  668. DEV_CLK(mck, pio1, pba, 11);
  669. static struct resource pio2_resource[] = {
  670. PBMEM(0xffe03000),
  671. IRQ(15),
  672. };
  673. DEFINE_DEV(pio, 2);
  674. DEV_CLK(mck, pio2, pba, 12);
  675. static struct resource pio3_resource[] = {
  676. PBMEM(0xffe03400),
  677. IRQ(16),
  678. };
  679. DEFINE_DEV(pio, 3);
  680. DEV_CLK(mck, pio3, pba, 13);
  681. static struct resource pio4_resource[] = {
  682. PBMEM(0xffe03800),
  683. IRQ(17),
  684. };
  685. DEFINE_DEV(pio, 4);
  686. DEV_CLK(mck, pio4, pba, 14);
  687. static int __init system_device_init(void)
  688. {
  689. platform_device_register(&at32_pm0_device);
  690. platform_device_register(&at32_intc0_device);
  691. platform_device_register(&at32ap700x_rtc0_device);
  692. platform_device_register(&at32_wdt0_device);
  693. platform_device_register(&at32_eic0_device);
  694. platform_device_register(&smc0_device);
  695. platform_device_register(&pdc_device);
  696. platform_device_register(&dw_dmac0_device);
  697. platform_device_register(&at32_tcb0_device);
  698. platform_device_register(&at32_tcb1_device);
  699. platform_device_register(&pio0_device);
  700. platform_device_register(&pio1_device);
  701. platform_device_register(&pio2_device);
  702. platform_device_register(&pio3_device);
  703. platform_device_register(&pio4_device);
  704. return 0;
  705. }
  706. core_initcall(system_device_init);
  707. /* --------------------------------------------------------------------
  708. * PSIF
  709. * -------------------------------------------------------------------- */
  710. static struct resource atmel_psif0_resource[] __initdata = {
  711. {
  712. .start = 0xffe03c00,
  713. .end = 0xffe03cff,
  714. .flags = IORESOURCE_MEM,
  715. },
  716. IRQ(18),
  717. };
  718. static struct clk atmel_psif0_pclk = {
  719. .name = "pclk",
  720. .parent = &pba_clk,
  721. .mode = pba_clk_mode,
  722. .get_rate = pba_clk_get_rate,
  723. .index = 15,
  724. };
  725. static struct resource atmel_psif1_resource[] __initdata = {
  726. {
  727. .start = 0xffe03d00,
  728. .end = 0xffe03dff,
  729. .flags = IORESOURCE_MEM,
  730. },
  731. IRQ(18),
  732. };
  733. static struct clk atmel_psif1_pclk = {
  734. .name = "pclk",
  735. .parent = &pba_clk,
  736. .mode = pba_clk_mode,
  737. .get_rate = pba_clk_get_rate,
  738. .index = 15,
  739. };
  740. struct platform_device *__init at32_add_device_psif(unsigned int id)
  741. {
  742. struct platform_device *pdev;
  743. u32 pin_mask;
  744. if (!(id == 0 || id == 1))
  745. return NULL;
  746. pdev = platform_device_alloc("atmel_psif", id);
  747. if (!pdev)
  748. return NULL;
  749. switch (id) {
  750. case 0:
  751. pin_mask = (1 << 8) | (1 << 9); /* CLOCK & DATA */
  752. if (platform_device_add_resources(pdev, atmel_psif0_resource,
  753. ARRAY_SIZE(atmel_psif0_resource)))
  754. goto err_add_resources;
  755. atmel_psif0_pclk.dev = &pdev->dev;
  756. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  757. break;
  758. case 1:
  759. pin_mask = (1 << 11) | (1 << 12); /* CLOCK & DATA */
  760. if (platform_device_add_resources(pdev, atmel_psif1_resource,
  761. ARRAY_SIZE(atmel_psif1_resource)))
  762. goto err_add_resources;
  763. atmel_psif1_pclk.dev = &pdev->dev;
  764. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  765. break;
  766. default:
  767. return NULL;
  768. }
  769. platform_device_add(pdev);
  770. return pdev;
  771. err_add_resources:
  772. platform_device_put(pdev);
  773. return NULL;
  774. }
  775. /* --------------------------------------------------------------------
  776. * USART
  777. * -------------------------------------------------------------------- */
  778. static struct atmel_uart_data atmel_usart0_data = {
  779. .use_dma_tx = 1,
  780. .use_dma_rx = 1,
  781. };
  782. static struct resource atmel_usart0_resource[] = {
  783. PBMEM(0xffe00c00),
  784. IRQ(6),
  785. };
  786. DEFINE_DEV_DATA(atmel_usart, 0);
  787. DEV_CLK(usart, atmel_usart0, pba, 3);
  788. static struct atmel_uart_data atmel_usart1_data = {
  789. .use_dma_tx = 1,
  790. .use_dma_rx = 1,
  791. };
  792. static struct resource atmel_usart1_resource[] = {
  793. PBMEM(0xffe01000),
  794. IRQ(7),
  795. };
  796. DEFINE_DEV_DATA(atmel_usart, 1);
  797. DEV_CLK(usart, atmel_usart1, pba, 4);
  798. static struct atmel_uart_data atmel_usart2_data = {
  799. .use_dma_tx = 1,
  800. .use_dma_rx = 1,
  801. };
  802. static struct resource atmel_usart2_resource[] = {
  803. PBMEM(0xffe01400),
  804. IRQ(8),
  805. };
  806. DEFINE_DEV_DATA(atmel_usart, 2);
  807. DEV_CLK(usart, atmel_usart2, pba, 5);
  808. static struct atmel_uart_data atmel_usart3_data = {
  809. .use_dma_tx = 1,
  810. .use_dma_rx = 1,
  811. };
  812. static struct resource atmel_usart3_resource[] = {
  813. PBMEM(0xffe01800),
  814. IRQ(9),
  815. };
  816. DEFINE_DEV_DATA(atmel_usart, 3);
  817. DEV_CLK(usart, atmel_usart3, pba, 6);
  818. static inline void configure_usart0_pins(int flags)
  819. {
  820. u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */
  821. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 6);
  822. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 7);
  823. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 10);
  824. select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  825. }
  826. static inline void configure_usart1_pins(int flags)
  827. {
  828. u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */
  829. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 19);
  830. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 20);
  831. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 16);
  832. select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);
  833. }
  834. static inline void configure_usart2_pins(int flags)
  835. {
  836. u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */
  837. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 30);
  838. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 29);
  839. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 28);
  840. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  841. }
  842. static inline void configure_usart3_pins(int flags)
  843. {
  844. u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */
  845. if (flags & ATMEL_USART_RTS) pin_mask |= (1 << 16);
  846. if (flags & ATMEL_USART_CTS) pin_mask |= (1 << 15);
  847. if (flags & ATMEL_USART_CLK) pin_mask |= (1 << 19);
  848. select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);
  849. }
  850. static struct platform_device *__initdata at32_usarts[4];
  851. void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags)
  852. {
  853. struct platform_device *pdev;
  854. switch (hw_id) {
  855. case 0:
  856. pdev = &atmel_usart0_device;
  857. configure_usart0_pins(flags);
  858. break;
  859. case 1:
  860. pdev = &atmel_usart1_device;
  861. configure_usart1_pins(flags);
  862. break;
  863. case 2:
  864. pdev = &atmel_usart2_device;
  865. configure_usart2_pins(flags);
  866. break;
  867. case 3:
  868. pdev = &atmel_usart3_device;
  869. configure_usart3_pins(flags);
  870. break;
  871. default:
  872. return;
  873. }
  874. if (PXSEG(pdev->resource[0].start) == P4SEG) {
  875. /* Addresses in the P4 segment are permanently mapped 1:1 */
  876. struct atmel_uart_data *data = pdev->dev.platform_data;
  877. data->regs = (void __iomem *)pdev->resource[0].start;
  878. }
  879. pdev->id = line;
  880. at32_usarts[line] = pdev;
  881. }
  882. struct platform_device *__init at32_add_device_usart(unsigned int id)
  883. {
  884. platform_device_register(at32_usarts[id]);
  885. return at32_usarts[id];
  886. }
  887. struct platform_device *atmel_default_console_device;
  888. void __init at32_setup_serial_console(unsigned int usart_id)
  889. {
  890. atmel_default_console_device = at32_usarts[usart_id];
  891. }
  892. /* --------------------------------------------------------------------
  893. * Ethernet
  894. * -------------------------------------------------------------------- */
  895. #ifdef CONFIG_CPU_AT32AP7000
  896. static struct eth_platform_data macb0_data;
  897. static struct resource macb0_resource[] = {
  898. PBMEM(0xfff01800),
  899. IRQ(25),
  900. };
  901. DEFINE_DEV_DATA(macb, 0);
  902. DEV_CLK(hclk, macb0, hsb, 8);
  903. DEV_CLK(pclk, macb0, pbb, 6);
  904. static struct eth_platform_data macb1_data;
  905. static struct resource macb1_resource[] = {
  906. PBMEM(0xfff01c00),
  907. IRQ(26),
  908. };
  909. DEFINE_DEV_DATA(macb, 1);
  910. DEV_CLK(hclk, macb1, hsb, 9);
  911. DEV_CLK(pclk, macb1, pbb, 7);
  912. struct platform_device *__init
  913. at32_add_device_eth(unsigned int id, struct eth_platform_data *data)
  914. {
  915. struct platform_device *pdev;
  916. u32 pin_mask;
  917. switch (id) {
  918. case 0:
  919. pdev = &macb0_device;
  920. pin_mask = (1 << 3); /* TXD0 */
  921. pin_mask |= (1 << 4); /* TXD1 */
  922. pin_mask |= (1 << 7); /* TXEN */
  923. pin_mask |= (1 << 8); /* TXCK */
  924. pin_mask |= (1 << 9); /* RXD0 */
  925. pin_mask |= (1 << 10); /* RXD1 */
  926. pin_mask |= (1 << 13); /* RXER */
  927. pin_mask |= (1 << 15); /* RXDV */
  928. pin_mask |= (1 << 16); /* MDC */
  929. pin_mask |= (1 << 17); /* MDIO */
  930. if (!data->is_rmii) {
  931. pin_mask |= (1 << 0); /* COL */
  932. pin_mask |= (1 << 1); /* CRS */
  933. pin_mask |= (1 << 2); /* TXER */
  934. pin_mask |= (1 << 5); /* TXD2 */
  935. pin_mask |= (1 << 6); /* TXD3 */
  936. pin_mask |= (1 << 11); /* RXD2 */
  937. pin_mask |= (1 << 12); /* RXD3 */
  938. pin_mask |= (1 << 14); /* RXCK */
  939. #ifndef CONFIG_BOARD_MIMC200
  940. pin_mask |= (1 << 18); /* SPD */
  941. #endif
  942. }
  943. select_peripheral(PIOC, pin_mask, PERIPH_A, 0);
  944. break;
  945. case 1:
  946. pdev = &macb1_device;
  947. pin_mask = (1 << 13); /* TXD0 */
  948. pin_mask |= (1 << 14); /* TXD1 */
  949. pin_mask |= (1 << 11); /* TXEN */
  950. pin_mask |= (1 << 12); /* TXCK */
  951. pin_mask |= (1 << 10); /* RXD0 */
  952. pin_mask |= (1 << 6); /* RXD1 */
  953. pin_mask |= (1 << 5); /* RXER */
  954. pin_mask |= (1 << 4); /* RXDV */
  955. pin_mask |= (1 << 3); /* MDC */
  956. pin_mask |= (1 << 2); /* MDIO */
  957. #ifndef CONFIG_BOARD_MIMC200
  958. if (!data->is_rmii)
  959. pin_mask |= (1 << 15); /* SPD */
  960. #endif
  961. select_peripheral(PIOD, pin_mask, PERIPH_B, 0);
  962. if (!data->is_rmii) {
  963. pin_mask = (1 << 19); /* COL */
  964. pin_mask |= (1 << 23); /* CRS */
  965. pin_mask |= (1 << 26); /* TXER */
  966. pin_mask |= (1 << 27); /* TXD2 */
  967. pin_mask |= (1 << 28); /* TXD3 */
  968. pin_mask |= (1 << 29); /* RXD2 */
  969. pin_mask |= (1 << 30); /* RXD3 */
  970. pin_mask |= (1 << 24); /* RXCK */
  971. select_peripheral(PIOC, pin_mask, PERIPH_B, 0);
  972. }
  973. break;
  974. default:
  975. return NULL;
  976. }
  977. memcpy(pdev->dev.platform_data, data, sizeof(struct eth_platform_data));
  978. platform_device_register(pdev);
  979. return pdev;
  980. }
  981. #endif
  982. /* --------------------------------------------------------------------
  983. * SPI
  984. * -------------------------------------------------------------------- */
  985. static struct resource atmel_spi0_resource[] = {
  986. PBMEM(0xffe00000),
  987. IRQ(3),
  988. };
  989. DEFINE_DEV(atmel_spi, 0);
  990. DEV_CLK(spi_clk, atmel_spi0, pba, 0);
  991. static struct resource atmel_spi1_resource[] = {
  992. PBMEM(0xffe00400),
  993. IRQ(4),
  994. };
  995. DEFINE_DEV(atmel_spi, 1);
  996. DEV_CLK(spi_clk, atmel_spi1, pba, 1);
  997. void __init
  998. at32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b, unsigned int n)
  999. {
  1000. /*
  1001. * Manage the chipselects as GPIOs, normally using the same pins
  1002. * the SPI controller expects; but boards can use other pins.
  1003. */
  1004. static u8 __initdata spi_pins[][4] = {
  1005. { GPIO_PIN_PA(3), GPIO_PIN_PA(4),
  1006. GPIO_PIN_PA(5), GPIO_PIN_PA(20) },
  1007. { GPIO_PIN_PB(2), GPIO_PIN_PB(3),
  1008. GPIO_PIN_PB(4), GPIO_PIN_PA(27) },
  1009. };
  1010. unsigned int pin, mode;
  1011. /* There are only 2 SPI controllers */
  1012. if (bus_num > 1)
  1013. return;
  1014. for (; n; n--, b++) {
  1015. b->bus_num = bus_num;
  1016. if (b->chip_select >= 4)
  1017. continue;
  1018. pin = (unsigned)b->controller_data;
  1019. if (!pin) {
  1020. pin = spi_pins[bus_num][b->chip_select];
  1021. b->controller_data = (void *)pin;
  1022. }
  1023. mode = AT32_GPIOF_OUTPUT;
  1024. if (!(b->mode & SPI_CS_HIGH))
  1025. mode |= AT32_GPIOF_HIGH;
  1026. at32_select_gpio(pin, mode);
  1027. }
  1028. }
  1029. struct platform_device *__init
  1030. at32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n)
  1031. {
  1032. struct platform_device *pdev;
  1033. u32 pin_mask;
  1034. switch (id) {
  1035. case 0:
  1036. pdev = &atmel_spi0_device;
  1037. pin_mask = (1 << 1) | (1 << 2); /* MOSI & SCK */
  1038. /* pullup MISO so a level is always defined */
  1039. select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);
  1040. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1041. at32_spi_setup_slaves(0, b, n);
  1042. break;
  1043. case 1:
  1044. pdev = &atmel_spi1_device;
  1045. pin_mask = (1 << 1) | (1 << 5); /* MOSI */
  1046. /* pullup MISO so a level is always defined */
  1047. select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);
  1048. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1049. at32_spi_setup_slaves(1, b, n);
  1050. break;
  1051. default:
  1052. return NULL;
  1053. }
  1054. spi_register_board_info(b, n);
  1055. platform_device_register(pdev);
  1056. return pdev;
  1057. }
  1058. /* --------------------------------------------------------------------
  1059. * TWI
  1060. * -------------------------------------------------------------------- */
  1061. static struct resource atmel_twi0_resource[] __initdata = {
  1062. PBMEM(0xffe00800),
  1063. IRQ(5),
  1064. };
  1065. static struct clk atmel_twi0_pclk = {
  1066. .name = "twi_pclk",
  1067. .parent = &pba_clk,
  1068. .mode = pba_clk_mode,
  1069. .get_rate = pba_clk_get_rate,
  1070. .index = 2,
  1071. };
  1072. struct platform_device *__init at32_add_device_twi(unsigned int id,
  1073. struct i2c_board_info *b,
  1074. unsigned int n)
  1075. {
  1076. struct platform_device *pdev;
  1077. u32 pin_mask;
  1078. if (id != 0)
  1079. return NULL;
  1080. pdev = platform_device_alloc("atmel_twi", id);
  1081. if (!pdev)
  1082. return NULL;
  1083. if (platform_device_add_resources(pdev, atmel_twi0_resource,
  1084. ARRAY_SIZE(atmel_twi0_resource)))
  1085. goto err_add_resources;
  1086. pin_mask = (1 << 6) | (1 << 7); /* SDA & SDL */
  1087. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1088. atmel_twi0_pclk.dev = &pdev->dev;
  1089. if (b)
  1090. i2c_register_board_info(id, b, n);
  1091. platform_device_add(pdev);
  1092. return pdev;
  1093. err_add_resources:
  1094. platform_device_put(pdev);
  1095. return NULL;
  1096. }
  1097. /* --------------------------------------------------------------------
  1098. * MMC
  1099. * -------------------------------------------------------------------- */
  1100. static struct resource atmel_mci0_resource[] __initdata = {
  1101. PBMEM(0xfff02400),
  1102. IRQ(28),
  1103. };
  1104. static struct clk atmel_mci0_pclk = {
  1105. .name = "mci_clk",
  1106. .parent = &pbb_clk,
  1107. .mode = pbb_clk_mode,
  1108. .get_rate = pbb_clk_get_rate,
  1109. .index = 9,
  1110. };
  1111. struct platform_device *__init
  1112. at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
  1113. {
  1114. struct platform_device *pdev;
  1115. struct mci_dma_data *slave;
  1116. u32 pioa_mask;
  1117. u32 piob_mask;
  1118. if (id != 0 || !data)
  1119. return NULL;
  1120. /* Must have at least one usable slot */
  1121. if (!data->slot[0].bus_width && !data->slot[1].bus_width)
  1122. return NULL;
  1123. pdev = platform_device_alloc("atmel_mci", id);
  1124. if (!pdev)
  1125. goto fail;
  1126. if (platform_device_add_resources(pdev, atmel_mci0_resource,
  1127. ARRAY_SIZE(atmel_mci0_resource)))
  1128. goto fail;
  1129. slave = kzalloc(sizeof(struct mci_dma_data), GFP_KERNEL);
  1130. if (!slave)
  1131. goto fail;
  1132. slave->sdata.dma_dev = &dw_dmac0_device.dev;
  1133. slave->sdata.reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
  1134. slave->sdata.cfg_hi = (DWC_CFGH_SRC_PER(0)
  1135. | DWC_CFGH_DST_PER(1));
  1136. slave->sdata.cfg_lo &= ~(DWC_CFGL_HS_DST_POL
  1137. | DWC_CFGL_HS_SRC_POL);
  1138. data->dma_slave = slave;
  1139. if (platform_device_add_data(pdev, data,
  1140. sizeof(struct mci_platform_data)))
  1141. goto fail_free;
  1142. /* CLK line is common to both slots */
  1143. pioa_mask = 1 << 10;
  1144. switch (data->slot[0].bus_width) {
  1145. case 4:
  1146. pioa_mask |= 1 << 13; /* DATA1 */
  1147. pioa_mask |= 1 << 14; /* DATA2 */
  1148. pioa_mask |= 1 << 15; /* DATA3 */
  1149. /* fall through */
  1150. case 1:
  1151. pioa_mask |= 1 << 11; /* CMD */
  1152. pioa_mask |= 1 << 12; /* DATA0 */
  1153. if (gpio_is_valid(data->slot[0].detect_pin))
  1154. at32_select_gpio(data->slot[0].detect_pin, 0);
  1155. if (gpio_is_valid(data->slot[0].wp_pin))
  1156. at32_select_gpio(data->slot[0].wp_pin, 0);
  1157. break;
  1158. case 0:
  1159. /* Slot is unused */
  1160. break;
  1161. default:
  1162. goto fail_free;
  1163. }
  1164. select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);
  1165. piob_mask = 0;
  1166. switch (data->slot[1].bus_width) {
  1167. case 4:
  1168. piob_mask |= 1 << 8; /* DATA1 */
  1169. piob_mask |= 1 << 9; /* DATA2 */
  1170. piob_mask |= 1 << 10; /* DATA3 */
  1171. /* fall through */
  1172. case 1:
  1173. piob_mask |= 1 << 6; /* CMD */
  1174. piob_mask |= 1 << 7; /* DATA0 */
  1175. select_peripheral(PIOB, piob_mask, PERIPH_B, 0);
  1176. if (gpio_is_valid(data->slot[1].detect_pin))
  1177. at32_select_gpio(data->slot[1].detect_pin, 0);
  1178. if (gpio_is_valid(data->slot[1].wp_pin))
  1179. at32_select_gpio(data->slot[1].wp_pin, 0);
  1180. break;
  1181. case 0:
  1182. /* Slot is unused */
  1183. break;
  1184. default:
  1185. if (!data->slot[0].bus_width)
  1186. goto fail_free;
  1187. data->slot[1].bus_width = 0;
  1188. break;
  1189. }
  1190. atmel_mci0_pclk.dev = &pdev->dev;
  1191. platform_device_add(pdev);
  1192. return pdev;
  1193. fail_free:
  1194. kfree(slave);
  1195. fail:
  1196. data->dma_slave = NULL;
  1197. platform_device_put(pdev);
  1198. return NULL;
  1199. }
  1200. /* --------------------------------------------------------------------
  1201. * LCDC
  1202. * -------------------------------------------------------------------- */
  1203. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1204. static struct atmel_lcdfb_info atmel_lcdfb0_data;
  1205. static struct resource atmel_lcdfb0_resource[] = {
  1206. {
  1207. .start = 0xff000000,
  1208. .end = 0xff000fff,
  1209. .flags = IORESOURCE_MEM,
  1210. },
  1211. IRQ(1),
  1212. {
  1213. /* Placeholder for pre-allocated fb memory */
  1214. .start = 0x00000000,
  1215. .end = 0x00000000,
  1216. .flags = 0,
  1217. },
  1218. };
  1219. DEFINE_DEV_DATA(atmel_lcdfb, 0);
  1220. DEV_CLK(hck1, atmel_lcdfb0, hsb, 7);
  1221. static struct clk atmel_lcdfb0_pixclk = {
  1222. .name = "lcdc_clk",
  1223. .dev = &atmel_lcdfb0_device.dev,
  1224. .mode = genclk_mode,
  1225. .get_rate = genclk_get_rate,
  1226. .set_rate = genclk_set_rate,
  1227. .set_parent = genclk_set_parent,
  1228. .index = 7,
  1229. };
  1230. struct platform_device *__init
  1231. at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
  1232. unsigned long fbmem_start, unsigned long fbmem_len,
  1233. u64 pin_mask)
  1234. {
  1235. struct platform_device *pdev;
  1236. struct atmel_lcdfb_info *info;
  1237. struct fb_monspecs *monspecs;
  1238. struct fb_videomode *modedb;
  1239. unsigned int modedb_size;
  1240. u32 portc_mask, portd_mask, porte_mask;
  1241. /*
  1242. * Do a deep copy of the fb data, monspecs and modedb. Make
  1243. * sure all allocations are done before setting up the
  1244. * portmux.
  1245. */
  1246. monspecs = kmemdup(data->default_monspecs,
  1247. sizeof(struct fb_monspecs), GFP_KERNEL);
  1248. if (!monspecs)
  1249. return NULL;
  1250. modedb_size = sizeof(struct fb_videomode) * monspecs->modedb_len;
  1251. modedb = kmemdup(monspecs->modedb, modedb_size, GFP_KERNEL);
  1252. if (!modedb)
  1253. goto err_dup_modedb;
  1254. monspecs->modedb = modedb;
  1255. switch (id) {
  1256. case 0:
  1257. pdev = &atmel_lcdfb0_device;
  1258. if (pin_mask == 0ULL)
  1259. /* Default to "full" lcdc control signals and 24bit */
  1260. pin_mask = ATMEL_LCDC_PRI_24BIT | ATMEL_LCDC_PRI_CONTROL;
  1261. /* LCDC on port C */
  1262. portc_mask = pin_mask & 0xfff80000;
  1263. select_peripheral(PIOC, portc_mask, PERIPH_A, 0);
  1264. /* LCDC on port D */
  1265. portd_mask = pin_mask & 0x0003ffff;
  1266. select_peripheral(PIOD, portd_mask, PERIPH_A, 0);
  1267. /* LCDC on port E */
  1268. porte_mask = (pin_mask >> 32) & 0x0007ffff;
  1269. select_peripheral(PIOE, porte_mask, PERIPH_B, 0);
  1270. clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
  1271. clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
  1272. break;
  1273. default:
  1274. goto err_invalid_id;
  1275. }
  1276. if (fbmem_len) {
  1277. pdev->resource[2].start = fbmem_start;
  1278. pdev->resource[2].end = fbmem_start + fbmem_len - 1;
  1279. pdev->resource[2].flags = IORESOURCE_MEM;
  1280. }
  1281. info = pdev->dev.platform_data;
  1282. memcpy(info, data, sizeof(struct atmel_lcdfb_info));
  1283. info->default_monspecs = monspecs;
  1284. platform_device_register(pdev);
  1285. return pdev;
  1286. err_invalid_id:
  1287. kfree(modedb);
  1288. err_dup_modedb:
  1289. kfree(monspecs);
  1290. return NULL;
  1291. }
  1292. #endif
  1293. /* --------------------------------------------------------------------
  1294. * PWM
  1295. * -------------------------------------------------------------------- */
  1296. static struct resource atmel_pwm0_resource[] __initdata = {
  1297. PBMEM(0xfff01400),
  1298. IRQ(24),
  1299. };
  1300. static struct clk atmel_pwm0_mck = {
  1301. .name = "pwm_clk",
  1302. .parent = &pbb_clk,
  1303. .mode = pbb_clk_mode,
  1304. .get_rate = pbb_clk_get_rate,
  1305. .index = 5,
  1306. };
  1307. struct platform_device *__init at32_add_device_pwm(u32 mask)
  1308. {
  1309. struct platform_device *pdev;
  1310. u32 pin_mask;
  1311. if (!mask)
  1312. return NULL;
  1313. pdev = platform_device_alloc("atmel_pwm", 0);
  1314. if (!pdev)
  1315. return NULL;
  1316. if (platform_device_add_resources(pdev, atmel_pwm0_resource,
  1317. ARRAY_SIZE(atmel_pwm0_resource)))
  1318. goto out_free_pdev;
  1319. if (platform_device_add_data(pdev, &mask, sizeof(mask)))
  1320. goto out_free_pdev;
  1321. pin_mask = 0;
  1322. if (mask & (1 << 0))
  1323. pin_mask |= (1 << 28);
  1324. if (mask & (1 << 1))
  1325. pin_mask |= (1 << 29);
  1326. if (pin_mask > 0)
  1327. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1328. pin_mask = 0;
  1329. if (mask & (1 << 2))
  1330. pin_mask |= (1 << 21);
  1331. if (mask & (1 << 3))
  1332. pin_mask |= (1 << 22);
  1333. if (pin_mask > 0)
  1334. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1335. atmel_pwm0_mck.dev = &pdev->dev;
  1336. platform_device_add(pdev);
  1337. return pdev;
  1338. out_free_pdev:
  1339. platform_device_put(pdev);
  1340. return NULL;
  1341. }
  1342. /* --------------------------------------------------------------------
  1343. * SSC
  1344. * -------------------------------------------------------------------- */
  1345. static struct resource ssc0_resource[] = {
  1346. PBMEM(0xffe01c00),
  1347. IRQ(10),
  1348. };
  1349. DEFINE_DEV(ssc, 0);
  1350. DEV_CLK(pclk, ssc0, pba, 7);
  1351. static struct resource ssc1_resource[] = {
  1352. PBMEM(0xffe02000),
  1353. IRQ(11),
  1354. };
  1355. DEFINE_DEV(ssc, 1);
  1356. DEV_CLK(pclk, ssc1, pba, 8);
  1357. static struct resource ssc2_resource[] = {
  1358. PBMEM(0xffe02400),
  1359. IRQ(12),
  1360. };
  1361. DEFINE_DEV(ssc, 2);
  1362. DEV_CLK(pclk, ssc2, pba, 9);
  1363. struct platform_device *__init
  1364. at32_add_device_ssc(unsigned int id, unsigned int flags)
  1365. {
  1366. struct platform_device *pdev;
  1367. u32 pin_mask = 0;
  1368. switch (id) {
  1369. case 0:
  1370. pdev = &ssc0_device;
  1371. if (flags & ATMEL_SSC_RF)
  1372. pin_mask |= (1 << 21); /* RF */
  1373. if (flags & ATMEL_SSC_RK)
  1374. pin_mask |= (1 << 22); /* RK */
  1375. if (flags & ATMEL_SSC_TK)
  1376. pin_mask |= (1 << 23); /* TK */
  1377. if (flags & ATMEL_SSC_TF)
  1378. pin_mask |= (1 << 24); /* TF */
  1379. if (flags & ATMEL_SSC_TD)
  1380. pin_mask |= (1 << 25); /* TD */
  1381. if (flags & ATMEL_SSC_RD)
  1382. pin_mask |= (1 << 26); /* RD */
  1383. if (pin_mask > 0)
  1384. select_peripheral(PIOA, pin_mask, PERIPH_A, 0);
  1385. break;
  1386. case 1:
  1387. pdev = &ssc1_device;
  1388. if (flags & ATMEL_SSC_RF)
  1389. pin_mask |= (1 << 0); /* RF */
  1390. if (flags & ATMEL_SSC_RK)
  1391. pin_mask |= (1 << 1); /* RK */
  1392. if (flags & ATMEL_SSC_TK)
  1393. pin_mask |= (1 << 2); /* TK */
  1394. if (flags & ATMEL_SSC_TF)
  1395. pin_mask |= (1 << 3); /* TF */
  1396. if (flags & ATMEL_SSC_TD)
  1397. pin_mask |= (1 << 4); /* TD */
  1398. if (flags & ATMEL_SSC_RD)
  1399. pin_mask |= (1 << 5); /* RD */
  1400. if (pin_mask > 0)
  1401. select_peripheral(PIOA, pin_mask, PERIPH_B, 0);
  1402. break;
  1403. case 2:
  1404. pdev = &ssc2_device;
  1405. if (flags & ATMEL_SSC_TD)
  1406. pin_mask |= (1 << 13); /* TD */
  1407. if (flags & ATMEL_SSC_RD)
  1408. pin_mask |= (1 << 14); /* RD */
  1409. if (flags & ATMEL_SSC_TK)
  1410. pin_mask |= (1 << 15); /* TK */
  1411. if (flags & ATMEL_SSC_TF)
  1412. pin_mask |= (1 << 16); /* TF */
  1413. if (flags & ATMEL_SSC_RF)
  1414. pin_mask |= (1 << 17); /* RF */
  1415. if (flags & ATMEL_SSC_RK)
  1416. pin_mask |= (1 << 18); /* RK */
  1417. if (pin_mask > 0)
  1418. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1419. break;
  1420. default:
  1421. return NULL;
  1422. }
  1423. platform_device_register(pdev);
  1424. return pdev;
  1425. }
  1426. /* --------------------------------------------------------------------
  1427. * USB Device Controller
  1428. * -------------------------------------------------------------------- */
  1429. static struct resource usba0_resource[] __initdata = {
  1430. {
  1431. .start = 0xff300000,
  1432. .end = 0xff3fffff,
  1433. .flags = IORESOURCE_MEM,
  1434. }, {
  1435. .start = 0xfff03000,
  1436. .end = 0xfff033ff,
  1437. .flags = IORESOURCE_MEM,
  1438. },
  1439. IRQ(31),
  1440. };
  1441. static struct clk usba0_pclk = {
  1442. .name = "pclk",
  1443. .parent = &pbb_clk,
  1444. .mode = pbb_clk_mode,
  1445. .get_rate = pbb_clk_get_rate,
  1446. .index = 12,
  1447. };
  1448. static struct clk usba0_hclk = {
  1449. .name = "hclk",
  1450. .parent = &hsb_clk,
  1451. .mode = hsb_clk_mode,
  1452. .get_rate = hsb_clk_get_rate,
  1453. .index = 6,
  1454. };
  1455. #define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
  1456. [idx] = { \
  1457. .name = nam, \
  1458. .index = idx, \
  1459. .fifo_size = maxpkt, \
  1460. .nr_banks = maxbk, \
  1461. .can_dma = dma, \
  1462. .can_isoc = isoc, \
  1463. }
  1464. static struct usba_ep_data at32_usba_ep[] __initdata = {
  1465. EP("ep0", 0, 64, 1, 0, 0),
  1466. EP("ep1", 1, 512, 2, 1, 1),
  1467. EP("ep2", 2, 512, 2, 1, 1),
  1468. EP("ep3-int", 3, 64, 3, 1, 0),
  1469. EP("ep4-int", 4, 64, 3, 1, 0),
  1470. EP("ep5", 5, 1024, 3, 1, 1),
  1471. EP("ep6", 6, 1024, 3, 1, 1),
  1472. };
  1473. #undef EP
  1474. struct platform_device *__init
  1475. at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
  1476. {
  1477. /*
  1478. * pdata doesn't have room for any endpoints, so we need to
  1479. * append room for the ones we need right after it.
  1480. */
  1481. struct {
  1482. struct usba_platform_data pdata;
  1483. struct usba_ep_data ep[7];
  1484. } usba_data;
  1485. struct platform_device *pdev;
  1486. if (id != 0)
  1487. return NULL;
  1488. pdev = platform_device_alloc("atmel_usba_udc", 0);
  1489. if (!pdev)
  1490. return NULL;
  1491. if (platform_device_add_resources(pdev, usba0_resource,
  1492. ARRAY_SIZE(usba0_resource)))
  1493. goto out_free_pdev;
  1494. if (data) {
  1495. usba_data.pdata.vbus_pin = data->vbus_pin;
  1496. usba_data.pdata.vbus_pin_inverted = data->vbus_pin_inverted;
  1497. } else {
  1498. usba_data.pdata.vbus_pin = -EINVAL;
  1499. usba_data.pdata.vbus_pin_inverted = -EINVAL;
  1500. }
  1501. data = &usba_data.pdata;
  1502. data->num_ep = ARRAY_SIZE(at32_usba_ep);
  1503. memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
  1504. if (platform_device_add_data(pdev, data, sizeof(usba_data)))
  1505. goto out_free_pdev;
  1506. if (gpio_is_valid(data->vbus_pin))
  1507. at32_select_gpio(data->vbus_pin, 0);
  1508. usba0_pclk.dev = &pdev->dev;
  1509. usba0_hclk.dev = &pdev->dev;
  1510. platform_device_add(pdev);
  1511. return pdev;
  1512. out_free_pdev:
  1513. platform_device_put(pdev);
  1514. return NULL;
  1515. }
  1516. /* --------------------------------------------------------------------
  1517. * IDE / CompactFlash
  1518. * -------------------------------------------------------------------- */
  1519. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7001)
  1520. static struct resource at32_smc_cs4_resource[] __initdata = {
  1521. {
  1522. .start = 0x04000000,
  1523. .end = 0x07ffffff,
  1524. .flags = IORESOURCE_MEM,
  1525. },
  1526. IRQ(~0UL), /* Magic IRQ will be overridden */
  1527. };
  1528. static struct resource at32_smc_cs5_resource[] __initdata = {
  1529. {
  1530. .start = 0x20000000,
  1531. .end = 0x23ffffff,
  1532. .flags = IORESOURCE_MEM,
  1533. },
  1534. IRQ(~0UL), /* Magic IRQ will be overridden */
  1535. };
  1536. static int __init at32_init_ide_or_cf(struct platform_device *pdev,
  1537. unsigned int cs, unsigned int extint)
  1538. {
  1539. static unsigned int extint_pin_map[4] __initdata = {
  1540. (1 << 25),
  1541. (1 << 26),
  1542. (1 << 27),
  1543. (1 << 28),
  1544. };
  1545. static bool common_pins_initialized __initdata = false;
  1546. unsigned int extint_pin;
  1547. int ret;
  1548. u32 pin_mask;
  1549. if (extint >= ARRAY_SIZE(extint_pin_map))
  1550. return -EINVAL;
  1551. extint_pin = extint_pin_map[extint];
  1552. switch (cs) {
  1553. case 4:
  1554. ret = platform_device_add_resources(pdev,
  1555. at32_smc_cs4_resource,
  1556. ARRAY_SIZE(at32_smc_cs4_resource));
  1557. if (ret)
  1558. return ret;
  1559. /* NCS4 -> OE_N */
  1560. select_peripheral(PIOE, (1 << 21), PERIPH_A, 0);
  1561. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF0_ENABLE);
  1562. break;
  1563. case 5:
  1564. ret = platform_device_add_resources(pdev,
  1565. at32_smc_cs5_resource,
  1566. ARRAY_SIZE(at32_smc_cs5_resource));
  1567. if (ret)
  1568. return ret;
  1569. /* NCS5 -> OE_N */
  1570. select_peripheral(PIOE, (1 << 22), PERIPH_A, 0);
  1571. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_CF1_ENABLE);
  1572. break;
  1573. default:
  1574. return -EINVAL;
  1575. }
  1576. if (!common_pins_initialized) {
  1577. pin_mask = (1 << 19); /* CFCE1 -> CS0_N */
  1578. pin_mask |= (1 << 20); /* CFCE2 -> CS1_N */
  1579. pin_mask |= (1 << 23); /* CFRNW -> DIR */
  1580. pin_mask |= (1 << 24); /* NWAIT <- IORDY */
  1581. select_peripheral(PIOE, pin_mask, PERIPH_A, 0);
  1582. common_pins_initialized = true;
  1583. }
  1584. select_peripheral(PIOB, extint_pin, PERIPH_A, AT32_GPIOF_DEGLITCH);
  1585. pdev->resource[1].start = EIM_IRQ_BASE + extint;
  1586. pdev->resource[1].end = pdev->resource[1].start;
  1587. return 0;
  1588. }
  1589. struct platform_device *__init
  1590. at32_add_device_ide(unsigned int id, unsigned int extint,
  1591. struct ide_platform_data *data)
  1592. {
  1593. struct platform_device *pdev;
  1594. pdev = platform_device_alloc("at32_ide", id);
  1595. if (!pdev)
  1596. goto fail;
  1597. if (platform_device_add_data(pdev, data,
  1598. sizeof(struct ide_platform_data)))
  1599. goto fail;
  1600. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1601. goto fail;
  1602. platform_device_add(pdev);
  1603. return pdev;
  1604. fail:
  1605. platform_device_put(pdev);
  1606. return NULL;
  1607. }
  1608. struct platform_device *__init
  1609. at32_add_device_cf(unsigned int id, unsigned int extint,
  1610. struct cf_platform_data *data)
  1611. {
  1612. struct platform_device *pdev;
  1613. pdev = platform_device_alloc("at32_cf", id);
  1614. if (!pdev)
  1615. goto fail;
  1616. if (platform_device_add_data(pdev, data,
  1617. sizeof(struct cf_platform_data)))
  1618. goto fail;
  1619. if (at32_init_ide_or_cf(pdev, data->cs, extint))
  1620. goto fail;
  1621. if (gpio_is_valid(data->detect_pin))
  1622. at32_select_gpio(data->detect_pin, AT32_GPIOF_DEGLITCH);
  1623. if (gpio_is_valid(data->reset_pin))
  1624. at32_select_gpio(data->reset_pin, 0);
  1625. if (gpio_is_valid(data->vcc_pin))
  1626. at32_select_gpio(data->vcc_pin, 0);
  1627. /* READY is used as extint, so we can't select it as gpio */
  1628. platform_device_add(pdev);
  1629. return pdev;
  1630. fail:
  1631. platform_device_put(pdev);
  1632. return NULL;
  1633. }
  1634. #endif
  1635. /* --------------------------------------------------------------------
  1636. * NAND Flash / SmartMedia
  1637. * -------------------------------------------------------------------- */
  1638. static struct resource smc_cs3_resource[] __initdata = {
  1639. {
  1640. .start = 0x0c000000,
  1641. .end = 0x0fffffff,
  1642. .flags = IORESOURCE_MEM,
  1643. }, {
  1644. .start = 0xfff03c00,
  1645. .end = 0xfff03fff,
  1646. .flags = IORESOURCE_MEM,
  1647. },
  1648. };
  1649. struct platform_device *__init
  1650. at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
  1651. {
  1652. struct platform_device *pdev;
  1653. if (id != 0 || !data)
  1654. return NULL;
  1655. pdev = platform_device_alloc("atmel_nand", id);
  1656. if (!pdev)
  1657. goto fail;
  1658. if (platform_device_add_resources(pdev, smc_cs3_resource,
  1659. ARRAY_SIZE(smc_cs3_resource)))
  1660. goto fail;
  1661. if (platform_device_add_data(pdev, data,
  1662. sizeof(struct atmel_nand_data)))
  1663. goto fail;
  1664. hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, HMATRIX_EBI_NAND_ENABLE);
  1665. if (data->enable_pin)
  1666. at32_select_gpio(data->enable_pin,
  1667. AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  1668. if (data->rdy_pin)
  1669. at32_select_gpio(data->rdy_pin, 0);
  1670. if (data->det_pin)
  1671. at32_select_gpio(data->det_pin, 0);
  1672. platform_device_add(pdev);
  1673. return pdev;
  1674. fail:
  1675. platform_device_put(pdev);
  1676. return NULL;
  1677. }
  1678. /* --------------------------------------------------------------------
  1679. * AC97C
  1680. * -------------------------------------------------------------------- */
  1681. static struct resource atmel_ac97c0_resource[] __initdata = {
  1682. PBMEM(0xfff02800),
  1683. IRQ(29),
  1684. };
  1685. static struct clk atmel_ac97c0_pclk = {
  1686. .name = "pclk",
  1687. .parent = &pbb_clk,
  1688. .mode = pbb_clk_mode,
  1689. .get_rate = pbb_clk_get_rate,
  1690. .index = 10,
  1691. };
  1692. struct platform_device *__init
  1693. at32_add_device_ac97c(unsigned int id, struct ac97c_platform_data *data,
  1694. unsigned int flags)
  1695. {
  1696. struct platform_device *pdev;
  1697. struct dw_dma_slave *rx_dws;
  1698. struct dw_dma_slave *tx_dws;
  1699. struct ac97c_platform_data _data;
  1700. u32 pin_mask;
  1701. if (id != 0)
  1702. return NULL;
  1703. pdev = platform_device_alloc("atmel_ac97c", id);
  1704. if (!pdev)
  1705. return NULL;
  1706. if (platform_device_add_resources(pdev, atmel_ac97c0_resource,
  1707. ARRAY_SIZE(atmel_ac97c0_resource)))
  1708. goto out_free_resources;
  1709. if (!data) {
  1710. data = &_data;
  1711. memset(data, 0, sizeof(struct ac97c_platform_data));
  1712. data->reset_pin = -ENODEV;
  1713. }
  1714. rx_dws = &data->rx_dws;
  1715. tx_dws = &data->tx_dws;
  1716. /* Check if DMA slave interface for capture should be configured. */
  1717. if (flags & AC97C_CAPTURE) {
  1718. rx_dws->dma_dev = &dw_dmac0_device.dev;
  1719. rx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
  1720. rx_dws->cfg_hi = DWC_CFGH_SRC_PER(3);
  1721. rx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1722. }
  1723. /* Check if DMA slave interface for playback should be configured. */
  1724. if (flags & AC97C_PLAYBACK) {
  1725. tx_dws->dma_dev = &dw_dmac0_device.dev;
  1726. tx_dws->reg_width = DW_DMA_SLAVE_WIDTH_16BIT;
  1727. tx_dws->cfg_hi = DWC_CFGH_DST_PER(4);
  1728. tx_dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1729. }
  1730. if (platform_device_add_data(pdev, data,
  1731. sizeof(struct ac97c_platform_data)))
  1732. goto out_free_resources;
  1733. /* SDO | SYNC | SCLK | SDI */
  1734. pin_mask = (1 << 20) | (1 << 21) | (1 << 22) | (1 << 23);
  1735. select_peripheral(PIOB, pin_mask, PERIPH_B, 0);
  1736. if (gpio_is_valid(data->reset_pin))
  1737. at32_select_gpio(data->reset_pin, AT32_GPIOF_OUTPUT
  1738. | AT32_GPIOF_HIGH);
  1739. atmel_ac97c0_pclk.dev = &pdev->dev;
  1740. platform_device_add(pdev);
  1741. return pdev;
  1742. out_free_resources:
  1743. platform_device_put(pdev);
  1744. return NULL;
  1745. }
  1746. /* --------------------------------------------------------------------
  1747. * ABDAC
  1748. * -------------------------------------------------------------------- */
  1749. static struct resource abdac0_resource[] __initdata = {
  1750. PBMEM(0xfff02000),
  1751. IRQ(27),
  1752. };
  1753. static struct clk abdac0_pclk = {
  1754. .name = "pclk",
  1755. .parent = &pbb_clk,
  1756. .mode = pbb_clk_mode,
  1757. .get_rate = pbb_clk_get_rate,
  1758. .index = 8,
  1759. };
  1760. static struct clk abdac0_sample_clk = {
  1761. .name = "sample_clk",
  1762. .mode = genclk_mode,
  1763. .get_rate = genclk_get_rate,
  1764. .set_rate = genclk_set_rate,
  1765. .set_parent = genclk_set_parent,
  1766. .index = 6,
  1767. };
  1768. struct platform_device *__init
  1769. at32_add_device_abdac(unsigned int id, struct atmel_abdac_pdata *data)
  1770. {
  1771. struct platform_device *pdev;
  1772. struct dw_dma_slave *dws;
  1773. u32 pin_mask;
  1774. if (id != 0 || !data)
  1775. return NULL;
  1776. pdev = platform_device_alloc("atmel_abdac", id);
  1777. if (!pdev)
  1778. return NULL;
  1779. if (platform_device_add_resources(pdev, abdac0_resource,
  1780. ARRAY_SIZE(abdac0_resource)))
  1781. goto out_free_resources;
  1782. dws = &data->dws;
  1783. dws->dma_dev = &dw_dmac0_device.dev;
  1784. dws->reg_width = DW_DMA_SLAVE_WIDTH_32BIT;
  1785. dws->cfg_hi = DWC_CFGH_DST_PER(2);
  1786. dws->cfg_lo &= ~(DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL);
  1787. if (platform_device_add_data(pdev, data,
  1788. sizeof(struct atmel_abdac_pdata)))
  1789. goto out_free_resources;
  1790. pin_mask = (1 << 20) | (1 << 22); /* DATA1 & DATAN1 */
  1791. pin_mask |= (1 << 21) | (1 << 23); /* DATA0 & DATAN0 */
  1792. select_peripheral(PIOB, pin_mask, PERIPH_A, 0);
  1793. abdac0_pclk.dev = &pdev->dev;
  1794. abdac0_sample_clk.dev = &pdev->dev;
  1795. platform_device_add(pdev);
  1796. return pdev;
  1797. out_free_resources:
  1798. platform_device_put(pdev);
  1799. return NULL;
  1800. }
  1801. /* --------------------------------------------------------------------
  1802. * GCLK
  1803. * -------------------------------------------------------------------- */
  1804. static struct clk gclk0 = {
  1805. .name = "gclk0",
  1806. .mode = genclk_mode,
  1807. .get_rate = genclk_get_rate,
  1808. .set_rate = genclk_set_rate,
  1809. .set_parent = genclk_set_parent,
  1810. .index = 0,
  1811. };
  1812. static struct clk gclk1 = {
  1813. .name = "gclk1",
  1814. .mode = genclk_mode,
  1815. .get_rate = genclk_get_rate,
  1816. .set_rate = genclk_set_rate,
  1817. .set_parent = genclk_set_parent,
  1818. .index = 1,
  1819. };
  1820. static struct clk gclk2 = {
  1821. .name = "gclk2",
  1822. .mode = genclk_mode,
  1823. .get_rate = genclk_get_rate,
  1824. .set_rate = genclk_set_rate,
  1825. .set_parent = genclk_set_parent,
  1826. .index = 2,
  1827. };
  1828. static struct clk gclk3 = {
  1829. .name = "gclk3",
  1830. .mode = genclk_mode,
  1831. .get_rate = genclk_get_rate,
  1832. .set_rate = genclk_set_rate,
  1833. .set_parent = genclk_set_parent,
  1834. .index = 3,
  1835. };
  1836. static struct clk gclk4 = {
  1837. .name = "gclk4",
  1838. .mode = genclk_mode,
  1839. .get_rate = genclk_get_rate,
  1840. .set_rate = genclk_set_rate,
  1841. .set_parent = genclk_set_parent,
  1842. .index = 4,
  1843. };
  1844. static __initdata struct clk *init_clocks[] = {
  1845. &osc32k,
  1846. &osc0,
  1847. &osc1,
  1848. &pll0,
  1849. &pll1,
  1850. &cpu_clk,
  1851. &hsb_clk,
  1852. &pba_clk,
  1853. &pbb_clk,
  1854. &at32_pm_pclk,
  1855. &at32_intc0_pclk,
  1856. &at32_hmatrix_clk,
  1857. &ebi_clk,
  1858. &hramc_clk,
  1859. &sdramc_clk,
  1860. &smc0_pclk,
  1861. &smc0_mck,
  1862. &pdc_hclk,
  1863. &pdc_pclk,
  1864. &dw_dmac0_hclk,
  1865. &pico_clk,
  1866. &pio0_mck,
  1867. &pio1_mck,
  1868. &pio2_mck,
  1869. &pio3_mck,
  1870. &pio4_mck,
  1871. &at32_tcb0_t0_clk,
  1872. &at32_tcb1_t0_clk,
  1873. &atmel_psif0_pclk,
  1874. &atmel_psif1_pclk,
  1875. &atmel_usart0_usart,
  1876. &atmel_usart1_usart,
  1877. &atmel_usart2_usart,
  1878. &atmel_usart3_usart,
  1879. &atmel_pwm0_mck,
  1880. #if defined(CONFIG_CPU_AT32AP7000)
  1881. &macb0_hclk,
  1882. &macb0_pclk,
  1883. &macb1_hclk,
  1884. &macb1_pclk,
  1885. #endif
  1886. &atmel_spi0_spi_clk,
  1887. &atmel_spi1_spi_clk,
  1888. &atmel_twi0_pclk,
  1889. &atmel_mci0_pclk,
  1890. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1891. &atmel_lcdfb0_hck1,
  1892. &atmel_lcdfb0_pixclk,
  1893. #endif
  1894. &ssc0_pclk,
  1895. &ssc1_pclk,
  1896. &ssc2_pclk,
  1897. &usba0_hclk,
  1898. &usba0_pclk,
  1899. &atmel_ac97c0_pclk,
  1900. &abdac0_pclk,
  1901. &abdac0_sample_clk,
  1902. &gclk0,
  1903. &gclk1,
  1904. &gclk2,
  1905. &gclk3,
  1906. &gclk4,
  1907. };
  1908. void __init setup_platform(void)
  1909. {
  1910. u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
  1911. int i;
  1912. if (pm_readl(MCCTRL) & PM_BIT(PLLSEL)) {
  1913. main_clock = &pll0;
  1914. cpu_clk.parent = &pll0;
  1915. } else {
  1916. main_clock = &osc0;
  1917. cpu_clk.parent = &osc0;
  1918. }
  1919. if (pm_readl(PLL0) & PM_BIT(PLLOSC))
  1920. pll0.parent = &osc1;
  1921. if (pm_readl(PLL1) & PM_BIT(PLLOSC))
  1922. pll1.parent = &osc1;
  1923. genclk_init_parent(&gclk0);
  1924. genclk_init_parent(&gclk1);
  1925. genclk_init_parent(&gclk2);
  1926. genclk_init_parent(&gclk3);
  1927. genclk_init_parent(&gclk4);
  1928. #if defined(CONFIG_CPU_AT32AP7000) || defined(CONFIG_CPU_AT32AP7002)
  1929. genclk_init_parent(&atmel_lcdfb0_pixclk);
  1930. #endif
  1931. genclk_init_parent(&abdac0_sample_clk);
  1932. /*
  1933. * Build initial dynamic clock list by registering all clocks
  1934. * from the array.
  1935. * At the same time, turn on all clocks that have at least one
  1936. * user already, and turn off everything else. We only do this
  1937. * for module clocks, and even though it isn't particularly
  1938. * pretty to check the address of the mode function, it should
  1939. * do the trick...
  1940. */
  1941. for (i = 0; i < ARRAY_SIZE(init_clocks); i++) {
  1942. struct clk *clk = init_clocks[i];
  1943. /* first, register clock */
  1944. at32_clk_register(clk);
  1945. if (clk->users == 0)
  1946. continue;
  1947. if (clk->mode == &cpu_clk_mode)
  1948. cpu_mask |= 1 << clk->index;
  1949. else if (clk->mode == &hsb_clk_mode)
  1950. hsb_mask |= 1 << clk->index;
  1951. else if (clk->mode == &pba_clk_mode)
  1952. pba_mask |= 1 << clk->index;
  1953. else if (clk->mode == &pbb_clk_mode)
  1954. pbb_mask |= 1 << clk->index;
  1955. }
  1956. pm_writel(CPU_MASK, cpu_mask);
  1957. pm_writel(HSB_MASK, hsb_mask);
  1958. pm_writel(PBA_MASK, pba_mask);
  1959. pm_writel(PBB_MASK, pbb_mask);
  1960. /* Initialize the port muxes */
  1961. at32_init_pio(&pio0_device);
  1962. at32_init_pio(&pio1_device);
  1963. at32_init_pio(&pio2_device);
  1964. at32_init_pio(&pio3_device);
  1965. at32_init_pio(&pio4_device);
  1966. }
  1967. struct gen_pool *sram_pool;
  1968. static int __init sram_init(void)
  1969. {
  1970. struct gen_pool *pool;
  1971. /* 1KiB granularity */
  1972. pool = gen_pool_create(10, -1);
  1973. if (!pool)
  1974. goto fail;
  1975. if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
  1976. goto err_pool_add;
  1977. sram_pool = pool;
  1978. return 0;
  1979. err_pool_add:
  1980. gen_pool_destroy(pool);
  1981. fail:
  1982. pr_err("Failed to create SRAM pool\n");
  1983. return -ENOMEM;
  1984. }
  1985. core_initcall(sram_init);