sa1111.c 36 KB

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