2 * linux/arch/arm/common/locomo.c
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This file contains all generic LoCoMo support.
12 * All initialization functions provided here are intended to be called
13 * from machine specific code with proper arguments when required.
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.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>
29 #include <asm/hardware.h>
30 #include <asm/mach-types.h>
33 #include <asm/mach/irq.h>
35 #include <asm/hardware/locomo.h>
37 /* the following is the overall data for the locomo chip */
45 struct locomo_dev_info {
53 static struct locomo_dev_info locomo_devices[] = {
57 /** LoCoMo interrupt handling stuff.
58 * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
59 * that is, there is only one real hardware interrupt
60 * we determine which interrupt it is by reading some IO memory.
61 * We have two levels of expansion, first in the handler for the
62 * hardware interrupt we generate an interrupt
63 * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
65 * hardware irq reads LOCOMO_ICR & 0x0f00
67 * IRQ_LOCOMO_GPIO_BASE
70 * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
72 * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
73 * IRQ_LOCOMO_GPIO[0-15]
74 * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
76 * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
83 #define LOCOMO_IRQ_START (IRQ_LOCOMO_KEY_BASE)
84 #define LOCOMO_IRQ_KEY_START (IRQ_LOCOMO_KEY)
85 #define LOCOMO_IRQ_GPIO_START (IRQ_LOCOMO_GPIO0)
86 #define LOCOMO_IRQ_LT_START (IRQ_LOCOMO_LT)
87 #define LOCOMO_IRQ_SPI_START (IRQ_LOCOMO_SPI_RFR)
89 static void locomo_handler(unsigned int irq, struct irqdesc *desc,
94 void *mapbase = get_irq_chipdata(irq);
96 /* Acknowledge the parent IRQ */
99 /* check why this interrupt was generated */
100 req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
103 /* generate the next interrupt(s) */
104 irq = LOCOMO_IRQ_START;
106 for (i = 0; i <= 3; i++, d++, irq++) {
107 if (req & (0x0100 << i)) {
108 d->handle(irq, d, regs);
115 static void locomo_ack_irq(unsigned int irq)
119 static void locomo_mask_irq(unsigned int irq)
121 void *mapbase = get_irq_chipdata(irq);
123 r = locomo_readl(mapbase + LOCOMO_ICR);
124 r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
125 locomo_writel(r, mapbase + LOCOMO_ICR);
128 static void locomo_unmask_irq(unsigned int irq)
130 void *mapbase = get_irq_chipdata(irq);
132 r = locomo_readl(mapbase + LOCOMO_ICR);
133 r |= (0x0010 << (irq - LOCOMO_IRQ_START));
134 locomo_writel(r, mapbase + LOCOMO_ICR);
137 static struct irqchip locomo_chip = {
138 .ack = locomo_ack_irq,
139 .mask = locomo_mask_irq,
140 .unmask = locomo_unmask_irq,
143 static void locomo_key_handler(unsigned int irq, struct irqdesc *desc,
144 struct pt_regs *regs)
147 void *mapbase = get_irq_chipdata(irq);
149 if (locomo_readl(mapbase + LOCOMO_KIC) & 0x0001) {
150 d = irq_desc + LOCOMO_IRQ_KEY_START;
151 d->handle(LOCOMO_IRQ_KEY_START, d, regs);
155 static void locomo_key_ack_irq(unsigned int irq)
157 void *mapbase = get_irq_chipdata(irq);
159 r = locomo_readl(mapbase + LOCOMO_KIC);
160 r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
161 locomo_writel(r, mapbase + LOCOMO_KIC);
164 static void locomo_key_mask_irq(unsigned int irq)
166 void *mapbase = get_irq_chipdata(irq);
168 r = locomo_readl(mapbase + LOCOMO_KIC);
169 r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
170 locomo_writel(r, mapbase + LOCOMO_KIC);
173 static void locomo_key_unmask_irq(unsigned int irq)
175 void *mapbase = get_irq_chipdata(irq);
177 r = locomo_readl(mapbase + LOCOMO_KIC);
178 r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
179 locomo_writel(r, mapbase + LOCOMO_KIC);
182 static struct irqchip locomo_key_chip = {
183 .ack = locomo_key_ack_irq,
184 .mask = locomo_key_mask_irq,
185 .unmask = locomo_key_unmask_irq,
188 static void locomo_gpio_handler(unsigned int irq, struct irqdesc *desc,
189 struct pt_regs *regs)
193 void *mapbase = get_irq_chipdata(irq);
195 req = locomo_readl(mapbase + LOCOMO_GIR) &
196 locomo_readl(mapbase + LOCOMO_GPD) &
200 irq = LOCOMO_IRQ_GPIO_START;
201 d = irq_desc + LOCOMO_IRQ_GPIO_START;
202 for (i = 0; i <= 15; i++, irq++, d++) {
203 if (req & (0x0001 << i)) {
204 d->handle(irq, d, regs);
210 static void locomo_gpio_ack_irq(unsigned int irq)
212 void *mapbase = get_irq_chipdata(irq);
214 r = locomo_readl(mapbase + LOCOMO_GWE);
215 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
216 locomo_writel(r, mapbase + LOCOMO_GWE);
218 r = locomo_readl(mapbase + LOCOMO_GIS);
219 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
220 locomo_writel(r, mapbase + LOCOMO_GIS);
222 r = locomo_readl(mapbase + LOCOMO_GWE);
223 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
224 locomo_writel(r, mapbase + LOCOMO_GWE);
227 static void locomo_gpio_mask_irq(unsigned int irq)
229 void *mapbase = get_irq_chipdata(irq);
231 r = locomo_readl(mapbase + LOCOMO_GIE);
232 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
233 locomo_writel(r, mapbase + LOCOMO_GIE);
236 static void locomo_gpio_unmask_irq(unsigned int irq)
238 void *mapbase = get_irq_chipdata(irq);
240 r = locomo_readl(mapbase + LOCOMO_GIE);
241 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
242 locomo_writel(r, mapbase + LOCOMO_GIE);
245 static struct irqchip locomo_gpio_chip = {
246 .ack = locomo_gpio_ack_irq,
247 .mask = locomo_gpio_mask_irq,
248 .unmask = locomo_gpio_unmask_irq,
251 static void locomo_lt_handler(unsigned int irq, struct irqdesc *desc,
252 struct pt_regs *regs)
255 void *mapbase = get_irq_chipdata(irq);
257 if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
258 d = irq_desc + LOCOMO_IRQ_LT_START;
259 d->handle(LOCOMO_IRQ_LT_START, d, regs);
263 static void locomo_lt_ack_irq(unsigned int irq)
265 void *mapbase = get_irq_chipdata(irq);
267 r = locomo_readl(mapbase + LOCOMO_LTINT);
268 r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
269 locomo_writel(r, mapbase + LOCOMO_LTINT);
272 static void locomo_lt_mask_irq(unsigned int irq)
274 void *mapbase = get_irq_chipdata(irq);
276 r = locomo_readl(mapbase + LOCOMO_LTINT);
277 r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
278 locomo_writel(r, mapbase + LOCOMO_LTINT);
281 static void locomo_lt_unmask_irq(unsigned int irq)
283 void *mapbase = get_irq_chipdata(irq);
285 r = locomo_readl(mapbase + LOCOMO_LTINT);
286 r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
287 locomo_writel(r, mapbase + LOCOMO_LTINT);
290 static struct irqchip locomo_lt_chip = {
291 .ack = locomo_lt_ack_irq,
292 .mask = locomo_lt_mask_irq,
293 .unmask = locomo_lt_unmask_irq,
296 static void locomo_spi_handler(unsigned int irq, struct irqdesc *desc,
297 struct pt_regs *regs)
301 void *mapbase = get_irq_chipdata(irq);
303 req = locomo_readl(mapbase + LOCOMO_SPIIR) & 0x000F;
305 irq = LOCOMO_IRQ_SPI_START;
308 for (i = 0; i <= 3; i++, irq++, d++) {
309 if (req & (0x0001 << i)) {
310 d->handle(irq, d, regs);
316 static void locomo_spi_ack_irq(unsigned int irq)
318 void *mapbase = get_irq_chipdata(irq);
320 r = locomo_readl(mapbase + LOCOMO_SPIWE);
321 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
322 locomo_writel(r, mapbase + LOCOMO_SPIWE);
324 r = locomo_readl(mapbase + LOCOMO_SPIIS);
325 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
326 locomo_writel(r, mapbase + LOCOMO_SPIIS);
328 r = locomo_readl(mapbase + LOCOMO_SPIWE);
329 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
330 locomo_writel(r, mapbase + LOCOMO_SPIWE);
333 static void locomo_spi_mask_irq(unsigned int irq)
335 void *mapbase = get_irq_chipdata(irq);
337 r = locomo_readl(mapbase + LOCOMO_SPIIE);
338 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
339 locomo_writel(r, mapbase + LOCOMO_SPIIE);
342 static void locomo_spi_unmask_irq(unsigned int irq)
344 void *mapbase = get_irq_chipdata(irq);
346 r = locomo_readl(mapbase + LOCOMO_SPIIE);
347 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
348 locomo_writel(r, mapbase + LOCOMO_SPIIE);
351 static struct irqchip locomo_spi_chip = {
352 .ack = locomo_spi_ack_irq,
353 .mask = locomo_spi_mask_irq,
354 .unmask = locomo_spi_unmask_irq,
357 static void locomo_setup_irq(struct locomo *lchip)
360 void *irqbase = lchip->base;
363 * Install handler for IRQ_LOCOMO_HW.
365 set_irq_type(lchip->irq, IRQT_FALLING);
366 set_irq_chipdata(lchip->irq, irqbase);
367 set_irq_chained_handler(lchip->irq, locomo_handler);
369 /* Install handlers for IRQ_LOCOMO_*_BASE */
370 set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
371 set_irq_chipdata(IRQ_LOCOMO_KEY_BASE, irqbase);
372 set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
373 set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
375 set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
376 set_irq_chipdata(IRQ_LOCOMO_GPIO_BASE, irqbase);
377 set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
378 set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
380 set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
381 set_irq_chipdata(IRQ_LOCOMO_LT_BASE, irqbase);
382 set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
383 set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
385 set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
386 set_irq_chipdata(IRQ_LOCOMO_SPI_BASE, irqbase);
387 set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
388 set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
390 /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
391 set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
392 set_irq_chipdata(LOCOMO_IRQ_KEY_START, irqbase);
393 set_irq_handler(LOCOMO_IRQ_KEY_START, do_edge_IRQ);
394 set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
396 /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
397 for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
398 set_irq_chip(irq, &locomo_gpio_chip);
399 set_irq_chipdata(irq, irqbase);
400 set_irq_handler(irq, do_edge_IRQ);
401 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
404 /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
405 set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
406 set_irq_chipdata(LOCOMO_IRQ_LT_START, irqbase);
407 set_irq_handler(LOCOMO_IRQ_LT_START, do_edge_IRQ);
408 set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
410 /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
411 for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
412 set_irq_chip(irq, &locomo_spi_chip);
413 set_irq_chipdata(irq, irqbase);
414 set_irq_handler(irq, do_edge_IRQ);
415 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
420 static void locomo_dev_release(struct device *_dev)
422 struct locomo_dev *dev = LOCOMO_DEV(_dev);
424 release_resource(&dev->res);
429 locomo_init_one_child(struct locomo *lchip, struct resource *parent,
430 struct locomo_dev_info *info)
432 struct locomo_dev *dev;
435 dev = kmalloc(sizeof(struct locomo_dev), GFP_KERNEL);
440 memset(dev, 0, sizeof(struct locomo_dev));
442 strncpy(dev->dev.bus_id,info->name,sizeof(dev->dev.bus_id));
444 * If the parent device has a DMA mask associated with it,
445 * propagate it down to the children.
447 if (lchip->dev->dma_mask) {
448 dev->dma_mask = *lchip->dev->dma_mask;
449 dev->dev.dma_mask = &dev->dma_mask;
452 dev->devid = info->devid;
453 dev->dev.parent = lchip->dev;
454 dev->dev.bus = &locomo_bus_type;
455 dev->dev.release = locomo_dev_release;
456 dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
457 dev->res.start = lchip->phys + info->offset;
458 dev->res.end = dev->res.start + info->length;
459 dev->res.name = dev->dev.bus_id;
460 dev->res.flags = IORESOURCE_MEM;
461 dev->mapbase = lchip->base + info->offset;
462 memmove(dev->irq, info->irq, sizeof(dev->irq));
465 ret = request_resource(parent, &dev->res);
467 printk("LoCoMo: failed to allocate resource for %s\n",
473 ret = device_register(&dev->dev);
475 release_resource(&dev->res);
483 * locomo_probe - probe for a single LoCoMo chip.
484 * @phys_addr: physical address of device.
486 * Probe for a LoCoMo chip. This must be called
487 * before any other locomo-specific code.
490 * %-ENODEV device not found.
491 * %-EBUSY physical address already marked in-use.
495 __locomo_probe(struct device *me, struct resource *mem, int irq)
497 struct locomo *lchip;
499 int i, ret = -ENODEV;
501 lchip = kmalloc(sizeof(struct locomo), GFP_KERNEL);
505 memset(lchip, 0, sizeof(struct locomo));
508 dev_set_drvdata(lchip->dev, lchip);
510 lchip->phys = mem->start;
514 * Map the whole region. This also maps the
515 * registers for our children.
517 lchip->base = ioremap(mem->start, PAGE_SIZE);
523 /* locomo initialize */
524 locomo_writel(0, lchip->base + LOCOMO_ICR);
526 locomo_writel(0, lchip->base + LOCOMO_KIC);
529 locomo_writel(0, lchip->base + LOCOMO_GPO);
530 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
531 , lchip->base + LOCOMO_GPE);
532 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
533 , lchip->base + LOCOMO_GPD);
534 locomo_writel(0, lchip->base + LOCOMO_GIE);
537 locomo_writel(0, lchip->base + LOCOMO_ALS);
538 locomo_writel(0, lchip->base + LOCOMO_ALD);
540 locomo_writel(0, lchip->base + LOCOMO_LTINT);
542 locomo_writel(0, lchip->base + LOCOMO_SPIIE);
544 locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
545 r = locomo_readl(lchip->base + LOCOMO_ASD);
547 locomo_writel(r, lchip->base + LOCOMO_ASD);
549 locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
550 r = locomo_readl(lchip->base + LOCOMO_HSD);
552 locomo_writel(r, lchip->base + LOCOMO_HSD);
554 locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
557 locomo_writel(0x80, lchip->base + LOCOMO_TADC);
560 r = locomo_readl(lchip->base + LOCOMO_TADC);
562 locomo_writel(r, lchip->base + LOCOMO_TADC);
566 r = locomo_readl(lchip->base + LOCOMO_DAC);
567 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
568 locomo_writel(r, lchip->base + LOCOMO_DAC);
570 r = locomo_readl(lchip->base + LOCOMO_VER);
571 printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
574 * The interrupt controller must be initialised before any
575 * other device to ensure that the interrupts are available.
577 if (lchip->irq != NO_IRQ)
578 locomo_setup_irq(lchip);
580 for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
581 locomo_init_one_child(lchip, mem, &locomo_devices[i]);
590 static void __locomo_remove(struct locomo *lchip)
592 struct list_head *l, *n;
594 list_for_each_safe(l, n, &lchip->dev->children) {
595 struct device *d = list_to_dev(l);
597 device_unregister(d);
600 if (lchip->irq != NO_IRQ) {
601 set_irq_chained_handler(lchip->irq, NULL);
602 set_irq_data(lchip->irq, NULL);
605 iounmap(lchip->base);
609 static int locomo_probe(struct device *dev)
611 struct platform_device *pdev = to_platform_device(dev);
612 struct resource *mem;
615 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
618 irq = platform_get_irq(pdev, 0);
620 return __locomo_probe(dev, mem, irq);
623 static int locomo_remove(struct device *dev)
625 struct locomo *lchip = dev_get_drvdata(dev);
628 __locomo_remove(lchip);
629 dev_set_drvdata(dev, NULL);
636 * Not sure if this should be on the system bus or not yet.
637 * We really want some way to register a system device at
638 * the per-machine level, and then have this driver pick
639 * up the registered devices.
641 static struct device_driver locomo_device_driver = {
643 .bus = &platform_bus_type,
644 .probe = locomo_probe,
645 .remove = locomo_remove,
649 * Get the parent device driver (us) structure
650 * from a child function device
652 static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
654 return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
658 * LoCoMo "Register Access Bus."
660 * We model this as a regular bus type, and hang devices directly
663 static int locomo_match(struct device *_dev, struct device_driver *_drv)
665 struct locomo_dev *dev = LOCOMO_DEV(_dev);
666 struct locomo_driver *drv = LOCOMO_DRV(_drv);
668 return dev->devid == drv->devid;
671 static int locomo_bus_suspend(struct device *dev, u32 state)
673 struct locomo_dev *ldev = LOCOMO_DEV(dev);
674 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
677 if (drv && drv->suspend)
678 ret = drv->suspend(ldev, state);
682 static int locomo_bus_resume(struct device *dev)
684 struct locomo_dev *ldev = LOCOMO_DEV(dev);
685 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
688 if (drv && drv->resume)
689 ret = drv->resume(ldev);
693 static int locomo_bus_probe(struct device *dev)
695 struct locomo_dev *ldev = LOCOMO_DEV(dev);
696 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
700 ret = drv->probe(ldev);
704 static int locomo_bus_remove(struct device *dev)
706 struct locomo_dev *ldev = LOCOMO_DEV(dev);
707 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
711 ret = drv->remove(ldev);
715 struct bus_type locomo_bus_type = {
716 .name = "locomo-bus",
717 .match = locomo_match,
718 .suspend = locomo_bus_suspend,
719 .resume = locomo_bus_resume,
722 int locomo_driver_register(struct locomo_driver *driver)
724 driver->drv.probe = locomo_bus_probe;
725 driver->drv.remove = locomo_bus_remove;
726 driver->drv.bus = &locomo_bus_type;
727 return driver_register(&driver->drv);
730 void locomo_driver_unregister(struct locomo_driver *driver)
732 driver_unregister(&driver->drv);
735 static int __init locomo_init(void)
737 int ret = bus_register(&locomo_bus_type);
739 driver_register(&locomo_device_driver);
743 static void __exit locomo_exit(void)
745 driver_unregister(&locomo_device_driver);
746 bus_unregister(&locomo_bus_type);
749 module_init(locomo_init);
750 module_exit(locomo_exit);
752 MODULE_DESCRIPTION("Sharp LoCoMo core driver");
753 MODULE_LICENSE("GPL");
754 MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
756 EXPORT_SYMBOL(locomo_driver_register);
757 EXPORT_SYMBOL(locomo_driver_unregister);