sa1111.c 39 KB

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