at32ap700x.c 43 KB

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