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