sa1111.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*
  2. * linux/arch/arm/common/sa1111.c
  3. *
  4. * SA1111 support
  5. *
  6. * Original code by John Dorsey
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This file contains all generic SA1111 support.
  13. *
  14. * All initialization functions provided here are intended to be called
  15. * from machine specific code with proper arguments when required.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/init.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/ioport.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/clk.h>
  30. #include <linux/io.h>
  31. #include <mach/hardware.h>
  32. #include <asm/mach/irq.h>
  33. #include <asm/mach-types.h>
  34. #include <asm/sizes.h>
  35. #include <asm/hardware/sa1111.h>
  36. /* SA1111 IRQs */
  37. #define IRQ_GPAIN0 (0)
  38. #define IRQ_GPAIN1 (1)
  39. #define IRQ_GPAIN2 (2)
  40. #define IRQ_GPAIN3 (3)
  41. #define IRQ_GPBIN0 (4)
  42. #define IRQ_GPBIN1 (5)
  43. #define IRQ_GPBIN2 (6)
  44. #define IRQ_GPBIN3 (7)
  45. #define IRQ_GPBIN4 (8)
  46. #define IRQ_GPBIN5 (9)
  47. #define IRQ_GPCIN0 (10)
  48. #define IRQ_GPCIN1 (11)
  49. #define IRQ_GPCIN2 (12)
  50. #define IRQ_GPCIN3 (13)
  51. #define IRQ_GPCIN4 (14)
  52. #define IRQ_GPCIN5 (15)
  53. #define IRQ_GPCIN6 (16)
  54. #define IRQ_GPCIN7 (17)
  55. #define IRQ_MSTXINT (18)
  56. #define IRQ_MSRXINT (19)
  57. #define IRQ_MSSTOPERRINT (20)
  58. #define IRQ_TPTXINT (21)
  59. #define IRQ_TPRXINT (22)
  60. #define IRQ_TPSTOPERRINT (23)
  61. #define SSPXMTINT (24)
  62. #define SSPRCVINT (25)
  63. #define SSPROR (26)
  64. #define AUDXMTDMADONEA (32)
  65. #define AUDRCVDMADONEA (33)
  66. #define AUDXMTDMADONEB (34)
  67. #define AUDRCVDMADONEB (35)
  68. #define AUDTFSR (36)
  69. #define AUDRFSR (37)
  70. #define AUDTUR (38)
  71. #define AUDROR (39)
  72. #define AUDDTS (40)
  73. #define AUDRDD (41)
  74. #define AUDSTO (42)
  75. #define IRQ_USBPWR (43)
  76. #define IRQ_HCIM (44)
  77. #define IRQ_HCIBUFFACC (45)
  78. #define IRQ_HCIRMTWKP (46)
  79. #define IRQ_NHCIMFCIR (47)
  80. #define IRQ_USB_PORT_RESUME (48)
  81. #define IRQ_S0_READY_NINT (49)
  82. #define IRQ_S1_READY_NINT (50)
  83. #define IRQ_S0_CD_VALID (51)
  84. #define IRQ_S1_CD_VALID (52)
  85. #define IRQ_S0_BVD1_STSCHG (53)
  86. #define IRQ_S1_BVD1_STSCHG (54)
  87. #define SA1111_IRQ_NR (55)
  88. extern void sa1110_mb_enable(void);
  89. extern void sa1110_mb_disable(void);
  90. /*
  91. * We keep the following data for the overall SA1111. Note that the
  92. * struct device and struct resource are "fake"; they should be supplied
  93. * by the bus above us. However, in the interests of getting all SA1111
  94. * drivers converted over to the device model, we provide this as an
  95. * anchor point for all the other drivers.
  96. */
  97. struct sa1111 {
  98. struct device *dev;
  99. struct clk *clk;
  100. unsigned long phys;
  101. int irq;
  102. int irq_base; /* base for cascaded on-chip IRQs */
  103. spinlock_t lock;
  104. void __iomem *base;
  105. struct sa1111_platform_data *pdata;
  106. struct irq_domain *irqdomain;
  107. struct gpio_chip gc;
  108. #ifdef CONFIG_PM
  109. void *saved_state;
  110. #endif
  111. };
  112. /*
  113. * We _really_ need to eliminate this. Its only users
  114. * are the PWM and DMA checking code.
  115. */
  116. static struct sa1111 *g_sa1111;
  117. struct sa1111_dev_info {
  118. unsigned long offset;
  119. unsigned long skpcr_mask;
  120. bool dma;
  121. unsigned int devid;
  122. unsigned int hwirq[6];
  123. };
  124. static struct sa1111_dev_info sa1111_devices[] = {
  125. {
  126. .offset = SA1111_USB,
  127. .skpcr_mask = SKPCR_UCLKEN,
  128. .dma = true,
  129. .devid = SA1111_DEVID_USB,
  130. .hwirq = {
  131. IRQ_USBPWR,
  132. IRQ_HCIM,
  133. IRQ_HCIBUFFACC,
  134. IRQ_HCIRMTWKP,
  135. IRQ_NHCIMFCIR,
  136. IRQ_USB_PORT_RESUME
  137. },
  138. },
  139. {
  140. .offset = 0x0600,
  141. .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
  142. .dma = true,
  143. .devid = SA1111_DEVID_SAC,
  144. .hwirq = {
  145. AUDXMTDMADONEA,
  146. AUDXMTDMADONEB,
  147. AUDRCVDMADONEA,
  148. AUDRCVDMADONEB
  149. },
  150. },
  151. {
  152. .offset = 0x0800,
  153. .skpcr_mask = SKPCR_SCLKEN,
  154. .devid = SA1111_DEVID_SSP,
  155. },
  156. {
  157. .offset = SA1111_KBD,
  158. .skpcr_mask = SKPCR_PTCLKEN,
  159. .devid = SA1111_DEVID_PS2_KBD,
  160. .hwirq = {
  161. IRQ_TPRXINT,
  162. IRQ_TPTXINT
  163. },
  164. },
  165. {
  166. .offset = SA1111_MSE,
  167. .skpcr_mask = SKPCR_PMCLKEN,
  168. .devid = SA1111_DEVID_PS2_MSE,
  169. .hwirq = {
  170. IRQ_MSRXINT,
  171. IRQ_MSTXINT
  172. },
  173. },
  174. {
  175. .offset = 0x1800,
  176. .skpcr_mask = 0,
  177. .devid = SA1111_DEVID_PCMCIA,
  178. .hwirq = {
  179. IRQ_S0_READY_NINT,
  180. IRQ_S0_CD_VALID,
  181. IRQ_S0_BVD1_STSCHG,
  182. IRQ_S1_READY_NINT,
  183. IRQ_S1_CD_VALID,
  184. IRQ_S1_BVD1_STSCHG,
  185. },
  186. },
  187. };
  188. static int sa1111_map_irq(struct sa1111 *sachip, irq_hw_number_t hwirq)
  189. {
  190. return irq_create_mapping(sachip->irqdomain, hwirq);
  191. }
  192. static void sa1111_handle_irqdomain(struct irq_domain *irqdomain, int irq)
  193. {
  194. struct irq_desc *d = irq_to_desc(irq_linear_revmap(irqdomain, irq));
  195. if (d)
  196. generic_handle_irq_desc(d);
  197. }
  198. /*
  199. * SA1111 interrupt support. Since clearing an IRQ while there are
  200. * active IRQs causes the interrupt output to pulse, the upper levels
  201. * will call us again if there are more interrupts to process.
  202. */
  203. static void sa1111_irq_handler(struct irq_desc *desc)
  204. {
  205. unsigned int stat0, stat1, i;
  206. struct sa1111 *sachip = irq_desc_get_handler_data(desc);
  207. struct irq_domain *irqdomain;
  208. void __iomem *mapbase = sachip->base + SA1111_INTC;
  209. stat0 = readl_relaxed(mapbase + SA1111_INTSTATCLR0);
  210. stat1 = readl_relaxed(mapbase + SA1111_INTSTATCLR1);
  211. writel_relaxed(stat0, mapbase + SA1111_INTSTATCLR0);
  212. desc->irq_data.chip->irq_ack(&desc->irq_data);
  213. writel_relaxed(stat1, mapbase + SA1111_INTSTATCLR1);
  214. if (stat0 == 0 && stat1 == 0) {
  215. do_bad_IRQ(desc);
  216. return;
  217. }
  218. irqdomain = sachip->irqdomain;
  219. for (i = 0; stat0; i++, stat0 >>= 1)
  220. if (stat0 & 1)
  221. sa1111_handle_irqdomain(irqdomain, i);
  222. for (i = 32; stat1; i++, stat1 >>= 1)
  223. if (stat1 & 1)
  224. sa1111_handle_irqdomain(irqdomain, i);
  225. /* For level-based interrupts */
  226. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  227. }
  228. static u32 sa1111_irqmask(struct irq_data *d)
  229. {
  230. return BIT(irqd_to_hwirq(d) & 31);
  231. }
  232. static int sa1111_irqbank(struct irq_data *d)
  233. {
  234. return (irqd_to_hwirq(d) / 32) * 4;
  235. }
  236. static void sa1111_ack_irq(struct irq_data *d)
  237. {
  238. }
  239. static void sa1111_mask_irq(struct irq_data *d)
  240. {
  241. struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
  242. void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
  243. u32 ie;
  244. ie = readl_relaxed(mapbase + SA1111_INTEN0);
  245. ie &= ~sa1111_irqmask(d);
  246. writel(ie, mapbase + SA1111_INTEN0);
  247. }
  248. static void sa1111_unmask_irq(struct irq_data *d)
  249. {
  250. struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
  251. void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
  252. u32 ie;
  253. ie = readl_relaxed(mapbase + SA1111_INTEN0);
  254. ie |= sa1111_irqmask(d);
  255. writel_relaxed(ie, mapbase + SA1111_INTEN0);
  256. }
  257. /*
  258. * Attempt to re-trigger the interrupt. The SA1111 contains a register
  259. * (INTSET) which claims to do this. However, in practice no amount of
  260. * manipulation of INTEN and INTSET guarantees that the interrupt will
  261. * be triggered. In fact, its very difficult, if not impossible to get
  262. * INTSET to re-trigger the interrupt.
  263. */
  264. static int sa1111_retrigger_irq(struct irq_data *d)
  265. {
  266. struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
  267. void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
  268. u32 ip, mask = sa1111_irqmask(d);
  269. int i;
  270. ip = readl_relaxed(mapbase + SA1111_INTPOL0);
  271. for (i = 0; i < 8; i++) {
  272. writel_relaxed(ip ^ mask, mapbase + SA1111_INTPOL0);
  273. writel_relaxed(ip, mapbase + SA1111_INTPOL0);
  274. if (readl_relaxed(mapbase + SA1111_INTSTATCLR0) & mask)
  275. break;
  276. }
  277. if (i == 8)
  278. pr_err("Danger Will Robinson: failed to re-trigger IRQ%d\n",
  279. d->irq);
  280. return i == 8 ? -1 : 0;
  281. }
  282. static int sa1111_type_irq(struct irq_data *d, unsigned int flags)
  283. {
  284. struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
  285. void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
  286. u32 ip, mask = sa1111_irqmask(d);
  287. if (flags == IRQ_TYPE_PROBE)
  288. return 0;
  289. if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
  290. return -EINVAL;
  291. ip = readl_relaxed(mapbase + SA1111_INTPOL0);
  292. if (flags & IRQ_TYPE_EDGE_RISING)
  293. ip &= ~mask;
  294. else
  295. ip |= mask;
  296. writel_relaxed(ip, mapbase + SA1111_INTPOL0);
  297. writel_relaxed(ip, mapbase + SA1111_WAKEPOL0);
  298. return 0;
  299. }
  300. static int sa1111_wake_irq(struct irq_data *d, unsigned int on)
  301. {
  302. struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
  303. void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
  304. u32 we, mask = sa1111_irqmask(d);
  305. we = readl_relaxed(mapbase + SA1111_WAKEEN0);
  306. if (on)
  307. we |= mask;
  308. else
  309. we &= ~mask;
  310. writel_relaxed(we, mapbase + SA1111_WAKEEN0);
  311. return 0;
  312. }
  313. static struct irq_chip sa1111_irq_chip = {
  314. .name = "SA1111",
  315. .irq_ack = sa1111_ack_irq,
  316. .irq_mask = sa1111_mask_irq,
  317. .irq_unmask = sa1111_unmask_irq,
  318. .irq_retrigger = sa1111_retrigger_irq,
  319. .irq_set_type = sa1111_type_irq,
  320. .irq_set_wake = sa1111_wake_irq,
  321. };
  322. static int sa1111_irqdomain_map(struct irq_domain *d, unsigned int irq,
  323. irq_hw_number_t hwirq)
  324. {
  325. struct sa1111 *sachip = d->host_data;
  326. /* Disallow unavailable interrupts */
  327. if (hwirq > SSPROR && hwirq < AUDXMTDMADONEA)
  328. return -EINVAL;
  329. irq_set_chip_data(irq, sachip);
  330. irq_set_chip_and_handler(irq, &sa1111_irq_chip, handle_edge_irq);
  331. irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  332. return 0;
  333. }
  334. static const struct irq_domain_ops sa1111_irqdomain_ops = {
  335. .map = sa1111_irqdomain_map,
  336. .xlate = irq_domain_xlate_twocell,
  337. };
  338. static int sa1111_setup_irq(struct sa1111 *sachip, unsigned irq_base)
  339. {
  340. void __iomem *irqbase = sachip->base + SA1111_INTC;
  341. int ret;
  342. /*
  343. * We're guaranteed that this region hasn't been taken.
  344. */
  345. request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
  346. ret = irq_alloc_descs(-1, irq_base, SA1111_IRQ_NR, -1);
  347. if (ret <= 0) {
  348. dev_err(sachip->dev, "unable to allocate %u irqs: %d\n",
  349. SA1111_IRQ_NR, ret);
  350. if (ret == 0)
  351. ret = -EINVAL;
  352. return ret;
  353. }
  354. sachip->irq_base = ret;
  355. /* disable all IRQs */
  356. writel_relaxed(0, irqbase + SA1111_INTEN0);
  357. writel_relaxed(0, irqbase + SA1111_INTEN1);
  358. writel_relaxed(0, irqbase + SA1111_WAKEEN0);
  359. writel_relaxed(0, irqbase + SA1111_WAKEEN1);
  360. /*
  361. * detect on rising edge. Note: Feb 2001 Errata for SA1111
  362. * specifies that S0ReadyInt and S1ReadyInt should be '1'.
  363. */
  364. writel_relaxed(0, irqbase + SA1111_INTPOL0);
  365. writel_relaxed(BIT(IRQ_S0_READY_NINT & 31) |
  366. BIT(IRQ_S1_READY_NINT & 31),
  367. irqbase + SA1111_INTPOL1);
  368. /* clear all IRQs */
  369. writel_relaxed(~0, irqbase + SA1111_INTSTATCLR0);
  370. writel_relaxed(~0, irqbase + SA1111_INTSTATCLR1);
  371. sachip->irqdomain = irq_domain_add_linear(NULL, SA1111_IRQ_NR,
  372. &sa1111_irqdomain_ops,
  373. sachip);
  374. if (!sachip->irqdomain) {
  375. irq_free_descs(sachip->irq_base, SA1111_IRQ_NR);
  376. return -ENOMEM;
  377. }
  378. irq_domain_associate_many(sachip->irqdomain,
  379. sachip->irq_base + IRQ_GPAIN0,
  380. IRQ_GPAIN0, SSPROR + 1 - IRQ_GPAIN0);
  381. irq_domain_associate_many(sachip->irqdomain,
  382. sachip->irq_base + AUDXMTDMADONEA,
  383. AUDXMTDMADONEA,
  384. IRQ_S1_BVD1_STSCHG + 1 - AUDXMTDMADONEA);
  385. /*
  386. * Register SA1111 interrupt
  387. */
  388. irq_set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
  389. irq_set_chained_handler_and_data(sachip->irq, sa1111_irq_handler,
  390. sachip);
  391. dev_info(sachip->dev, "Providing IRQ%u-%u\n",
  392. sachip->irq_base, sachip->irq_base + SA1111_IRQ_NR - 1);
  393. return 0;
  394. }
  395. static void sa1111_remove_irq(struct sa1111 *sachip)
  396. {
  397. struct irq_domain *domain = sachip->irqdomain;
  398. void __iomem *irqbase = sachip->base + SA1111_INTC;
  399. int i;
  400. /* disable all IRQs */
  401. writel_relaxed(0, irqbase + SA1111_INTEN0);
  402. writel_relaxed(0, irqbase + SA1111_INTEN1);
  403. writel_relaxed(0, irqbase + SA1111_WAKEEN0);
  404. writel_relaxed(0, irqbase + SA1111_WAKEEN1);
  405. irq_set_chained_handler_and_data(sachip->irq, NULL, NULL);
  406. for (i = 0; i < SA1111_IRQ_NR; i++)
  407. irq_dispose_mapping(irq_find_mapping(domain, i));
  408. irq_domain_remove(domain);
  409. release_mem_region(sachip->phys + SA1111_INTC, 512);
  410. }
  411. enum {
  412. SA1111_GPIO_PXDDR = (SA1111_GPIO_PADDR - SA1111_GPIO_PADDR),
  413. SA1111_GPIO_PXDRR = (SA1111_GPIO_PADRR - SA1111_GPIO_PADDR),
  414. SA1111_GPIO_PXDWR = (SA1111_GPIO_PADWR - SA1111_GPIO_PADDR),
  415. SA1111_GPIO_PXSDR = (SA1111_GPIO_PASDR - SA1111_GPIO_PADDR),
  416. SA1111_GPIO_PXSSR = (SA1111_GPIO_PASSR - SA1111_GPIO_PADDR),
  417. };
  418. static struct sa1111 *gc_to_sa1111(struct gpio_chip *gc)
  419. {
  420. return container_of(gc, struct sa1111, gc);
  421. }
  422. static void __iomem *sa1111_gpio_map_reg(struct sa1111 *sachip, unsigned offset)
  423. {
  424. void __iomem *reg = sachip->base + SA1111_GPIO;
  425. if (offset < 4)
  426. return reg + SA1111_GPIO_PADDR;
  427. if (offset < 10)
  428. return reg + SA1111_GPIO_PBDDR;
  429. if (offset < 18)
  430. return reg + SA1111_GPIO_PCDDR;
  431. return NULL;
  432. }
  433. static u32 sa1111_gpio_map_bit(unsigned offset)
  434. {
  435. if (offset < 4)
  436. return BIT(offset);
  437. if (offset < 10)
  438. return BIT(offset - 4);
  439. if (offset < 18)
  440. return BIT(offset - 10);
  441. return 0;
  442. }
  443. static void sa1111_gpio_modify(void __iomem *reg, u32 mask, u32 set)
  444. {
  445. u32 val;
  446. val = readl_relaxed(reg);
  447. val &= ~mask;
  448. val |= mask & set;
  449. writel_relaxed(val, reg);
  450. }
  451. static int sa1111_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
  452. {
  453. struct sa1111 *sachip = gc_to_sa1111(gc);
  454. void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
  455. u32 mask = sa1111_gpio_map_bit(offset);
  456. return !!(readl_relaxed(reg + SA1111_GPIO_PXDDR) & mask);
  457. }
  458. static int sa1111_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  459. {
  460. struct sa1111 *sachip = gc_to_sa1111(gc);
  461. unsigned long flags;
  462. void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
  463. u32 mask = sa1111_gpio_map_bit(offset);
  464. spin_lock_irqsave(&sachip->lock, flags);
  465. sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, mask);
  466. sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, mask);
  467. spin_unlock_irqrestore(&sachip->lock, flags);
  468. return 0;
  469. }
  470. static int sa1111_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  471. int value)
  472. {
  473. struct sa1111 *sachip = gc_to_sa1111(gc);
  474. unsigned long flags;
  475. void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
  476. u32 mask = sa1111_gpio_map_bit(offset);
  477. spin_lock_irqsave(&sachip->lock, flags);
  478. sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
  479. sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
  480. sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, 0);
  481. sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, 0);
  482. spin_unlock_irqrestore(&sachip->lock, flags);
  483. return 0;
  484. }
  485. static int sa1111_gpio_get(struct gpio_chip *gc, unsigned offset)
  486. {
  487. struct sa1111 *sachip = gc_to_sa1111(gc);
  488. void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
  489. u32 mask = sa1111_gpio_map_bit(offset);
  490. return !!(readl_relaxed(reg + SA1111_GPIO_PXDRR) & mask);
  491. }
  492. static void sa1111_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  493. {
  494. struct sa1111 *sachip = gc_to_sa1111(gc);
  495. unsigned long flags;
  496. void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
  497. u32 mask = sa1111_gpio_map_bit(offset);
  498. spin_lock_irqsave(&sachip->lock, flags);
  499. sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
  500. sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
  501. spin_unlock_irqrestore(&sachip->lock, flags);
  502. }
  503. static void sa1111_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  504. unsigned long *bits)
  505. {
  506. struct sa1111 *sachip = gc_to_sa1111(gc);
  507. unsigned long flags;
  508. void __iomem *reg = sachip->base + SA1111_GPIO;
  509. u32 msk, val;
  510. msk = *mask;
  511. val = *bits;
  512. spin_lock_irqsave(&sachip->lock, flags);
  513. sa1111_gpio_modify(reg + SA1111_GPIO_PADWR, msk & 15, val);
  514. sa1111_gpio_modify(reg + SA1111_GPIO_PASSR, msk & 15, val);
  515. sa1111_gpio_modify(reg + SA1111_GPIO_PBDWR, (msk >> 4) & 255, val >> 4);
  516. sa1111_gpio_modify(reg + SA1111_GPIO_PBSSR, (msk >> 4) & 255, val >> 4);
  517. sa1111_gpio_modify(reg + SA1111_GPIO_PCDWR, (msk >> 12) & 255, val >> 12);
  518. sa1111_gpio_modify(reg + SA1111_GPIO_PCSSR, (msk >> 12) & 255, val >> 12);
  519. spin_unlock_irqrestore(&sachip->lock, flags);
  520. }
  521. static int sa1111_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  522. {
  523. struct sa1111 *sachip = gc_to_sa1111(gc);
  524. return sa1111_map_irq(sachip, offset);
  525. }
  526. static int sa1111_setup_gpios(struct sa1111 *sachip)
  527. {
  528. sachip->gc.label = "sa1111";
  529. sachip->gc.parent = sachip->dev;
  530. sachip->gc.owner = THIS_MODULE;
  531. sachip->gc.get_direction = sa1111_gpio_get_direction;
  532. sachip->gc.direction_input = sa1111_gpio_direction_input;
  533. sachip->gc.direction_output = sa1111_gpio_direction_output;
  534. sachip->gc.get = sa1111_gpio_get;
  535. sachip->gc.set = sa1111_gpio_set;
  536. sachip->gc.set_multiple = sa1111_gpio_set_multiple;
  537. sachip->gc.to_irq = sa1111_gpio_to_irq;
  538. sachip->gc.base = -1;
  539. sachip->gc.ngpio = 18;
  540. return devm_gpiochip_add_data(sachip->dev, &sachip->gc, sachip);
  541. }
  542. /*
  543. * Bring the SA1111 out of reset. This requires a set procedure:
  544. * 1. nRESET asserted (by hardware)
  545. * 2. CLK turned on from SA1110
  546. * 3. nRESET deasserted
  547. * 4. VCO turned on, PLL_BYPASS turned off
  548. * 5. Wait lock time, then assert RCLKEn
  549. * 7. PCR set to allow clocking of individual functions
  550. *
  551. * Until we've done this, the only registers we can access are:
  552. * SBI_SKCR
  553. * SBI_SMCR
  554. * SBI_SKID
  555. */
  556. static void sa1111_wake(struct sa1111 *sachip)
  557. {
  558. unsigned long flags, r;
  559. spin_lock_irqsave(&sachip->lock, flags);
  560. clk_enable(sachip->clk);
  561. /*
  562. * Turn VCO on, and disable PLL Bypass.
  563. */
  564. r = readl_relaxed(sachip->base + SA1111_SKCR);
  565. r &= ~SKCR_VCO_OFF;
  566. writel_relaxed(r, sachip->base + SA1111_SKCR);
  567. r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
  568. writel_relaxed(r, sachip->base + SA1111_SKCR);
  569. /*
  570. * Wait lock time. SA1111 manual _doesn't_
  571. * specify a figure for this! We choose 100us.
  572. */
  573. udelay(100);
  574. /*
  575. * Enable RCLK. We also ensure that RDYEN is set.
  576. */
  577. r |= SKCR_RCLKEN | SKCR_RDYEN;
  578. writel_relaxed(r, sachip->base + SA1111_SKCR);
  579. /*
  580. * Wait 14 RCLK cycles for the chip to finish coming out
  581. * of reset. (RCLK=24MHz). This is 590ns.
  582. */
  583. udelay(1);
  584. /*
  585. * Ensure all clocks are initially off.
  586. */
  587. writel_relaxed(0, sachip->base + SA1111_SKPCR);
  588. spin_unlock_irqrestore(&sachip->lock, flags);
  589. }
  590. #ifdef CONFIG_ARCH_SA1100
  591. static u32 sa1111_dma_mask[] = {
  592. ~0,
  593. ~(1 << 20),
  594. ~(1 << 23),
  595. ~(1 << 24),
  596. ~(1 << 25),
  597. ~(1 << 20),
  598. ~(1 << 20),
  599. 0,
  600. };
  601. /*
  602. * Configure the SA1111 shared memory controller.
  603. */
  604. void
  605. sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
  606. unsigned int cas_latency)
  607. {
  608. unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
  609. if (cas_latency == 3)
  610. smcr |= SMCR_CLAT;
  611. writel_relaxed(smcr, sachip->base + SA1111_SMCR);
  612. /*
  613. * Now clear the bits in the DMA mask to work around the SA1111
  614. * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
  615. * Chip Specification Update, June 2000, Erratum #7).
  616. */
  617. if (sachip->dev->dma_mask)
  618. *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
  619. sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
  620. }
  621. #endif
  622. static void sa1111_dev_release(struct device *_dev)
  623. {
  624. struct sa1111_dev *dev = to_sa1111_device(_dev);
  625. kfree(dev);
  626. }
  627. static int
  628. sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
  629. struct sa1111_dev_info *info)
  630. {
  631. struct sa1111_dev *dev;
  632. unsigned i;
  633. int ret;
  634. dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
  635. if (!dev) {
  636. ret = -ENOMEM;
  637. goto err_alloc;
  638. }
  639. device_initialize(&dev->dev);
  640. dev_set_name(&dev->dev, "%4.4lx", info->offset);
  641. dev->devid = info->devid;
  642. dev->dev.parent = sachip->dev;
  643. dev->dev.bus = &sa1111_bus_type;
  644. dev->dev.release = sa1111_dev_release;
  645. dev->res.start = sachip->phys + info->offset;
  646. dev->res.end = dev->res.start + 511;
  647. dev->res.name = dev_name(&dev->dev);
  648. dev->res.flags = IORESOURCE_MEM;
  649. dev->mapbase = sachip->base + info->offset;
  650. dev->skpcr_mask = info->skpcr_mask;
  651. for (i = 0; i < ARRAY_SIZE(info->hwirq); i++)
  652. dev->hwirq[i] = info->hwirq[i];
  653. /*
  654. * If the parent device has a DMA mask associated with it, and
  655. * this child supports DMA, propagate it down to the children.
  656. */
  657. if (info->dma && sachip->dev->dma_mask) {
  658. dev->dma_mask = *sachip->dev->dma_mask;
  659. dev->dev.dma_mask = &dev->dma_mask;
  660. dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
  661. }
  662. ret = request_resource(parent, &dev->res);
  663. if (ret) {
  664. dev_err(sachip->dev, "failed to allocate resource for %s\n",
  665. dev->res.name);
  666. goto err_resource;
  667. }
  668. ret = device_add(&dev->dev);
  669. if (ret)
  670. goto err_add;
  671. return 0;
  672. err_add:
  673. release_resource(&dev->res);
  674. err_resource:
  675. put_device(&dev->dev);
  676. err_alloc:
  677. return ret;
  678. }
  679. /**
  680. * sa1111_probe - probe for a single SA1111 chip.
  681. * @phys_addr: physical address of device.
  682. *
  683. * Probe for a SA1111 chip. This must be called
  684. * before any other SA1111-specific code.
  685. *
  686. * Returns:
  687. * %-ENODEV device not found.
  688. * %-EBUSY physical address already marked in-use.
  689. * %-EINVAL no platform data passed
  690. * %0 successful.
  691. */
  692. static int __sa1111_probe(struct device *me, struct resource *mem, int irq)
  693. {
  694. struct sa1111_platform_data *pd = me->platform_data;
  695. struct sa1111 *sachip;
  696. unsigned long id;
  697. unsigned int has_devs;
  698. int i, ret = -ENODEV;
  699. if (!pd)
  700. return -EINVAL;
  701. sachip = devm_kzalloc(me, sizeof(struct sa1111), GFP_KERNEL);
  702. if (!sachip)
  703. return -ENOMEM;
  704. sachip->clk = devm_clk_get(me, "SA1111_CLK");
  705. if (IS_ERR(sachip->clk))
  706. return PTR_ERR(sachip->clk);
  707. ret = clk_prepare(sachip->clk);
  708. if (ret)
  709. return ret;
  710. spin_lock_init(&sachip->lock);
  711. sachip->dev = me;
  712. dev_set_drvdata(sachip->dev, sachip);
  713. sachip->pdata = pd;
  714. sachip->phys = mem->start;
  715. sachip->irq = irq;
  716. /*
  717. * Map the whole region. This also maps the
  718. * registers for our children.
  719. */
  720. sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
  721. if (!sachip->base) {
  722. ret = -ENOMEM;
  723. goto err_clk_unprep;
  724. }
  725. /*
  726. * Probe for the chip. Only touch the SBI registers.
  727. */
  728. id = readl_relaxed(sachip->base + SA1111_SKID);
  729. if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
  730. printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
  731. ret = -ENODEV;
  732. goto err_unmap;
  733. }
  734. pr_info("SA1111 Microprocessor Companion Chip: silicon revision %lx, metal revision %lx\n",
  735. (id & SKID_SIREV_MASK) >> 4, id & SKID_MTREV_MASK);
  736. /*
  737. * We found it. Wake the chip up, and initialise.
  738. */
  739. sa1111_wake(sachip);
  740. /*
  741. * The interrupt controller must be initialised before any
  742. * other device to ensure that the interrupts are available.
  743. */
  744. ret = sa1111_setup_irq(sachip, pd->irq_base);
  745. if (ret)
  746. goto err_clk;
  747. /* Setup the GPIOs - should really be done after the IRQ setup */
  748. ret = sa1111_setup_gpios(sachip);
  749. if (ret)
  750. goto err_irq;
  751. #ifdef CONFIG_ARCH_SA1100
  752. {
  753. unsigned int val;
  754. /*
  755. * The SDRAM configuration of the SA1110 and the SA1111 must
  756. * match. This is very important to ensure that SA1111 accesses
  757. * don't corrupt the SDRAM. Note that this ungates the SA1111's
  758. * MBGNT signal, so we must have called sa1110_mb_disable()
  759. * beforehand.
  760. */
  761. sa1111_configure_smc(sachip, 1,
  762. FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
  763. FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
  764. /*
  765. * We only need to turn on DCLK whenever we want to use the
  766. * DMA. It can otherwise be held firmly in the off position.
  767. * (currently, we always enable it.)
  768. */
  769. val = readl_relaxed(sachip->base + SA1111_SKPCR);
  770. writel_relaxed(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
  771. /*
  772. * Enable the SA1110 memory bus request and grant signals.
  773. */
  774. sa1110_mb_enable();
  775. }
  776. #endif
  777. g_sa1111 = sachip;
  778. has_devs = ~0;
  779. if (pd)
  780. has_devs &= ~pd->disable_devs;
  781. for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
  782. if (sa1111_devices[i].devid & has_devs)
  783. sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
  784. return 0;
  785. err_irq:
  786. sa1111_remove_irq(sachip);
  787. err_clk:
  788. clk_disable(sachip->clk);
  789. err_unmap:
  790. iounmap(sachip->base);
  791. err_clk_unprep:
  792. clk_unprepare(sachip->clk);
  793. return ret;
  794. }
  795. static int sa1111_remove_one(struct device *dev, void *data)
  796. {
  797. struct sa1111_dev *sadev = to_sa1111_device(dev);
  798. if (dev->bus != &sa1111_bus_type)
  799. return 0;
  800. device_del(&sadev->dev);
  801. release_resource(&sadev->res);
  802. put_device(&sadev->dev);
  803. return 0;
  804. }
  805. static void __sa1111_remove(struct sa1111 *sachip)
  806. {
  807. device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
  808. sa1111_remove_irq(sachip);
  809. clk_disable(sachip->clk);
  810. clk_unprepare(sachip->clk);
  811. iounmap(sachip->base);
  812. }
  813. struct sa1111_save_data {
  814. unsigned int skcr;
  815. unsigned int skpcr;
  816. unsigned int skcdr;
  817. unsigned char skaud;
  818. unsigned char skpwm0;
  819. unsigned char skpwm1;
  820. /*
  821. * Interrupt controller
  822. */
  823. unsigned int intpol0;
  824. unsigned int intpol1;
  825. unsigned int inten0;
  826. unsigned int inten1;
  827. unsigned int wakepol0;
  828. unsigned int wakepol1;
  829. unsigned int wakeen0;
  830. unsigned int wakeen1;
  831. };
  832. #ifdef CONFIG_PM
  833. static int sa1111_suspend_noirq(struct device *dev)
  834. {
  835. struct sa1111 *sachip = dev_get_drvdata(dev);
  836. struct sa1111_save_data *save;
  837. unsigned long flags;
  838. unsigned int val;
  839. void __iomem *base;
  840. save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
  841. if (!save)
  842. return -ENOMEM;
  843. sachip->saved_state = save;
  844. spin_lock_irqsave(&sachip->lock, flags);
  845. /*
  846. * Save state.
  847. */
  848. base = sachip->base;
  849. save->skcr = readl_relaxed(base + SA1111_SKCR);
  850. save->skpcr = readl_relaxed(base + SA1111_SKPCR);
  851. save->skcdr = readl_relaxed(base + SA1111_SKCDR);
  852. save->skaud = readl_relaxed(base + SA1111_SKAUD);
  853. save->skpwm0 = readl_relaxed(base + SA1111_SKPWM0);
  854. save->skpwm1 = readl_relaxed(base + SA1111_SKPWM1);
  855. writel_relaxed(0, sachip->base + SA1111_SKPWM0);
  856. writel_relaxed(0, sachip->base + SA1111_SKPWM1);
  857. base = sachip->base + SA1111_INTC;
  858. save->intpol0 = readl_relaxed(base + SA1111_INTPOL0);
  859. save->intpol1 = readl_relaxed(base + SA1111_INTPOL1);
  860. save->inten0 = readl_relaxed(base + SA1111_INTEN0);
  861. save->inten1 = readl_relaxed(base + SA1111_INTEN1);
  862. save->wakepol0 = readl_relaxed(base + SA1111_WAKEPOL0);
  863. save->wakepol1 = readl_relaxed(base + SA1111_WAKEPOL1);
  864. save->wakeen0 = readl_relaxed(base + SA1111_WAKEEN0);
  865. save->wakeen1 = readl_relaxed(base + SA1111_WAKEEN1);
  866. /*
  867. * Disable.
  868. */
  869. val = readl_relaxed(sachip->base + SA1111_SKCR);
  870. writel_relaxed(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
  871. clk_disable(sachip->clk);
  872. spin_unlock_irqrestore(&sachip->lock, flags);
  873. #ifdef CONFIG_ARCH_SA1100
  874. sa1110_mb_disable();
  875. #endif
  876. return 0;
  877. }
  878. /*
  879. * sa1111_resume - Restore the SA1111 device state.
  880. * @dev: device to restore
  881. *
  882. * Restore the general state of the SA1111; clock control and
  883. * interrupt controller. Other parts of the SA1111 must be
  884. * restored by their respective drivers, and must be called
  885. * via LDM after this function.
  886. */
  887. static int sa1111_resume_noirq(struct device *dev)
  888. {
  889. struct sa1111 *sachip = dev_get_drvdata(dev);
  890. struct sa1111_save_data *save;
  891. unsigned long flags, id;
  892. void __iomem *base;
  893. save = sachip->saved_state;
  894. if (!save)
  895. return 0;
  896. /*
  897. * Ensure that the SA1111 is still here.
  898. * FIXME: shouldn't do this here.
  899. */
  900. id = readl_relaxed(sachip->base + SA1111_SKID);
  901. if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
  902. __sa1111_remove(sachip);
  903. dev_set_drvdata(dev, NULL);
  904. kfree(save);
  905. return 0;
  906. }
  907. /*
  908. * First of all, wake up the chip.
  909. */
  910. sa1111_wake(sachip);
  911. #ifdef CONFIG_ARCH_SA1100
  912. /* Enable the memory bus request/grant signals */
  913. sa1110_mb_enable();
  914. #endif
  915. /*
  916. * Only lock for write ops. Also, sa1111_wake must be called with
  917. * released spinlock!
  918. */
  919. spin_lock_irqsave(&sachip->lock, flags);
  920. writel_relaxed(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
  921. writel_relaxed(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
  922. base = sachip->base;
  923. writel_relaxed(save->skcr, base + SA1111_SKCR);
  924. writel_relaxed(save->skpcr, base + SA1111_SKPCR);
  925. writel_relaxed(save->skcdr, base + SA1111_SKCDR);
  926. writel_relaxed(save->skaud, base + SA1111_SKAUD);
  927. writel_relaxed(save->skpwm0, base + SA1111_SKPWM0);
  928. writel_relaxed(save->skpwm1, base + SA1111_SKPWM1);
  929. base = sachip->base + SA1111_INTC;
  930. writel_relaxed(save->intpol0, base + SA1111_INTPOL0);
  931. writel_relaxed(save->intpol1, base + SA1111_INTPOL1);
  932. writel_relaxed(save->inten0, base + SA1111_INTEN0);
  933. writel_relaxed(save->inten1, base + SA1111_INTEN1);
  934. writel_relaxed(save->wakepol0, base + SA1111_WAKEPOL0);
  935. writel_relaxed(save->wakepol1, base + SA1111_WAKEPOL1);
  936. writel_relaxed(save->wakeen0, base + SA1111_WAKEEN0);
  937. writel_relaxed(save->wakeen1, base + SA1111_WAKEEN1);
  938. spin_unlock_irqrestore(&sachip->lock, flags);
  939. sachip->saved_state = NULL;
  940. kfree(save);
  941. return 0;
  942. }
  943. #else
  944. #define sa1111_suspend_noirq NULL
  945. #define sa1111_resume_noirq NULL
  946. #endif
  947. static int sa1111_probe(struct platform_device *pdev)
  948. {
  949. struct resource *mem;
  950. int irq;
  951. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  952. if (!mem)
  953. return -EINVAL;
  954. irq = platform_get_irq(pdev, 0);
  955. if (irq < 0)
  956. return irq;
  957. return __sa1111_probe(&pdev->dev, mem, irq);
  958. }
  959. static int sa1111_remove(struct platform_device *pdev)
  960. {
  961. struct sa1111 *sachip = platform_get_drvdata(pdev);
  962. if (sachip) {
  963. #ifdef CONFIG_PM
  964. kfree(sachip->saved_state);
  965. sachip->saved_state = NULL;
  966. #endif
  967. __sa1111_remove(sachip);
  968. platform_set_drvdata(pdev, NULL);
  969. }
  970. return 0;
  971. }
  972. static struct dev_pm_ops sa1111_pm_ops = {
  973. .suspend_noirq = sa1111_suspend_noirq,
  974. .resume_noirq = sa1111_resume_noirq,
  975. };
  976. /*
  977. * Not sure if this should be on the system bus or not yet.
  978. * We really want some way to register a system device at
  979. * the per-machine level, and then have this driver pick
  980. * up the registered devices.
  981. *
  982. * We also need to handle the SDRAM configuration for
  983. * PXA250/SA1110 machine classes.
  984. */
  985. static struct platform_driver sa1111_device_driver = {
  986. .probe = sa1111_probe,
  987. .remove = sa1111_remove,
  988. .driver = {
  989. .name = "sa1111",
  990. .pm = &sa1111_pm_ops,
  991. },
  992. };
  993. /*
  994. * Get the parent device driver (us) structure
  995. * from a child function device
  996. */
  997. static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
  998. {
  999. return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
  1000. }
  1001. /*
  1002. * The bits in the opdiv field are non-linear.
  1003. */
  1004. static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
  1005. static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
  1006. {
  1007. unsigned int skcdr, fbdiv, ipdiv, opdiv;
  1008. skcdr = readl_relaxed(sachip->base + SA1111_SKCDR);
  1009. fbdiv = (skcdr & 0x007f) + 2;
  1010. ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
  1011. opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
  1012. return 3686400 * fbdiv / (ipdiv * opdiv);
  1013. }
  1014. /**
  1015. * sa1111_pll_clock - return the current PLL clock frequency.
  1016. * @sadev: SA1111 function block
  1017. *
  1018. * BUG: we should look at SKCR. We also blindly believe that
  1019. * the chip is being fed with the 3.6864MHz clock.
  1020. *
  1021. * Returns the PLL clock in Hz.
  1022. */
  1023. unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
  1024. {
  1025. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1026. return __sa1111_pll_clock(sachip);
  1027. }
  1028. EXPORT_SYMBOL(sa1111_pll_clock);
  1029. /**
  1030. * sa1111_select_audio_mode - select I2S or AC link mode
  1031. * @sadev: SA1111 function block
  1032. * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
  1033. *
  1034. * Frob the SKCR to select AC Link mode or I2S mode for
  1035. * the audio block.
  1036. */
  1037. void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
  1038. {
  1039. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1040. unsigned long flags;
  1041. unsigned int val;
  1042. spin_lock_irqsave(&sachip->lock, flags);
  1043. val = readl_relaxed(sachip->base + SA1111_SKCR);
  1044. if (mode == SA1111_AUDIO_I2S) {
  1045. val &= ~SKCR_SELAC;
  1046. } else {
  1047. val |= SKCR_SELAC;
  1048. }
  1049. writel_relaxed(val, sachip->base + SA1111_SKCR);
  1050. spin_unlock_irqrestore(&sachip->lock, flags);
  1051. }
  1052. EXPORT_SYMBOL(sa1111_select_audio_mode);
  1053. /**
  1054. * sa1111_set_audio_rate - set the audio sample rate
  1055. * @sadev: SA1111 SAC function block
  1056. * @rate: sample rate to select
  1057. */
  1058. int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
  1059. {
  1060. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1061. unsigned int div;
  1062. if (sadev->devid != SA1111_DEVID_SAC)
  1063. return -EINVAL;
  1064. div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
  1065. if (div == 0)
  1066. div = 1;
  1067. if (div > 128)
  1068. div = 128;
  1069. writel_relaxed(div - 1, sachip->base + SA1111_SKAUD);
  1070. return 0;
  1071. }
  1072. EXPORT_SYMBOL(sa1111_set_audio_rate);
  1073. /**
  1074. * sa1111_get_audio_rate - get the audio sample rate
  1075. * @sadev: SA1111 SAC function block device
  1076. */
  1077. int sa1111_get_audio_rate(struct sa1111_dev *sadev)
  1078. {
  1079. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1080. unsigned long div;
  1081. if (sadev->devid != SA1111_DEVID_SAC)
  1082. return -EINVAL;
  1083. div = readl_relaxed(sachip->base + SA1111_SKAUD) + 1;
  1084. return __sa1111_pll_clock(sachip) / (256 * div);
  1085. }
  1086. EXPORT_SYMBOL(sa1111_get_audio_rate);
  1087. void sa1111_set_io_dir(struct sa1111_dev *sadev,
  1088. unsigned int bits, unsigned int dir,
  1089. unsigned int sleep_dir)
  1090. {
  1091. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1092. unsigned long flags;
  1093. unsigned int val;
  1094. void __iomem *gpio = sachip->base + SA1111_GPIO;
  1095. #define MODIFY_BITS(port, mask, dir) \
  1096. if (mask) { \
  1097. val = readl_relaxed(port); \
  1098. val &= ~(mask); \
  1099. val |= (dir) & (mask); \
  1100. writel_relaxed(val, port); \
  1101. }
  1102. spin_lock_irqsave(&sachip->lock, flags);
  1103. MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
  1104. MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
  1105. MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
  1106. MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
  1107. MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
  1108. MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
  1109. spin_unlock_irqrestore(&sachip->lock, flags);
  1110. }
  1111. EXPORT_SYMBOL(sa1111_set_io_dir);
  1112. void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
  1113. {
  1114. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1115. unsigned long flags;
  1116. unsigned int val;
  1117. void __iomem *gpio = sachip->base + SA1111_GPIO;
  1118. spin_lock_irqsave(&sachip->lock, flags);
  1119. MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
  1120. MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
  1121. MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
  1122. spin_unlock_irqrestore(&sachip->lock, flags);
  1123. }
  1124. EXPORT_SYMBOL(sa1111_set_io);
  1125. void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
  1126. {
  1127. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1128. unsigned long flags;
  1129. unsigned int val;
  1130. void __iomem *gpio = sachip->base + SA1111_GPIO;
  1131. spin_lock_irqsave(&sachip->lock, flags);
  1132. MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
  1133. MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
  1134. MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
  1135. spin_unlock_irqrestore(&sachip->lock, flags);
  1136. }
  1137. EXPORT_SYMBOL(sa1111_set_sleep_io);
  1138. /*
  1139. * Individual device operations.
  1140. */
  1141. /**
  1142. * sa1111_enable_device - enable an on-chip SA1111 function block
  1143. * @sadev: SA1111 function block device to enable
  1144. */
  1145. int sa1111_enable_device(struct sa1111_dev *sadev)
  1146. {
  1147. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1148. unsigned long flags;
  1149. unsigned int val;
  1150. int ret = 0;
  1151. if (sachip->pdata && sachip->pdata->enable)
  1152. ret = sachip->pdata->enable(sachip->pdata->data, sadev->devid);
  1153. if (ret == 0) {
  1154. spin_lock_irqsave(&sachip->lock, flags);
  1155. val = readl_relaxed(sachip->base + SA1111_SKPCR);
  1156. writel_relaxed(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
  1157. spin_unlock_irqrestore(&sachip->lock, flags);
  1158. }
  1159. return ret;
  1160. }
  1161. EXPORT_SYMBOL(sa1111_enable_device);
  1162. /**
  1163. * sa1111_disable_device - disable an on-chip SA1111 function block
  1164. * @sadev: SA1111 function block device to disable
  1165. */
  1166. void sa1111_disable_device(struct sa1111_dev *sadev)
  1167. {
  1168. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1169. unsigned long flags;
  1170. unsigned int val;
  1171. spin_lock_irqsave(&sachip->lock, flags);
  1172. val = readl_relaxed(sachip->base + SA1111_SKPCR);
  1173. writel_relaxed(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
  1174. spin_unlock_irqrestore(&sachip->lock, flags);
  1175. if (sachip->pdata && sachip->pdata->disable)
  1176. sachip->pdata->disable(sachip->pdata->data, sadev->devid);
  1177. }
  1178. EXPORT_SYMBOL(sa1111_disable_device);
  1179. int sa1111_get_irq(struct sa1111_dev *sadev, unsigned num)
  1180. {
  1181. struct sa1111 *sachip = sa1111_chip_driver(sadev);
  1182. if (num >= ARRAY_SIZE(sadev->hwirq))
  1183. return -EINVAL;
  1184. return sa1111_map_irq(sachip, sadev->hwirq[num]);
  1185. }
  1186. EXPORT_SYMBOL_GPL(sa1111_get_irq);
  1187. /*
  1188. * SA1111 "Register Access Bus."
  1189. *
  1190. * We model this as a regular bus type, and hang devices directly
  1191. * off this.
  1192. */
  1193. static int sa1111_match(struct device *_dev, struct device_driver *_drv)
  1194. {
  1195. struct sa1111_dev *dev = to_sa1111_device(_dev);
  1196. struct sa1111_driver *drv = SA1111_DRV(_drv);
  1197. return !!(dev->devid & drv->devid);
  1198. }
  1199. static int sa1111_bus_probe(struct device *dev)
  1200. {
  1201. struct sa1111_dev *sadev = to_sa1111_device(dev);
  1202. struct sa1111_driver *drv = SA1111_DRV(dev->driver);
  1203. int ret = -ENODEV;
  1204. if (drv->probe)
  1205. ret = drv->probe(sadev);
  1206. return ret;
  1207. }
  1208. static int sa1111_bus_remove(struct device *dev)
  1209. {
  1210. struct sa1111_dev *sadev = to_sa1111_device(dev);
  1211. struct sa1111_driver *drv = SA1111_DRV(dev->driver);
  1212. int ret = 0;
  1213. if (drv->remove)
  1214. ret = drv->remove(sadev);
  1215. return ret;
  1216. }
  1217. struct bus_type sa1111_bus_type = {
  1218. .name = "sa1111-rab",
  1219. .match = sa1111_match,
  1220. .probe = sa1111_bus_probe,
  1221. .remove = sa1111_bus_remove,
  1222. };
  1223. EXPORT_SYMBOL(sa1111_bus_type);
  1224. int sa1111_driver_register(struct sa1111_driver *driver)
  1225. {
  1226. driver->drv.bus = &sa1111_bus_type;
  1227. return driver_register(&driver->drv);
  1228. }
  1229. EXPORT_SYMBOL(sa1111_driver_register);
  1230. void sa1111_driver_unregister(struct sa1111_driver *driver)
  1231. {
  1232. driver_unregister(&driver->drv);
  1233. }
  1234. EXPORT_SYMBOL(sa1111_driver_unregister);
  1235. #ifdef CONFIG_DMABOUNCE
  1236. /*
  1237. * According to the "Intel StrongARM SA-1111 Microprocessor Companion
  1238. * Chip Specification Update" (June 2000), erratum #7, there is a
  1239. * significant bug in the SA1111 SDRAM shared memory controller. If
  1240. * an access to a region of memory above 1MB relative to the bank base,
  1241. * it is important that address bit 10 _NOT_ be asserted. Depending
  1242. * on the configuration of the RAM, bit 10 may correspond to one
  1243. * of several different (processor-relative) address bits.
  1244. *
  1245. * This routine only identifies whether or not a given DMA address
  1246. * is susceptible to the bug.
  1247. *
  1248. * This should only get called for sa1111_device types due to the
  1249. * way we configure our device dma_masks.
  1250. */
  1251. static int sa1111_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
  1252. {
  1253. /*
  1254. * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
  1255. * User's Guide" mentions that jumpers R51 and R52 control the
  1256. * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
  1257. * SDRAM bank 1 on Neponset). The default configuration selects
  1258. * Assabet, so any address in bank 1 is necessarily invalid.
  1259. */
  1260. return (machine_is_assabet() || machine_is_pfs168()) &&
  1261. (addr >= 0xc8000000 || (addr + size) >= 0xc8000000);
  1262. }
  1263. static int sa1111_notifier_call(struct notifier_block *n, unsigned long action,
  1264. void *data)
  1265. {
  1266. struct sa1111_dev *dev = to_sa1111_device(data);
  1267. switch (action) {
  1268. case BUS_NOTIFY_ADD_DEVICE:
  1269. if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL) {
  1270. int ret = dmabounce_register_dev(&dev->dev, 1024, 4096,
  1271. sa1111_needs_bounce);
  1272. if (ret)
  1273. dev_err(&dev->dev, "failed to register with dmabounce: %d\n", ret);
  1274. }
  1275. break;
  1276. case BUS_NOTIFY_DEL_DEVICE:
  1277. if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL)
  1278. dmabounce_unregister_dev(&dev->dev);
  1279. break;
  1280. }
  1281. return NOTIFY_OK;
  1282. }
  1283. static struct notifier_block sa1111_bus_notifier = {
  1284. .notifier_call = sa1111_notifier_call,
  1285. };
  1286. #endif
  1287. static int __init sa1111_init(void)
  1288. {
  1289. int ret = bus_register(&sa1111_bus_type);
  1290. #ifdef CONFIG_DMABOUNCE
  1291. if (ret == 0)
  1292. bus_register_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
  1293. #endif
  1294. if (ret == 0)
  1295. platform_driver_register(&sa1111_device_driver);
  1296. return ret;
  1297. }
  1298. static void __exit sa1111_exit(void)
  1299. {
  1300. platform_driver_unregister(&sa1111_device_driver);
  1301. #ifdef CONFIG_DMABOUNCE
  1302. bus_unregister_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
  1303. #endif
  1304. bus_unregister(&sa1111_bus_type);
  1305. }
  1306. subsys_initcall(sa1111_init);
  1307. module_exit(sa1111_exit);
  1308. MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
  1309. MODULE_LICENSE("GPL");