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