Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/arch/arm/mach-sa1100/neponset.c
0004  */
0005 #include <linux/err.h>
0006 #include <linux/gpio/driver.h>
0007 #include <linux/gpio/gpio-reg.h>
0008 #include <linux/gpio/machine.h>
0009 #include <linux/init.h>
0010 #include <linux/ioport.h>
0011 #include <linux/irq.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/pm.h>
0016 #include <linux/serial_core.h>
0017 #include <linux/slab.h>
0018 #include <linux/smc91x.h>
0019 
0020 #include <asm/mach-types.h>
0021 #include <asm/mach/map.h>
0022 #include <asm/hardware/sa1111.h>
0023 #include <linux/sizes.h>
0024 
0025 #include <mach/hardware.h>
0026 #include <mach/assabet.h>
0027 #include <mach/neponset.h>
0028 #include <mach/irqs.h>
0029 
0030 #define NEP_IRQ_SMC91X  0
0031 #define NEP_IRQ_USAR    1
0032 #define NEP_IRQ_SA1111  2
0033 #define NEP_IRQ_NR  3
0034 
0035 #define WHOAMI      0x00
0036 #define LEDS        0x10
0037 #define SWPK        0x20
0038 #define IRR     0x24
0039 #define KP_Y_IN     0x80
0040 #define KP_X_OUT    0x90
0041 #define NCR_0       0xa0
0042 #define MDM_CTL_0   0xb0
0043 #define MDM_CTL_1   0xb4
0044 #define AUD_CTL     0xc0
0045 
0046 #define IRR_ETHERNET    (1 << 0)
0047 #define IRR_USAR    (1 << 1)
0048 #define IRR_SA1111  (1 << 2)
0049 
0050 #define NCR_NGPIO   7
0051 #define MDM_CTL0_NGPIO  4
0052 #define MDM_CTL1_NGPIO  6
0053 #define AUD_NGPIO   2
0054 
0055 extern void sa1110_mb_disable(void);
0056 
0057 #define to_neponset_gpio_chip(x) container_of(x, struct neponset_gpio_chip, gc)
0058 
0059 static const char *neponset_ncr_names[] = {
0060     "gp01_off", "tp_power", "ms_power", "enet_osc",
0061     "spi_kb_wk_up", "a0vpp", "a1vpp"
0062 };
0063 
0064 static const char *neponset_mdmctl0_names[] = {
0065     "rts3", "dtr3", "rts1", "dtr1",
0066 };
0067 
0068 static const char *neponset_mdmctl1_names[] = {
0069     "cts3", "dsr3", "dcd3", "cts1", "dsr1", "dcd1"
0070 };
0071 
0072 static const char *neponset_aud_names[] = {
0073     "sel_1341", "mute_1341",
0074 };
0075 
0076 struct neponset_drvdata {
0077     void __iomem *base;
0078     struct platform_device *sa1111;
0079     struct platform_device *smc91x;
0080     unsigned irq_base;
0081     struct gpio_chip *gpio[4];
0082 };
0083 
0084 static struct gpiod_lookup_table neponset_uart1_gpio_table = {
0085     .dev_id = "sa11x0-uart.1",
0086     .table = {
0087         GPIO_LOOKUP("neponset-mdm-ctl0", 2, "rts", GPIO_ACTIVE_LOW),
0088         GPIO_LOOKUP("neponset-mdm-ctl0", 3, "dtr", GPIO_ACTIVE_LOW),
0089         GPIO_LOOKUP("neponset-mdm-ctl1", 3, "cts", GPIO_ACTIVE_LOW),
0090         GPIO_LOOKUP("neponset-mdm-ctl1", 4, "dsr", GPIO_ACTIVE_LOW),
0091         GPIO_LOOKUP("neponset-mdm-ctl1", 5, "dcd", GPIO_ACTIVE_LOW),
0092         { },
0093     },
0094 };
0095 
0096 static struct gpiod_lookup_table neponset_uart3_gpio_table = {
0097     .dev_id = "sa11x0-uart.3",
0098     .table = {
0099         GPIO_LOOKUP("neponset-mdm-ctl0", 0, "rts", GPIO_ACTIVE_LOW),
0100         GPIO_LOOKUP("neponset-mdm-ctl0", 1, "dtr", GPIO_ACTIVE_LOW),
0101         GPIO_LOOKUP("neponset-mdm-ctl1", 0, "cts", GPIO_ACTIVE_LOW),
0102         GPIO_LOOKUP("neponset-mdm-ctl1", 1, "dsr", GPIO_ACTIVE_LOW),
0103         GPIO_LOOKUP("neponset-mdm-ctl1", 2, "dcd", GPIO_ACTIVE_LOW),
0104         { },
0105     },
0106 };
0107 
0108 static struct gpiod_lookup_table neponset_pcmcia_table = {
0109     .dev_id = "1800",
0110     .table = {
0111         GPIO_LOOKUP("sa1111", 1, "a0vcc", GPIO_ACTIVE_HIGH),
0112         GPIO_LOOKUP("sa1111", 0, "a1vcc", GPIO_ACTIVE_HIGH),
0113         GPIO_LOOKUP("neponset-ncr", 5, "a0vpp", GPIO_ACTIVE_HIGH),
0114         GPIO_LOOKUP("neponset-ncr", 6, "a1vpp", GPIO_ACTIVE_HIGH),
0115         GPIO_LOOKUP("sa1111", 2, "b0vcc", GPIO_ACTIVE_HIGH),
0116         GPIO_LOOKUP("sa1111", 3, "b1vcc", GPIO_ACTIVE_HIGH),
0117         { },
0118     },
0119 };
0120 
0121 static struct neponset_drvdata *nep;
0122 
0123 void neponset_ncr_frob(unsigned int mask, unsigned int val)
0124 {
0125     struct neponset_drvdata *n = nep;
0126     unsigned long m = mask, v = val;
0127 
0128     if (nep)
0129         n->gpio[0]->set_multiple(n->gpio[0], &m, &v);
0130     else
0131         WARN(1, "nep unset\n");
0132 }
0133 EXPORT_SYMBOL(neponset_ncr_frob);
0134 
0135 /*
0136  * Install handler for Neponset IRQ.  Note that we have to loop here
0137  * since the ETHERNET and USAR IRQs are level based, and we need to
0138  * ensure that the IRQ signal is deasserted before returning.  This
0139  * is rather unfortunate.
0140  */
0141 static void neponset_irq_handler(struct irq_desc *desc)
0142 {
0143     struct neponset_drvdata *d = irq_desc_get_handler_data(desc);
0144     unsigned int irr;
0145 
0146     while (1) {
0147         /*
0148          * Acknowledge the parent IRQ.
0149          */
0150         desc->irq_data.chip->irq_ack(&desc->irq_data);
0151 
0152         /*
0153          * Read the interrupt reason register.  Let's have all
0154          * active IRQ bits high.  Note: there is a typo in the
0155          * Neponset user's guide for the SA1111 IRR level.
0156          */
0157         irr = readb_relaxed(d->base + IRR);
0158         irr ^= IRR_ETHERNET | IRR_USAR;
0159 
0160         if ((irr & (IRR_ETHERNET | IRR_USAR | IRR_SA1111)) == 0)
0161             break;
0162 
0163         /*
0164          * Since there is no individual mask, we have to
0165          * mask the parent IRQ.  This is safe, since we'll
0166          * recheck the register for any pending IRQs.
0167          */
0168         if (irr & (IRR_ETHERNET | IRR_USAR)) {
0169             desc->irq_data.chip->irq_mask(&desc->irq_data);
0170 
0171             /*
0172              * Ack the interrupt now to prevent re-entering
0173              * this neponset handler.  Again, this is safe
0174              * since we'll check the IRR register prior to
0175              * leaving.
0176              */
0177             desc->irq_data.chip->irq_ack(&desc->irq_data);
0178 
0179             if (irr & IRR_ETHERNET)
0180                 generic_handle_irq(d->irq_base + NEP_IRQ_SMC91X);
0181 
0182             if (irr & IRR_USAR)
0183                 generic_handle_irq(d->irq_base + NEP_IRQ_USAR);
0184 
0185             desc->irq_data.chip->irq_unmask(&desc->irq_data);
0186         }
0187 
0188         if (irr & IRR_SA1111)
0189             generic_handle_irq(d->irq_base + NEP_IRQ_SA1111);
0190     }
0191 }
0192 
0193 /* Yes, we really do not have any kind of masking or unmasking */
0194 static void nochip_noop(struct irq_data *irq)
0195 {
0196 }
0197 
0198 static struct irq_chip nochip = {
0199     .name = "neponset",
0200     .irq_ack = nochip_noop,
0201     .irq_mask = nochip_noop,
0202     .irq_unmask = nochip_noop,
0203 };
0204 
0205 static int neponset_init_gpio(struct gpio_chip **gcp,
0206     struct device *dev, const char *label, void __iomem *reg,
0207     unsigned num, bool in, const char *const * names)
0208 {
0209     struct gpio_chip *gc;
0210 
0211     gc = gpio_reg_init(dev, reg, -1, num, label, in ? 0xffffffff : 0,
0212                readl_relaxed(reg), names, NULL, NULL);
0213     if (IS_ERR(gc))
0214         return PTR_ERR(gc);
0215 
0216     *gcp = gc;
0217 
0218     return 0;
0219 }
0220 
0221 static struct sa1111_platform_data sa1111_info = {
0222     .disable_devs   = SA1111_DEVID_PS2_MSE,
0223 };
0224 
0225 static int neponset_probe(struct platform_device *dev)
0226 {
0227     struct neponset_drvdata *d;
0228     struct resource *nep_res, *sa1111_res, *smc91x_res;
0229     struct resource sa1111_resources[] = {
0230         DEFINE_RES_MEM(0x40000000, SZ_8K),
0231         { .flags = IORESOURCE_IRQ },
0232     };
0233     struct platform_device_info sa1111_devinfo = {
0234         .parent = &dev->dev,
0235         .name = "sa1111",
0236         .id = 0,
0237         .res = sa1111_resources,
0238         .num_res = ARRAY_SIZE(sa1111_resources),
0239         .data = &sa1111_info,
0240         .size_data = sizeof(sa1111_info),
0241         .dma_mask = 0xffffffffUL,
0242     };
0243     struct resource smc91x_resources[] = {
0244         DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS,
0245             0x02000000, "smc91x-regs"),
0246         DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS + 0x02000000,
0247             0x02000000, "smc91x-attrib"),
0248         { .flags = IORESOURCE_IRQ },
0249     };
0250     struct smc91x_platdata smc91x_platdata = {
0251         .flags = SMC91X_USE_8BIT | SMC91X_IO_SHIFT_2 | SMC91X_NOWAIT,
0252     };
0253     struct platform_device_info smc91x_devinfo = {
0254         .parent = &dev->dev,
0255         .name = "smc91x",
0256         .id = 0,
0257         .res = smc91x_resources,
0258         .num_res = ARRAY_SIZE(smc91x_resources),
0259         .data = &smc91x_platdata,
0260         .size_data = sizeof(smc91x_platdata),
0261     };
0262     int ret, irq;
0263 
0264     if (nep)
0265         return -EBUSY;
0266 
0267     irq = ret = platform_get_irq(dev, 0);
0268     if (ret < 0)
0269         goto err_alloc;
0270 
0271     nep_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
0272     smc91x_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
0273     sa1111_res = platform_get_resource(dev, IORESOURCE_MEM, 2);
0274     if (!nep_res || !smc91x_res || !sa1111_res) {
0275         ret = -ENXIO;
0276         goto err_alloc;
0277     }
0278 
0279     d = kzalloc(sizeof(*d), GFP_KERNEL);
0280     if (!d) {
0281         ret = -ENOMEM;
0282         goto err_alloc;
0283     }
0284 
0285     d->base = ioremap(nep_res->start, SZ_4K);
0286     if (!d->base) {
0287         ret = -ENOMEM;
0288         goto err_ioremap;
0289     }
0290 
0291     if (readb_relaxed(d->base + WHOAMI) != 0x11) {
0292         dev_warn(&dev->dev, "Neponset board detected, but wrong ID: %02x\n",
0293              readb_relaxed(d->base + WHOAMI));
0294         ret = -ENODEV;
0295         goto err_id;
0296     }
0297 
0298     ret = irq_alloc_descs(-1, IRQ_BOARD_START, NEP_IRQ_NR, -1);
0299     if (ret <= 0) {
0300         dev_err(&dev->dev, "unable to allocate %u irqs: %d\n",
0301             NEP_IRQ_NR, ret);
0302         if (ret == 0)
0303             ret = -ENOMEM;
0304         goto err_irq_alloc;
0305     }
0306 
0307     d->irq_base = ret;
0308 
0309     irq_set_chip_and_handler(d->irq_base + NEP_IRQ_SMC91X, &nochip,
0310         handle_simple_irq);
0311     irq_clear_status_flags(d->irq_base + NEP_IRQ_SMC91X, IRQ_NOREQUEST | IRQ_NOPROBE);
0312     irq_set_chip_and_handler(d->irq_base + NEP_IRQ_USAR, &nochip,
0313         handle_simple_irq);
0314     irq_clear_status_flags(d->irq_base + NEP_IRQ_USAR, IRQ_NOREQUEST | IRQ_NOPROBE);
0315     irq_set_chip(d->irq_base + NEP_IRQ_SA1111, &nochip);
0316 
0317     irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
0318     irq_set_chained_handler_and_data(irq, neponset_irq_handler, d);
0319 
0320     /* Disable GPIO 0/1 drivers so the buttons work on the Assabet */
0321     writeb_relaxed(NCR_GP01_OFF, d->base + NCR_0);
0322 
0323     neponset_init_gpio(&d->gpio[0], &dev->dev, "neponset-ncr",
0324                d->base + NCR_0, NCR_NGPIO, false,
0325                neponset_ncr_names);
0326     neponset_init_gpio(&d->gpio[1], &dev->dev, "neponset-mdm-ctl0",
0327                d->base + MDM_CTL_0, MDM_CTL0_NGPIO, false,
0328                neponset_mdmctl0_names);
0329     neponset_init_gpio(&d->gpio[2], &dev->dev, "neponset-mdm-ctl1",
0330                d->base + MDM_CTL_1, MDM_CTL1_NGPIO, true,
0331                neponset_mdmctl1_names);
0332     neponset_init_gpio(&d->gpio[3], &dev->dev, "neponset-aud-ctl",
0333                d->base + AUD_CTL, AUD_NGPIO, false,
0334                neponset_aud_names);
0335 
0336     gpiod_add_lookup_table(&neponset_uart1_gpio_table);
0337     gpiod_add_lookup_table(&neponset_uart3_gpio_table);
0338     gpiod_add_lookup_table(&neponset_pcmcia_table);
0339 
0340     /*
0341      * We would set IRQ_GPIO25 to be a wake-up IRQ, but unfortunately
0342      * something on the Neponset activates this IRQ on sleep (eth?)
0343      */
0344 #if 0
0345     enable_irq_wake(irq);
0346 #endif
0347 
0348     dev_info(&dev->dev, "Neponset daughter board, providing IRQ%u-%u\n",
0349          d->irq_base, d->irq_base + NEP_IRQ_NR - 1);
0350     nep = d;
0351 
0352     /* Ensure that the memory bus request/grant signals are setup */
0353     sa1110_mb_disable();
0354 
0355     sa1111_resources[0].parent = sa1111_res;
0356     sa1111_resources[1].start = d->irq_base + NEP_IRQ_SA1111;
0357     sa1111_resources[1].end = d->irq_base + NEP_IRQ_SA1111;
0358     d->sa1111 = platform_device_register_full(&sa1111_devinfo);
0359 
0360     smc91x_resources[0].parent = smc91x_res;
0361     smc91x_resources[1].parent = smc91x_res;
0362     smc91x_resources[2].start = d->irq_base + NEP_IRQ_SMC91X;
0363     smc91x_resources[2].end = d->irq_base + NEP_IRQ_SMC91X;
0364     d->smc91x = platform_device_register_full(&smc91x_devinfo);
0365 
0366     platform_set_drvdata(dev, d);
0367 
0368     return 0;
0369 
0370  err_irq_alloc:
0371  err_id:
0372     iounmap(d->base);
0373  err_ioremap:
0374     kfree(d);
0375  err_alloc:
0376     return ret;
0377 }
0378 
0379 static int neponset_remove(struct platform_device *dev)
0380 {
0381     struct neponset_drvdata *d = platform_get_drvdata(dev);
0382     int irq = platform_get_irq(dev, 0);
0383 
0384     if (!IS_ERR(d->sa1111))
0385         platform_device_unregister(d->sa1111);
0386     if (!IS_ERR(d->smc91x))
0387         platform_device_unregister(d->smc91x);
0388 
0389     gpiod_remove_lookup_table(&neponset_pcmcia_table);
0390     gpiod_remove_lookup_table(&neponset_uart3_gpio_table);
0391     gpiod_remove_lookup_table(&neponset_uart1_gpio_table);
0392 
0393     irq_set_chained_handler(irq, NULL);
0394     irq_free_descs(d->irq_base, NEP_IRQ_NR);
0395     nep = NULL;
0396     iounmap(d->base);
0397     kfree(d);
0398 
0399     return 0;
0400 }
0401 
0402 #ifdef CONFIG_PM_SLEEP
0403 static int neponset_resume(struct device *dev)
0404 {
0405     struct neponset_drvdata *d = dev_get_drvdata(dev);
0406     int i, ret = 0;
0407 
0408     for (i = 0; i < ARRAY_SIZE(d->gpio); i++) {
0409         ret = gpio_reg_resume(d->gpio[i]);
0410         if (ret)
0411             break;
0412     }
0413 
0414     return ret;
0415 }
0416 
0417 static const struct dev_pm_ops neponset_pm_ops = {
0418     .resume_noirq = neponset_resume,
0419     .restore_noirq = neponset_resume,
0420 };
0421 #define PM_OPS &neponset_pm_ops
0422 #else
0423 #define PM_OPS NULL
0424 #endif
0425 
0426 static struct platform_driver neponset_device_driver = {
0427     .probe      = neponset_probe,
0428     .remove     = neponset_remove,
0429     .driver     = {
0430         .name   = "neponset",
0431         .pm = PM_OPS,
0432     },
0433 };
0434 
0435 static int __init neponset_init(void)
0436 {
0437     return platform_driver_register(&neponset_device_driver);
0438 }
0439 
0440 subsys_initcall(neponset_init);