1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Xilinx XPS PS/2 device driver 4 * 5 * (c) 2005 MontaVista Software, Inc. 6 * (c) 2008 Xilinx, Inc. 7 */ 8 9 10 #include <linux/module.h> 11 #include <linux/serio.h> 12 #include <linux/interrupt.h> 13 #include <linux/errno.h> 14 #include <linux/slab.h> 15 #include <linux/list.h> 16 #include <linux/io.h> 17 #include <linux/mod_devicetable.h> 18 #include <linux/of_address.h> 19 #include <linux/of_irq.h> 20 #include <linux/platform_device.h> 21 22 #define DRIVER_NAME "xilinx_ps2" 23 24 /* Register offsets for the xps2 device */ 25 #define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */ 26 #define XPS2_STATUS_OFFSET 0x00000004 /* Status register */ 27 #define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */ 28 #define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */ 29 #define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */ 30 #define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */ 31 #define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */ 32 33 /* Reset Register Bit Definitions */ 34 #define XPS2_SRST_RESET 0x0000000A /* Software Reset */ 35 36 /* Status Register Bit Positions */ 37 #define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */ 38 #define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */ 39 40 /* 41 * Bit definitions for ISR/IER registers. Both the registers have the same bit 42 * definitions and are only defined once. 43 */ 44 #define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */ 45 #define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */ 46 #define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */ 47 #define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */ 48 #define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */ 49 #define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */ 50 51 /* Mask for all the Transmit Interrupts */ 52 #define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK) 53 54 /* Mask for all the Receive Interrupts */ 55 #define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \ 56 XPS2_IPIXR_RX_FULL) 57 58 /* Mask for all the Interrupts */ 59 #define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \ 60 XPS2_IPIXR_WDT_TOUT) 61 62 /* Global Interrupt Enable mask */ 63 #define XPS2_GIER_GIE_MASK 0x80000000 64 65 struct xps2data { 66 int irq; 67 spinlock_t lock; 68 void __iomem *base_address; /* virt. address of control registers */ 69 unsigned int flags; 70 struct serio *serio; /* serio */ 71 struct device *dev; 72 }; 73 74 /************************************/ 75 /* XPS PS/2 data transmission calls */ 76 /************************************/ 77 78 /** 79 * xps2_recv() - attempts to receive a byte from the PS/2 port. 80 * @drvdata: pointer to ps2 device private data structure 81 * @byte: address where the read data will be copied 82 * 83 * If there is any data available in the PS/2 receiver, this functions reads 84 * the data, otherwise it returns error. 85 */ 86 static int xps2_recv(struct xps2data *drvdata, u8 *byte) 87 { 88 u32 sr; 89 int status = -1; 90 91 /* If there is data available in the PS/2 receiver, read it */ 92 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET); 93 if (sr & XPS2_STATUS_RX_FULL) { 94 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET); 95 status = 0; 96 } 97 98 return status; 99 } 100 101 /*********************/ 102 /* Interrupt handler */ 103 /*********************/ 104 static irqreturn_t xps2_interrupt(int irq, void *dev_id) 105 { 106 struct xps2data *drvdata = dev_id; 107 u32 intr_sr; 108 u8 c; 109 int status; 110 111 /* Get the PS/2 interrupts and clear them */ 112 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET); 113 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr); 114 115 /* Check which interrupt is active */ 116 if (intr_sr & XPS2_IPIXR_RX_OVF) 117 dev_warn(drvdata->dev, "receive overrun error\n"); 118 119 if (intr_sr & XPS2_IPIXR_RX_ERR) 120 drvdata->flags |= SERIO_PARITY; 121 122 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT)) 123 drvdata->flags |= SERIO_TIMEOUT; 124 125 if (intr_sr & XPS2_IPIXR_RX_FULL) { 126 status = xps2_recv(drvdata, &c); 127 128 /* Error, if a byte is not received */ 129 if (status) { 130 dev_err(drvdata->dev, 131 "wrong rcvd byte count (%d)\n", status); 132 } else { 133 serio_interrupt(drvdata->serio, c, drvdata->flags); 134 drvdata->flags = 0; 135 } 136 } 137 138 return IRQ_HANDLED; 139 } 140 141 /*******************/ 142 /* serio callbacks */ 143 /*******************/ 144 145 /** 146 * sxps2_write() - sends a byte out through the PS/2 port. 147 * @pserio: pointer to the serio structure of the PS/2 port 148 * @c: data that needs to be written to the PS/2 port 149 * 150 * This function checks if the PS/2 transmitter is empty and sends a byte. 151 * Otherwise it returns error. Transmission fails only when nothing is connected 152 * to the PS/2 port. Thats why, we do not try to resend the data in case of a 153 * failure. 154 */ 155 static int sxps2_write(struct serio *pserio, unsigned char c) 156 { 157 struct xps2data *drvdata = pserio->port_data; 158 u32 sr; 159 160 guard(spinlock_irqsave)(&drvdata->lock); 161 162 /* If the PS/2 transmitter is empty send a byte of data */ 163 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET); 164 if (sr & XPS2_STATUS_TX_FULL) 165 return -EAGAIN; 166 167 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c); 168 return 0; 169 } 170 171 /** 172 * sxps2_open() - called when a port is opened by the higher layer. 173 * @pserio: pointer to the serio structure of the PS/2 device 174 * 175 * This function requests irq and enables interrupts for the PS/2 device. 176 */ 177 static int sxps2_open(struct serio *pserio) 178 { 179 struct xps2data *drvdata = pserio->port_data; 180 int error; 181 u8 c; 182 183 error = request_irq(drvdata->irq, &xps2_interrupt, 0, 184 DRIVER_NAME, drvdata); 185 if (error) { 186 dev_err(drvdata->dev, 187 "Couldn't allocate interrupt %d\n", drvdata->irq); 188 return error; 189 } 190 191 /* start reception by enabling the interrupts */ 192 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK); 193 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL); 194 (void)xps2_recv(drvdata, &c); 195 196 return 0; /* success */ 197 } 198 199 /** 200 * sxps2_close() - frees the interrupt. 201 * @pserio: pointer to the serio structure of the PS/2 device 202 * 203 * This function frees the irq and disables interrupts for the PS/2 device. 204 */ 205 static void sxps2_close(struct serio *pserio) 206 { 207 struct xps2data *drvdata = pserio->port_data; 208 209 /* Disable the PS2 interrupts */ 210 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00); 211 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00); 212 free_irq(drvdata->irq, drvdata); 213 } 214 215 /** 216 * xps2_of_probe - probe method for the PS/2 device. 217 * @ofdev: pointer to OF device structure 218 * 219 * This function probes the PS/2 device in the device tree. 220 * It initializes the driver data structure and the hardware. 221 * It returns 0, if the driver is bound to the PS/2 device, or a negative 222 * value if there is an error. 223 */ 224 static int xps2_of_probe(struct platform_device *ofdev) 225 { 226 struct resource r_mem; /* IO mem resources */ 227 struct xps2data *drvdata; 228 struct serio *serio; 229 struct device *dev = &ofdev->dev; 230 resource_size_t remap_size, phys_addr; 231 unsigned int irq; 232 int error; 233 234 dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node); 235 236 /* Get iospace for the device */ 237 error = of_address_to_resource(dev->of_node, 0, &r_mem); 238 if (error) { 239 dev_err(dev, "invalid address\n"); 240 return error; 241 } 242 243 /* Get IRQ for the device */ 244 irq = irq_of_parse_and_map(dev->of_node, 0); 245 if (!irq) { 246 dev_err(dev, "no IRQ found\n"); 247 return -ENODEV; 248 } 249 250 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL); 251 serio = kzalloc(sizeof(*serio), GFP_KERNEL); 252 if (!drvdata || !serio) { 253 error = -ENOMEM; 254 goto failed1; 255 } 256 257 spin_lock_init(&drvdata->lock); 258 drvdata->irq = irq; 259 drvdata->serio = serio; 260 drvdata->dev = dev; 261 262 phys_addr = r_mem.start; 263 remap_size = resource_size(&r_mem); 264 if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) { 265 dev_err(dev, "Couldn't lock memory region at 0x%08llX\n", 266 (unsigned long long)phys_addr); 267 error = -EBUSY; 268 goto failed1; 269 } 270 271 /* Fill in configuration data and add them to the list */ 272 drvdata->base_address = ioremap(phys_addr, remap_size); 273 if (drvdata->base_address == NULL) { 274 dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n", 275 (unsigned long long)phys_addr); 276 error = -EFAULT; 277 goto failed2; 278 } 279 280 /* Disable all the interrupts, just in case */ 281 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0); 282 283 /* 284 * Reset the PS2 device and abort any current transaction, 285 * to make sure we have the PS2 in a good state. 286 */ 287 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET); 288 289 dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n", 290 (unsigned long long)phys_addr, drvdata->base_address, 291 drvdata->irq); 292 293 serio->id.type = SERIO_8042; 294 serio->write = sxps2_write; 295 serio->open = sxps2_open; 296 serio->close = sxps2_close; 297 serio->port_data = drvdata; 298 serio->dev.parent = dev; 299 snprintf(serio->name, sizeof(serio->name), 300 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr); 301 snprintf(serio->phys, sizeof(serio->phys), 302 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr); 303 304 serio_register_port(serio); 305 306 platform_set_drvdata(ofdev, drvdata); 307 return 0; /* success */ 308 309 failed2: 310 release_mem_region(phys_addr, remap_size); 311 failed1: 312 kfree(serio); 313 kfree(drvdata); 314 315 return error; 316 } 317 318 /** 319 * xps2_of_remove - unbinds the driver from the PS/2 device. 320 * @of_dev: pointer to OF device structure 321 * 322 * This function is called if a device is physically removed from the system or 323 * if the driver module is being unloaded. It frees any resources allocated to 324 * the device. 325 */ 326 static void xps2_of_remove(struct platform_device *of_dev) 327 { 328 struct xps2data *drvdata = platform_get_drvdata(of_dev); 329 struct resource r_mem; /* IO mem resources */ 330 331 serio_unregister_port(drvdata->serio); 332 iounmap(drvdata->base_address); 333 334 /* Get iospace of the device */ 335 if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem)) 336 dev_err(drvdata->dev, "invalid address\n"); 337 else 338 release_mem_region(r_mem.start, resource_size(&r_mem)); 339 340 kfree(drvdata); 341 } 342 343 /* Match table for of_platform binding */ 344 static const struct of_device_id xps2_of_match[] = { 345 { .compatible = "xlnx,xps-ps2-1.00.a", }, 346 { /* end of list */ }, 347 }; 348 MODULE_DEVICE_TABLE(of, xps2_of_match); 349 350 static struct platform_driver xps2_of_driver = { 351 .driver = { 352 .name = DRIVER_NAME, 353 .of_match_table = xps2_of_match, 354 }, 355 .probe = xps2_of_probe, 356 .remove = xps2_of_remove, 357 }; 358 module_platform_driver(xps2_of_driver); 359 360 MODULE_AUTHOR("Xilinx, Inc."); 361 MODULE_DESCRIPTION("Xilinx XPS PS/2 driver"); 362 MODULE_LICENSE("GPL"); 363 364