Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/drivers/input/serio/sa1111ps2.c
0004  *
0005  *  Copyright (C) 2002 Russell King
0006  */
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/input.h>
0010 #include <linux/serio.h>
0011 #include <linux/errno.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/ioport.h>
0014 #include <linux/delay.h>
0015 #include <linux/device.h>
0016 #include <linux/slab.h>
0017 #include <linux/spinlock.h>
0018 
0019 #include <asm/io.h>
0020 
0021 #include <asm/hardware/sa1111.h>
0022 
0023 #define PS2CR       0x0000
0024 #define PS2STAT     0x0004
0025 #define PS2DATA     0x0008
0026 #define PS2CLKDIV   0x000c
0027 #define PS2PRECNT   0x0010
0028 
0029 #define PS2CR_ENA   0x08
0030 #define PS2CR_FKD   0x02
0031 #define PS2CR_FKC   0x01
0032 
0033 #define PS2STAT_STP 0x0100
0034 #define PS2STAT_TXE 0x0080
0035 #define PS2STAT_TXB 0x0040
0036 #define PS2STAT_RXF 0x0020
0037 #define PS2STAT_RXB 0x0010
0038 #define PS2STAT_ENA 0x0008
0039 #define PS2STAT_RXP 0x0004
0040 #define PS2STAT_KBD 0x0002
0041 #define PS2STAT_KBC 0x0001
0042 
0043 struct ps2if {
0044     struct serio        *io;
0045     struct sa1111_dev   *dev;
0046     void __iomem        *base;
0047     int         rx_irq;
0048     int         tx_irq;
0049     unsigned int        open;
0050     spinlock_t      lock;
0051     unsigned int        head;
0052     unsigned int        tail;
0053     unsigned char       buf[4];
0054 };
0055 
0056 /*
0057  * Read all bytes waiting in the PS2 port.  There should be
0058  * at the most one, but we loop for safety.  If there was a
0059  * framing error, we have to manually clear the status.
0060  */
0061 static irqreturn_t ps2_rxint(int irq, void *dev_id)
0062 {
0063     struct ps2if *ps2if = dev_id;
0064     unsigned int scancode, flag, status;
0065 
0066     status = readl_relaxed(ps2if->base + PS2STAT);
0067     while (status & PS2STAT_RXF) {
0068         if (status & PS2STAT_STP)
0069             writel_relaxed(PS2STAT_STP, ps2if->base + PS2STAT);
0070 
0071         flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
0072                (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
0073 
0074         scancode = readl_relaxed(ps2if->base + PS2DATA) & 0xff;
0075 
0076         if (hweight8(scancode) & 1)
0077             flag ^= SERIO_PARITY;
0078 
0079         serio_interrupt(ps2if->io, scancode, flag);
0080 
0081         status = readl_relaxed(ps2if->base + PS2STAT);
0082         }
0083 
0084         return IRQ_HANDLED;
0085 }
0086 
0087 /*
0088  * Completion of ps2 write
0089  */
0090 static irqreturn_t ps2_txint(int irq, void *dev_id)
0091 {
0092     struct ps2if *ps2if = dev_id;
0093     unsigned int status;
0094 
0095     spin_lock(&ps2if->lock);
0096     status = readl_relaxed(ps2if->base + PS2STAT);
0097     if (ps2if->head == ps2if->tail) {
0098         disable_irq_nosync(irq);
0099         /* done */
0100     } else if (status & PS2STAT_TXE) {
0101         writel_relaxed(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
0102         ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
0103     }
0104     spin_unlock(&ps2if->lock);
0105 
0106     return IRQ_HANDLED;
0107 }
0108 
0109 /*
0110  * Write a byte to the PS2 port.  We have to wait for the
0111  * port to indicate that the transmitter is empty.
0112  */
0113 static int ps2_write(struct serio *io, unsigned char val)
0114 {
0115     struct ps2if *ps2if = io->port_data;
0116     unsigned long flags;
0117     unsigned int head;
0118 
0119     spin_lock_irqsave(&ps2if->lock, flags);
0120 
0121     /*
0122      * If the TX register is empty, we can go straight out.
0123      */
0124     if (readl_relaxed(ps2if->base + PS2STAT) & PS2STAT_TXE) {
0125         writel_relaxed(val, ps2if->base + PS2DATA);
0126     } else {
0127         if (ps2if->head == ps2if->tail)
0128             enable_irq(ps2if->tx_irq);
0129         head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
0130         if (head != ps2if->tail) {
0131             ps2if->buf[ps2if->head] = val;
0132             ps2if->head = head;
0133         }
0134     }
0135 
0136     spin_unlock_irqrestore(&ps2if->lock, flags);
0137     return 0;
0138 }
0139 
0140 static int ps2_open(struct serio *io)
0141 {
0142     struct ps2if *ps2if = io->port_data;
0143     int ret;
0144 
0145     ret = sa1111_enable_device(ps2if->dev);
0146     if (ret)
0147         return ret;
0148 
0149     ret = request_irq(ps2if->rx_irq, ps2_rxint, 0,
0150               SA1111_DRIVER_NAME(ps2if->dev), ps2if);
0151     if (ret) {
0152         printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
0153             ps2if->rx_irq, ret);
0154         sa1111_disable_device(ps2if->dev);
0155         return ret;
0156     }
0157 
0158     ret = request_irq(ps2if->tx_irq, ps2_txint, 0,
0159               SA1111_DRIVER_NAME(ps2if->dev), ps2if);
0160     if (ret) {
0161         printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
0162             ps2if->tx_irq, ret);
0163         free_irq(ps2if->rx_irq, ps2if);
0164         sa1111_disable_device(ps2if->dev);
0165         return ret;
0166     }
0167 
0168     ps2if->open = 1;
0169 
0170     enable_irq_wake(ps2if->rx_irq);
0171 
0172     writel_relaxed(PS2CR_ENA, ps2if->base + PS2CR);
0173     return 0;
0174 }
0175 
0176 static void ps2_close(struct serio *io)
0177 {
0178     struct ps2if *ps2if = io->port_data;
0179 
0180     writel_relaxed(0, ps2if->base + PS2CR);
0181 
0182     disable_irq_wake(ps2if->rx_irq);
0183 
0184     ps2if->open = 0;
0185 
0186     free_irq(ps2if->tx_irq, ps2if);
0187     free_irq(ps2if->rx_irq, ps2if);
0188 
0189     sa1111_disable_device(ps2if->dev);
0190 }
0191 
0192 /*
0193  * Clear the input buffer.
0194  */
0195 static void ps2_clear_input(struct ps2if *ps2if)
0196 {
0197     int maxread = 100;
0198 
0199     while (maxread--) {
0200         if ((readl_relaxed(ps2if->base + PS2DATA) & 0xff) == 0xff)
0201             break;
0202     }
0203 }
0204 
0205 static unsigned int ps2_test_one(struct ps2if *ps2if,
0206                        unsigned int mask)
0207 {
0208     unsigned int val;
0209 
0210     writel_relaxed(PS2CR_ENA | mask, ps2if->base + PS2CR);
0211 
0212     udelay(10);
0213 
0214     val = readl_relaxed(ps2if->base + PS2STAT);
0215     return val & (PS2STAT_KBC | PS2STAT_KBD);
0216 }
0217 
0218 /*
0219  * Test the keyboard interface.  We basically check to make sure that
0220  * we can drive each line to the keyboard independently of each other.
0221  */
0222 static int ps2_test(struct ps2if *ps2if)
0223 {
0224     unsigned int stat;
0225     int ret = 0;
0226 
0227     stat = ps2_test_one(ps2if, PS2CR_FKC);
0228     if (stat != PS2STAT_KBD) {
0229         printk("PS/2 interface test failed[1]: %02x\n", stat);
0230         ret = -ENODEV;
0231     }
0232 
0233     stat = ps2_test_one(ps2if, 0);
0234     if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
0235         printk("PS/2 interface test failed[2]: %02x\n", stat);
0236         ret = -ENODEV;
0237     }
0238 
0239     stat = ps2_test_one(ps2if, PS2CR_FKD);
0240     if (stat != PS2STAT_KBC) {
0241         printk("PS/2 interface test failed[3]: %02x\n", stat);
0242         ret = -ENODEV;
0243     }
0244 
0245     writel_relaxed(0, ps2if->base + PS2CR);
0246 
0247     return ret;
0248 }
0249 
0250 /*
0251  * Add one device to this driver.
0252  */
0253 static int ps2_probe(struct sa1111_dev *dev)
0254 {
0255     struct ps2if *ps2if;
0256     struct serio *serio;
0257     int ret;
0258 
0259     ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
0260     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0261     if (!ps2if || !serio) {
0262         ret = -ENOMEM;
0263         goto free;
0264     }
0265 
0266     serio->id.type      = SERIO_8042;
0267     serio->write        = ps2_write;
0268     serio->open     = ps2_open;
0269     serio->close        = ps2_close;
0270     strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
0271     strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
0272     serio->port_data    = ps2if;
0273     serio->dev.parent   = &dev->dev;
0274     ps2if->io       = serio;
0275     ps2if->dev      = dev;
0276     sa1111_set_drvdata(dev, ps2if);
0277 
0278     spin_lock_init(&ps2if->lock);
0279 
0280     ps2if->rx_irq = sa1111_get_irq(dev, 0);
0281     if (ps2if->rx_irq <= 0) {
0282         ret = ps2if->rx_irq ? : -ENXIO;
0283         goto free;
0284     }
0285 
0286     ps2if->tx_irq = sa1111_get_irq(dev, 1);
0287     if (ps2if->tx_irq <= 0) {
0288         ret = ps2if->tx_irq ? : -ENXIO;
0289         goto free;
0290     }
0291 
0292     /*
0293      * Request the physical region for this PS2 port.
0294      */
0295     if (!request_mem_region(dev->res.start,
0296                 dev->res.end - dev->res.start + 1,
0297                 SA1111_DRIVER_NAME(dev))) {
0298         ret = -EBUSY;
0299         goto free;
0300     }
0301 
0302     /*
0303      * Our parent device has already mapped the region.
0304      */
0305     ps2if->base = dev->mapbase;
0306 
0307     sa1111_enable_device(ps2if->dev);
0308 
0309     /* Incoming clock is 8MHz */
0310     writel_relaxed(0, ps2if->base + PS2CLKDIV);
0311     writel_relaxed(127, ps2if->base + PS2PRECNT);
0312 
0313     /*
0314      * Flush any pending input.
0315      */
0316     ps2_clear_input(ps2if);
0317 
0318     /*
0319      * Test the keyboard interface.
0320      */
0321     ret = ps2_test(ps2if);
0322     if (ret)
0323         goto out;
0324 
0325     /*
0326      * Flush any pending input.
0327      */
0328     ps2_clear_input(ps2if);
0329 
0330     sa1111_disable_device(ps2if->dev);
0331     serio_register_port(ps2if->io);
0332     return 0;
0333 
0334  out:
0335     sa1111_disable_device(ps2if->dev);
0336     release_mem_region(dev->res.start, resource_size(&dev->res));
0337  free:
0338     sa1111_set_drvdata(dev, NULL);
0339     kfree(ps2if);
0340     kfree(serio);
0341     return ret;
0342 }
0343 
0344 /*
0345  * Remove one device from this driver.
0346  */
0347 static void ps2_remove(struct sa1111_dev *dev)
0348 {
0349     struct ps2if *ps2if = sa1111_get_drvdata(dev);
0350 
0351     serio_unregister_port(ps2if->io);
0352     release_mem_region(dev->res.start, resource_size(&dev->res));
0353     sa1111_set_drvdata(dev, NULL);
0354 
0355     kfree(ps2if);
0356 }
0357 
0358 /*
0359  * Our device driver structure
0360  */
0361 static struct sa1111_driver ps2_driver = {
0362     .drv = {
0363         .name   = "sa1111-ps2",
0364         .owner  = THIS_MODULE,
0365     },
0366     .devid      = SA1111_DEVID_PS2,
0367     .probe      = ps2_probe,
0368     .remove     = ps2_remove,
0369 };
0370 
0371 static int __init ps2_init(void)
0372 {
0373     return sa1111_driver_register(&ps2_driver);
0374 }
0375 
0376 static void __exit ps2_exit(void)
0377 {
0378     sa1111_driver_unregister(&ps2_driver);
0379 }
0380 
0381 module_init(ps2_init);
0382 module_exit(ps2_exit);
0383 
0384 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
0385 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
0386 MODULE_LICENSE("GPL");