Path: blob/master/drivers/input/serio/xilinx_ps2.c
15111 views
/*1* Xilinx XPS PS/2 device driver2*3* (c) 2005 MontaVista Software, Inc.4* (c) 2008 Xilinx, Inc.5*6* This program is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License as published by the8* Free Software Foundation; either version 2 of the License, or (at your9* option) any later version.10*11* You should have received a copy of the GNU General Public License along12* with this program; if not, write to the Free Software Foundation, Inc.,13* 675 Mass Ave, Cambridge, MA 02139, USA.14*/151617#include <linux/module.h>18#include <linux/serio.h>19#include <linux/interrupt.h>20#include <linux/errno.h>21#include <linux/slab.h>22#include <linux/init.h>23#include <linux/list.h>24#include <linux/io.h>2526#include <linux/of_device.h>27#include <linux/of_platform.h>2829#define DRIVER_NAME "xilinx_ps2"3031/* 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 */3940/* Reset Register Bit Definitions */41#define XPS2_SRST_RESET 0x0000000A /* Software Reset */4243/* Status Register Bit Positions */44#define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */45#define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */4647/* Bit definitions for ISR/IER registers. Both the registers have the same bit48* 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 */5556/* Mask for all the Transmit Interrupts */57#define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)5859/* Mask for all the Receive Interrupts */60#define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \61XPS2_IPIXR_RX_FULL)6263/* Mask for all the Interrupts */64#define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \65XPS2_IPIXR_WDT_TOUT)6667/* Global Interrupt Enable mask */68#define XPS2_GIER_GIE_MASK 0x800000006970struct xps2data {71int irq;72spinlock_t lock;73void __iomem *base_address; /* virt. address of control registers */74unsigned int flags;75struct serio serio; /* serio */76};7778/************************************/79/* XPS PS/2 data transmission calls */80/************************************/8182/**83* xps2_recv() - attempts to receive a byte from the PS/2 port.84* @drvdata: pointer to ps2 device private data structure85* @byte: address where the read data will be copied86*87* If there is any data available in the PS/2 receiver, this functions reads88* the data, otherwise it returns error.89*/90static int xps2_recv(struct xps2data *drvdata, u8 *byte)91{92u32 sr;93int status = -1;9495/* If there is data available in the PS/2 receiver, read it */96sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);97if (sr & XPS2_STATUS_RX_FULL) {98*byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);99status = 0;100}101102return status;103}104105/*********************/106/* Interrupt handler */107/*********************/108static irqreturn_t xps2_interrupt(int irq, void *dev_id)109{110struct xps2data *drvdata = dev_id;111u32 intr_sr;112u8 c;113int status;114115/* Get the PS/2 interrupts and clear them */116intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);117out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);118119/* Check which interrupt is active */120if (intr_sr & XPS2_IPIXR_RX_OVF)121dev_warn(drvdata->serio.dev.parent, "receive overrun error\n");122123if (intr_sr & XPS2_IPIXR_RX_ERR)124drvdata->flags |= SERIO_PARITY;125126if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))127drvdata->flags |= SERIO_TIMEOUT;128129if (intr_sr & XPS2_IPIXR_RX_FULL) {130status = xps2_recv(drvdata, &c);131132/* Error, if a byte is not received */133if (status) {134dev_err(drvdata->serio.dev.parent,135"wrong rcvd byte count (%d)\n", status);136} else {137serio_interrupt(&drvdata->serio, c, drvdata->flags);138drvdata->flags = 0;139}140}141142return IRQ_HANDLED;143}144145/*******************/146/* serio callbacks */147/*******************/148149/**150* sxps2_write() - sends a byte out through the PS/2 port.151* @pserio: pointer to the serio structure of the PS/2 port152* @c: data that needs to be written to the PS/2 port153*154* This function checks if the PS/2 transmitter is empty and sends a byte.155* Otherwise it returns error. Transmission fails only when nothing is connected156* to the PS/2 port. Thats why, we do not try to resend the data in case of a157* failure.158*/159static int sxps2_write(struct serio *pserio, unsigned char c)160{161struct xps2data *drvdata = pserio->port_data;162unsigned long flags;163u32 sr;164int status = -1;165166spin_lock_irqsave(&drvdata->lock, flags);167168/* If the PS/2 transmitter is empty send a byte of data */169sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);170if (!(sr & XPS2_STATUS_TX_FULL)) {171out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);172status = 0;173}174175spin_unlock_irqrestore(&drvdata->lock, flags);176177return status;178}179180/**181* sxps2_open() - called when a port is opened by the higher layer.182* @pserio: pointer to the serio structure of the PS/2 device183*184* This function requests irq and enables interrupts for the PS/2 device.185*/186static int sxps2_open(struct serio *pserio)187{188struct xps2data *drvdata = pserio->port_data;189int error;190u8 c;191192error = request_irq(drvdata->irq, &xps2_interrupt, 0,193DRIVER_NAME, drvdata);194if (error) {195dev_err(drvdata->serio.dev.parent,196"Couldn't allocate interrupt %d\n", drvdata->irq);197return error;198}199200/* start reception by enabling the interrupts */201out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);202out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);203(void)xps2_recv(drvdata, &c);204205return 0; /* success */206}207208/**209* sxps2_close() - frees the interrupt.210* @pserio: pointer to the serio structure of the PS/2 device211*212* This function frees the irq and disables interrupts for the PS/2 device.213*/214static void sxps2_close(struct serio *pserio)215{216struct xps2data *drvdata = pserio->port_data;217218/* Disable the PS2 interrupts */219out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);220out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);221free_irq(drvdata->irq, drvdata);222}223224/**225* xps2_of_probe - probe method for the PS/2 device.226* @of_dev: pointer to OF device structure227* @match: pointer to the structure used for matching a device228*229* This function probes the PS/2 device in the device tree.230* It initializes the driver data structure and the hardware.231* It returns 0, if the driver is bound to the PS/2 device, or a negative232* value if there is an error.233*/234static int __devinit xps2_of_probe(struct platform_device *ofdev)235{236struct resource r_irq; /* Interrupt resources */237struct resource r_mem; /* IO mem resources */238struct xps2data *drvdata;239struct serio *serio;240struct device *dev = &ofdev->dev;241resource_size_t remap_size, phys_addr;242int error;243244dev_info(dev, "Device Tree Probing \'%s\'\n",245ofdev->dev.of_node->name);246247/* Get iospace for the device */248error = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem);249if (error) {250dev_err(dev, "invalid address\n");251return error;252}253254/* Get IRQ for the device */255if (of_irq_to_resource(ofdev->dev.of_node, 0, &r_irq) == NO_IRQ) {256dev_err(dev, "no IRQ found\n");257return -ENODEV;258}259260drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);261if (!drvdata) {262dev_err(dev, "Couldn't allocate device private record\n");263return -ENOMEM;264}265266dev_set_drvdata(dev, drvdata);267268spin_lock_init(&drvdata->lock);269drvdata->irq = r_irq.start;270271phys_addr = r_mem.start;272remap_size = resource_size(&r_mem);273if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {274dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",275(unsigned long long)phys_addr);276error = -EBUSY;277goto failed1;278}279280/* Fill in configuration data and add them to the list */281drvdata->base_address = ioremap(phys_addr, remap_size);282if (drvdata->base_address == NULL) {283dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",284(unsigned long long)phys_addr);285error = -EFAULT;286goto failed2;287}288289/* Disable all the interrupts, just in case */290out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);291292/* Reset the PS2 device and abort any current transaction, to make sure293* we have the PS2 in a good state */294out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);295296dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",297(unsigned long long)phys_addr, drvdata->base_address,298drvdata->irq);299300serio = &drvdata->serio;301serio->id.type = SERIO_8042;302serio->write = sxps2_write;303serio->open = sxps2_open;304serio->close = sxps2_close;305serio->port_data = drvdata;306serio->dev.parent = dev;307snprintf(serio->name, sizeof(serio->name),308"Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);309snprintf(serio->phys, sizeof(serio->phys),310"xilinxps2/serio at %08llX", (unsigned long long)phys_addr);311312serio_register_port(serio);313314return 0; /* success */315316failed2:317release_mem_region(phys_addr, remap_size);318failed1:319kfree(drvdata);320dev_set_drvdata(dev, NULL);321322return error;323}324325/**326* xps2_of_remove - unbinds the driver from the PS/2 device.327* @of_dev: pointer to OF device structure328*329* This function is called if a device is physically removed from the system or330* if the driver module is being unloaded. It frees any resources allocated to331* the device.332*/333static int __devexit xps2_of_remove(struct platform_device *of_dev)334{335struct device *dev = &of_dev->dev;336struct xps2data *drvdata = dev_get_drvdata(dev);337struct resource r_mem; /* IO mem resources */338339serio_unregister_port(&drvdata->serio);340iounmap(drvdata->base_address);341342/* Get iospace of the device */343if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))344dev_err(dev, "invalid address\n");345else346release_mem_region(r_mem.start, resource_size(&r_mem));347348kfree(drvdata);349350dev_set_drvdata(dev, NULL);351352return 0;353}354355/* Match table for of_platform binding */356static const struct of_device_id xps2_of_match[] __devinitconst = {357{ .compatible = "xlnx,xps-ps2-1.00.a", },358{ /* end of list */ },359};360MODULE_DEVICE_TABLE(of, xps2_of_match);361362static struct platform_driver xps2_of_driver = {363.driver = {364.name = DRIVER_NAME,365.owner = THIS_MODULE,366.of_match_table = xps2_of_match,367},368.probe = xps2_of_probe,369.remove = __devexit_p(xps2_of_remove),370};371372static int __init xps2_init(void)373{374return platform_driver_register(&xps2_of_driver);375}376377static void __exit xps2_cleanup(void)378{379platform_driver_unregister(&xps2_of_driver);380}381382module_init(xps2_init);383module_exit(xps2_cleanup);384385MODULE_AUTHOR("Xilinx, Inc.");386MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");387MODULE_LICENSE("GPL");388389390391