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