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/config.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/dma-mapping.h>
30 #include <asm/hardware.h>
31 #include <asm/mach-types.h>
34 #include <asm/mach/irq.h>
36 #include <asm/hardware/sa1111.h>
38 #ifdef CONFIG_ARCH_PXA
39 #include <asm/arch/pxa-regs.h>
42 extern void __init sa1110_mb_enable(void);
45 * We keep the following data for the overall SA1111. Note that the
46 * struct device and struct resource are "fake"; they should be supplied
47 * by the bus above us. However, in the interests of getting all SA1111
48 * drivers converted over to the device model, we provide this as an
49 * anchor point for all the other drivers.
60 * We _really_ need to eliminate this. Its only users
61 * are the PWM and DMA checking code.
63 static struct sa1111 *g_sa1111;
65 struct sa1111_dev_info {
67 unsigned long skpcr_mask;
72 static struct sa1111_dev_info sa1111_devices[] = {
75 .skpcr_mask = SKPCR_UCLKEN,
76 .devid = SA1111_DEVID_USB,
88 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
89 .devid = SA1111_DEVID_SAC,
99 .skpcr_mask = SKPCR_SCLKEN,
100 .devid = SA1111_DEVID_SSP,
103 .offset = SA1111_KBD,
104 .skpcr_mask = SKPCR_PTCLKEN,
105 .devid = SA1111_DEVID_PS2,
112 .offset = SA1111_MSE,
113 .skpcr_mask = SKPCR_PMCLKEN,
114 .devid = SA1111_DEVID_PS2,
123 .devid = SA1111_DEVID_PCMCIA,
136 * SA1111 interrupt support. Since clearing an IRQ while there are
137 * active IRQs causes the interrupt output to pulse, the upper levels
138 * will call us again if there are more interrupts to process.
141 sa1111_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
143 unsigned int stat0, stat1, i;
145 stat0 = sa1111_readl(desc->data + SA1111_INTSTATCLR0);
146 stat1 = sa1111_readl(desc->data + SA1111_INTSTATCLR1);
148 sa1111_writel(stat0, desc->data + SA1111_INTSTATCLR0);
150 desc->chip->ack(irq);
152 sa1111_writel(stat1, desc->data + SA1111_INTSTATCLR1);
154 if (stat0 == 0 && stat1 == 0) {
155 do_bad_IRQ(irq, desc, regs);
159 for (i = IRQ_SA1111_START; stat0; i++, stat0 >>= 1)
161 do_edge_IRQ(i, irq_desc + i, regs);
163 for (i = IRQ_SA1111_START + 32; stat1; i++, stat1 >>= 1)
165 do_edge_IRQ(i, irq_desc + i, regs);
167 /* For level-based interrupts */
168 desc->chip->unmask(irq);
171 #define SA1111_IRQMASK_LO(x) (1 << (x - IRQ_SA1111_START))
172 #define SA1111_IRQMASK_HI(x) (1 << (x - IRQ_SA1111_START - 32))
174 static void sa1111_ack_irq(unsigned int irq)
178 static void sa1111_mask_lowirq(unsigned int irq)
180 void *mapbase = get_irq_chipdata(irq);
183 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
184 ie0 &= ~SA1111_IRQMASK_LO(irq);
185 writel(ie0, mapbase + SA1111_INTEN0);
188 static void sa1111_unmask_lowirq(unsigned int irq)
190 void *mapbase = get_irq_chipdata(irq);
193 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
194 ie0 |= SA1111_IRQMASK_LO(irq);
195 sa1111_writel(ie0, mapbase + SA1111_INTEN0);
199 * Attempt to re-trigger the interrupt. The SA1111 contains a register
200 * (INTSET) which claims to do this. However, in practice no amount of
201 * manipulation of INTEN and INTSET guarantees that the interrupt will
202 * be triggered. In fact, its very difficult, if not impossible to get
203 * INTSET to re-trigger the interrupt.
205 static int sa1111_retrigger_lowirq(unsigned int irq)
207 unsigned int mask = SA1111_IRQMASK_LO(irq);
208 void *mapbase = get_irq_chipdata(irq);
212 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
213 for (i = 0; i < 8; i++) {
214 sa1111_writel(ip0 ^ mask, mapbase + SA1111_INTPOL0);
215 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
216 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
221 printk(KERN_ERR "Danger Will Robinson: failed to "
222 "re-trigger IRQ%d\n", irq);
223 return i == 8 ? -1 : 0;
226 static int sa1111_type_lowirq(unsigned int irq, unsigned int flags)
228 unsigned int mask = SA1111_IRQMASK_LO(irq);
229 void *mapbase = get_irq_chipdata(irq);
232 if (flags == IRQT_PROBE)
235 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
238 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
239 if (flags & __IRQT_RISEDGE)
243 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
244 sa1111_writel(ip0, mapbase + SA1111_WAKEPOL0);
249 static int sa1111_wake_lowirq(unsigned int irq, unsigned int on)
251 unsigned int mask = SA1111_IRQMASK_LO(irq);
252 void *mapbase = get_irq_chipdata(irq);
255 we0 = sa1111_readl(mapbase + SA1111_WAKEEN0);
260 sa1111_writel(we0, mapbase + SA1111_WAKEEN0);
265 static struct irqchip sa1111_low_chip = {
266 .ack = sa1111_ack_irq,
267 .mask = sa1111_mask_lowirq,
268 .unmask = sa1111_unmask_lowirq,
269 .retrigger = sa1111_retrigger_lowirq,
270 .type = sa1111_type_lowirq,
271 .wake = sa1111_wake_lowirq,
274 static void sa1111_mask_highirq(unsigned int irq)
276 void *mapbase = get_irq_chipdata(irq);
279 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
280 ie1 &= ~SA1111_IRQMASK_HI(irq);
281 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
284 static void sa1111_unmask_highirq(unsigned int irq)
286 void *mapbase = get_irq_chipdata(irq);
289 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
290 ie1 |= SA1111_IRQMASK_HI(irq);
291 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
295 * Attempt to re-trigger the interrupt. The SA1111 contains a register
296 * (INTSET) which claims to do this. However, in practice no amount of
297 * manipulation of INTEN and INTSET guarantees that the interrupt will
298 * be triggered. In fact, its very difficult, if not impossible to get
299 * INTSET to re-trigger the interrupt.
301 static int sa1111_retrigger_highirq(unsigned int irq)
303 unsigned int mask = SA1111_IRQMASK_HI(irq);
304 void *mapbase = get_irq_chipdata(irq);
308 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
309 for (i = 0; i < 8; i++) {
310 sa1111_writel(ip1 ^ mask, mapbase + SA1111_INTPOL1);
311 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
312 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
317 printk(KERN_ERR "Danger Will Robinson: failed to "
318 "re-trigger IRQ%d\n", irq);
319 return i == 8 ? -1 : 0;
322 static int sa1111_type_highirq(unsigned int irq, unsigned int flags)
324 unsigned int mask = SA1111_IRQMASK_HI(irq);
325 void *mapbase = get_irq_chipdata(irq);
328 if (flags == IRQT_PROBE)
331 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
334 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
335 if (flags & __IRQT_RISEDGE)
339 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
340 sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);
345 static int sa1111_wake_highirq(unsigned int irq, unsigned int on)
347 unsigned int mask = SA1111_IRQMASK_HI(irq);
348 void *mapbase = get_irq_chipdata(irq);
351 we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
356 sa1111_writel(we1, mapbase + SA1111_WAKEEN1);
361 static struct irqchip sa1111_high_chip = {
362 .ack = sa1111_ack_irq,
363 .mask = sa1111_mask_highirq,
364 .unmask = sa1111_unmask_highirq,
365 .retrigger = sa1111_retrigger_highirq,
366 .type = sa1111_type_highirq,
367 .wake = sa1111_wake_highirq,
370 static void sa1111_setup_irq(struct sa1111 *sachip)
372 void *irqbase = sachip->base + SA1111_INTC;
376 * We're guaranteed that this region hasn't been taken.
378 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
380 /* disable all IRQs */
381 sa1111_writel(0, irqbase + SA1111_INTEN0);
382 sa1111_writel(0, irqbase + SA1111_INTEN1);
383 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
384 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
387 * detect on rising edge. Note: Feb 2001 Errata for SA1111
388 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
390 sa1111_writel(0, irqbase + SA1111_INTPOL0);
391 sa1111_writel(SA1111_IRQMASK_HI(IRQ_S0_READY_NINT) |
392 SA1111_IRQMASK_HI(IRQ_S1_READY_NINT),
393 irqbase + SA1111_INTPOL1);
396 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR0);
397 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR1);
399 for (irq = IRQ_GPAIN0; irq <= SSPROR; irq++) {
400 set_irq_chip(irq, &sa1111_low_chip);
401 set_irq_chipdata(irq, irqbase);
402 set_irq_handler(irq, do_edge_IRQ);
403 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
406 for (irq = AUDXMTDMADONEA; irq <= IRQ_S1_BVD1_STSCHG; irq++) {
407 set_irq_chip(irq, &sa1111_high_chip);
408 set_irq_chipdata(irq, irqbase);
409 set_irq_handler(irq, do_edge_IRQ);
410 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
414 * Register SA1111 interrupt
416 set_irq_type(sachip->irq, IRQT_RISING);
417 set_irq_data(sachip->irq, irqbase);
418 set_irq_chained_handler(sachip->irq, sa1111_irq_handler);
422 * Bring the SA1111 out of reset. This requires a set procedure:
423 * 1. nRESET asserted (by hardware)
424 * 2. CLK turned on from SA1110
425 * 3. nRESET deasserted
426 * 4. VCO turned on, PLL_BYPASS turned off
427 * 5. Wait lock time, then assert RCLKEn
428 * 7. PCR set to allow clocking of individual functions
430 * Until we've done this, the only registers we can access are:
435 static void sa1111_wake(struct sa1111 *sachip)
437 unsigned long flags, r;
439 spin_lock_irqsave(&sachip->lock, flags);
441 #ifdef CONFIG_ARCH_SA1100
443 * First, set up the 3.6864MHz clock on GPIO 27 for the SA-1111:
444 * (SA-1110 Developer's Manual, section 9.1.2.1)
446 GAFR |= GPIO_32_768kHz;
447 GPDR |= GPIO_32_768kHz;
448 TUCR = TUCR_3_6864MHz;
449 #elif CONFIG_ARCH_PXA
450 pxa_gpio_mode(GPIO11_3_6MHz_MD);
452 #error missing clock setup
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 = kmalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
550 memset(dev, 0, sizeof(struct sa1111_dev));
552 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
553 "%4.4lx", info->offset);
555 dev->devid = info->devid;
556 dev->dev.parent = sachip->dev;
557 dev->dev.bus = &sa1111_bus_type;
558 dev->dev.release = sa1111_dev_release;
559 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
560 dev->res.start = sachip->phys + info->offset;
561 dev->res.end = dev->res.start + 511;
562 dev->res.name = dev->dev.bus_id;
563 dev->res.flags = IORESOURCE_MEM;
564 dev->mapbase = sachip->base + info->offset;
565 dev->skpcr_mask = info->skpcr_mask;
566 memmove(dev->irq, info->irq, sizeof(dev->irq));
568 ret = request_resource(parent, &dev->res);
570 printk("SA1111: failed to allocate resource for %s\n",
577 ret = device_register(&dev->dev);
579 release_resource(&dev->res);
585 * If the parent device has a DMA mask associated with it,
586 * propagate it down to the children.
588 if (sachip->dev->dma_mask) {
589 dev->dma_mask = *sachip->dev->dma_mask;
590 dev->dev.dma_mask = &dev->dma_mask;
592 if (dev->dma_mask != 0xffffffffUL) {
593 ret = dmabounce_register_dev(&dev->dev, 1024, 4096);
595 printk("SA1111: Failed to register %s with dmabounce", dev->dev.bus_id);
596 device_unregister(&dev->dev);
606 * sa1111_probe - probe for a single SA1111 chip.
607 * @phys_addr: physical address of device.
609 * Probe for a SA1111 chip. This must be called
610 * before any other SA1111-specific code.
613 * %-ENODEV device not found.
614 * %-EBUSY physical address already marked in-use.
618 __sa1111_probe(struct device *me, struct resource *mem, int irq)
620 struct sa1111 *sachip;
622 unsigned int has_devs, val;
623 int i, ret = -ENODEV;
625 sachip = kmalloc(sizeof(struct sa1111), GFP_KERNEL);
629 memset(sachip, 0, sizeof(struct sa1111));
631 spin_lock_init(&sachip->lock);
634 dev_set_drvdata(sachip->dev, sachip);
636 sachip->phys = mem->start;
640 * Map the whole region. This also maps the
641 * registers for our children.
643 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
650 * Probe for the chip. Only touch the SBI registers.
652 id = sa1111_readl(sachip->base + SA1111_SKID);
653 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
654 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
659 printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
660 "silicon revision %lx, metal revision %lx\n",
661 (id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));
664 * We found it. Wake the chip up, and initialise.
668 #ifdef CONFIG_ARCH_SA1100
670 * The SDRAM configuration of the SA1110 and the SA1111 must
671 * match. This is very important to ensure that SA1111 accesses
672 * don't corrupt the SDRAM. Note that this ungates the SA1111's
673 * MBGNT signal, so we must have called sa1110_mb_disable()
676 sa1111_configure_smc(sachip, 1,
677 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
678 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
681 * We only need to turn on DCLK whenever we want to use the
682 * DMA. It can otherwise be held firmly in the off position.
683 * (currently, we always enable it.)
685 val = sa1111_readl(sachip->base + SA1111_SKPCR);
686 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
689 * Enable the SA1110 memory bus request and grant signals.
695 * The interrupt controller must be initialised before any
696 * other device to ensure that the interrupts are available.
698 if (sachip->irq != NO_IRQ)
699 sa1111_setup_irq(sachip);
704 if (machine_is_assabet() || machine_is_jornada720() ||
706 has_devs &= ~(1 << 4);
708 has_devs &= ~(1 << 1);
710 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
711 if (has_devs & (1 << i))
712 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
717 iounmap(sachip->base);
723 static void __sa1111_remove(struct sa1111 *sachip)
725 struct list_head *l, *n;
726 void *irqbase = sachip->base + SA1111_INTC;
728 list_for_each_safe(l, n, &sachip->dev->children) {
729 struct device *d = list_to_dev(l);
731 device_unregister(d);
734 /* disable all IRQs */
735 sa1111_writel(0, irqbase + SA1111_INTEN0);
736 sa1111_writel(0, irqbase + SA1111_INTEN1);
737 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
738 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
740 if (sachip->irq != NO_IRQ) {
741 set_irq_chained_handler(sachip->irq, NULL);
742 set_irq_data(sachip->irq, NULL);
744 release_mem_region(sachip->phys + SA1111_INTC, 512);
747 iounmap(sachip->base);
752 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
753 * Chip Specification Update" (June 2000), erratum #7, there is a
754 * significant bug in the SA1111 SDRAM shared memory controller. If
755 * an access to a region of memory above 1MB relative to the bank base,
756 * it is important that address bit 10 _NOT_ be asserted. Depending
757 * on the configuration of the RAM, bit 10 may correspond to one
758 * of several different (processor-relative) address bits.
760 * This routine only identifies whether or not a given DMA address
761 * is susceptible to the bug.
763 * This should only get called for sa1111_device types due to the
764 * way we configure our device dma_masks.
766 int dma_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
769 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
770 * User's Guide" mentions that jumpers R51 and R52 control the
771 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
772 * SDRAM bank 1 on Neponset). The default configuration selects
773 * Assabet, so any address in bank 1 is necessarily invalid.
775 return ((machine_is_assabet() || machine_is_pfs168()) &&
776 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000));
779 struct sa1111_save_data {
784 unsigned char skpwm0;
785 unsigned char skpwm1;
788 * Interrupt controller
790 unsigned int intpol0;
791 unsigned int intpol1;
794 unsigned int wakepol0;
795 unsigned int wakepol1;
796 unsigned int wakeen0;
797 unsigned int wakeen1;
802 static int sa1111_suspend(struct device *dev, u32 state, u32 level)
804 struct sa1111 *sachip = dev_get_drvdata(dev);
805 struct sa1111_save_data *save;
810 if (level != SUSPEND_DISABLE)
813 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
816 dev->power.saved_state = save;
818 spin_lock_irqsave(&sachip->lock, flags);
824 save->skcr = sa1111_readl(base + SA1111_SKCR);
825 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
826 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
827 save->skaud = sa1111_readl(base + SA1111_SKAUD);
828 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
829 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
831 base = sachip->base + SA1111_INTC;
832 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
833 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
834 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
835 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
836 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
837 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
838 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
839 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
844 val = sa1111_readl(sachip->base + SA1111_SKCR);
845 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
846 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
847 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
849 spin_unlock_irqrestore(&sachip->lock, flags);
855 * sa1111_resume - Restore the SA1111 device state.
856 * @dev: device to restore
857 * @level: resume level
859 * Restore the general state of the SA1111; clock control and
860 * interrupt controller. Other parts of the SA1111 must be
861 * restored by their respective drivers, and must be called
862 * via LDM after this function.
864 static int sa1111_resume(struct device *dev, u32 level)
866 struct sa1111 *sachip = dev_get_drvdata(dev);
867 struct sa1111_save_data *save;
868 unsigned long flags, id;
871 if (level != RESUME_ENABLE)
874 save = (struct sa1111_save_data *)dev->power.saved_state;
878 spin_lock_irqsave(&sachip->lock, flags);
881 * Ensure that the SA1111 is still here.
882 * FIXME: shouldn't do this here.
884 id = sa1111_readl(sachip->base + SA1111_SKID);
885 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
886 __sa1111_remove(sachip);
887 dev_set_drvdata(dev, NULL);
893 * First of all, wake up the chip.
896 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
897 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
900 sa1111_writel(save->skcr, base + SA1111_SKCR);
901 sa1111_writel(save->skpcr, base + SA1111_SKPCR);
902 sa1111_writel(save->skcdr, base + SA1111_SKCDR);
903 sa1111_writel(save->skaud, base + SA1111_SKAUD);
904 sa1111_writel(save->skpwm0, base + SA1111_SKPWM0);
905 sa1111_writel(save->skpwm1, base + SA1111_SKPWM1);
907 base = sachip->base + SA1111_INTC;
908 sa1111_writel(save->intpol0, base + SA1111_INTPOL0);
909 sa1111_writel(save->intpol1, base + SA1111_INTPOL1);
910 sa1111_writel(save->inten0, base + SA1111_INTEN0);
911 sa1111_writel(save->inten1, base + SA1111_INTEN1);
912 sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
913 sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
914 sa1111_writel(save->wakeen0, base + SA1111_WAKEEN0);
915 sa1111_writel(save->wakeen1, base + SA1111_WAKEEN1);
917 spin_unlock_irqrestore(&sachip->lock, flags);
919 dev->power.saved_state = NULL;
925 #else /* !CONFIG_PM */
926 #define sa1111_resume NULL
927 #define sa1111_suspend NULL
928 #endif /* !CONFIG_PM */
931 static int sa1111_probe(struct device *dev)
933 struct platform_device *pdev = to_platform_device(dev);
934 struct resource *mem;
937 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
940 irq = platform_get_irq(pdev, 0);
942 return __sa1111_probe(dev, mem, irq);
945 static int sa1111_remove(struct device *dev)
947 struct sa1111 *sachip = dev_get_drvdata(dev);
950 __sa1111_remove(sachip);
951 dev_set_drvdata(dev, NULL);
954 kfree(dev->power.saved_state);
955 dev->power.saved_state = NULL;
963 * Not sure if this should be on the system bus or not yet.
964 * We really want some way to register a system device at
965 * the per-machine level, and then have this driver pick
966 * up the registered devices.
968 * We also need to handle the SDRAM configuration for
969 * PXA250/SA1110 machine classes.
971 static struct device_driver sa1111_device_driver = {
973 .bus = &platform_bus_type,
974 .probe = sa1111_probe,
975 .remove = sa1111_remove,
976 .suspend = sa1111_suspend,
977 .resume = sa1111_resume,
981 * Get the parent device driver (us) structure
982 * from a child function device
984 static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
986 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
990 * The bits in the opdiv field are non-linear.
992 static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
994 static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
996 unsigned int skcdr, fbdiv, ipdiv, opdiv;
998 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
1000 fbdiv = (skcdr & 0x007f) + 2;
1001 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1002 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1004 return 3686400 * fbdiv / (ipdiv * opdiv);
1008 * sa1111_pll_clock - return the current PLL clock frequency.
1009 * @sadev: SA1111 function block
1011 * BUG: we should look at SKCR. We also blindly believe that
1012 * the chip is being fed with the 3.6864MHz clock.
1014 * Returns the PLL clock in Hz.
1016 unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1018 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1020 return __sa1111_pll_clock(sachip);
1024 * sa1111_select_audio_mode - select I2S or AC link mode
1025 * @sadev: SA1111 function block
1026 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1028 * Frob the SKCR to select AC Link mode or I2S mode for
1031 void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1033 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1034 unsigned long flags;
1037 spin_lock_irqsave(&sachip->lock, flags);
1039 val = sa1111_readl(sachip->base + SA1111_SKCR);
1040 if (mode == SA1111_AUDIO_I2S) {
1045 sa1111_writel(val, sachip->base + SA1111_SKCR);
1047 spin_unlock_irqrestore(&sachip->lock, flags);
1051 * sa1111_set_audio_rate - set the audio sample rate
1052 * @sadev: SA1111 SAC function block
1053 * @rate: sample rate to select
1055 int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1057 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1060 if (sadev->devid != SA1111_DEVID_SAC)
1063 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1069 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1075 * sa1111_get_audio_rate - get the audio sample rate
1076 * @sadev: SA1111 SAC function block device
1078 int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1080 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1083 if (sadev->devid != SA1111_DEVID_SAC)
1086 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1088 return __sa1111_pll_clock(sachip) / (256 * div);
1091 void sa1111_set_io_dir(struct sa1111_dev *sadev,
1092 unsigned int bits, unsigned int dir,
1093 unsigned int sleep_dir)
1095 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1096 unsigned long flags;
1098 void *gpio = sachip->base + SA1111_GPIO;
1100 #define MODIFY_BITS(port, mask, dir) \
1102 val = sa1111_readl(port); \
1104 val |= (dir) & (mask); \
1105 sa1111_writel(val, port); \
1108 spin_lock_irqsave(&sachip->lock, flags);
1109 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1110 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1111 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1113 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1114 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1115 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1116 spin_unlock_irqrestore(&sachip->lock, flags);
1119 void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1121 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1122 unsigned long flags;
1124 void *gpio = sachip->base + SA1111_GPIO;
1126 spin_lock_irqsave(&sachip->lock, flags);
1127 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1128 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1129 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1130 spin_unlock_irqrestore(&sachip->lock, flags);
1133 void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1135 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1136 unsigned long flags;
1138 void *gpio = sachip->base + SA1111_GPIO;
1140 spin_lock_irqsave(&sachip->lock, flags);
1141 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1142 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1143 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1144 spin_unlock_irqrestore(&sachip->lock, flags);
1148 * Individual device operations.
1152 * sa1111_enable_device - enable an on-chip SA1111 function block
1153 * @sadev: SA1111 function block device to enable
1155 void sa1111_enable_device(struct sa1111_dev *sadev)
1157 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1158 unsigned long flags;
1161 spin_lock_irqsave(&sachip->lock, flags);
1162 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1163 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1164 spin_unlock_irqrestore(&sachip->lock, flags);
1168 * sa1111_disable_device - disable an on-chip SA1111 function block
1169 * @sadev: SA1111 function block device to disable
1171 void sa1111_disable_device(struct sa1111_dev *sadev)
1173 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1174 unsigned long flags;
1177 spin_lock_irqsave(&sachip->lock, flags);
1178 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1179 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1180 spin_unlock_irqrestore(&sachip->lock, flags);
1184 * SA1111 "Register Access Bus."
1186 * We model this as a regular bus type, and hang devices directly
1189 static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1191 struct sa1111_dev *dev = SA1111_DEV(_dev);
1192 struct sa1111_driver *drv = SA1111_DRV(_drv);
1194 return dev->devid == drv->devid;
1197 static int sa1111_bus_suspend(struct device *dev, u32 state)
1199 struct sa1111_dev *sadev = SA1111_DEV(dev);
1200 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1203 if (drv && drv->suspend)
1204 ret = drv->suspend(sadev, state);
1208 static int sa1111_bus_resume(struct device *dev)
1210 struct sa1111_dev *sadev = SA1111_DEV(dev);
1211 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1214 if (drv && drv->resume)
1215 ret = drv->resume(sadev);
1219 static int sa1111_bus_probe(struct device *dev)
1221 struct sa1111_dev *sadev = SA1111_DEV(dev);
1222 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1226 ret = drv->probe(sadev);
1230 static int sa1111_bus_remove(struct device *dev)
1232 struct sa1111_dev *sadev = SA1111_DEV(dev);
1233 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1237 ret = drv->remove(sadev);
1241 struct bus_type sa1111_bus_type = {
1242 .name = "sa1111-rab",
1243 .match = sa1111_match,
1244 .suspend = sa1111_bus_suspend,
1245 .resume = sa1111_bus_resume,
1248 int sa1111_driver_register(struct sa1111_driver *driver)
1250 driver->drv.probe = sa1111_bus_probe;
1251 driver->drv.remove = sa1111_bus_remove;
1252 driver->drv.bus = &sa1111_bus_type;
1253 return driver_register(&driver->drv);
1256 void sa1111_driver_unregister(struct sa1111_driver *driver)
1258 driver_unregister(&driver->drv);
1261 static int __init sa1111_init(void)
1263 int ret = bus_register(&sa1111_bus_type);
1265 driver_register(&sa1111_device_driver);
1269 static void __exit sa1111_exit(void)
1271 driver_unregister(&sa1111_device_driver);
1272 bus_unregister(&sa1111_bus_type);
1275 module_init(sa1111_init);
1276 module_exit(sa1111_exit);
1278 MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1279 MODULE_LICENSE("GPL");
1281 EXPORT_SYMBOL(sa1111_select_audio_mode);
1282 EXPORT_SYMBOL(sa1111_set_audio_rate);
1283 EXPORT_SYMBOL(sa1111_get_audio_rate);
1284 EXPORT_SYMBOL(sa1111_set_io_dir);
1285 EXPORT_SYMBOL(sa1111_set_io);
1286 EXPORT_SYMBOL(sa1111_set_sleep_io);
1287 EXPORT_SYMBOL(sa1111_enable_device);
1288 EXPORT_SYMBOL(sa1111_disable_device);
1289 EXPORT_SYMBOL(sa1111_pll_clock);
1290 EXPORT_SYMBOL(sa1111_bus_type);
1291 EXPORT_SYMBOL(sa1111_driver_register);
1292 EXPORT_SYMBOL(sa1111_driver_unregister);