xref: /openbmc/linux/drivers/input/serio/sa1111ps2.c (revision e3d786a3)
1 /*
2  *  linux/drivers/input/serio/sa1111ps2.c
3  *
4  *  Copyright (C) 2002 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/input.h>
13 #include <linux/serio.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 
22 #include <asm/io.h>
23 
24 #include <asm/hardware/sa1111.h>
25 
26 #define PS2CR		0x0000
27 #define PS2STAT		0x0004
28 #define PS2DATA		0x0008
29 #define PS2CLKDIV	0x000c
30 #define PS2PRECNT	0x0010
31 
32 #define PS2CR_ENA	0x08
33 #define PS2CR_FKD	0x02
34 #define PS2CR_FKC	0x01
35 
36 #define PS2STAT_STP	0x0100
37 #define PS2STAT_TXE	0x0080
38 #define PS2STAT_TXB	0x0040
39 #define PS2STAT_RXF	0x0020
40 #define PS2STAT_RXB	0x0010
41 #define PS2STAT_ENA	0x0008
42 #define PS2STAT_RXP	0x0004
43 #define PS2STAT_KBD	0x0002
44 #define PS2STAT_KBC	0x0001
45 
46 struct ps2if {
47 	struct serio		*io;
48 	struct sa1111_dev	*dev;
49 	void __iomem		*base;
50 	int			rx_irq;
51 	int			tx_irq;
52 	unsigned int		open;
53 	spinlock_t		lock;
54 	unsigned int		head;
55 	unsigned int		tail;
56 	unsigned char		buf[4];
57 };
58 
59 /*
60  * Read all bytes waiting in the PS2 port.  There should be
61  * at the most one, but we loop for safety.  If there was a
62  * framing error, we have to manually clear the status.
63  */
64 static irqreturn_t ps2_rxint(int irq, void *dev_id)
65 {
66 	struct ps2if *ps2if = dev_id;
67 	unsigned int scancode, flag, status;
68 
69 	status = readl_relaxed(ps2if->base + PS2STAT);
70 	while (status & PS2STAT_RXF) {
71 		if (status & PS2STAT_STP)
72 			writel_relaxed(PS2STAT_STP, ps2if->base + PS2STAT);
73 
74 		flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
75 		       (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
76 
77 		scancode = readl_relaxed(ps2if->base + PS2DATA) & 0xff;
78 
79 		if (hweight8(scancode) & 1)
80 			flag ^= SERIO_PARITY;
81 
82 		serio_interrupt(ps2if->io, scancode, flag);
83 
84 		status = readl_relaxed(ps2if->base + PS2STAT);
85         }
86 
87         return IRQ_HANDLED;
88 }
89 
90 /*
91  * Completion of ps2 write
92  */
93 static irqreturn_t ps2_txint(int irq, void *dev_id)
94 {
95 	struct ps2if *ps2if = dev_id;
96 	unsigned int status;
97 
98 	spin_lock(&ps2if->lock);
99 	status = readl_relaxed(ps2if->base + PS2STAT);
100 	if (ps2if->head == ps2if->tail) {
101 		disable_irq_nosync(irq);
102 		/* done */
103 	} else if (status & PS2STAT_TXE) {
104 		writel_relaxed(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
105 		ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
106 	}
107 	spin_unlock(&ps2if->lock);
108 
109 	return IRQ_HANDLED;
110 }
111 
112 /*
113  * Write a byte to the PS2 port.  We have to wait for the
114  * port to indicate that the transmitter is empty.
115  */
116 static int ps2_write(struct serio *io, unsigned char val)
117 {
118 	struct ps2if *ps2if = io->port_data;
119 	unsigned long flags;
120 	unsigned int head;
121 
122 	spin_lock_irqsave(&ps2if->lock, flags);
123 
124 	/*
125 	 * If the TX register is empty, we can go straight out.
126 	 */
127 	if (readl_relaxed(ps2if->base + PS2STAT) & PS2STAT_TXE) {
128 		writel_relaxed(val, ps2if->base + PS2DATA);
129 	} else {
130 		if (ps2if->head == ps2if->tail)
131 			enable_irq(ps2if->tx_irq);
132 		head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
133 		if (head != ps2if->tail) {
134 			ps2if->buf[ps2if->head] = val;
135 			ps2if->head = head;
136 		}
137 	}
138 
139 	spin_unlock_irqrestore(&ps2if->lock, flags);
140 	return 0;
141 }
142 
143 static int ps2_open(struct serio *io)
144 {
145 	struct ps2if *ps2if = io->port_data;
146 	int ret;
147 
148 	ret = sa1111_enable_device(ps2if->dev);
149 	if (ret)
150 		return ret;
151 
152 	ret = request_irq(ps2if->rx_irq, ps2_rxint, 0,
153 			  SA1111_DRIVER_NAME(ps2if->dev), ps2if);
154 	if (ret) {
155 		printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
156 			ps2if->rx_irq, ret);
157 		sa1111_disable_device(ps2if->dev);
158 		return ret;
159 	}
160 
161 	ret = request_irq(ps2if->tx_irq, ps2_txint, 0,
162 			  SA1111_DRIVER_NAME(ps2if->dev), ps2if);
163 	if (ret) {
164 		printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
165 			ps2if->tx_irq, ret);
166 		free_irq(ps2if->rx_irq, ps2if);
167 		sa1111_disable_device(ps2if->dev);
168 		return ret;
169 	}
170 
171 	ps2if->open = 1;
172 
173 	enable_irq_wake(ps2if->rx_irq);
174 
175 	writel_relaxed(PS2CR_ENA, ps2if->base + PS2CR);
176 	return 0;
177 }
178 
179 static void ps2_close(struct serio *io)
180 {
181 	struct ps2if *ps2if = io->port_data;
182 
183 	writel_relaxed(0, ps2if->base + PS2CR);
184 
185 	disable_irq_wake(ps2if->rx_irq);
186 
187 	ps2if->open = 0;
188 
189 	free_irq(ps2if->tx_irq, ps2if);
190 	free_irq(ps2if->rx_irq, ps2if);
191 
192 	sa1111_disable_device(ps2if->dev);
193 }
194 
195 /*
196  * Clear the input buffer.
197  */
198 static void ps2_clear_input(struct ps2if *ps2if)
199 {
200 	int maxread = 100;
201 
202 	while (maxread--) {
203 		if ((readl_relaxed(ps2if->base + PS2DATA) & 0xff) == 0xff)
204 			break;
205 	}
206 }
207 
208 static unsigned int ps2_test_one(struct ps2if *ps2if,
209 					   unsigned int mask)
210 {
211 	unsigned int val;
212 
213 	writel_relaxed(PS2CR_ENA | mask, ps2if->base + PS2CR);
214 
215 	udelay(10);
216 
217 	val = readl_relaxed(ps2if->base + PS2STAT);
218 	return val & (PS2STAT_KBC | PS2STAT_KBD);
219 }
220 
221 /*
222  * Test the keyboard interface.  We basically check to make sure that
223  * we can drive each line to the keyboard independently of each other.
224  */
225 static int ps2_test(struct ps2if *ps2if)
226 {
227 	unsigned int stat;
228 	int ret = 0;
229 
230 	stat = ps2_test_one(ps2if, PS2CR_FKC);
231 	if (stat != PS2STAT_KBD) {
232 		printk("PS/2 interface test failed[1]: %02x\n", stat);
233 		ret = -ENODEV;
234 	}
235 
236 	stat = ps2_test_one(ps2if, 0);
237 	if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
238 		printk("PS/2 interface test failed[2]: %02x\n", stat);
239 		ret = -ENODEV;
240 	}
241 
242 	stat = ps2_test_one(ps2if, PS2CR_FKD);
243 	if (stat != PS2STAT_KBC) {
244 		printk("PS/2 interface test failed[3]: %02x\n", stat);
245 		ret = -ENODEV;
246 	}
247 
248 	writel_relaxed(0, ps2if->base + PS2CR);
249 
250 	return ret;
251 }
252 
253 /*
254  * Add one device to this driver.
255  */
256 static int ps2_probe(struct sa1111_dev *dev)
257 {
258 	struct ps2if *ps2if;
259 	struct serio *serio;
260 	int ret;
261 
262 	ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
263 	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
264 	if (!ps2if || !serio) {
265 		ret = -ENOMEM;
266 		goto free;
267 	}
268 
269 	serio->id.type		= SERIO_8042;
270 	serio->write		= ps2_write;
271 	serio->open		= ps2_open;
272 	serio->close		= ps2_close;
273 	strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
274 	strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
275 	serio->port_data	= ps2if;
276 	serio->dev.parent	= &dev->dev;
277 	ps2if->io		= serio;
278 	ps2if->dev		= dev;
279 	sa1111_set_drvdata(dev, ps2if);
280 
281 	spin_lock_init(&ps2if->lock);
282 
283 	ps2if->rx_irq = sa1111_get_irq(dev, 0);
284 	if (ps2if->rx_irq <= 0) {
285 		ret = ps2if->rx_irq ? : -ENXIO;
286 		goto free;
287 	}
288 
289 	ps2if->tx_irq = sa1111_get_irq(dev, 1);
290 	if (ps2if->tx_irq <= 0) {
291 		ret = ps2if->tx_irq ? : -ENXIO;
292 		goto free;
293 	}
294 
295 	/*
296 	 * Request the physical region for this PS2 port.
297 	 */
298 	if (!request_mem_region(dev->res.start,
299 				dev->res.end - dev->res.start + 1,
300 				SA1111_DRIVER_NAME(dev))) {
301 		ret = -EBUSY;
302 		goto free;
303 	}
304 
305 	/*
306 	 * Our parent device has already mapped the region.
307 	 */
308 	ps2if->base = dev->mapbase;
309 
310 	sa1111_enable_device(ps2if->dev);
311 
312 	/* Incoming clock is 8MHz */
313 	writel_relaxed(0, ps2if->base + PS2CLKDIV);
314 	writel_relaxed(127, ps2if->base + PS2PRECNT);
315 
316 	/*
317 	 * Flush any pending input.
318 	 */
319 	ps2_clear_input(ps2if);
320 
321 	/*
322 	 * Test the keyboard interface.
323 	 */
324 	ret = ps2_test(ps2if);
325 	if (ret)
326 		goto out;
327 
328 	/*
329 	 * Flush any pending input.
330 	 */
331 	ps2_clear_input(ps2if);
332 
333 	sa1111_disable_device(ps2if->dev);
334 	serio_register_port(ps2if->io);
335 	return 0;
336 
337  out:
338 	sa1111_disable_device(ps2if->dev);
339 	release_mem_region(dev->res.start, resource_size(&dev->res));
340  free:
341 	sa1111_set_drvdata(dev, NULL);
342 	kfree(ps2if);
343 	kfree(serio);
344 	return ret;
345 }
346 
347 /*
348  * Remove one device from this driver.
349  */
350 static int ps2_remove(struct sa1111_dev *dev)
351 {
352 	struct ps2if *ps2if = sa1111_get_drvdata(dev);
353 
354 	serio_unregister_port(ps2if->io);
355 	release_mem_region(dev->res.start, resource_size(&dev->res));
356 	sa1111_set_drvdata(dev, NULL);
357 
358 	kfree(ps2if);
359 
360 	return 0;
361 }
362 
363 /*
364  * Our device driver structure
365  */
366 static struct sa1111_driver ps2_driver = {
367 	.drv = {
368 		.name	= "sa1111-ps2",
369 		.owner	= THIS_MODULE,
370 	},
371 	.devid		= SA1111_DEVID_PS2,
372 	.probe		= ps2_probe,
373 	.remove		= ps2_remove,
374 };
375 
376 static int __init ps2_init(void)
377 {
378 	return sa1111_driver_register(&ps2_driver);
379 }
380 
381 static void __exit ps2_exit(void)
382 {
383 	sa1111_driver_unregister(&ps2_driver);
384 }
385 
386 module_init(ps2_init);
387 module_exit(ps2_exit);
388 
389 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
390 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
391 MODULE_LICENSE("GPL");
392