Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Xilinx XPS PS/2 device driver
0004  *
0005  * (c) 2005 MontaVista Software, Inc.
0006  * (c) 2008 Xilinx, Inc.
0007  */
0008 
0009 
0010 #include <linux/module.h>
0011 #include <linux/serio.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/errno.h>
0014 #include <linux/slab.h>
0015 #include <linux/list.h>
0016 #include <linux/io.h>
0017 #include <linux/of_address.h>
0018 #include <linux/of_device.h>
0019 #include <linux/of_irq.h>
0020 #include <linux/of_platform.h>
0021 
0022 #define DRIVER_NAME     "xilinx_ps2"
0023 
0024 /* Register offsets for the xps2 device */
0025 #define XPS2_SRST_OFFSET    0x00000000 /* Software Reset register */
0026 #define XPS2_STATUS_OFFSET  0x00000004 /* Status register */
0027 #define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */
0028 #define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */
0029 #define XPS2_GIER_OFFSET    0x0000002C /* Global Interrupt Enable reg */
0030 #define XPS2_IPISR_OFFSET   0x00000030 /* Interrupt Status register */
0031 #define XPS2_IPIER_OFFSET   0x00000038 /* Interrupt Enable register */
0032 
0033 /* Reset Register Bit Definitions */
0034 #define XPS2_SRST_RESET     0x0000000A /* Software Reset  */
0035 
0036 /* Status Register Bit Positions */
0037 #define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full  */
0038 #define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full  */
0039 
0040 /*
0041  * Bit definitions for ISR/IER registers. Both the registers have the same bit
0042  * definitions and are only defined once.
0043  */
0044 #define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */
0045 #define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */
0046 #define XPS2_IPIXR_TX_ACK   0x00000004 /* Transmit ACK (Data) Interrupt */
0047 #define XPS2_IPIXR_RX_OVF   0x00000008 /* Receive Overflow Interrupt */
0048 #define XPS2_IPIXR_RX_ERR   0x00000010 /* Receive Error Interrupt */
0049 #define XPS2_IPIXR_RX_FULL  0x00000020 /* Receive Data Interrupt */
0050 
0051 /* Mask for all the Transmit Interrupts */
0052 #define XPS2_IPIXR_TX_ALL   (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
0053 
0054 /* Mask for all the Receive Interrupts */
0055 #define XPS2_IPIXR_RX_ALL   (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR |  \
0056                  XPS2_IPIXR_RX_FULL)
0057 
0058 /* Mask for all the Interrupts */
0059 #define XPS2_IPIXR_ALL      (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL |  \
0060                  XPS2_IPIXR_WDT_TOUT)
0061 
0062 /* Global Interrupt Enable mask */
0063 #define XPS2_GIER_GIE_MASK  0x80000000
0064 
0065 struct xps2data {
0066     int irq;
0067     spinlock_t lock;
0068     void __iomem *base_address; /* virt. address of control registers */
0069     unsigned int flags;
0070     struct serio *serio;        /* serio */
0071     struct device *dev;
0072 };
0073 
0074 /************************************/
0075 /* XPS PS/2 data transmission calls */
0076 /************************************/
0077 
0078 /**
0079  * xps2_recv() - attempts to receive a byte from the PS/2 port.
0080  * @drvdata:    pointer to ps2 device private data structure
0081  * @byte:   address where the read data will be copied
0082  *
0083  * If there is any data available in the PS/2 receiver, this functions reads
0084  * the data, otherwise it returns error.
0085  */
0086 static int xps2_recv(struct xps2data *drvdata, u8 *byte)
0087 {
0088     u32 sr;
0089     int status = -1;
0090 
0091     /* If there is data available in the PS/2 receiver, read it */
0092     sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
0093     if (sr & XPS2_STATUS_RX_FULL) {
0094         *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
0095         status = 0;
0096     }
0097 
0098     return status;
0099 }
0100 
0101 /*********************/
0102 /* Interrupt handler */
0103 /*********************/
0104 static irqreturn_t xps2_interrupt(int irq, void *dev_id)
0105 {
0106     struct xps2data *drvdata = dev_id;
0107     u32 intr_sr;
0108     u8 c;
0109     int status;
0110 
0111     /* Get the PS/2 interrupts and clear them */
0112     intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
0113     out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
0114 
0115     /* Check which interrupt is active */
0116     if (intr_sr & XPS2_IPIXR_RX_OVF)
0117         dev_warn(drvdata->dev, "receive overrun error\n");
0118 
0119     if (intr_sr & XPS2_IPIXR_RX_ERR)
0120         drvdata->flags |= SERIO_PARITY;
0121 
0122     if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
0123         drvdata->flags |= SERIO_TIMEOUT;
0124 
0125     if (intr_sr & XPS2_IPIXR_RX_FULL) {
0126         status = xps2_recv(drvdata, &c);
0127 
0128         /* Error, if a byte is not received */
0129         if (status) {
0130             dev_err(drvdata->dev,
0131                 "wrong rcvd byte count (%d)\n", status);
0132         } else {
0133             serio_interrupt(drvdata->serio, c, drvdata->flags);
0134             drvdata->flags = 0;
0135         }
0136     }
0137 
0138     return IRQ_HANDLED;
0139 }
0140 
0141 /*******************/
0142 /* serio callbacks */
0143 /*******************/
0144 
0145 /**
0146  * sxps2_write() - sends a byte out through the PS/2 port.
0147  * @pserio: pointer to the serio structure of the PS/2 port
0148  * @c:      data that needs to be written to the PS/2 port
0149  *
0150  * This function checks if the PS/2 transmitter is empty and sends a byte.
0151  * Otherwise it returns error. Transmission fails only when nothing is connected
0152  * to the PS/2 port. Thats why, we do not try to resend the data in case of a
0153  * failure.
0154  */
0155 static int sxps2_write(struct serio *pserio, unsigned char c)
0156 {
0157     struct xps2data *drvdata = pserio->port_data;
0158     unsigned long flags;
0159     u32 sr;
0160     int status = -1;
0161 
0162     spin_lock_irqsave(&drvdata->lock, flags);
0163 
0164     /* If the PS/2 transmitter is empty send a byte of data */
0165     sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
0166     if (!(sr & XPS2_STATUS_TX_FULL)) {
0167         out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
0168         status = 0;
0169     }
0170 
0171     spin_unlock_irqrestore(&drvdata->lock, flags);
0172 
0173     return status;
0174 }
0175 
0176 /**
0177  * sxps2_open() - called when a port is opened by the higher layer.
0178  * @pserio: pointer to the serio structure of the PS/2 device
0179  *
0180  * This function requests irq and enables interrupts for the PS/2 device.
0181  */
0182 static int sxps2_open(struct serio *pserio)
0183 {
0184     struct xps2data *drvdata = pserio->port_data;
0185     int error;
0186     u8 c;
0187 
0188     error = request_irq(drvdata->irq, &xps2_interrupt, 0,
0189                 DRIVER_NAME, drvdata);
0190     if (error) {
0191         dev_err(drvdata->dev,
0192             "Couldn't allocate interrupt %d\n", drvdata->irq);
0193         return error;
0194     }
0195 
0196     /* start reception by enabling the interrupts */
0197     out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
0198     out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
0199     (void)xps2_recv(drvdata, &c);
0200 
0201     return 0;       /* success */
0202 }
0203 
0204 /**
0205  * sxps2_close() - frees the interrupt.
0206  * @pserio: pointer to the serio structure of the PS/2 device
0207  *
0208  * This function frees the irq and disables interrupts for the PS/2 device.
0209  */
0210 static void sxps2_close(struct serio *pserio)
0211 {
0212     struct xps2data *drvdata = pserio->port_data;
0213 
0214     /* Disable the PS2 interrupts */
0215     out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
0216     out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
0217     free_irq(drvdata->irq, drvdata);
0218 }
0219 
0220 /**
0221  * xps2_of_probe - probe method for the PS/2 device.
0222  * @of_dev: pointer to OF device structure
0223  * @match:  pointer to the structure used for matching a device
0224  *
0225  * This function probes the PS/2 device in the device tree.
0226  * It initializes the driver data structure and the hardware.
0227  * It returns 0, if the driver is bound to the PS/2 device, or a negative
0228  * value if there is an error.
0229  */
0230 static int xps2_of_probe(struct platform_device *ofdev)
0231 {
0232     struct resource r_mem; /* IO mem resources */
0233     struct xps2data *drvdata;
0234     struct serio *serio;
0235     struct device *dev = &ofdev->dev;
0236     resource_size_t remap_size, phys_addr;
0237     unsigned int irq;
0238     int error;
0239 
0240     dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
0241 
0242     /* Get iospace for the device */
0243     error = of_address_to_resource(dev->of_node, 0, &r_mem);
0244     if (error) {
0245         dev_err(dev, "invalid address\n");
0246         return error;
0247     }
0248 
0249     /* Get IRQ for the device */
0250     irq = irq_of_parse_and_map(dev->of_node, 0);
0251     if (!irq) {
0252         dev_err(dev, "no IRQ found\n");
0253         return -ENODEV;
0254     }
0255 
0256     drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
0257     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0258     if (!drvdata || !serio) {
0259         error = -ENOMEM;
0260         goto failed1;
0261     }
0262 
0263     spin_lock_init(&drvdata->lock);
0264     drvdata->irq = irq;
0265     drvdata->serio = serio;
0266     drvdata->dev = dev;
0267 
0268     phys_addr = r_mem.start;
0269     remap_size = resource_size(&r_mem);
0270     if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
0271         dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
0272             (unsigned long long)phys_addr);
0273         error = -EBUSY;
0274         goto failed1;
0275     }
0276 
0277     /* Fill in configuration data and add them to the list */
0278     drvdata->base_address = ioremap(phys_addr, remap_size);
0279     if (drvdata->base_address == NULL) {
0280         dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
0281             (unsigned long long)phys_addr);
0282         error = -EFAULT;
0283         goto failed2;
0284     }
0285 
0286     /* Disable all the interrupts, just in case */
0287     out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
0288 
0289     /*
0290      * Reset the PS2 device and abort any current transaction,
0291      * to make sure we have the PS2 in a good state.
0292      */
0293     out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
0294 
0295     dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
0296          (unsigned long long)phys_addr, drvdata->base_address,
0297          drvdata->irq);
0298 
0299     serio->id.type = SERIO_8042;
0300     serio->write = sxps2_write;
0301     serio->open = sxps2_open;
0302     serio->close = sxps2_close;
0303     serio->port_data = drvdata;
0304     serio->dev.parent = dev;
0305     snprintf(serio->name, sizeof(serio->name),
0306          "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
0307     snprintf(serio->phys, sizeof(serio->phys),
0308          "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
0309 
0310     serio_register_port(serio);
0311 
0312     platform_set_drvdata(ofdev, drvdata);
0313     return 0;       /* success */
0314 
0315 failed2:
0316     release_mem_region(phys_addr, remap_size);
0317 failed1:
0318     kfree(serio);
0319     kfree(drvdata);
0320 
0321     return error;
0322 }
0323 
0324 /**
0325  * xps2_of_remove - unbinds the driver from the PS/2 device.
0326  * @of_dev: pointer to OF device structure
0327  *
0328  * This function is called if a device is physically removed from the system or
0329  * if the driver module is being unloaded. It frees any resources allocated to
0330  * the device.
0331  */
0332 static int xps2_of_remove(struct platform_device *of_dev)
0333 {
0334     struct xps2data *drvdata = platform_get_drvdata(of_dev);
0335     struct resource r_mem; /* IO mem resources */
0336 
0337     serio_unregister_port(drvdata->serio);
0338     iounmap(drvdata->base_address);
0339 
0340     /* Get iospace of the device */
0341     if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
0342         dev_err(drvdata->dev, "invalid address\n");
0343     else
0344         release_mem_region(r_mem.start, resource_size(&r_mem));
0345 
0346     kfree(drvdata);
0347 
0348     return 0;
0349 }
0350 
0351 /* Match table for of_platform binding */
0352 static const struct of_device_id xps2_of_match[] = {
0353     { .compatible = "xlnx,xps-ps2-1.00.a", },
0354     { /* end of list */ },
0355 };
0356 MODULE_DEVICE_TABLE(of, xps2_of_match);
0357 
0358 static struct platform_driver xps2_of_driver = {
0359     .driver = {
0360         .name = DRIVER_NAME,
0361         .of_match_table = xps2_of_match,
0362     },
0363     .probe      = xps2_of_probe,
0364     .remove     = xps2_of_remove,
0365 };
0366 module_platform_driver(xps2_of_driver);
0367 
0368 MODULE_AUTHOR("Xilinx, Inc.");
0369 MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
0370 MODULE_LICENSE("GPL");
0371