1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/input/serio/sa1111ps2.c
5 * Copyright (C) 2002 Russell King
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/input.h>
10 #include <linux/serio.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/ioport.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
21 #include <asm/hardware/sa1111.h>
24 #define PS2STAT 0x0004
25 #define PS2DATA 0x0008
26 #define PS2CLKDIV 0x000c
27 #define PS2PRECNT 0x0010
29 #define PS2CR_ENA 0x08
30 #define PS2CR_FKD 0x02
31 #define PS2CR_FKC 0x01
33 #define PS2STAT_STP 0x0100
34 #define PS2STAT_TXE 0x0080
35 #define PS2STAT_TXB 0x0040
36 #define PS2STAT_RXF 0x0020
37 #define PS2STAT_RXB 0x0010
38 #define PS2STAT_ENA 0x0008
39 #define PS2STAT_RXP 0x0004
40 #define PS2STAT_KBD 0x0002
41 #define PS2STAT_KBC 0x0001
45 struct sa1111_dev *dev;
57 * Read all bytes waiting in the PS2 port. There should be
58 * at the most one, but we loop for safety. If there was a
59 * framing error, we have to manually clear the status.
61 static irqreturn_t ps2_rxint(int irq, void *dev_id)
63 struct ps2if *ps2if = dev_id;
64 unsigned int scancode, flag, status;
66 status = readl_relaxed(ps2if->base + PS2STAT);
67 while (status & PS2STAT_RXF) {
68 if (status & PS2STAT_STP)
69 writel_relaxed(PS2STAT_STP, ps2if->base + PS2STAT);
71 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
72 (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
74 scancode = readl_relaxed(ps2if->base + PS2DATA) & 0xff;
76 if (hweight8(scancode) & 1)
79 serio_interrupt(ps2if->io, scancode, flag);
81 status = readl_relaxed(ps2if->base + PS2STAT);
88 * Completion of ps2 write
90 static irqreturn_t ps2_txint(int irq, void *dev_id)
92 struct ps2if *ps2if = dev_id;
95 spin_lock(&ps2if->lock);
96 status = readl_relaxed(ps2if->base + PS2STAT);
97 if (ps2if->head == ps2if->tail) {
98 disable_irq_nosync(irq);
100 } else if (status & PS2STAT_TXE) {
101 writel_relaxed(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
102 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
104 spin_unlock(&ps2if->lock);
110 * Write a byte to the PS2 port. We have to wait for the
111 * port to indicate that the transmitter is empty.
113 static int ps2_write(struct serio *io, unsigned char val)
115 struct ps2if *ps2if = io->port_data;
119 spin_lock_irqsave(&ps2if->lock, flags);
122 * If the TX register is empty, we can go straight out.
124 if (readl_relaxed(ps2if->base + PS2STAT) & PS2STAT_TXE) {
125 writel_relaxed(val, ps2if->base + PS2DATA);
127 if (ps2if->head == ps2if->tail)
128 enable_irq(ps2if->tx_irq);
129 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
130 if (head != ps2if->tail) {
131 ps2if->buf[ps2if->head] = val;
136 spin_unlock_irqrestore(&ps2if->lock, flags);
140 static int ps2_open(struct serio *io)
142 struct ps2if *ps2if = io->port_data;
145 ret = sa1111_enable_device(ps2if->dev);
149 ret = request_irq(ps2if->rx_irq, ps2_rxint, 0,
150 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
152 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
154 sa1111_disable_device(ps2if->dev);
158 ret = request_irq(ps2if->tx_irq, ps2_txint, 0,
159 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
161 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
163 free_irq(ps2if->rx_irq, ps2if);
164 sa1111_disable_device(ps2if->dev);
170 enable_irq_wake(ps2if->rx_irq);
172 writel_relaxed(PS2CR_ENA, ps2if->base + PS2CR);
176 static void ps2_close(struct serio *io)
178 struct ps2if *ps2if = io->port_data;
180 writel_relaxed(0, ps2if->base + PS2CR);
182 disable_irq_wake(ps2if->rx_irq);
186 free_irq(ps2if->tx_irq, ps2if);
187 free_irq(ps2if->rx_irq, ps2if);
189 sa1111_disable_device(ps2if->dev);
193 * Clear the input buffer.
195 static void ps2_clear_input(struct ps2if *ps2if)
200 if ((readl_relaxed(ps2if->base + PS2DATA) & 0xff) == 0xff)
205 static unsigned int ps2_test_one(struct ps2if *ps2if,
210 writel_relaxed(PS2CR_ENA | mask, ps2if->base + PS2CR);
214 val = readl_relaxed(ps2if->base + PS2STAT);
215 return val & (PS2STAT_KBC | PS2STAT_KBD);
219 * Test the keyboard interface. We basically check to make sure that
220 * we can drive each line to the keyboard independently of each other.
222 static int ps2_test(struct ps2if *ps2if)
227 stat = ps2_test_one(ps2if, PS2CR_FKC);
228 if (stat != PS2STAT_KBD) {
229 printk("PS/2 interface test failed[1]: %02x\n", stat);
233 stat = ps2_test_one(ps2if, 0);
234 if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
235 printk("PS/2 interface test failed[2]: %02x\n", stat);
239 stat = ps2_test_one(ps2if, PS2CR_FKD);
240 if (stat != PS2STAT_KBC) {
241 printk("PS/2 interface test failed[3]: %02x\n", stat);
245 writel_relaxed(0, ps2if->base + PS2CR);
251 * Add one device to this driver.
253 static int ps2_probe(struct sa1111_dev *dev)
259 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
260 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
261 if (!ps2if || !serio) {
266 serio->id.type = SERIO_8042;
267 serio->write = ps2_write;
268 serio->open = ps2_open;
269 serio->close = ps2_close;
270 strscpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
271 strscpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
272 serio->port_data = ps2if;
273 serio->dev.parent = &dev->dev;
276 sa1111_set_drvdata(dev, ps2if);
278 spin_lock_init(&ps2if->lock);
280 ps2if->rx_irq = sa1111_get_irq(dev, 0);
281 if (ps2if->rx_irq <= 0) {
282 ret = ps2if->rx_irq ? : -ENXIO;
286 ps2if->tx_irq = sa1111_get_irq(dev, 1);
287 if (ps2if->tx_irq <= 0) {
288 ret = ps2if->tx_irq ? : -ENXIO;
293 * Request the physical region for this PS2 port.
295 if (!request_mem_region(dev->res.start,
296 dev->res.end - dev->res.start + 1,
297 SA1111_DRIVER_NAME(dev))) {
303 * Our parent device has already mapped the region.
305 ps2if->base = dev->mapbase;
307 sa1111_enable_device(ps2if->dev);
309 /* Incoming clock is 8MHz */
310 writel_relaxed(0, ps2if->base + PS2CLKDIV);
311 writel_relaxed(127, ps2if->base + PS2PRECNT);
314 * Flush any pending input.
316 ps2_clear_input(ps2if);
319 * Test the keyboard interface.
321 ret = ps2_test(ps2if);
326 * Flush any pending input.
328 ps2_clear_input(ps2if);
330 sa1111_disable_device(ps2if->dev);
331 serio_register_port(ps2if->io);
335 sa1111_disable_device(ps2if->dev);
336 release_mem_region(dev->res.start, resource_size(&dev->res));
338 sa1111_set_drvdata(dev, NULL);
345 * Remove one device from this driver.
347 static void ps2_remove(struct sa1111_dev *dev)
349 struct ps2if *ps2if = sa1111_get_drvdata(dev);
351 serio_unregister_port(ps2if->io);
352 release_mem_region(dev->res.start, resource_size(&dev->res));
353 sa1111_set_drvdata(dev, NULL);
359 * Our device driver structure
361 static struct sa1111_driver ps2_driver = {
363 .name = "sa1111-ps2",
364 .owner = THIS_MODULE,
366 .devid = SA1111_DEVID_PS2,
368 .remove = ps2_remove,
371 static int __init ps2_init(void)
373 return sa1111_driver_register(&ps2_driver);
376 static void __exit ps2_exit(void)
378 sa1111_driver_unregister(&ps2_driver);
381 module_init(ps2_init);
382 module_exit(ps2_exit);
384 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
385 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
386 MODULE_LICENSE("GPL");