2 * linux/arch/arm/mach-sa1100/sa1111.c
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>
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.
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 {
65 unsigned long skpcr_mask;
70 static struct sa1111_dev_info sa1111_devices[] = {
73 .skpcr_mask = SKPCR_UCLKEN,
74 .devid = SA1111_DEVID_USB,
86 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
87 .devid = SA1111_DEVID_SAC,
97 .skpcr_mask = SKPCR_SCLKEN,
98 .devid = SA1111_DEVID_SSP,
101 .offset = SA1111_KBD,
102 .skpcr_mask = SKPCR_PTCLKEN,
103 .devid = SA1111_DEVID_PS2,
110 .offset = SA1111_MSE,
111 .skpcr_mask = SKPCR_PMCLKEN,
112 .devid = SA1111_DEVID_PS2,
121 .devid = SA1111_DEVID_PCMCIA,
133 void __init sa1111_adjust_zones(int node, unsigned long *size, unsigned long *holes)
135 unsigned int sz = SZ_1M >> PAGE_SHIFT;
140 size[1] = 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.
150 sa1111_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
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, regs);
169 for (i = IRQ_SA1111_START; stat0; i++, stat0 >>= 1)
171 handle_edge_irq(i, irq_desc + i, regs);
173 for (i = IRQ_SA1111_START + 32; stat1; i++, stat1 >>= 1)
175 handle_edge_irq(i, irq_desc + i, regs);
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);
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);
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);
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)
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);
242 if (flags == IRQT_PROBE)
245 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
248 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
249 if (flags & __IRQT_RISEDGE)
253 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
254 sa1111_writel(ip0, mapbase + SA1111_WAKEPOL0);
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);
265 we0 = sa1111_readl(mapbase + SA1111_WAKEEN0);
270 sa1111_writel(we0, mapbase + SA1111_WAKEEN0);
275 static struct irq_chip sa1111_low_chip = {
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);
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);
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);
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)
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);
339 if (flags == IRQT_PROBE)
342 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
345 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
346 if (flags & __IRQT_RISEDGE)
350 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
351 sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);
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);
362 we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
367 sa1111_writel(we1, mapbase + SA1111_WAKEEN1);
372 static struct irq_chip sa1111_high_chip = {
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;
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);
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:
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);
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.
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.
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[] = {
504 * Configure the SA1111 shared memory controller.
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)
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];
530 static void sa1111_dev_release(struct device *_dev)
532 struct sa1111_dev *dev = SA1111_DEV(_dev);
534 release_resource(&dev->res);
539 sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
540 struct sa1111_dev_info *info)
542 struct sa1111_dev *dev;
545 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
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));
567 ret = request_resource(parent, &dev->res);
569 printk("SA1111: failed to allocate resource for %s\n",
576 ret = device_register(&dev->dev);
578 release_resource(&dev->res);
584 * If the parent device has a DMA mask associated with it,
585 * propagate it down to the children.
587 if (sachip->dev->dma_mask) {
588 dev->dma_mask = *sachip->dev->dma_mask;
589 dev->dev.dma_mask = &dev->dma_mask;
591 if (dev->dma_mask != 0xffffffffUL) {
592 ret = dmabounce_register_dev(&dev->dev, 1024, 4096);
594 printk("SA1111: Failed to register %s with dmabounce", dev->dev.bus_id);
595 device_unregister(&dev->dev);
605 * sa1111_probe - probe for a single SA1111 chip.
606 * @phys_addr: physical address of device.
608 * Probe for a SA1111 chip. This must be called
609 * before any other SA1111-specific code.
612 * %-ENODEV device not found.
613 * %-EBUSY physical address already marked in-use.
617 __sa1111_probe(struct device *me, struct resource *mem, int irq)
619 struct sa1111 *sachip;
621 unsigned int has_devs, val;
622 int i, ret = -ENODEV;
624 sachip = kzalloc(sizeof(struct sa1111), GFP_KERNEL);
628 sachip->clk = clk_get(me, "GPIO27_CLK");
630 ret = PTR_ERR(sachip->clk);
634 spin_lock_init(&sachip->lock);
637 dev_set_drvdata(sachip->dev, sachip);
639 sachip->phys = mem->start;
643 * Map the whole region. This also maps the
644 * registers for our children.
646 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
653 * Probe for the chip. Only touch the SBI registers.
655 id = sa1111_readl(sachip->base + SA1111_SKID);
656 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
657 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
662 printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
663 "silicon revision %lx, metal revision %lx\n",
664 (id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));
667 * We found it. Wake the chip up, and initialise.
671 #ifdef CONFIG_ARCH_SA1100
673 * The SDRAM configuration of the SA1110 and the SA1111 must
674 * match. This is very important to ensure that SA1111 accesses
675 * don't corrupt the SDRAM. Note that this ungates the SA1111's
676 * MBGNT signal, so we must have called sa1110_mb_disable()
679 sa1111_configure_smc(sachip, 1,
680 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
681 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
684 * We only need to turn on DCLK whenever we want to use the
685 * DMA. It can otherwise be held firmly in the off position.
686 * (currently, we always enable it.)
688 val = sa1111_readl(sachip->base + SA1111_SKPCR);
689 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
692 * Enable the SA1110 memory bus request and grant signals.
698 * The interrupt controller must be initialised before any
699 * other device to ensure that the interrupts are available.
701 if (sachip->irq != NO_IRQ)
702 sa1111_setup_irq(sachip);
707 if (machine_is_assabet() || machine_is_jornada720() ||
709 has_devs &= ~(1 << 4);
711 has_devs &= ~(1 << 1);
713 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
714 if (has_devs & (1 << i))
715 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
720 iounmap(sachip->base);
722 clk_put(sachip->clk);
728 static int sa1111_remove_one(struct device *dev, void *data)
730 device_unregister(dev);
734 static void __sa1111_remove(struct sa1111 *sachip)
736 void __iomem *irqbase = sachip->base + SA1111_INTC;
738 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
740 /* disable all IRQs */
741 sa1111_writel(0, irqbase + SA1111_INTEN0);
742 sa1111_writel(0, irqbase + SA1111_INTEN1);
743 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
744 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
746 clk_disable(sachip->clk);
748 if (sachip->irq != NO_IRQ) {
749 set_irq_chained_handler(sachip->irq, NULL);
750 set_irq_data(sachip->irq, NULL);
752 release_mem_region(sachip->phys + SA1111_INTC, 512);
755 iounmap(sachip->base);
756 clk_put(sachip->clk);
761 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
762 * Chip Specification Update" (June 2000), erratum #7, there is a
763 * significant bug in the SA1111 SDRAM shared memory controller. If
764 * an access to a region of memory above 1MB relative to the bank base,
765 * it is important that address bit 10 _NOT_ be asserted. Depending
766 * on the configuration of the RAM, bit 10 may correspond to one
767 * of several different (processor-relative) address bits.
769 * This routine only identifies whether or not a given DMA address
770 * is susceptible to the bug.
772 * This should only get called for sa1111_device types due to the
773 * way we configure our device dma_masks.
775 int dma_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
778 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
779 * User's Guide" mentions that jumpers R51 and R52 control the
780 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
781 * SDRAM bank 1 on Neponset). The default configuration selects
782 * Assabet, so any address in bank 1 is necessarily invalid.
784 return ((machine_is_assabet() || machine_is_pfs168()) &&
785 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000));
788 struct sa1111_save_data {
793 unsigned char skpwm0;
794 unsigned char skpwm1;
797 * Interrupt controller
799 unsigned int intpol0;
800 unsigned int intpol1;
803 unsigned int wakepol0;
804 unsigned int wakepol1;
805 unsigned int wakeen0;
806 unsigned int wakeen1;
811 static int sa1111_suspend(struct platform_device *dev, pm_message_t state)
813 struct sa1111 *sachip = platform_get_drvdata(dev);
814 struct sa1111_save_data *save;
819 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
822 dev->dev.power.saved_state = save;
824 spin_lock_irqsave(&sachip->lock, flags);
830 save->skcr = sa1111_readl(base + SA1111_SKCR);
831 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
832 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
833 save->skaud = sa1111_readl(base + SA1111_SKAUD);
834 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
835 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
837 base = sachip->base + SA1111_INTC;
838 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
839 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
840 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
841 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
842 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
843 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
844 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
845 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
850 val = sa1111_readl(sachip->base + SA1111_SKCR);
851 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
852 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
853 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
855 clk_disable(sachip->clk);
857 spin_unlock_irqrestore(&sachip->lock, flags);
863 * sa1111_resume - Restore the SA1111 device state.
864 * @dev: device to restore
866 * Restore the general state of the SA1111; clock control and
867 * interrupt controller. Other parts of the SA1111 must be
868 * restored by their respective drivers, and must be called
869 * via LDM after this function.
871 static int sa1111_resume(struct platform_device *dev)
873 struct sa1111 *sachip = platform_get_drvdata(dev);
874 struct sa1111_save_data *save;
875 unsigned long flags, id;
878 save = (struct sa1111_save_data *)dev->dev.power.saved_state;
882 spin_lock_irqsave(&sachip->lock, flags);
885 * Ensure that the SA1111 is still here.
886 * FIXME: shouldn't do this here.
888 id = sa1111_readl(sachip->base + SA1111_SKID);
889 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
890 __sa1111_remove(sachip);
891 platform_set_drvdata(dev, NULL);
897 * First of all, wake up the chip.
900 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
901 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
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);
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);
921 spin_unlock_irqrestore(&sachip->lock, flags);
923 dev->dev.power.saved_state = NULL;
930 #define sa1111_suspend NULL
931 #define sa1111_resume NULL
934 static int sa1111_probe(struct platform_device *pdev)
936 struct resource *mem;
939 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
942 irq = platform_get_irq(pdev, 0);
946 return __sa1111_probe(&pdev->dev, mem, irq);
949 static int sa1111_remove(struct platform_device *pdev)
951 struct sa1111 *sachip = platform_get_drvdata(pdev);
954 __sa1111_remove(sachip);
955 platform_set_drvdata(pdev, NULL);
958 kfree(pdev->dev.power.saved_state);
959 pdev->dev.power.saved_state = NULL;
967 * Not sure if this should be on the system bus or not yet.
968 * We really want some way to register a system device at
969 * the per-machine level, and then have this driver pick
970 * up the registered devices.
972 * We also need to handle the SDRAM configuration for
973 * PXA250/SA1110 machine classes.
975 static struct platform_driver sa1111_device_driver = {
976 .probe = sa1111_probe,
977 .remove = sa1111_remove,
978 .suspend = sa1111_suspend,
979 .resume = sa1111_resume,
986 * Get the parent device driver (us) structure
987 * from a child function device
989 static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
991 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
995 * The bits in the opdiv field are non-linear.
997 static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
999 static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1001 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1003 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
1005 fbdiv = (skcdr & 0x007f) + 2;
1006 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1007 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1009 return 3686400 * fbdiv / (ipdiv * opdiv);
1013 * sa1111_pll_clock - return the current PLL clock frequency.
1014 * @sadev: SA1111 function block
1016 * BUG: we should look at SKCR. We also blindly believe that
1017 * the chip is being fed with the 3.6864MHz clock.
1019 * Returns the PLL clock in Hz.
1021 unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1023 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1025 return __sa1111_pll_clock(sachip);
1029 * sa1111_select_audio_mode - select I2S or AC link mode
1030 * @sadev: SA1111 function block
1031 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1033 * Frob the SKCR to select AC Link mode or I2S mode for
1036 void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1038 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1039 unsigned long flags;
1042 spin_lock_irqsave(&sachip->lock, flags);
1044 val = sa1111_readl(sachip->base + SA1111_SKCR);
1045 if (mode == SA1111_AUDIO_I2S) {
1050 sa1111_writel(val, sachip->base + SA1111_SKCR);
1052 spin_unlock_irqrestore(&sachip->lock, flags);
1056 * sa1111_set_audio_rate - set the audio sample rate
1057 * @sadev: SA1111 SAC function block
1058 * @rate: sample rate to select
1060 int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1062 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1065 if (sadev->devid != SA1111_DEVID_SAC)
1068 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1074 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1080 * sa1111_get_audio_rate - get the audio sample rate
1081 * @sadev: SA1111 SAC function block device
1083 int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1085 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1088 if (sadev->devid != SA1111_DEVID_SAC)
1091 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1093 return __sa1111_pll_clock(sachip) / (256 * div);
1096 void sa1111_set_io_dir(struct sa1111_dev *sadev,
1097 unsigned int bits, unsigned int dir,
1098 unsigned int sleep_dir)
1100 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1101 unsigned long flags;
1103 void __iomem *gpio = sachip->base + SA1111_GPIO;
1105 #define MODIFY_BITS(port, mask, dir) \
1107 val = sa1111_readl(port); \
1109 val |= (dir) & (mask); \
1110 sa1111_writel(val, port); \
1113 spin_lock_irqsave(&sachip->lock, flags);
1114 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1115 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1116 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1118 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1119 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1120 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1121 spin_unlock_irqrestore(&sachip->lock, flags);
1124 void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1126 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1127 unsigned long flags;
1129 void __iomem *gpio = sachip->base + SA1111_GPIO;
1131 spin_lock_irqsave(&sachip->lock, flags);
1132 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1133 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1134 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1135 spin_unlock_irqrestore(&sachip->lock, flags);
1138 void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1140 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1141 unsigned long flags;
1143 void __iomem *gpio = sachip->base + SA1111_GPIO;
1145 spin_lock_irqsave(&sachip->lock, flags);
1146 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1147 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1148 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1149 spin_unlock_irqrestore(&sachip->lock, flags);
1153 * Individual device operations.
1157 * sa1111_enable_device - enable an on-chip SA1111 function block
1158 * @sadev: SA1111 function block device to enable
1160 void sa1111_enable_device(struct sa1111_dev *sadev)
1162 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1163 unsigned long flags;
1166 spin_lock_irqsave(&sachip->lock, flags);
1167 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1168 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1169 spin_unlock_irqrestore(&sachip->lock, flags);
1173 * sa1111_disable_device - disable an on-chip SA1111 function block
1174 * @sadev: SA1111 function block device to disable
1176 void sa1111_disable_device(struct sa1111_dev *sadev)
1178 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1179 unsigned long flags;
1182 spin_lock_irqsave(&sachip->lock, flags);
1183 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1184 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1185 spin_unlock_irqrestore(&sachip->lock, flags);
1189 * SA1111 "Register Access Bus."
1191 * We model this as a regular bus type, and hang devices directly
1194 static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1196 struct sa1111_dev *dev = SA1111_DEV(_dev);
1197 struct sa1111_driver *drv = SA1111_DRV(_drv);
1199 return dev->devid == drv->devid;
1202 static int sa1111_bus_suspend(struct device *dev, pm_message_t state)
1204 struct sa1111_dev *sadev = SA1111_DEV(dev);
1205 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1208 if (drv && drv->suspend)
1209 ret = drv->suspend(sadev, state);
1213 static int sa1111_bus_resume(struct device *dev)
1215 struct sa1111_dev *sadev = SA1111_DEV(dev);
1216 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1219 if (drv && drv->resume)
1220 ret = drv->resume(sadev);
1224 static int sa1111_bus_probe(struct device *dev)
1226 struct sa1111_dev *sadev = SA1111_DEV(dev);
1227 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1231 ret = drv->probe(sadev);
1235 static int sa1111_bus_remove(struct device *dev)
1237 struct sa1111_dev *sadev = SA1111_DEV(dev);
1238 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1242 ret = drv->remove(sadev);
1246 struct bus_type sa1111_bus_type = {
1247 .name = "sa1111-rab",
1248 .match = sa1111_match,
1249 .probe = sa1111_bus_probe,
1250 .remove = sa1111_bus_remove,
1251 .suspend = sa1111_bus_suspend,
1252 .resume = sa1111_bus_resume,
1255 int sa1111_driver_register(struct sa1111_driver *driver)
1257 driver->drv.bus = &sa1111_bus_type;
1258 return driver_register(&driver->drv);
1261 void sa1111_driver_unregister(struct sa1111_driver *driver)
1263 driver_unregister(&driver->drv);
1266 static int __init sa1111_init(void)
1268 int ret = bus_register(&sa1111_bus_type);
1270 platform_driver_register(&sa1111_device_driver);
1274 static void __exit sa1111_exit(void)
1276 platform_driver_unregister(&sa1111_device_driver);
1277 bus_unregister(&sa1111_bus_type);
1280 subsys_initcall(sa1111_init);
1281 module_exit(sa1111_exit);
1283 MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1284 MODULE_LICENSE("GPL");
1286 EXPORT_SYMBOL(sa1111_select_audio_mode);
1287 EXPORT_SYMBOL(sa1111_set_audio_rate);
1288 EXPORT_SYMBOL(sa1111_get_audio_rate);
1289 EXPORT_SYMBOL(sa1111_set_io_dir);
1290 EXPORT_SYMBOL(sa1111_set_io);
1291 EXPORT_SYMBOL(sa1111_set_sleep_io);
1292 EXPORT_SYMBOL(sa1111_enable_device);
1293 EXPORT_SYMBOL(sa1111_disable_device);
1294 EXPORT_SYMBOL(sa1111_pll_clock);
1295 EXPORT_SYMBOL(sa1111_bus_type);
1296 EXPORT_SYMBOL(sa1111_driver_register);
1297 EXPORT_SYMBOL(sa1111_driver_unregister);