sa1111.c 35 KB

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