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/device.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
28 #include <asm/hardware.h>
29 #include <asm/mach-types.h>
32 #include <asm/mach/irq.h>
34 #include <asm/hardware/sa1111.h>
37 * We keep the following data for the overall SA1111. Note that the
38 * struct device and struct resource are "fake"; they should be supplied
39 * by the bus above us. However, in the interests of getting all SA1111
40 * drivers converted over to the device model, we provide this as an
41 * anchor point for all the other drivers.
52 * We _really_ need to eliminate this. Its only users
53 * are the PWM and DMA checking code.
55 static struct sa1111 *g_sa1111;
57 static struct sa1111_dev usb_dev = {
59 .name = "Intel Corporation SA1111 [USB Controller]",
61 .skpcr_mask = SKPCR_UCLKEN,
62 .devid = SA1111_DEVID_USB,
73 static struct sa1111_dev sac_dev = {
75 .name = "Intel Corporation SA1111 [Audio Controller]",
77 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
78 .devid = SA1111_DEVID_SAC,
87 static struct sa1111_dev ssp_dev = {
89 .name = "Intel Corporation SA1111 [SSP Controller]",
91 .skpcr_mask = SKPCR_SCLKEN,
92 .devid = SA1111_DEVID_SSP,
95 static struct sa1111_dev kbd_dev = {
97 .name = "Intel Corporation SA1111 [PS2]",
99 .skpcr_mask = SKPCR_PTCLKEN,
100 .devid = SA1111_DEVID_PS2,
107 static struct sa1111_dev mse_dev = {
109 .name = "Intel Corporation SA1111 [PS2]",
111 .skpcr_mask = SKPCR_PMCLKEN,
112 .devid = SA1111_DEVID_PS2,
119 static struct sa1111_dev int_dev = {
121 .name = "Intel Corporation SA1111 [Interrupt Controller]",
124 .devid = SA1111_DEVID_INT,
127 static struct sa1111_dev pcmcia_dev = {
129 .name = "Intel Corporation SA1111 [PCMCIA Controller]",
132 .devid = SA1111_DEVID_PCMCIA,
143 static struct sa1111_dev *devs[] = {
152 static unsigned int dev_offset[] = {
162 * SA1111 interrupt support. Since clearing an IRQ while there are
163 * active IRQs causes the interrupt output to pulse, the upper levels
164 * will call us again if there are more interrupts to process.
167 sa1111_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
169 unsigned int stat0, stat1, i;
176 desc->chip->ack(irq);
180 if (stat0 == 0 && stat1 == 0) {
181 do_bad_IRQ(irq, desc, regs);
185 for (i = IRQ_SA1111_START; stat0; i++, stat0 >>= 1)
187 do_edge_IRQ(i, irq_desc + i, regs);
189 for (i = IRQ_SA1111_START + 32; stat1; i++, stat1 >>= 1)
191 do_edge_IRQ(i, irq_desc + i, regs);
193 /* For level-based interrupts */
194 desc->chip->unmask(irq);
197 #define SA1111_IRQMASK_LO(x) (1 << (x - IRQ_SA1111_START))
198 #define SA1111_IRQMASK_HI(x) (1 << (x - IRQ_SA1111_START - 32))
200 static void sa1111_ack_irq(unsigned int irq)
204 static void sa1111_mask_lowirq(unsigned int irq)
206 INTEN0 &= ~SA1111_IRQMASK_LO(irq);
209 static void sa1111_unmask_lowirq(unsigned int irq)
211 INTEN0 |= SA1111_IRQMASK_LO(irq);
215 * Attempt to re-trigger the interrupt. The SA1111 contains a register
216 * (INTSET) which claims to do this. However, in practice no amount of
217 * manipulation of INTEN and INTSET guarantees that the interrupt will
218 * be triggered. In fact, its very difficult, if not impossible to get
219 * INTSET to re-trigger the interrupt.
221 static int sa1111_retrigger_lowirq(unsigned int irq)
223 unsigned int mask = SA1111_IRQMASK_LO(irq);
226 for (i = 0; i < 8; i++) {
229 if (INTSTATCLR1 & mask)
234 printk(KERN_ERR "Danger Will Robinson: failed to "
235 "re-trigger IRQ%d\n", irq);
236 return i == 8 ? -1 : 0;
239 static int sa1111_type_lowirq(unsigned int irq, unsigned int flags)
241 unsigned int mask = SA1111_IRQMASK_LO(irq);
243 if (flags == IRQT_PROBE)
246 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
249 if (flags & __IRQT_RISEDGE)
258 static int sa1111_wake_lowirq(unsigned int irq, unsigned int on)
260 unsigned int mask = SA1111_IRQMASK_LO(irq);
270 static struct irqchip sa1111_low_chip = {
271 .ack = sa1111_ack_irq,
272 .mask = sa1111_mask_lowirq,
273 .unmask = sa1111_unmask_lowirq,
274 .retrigger = sa1111_retrigger_lowirq,
275 .type = sa1111_type_lowirq,
276 .wake = sa1111_wake_lowirq,
279 static void sa1111_mask_highirq(unsigned int irq)
281 INTEN1 &= ~SA1111_IRQMASK_HI(irq);
284 static void sa1111_unmask_highirq(unsigned int irq)
286 INTEN1 |= SA1111_IRQMASK_HI(irq);
290 * Attempt to re-trigger the interrupt. The SA1111 contains a register
291 * (INTSET) which claims to do this. However, in practice no amount of
292 * manipulation of INTEN and INTSET guarantees that the interrupt will
293 * be triggered. In fact, its very difficult, if not impossible to get
294 * INTSET to re-trigger the interrupt.
296 static int sa1111_retrigger_highirq(unsigned int irq)
298 unsigned int mask = SA1111_IRQMASK_HI(irq);
301 for (i = 0; i < 8; i++) {
304 if (INTSTATCLR1 & mask)
309 printk(KERN_ERR "Danger Will Robinson: failed to "
310 "re-trigger IRQ%d\n", irq);
311 return i == 8 ? -1 : 0;
314 static int sa1111_type_highirq(unsigned int irq, unsigned int flags)
316 unsigned int mask = SA1111_IRQMASK_HI(irq);
318 if (flags == IRQT_PROBE)
321 if ((!(flags & __IRQT_RISEDGE) ^ !(flags & __IRQT_FALEDGE)) == 0)
324 if (flags & __IRQT_RISEDGE)
333 static int sa1111_wake_highirq(unsigned int irq, unsigned int on)
335 unsigned int mask = SA1111_IRQMASK_HI(irq);
345 static struct irqchip sa1111_high_chip = {
346 .ack = sa1111_ack_irq,
347 .mask = sa1111_mask_highirq,
348 .unmask = sa1111_unmask_highirq,
349 .retrigger = sa1111_retrigger_highirq,
350 .type = sa1111_type_highirq,
351 .wake = sa1111_wake_highirq,
354 static void __init sa1111_init_irq(struct sa1111_dev *sadev)
359 * We're guaranteed that this region hasn't been taken.
361 request_mem_region(sadev->res.start, 512, "irqs");
363 /* disable all IRQs */
364 sa1111_writel(0, sadev->mapbase + SA1111_INTEN0);
365 sa1111_writel(0, sadev->mapbase + SA1111_INTEN1);
366 sa1111_writel(0, sadev->mapbase + SA1111_WAKEEN0);
367 sa1111_writel(0, sadev->mapbase + SA1111_WAKEEN1);
370 * detect on rising edge. Note: Feb 2001 Errata for SA1111
371 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
373 sa1111_writel(0, sadev->mapbase + SA1111_INTPOL0);
374 sa1111_writel(SA1111_IRQMASK_HI(IRQ_S0_READY_NINT) |
375 SA1111_IRQMASK_HI(IRQ_S1_READY_NINT),
376 sadev->mapbase + SA1111_INTPOL1);
379 sa1111_writel(~0, sadev->mapbase + SA1111_INTSTATCLR0);
380 sa1111_writel(~0, sadev->mapbase + SA1111_INTSTATCLR1);
382 for (irq = IRQ_GPAIN0; irq <= SSPROR; irq++) {
383 set_irq_chip(irq, &sa1111_low_chip);
384 set_irq_handler(irq, do_edge_IRQ);
385 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
388 for (irq = AUDXMTDMADONEA; irq <= IRQ_S1_BVD1_STSCHG; irq++) {
389 set_irq_chip(irq, &sa1111_high_chip);
390 set_irq_handler(irq, do_edge_IRQ);
391 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
395 * Register SA1111 interrupt
397 set_irq_type(sadev->irq[0], IRQT_RISING);
398 set_irq_chained_handler(sadev->irq[0], sa1111_irq_handler);
402 * Bring the SA1111 out of reset. This requires a set procedure:
403 * 1. nRESET asserted (by hardware)
404 * 2. CLK turned on from SA1110
405 * 3. nRESET deasserted
406 * 4. VCO turned on, PLL_BYPASS turned off
407 * 5. Wait lock time, then assert RCLKEn
408 * 7. PCR set to allow clocking of individual functions
410 * Until we've done this, the only registers we can access are:
415 static void sa1111_wake(struct sa1111 *sachip)
417 unsigned long flags, r;
419 spin_lock_irqsave(&sachip->lock, flags);
421 #ifdef CONFIG_ARCH_SA1100
423 * First, set up the 3.6864MHz clock on GPIO 27 for the SA-1111:
424 * (SA-1110 Developer's Manual, section 9.1.2.1)
426 GAFR |= GPIO_32_768kHz;
427 GPDR |= GPIO_32_768kHz;
428 TUCR = TUCR_3_6864MHz;
429 #elif CONFIG_ARCH_PXA
430 pxa_gpio_mode(GPIO11_3_6MHz_MD);
432 #error missing clock setup
436 * Turn VCO on, and disable PLL Bypass.
438 r = sa1111_readl(sachip->base + SA1111_SKCR);
440 sa1111_writel(r, sachip->base + SA1111_SKCR);
441 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
442 sa1111_writel(r, sachip->base + SA1111_SKCR);
445 * Wait lock time. SA1111 manual _doesn't_
446 * specify a figure for this! We choose 100us.
451 * Enable RCLK. We also ensure that RDYEN is set.
453 r |= SKCR_RCLKEN | SKCR_RDYEN;
454 sa1111_writel(r, sachip->base + SA1111_SKCR);
457 * Wait 14 RCLK cycles for the chip to finish coming out
458 * of reset. (RCLK=24MHz). This is 590ns.
463 * Ensure all clocks are initially off.
465 sa1111_writel(0, sachip->base + SA1111_SKPCR);
467 spin_unlock_irqrestore(&sachip->lock, flags);
470 #ifdef CONFIG_ARCH_SA1100
472 static u32 sa1111_dma_mask[] = {
484 * Configure the SA1111 shared memory controller.
487 sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
488 unsigned int cas_latency)
490 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
492 if (cas_latency == 3)
495 sa1111_writel(smcr, sachip->base + SA1111_SMCR);
498 * Now clear the bits in the DMA mask to work around the SA1111
499 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
500 * Chip Specification Update, June 2000, Erratum #7).
502 if (sachip->dev->dma_mask)
503 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
509 sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
510 struct sa1111_dev *sadev, unsigned int offset)
512 snprintf(sadev->dev.bus_id, sizeof(sadev->dev.bus_id),
516 * If the parent device has a DMA mask associated with it,
517 * propagate it down to the children.
519 if (sachip->dev->dma_mask) {
520 sadev->dma_mask = *sachip->dev->dma_mask;
521 sadev->dev.dma_mask = &sadev->dma_mask;
524 sadev->dev.parent = sachip->dev;
525 sadev->dev.bus = &sa1111_bus_type;
526 sadev->res.start = sachip->phys + offset;
527 sadev->res.end = sadev->res.start + 511;
528 sadev->res.name = sadev->dev.name;
529 sadev->res.flags = IORESOURCE_MEM;
530 sadev->mapbase = sachip->base + offset;
532 if (request_resource(parent, &sadev->res)) {
533 printk("SA1111: failed to allocate resource for %s\n",
538 device_register(&sadev->dev);
542 * sa1111_probe - probe for a single SA1111 chip.
543 * @phys_addr: physical address of device.
545 * Probe for a SA1111 chip. This must be called
546 * before any other SA1111-specific code.
549 * %-ENODEV device not found.
550 * %-EBUSY physical address already marked in-use.
554 __sa1111_probe(struct device *me, struct resource *mem, int irq)
556 struct sa1111 *sachip;
558 unsigned int has_devs, val;
559 int i, ret = -ENODEV;
561 sachip = kmalloc(sizeof(struct sa1111), GFP_KERNEL);
565 memset(sachip, 0, sizeof(struct sa1111));
567 spin_lock_init(&sachip->lock);
570 dev_set_drvdata(sachip->dev, sachip);
572 sachip->phys = mem->start;
576 * Map the whole region. This also maps the
577 * registers for our children.
579 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
586 * Probe for the chip. Only touch the SBI registers.
588 id = sa1111_readl(sachip->base + SA1111_SKID);
589 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
590 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
595 printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
596 "silicon revision %lx, metal revision %lx\n",
597 (id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));
600 * We found it. Wake the chip up, and initialise.
604 #ifdef CONFIG_ARCH_SA1100
606 * The SDRAM configuration of the SA1110 and the SA1111 must
607 * match. This is very important to ensure that SA1111 accesses
608 * don't corrupt the SDRAM. Note that this ungates the SA1111's
609 * MBGNT signal, so we must have called sa1110_mb_disable()
612 sa1111_configure_smc(sachip, 1,
613 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
614 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
617 * We only need to turn on DCLK whenever we want to use the
618 * DMA. It can otherwise be held firmly in the off position.
619 * (currently, we always enable it.)
621 val = sa1111_readl(sachip->base + SA1111_SKPCR);
622 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
625 * Enable the SA1110 memory bus request and grant signals.
631 * The interrupt controller must be initialised before any
632 * other device to ensure that the interrupts are available.
635 int_dev.irq[0] = irq;
636 sa1111_init_one_child(sachip, mem, &int_dev, SA1111_INTC);
637 sa1111_init_irq(&int_dev);
643 if (machine_is_assabet() || machine_is_jornada720() ||
645 has_devs &= ~(1 << 4);
647 has_devs &= ~(1 << 1);
649 for (i = 0; i < ARRAY_SIZE(devs); i++)
650 if (has_devs & (1 << i))
651 sa1111_init_one_child(sachip, mem, devs[i], dev_offset[i]);
656 iounmap(sachip->base);
662 static void __sa1111_remove(struct sa1111 *sachip)
666 for (i = 0; i < ARRAY_SIZE(devs); i++) {
667 put_device(&devs[i]->dev);
668 release_resource(&devs[i]->res);
671 iounmap(sachip->base);
676 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
677 * Chip Specification Update" (June 2000), erratum #7, there is a
678 * significant bug in the SA1111 SDRAM shared memory controller. If
679 * an access to a region of memory above 1MB relative to the bank base,
680 * it is important that address bit 10 _NOT_ be asserted. Depending
681 * on the configuration of the RAM, bit 10 may correspond to one
682 * of several different (processor-relative) address bits.
684 * This routine only identifies whether or not a given DMA address
685 * is susceptible to the bug.
687 int sa1111_check_dma_bug(dma_addr_t addr)
689 struct sa1111 *sachip = g_sa1111;
690 unsigned int physaddr = SA1111_DMA_ADDR((unsigned int)addr);
693 /* Section 4.6 of the "Intel StrongARM SA-1111 Development Module
694 * User's Guide" mentions that jumpers R51 and R52 control the
695 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
696 * SDRAM bank 1 on Neponset). The default configuration selects
697 * Assabet, so any address in bank 1 is necessarily invalid.
699 if ((machine_is_assabet() || machine_is_pfs168()) && addr >= 0xc8000000)
702 /* The bug only applies to buffers located more than one megabyte
703 * above the start of the target bank:
705 if (physaddr<(1<<20))
708 smcr = sa1111_readl(sachip->base + SA1111_SMCR);
709 switch (FExtr(smcr, SMCR_DRAC)) {
710 case 01: /* 10 row + bank address bits, A<20> must not be set */
711 if (physaddr & (1<<20))
714 case 02: /* 11 row + bank address bits, A<23> must not be set */
715 if (physaddr & (1<<23))
718 case 03: /* 12 row + bank address bits, A<24> must not be set */
719 if (physaddr & (1<<24))
722 case 04: /* 13 row + bank address bits, A<25> must not be set */
723 if (physaddr & (1<<25))
726 case 05: /* 14 row + bank address bits, A<20> must not be set */
727 if (physaddr & (1<<20))
730 case 06: /* 15 row + bank address bits, A<20> must not be set */
731 if (physaddr & (1<<20))
735 printk(KERN_ERR "%s(): invalid SMCR DRAC value 0%lo\n",
736 __FUNCTION__, FExtr(smcr, SMCR_DRAC));
743 struct sa1111_save_data {
748 unsigned char skpwm0;
749 unsigned char skpwm1;
752 * Interrupt controller
754 unsigned int intpol0;
755 unsigned int intpol1;
758 unsigned int wakepol0;
759 unsigned int wakepol1;
760 unsigned int wakeen0;
761 unsigned int wakeen1;
764 static int sa1111_suspend(struct device *dev, u32 state, u32 level)
766 struct sa1111 *sachip = dev_get_drvdata(dev);
767 struct sa1111_save_data *save;
771 if (!dev->saved_state && level == SUSPEND_NOTIFY)
772 dev->saved_state = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
773 if (!dev->saved_state)
776 save = (struct sa1111_save_data *)dev->saved_state;
778 spin_lock_irqsave(&sachip->lock, flags);
783 if (level == SUSPEND_SAVE_STATE) {
785 save->skcr = sa1111_readl(base + SA1111_SKCR);
786 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
787 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
788 save->skaud = sa1111_readl(base + SA1111_SKAUD);
789 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
790 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
792 base = sachip->base + SA1111_INTC;
793 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
794 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
795 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
796 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
797 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
798 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
799 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
800 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
806 if (level == SUSPEND_POWER_DOWN && state == 4) {
807 unsigned int val = sa1111_readl(sachip->base + SA1111_SKCR);
809 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
810 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
811 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
814 spin_unlock_irqrestore(&sachip->lock, flags);
820 * sa1111_resume - Restore the SA1111 device state.
821 * @dev: device to restore
822 * @level: resume level
824 * Restore the general state of the SA1111; clock control and
825 * interrupt controller. Other parts of the SA1111 must be
826 * restored by their respective drivers, and must be called
827 * via LDM after this function.
829 static int sa1111_resume(struct device *dev, u32 level)
831 struct sa1111 *sachip = dev_get_drvdata(dev);
832 struct sa1111_save_data *save;
833 unsigned long flags, id;
836 save = (struct sa1111_save_data *)dev->saved_state;
840 spin_lock_irqsave(&sachip->lock, flags);
843 * Ensure that the SA1111 is still here.
844 * FIXME: shouldn't do this here.
846 id = sa1111_readl(sachip->base + SA1111_SKID);
847 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
848 __sa1111_remove(sachip);
849 dev_set_drvdata(dev, NULL);
855 * First of all, wake up the chip.
857 if (level == RESUME_POWER_ON) {
860 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
861 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
864 if (level == RESUME_RESTORE_STATE) {
866 sa1111_writel(save->skcr, base + SA1111_SKCR);
867 sa1111_writel(save->skpcr, base + SA1111_SKPCR);
868 sa1111_writel(save->skcdr, base + SA1111_SKCDR);
869 sa1111_writel(save->skaud, base + SA1111_SKAUD);
870 sa1111_writel(save->skpwm0, base + SA1111_SKPWM0);
871 sa1111_writel(save->skpwm1, base + SA1111_SKPWM1);
873 base = sachip->base + SA1111_INTC;
874 sa1111_writel(save->intpol0, base + SA1111_INTPOL0);
875 sa1111_writel(save->intpol1, base + SA1111_INTPOL1);
876 sa1111_writel(save->inten0, base + SA1111_INTEN0);
877 sa1111_writel(save->inten1, base + SA1111_INTEN1);
878 sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
879 sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
880 sa1111_writel(save->wakeen0, base + SA1111_WAKEEN0);
881 sa1111_writel(save->wakeen1, base + SA1111_WAKEEN1);
884 spin_unlock_irqrestore(&sachip->lock, flags);
886 if (level == RESUME_ENABLE) {
887 dev->saved_state = NULL;
894 static int sa1111_probe(struct device *dev)
896 struct platform_device *pdev = to_platform_device(dev);
897 struct resource *mem = NULL, *irq = NULL;
900 for (i = 0; i < pdev->num_resources; i++) {
901 if (pdev->resource[i].flags & IORESOURCE_MEM)
902 mem = &pdev->resource[i];
903 if (pdev->resource[i].flags & IORESOURCE_IRQ)
904 irq = &pdev->resource[i];
906 return __sa1111_probe(dev, mem, irq ? irq->start : NO_IRQ);
909 static int sa1111_remove(struct device *dev)
911 struct sa1111 *sachip = dev_get_drvdata(dev);
914 __sa1111_remove(sachip);
915 dev_set_drvdata(dev, NULL);
917 kfree(dev->saved_state);
918 dev->saved_state = NULL;
925 * Not sure if this should be on the system bus or not yet.
926 * We really want some way to register a system device at
927 * the per-machine level, and then have this driver pick
928 * up the registered devices.
930 * We also need to handle the SDRAM configuration for
931 * PXA250/SA1110 machine classes.
933 static struct device_driver sa1111_device_driver = {
935 .bus = &platform_bus_type,
936 .probe = sa1111_probe,
937 .remove = sa1111_remove,
938 .suspend = sa1111_suspend,
939 .resume = sa1111_resume,
943 * Register the SA1111 driver with LDM.
945 static int sa1111_driver_init(void)
947 driver_register(&sa1111_device_driver);
951 arch_initcall(sa1111_driver_init);
954 * Get the parent device driver (us) structure
955 * from a child function device
957 static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
959 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
963 * The bits in the opdiv field are non-linear.
965 static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
967 static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
969 unsigned int skcdr, fbdiv, ipdiv, opdiv;
971 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
973 fbdiv = (skcdr & 0x007f) + 2;
974 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
975 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
977 return 3686400 * fbdiv / (ipdiv * opdiv);
981 * sa1111_pll_clock - return the current PLL clock frequency.
982 * @sadev: SA1111 function block
984 * BUG: we should look at SKCR. We also blindly believe that
985 * the chip is being fed with the 3.6864MHz clock.
987 * Returns the PLL clock in Hz.
989 unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
991 struct sa1111 *sachip = sa1111_chip_driver(sadev);
993 return __sa1111_pll_clock(sachip);
997 * sa1111_select_audio_mode - select I2S or AC link mode
998 * @sadev: SA1111 function block
999 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1001 * Frob the SKCR to select AC Link mode or I2S mode for
1004 void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1006 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1007 unsigned long flags;
1010 spin_lock_irqsave(&sachip->lock, flags);
1012 val = sa1111_readl(sachip->base + SA1111_SKCR);
1013 if (mode == SA1111_AUDIO_I2S) {
1018 sa1111_writel(val, sachip->base + SA1111_SKCR);
1020 spin_unlock_irqrestore(&sachip->lock, flags);
1024 * sa1111_set_audio_rate - set the audio sample rate
1025 * @sadev: SA1111 SAC function block
1026 * @rate: sample rate to select
1028 int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1030 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1033 if (sadev->devid != SA1111_DEVID_SAC)
1036 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1042 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1048 * sa1111_get_audio_rate - get the audio sample rate
1049 * @sadev: SA1111 SAC function block device
1051 int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1053 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1056 if (sadev->devid != SA1111_DEVID_SAC)
1059 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1061 return __sa1111_pll_clock(sachip) / (256 * div);
1065 * Individual device operations.
1069 * sa1111_enable_device - enable an on-chip SA1111 function block
1070 * @sadev: SA1111 function block device to enable
1072 void sa1111_enable_device(struct sa1111_dev *sadev)
1074 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1075 unsigned long flags;
1078 spin_lock_irqsave(&sachip->lock, flags);
1079 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1080 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1081 spin_unlock_irqrestore(&sachip->lock, flags);
1085 * sa1111_disable_device - disable an on-chip SA1111 function block
1086 * @sadev: SA1111 function block device to disable
1088 void sa1111_disable_device(struct sa1111_dev *sadev)
1090 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1091 unsigned long flags;
1094 spin_lock_irqsave(&sachip->lock, flags);
1095 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1096 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1097 spin_unlock_irqrestore(&sachip->lock, flags);
1101 * SA1111 "Register Access Bus."
1103 * We model this as a regular bus type, and hang devices directly
1106 static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1108 struct sa1111_dev *dev = SA1111_DEV(_dev);
1109 struct sa1111_driver *drv = SA1111_DRV(_drv);
1111 return dev->devid == drv->devid;
1114 struct bus_type sa1111_bus_type = {
1116 .match = sa1111_match,
1119 static int sa1111_rab_bus_init(void)
1121 return bus_register(&sa1111_bus_type);
1124 postcore_initcall(sa1111_rab_bus_init);
1126 EXPORT_SYMBOL(sa1111_check_dma_bug);
1127 EXPORT_SYMBOL(sa1111_select_audio_mode);
1128 EXPORT_SYMBOL(sa1111_set_audio_rate);
1129 EXPORT_SYMBOL(sa1111_get_audio_rate);
1130 EXPORT_SYMBOL(sa1111_enable_device);
1131 EXPORT_SYMBOL(sa1111_disable_device);
1132 EXPORT_SYMBOL(sa1111_pll_clock);
1133 EXPORT_SYMBOL(sa1111_bus_type);