MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / arm / common / sa1111.c
blob102bc86ba140b76a869c90c157567fccafb6f780
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/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>
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>
37 #include <asm/hardware/sa1111.h>
39 extern void __init sa1110_mb_enable(void);
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.
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;
58 * We _really_ need to eliminate this. Its only users
59 * are the PWM and DMA checking code.
61 static struct sa1111 *g_sa1111;
63 struct sa1111_dev_info {
64 unsigned long offset;
65 unsigned long skpcr_mask;
66 unsigned int devid;
67 unsigned int irq[6];
70 static struct sa1111_dev_info sa1111_devices[] = {
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
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
96 .offset = 0x0800,
97 .skpcr_mask = SKPCR_SCLKEN,
98 .devid = SA1111_DEVID_SSP,
101 .offset = SA1111_KBD,
102 .skpcr_mask = SKPCR_PTCLKEN,
103 .devid = SA1111_DEVID_PS2,
104 .irq = {
105 IRQ_TPRXINT,
106 IRQ_TPTXINT
110 .offset = SA1111_MSE,
111 .skpcr_mask = SKPCR_PMCLKEN,
112 .devid = SA1111_DEVID_PS2,
113 .irq = {
114 IRQ_MSRXINT,
115 IRQ_MSTXINT
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,
133 void __init sa1111_adjust_zones(int node, unsigned long *size, unsigned long *holes)
135 unsigned int sz = SZ_1M >> PAGE_SHIFT;
137 if (node != 0)
138 sz = 0;
140 size[1] = size[0] - sz;
141 size[0] = sz;
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.
149 static void
150 sa1111_irq_handler(unsigned int irq, struct irqdesc *desc)
152 unsigned int stat0, stat1, i;
153 void __iomem *base = get_irq_data(irq);
155 stat0 = sa1111_readl(base + SA1111_INTSTATCLR0);
156 stat1 = sa1111_readl(base + SA1111_INTSTATCLR1);
158 sa1111_writel(stat0, base + SA1111_INTSTATCLR0);
160 desc->chip->ack(irq);
162 sa1111_writel(stat1, base + SA1111_INTSTATCLR1);
164 if (stat0 == 0 && stat1 == 0) {
165 do_bad_IRQ(irq, desc);
166 return;
169 for (i = IRQ_SA1111_START; stat0; i++, stat0 >>= 1)
170 if (stat0 & 1)
171 handle_edge_irq(i, irq_desc + i);
173 for (i = IRQ_SA1111_START + 32; stat1; i++, stat1 >>= 1)
174 if (stat1 & 1)
175 handle_edge_irq(i, irq_desc + i);
177 /* For level-based interrupts */
178 desc->chip->unmask(irq);
181 #define SA1111_IRQMASK_LO(x) (1 << (x - IRQ_SA1111_START))
182 #define SA1111_IRQMASK_HI(x) (1 << (x - IRQ_SA1111_START - 32))
184 static void sa1111_ack_irq(unsigned int irq)
188 static void sa1111_mask_lowirq(unsigned int irq)
190 void __iomem *mapbase = get_irq_chipdata(irq);
191 unsigned long ie0;
193 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
194 ie0 &= ~SA1111_IRQMASK_LO(irq);
195 writel(ie0, mapbase + SA1111_INTEN0);
198 static void sa1111_unmask_lowirq(unsigned int irq)
200 void __iomem *mapbase = get_irq_chipdata(irq);
201 unsigned long ie0;
203 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
204 ie0 |= SA1111_IRQMASK_LO(irq);
205 sa1111_writel(ie0, mapbase + SA1111_INTEN0);
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.
215 static int sa1111_retrigger_lowirq(unsigned int irq)
217 unsigned int mask = SA1111_IRQMASK_LO(irq);
218 void __iomem *mapbase = get_irq_chipdata(irq);
219 unsigned long ip0;
220 int i;
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;
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;
236 static int sa1111_type_lowirq(unsigned int irq, unsigned int flags)
238 unsigned int mask = SA1111_IRQMASK_LO(irq);
239 void __iomem *mapbase = get_irq_chipdata(irq);
240 unsigned long ip0;
242 if (flags == IRQT_PROBE)
243 return 0;
245 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
246 return -EINVAL;
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);
256 return 0;
259 static int sa1111_wake_lowirq(unsigned int irq, unsigned int on)
261 unsigned int mask = SA1111_IRQMASK_LO(irq);
262 void __iomem *mapbase = get_irq_chipdata(irq);
263 unsigned long we0;
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);
272 return 0;
275 static struct irq_chip sa1111_low_chip = {
276 .name = "SA1111-l",
277 .ack = sa1111_ack_irq,
278 .mask = sa1111_mask_lowirq,
279 .unmask = sa1111_unmask_lowirq,
280 .retrigger = sa1111_retrigger_lowirq,
281 .set_type = sa1111_type_lowirq,
282 .set_wake = sa1111_wake_lowirq,
285 static void sa1111_mask_highirq(unsigned int irq)
287 void __iomem *mapbase = get_irq_chipdata(irq);
288 unsigned long ie1;
290 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
291 ie1 &= ~SA1111_IRQMASK_HI(irq);
292 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
295 static void sa1111_unmask_highirq(unsigned int irq)
297 void __iomem *mapbase = get_irq_chipdata(irq);
298 unsigned long ie1;
300 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
301 ie1 |= SA1111_IRQMASK_HI(irq);
302 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
306 * Attempt to re-trigger the interrupt. The SA1111 contains a register
307 * (INTSET) which claims to do this. However, in practice no amount of
308 * manipulation of INTEN and INTSET guarantees that the interrupt will
309 * be triggered. In fact, its very difficult, if not impossible to get
310 * INTSET to re-trigger the interrupt.
312 static int sa1111_retrigger_highirq(unsigned int irq)
314 unsigned int mask = SA1111_IRQMASK_HI(irq);
315 void __iomem *mapbase = get_irq_chipdata(irq);
316 unsigned long ip1;
317 int i;
319 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
320 for (i = 0; i < 8; i++) {
321 sa1111_writel(ip1 ^ mask, mapbase + SA1111_INTPOL1);
322 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
323 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
324 break;
327 if (i == 8)
328 printk(KERN_ERR "Danger Will Robinson: failed to "
329 "re-trigger IRQ%d\n", irq);
330 return i == 8 ? -1 : 0;
333 static int sa1111_type_highirq(unsigned int irq, unsigned int flags)
335 unsigned int mask = SA1111_IRQMASK_HI(irq);
336 void __iomem *mapbase = get_irq_chipdata(irq);
337 unsigned long ip1;
339 if (flags == IRQT_PROBE)
340 return 0;
342 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
343 return -EINVAL;
345 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
346 if (flags & __IRQT_RISEDGE)
347 ip1 &= ~mask;
348 else
349 ip1 |= mask;
350 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
351 sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);
353 return 0;
356 static int sa1111_wake_highirq(unsigned int irq, unsigned int on)
358 unsigned int mask = SA1111_IRQMASK_HI(irq);
359 void __iomem *mapbase = get_irq_chipdata(irq);
360 unsigned long we1;
362 we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
363 if (on)
364 we1 |= mask;
365 else
366 we1 &= ~mask;
367 sa1111_writel(we1, mapbase + SA1111_WAKEEN1);
369 return 0;
372 static struct irq_chip sa1111_high_chip = {
373 .name = "SA1111-h",
374 .ack = sa1111_ack_irq,
375 .mask = sa1111_mask_highirq,
376 .unmask = sa1111_unmask_highirq,
377 .retrigger = sa1111_retrigger_highirq,
378 .set_type = sa1111_type_highirq,
379 .set_wake = sa1111_wake_highirq,
382 static void sa1111_setup_irq(struct sa1111 *sachip)
384 void __iomem *irqbase = sachip->base + SA1111_INTC;
385 unsigned int irq;
388 * We're guaranteed that this region hasn't been taken.
390 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
392 /* disable all IRQs */
393 sa1111_writel(0, irqbase + SA1111_INTEN0);
394 sa1111_writel(0, irqbase + SA1111_INTEN1);
395 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
396 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
399 * detect on rising edge. Note: Feb 2001 Errata for SA1111
400 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
402 sa1111_writel(0, irqbase + SA1111_INTPOL0);
403 sa1111_writel(SA1111_IRQMASK_HI(IRQ_S0_READY_NINT) |
404 SA1111_IRQMASK_HI(IRQ_S1_READY_NINT),
405 irqbase + SA1111_INTPOL1);
407 /* clear all IRQs */
408 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR0);
409 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR1);
411 for (irq = IRQ_GPAIN0; irq <= SSPROR; irq++) {
412 set_irq_chip(irq, &sa1111_low_chip);
413 set_irq_chipdata(irq, irqbase);
414 set_irq_handler(irq, do_edge_IRQ);
415 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
418 for (irq = AUDXMTDMADONEA; irq <= IRQ_S1_BVD1_STSCHG; irq++) {
419 set_irq_chip(irq, &sa1111_high_chip);
420 set_irq_chipdata(irq, irqbase);
421 set_irq_handler(irq, do_edge_IRQ);
422 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
426 * Register SA1111 interrupt
428 set_irq_type(sachip->irq, IRQT_RISING);
429 set_irq_data(sachip->irq, irqbase);
430 set_irq_chained_handler(sachip->irq, sa1111_irq_handler);
434 * Bring the SA1111 out of reset. This requires a set procedure:
435 * 1. nRESET asserted (by hardware)
436 * 2. CLK turned on from SA1110
437 * 3. nRESET deasserted
438 * 4. VCO turned on, PLL_BYPASS turned off
439 * 5. Wait lock time, then assert RCLKEn
440 * 7. PCR set to allow clocking of individual functions
442 * Until we've done this, the only registers we can access are:
443 * SBI_SKCR
444 * SBI_SMCR
445 * SBI_SKID
447 static void sa1111_wake(struct sa1111 *sachip)
449 unsigned long flags, r;
451 spin_lock_irqsave(&sachip->lock, flags);
453 clk_enable(sachip->clk);
456 * Turn VCO on, and disable PLL Bypass.
458 r = sa1111_readl(sachip->base + SA1111_SKCR);
459 r &= ~SKCR_VCO_OFF;
460 sa1111_writel(r, sachip->base + SA1111_SKCR);
461 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
462 sa1111_writel(r, sachip->base + SA1111_SKCR);
465 * Wait lock time. SA1111 manual _doesn't_
466 * specify a figure for this! We choose 100us.
468 udelay(100);
471 * Enable RCLK. We also ensure that RDYEN is set.
473 r |= SKCR_RCLKEN | SKCR_RDYEN;
474 sa1111_writel(r, sachip->base + SA1111_SKCR);
477 * Wait 14 RCLK cycles for the chip to finish coming out
478 * of reset. (RCLK=24MHz). This is 590ns.
480 udelay(1);
483 * Ensure all clocks are initially off.
485 sa1111_writel(0, sachip->base + SA1111_SKPCR);
487 spin_unlock_irqrestore(&sachip->lock, flags);
490 #ifdef CONFIG_ARCH_SA1100
492 static u32 sa1111_dma_mask[] = {
494 ~(1 << 20),
495 ~(1 << 23),
496 ~(1 << 24),
497 ~(1 << 25),
498 ~(1 << 20),
499 ~(1 << 20),
504 * Configure the SA1111 shared memory controller.
506 void
507 sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
508 unsigned int cas_latency)
510 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
512 if (cas_latency == 3)
513 smcr |= SMCR_CLAT;
515 sa1111_writel(smcr, sachip->base + SA1111_SMCR);
518 * Now clear the bits in the DMA mask to work around the SA1111
519 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
520 * Chip Specification Update, June 2000, Erratum #7).
522 if (sachip->dev->dma_mask)
523 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
525 sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
528 #endif
530 static void sa1111_dev_release(struct device *_dev)
532 struct sa1111_dev *dev = SA1111_DEV(_dev);
534 release_resource(&dev->res);
535 kfree(dev);
538 static int
539 sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
540 struct sa1111_dev_info *info)
542 struct sa1111_dev *dev;
543 int ret;
545 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
546 if (!dev) {
547 ret = -ENOMEM;
548 goto out;
551 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
552 "%4.4lx", info->offset);
554 dev->devid = info->devid;
555 dev->dev.parent = sachip->dev;
556 dev->dev.bus = &sa1111_bus_type;
557 dev->dev.release = sa1111_dev_release;
558 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
559 dev->res.start = sachip->phys + info->offset;
560 dev->res.end = dev->res.start + 511;
561 dev->res.name = dev->dev.bus_id;
562 dev->res.flags = IORESOURCE_MEM;
563 dev->mapbase = sachip->base + info->offset;
564 dev->skpcr_mask = info->skpcr_mask;
565 memmove(dev->irq, info->irq, sizeof(dev->irq));
568 * If the parent device has a DMA mask associated with it,
569 * propagate it down to the children.
571 if (sachip->dev->dma_mask) {
572 dev->dma_mask = *sachip->dev->dma_mask;
573 dev->dev.dma_mask = &dev->dma_mask;
575 if (dev->dma_mask != 0xffffffffUL) {
576 ret = dmabounce_register_dev(&dev->dev, 1024, 4096);
577 if (ret) {
578 printk("SA1111: Failed to register %s with dmabounce", dev->dev.bus_id);
583 ret = request_resource(parent, &dev->res);
584 if (ret) {
585 printk("SA1111: failed to allocate resource for %s\n",
586 dev->res.name);
587 kfree(dev);
588 goto out;
592 ret = device_register(&dev->dev);
593 if (ret) {
594 release_resource(&dev->res);
595 kfree(dev);
596 goto out;
600 * If the parent device has a DMA mask associated with it,
601 * propagate it down to the children.
603 if (sachip->dev->dma_mask) {
604 dev->dma_mask = *sachip->dev->dma_mask;
605 dev->dev.dma_mask = &dev->dma_mask;
607 if (dev->dma_mask != 0xffffffffUL) {
608 ret = dmabounce_register_dev(&dev->dev, 1024, 4096);
609 if (ret) {
610 printk("SA1111: Failed to register %s with dmabounce", dev->dev.bus_id);
611 device_unregister(&dev->dev);
616 out:
617 return ret;
621 * sa1111_probe - probe for a single SA1111 chip.
622 * @phys_addr: physical address of device.
624 * Probe for a SA1111 chip. This must be called
625 * before any other SA1111-specific code.
627 * Returns:
628 * %-ENODEV device not found.
629 * %-EBUSY physical address already marked in-use.
630 * %0 successful.
632 static int
633 __sa1111_probe(struct device *me, struct resource *mem, int irq)
635 struct sa1111 *sachip;
636 unsigned long id;
637 unsigned int has_devs;
638 int i, ret = -ENODEV;
640 sachip = kzalloc(sizeof(struct sa1111), GFP_KERNEL);
641 if (!sachip)
642 return -ENOMEM;
644 sachip->clk = clk_get(me, "GPIO27_CLK");
645 if (!sachip->clk) {
646 ret = PTR_ERR(sachip->clk);
647 goto err_free;
650 spin_lock_init(&sachip->lock);
652 sachip->dev = me;
653 dev_set_drvdata(sachip->dev, sachip);
655 sachip->phys = mem->start;
656 sachip->irq = irq;
659 * Map the whole region. This also maps the
660 * registers for our children.
662 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
663 if (!sachip->base) {
664 ret = -ENOMEM;
665 goto err_clkput;
669 * Probe for the chip. Only touch the SBI registers.
671 id = sa1111_readl(sachip->base + SA1111_SKID);
672 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
673 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
674 ret = -ENODEV;
675 goto err_unmap;
678 printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
679 "silicon revision %lx, metal revision %lx\n",
680 (id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));
683 * We found it. Wake the chip up, and initialise.
685 sa1111_wake(sachip);
687 #ifdef CONFIG_ARCH_SA1100
689 unsigned int val;
692 * The SDRAM configuration of the SA1110 and the SA1111 must
693 * match. This is very important to ensure that SA1111 accesses
694 * don't corrupt the SDRAM. Note that this ungates the SA1111's
695 * MBGNT signal, so we must have called sa1110_mb_disable()
696 * beforehand.
698 sa1111_configure_smc(sachip, 1,
699 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
700 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
703 * We only need to turn on DCLK whenever we want to use the
704 * DMA. It can otherwise be held firmly in the off position.
705 * (currently, we always enable it.)
707 val = sa1111_readl(sachip->base + SA1111_SKPCR);
708 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
711 * Enable the SA1110 memory bus request and grant signals.
713 sa1110_mb_enable();
715 #endif
718 * The interrupt controller must be initialised before any
719 * other device to ensure that the interrupts are available.
721 if (sachip->irq != NO_IRQ)
722 sa1111_setup_irq(sachip);
724 g_sa1111 = sachip;
726 has_devs = ~0;
727 if (machine_is_assabet() || machine_is_jornada720() ||
728 machine_is_badge4())
729 has_devs &= ~(1 << 4);
730 else
731 has_devs &= ~(1 << 1);
733 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
734 if (has_devs & (1 << i))
735 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
737 return 0;
739 err_unmap:
740 iounmap(sachip->base);
741 err_clkput:
742 clk_put(sachip->clk);
743 err_free:
744 kfree(sachip);
745 return ret;
748 static int sa1111_remove_one(struct device *dev, void *data)
750 device_unregister(dev);
751 return 0;
754 static void __sa1111_remove(struct sa1111 *sachip)
756 void __iomem *irqbase = sachip->base + SA1111_INTC;
758 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
760 /* disable all IRQs */
761 sa1111_writel(0, irqbase + SA1111_INTEN0);
762 sa1111_writel(0, irqbase + SA1111_INTEN1);
763 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
764 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
766 clk_disable(sachip->clk);
768 if (sachip->irq != NO_IRQ) {
769 set_irq_chained_handler(sachip->irq, NULL);
770 set_irq_data(sachip->irq, NULL);
772 release_mem_region(sachip->phys + SA1111_INTC, 512);
775 iounmap(sachip->base);
776 clk_put(sachip->clk);
777 kfree(sachip);
781 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
782 * Chip Specification Update" (June 2000), erratum #7, there is a
783 * significant bug in the SA1111 SDRAM shared memory controller. If
784 * an access to a region of memory above 1MB relative to the bank base,
785 * it is important that address bit 10 _NOT_ be asserted. Depending
786 * on the configuration of the RAM, bit 10 may correspond to one
787 * of several different (processor-relative) address bits.
789 * This routine only identifies whether or not a given DMA address
790 * is susceptible to the bug.
792 * This should only get called for sa1111_device types due to the
793 * way we configure our device dma_masks.
795 int dma_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
798 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
799 * User's Guide" mentions that jumpers R51 and R52 control the
800 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
801 * SDRAM bank 1 on Neponset). The default configuration selects
802 * Assabet, so any address in bank 1 is necessarily invalid.
804 return ((machine_is_assabet() || machine_is_pfs168()) &&
805 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000));
808 struct sa1111_save_data {
809 unsigned int skcr;
810 unsigned int skpcr;
811 unsigned int skcdr;
812 unsigned char skaud;
813 unsigned char skpwm0;
814 unsigned char skpwm1;
817 * Interrupt controller
819 unsigned int intpol0;
820 unsigned int intpol1;
821 unsigned int inten0;
822 unsigned int inten1;
823 unsigned int wakepol0;
824 unsigned int wakepol1;
825 unsigned int wakeen0;
826 unsigned int wakeen1;
829 #ifdef CONFIG_PM
831 static int sa1111_suspend(struct platform_device *dev, pm_message_t state)
833 struct sa1111 *sachip = platform_get_drvdata(dev);
834 struct sa1111_save_data *save;
835 unsigned long flags;
836 unsigned int val;
837 void __iomem *base;
839 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
840 if (!save)
841 return -ENOMEM;
842 dev->dev.power.saved_state = save;
844 spin_lock_irqsave(&sachip->lock, flags);
847 * Save state.
849 base = sachip->base;
850 save->skcr = sa1111_readl(base + SA1111_SKCR);
851 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
852 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
853 save->skaud = sa1111_readl(base + SA1111_SKAUD);
854 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
855 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
857 base = sachip->base + SA1111_INTC;
858 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
859 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
860 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
861 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
862 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
863 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
864 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
865 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
868 * Disable.
870 val = sa1111_readl(sachip->base + SA1111_SKCR);
871 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
872 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
873 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
875 clk_disable(sachip->clk);
877 spin_unlock_irqrestore(&sachip->lock, flags);
879 return 0;
883 * sa1111_resume - Restore the SA1111 device state.
884 * @dev: device to restore
886 * Restore the general state of the SA1111; clock control and
887 * interrupt controller. Other parts of the SA1111 must be
888 * restored by their respective drivers, and must be called
889 * via LDM after this function.
891 static int sa1111_resume(struct platform_device *dev)
893 struct sa1111 *sachip = platform_get_drvdata(dev);
894 struct sa1111_save_data *save;
895 unsigned long flags, id;
896 void __iomem *base;
898 save = (struct sa1111_save_data *)dev->dev.power.saved_state;
899 if (!save)
900 return 0;
902 spin_lock_irqsave(&sachip->lock, flags);
905 * Ensure that the SA1111 is still here.
906 * FIXME: shouldn't do this here.
908 id = sa1111_readl(sachip->base + SA1111_SKID);
909 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
910 __sa1111_remove(sachip);
911 platform_set_drvdata(dev, NULL);
912 kfree(save);
913 return 0;
917 * First of all, wake up the chip.
919 sa1111_wake(sachip);
920 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
921 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
923 base = sachip->base;
924 sa1111_writel(save->skcr, base + SA1111_SKCR);
925 sa1111_writel(save->skpcr, base + SA1111_SKPCR);
926 sa1111_writel(save->skcdr, base + SA1111_SKCDR);
927 sa1111_writel(save->skaud, base + SA1111_SKAUD);
928 sa1111_writel(save->skpwm0, base + SA1111_SKPWM0);
929 sa1111_writel(save->skpwm1, base + SA1111_SKPWM1);
931 base = sachip->base + SA1111_INTC;
932 sa1111_writel(save->intpol0, base + SA1111_INTPOL0);
933 sa1111_writel(save->intpol1, base + SA1111_INTPOL1);
934 sa1111_writel(save->inten0, base + SA1111_INTEN0);
935 sa1111_writel(save->inten1, base + SA1111_INTEN1);
936 sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
937 sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
938 sa1111_writel(save->wakeen0, base + SA1111_WAKEEN0);
939 sa1111_writel(save->wakeen1, base + SA1111_WAKEEN1);
941 spin_unlock_irqrestore(&sachip->lock, flags);
943 dev->dev.power.saved_state = NULL;
944 kfree(save);
946 return 0;
949 #else
950 #define sa1111_suspend NULL
951 #define sa1111_resume NULL
952 #endif
954 static int sa1111_probe(struct platform_device *pdev)
956 struct resource *mem;
957 int irq;
959 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
960 if (!mem)
961 return -EINVAL;
962 irq = platform_get_irq(pdev, 0);
963 if (irq < 0)
964 return -ENXIO;
966 return __sa1111_probe(&pdev->dev, mem, irq);
969 static int sa1111_remove(struct platform_device *pdev)
971 struct sa1111 *sachip = platform_get_drvdata(pdev);
973 if (sachip) {
974 __sa1111_remove(sachip);
975 platform_set_drvdata(pdev, NULL);
977 #ifdef CONFIG_PM
978 kfree(pdev->dev.power.saved_state);
979 pdev->dev.power.saved_state = NULL;
980 #endif
983 return 0;
987 * Not sure if this should be on the system bus or not yet.
988 * We really want some way to register a system device at
989 * the per-machine level, and then have this driver pick
990 * up the registered devices.
992 * We also need to handle the SDRAM configuration for
993 * PXA250/SA1110 machine classes.
995 static struct platform_driver sa1111_device_driver = {
996 .probe = sa1111_probe,
997 .remove = sa1111_remove,
998 .suspend = sa1111_suspend,
999 .resume = sa1111_resume,
1000 .driver = {
1001 .name = "sa1111",
1006 * Get the parent device driver (us) structure
1007 * from a child function device
1009 static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
1011 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
1015 * The bits in the opdiv field are non-linear.
1017 static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
1019 static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1021 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1023 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
1025 fbdiv = (skcdr & 0x007f) + 2;
1026 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1027 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1029 return 3686400 * fbdiv / (ipdiv * opdiv);
1033 * sa1111_pll_clock - return the current PLL clock frequency.
1034 * @sadev: SA1111 function block
1036 * BUG: we should look at SKCR. We also blindly believe that
1037 * the chip is being fed with the 3.6864MHz clock.
1039 * Returns the PLL clock in Hz.
1041 unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1043 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1045 return __sa1111_pll_clock(sachip);
1049 * sa1111_select_audio_mode - select I2S or AC link mode
1050 * @sadev: SA1111 function block
1051 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1053 * Frob the SKCR to select AC Link mode or I2S mode for
1054 * the audio block.
1056 void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1058 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1059 unsigned long flags;
1060 unsigned int val;
1062 spin_lock_irqsave(&sachip->lock, flags);
1064 val = sa1111_readl(sachip->base + SA1111_SKCR);
1065 if (mode == SA1111_AUDIO_I2S) {
1066 val &= ~SKCR_SELAC;
1067 } else {
1068 val |= SKCR_SELAC;
1070 sa1111_writel(val, sachip->base + SA1111_SKCR);
1072 spin_unlock_irqrestore(&sachip->lock, flags);
1076 * sa1111_set_audio_rate - set the audio sample rate
1077 * @sadev: SA1111 SAC function block
1078 * @rate: sample rate to select
1080 int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1082 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1083 unsigned int div;
1085 if (sadev->devid != SA1111_DEVID_SAC)
1086 return -EINVAL;
1088 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1089 if (div == 0)
1090 div = 1;
1091 if (div > 128)
1092 div = 128;
1094 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1096 return 0;
1100 * sa1111_get_audio_rate - get the audio sample rate
1101 * @sadev: SA1111 SAC function block device
1103 int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1105 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1106 unsigned long div;
1108 if (sadev->devid != SA1111_DEVID_SAC)
1109 return -EINVAL;
1111 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1113 return __sa1111_pll_clock(sachip) / (256 * div);
1116 void sa1111_set_io_dir(struct sa1111_dev *sadev,
1117 unsigned int bits, unsigned int dir,
1118 unsigned int sleep_dir)
1120 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1121 unsigned long flags;
1122 unsigned int val;
1123 void __iomem *gpio = sachip->base + SA1111_GPIO;
1125 #define MODIFY_BITS(port, mask, dir) \
1126 if (mask) { \
1127 val = sa1111_readl(port); \
1128 val &= ~(mask); \
1129 val |= (dir) & (mask); \
1130 sa1111_writel(val, port); \
1133 spin_lock_irqsave(&sachip->lock, flags);
1134 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1135 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1136 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1138 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1139 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1140 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1141 spin_unlock_irqrestore(&sachip->lock, flags);
1144 void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1146 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1147 unsigned long flags;
1148 unsigned int val;
1149 void __iomem *gpio = sachip->base + SA1111_GPIO;
1151 spin_lock_irqsave(&sachip->lock, flags);
1152 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1153 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1154 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1155 spin_unlock_irqrestore(&sachip->lock, flags);
1158 void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1160 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1161 unsigned long flags;
1162 unsigned int val;
1163 void __iomem *gpio = sachip->base + SA1111_GPIO;
1165 spin_lock_irqsave(&sachip->lock, flags);
1166 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1167 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1168 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1169 spin_unlock_irqrestore(&sachip->lock, flags);
1173 * Individual device operations.
1177 * sa1111_enable_device - enable an on-chip SA1111 function block
1178 * @sadev: SA1111 function block device to enable
1180 void sa1111_enable_device(struct sa1111_dev *sadev)
1182 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1183 unsigned long flags;
1184 unsigned int val;
1186 spin_lock_irqsave(&sachip->lock, flags);
1187 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1188 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1189 spin_unlock_irqrestore(&sachip->lock, flags);
1193 * sa1111_disable_device - disable an on-chip SA1111 function block
1194 * @sadev: SA1111 function block device to disable
1196 void sa1111_disable_device(struct sa1111_dev *sadev)
1198 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1199 unsigned long flags;
1200 unsigned int val;
1202 spin_lock_irqsave(&sachip->lock, flags);
1203 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1204 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1205 spin_unlock_irqrestore(&sachip->lock, flags);
1209 * SA1111 "Register Access Bus."
1211 * We model this as a regular bus type, and hang devices directly
1212 * off this.
1214 static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1216 struct sa1111_dev *dev = SA1111_DEV(_dev);
1217 struct sa1111_driver *drv = SA1111_DRV(_drv);
1219 return dev->devid == drv->devid;
1222 static int sa1111_bus_suspend(struct device *dev, pm_message_t state)
1224 struct sa1111_dev *sadev = SA1111_DEV(dev);
1225 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1226 int ret = 0;
1228 if (drv && drv->suspend)
1229 ret = drv->suspend(sadev, state);
1230 return ret;
1233 static int sa1111_bus_resume(struct device *dev)
1235 struct sa1111_dev *sadev = SA1111_DEV(dev);
1236 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1237 int ret = 0;
1239 if (drv && drv->resume)
1240 ret = drv->resume(sadev);
1241 return ret;
1244 static int sa1111_bus_probe(struct device *dev)
1246 struct sa1111_dev *sadev = SA1111_DEV(dev);
1247 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1248 int ret = -ENODEV;
1250 if (drv->probe)
1251 ret = drv->probe(sadev);
1252 return ret;
1255 static int sa1111_bus_remove(struct device *dev)
1257 struct sa1111_dev *sadev = SA1111_DEV(dev);
1258 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1259 int ret = 0;
1261 if (drv->remove)
1262 ret = drv->remove(sadev);
1263 return ret;
1266 struct bus_type sa1111_bus_type = {
1267 .name = "sa1111-rab",
1268 .match = sa1111_match,
1269 .probe = sa1111_bus_probe,
1270 .remove = sa1111_bus_remove,
1271 .suspend = sa1111_bus_suspend,
1272 .resume = sa1111_bus_resume,
1275 int sa1111_driver_register(struct sa1111_driver *driver)
1277 driver->drv.bus = &sa1111_bus_type;
1278 return driver_register(&driver->drv);
1281 void sa1111_driver_unregister(struct sa1111_driver *driver)
1283 driver_unregister(&driver->drv);
1286 static int __init sa1111_init(void)
1288 int ret = bus_register(&sa1111_bus_type);
1289 if (ret == 0)
1290 platform_driver_register(&sa1111_device_driver);
1291 return ret;
1294 static void __exit sa1111_exit(void)
1296 platform_driver_unregister(&sa1111_device_driver);
1297 bus_unregister(&sa1111_bus_type);
1300 subsys_initcall(sa1111_init);
1301 module_exit(sa1111_exit);
1303 MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1304 MODULE_LICENSE("GPL");
1306 EXPORT_SYMBOL(sa1111_select_audio_mode);
1307 EXPORT_SYMBOL(sa1111_set_audio_rate);
1308 EXPORT_SYMBOL(sa1111_get_audio_rate);
1309 EXPORT_SYMBOL(sa1111_set_io_dir);
1310 EXPORT_SYMBOL(sa1111_set_io);
1311 EXPORT_SYMBOL(sa1111_set_sleep_io);
1312 EXPORT_SYMBOL(sa1111_enable_device);
1313 EXPORT_SYMBOL(sa1111_disable_device);
1314 EXPORT_SYMBOL(sa1111_pll_clock);
1315 EXPORT_SYMBOL(sa1111_bus_type);
1316 EXPORT_SYMBOL(sa1111_driver_register);
1317 EXPORT_SYMBOL(sa1111_driver_unregister);