1 /* 2 * Xilinx XPS PS/2 device driver 3 * 4 * (c) 2005 MontaVista Software, Inc. 5 * (c) 2008 Xilinx, Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the 9 * Free Software Foundation; either version 2 of the License, or (at your 10 * option) any later version. 11 * 12 * You should have received a copy of the GNU General Public License along 13 * with this program; if not, write to the Free Software Foundation, Inc., 14 * 675 Mass Ave, Cambridge, MA 02139, USA. 15 */ 16 17 18 #include <linux/module.h> 19 #include <linux/serio.h> 20 #include <linux/interrupt.h> 21 #include <linux/errno.h> 22 #include <linux/init.h> 23 #include <linux/list.h> 24 #include <linux/io.h> 25 26 #include <linux/of_device.h> 27 #include <linux/of_platform.h> 28 29 #define DRIVER_NAME "xilinx_ps2" 30 31 /* Register offsets for the xps2 device */ 32 #define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */ 33 #define XPS2_STATUS_OFFSET 0x00000004 /* Status register */ 34 #define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */ 35 #define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */ 36 #define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */ 37 #define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */ 38 #define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */ 39 40 /* Reset Register Bit Definitions */ 41 #define XPS2_SRST_RESET 0x0000000A /* Software Reset */ 42 43 /* Status Register Bit Positions */ 44 #define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */ 45 #define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */ 46 47 /* Bit definitions for ISR/IER registers. Both the registers have the same bit 48 * definitions and are only defined once. */ 49 #define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */ 50 #define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */ 51 #define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */ 52 #define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */ 53 #define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */ 54 #define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */ 55 56 /* Mask for all the Transmit Interrupts */ 57 #define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK) 58 59 /* Mask for all the Receive Interrupts */ 60 #define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \ 61 XPS2_IPIXR_RX_FULL) 62 63 /* Mask for all the Interrupts */ 64 #define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \ 65 XPS2_IPIXR_WDT_TOUT) 66 67 /* Global Interrupt Enable mask */ 68 #define XPS2_GIER_GIE_MASK 0x80000000 69 70 struct xps2data { 71 int irq; 72 u32 phys_addr; 73 u32 remap_size; 74 spinlock_t lock; 75 u8 rxb; /* Rx buffer */ 76 void __iomem *base_address; /* virt. address of control registers */ 77 unsigned int dfl; 78 struct serio serio; /* serio */ 79 }; 80 81 /************************************/ 82 /* XPS PS/2 data transmission calls */ 83 /************************************/ 84 85 /* 86 * xps2_recv() will attempt to receive a byte of data from the PS/2 port. 87 */ 88 static int xps2_recv(struct xps2data *drvdata, u8 *byte) 89 { 90 u32 sr; 91 int status = -1; 92 93 /* If there is data available in the PS/2 receiver, read it */ 94 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET); 95 if (sr & XPS2_STATUS_RX_FULL) { 96 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET); 97 status = 0; 98 } 99 100 return status; 101 } 102 103 /*********************/ 104 /* Interrupt handler */ 105 /*********************/ 106 static irqreturn_t xps2_interrupt(int irq, void *dev_id) 107 { 108 struct xps2data *drvdata = dev_id; 109 u32 intr_sr; 110 u8 c; 111 int status; 112 113 /* Get the PS/2 interrupts and clear them */ 114 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET); 115 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr); 116 117 /* Check which interrupt is active */ 118 if (intr_sr & XPS2_IPIXR_RX_OVF) 119 printk(KERN_WARNING "%s: receive overrun error\n", 120 drvdata->serio.name); 121 122 if (intr_sr & XPS2_IPIXR_RX_ERR) 123 drvdata->dfl |= SERIO_PARITY; 124 125 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT)) 126 drvdata->dfl |= SERIO_TIMEOUT; 127 128 if (intr_sr & XPS2_IPIXR_RX_FULL) { 129 status = xps2_recv(drvdata, &drvdata->rxb); 130 131 /* Error, if a byte is not received */ 132 if (status) { 133 printk(KERN_ERR 134 "%s: wrong rcvd byte count (%d)\n", 135 drvdata->serio.name, status); 136 } else { 137 c = drvdata->rxb; 138 serio_interrupt(&drvdata->serio, c, drvdata->dfl); 139 drvdata->dfl = 0; 140 } 141 } 142 143 if (intr_sr & XPS2_IPIXR_TX_ACK) 144 drvdata->dfl = 0; 145 146 return IRQ_HANDLED; 147 } 148 149 /*******************/ 150 /* serio callbacks */ 151 /*******************/ 152 153 /* 154 * sxps2_write() sends a byte out through the PS/2 interface. 155 */ 156 static int sxps2_write(struct serio *pserio, unsigned char c) 157 { 158 struct xps2data *drvdata = pserio->port_data; 159 unsigned long flags; 160 u32 sr; 161 int status = -1; 162 163 spin_lock_irqsave(&drvdata->lock, flags); 164 165 /* If the PS/2 transmitter is empty send a byte of data */ 166 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET); 167 if (!(sr & XPS2_STATUS_TX_FULL)) { 168 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c); 169 status = 0; 170 } 171 172 spin_unlock_irqrestore(&drvdata->lock, flags); 173 174 return status; 175 } 176 177 /* 178 * sxps2_open() is called when a port is open by the higher layer. 179 */ 180 static int sxps2_open(struct serio *pserio) 181 { 182 struct xps2data *drvdata = pserio->port_data; 183 int retval; 184 185 retval = request_irq(drvdata->irq, &xps2_interrupt, 0, 186 DRIVER_NAME, drvdata); 187 if (retval) { 188 printk(KERN_ERR 189 "%s: Couldn't allocate interrupt %d\n", 190 drvdata->serio.name, drvdata->irq); 191 return retval; 192 } 193 194 /* start reception by enabling the interrupts */ 195 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK); 196 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL); 197 (void)xps2_recv(drvdata, &drvdata->rxb); 198 199 return 0; /* success */ 200 } 201 202 /* 203 * sxps2_close() frees the interrupt. 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 /* Device setup code */ 217 /*********************/ 218 219 static int xps2_setup(struct device *dev, struct resource *regs_res, 220 struct resource *irq_res) 221 { 222 struct xps2data *drvdata; 223 struct serio *serio; 224 unsigned long remap_size; 225 int retval; 226 227 if (!dev) 228 return -EINVAL; 229 230 if (!regs_res || !irq_res) { 231 dev_err(dev, "IO resource(s) not found\n"); 232 return -EINVAL; 233 } 234 235 drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL); 236 if (!drvdata) { 237 dev_err(dev, "Couldn't allocate device private record\n"); 238 return -ENOMEM; 239 } 240 241 dev_set_drvdata(dev, drvdata); 242 243 spin_lock_init(&drvdata->lock); 244 drvdata->irq = irq_res->start; 245 246 remap_size = regs_res->end - regs_res->start + 1; 247 if (!request_mem_region(regs_res->start, remap_size, DRIVER_NAME)) { 248 dev_err(dev, "Couldn't lock memory region at 0x%08X\n", 249 (unsigned int)regs_res->start); 250 retval = -EBUSY; 251 goto failed1; 252 } 253 254 /* Fill in configuration data and add them to the list */ 255 drvdata->phys_addr = regs_res->start; 256 drvdata->remap_size = remap_size; 257 drvdata->base_address = ioremap(regs_res->start, remap_size); 258 if (drvdata->base_address == NULL) { 259 dev_err(dev, "Couldn't ioremap memory at 0x%08X\n", 260 (unsigned int)regs_res->start); 261 retval = -EFAULT; 262 goto failed2; 263 } 264 265 /* Disable all the interrupts, just in case */ 266 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0); 267 268 /* Reset the PS2 device and abort any current transaction, to make sure 269 * we have the PS2 in a good state */ 270 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET); 271 272 dev_info(dev, "Xilinx PS2 at 0x%08X mapped to 0x%08X, irq=%d\n", 273 drvdata->phys_addr, (u32)drvdata->base_address, drvdata->irq); 274 275 serio = &drvdata->serio; 276 serio->id.type = SERIO_8042; 277 serio->write = sxps2_write; 278 serio->open = sxps2_open; 279 serio->close = sxps2_close; 280 serio->port_data = drvdata; 281 serio->dev.parent = dev; 282 snprintf(serio->name, sizeof(serio->name), 283 "Xilinx XPS PS/2 at %08X", drvdata->phys_addr); 284 snprintf(serio->phys, sizeof(serio->phys), 285 "xilinxps2/serio at %08X", drvdata->phys_addr); 286 serio_register_port(serio); 287 288 return 0; /* success */ 289 290 failed2: 291 release_mem_region(regs_res->start, remap_size); 292 failed1: 293 kfree(drvdata); 294 dev_set_drvdata(dev, NULL); 295 296 return retval; 297 } 298 299 /***************************/ 300 /* OF Platform Bus Support */ 301 /***************************/ 302 303 static int __devinit xps2_of_probe(struct of_device *ofdev, const struct 304 of_device_id * match) 305 { 306 struct resource r_irq; /* Interrupt resources */ 307 struct resource r_mem; /* IO mem resources */ 308 int rc = 0; 309 310 printk(KERN_INFO "Device Tree Probing \'%s\'\n", 311 ofdev->node->name); 312 313 /* Get iospace for the device */ 314 rc = of_address_to_resource(ofdev->node, 0, &r_mem); 315 if (rc) { 316 dev_err(&ofdev->dev, "invalid address\n"); 317 return rc; 318 } 319 320 /* Get IRQ for the device */ 321 rc = of_irq_to_resource(ofdev->node, 0, &r_irq); 322 if (rc == NO_IRQ) { 323 dev_err(&ofdev->dev, "no IRQ found\n"); 324 return rc; 325 } 326 327 return xps2_setup(&ofdev->dev, &r_mem, &r_irq); 328 } 329 330 static int __devexit xps2_of_remove(struct of_device *of_dev) 331 { 332 struct device *dev = &of_dev->dev; 333 struct xps2data *drvdata; 334 335 if (!dev) 336 return -EINVAL; 337 338 drvdata = dev_get_drvdata(dev); 339 340 serio_unregister_port(&drvdata->serio); 341 iounmap(drvdata->base_address); 342 release_mem_region(drvdata->phys_addr, drvdata->remap_size); 343 kfree(drvdata); 344 345 dev_set_drvdata(dev, NULL); 346 347 return 0; /* success */ 348 } 349 350 /* Match table for of_platform binding */ 351 static struct of_device_id xps2_of_match[] __devinitdata = { 352 { .compatible = "xlnx,xps-ps2-1.00.a", }, 353 { /* end of list */ }, 354 }; 355 MODULE_DEVICE_TABLE(of, xps2_of_match); 356 357 static struct of_platform_driver xps2_of_driver = { 358 .name = DRIVER_NAME, 359 .match_table = xps2_of_match, 360 .probe = xps2_of_probe, 361 .remove = __devexit_p(xps2_of_remove), 362 }; 363 364 static int __init xps2_init(void) 365 { 366 return of_register_platform_driver(&xps2_of_driver); 367 } 368 369 static void __exit xps2_cleanup(void) 370 { 371 of_unregister_platform_driver(&xps2_of_driver); 372 } 373 374 module_init(xps2_init); 375 module_exit(xps2_cleanup); 376 377 MODULE_AUTHOR("Xilinx, Inc."); 378 MODULE_DESCRIPTION("Xilinx XPS PS/2 driver"); 379 MODULE_LICENSE("GPL"); 380 381