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