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