GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / arm / common / sa1111.c
blobdfdd2241136334cbb1096e2f2ab784f7faca16b1
1 /*
2 * linux/arch/arm/common/sa1111.c
4 * SA1111 support
6 * Original code by John Dorsey
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.
12 * This file contains all generic SA1111 support.
14 * All initialization functions provided here are intended to be called
15 * from machine specific code with proper arguments when required.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/ioport.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/clk.h>
28 #include <linux/io.h>
30 #include <mach/hardware.h>
31 #include <asm/mach-types.h>
32 #include <asm/irq.h>
33 #include <asm/mach/irq.h>
34 #include <asm/sizes.h>
36 #include <asm/hardware/sa1111.h>
38 /* SA1111 IRQs */
39 #define IRQ_GPAIN0 (0)
40 #define IRQ_GPAIN1 (1)
41 #define IRQ_GPAIN2 (2)
42 #define IRQ_GPAIN3 (3)
43 #define IRQ_GPBIN0 (4)
44 #define IRQ_GPBIN1 (5)
45 #define IRQ_GPBIN2 (6)
46 #define IRQ_GPBIN3 (7)
47 #define IRQ_GPBIN4 (8)
48 #define IRQ_GPBIN5 (9)
49 #define IRQ_GPCIN0 (10)
50 #define IRQ_GPCIN1 (11)
51 #define IRQ_GPCIN2 (12)
52 #define IRQ_GPCIN3 (13)
53 #define IRQ_GPCIN4 (14)
54 #define IRQ_GPCIN5 (15)
55 #define IRQ_GPCIN6 (16)
56 #define IRQ_GPCIN7 (17)
57 #define IRQ_MSTXINT (18)
58 #define IRQ_MSRXINT (19)
59 #define IRQ_MSSTOPERRINT (20)
60 #define IRQ_TPTXINT (21)
61 #define IRQ_TPRXINT (22)
62 #define IRQ_TPSTOPERRINT (23)
63 #define SSPXMTINT (24)
64 #define SSPRCVINT (25)
65 #define SSPROR (26)
66 #define AUDXMTDMADONEA (32)
67 #define AUDRCVDMADONEA (33)
68 #define AUDXMTDMADONEB (34)
69 #define AUDRCVDMADONEB (35)
70 #define AUDTFSR (36)
71 #define AUDRFSR (37)
72 #define AUDTUR (38)
73 #define AUDROR (39)
74 #define AUDDTS (40)
75 #define AUDRDD (41)
76 #define AUDSTO (42)
77 #define IRQ_USBPWR (43)
78 #define IRQ_HCIM (44)
79 #define IRQ_HCIBUFFACC (45)
80 #define IRQ_HCIRMTWKP (46)
81 #define IRQ_NHCIMFCIR (47)
82 #define IRQ_USB_PORT_RESUME (48)
83 #define IRQ_S0_READY_NINT (49)
84 #define IRQ_S1_READY_NINT (50)
85 #define IRQ_S0_CD_VALID (51)
86 #define IRQ_S1_CD_VALID (52)
87 #define IRQ_S0_BVD1_STSCHG (53)
88 #define IRQ_S1_BVD1_STSCHG (54)
90 extern void __init sa1110_mb_enable(void);
93 * We keep the following data for the overall SA1111. Note that the
94 * struct device and struct resource are "fake"; they should be supplied
95 * by the bus above us. However, in the interests of getting all SA1111
96 * drivers converted over to the device model, we provide this as an
97 * anchor point for all the other drivers.
99 struct sa1111 {
100 struct device *dev;
101 struct clk *clk;
102 unsigned long phys;
103 int irq;
104 int irq_base; /* base for cascaded on-chip IRQs */
105 spinlock_t lock;
106 void __iomem *base;
107 #ifdef CONFIG_PM
108 void *saved_state;
109 #endif
113 * We _really_ need to eliminate this. Its only users
114 * are the PWM and DMA checking code.
116 static struct sa1111 *g_sa1111;
118 struct sa1111_dev_info {
119 unsigned long offset;
120 unsigned long skpcr_mask;
121 unsigned int devid;
122 unsigned int irq[6];
125 static struct sa1111_dev_info sa1111_devices[] = {
127 .offset = SA1111_USB,
128 .skpcr_mask = SKPCR_UCLKEN,
129 .devid = SA1111_DEVID_USB,
130 .irq = {
131 IRQ_USBPWR,
132 IRQ_HCIM,
133 IRQ_HCIBUFFACC,
134 IRQ_HCIRMTWKP,
135 IRQ_NHCIMFCIR,
136 IRQ_USB_PORT_RESUME
140 .offset = 0x0600,
141 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
142 .devid = SA1111_DEVID_SAC,
143 .irq = {
144 AUDXMTDMADONEA,
145 AUDXMTDMADONEB,
146 AUDRCVDMADONEA,
147 AUDRCVDMADONEB
151 .offset = 0x0800,
152 .skpcr_mask = SKPCR_SCLKEN,
153 .devid = SA1111_DEVID_SSP,
156 .offset = SA1111_KBD,
157 .skpcr_mask = SKPCR_PTCLKEN,
158 .devid = SA1111_DEVID_PS2,
159 .irq = {
160 IRQ_TPRXINT,
161 IRQ_TPTXINT
165 .offset = SA1111_MSE,
166 .skpcr_mask = SKPCR_PMCLKEN,
167 .devid = SA1111_DEVID_PS2,
168 .irq = {
169 IRQ_MSRXINT,
170 IRQ_MSTXINT
174 .offset = 0x1800,
175 .skpcr_mask = 0,
176 .devid = SA1111_DEVID_PCMCIA,
177 .irq = {
178 IRQ_S0_READY_NINT,
179 IRQ_S0_CD_VALID,
180 IRQ_S0_BVD1_STSCHG,
181 IRQ_S1_READY_NINT,
182 IRQ_S1_CD_VALID,
183 IRQ_S1_BVD1_STSCHG,
188 void __init sa1111_adjust_zones(unsigned long *size, unsigned long *holes)
190 unsigned int sz = SZ_1M >> PAGE_SHIFT;
192 size[1] = size[0] - sz;
193 size[0] = sz;
197 * SA1111 interrupt support. Since clearing an IRQ while there are
198 * active IRQs causes the interrupt output to pulse, the upper levels
199 * will call us again if there are more interrupts to process.
201 static void
202 sa1111_irq_handler(unsigned int irq, struct irq_desc *desc)
204 unsigned int stat0, stat1, i;
205 struct sa1111 *sachip = get_irq_data(irq);
206 void __iomem *mapbase = sachip->base + SA1111_INTC;
208 stat0 = sa1111_readl(mapbase + SA1111_INTSTATCLR0);
209 stat1 = sa1111_readl(mapbase + SA1111_INTSTATCLR1);
211 sa1111_writel(stat0, mapbase + SA1111_INTSTATCLR0);
213 desc->chip->ack(irq);
215 sa1111_writel(stat1, mapbase + SA1111_INTSTATCLR1);
217 if (stat0 == 0 && stat1 == 0) {
218 do_bad_IRQ(irq, desc);
219 return;
222 for (i = 0; stat0; i++, stat0 >>= 1)
223 if (stat0 & 1)
224 generic_handle_irq(i + sachip->irq_base);
226 for (i = 32; stat1; i++, stat1 >>= 1)
227 if (stat1 & 1)
228 generic_handle_irq(i + sachip->irq_base);
230 /* For level-based interrupts */
231 desc->chip->unmask(irq);
234 #define SA1111_IRQMASK_LO(x) (1 << (x - sachip->irq_base))
235 #define SA1111_IRQMASK_HI(x) (1 << (x - sachip->irq_base - 32))
237 static void sa1111_ack_irq(unsigned int irq)
241 static void sa1111_mask_lowirq(unsigned int irq)
243 struct sa1111 *sachip = get_irq_chip_data(irq);
244 void __iomem *mapbase = sachip->base + SA1111_INTC;
245 unsigned long ie0;
247 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
248 ie0 &= ~SA1111_IRQMASK_LO(irq);
249 writel(ie0, mapbase + SA1111_INTEN0);
252 static void sa1111_unmask_lowirq(unsigned int irq)
254 struct sa1111 *sachip = get_irq_chip_data(irq);
255 void __iomem *mapbase = sachip->base + SA1111_INTC;
256 unsigned long ie0;
258 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
259 ie0 |= SA1111_IRQMASK_LO(irq);
260 sa1111_writel(ie0, mapbase + SA1111_INTEN0);
264 * Attempt to re-trigger the interrupt. The SA1111 contains a register
265 * (INTSET) which claims to do this. However, in practice no amount of
266 * manipulation of INTEN and INTSET guarantees that the interrupt will
267 * be triggered. In fact, its very difficult, if not impossible to get
268 * INTSET to re-trigger the interrupt.
270 static int sa1111_retrigger_lowirq(unsigned int irq)
272 struct sa1111 *sachip = get_irq_chip_data(irq);
273 void __iomem *mapbase = sachip->base + SA1111_INTC;
274 unsigned int mask = SA1111_IRQMASK_LO(irq);
275 unsigned long ip0;
276 int i;
278 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
279 for (i = 0; i < 8; i++) {
280 sa1111_writel(ip0 ^ mask, mapbase + SA1111_INTPOL0);
281 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
282 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
283 break;
286 if (i == 8)
287 printk(KERN_ERR "Danger Will Robinson: failed to "
288 "re-trigger IRQ%d\n", irq);
289 return i == 8 ? -1 : 0;
292 static int sa1111_type_lowirq(unsigned int irq, unsigned int flags)
294 struct sa1111 *sachip = get_irq_chip_data(irq);
295 void __iomem *mapbase = sachip->base + SA1111_INTC;
296 unsigned int mask = SA1111_IRQMASK_LO(irq);
297 unsigned long ip0;
299 if (flags == IRQ_TYPE_PROBE)
300 return 0;
302 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
303 return -EINVAL;
305 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
306 if (flags & IRQ_TYPE_EDGE_RISING)
307 ip0 &= ~mask;
308 else
309 ip0 |= mask;
310 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
311 sa1111_writel(ip0, mapbase + SA1111_WAKEPOL0);
313 return 0;
316 static int sa1111_wake_lowirq(unsigned int irq, unsigned int on)
318 struct sa1111 *sachip = get_irq_chip_data(irq);
319 void __iomem *mapbase = sachip->base + SA1111_INTC;
320 unsigned int mask = SA1111_IRQMASK_LO(irq);
321 unsigned long we0;
323 we0 = sa1111_readl(mapbase + SA1111_WAKEEN0);
324 if (on)
325 we0 |= mask;
326 else
327 we0 &= ~mask;
328 sa1111_writel(we0, mapbase + SA1111_WAKEEN0);
330 return 0;
333 static struct irq_chip sa1111_low_chip = {
334 .name = "SA1111-l",
335 .ack = sa1111_ack_irq,
336 .mask = sa1111_mask_lowirq,
337 .unmask = sa1111_unmask_lowirq,
338 .retrigger = sa1111_retrigger_lowirq,
339 .set_type = sa1111_type_lowirq,
340 .set_wake = sa1111_wake_lowirq,
343 static void sa1111_mask_highirq(unsigned int irq)
345 struct sa1111 *sachip = get_irq_chip_data(irq);
346 void __iomem *mapbase = sachip->base + SA1111_INTC;
347 unsigned long ie1;
349 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
350 ie1 &= ~SA1111_IRQMASK_HI(irq);
351 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
354 static void sa1111_unmask_highirq(unsigned int irq)
356 struct sa1111 *sachip = get_irq_chip_data(irq);
357 void __iomem *mapbase = sachip->base + SA1111_INTC;
358 unsigned long ie1;
360 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
361 ie1 |= SA1111_IRQMASK_HI(irq);
362 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
366 * Attempt to re-trigger the interrupt. The SA1111 contains a register
367 * (INTSET) which claims to do this. However, in practice no amount of
368 * manipulation of INTEN and INTSET guarantees that the interrupt will
369 * be triggered. In fact, its very difficult, if not impossible to get
370 * INTSET to re-trigger the interrupt.
372 static int sa1111_retrigger_highirq(unsigned int irq)
374 struct sa1111 *sachip = get_irq_chip_data(irq);
375 void __iomem *mapbase = sachip->base + SA1111_INTC;
376 unsigned int mask = SA1111_IRQMASK_HI(irq);
377 unsigned long ip1;
378 int i;
380 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
381 for (i = 0; i < 8; i++) {
382 sa1111_writel(ip1 ^ mask, mapbase + SA1111_INTPOL1);
383 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
384 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
385 break;
388 if (i == 8)
389 printk(KERN_ERR "Danger Will Robinson: failed to "
390 "re-trigger IRQ%d\n", irq);
391 return i == 8 ? -1 : 0;
394 static int sa1111_type_highirq(unsigned int irq, unsigned int flags)
396 struct sa1111 *sachip = get_irq_chip_data(irq);
397 void __iomem *mapbase = sachip->base + SA1111_INTC;
398 unsigned int mask = SA1111_IRQMASK_HI(irq);
399 unsigned long ip1;
401 if (flags == IRQ_TYPE_PROBE)
402 return 0;
404 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
405 return -EINVAL;
407 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
408 if (flags & IRQ_TYPE_EDGE_RISING)
409 ip1 &= ~mask;
410 else
411 ip1 |= mask;
412 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
413 sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);
415 return 0;
418 static int sa1111_wake_highirq(unsigned int irq, unsigned int on)
420 struct sa1111 *sachip = get_irq_chip_data(irq);
421 void __iomem *mapbase = sachip->base + SA1111_INTC;
422 unsigned int mask = SA1111_IRQMASK_HI(irq);
423 unsigned long we1;
425 we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
426 if (on)
427 we1 |= mask;
428 else
429 we1 &= ~mask;
430 sa1111_writel(we1, mapbase + SA1111_WAKEEN1);
432 return 0;
435 static struct irq_chip sa1111_high_chip = {
436 .name = "SA1111-h",
437 .ack = sa1111_ack_irq,
438 .mask = sa1111_mask_highirq,
439 .unmask = sa1111_unmask_highirq,
440 .retrigger = sa1111_retrigger_highirq,
441 .set_type = sa1111_type_highirq,
442 .set_wake = sa1111_wake_highirq,
445 static void sa1111_setup_irq(struct sa1111 *sachip)
447 void __iomem *irqbase = sachip->base + SA1111_INTC;
448 unsigned int irq;
451 * We're guaranteed that this region hasn't been taken.
453 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
455 /* disable all IRQs */
456 sa1111_writel(0, irqbase + SA1111_INTEN0);
457 sa1111_writel(0, irqbase + SA1111_INTEN1);
458 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
459 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
462 * detect on rising edge. Note: Feb 2001 Errata for SA1111
463 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
465 sa1111_writel(0, irqbase + SA1111_INTPOL0);
466 sa1111_writel(SA1111_IRQMASK_HI(IRQ_S0_READY_NINT) |
467 SA1111_IRQMASK_HI(IRQ_S1_READY_NINT),
468 irqbase + SA1111_INTPOL1);
470 /* clear all IRQs */
471 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR0);
472 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR1);
474 for (irq = IRQ_GPAIN0; irq <= SSPROR; irq++) {
475 set_irq_chip(irq, &sa1111_low_chip);
476 set_irq_chip_data(irq, sachip);
477 set_irq_handler(irq, handle_edge_irq);
478 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
481 for (irq = AUDXMTDMADONEA; irq <= IRQ_S1_BVD1_STSCHG; irq++) {
482 set_irq_chip(irq, &sa1111_high_chip);
483 set_irq_chip_data(irq, sachip);
484 set_irq_handler(irq, handle_edge_irq);
485 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
489 * Register SA1111 interrupt
491 set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
492 set_irq_data(sachip->irq, sachip);
493 set_irq_chained_handler(sachip->irq, sa1111_irq_handler);
497 * Bring the SA1111 out of reset. This requires a set procedure:
498 * 1. nRESET asserted (by hardware)
499 * 2. CLK turned on from SA1110
500 * 3. nRESET deasserted
501 * 4. VCO turned on, PLL_BYPASS turned off
502 * 5. Wait lock time, then assert RCLKEn
503 * 7. PCR set to allow clocking of individual functions
505 * Until we've done this, the only registers we can access are:
506 * SBI_SKCR
507 * SBI_SMCR
508 * SBI_SKID
510 static void sa1111_wake(struct sa1111 *sachip)
512 unsigned long flags, r;
514 spin_lock_irqsave(&sachip->lock, flags);
516 clk_enable(sachip->clk);
519 * Turn VCO on, and disable PLL Bypass.
521 r = sa1111_readl(sachip->base + SA1111_SKCR);
522 r &= ~SKCR_VCO_OFF;
523 sa1111_writel(r, sachip->base + SA1111_SKCR);
524 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
525 sa1111_writel(r, sachip->base + SA1111_SKCR);
528 * Wait lock time. SA1111 manual _doesn't_
529 * specify a figure for this! We choose 100us.
531 udelay(100);
534 * Enable RCLK. We also ensure that RDYEN is set.
536 r |= SKCR_RCLKEN | SKCR_RDYEN;
537 sa1111_writel(r, sachip->base + SA1111_SKCR);
540 * Wait 14 RCLK cycles for the chip to finish coming out
541 * of reset. (RCLK=24MHz). This is 590ns.
543 udelay(1);
546 * Ensure all clocks are initially off.
548 sa1111_writel(0, sachip->base + SA1111_SKPCR);
550 spin_unlock_irqrestore(&sachip->lock, flags);
553 #ifdef CONFIG_ARCH_SA1100
555 static u32 sa1111_dma_mask[] = {
557 ~(1 << 20),
558 ~(1 << 23),
559 ~(1 << 24),
560 ~(1 << 25),
561 ~(1 << 20),
562 ~(1 << 20),
567 * Configure the SA1111 shared memory controller.
569 void
570 sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
571 unsigned int cas_latency)
573 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
575 if (cas_latency == 3)
576 smcr |= SMCR_CLAT;
578 sa1111_writel(smcr, sachip->base + SA1111_SMCR);
580 if (sachip->dev->dma_mask)
581 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
583 sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
586 #endif
588 static void sa1111_dev_release(struct device *_dev)
590 struct sa1111_dev *dev = SA1111_DEV(_dev);
592 release_resource(&dev->res);
593 kfree(dev);
596 static int
597 sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
598 struct sa1111_dev_info *info)
600 struct sa1111_dev *dev;
601 int ret;
603 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
604 if (!dev) {
605 ret = -ENOMEM;
606 goto out;
609 dev_set_name(&dev->dev, "%4.4lx", info->offset);
610 dev->devid = info->devid;
611 dev->dev.parent = sachip->dev;
612 dev->dev.bus = &sa1111_bus_type;
613 dev->dev.release = sa1111_dev_release;
614 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
615 dev->res.start = sachip->phys + info->offset;
616 dev->res.end = dev->res.start + 511;
617 dev->res.name = dev_name(&dev->dev);
618 dev->res.flags = IORESOURCE_MEM;
619 dev->mapbase = sachip->base + info->offset;
620 dev->skpcr_mask = info->skpcr_mask;
621 memmove(dev->irq, info->irq, sizeof(dev->irq));
623 ret = request_resource(parent, &dev->res);
624 if (ret) {
625 printk("SA1111: failed to allocate resource for %s\n",
626 dev->res.name);
627 dev_set_name(&dev->dev, NULL);
628 kfree(dev);
629 goto out;
633 ret = device_register(&dev->dev);
634 if (ret) {
635 release_resource(&dev->res);
636 kfree(dev);
637 goto out;
640 #ifdef CONFIG_DMABOUNCE
642 * If the parent device has a DMA mask associated with it,
643 * propagate it down to the children.
645 if (sachip->dev->dma_mask) {
646 dev->dma_mask = *sachip->dev->dma_mask;
647 dev->dev.dma_mask = &dev->dma_mask;
649 if (dev->dma_mask != 0xffffffffUL) {
650 ret = dmabounce_register_dev(&dev->dev, 1024, 4096);
651 if (ret) {
652 dev_err(&dev->dev, "SA1111: Failed to register"
653 " with dmabounce\n");
654 device_unregister(&dev->dev);
658 #endif
660 out:
661 return ret;
665 * sa1111_probe - probe for a single SA1111 chip.
666 * @phys_addr: physical address of device.
668 * Probe for a SA1111 chip. This must be called
669 * before any other SA1111-specific code.
671 * Returns:
672 * %-ENODEV device not found.
673 * %-EBUSY physical address already marked in-use.
674 * %0 successful.
676 static int
677 __sa1111_probe(struct device *me, struct resource *mem, int irq)
679 struct sa1111 *sachip;
680 unsigned long id;
681 unsigned int has_devs;
682 int i, ret = -ENODEV;
684 sachip = kzalloc(sizeof(struct sa1111), GFP_KERNEL);
685 if (!sachip)
686 return -ENOMEM;
688 sachip->clk = clk_get(me, "SA1111_CLK");
689 if (IS_ERR(sachip->clk)) {
690 ret = PTR_ERR(sachip->clk);
691 goto err_free;
694 spin_lock_init(&sachip->lock);
696 sachip->dev = me;
697 dev_set_drvdata(sachip->dev, sachip);
699 sachip->phys = mem->start;
700 sachip->irq = irq;
703 * Map the whole region. This also maps the
704 * registers for our children.
706 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
707 if (!sachip->base) {
708 ret = -ENOMEM;
709 goto err_clkput;
713 * Probe for the chip. Only touch the SBI registers.
715 id = sa1111_readl(sachip->base + SA1111_SKID);
716 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
717 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
718 ret = -ENODEV;
719 goto err_unmap;
722 printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
723 "silicon revision %lx, metal revision %lx\n",
724 (id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));
727 * We found it. Wake the chip up, and initialise.
729 sa1111_wake(sachip);
731 #ifdef CONFIG_ARCH_SA1100
733 unsigned int val;
736 * The SDRAM configuration of the SA1110 and the SA1111 must
737 * match. This is very important to ensure that SA1111 accesses
738 * don't corrupt the SDRAM. Note that this ungates the SA1111's
739 * MBGNT signal, so we must have called sa1110_mb_disable()
740 * beforehand.
742 sa1111_configure_smc(sachip, 1,
743 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
744 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
747 * We only need to turn on DCLK whenever we want to use the
748 * DMA. It can otherwise be held firmly in the off position.
749 * (currently, we always enable it.)
751 val = sa1111_readl(sachip->base + SA1111_SKPCR);
752 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
755 * Enable the SA1110 memory bus request and grant signals.
757 sa1110_mb_enable();
759 #endif
762 * The interrupt controller must be initialised before any
763 * other device to ensure that the interrupts are available.
765 if (sachip->irq != NO_IRQ)
766 sa1111_setup_irq(sachip);
768 g_sa1111 = sachip;
770 has_devs = ~0;
771 if (machine_is_assabet() || machine_is_jornada720() ||
772 machine_is_badge4())
773 has_devs &= ~(1 << 4);
774 else
775 has_devs &= ~(1 << 1);
777 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
778 if (has_devs & (1 << i))
779 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
781 return 0;
783 err_unmap:
784 iounmap(sachip->base);
785 err_clkput:
786 clk_put(sachip->clk);
787 err_free:
788 kfree(sachip);
789 return ret;
792 static int sa1111_remove_one(struct device *dev, void *data)
794 device_unregister(dev);
795 return 0;
798 static void __sa1111_remove(struct sa1111 *sachip)
800 void __iomem *irqbase = sachip->base + SA1111_INTC;
802 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
804 /* disable all IRQs */
805 sa1111_writel(0, irqbase + SA1111_INTEN0);
806 sa1111_writel(0, irqbase + SA1111_INTEN1);
807 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
808 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
810 clk_disable(sachip->clk);
812 if (sachip->irq != NO_IRQ) {
813 set_irq_chained_handler(sachip->irq, NULL);
814 set_irq_data(sachip->irq, NULL);
816 release_mem_region(sachip->phys + SA1111_INTC, 512);
819 iounmap(sachip->base);
820 clk_put(sachip->clk);
821 kfree(sachip);
825 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
826 * Chip Specification Update" (June 2000), erratum #7, there is a
827 * significant bug in the SA1111 SDRAM shared memory controller. If
828 * an access to a region of memory above 1MB relative to the bank base,
829 * it is important that address bit 10 _NOT_ be asserted. Depending
830 * on the configuration of the RAM, bit 10 may correspond to one
831 * of several different (processor-relative) address bits.
833 * This routine only identifies whether or not a given DMA address
834 * is susceptible to the bug.
836 * This should only get called for sa1111_device types due to the
837 * way we configure our device dma_masks.
839 int dma_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
842 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
843 * User's Guide" mentions that jumpers R51 and R52 control the
844 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
845 * SDRAM bank 1 on Neponset). The default configuration selects
846 * Assabet, so any address in bank 1 is necessarily invalid.
848 return ((machine_is_assabet() || machine_is_pfs168()) &&
849 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000));
852 struct sa1111_save_data {
853 unsigned int skcr;
854 unsigned int skpcr;
855 unsigned int skcdr;
856 unsigned char skaud;
857 unsigned char skpwm0;
858 unsigned char skpwm1;
861 * Interrupt controller
863 unsigned int intpol0;
864 unsigned int intpol1;
865 unsigned int inten0;
866 unsigned int inten1;
867 unsigned int wakepol0;
868 unsigned int wakepol1;
869 unsigned int wakeen0;
870 unsigned int wakeen1;
873 #ifdef CONFIG_PM
875 static int sa1111_suspend(struct platform_device *dev, pm_message_t state)
877 struct sa1111 *sachip = platform_get_drvdata(dev);
878 struct sa1111_save_data *save;
879 unsigned long flags;
880 unsigned int val;
881 void __iomem *base;
883 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
884 if (!save)
885 return -ENOMEM;
886 sachip->saved_state = save;
888 spin_lock_irqsave(&sachip->lock, flags);
891 * Save state.
893 base = sachip->base;
894 save->skcr = sa1111_readl(base + SA1111_SKCR);
895 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
896 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
897 save->skaud = sa1111_readl(base + SA1111_SKAUD);
898 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
899 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
901 base = sachip->base + SA1111_INTC;
902 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
903 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
904 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
905 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
906 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
907 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
908 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
909 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
912 * Disable.
914 val = sa1111_readl(sachip->base + SA1111_SKCR);
915 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
916 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
917 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
919 clk_disable(sachip->clk);
921 spin_unlock_irqrestore(&sachip->lock, flags);
923 return 0;
927 * sa1111_resume - Restore the SA1111 device state.
928 * @dev: device to restore
930 * Restore the general state of the SA1111; clock control and
931 * interrupt controller. Other parts of the SA1111 must be
932 * restored by their respective drivers, and must be called
933 * via LDM after this function.
935 static int sa1111_resume(struct platform_device *dev)
937 struct sa1111 *sachip = platform_get_drvdata(dev);
938 struct sa1111_save_data *save;
939 unsigned long flags, id;
940 void __iomem *base;
942 save = sachip->saved_state;
943 if (!save)
944 return 0;
946 id = sa1111_readl(sachip->base + SA1111_SKID);
947 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
948 __sa1111_remove(sachip);
949 platform_set_drvdata(dev, NULL);
950 kfree(save);
951 return 0;
955 * First of all, wake up the chip.
957 sa1111_wake(sachip);
960 * Only lock for write ops. Also, sa1111_wake must be called with
961 * released spinlock!
963 spin_lock_irqsave(&sachip->lock, flags);
965 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
966 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
968 base = sachip->base;
969 sa1111_writel(save->skcr, base + SA1111_SKCR);
970 sa1111_writel(save->skpcr, base + SA1111_SKPCR);
971 sa1111_writel(save->skcdr, base + SA1111_SKCDR);
972 sa1111_writel(save->skaud, base + SA1111_SKAUD);
973 sa1111_writel(save->skpwm0, base + SA1111_SKPWM0);
974 sa1111_writel(save->skpwm1, base + SA1111_SKPWM1);
976 base = sachip->base + SA1111_INTC;
977 sa1111_writel(save->intpol0, base + SA1111_INTPOL0);
978 sa1111_writel(save->intpol1, base + SA1111_INTPOL1);
979 sa1111_writel(save->inten0, base + SA1111_INTEN0);
980 sa1111_writel(save->inten1, base + SA1111_INTEN1);
981 sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
982 sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
983 sa1111_writel(save->wakeen0, base + SA1111_WAKEEN0);
984 sa1111_writel(save->wakeen1, base + SA1111_WAKEEN1);
986 spin_unlock_irqrestore(&sachip->lock, flags);
988 sachip->saved_state = NULL;
989 kfree(save);
991 return 0;
994 #else
995 #define sa1111_suspend NULL
996 #define sa1111_resume NULL
997 #endif
999 static int __devinit sa1111_probe(struct platform_device *pdev)
1001 struct resource *mem;
1002 int irq;
1004 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1005 if (!mem)
1006 return -EINVAL;
1007 irq = platform_get_irq(pdev, 0);
1008 if (irq < 0)
1009 return -ENXIO;
1011 return __sa1111_probe(&pdev->dev, mem, irq);
1014 static int sa1111_remove(struct platform_device *pdev)
1016 struct sa1111 *sachip = platform_get_drvdata(pdev);
1018 if (sachip) {
1019 #ifdef CONFIG_PM
1020 kfree(sachip->saved_state);
1021 sachip->saved_state = NULL;
1022 #endif
1023 __sa1111_remove(sachip);
1024 platform_set_drvdata(pdev, NULL);
1027 return 0;
1031 * Not sure if this should be on the system bus or not yet.
1032 * We really want some way to register a system device at
1033 * the per-machine level, and then have this driver pick
1034 * up the registered devices.
1036 * We also need to handle the SDRAM configuration for
1037 * PXA250/SA1110 machine classes.
1039 static struct platform_driver sa1111_device_driver = {
1040 .probe = sa1111_probe,
1041 .remove = sa1111_remove,
1042 .suspend = sa1111_suspend,
1043 .resume = sa1111_resume,
1044 .driver = {
1045 .name = "sa1111",
1050 * Get the parent device driver (us) structure
1051 * from a child function device
1053 static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
1055 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
1059 * The bits in the opdiv field are non-linear.
1061 static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
1063 static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1065 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1067 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
1069 fbdiv = (skcdr & 0x007f) + 2;
1070 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1071 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1073 return 3686400 * fbdiv / (ipdiv * opdiv);
1077 * sa1111_pll_clock - return the current PLL clock frequency.
1078 * @sadev: SA1111 function block
1080 * BUG: we should look at SKCR. We also blindly believe that
1081 * the chip is being fed with the 3.6864MHz clock.
1083 * Returns the PLL clock in Hz.
1085 unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1087 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1089 return __sa1111_pll_clock(sachip);
1091 EXPORT_SYMBOL(sa1111_pll_clock);
1094 * sa1111_select_audio_mode - select I2S or AC link mode
1095 * @sadev: SA1111 function block
1096 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1098 * Frob the SKCR to select AC Link mode or I2S mode for
1099 * the audio block.
1101 void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1103 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1104 unsigned long flags;
1105 unsigned int val;
1107 spin_lock_irqsave(&sachip->lock, flags);
1109 val = sa1111_readl(sachip->base + SA1111_SKCR);
1110 if (mode == SA1111_AUDIO_I2S) {
1111 val &= ~SKCR_SELAC;
1112 } else {
1113 val |= SKCR_SELAC;
1115 sa1111_writel(val, sachip->base + SA1111_SKCR);
1117 spin_unlock_irqrestore(&sachip->lock, flags);
1119 EXPORT_SYMBOL(sa1111_select_audio_mode);
1122 * sa1111_set_audio_rate - set the audio sample rate
1123 * @sadev: SA1111 SAC function block
1124 * @rate: sample rate to select
1126 int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1128 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1129 unsigned int div;
1131 if (sadev->devid != SA1111_DEVID_SAC)
1132 return -EINVAL;
1134 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1135 if (div == 0)
1136 div = 1;
1137 if (div > 128)
1138 div = 128;
1140 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1142 return 0;
1144 EXPORT_SYMBOL(sa1111_set_audio_rate);
1147 * sa1111_get_audio_rate - get the audio sample rate
1148 * @sadev: SA1111 SAC function block device
1150 int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1152 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1153 unsigned long div;
1155 if (sadev->devid != SA1111_DEVID_SAC)
1156 return -EINVAL;
1158 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1160 return __sa1111_pll_clock(sachip) / (256 * div);
1162 EXPORT_SYMBOL(sa1111_get_audio_rate);
1164 void sa1111_set_io_dir(struct sa1111_dev *sadev,
1165 unsigned int bits, unsigned int dir,
1166 unsigned int sleep_dir)
1168 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1169 unsigned long flags;
1170 unsigned int val;
1171 void __iomem *gpio = sachip->base + SA1111_GPIO;
1173 #define MODIFY_BITS(port, mask, dir) \
1174 if (mask) { \
1175 val = sa1111_readl(port); \
1176 val &= ~(mask); \
1177 val |= (dir) & (mask); \
1178 sa1111_writel(val, port); \
1181 spin_lock_irqsave(&sachip->lock, flags);
1182 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1183 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1184 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1186 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1187 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1188 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1189 spin_unlock_irqrestore(&sachip->lock, flags);
1191 EXPORT_SYMBOL(sa1111_set_io_dir);
1193 void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1195 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1196 unsigned long flags;
1197 unsigned int val;
1198 void __iomem *gpio = sachip->base + SA1111_GPIO;
1200 spin_lock_irqsave(&sachip->lock, flags);
1201 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1202 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1203 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1204 spin_unlock_irqrestore(&sachip->lock, flags);
1206 EXPORT_SYMBOL(sa1111_set_io);
1208 void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1210 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1211 unsigned long flags;
1212 unsigned int val;
1213 void __iomem *gpio = sachip->base + SA1111_GPIO;
1215 spin_lock_irqsave(&sachip->lock, flags);
1216 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1217 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1218 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1219 spin_unlock_irqrestore(&sachip->lock, flags);
1221 EXPORT_SYMBOL(sa1111_set_sleep_io);
1224 * Individual device operations.
1228 * sa1111_enable_device - enable an on-chip SA1111 function block
1229 * @sadev: SA1111 function block device to enable
1231 void sa1111_enable_device(struct sa1111_dev *sadev)
1233 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1234 unsigned long flags;
1235 unsigned int val;
1237 spin_lock_irqsave(&sachip->lock, flags);
1238 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1239 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1240 spin_unlock_irqrestore(&sachip->lock, flags);
1242 EXPORT_SYMBOL(sa1111_enable_device);
1245 * sa1111_disable_device - disable an on-chip SA1111 function block
1246 * @sadev: SA1111 function block device to disable
1248 void sa1111_disable_device(struct sa1111_dev *sadev)
1250 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1251 unsigned long flags;
1252 unsigned int val;
1254 spin_lock_irqsave(&sachip->lock, flags);
1255 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1256 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1257 spin_unlock_irqrestore(&sachip->lock, flags);
1259 EXPORT_SYMBOL(sa1111_disable_device);
1262 * SA1111 "Register Access Bus."
1264 * We model this as a regular bus type, and hang devices directly
1265 * off this.
1267 static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1269 struct sa1111_dev *dev = SA1111_DEV(_dev);
1270 struct sa1111_driver *drv = SA1111_DRV(_drv);
1272 return dev->devid == drv->devid;
1275 static int sa1111_bus_suspend(struct device *dev, pm_message_t state)
1277 struct sa1111_dev *sadev = SA1111_DEV(dev);
1278 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1279 int ret = 0;
1281 if (drv && drv->suspend)
1282 ret = drv->suspend(sadev, state);
1283 return ret;
1286 static int sa1111_bus_resume(struct device *dev)
1288 struct sa1111_dev *sadev = SA1111_DEV(dev);
1289 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1290 int ret = 0;
1292 if (drv && drv->resume)
1293 ret = drv->resume(sadev);
1294 return ret;
1297 static int sa1111_bus_probe(struct device *dev)
1299 struct sa1111_dev *sadev = SA1111_DEV(dev);
1300 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1301 int ret = -ENODEV;
1303 if (drv->probe)
1304 ret = drv->probe(sadev);
1305 return ret;
1308 static int sa1111_bus_remove(struct device *dev)
1310 struct sa1111_dev *sadev = SA1111_DEV(dev);
1311 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1312 int ret = 0;
1314 if (drv->remove)
1315 ret = drv->remove(sadev);
1316 return ret;
1319 struct bus_type sa1111_bus_type = {
1320 .name = "sa1111-rab",
1321 .match = sa1111_match,
1322 .probe = sa1111_bus_probe,
1323 .remove = sa1111_bus_remove,
1324 .suspend = sa1111_bus_suspend,
1325 .resume = sa1111_bus_resume,
1327 EXPORT_SYMBOL(sa1111_bus_type);
1329 int sa1111_driver_register(struct sa1111_driver *driver)
1331 driver->drv.bus = &sa1111_bus_type;
1332 return driver_register(&driver->drv);
1334 EXPORT_SYMBOL(sa1111_driver_register);
1336 void sa1111_driver_unregister(struct sa1111_driver *driver)
1338 driver_unregister(&driver->drv);
1340 EXPORT_SYMBOL(sa1111_driver_unregister);
1342 static int __init sa1111_init(void)
1344 int ret = bus_register(&sa1111_bus_type);
1345 if (ret == 0)
1346 platform_driver_register(&sa1111_device_driver);
1347 return ret;
1350 static void __exit sa1111_exit(void)
1352 platform_driver_unregister(&sa1111_device_driver);
1353 bus_unregister(&sa1111_bus_type);
1356 subsys_initcall(sa1111_init);
1357 module_exit(sa1111_exit);
1359 MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1360 MODULE_LICENSE("GPL");