1a912e80bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21191828fSJohn Linn /*
31191828fSJohn Linn * Xilinx XPS PS/2 device driver
41191828fSJohn Linn *
51191828fSJohn Linn * (c) 2005 MontaVista Software, Inc.
61191828fSJohn Linn * (c) 2008 Xilinx, Inc.
71191828fSJohn Linn */
81191828fSJohn Linn
91191828fSJohn Linn
101191828fSJohn Linn #include <linux/module.h>
111191828fSJohn Linn #include <linux/serio.h>
121191828fSJohn Linn #include <linux/interrupt.h>
131191828fSJohn Linn #include <linux/errno.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
151191828fSJohn Linn #include <linux/list.h>
161191828fSJohn Linn #include <linux/io.h>
17dbce1a7dSRob Herring #include <linux/mod_devicetable.h>
185f9288afSMichal Simek #include <linux/of_address.h>
195af50730SRob Herring #include <linux/of_irq.h>
20dbce1a7dSRob Herring #include <linux/platform_device.h>
211191828fSJohn Linn
221191828fSJohn Linn #define DRIVER_NAME "xilinx_ps2"
231191828fSJohn Linn
241191828fSJohn Linn /* Register offsets for the xps2 device */
251191828fSJohn Linn #define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */
261191828fSJohn Linn #define XPS2_STATUS_OFFSET 0x00000004 /* Status register */
271191828fSJohn Linn #define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */
281191828fSJohn Linn #define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */
291191828fSJohn Linn #define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */
301191828fSJohn Linn #define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */
311191828fSJohn Linn #define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */
321191828fSJohn Linn
331191828fSJohn Linn /* Reset Register Bit Definitions */
341191828fSJohn Linn #define XPS2_SRST_RESET 0x0000000A /* Software Reset */
351191828fSJohn Linn
361191828fSJohn Linn /* Status Register Bit Positions */
371191828fSJohn Linn #define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */
381191828fSJohn Linn #define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */
391191828fSJohn Linn
40da1a42cdSMichal Simek /*
41da1a42cdSMichal Simek * Bit definitions for ISR/IER registers. Both the registers have the same bit
42da1a42cdSMichal Simek * definitions and are only defined once.
43da1a42cdSMichal Simek */
441191828fSJohn Linn #define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */
451191828fSJohn Linn #define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */
461191828fSJohn Linn #define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */
471191828fSJohn Linn #define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */
481191828fSJohn Linn #define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */
491191828fSJohn Linn #define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */
501191828fSJohn Linn
511191828fSJohn Linn /* Mask for all the Transmit Interrupts */
521191828fSJohn Linn #define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
531191828fSJohn Linn
541191828fSJohn Linn /* Mask for all the Receive Interrupts */
551191828fSJohn Linn #define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
561191828fSJohn Linn XPS2_IPIXR_RX_FULL)
571191828fSJohn Linn
581191828fSJohn Linn /* Mask for all the Interrupts */
591191828fSJohn Linn #define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
601191828fSJohn Linn XPS2_IPIXR_WDT_TOUT)
611191828fSJohn Linn
621191828fSJohn Linn /* Global Interrupt Enable mask */
631191828fSJohn Linn #define XPS2_GIER_GIE_MASK 0x80000000
641191828fSJohn Linn
651191828fSJohn Linn struct xps2data {
661191828fSJohn Linn int irq;
671191828fSJohn Linn spinlock_t lock;
681191828fSJohn Linn void __iomem *base_address; /* virt. address of control registers */
69b4a3ac9aSJohn Linn unsigned int flags;
70271002caSDmitry Torokhov struct serio *serio; /* serio */
71271002caSDmitry Torokhov struct device *dev;
721191828fSJohn Linn };
731191828fSJohn Linn
741191828fSJohn Linn /************************************/
751191828fSJohn Linn /* XPS PS/2 data transmission calls */
761191828fSJohn Linn /************************************/
771191828fSJohn Linn
78b4a3ac9aSJohn Linn /**
79b4a3ac9aSJohn Linn * xps2_recv() - attempts to receive a byte from the PS/2 port.
80b4a3ac9aSJohn Linn * @drvdata: pointer to ps2 device private data structure
81b4a3ac9aSJohn Linn * @byte: address where the read data will be copied
82b4a3ac9aSJohn Linn *
83b4a3ac9aSJohn Linn * If there is any data available in the PS/2 receiver, this functions reads
84b4a3ac9aSJohn Linn * the data, otherwise it returns error.
851191828fSJohn Linn */
xps2_recv(struct xps2data * drvdata,u8 * byte)861191828fSJohn Linn static int xps2_recv(struct xps2data *drvdata, u8 *byte)
871191828fSJohn Linn {
881191828fSJohn Linn u32 sr;
891191828fSJohn Linn int status = -1;
901191828fSJohn Linn
911191828fSJohn Linn /* If there is data available in the PS/2 receiver, read it */
921191828fSJohn Linn sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
931191828fSJohn Linn if (sr & XPS2_STATUS_RX_FULL) {
941191828fSJohn Linn *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
951191828fSJohn Linn status = 0;
961191828fSJohn Linn }
971191828fSJohn Linn
981191828fSJohn Linn return status;
991191828fSJohn Linn }
1001191828fSJohn Linn
1011191828fSJohn Linn /*********************/
1021191828fSJohn Linn /* Interrupt handler */
1031191828fSJohn Linn /*********************/
xps2_interrupt(int irq,void * dev_id)1041191828fSJohn Linn static irqreturn_t xps2_interrupt(int irq, void *dev_id)
1051191828fSJohn Linn {
1061191828fSJohn Linn struct xps2data *drvdata = dev_id;
1071191828fSJohn Linn u32 intr_sr;
1081191828fSJohn Linn u8 c;
1091191828fSJohn Linn int status;
1101191828fSJohn Linn
1111191828fSJohn Linn /* Get the PS/2 interrupts and clear them */
1121191828fSJohn Linn intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
1131191828fSJohn Linn out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
1141191828fSJohn Linn
1151191828fSJohn Linn /* Check which interrupt is active */
1161191828fSJohn Linn if (intr_sr & XPS2_IPIXR_RX_OVF)
117271002caSDmitry Torokhov dev_warn(drvdata->dev, "receive overrun error\n");
1181191828fSJohn Linn
1191191828fSJohn Linn if (intr_sr & XPS2_IPIXR_RX_ERR)
120b4a3ac9aSJohn Linn drvdata->flags |= SERIO_PARITY;
1211191828fSJohn Linn
1221191828fSJohn Linn if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
123b4a3ac9aSJohn Linn drvdata->flags |= SERIO_TIMEOUT;
1241191828fSJohn Linn
1251191828fSJohn Linn if (intr_sr & XPS2_IPIXR_RX_FULL) {
126b4a3ac9aSJohn Linn status = xps2_recv(drvdata, &c);
1271191828fSJohn Linn
1281191828fSJohn Linn /* Error, if a byte is not received */
1291191828fSJohn Linn if (status) {
130271002caSDmitry Torokhov dev_err(drvdata->dev,
131b4a3ac9aSJohn Linn "wrong rcvd byte count (%d)\n", status);
1321191828fSJohn Linn } else {
133271002caSDmitry Torokhov serio_interrupt(drvdata->serio, c, drvdata->flags);
134b4a3ac9aSJohn Linn drvdata->flags = 0;
1351191828fSJohn Linn }
1361191828fSJohn Linn }
1371191828fSJohn Linn
1381191828fSJohn Linn return IRQ_HANDLED;
1391191828fSJohn Linn }
1401191828fSJohn Linn
1411191828fSJohn Linn /*******************/
1421191828fSJohn Linn /* serio callbacks */
1431191828fSJohn Linn /*******************/
1441191828fSJohn Linn
145b4a3ac9aSJohn Linn /**
146b4a3ac9aSJohn Linn * sxps2_write() - sends a byte out through the PS/2 port.
147b4a3ac9aSJohn Linn * @pserio: pointer to the serio structure of the PS/2 port
148b4a3ac9aSJohn Linn * @c: data that needs to be written to the PS/2 port
149b4a3ac9aSJohn Linn *
150b4a3ac9aSJohn Linn * This function checks if the PS/2 transmitter is empty and sends a byte.
151b4a3ac9aSJohn Linn * Otherwise it returns error. Transmission fails only when nothing is connected
152b4a3ac9aSJohn Linn * to the PS/2 port. Thats why, we do not try to resend the data in case of a
153b4a3ac9aSJohn Linn * failure.
1541191828fSJohn Linn */
sxps2_write(struct serio * pserio,unsigned char c)1551191828fSJohn Linn static int sxps2_write(struct serio *pserio, unsigned char c)
1561191828fSJohn Linn {
1571191828fSJohn Linn struct xps2data *drvdata = pserio->port_data;
1581191828fSJohn Linn unsigned long flags;
1591191828fSJohn Linn u32 sr;
1601191828fSJohn Linn int status = -1;
1611191828fSJohn Linn
1621191828fSJohn Linn spin_lock_irqsave(&drvdata->lock, flags);
1631191828fSJohn Linn
1641191828fSJohn Linn /* If the PS/2 transmitter is empty send a byte of data */
1651191828fSJohn Linn sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
1661191828fSJohn Linn if (!(sr & XPS2_STATUS_TX_FULL)) {
1671191828fSJohn Linn out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
1681191828fSJohn Linn status = 0;
1691191828fSJohn Linn }
1701191828fSJohn Linn
1711191828fSJohn Linn spin_unlock_irqrestore(&drvdata->lock, flags);
1721191828fSJohn Linn
1731191828fSJohn Linn return status;
1741191828fSJohn Linn }
1751191828fSJohn Linn
176b4a3ac9aSJohn Linn /**
177b4a3ac9aSJohn Linn * sxps2_open() - called when a port is opened by the higher layer.
178b4a3ac9aSJohn Linn * @pserio: pointer to the serio structure of the PS/2 device
179b4a3ac9aSJohn Linn *
180b4a3ac9aSJohn Linn * This function requests irq and enables interrupts for the PS/2 device.
1811191828fSJohn Linn */
sxps2_open(struct serio * pserio)1821191828fSJohn Linn static int sxps2_open(struct serio *pserio)
1831191828fSJohn Linn {
1841191828fSJohn Linn struct xps2data *drvdata = pserio->port_data;
185b4a3ac9aSJohn Linn int error;
186b4a3ac9aSJohn Linn u8 c;
1871191828fSJohn Linn
188b4a3ac9aSJohn Linn error = request_irq(drvdata->irq, &xps2_interrupt, 0,
1891191828fSJohn Linn DRIVER_NAME, drvdata);
190b4a3ac9aSJohn Linn if (error) {
191271002caSDmitry Torokhov dev_err(drvdata->dev,
192b4a3ac9aSJohn Linn "Couldn't allocate interrupt %d\n", drvdata->irq);
193b4a3ac9aSJohn Linn return error;
1941191828fSJohn Linn }
1951191828fSJohn Linn
1961191828fSJohn Linn /* start reception by enabling the interrupts */
1971191828fSJohn Linn out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
1981191828fSJohn Linn out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
199b4a3ac9aSJohn Linn (void)xps2_recv(drvdata, &c);
2001191828fSJohn Linn
2011191828fSJohn Linn return 0; /* success */
2021191828fSJohn Linn }
2031191828fSJohn Linn
204b4a3ac9aSJohn Linn /**
205b4a3ac9aSJohn Linn * sxps2_close() - frees the interrupt.
206b4a3ac9aSJohn Linn * @pserio: pointer to the serio structure of the PS/2 device
207b4a3ac9aSJohn Linn *
208b4a3ac9aSJohn Linn * This function frees the irq and disables interrupts for the PS/2 device.
2091191828fSJohn Linn */
sxps2_close(struct serio * pserio)2101191828fSJohn Linn static void sxps2_close(struct serio *pserio)
2111191828fSJohn Linn {
2121191828fSJohn Linn struct xps2data *drvdata = pserio->port_data;
2131191828fSJohn Linn
2141191828fSJohn Linn /* Disable the PS2 interrupts */
2151191828fSJohn Linn out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
2161191828fSJohn Linn out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
2171191828fSJohn Linn free_irq(drvdata->irq, drvdata);
2181191828fSJohn Linn }
2191191828fSJohn Linn
220b4a3ac9aSJohn Linn /**
221b4a3ac9aSJohn Linn * xps2_of_probe - probe method for the PS/2 device.
222d49193beSYang Li * @ofdev: pointer to OF device structure
223b4a3ac9aSJohn Linn *
224b4a3ac9aSJohn Linn * This function probes the PS/2 device in the device tree.
225b4a3ac9aSJohn Linn * It initializes the driver data structure and the hardware.
226b4a3ac9aSJohn Linn * It returns 0, if the driver is bound to the PS/2 device, or a negative
227b4a3ac9aSJohn Linn * value if there is an error.
228b4a3ac9aSJohn Linn */
xps2_of_probe(struct platform_device * ofdev)2295298cc4cSBill Pemberton static int xps2_of_probe(struct platform_device *ofdev)
2301191828fSJohn Linn {
231b4a3ac9aSJohn Linn struct resource r_mem; /* IO mem resources */
2321191828fSJohn Linn struct xps2data *drvdata;
2331191828fSJohn Linn struct serio *serio;
234b4a3ac9aSJohn Linn struct device *dev = &ofdev->dev;
235b4a3ac9aSJohn Linn resource_size_t remap_size, phys_addr;
236f7578496SThierry Reding unsigned int irq;
237b4a3ac9aSJohn Linn int error;
2381191828fSJohn Linn
2395d68fa58SRob Herring dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
2401191828fSJohn Linn
241b4a3ac9aSJohn Linn /* Get iospace for the device */
24232237677SGuenter Roeck error = of_address_to_resource(dev->of_node, 0, &r_mem);
243b4a3ac9aSJohn Linn if (error) {
244b4a3ac9aSJohn Linn dev_err(dev, "invalid address\n");
245b4a3ac9aSJohn Linn return error;
246b4a3ac9aSJohn Linn }
247b4a3ac9aSJohn Linn
248b4a3ac9aSJohn Linn /* Get IRQ for the device */
24932237677SGuenter Roeck irq = irq_of_parse_and_map(dev->of_node, 0);
250f7578496SThierry Reding if (!irq) {
251b4a3ac9aSJohn Linn dev_err(dev, "no IRQ found\n");
252b4a3ac9aSJohn Linn return -ENODEV;
2531191828fSJohn Linn }
2541191828fSJohn Linn
255*06b449d7SErick Archer drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
256*06b449d7SErick Archer serio = kzalloc(sizeof(*serio), GFP_KERNEL);
257271002caSDmitry Torokhov if (!drvdata || !serio) {
258271002caSDmitry Torokhov error = -ENOMEM;
259271002caSDmitry Torokhov goto failed1;
2601191828fSJohn Linn }
2611191828fSJohn Linn
2621191828fSJohn Linn spin_lock_init(&drvdata->lock);
263f7578496SThierry Reding drvdata->irq = irq;
264271002caSDmitry Torokhov drvdata->serio = serio;
265271002caSDmitry Torokhov drvdata->dev = dev;
2661191828fSJohn Linn
267b4a3ac9aSJohn Linn phys_addr = r_mem.start;
268ce841b94STobias Klauser remap_size = resource_size(&r_mem);
269b4a3ac9aSJohn Linn if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
270b4a3ac9aSJohn Linn dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
271b4a3ac9aSJohn Linn (unsigned long long)phys_addr);
272b4a3ac9aSJohn Linn error = -EBUSY;
2731191828fSJohn Linn goto failed1;
2741191828fSJohn Linn }
2751191828fSJohn Linn
2761191828fSJohn Linn /* Fill in configuration data and add them to the list */
277b4a3ac9aSJohn Linn drvdata->base_address = ioremap(phys_addr, remap_size);
2781191828fSJohn Linn if (drvdata->base_address == NULL) {
279b4a3ac9aSJohn Linn dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
280b4a3ac9aSJohn Linn (unsigned long long)phys_addr);
281b4a3ac9aSJohn Linn error = -EFAULT;
2821191828fSJohn Linn goto failed2;
2831191828fSJohn Linn }
2841191828fSJohn Linn
2851191828fSJohn Linn /* Disable all the interrupts, just in case */
2861191828fSJohn Linn out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
2871191828fSJohn Linn
288da1a42cdSMichal Simek /*
289da1a42cdSMichal Simek * Reset the PS2 device and abort any current transaction,
290da1a42cdSMichal Simek * to make sure we have the PS2 in a good state.
291da1a42cdSMichal Simek */
2921191828fSJohn Linn out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
2931191828fSJohn Linn
294b4a3ac9aSJohn Linn dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
295b4a3ac9aSJohn Linn (unsigned long long)phys_addr, drvdata->base_address,
296b4a3ac9aSJohn Linn drvdata->irq);
2971191828fSJohn Linn
2981191828fSJohn Linn serio->id.type = SERIO_8042;
2991191828fSJohn Linn serio->write = sxps2_write;
3001191828fSJohn Linn serio->open = sxps2_open;
3011191828fSJohn Linn serio->close = sxps2_close;
3021191828fSJohn Linn serio->port_data = drvdata;
3031191828fSJohn Linn serio->dev.parent = dev;
3041191828fSJohn Linn snprintf(serio->name, sizeof(serio->name),
305b4a3ac9aSJohn Linn "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
3061191828fSJohn Linn snprintf(serio->phys, sizeof(serio->phys),
307b4a3ac9aSJohn Linn "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
308b4a3ac9aSJohn Linn
3091191828fSJohn Linn serio_register_port(serio);
3101191828fSJohn Linn
311271002caSDmitry Torokhov platform_set_drvdata(ofdev, drvdata);
3121191828fSJohn Linn return 0; /* success */
3131191828fSJohn Linn
3141191828fSJohn Linn failed2:
315b4a3ac9aSJohn Linn release_mem_region(phys_addr, remap_size);
3161191828fSJohn Linn failed1:
317271002caSDmitry Torokhov kfree(serio);
3181191828fSJohn Linn kfree(drvdata);
3191191828fSJohn Linn
320b4a3ac9aSJohn Linn return error;
3211191828fSJohn Linn }
3221191828fSJohn Linn
323b4a3ac9aSJohn Linn /**
324b4a3ac9aSJohn Linn * xps2_of_remove - unbinds the driver from the PS/2 device.
325b4a3ac9aSJohn Linn * @of_dev: pointer to OF device structure
326b4a3ac9aSJohn Linn *
327b4a3ac9aSJohn Linn * This function is called if a device is physically removed from the system or
328b4a3ac9aSJohn Linn * if the driver module is being unloaded. It frees any resources allocated to
329b4a3ac9aSJohn Linn * the device.
330b4a3ac9aSJohn Linn */
xps2_of_remove(struct platform_device * of_dev)3318df82842SUwe Kleine-König static void xps2_of_remove(struct platform_device *of_dev)
3321191828fSJohn Linn {
333271002caSDmitry Torokhov struct xps2data *drvdata = platform_get_drvdata(of_dev);
334b4a3ac9aSJohn Linn struct resource r_mem; /* IO mem resources */
3351191828fSJohn Linn
336271002caSDmitry Torokhov serio_unregister_port(drvdata->serio);
3371191828fSJohn Linn iounmap(drvdata->base_address);
338b4a3ac9aSJohn Linn
339b4a3ac9aSJohn Linn /* Get iospace of the device */
34061c7a080SGrant Likely if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
341271002caSDmitry Torokhov dev_err(drvdata->dev, "invalid address\n");
342b4a3ac9aSJohn Linn else
343ce841b94STobias Klauser release_mem_region(r_mem.start, resource_size(&r_mem));
344b4a3ac9aSJohn Linn
3451191828fSJohn Linn kfree(drvdata);
3461191828fSJohn Linn }
3471191828fSJohn Linn
3481191828fSJohn Linn /* Match table for of_platform binding */
34978f50c24SBill Pemberton static const struct of_device_id xps2_of_match[] = {
3501191828fSJohn Linn { .compatible = "xlnx,xps-ps2-1.00.a", },
3511191828fSJohn Linn { /* end of list */ },
3521191828fSJohn Linn };
3531191828fSJohn Linn MODULE_DEVICE_TABLE(of, xps2_of_match);
3541191828fSJohn Linn
3551c48a5c9SGrant Likely static struct platform_driver xps2_of_driver = {
3564018294bSGrant Likely .driver = {
3571191828fSJohn Linn .name = DRIVER_NAME,
3584018294bSGrant Likely .of_match_table = xps2_of_match,
3594018294bSGrant Likely },
3601191828fSJohn Linn .probe = xps2_of_probe,
3618df82842SUwe Kleine-König .remove_new = xps2_of_remove,
3621191828fSJohn Linn };
36324d2469aSJJ Ding module_platform_driver(xps2_of_driver);
3641191828fSJohn Linn
3651191828fSJohn Linn MODULE_AUTHOR("Xilinx, Inc.");
3661191828fSJohn Linn MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
3671191828fSJohn Linn MODULE_LICENSE("GPL");
3681191828fSJohn Linn
369