sa1111ps2.c 7.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  linux/drivers/input/serio/sa1111ps2.c
 *
 *  Copyright (C) 2002 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 */
#include <linux/module.h>
#include <linux/init.h>
12
#include <linux/input.h>
13 14 15 16 17 18
#include <linux/serio.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/device.h>
19 20
#include <linux/slab.h>
#include <linux/spinlock.h>
21 22 23 24 25

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/system.h>

26 27
#include <asm/hardware/sa1111.h>

28
struct ps2if {
29 30 31 32 33 34 35 36
	struct serio		io;
	struct sa1111_dev	*dev;
	unsigned long		base;
	unsigned int		open;
	spinlock_t		lock;
	unsigned int		head;
	unsigned int		tail;
	unsigned char		buf[4];
37 38 39 40 41 42 43
};

/*
 * Read all bytes waiting in the PS2 port.  There should be
 * at the most one, but we loop for safety.  If there was a
 * framing error, we have to manually clear the status.
 */
44
static irqreturn_t ps2_rxint(int irq, void *dev_id, struct pt_regs *regs)
45
{
46
	struct ps2if *ps2if = dev_id;
47
	unsigned int scancode, flag, status;
48
	int handled = IRQ_NONE;
49

50
	status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
51 52
	while (status & PS2STAT_RXF) {
		if (status & PS2STAT_STP)
53
			sa1111_writel(PS2STAT_STP, ps2if->base + SA1111_PS2STAT);
54 55 56 57

		flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
		       (status & PS2STAT_RXP ? 0 : SERIO_PARITY);

58
		scancode = sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff;
59 60 61 62

		if (hweight8(scancode) & 1)
			flag ^= SERIO_PARITY;

63
		serio_interrupt(&ps2if->io, scancode, flag, regs);
64

65
		status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
66

67
		handled = IRQ_HANDLED;
68
        }
69 70

        return handled;
71 72
}

73 74 75
/*
 * Completion of ps2 write
 */
76
static irqreturn_t ps2_txint(int irq, void *dev_id, struct pt_regs *regs)
77 78 79 80 81 82 83 84 85 86 87 88 89 90
{
	struct ps2if *ps2if = dev_id;
	unsigned int status;

	spin_lock(&ps2if->lock);
	status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
	if (ps2if->head == ps2if->tail) {
		disable_irq(irq);
		/* done */
	} else if (status & PS2STAT_TXE) {
		sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + SA1111_PS2DATA);
		ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
	}
	spin_unlock(&ps2if->lock);
91 92

	return IRQ_HANDLED;
93 94
}

95 96 97 98 99 100
/*
 * Write a byte to the PS2 port.  We have to wait for the
 * port to indicate that the transmitter is empty.
 */
static int ps2_write(struct serio *io, unsigned char val)
{
101 102 103
	struct ps2if *ps2if = io->driver;
	unsigned long flags;
	unsigned int head;
104

105
	spin_lock_irqsave(&ps2if->lock, flags);
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120
	/*
	 * If the TX register is empty, we can go straight out.
	 */
	if (sa1111_readl(ps2if->base + SA1111_PS2STAT) & PS2STAT_TXE) {
		sa1111_writel(val, ps2if->base + SA1111_PS2DATA);
	} else {
		if (ps2if->head == ps2if->tail)
			enable_irq(ps2if->dev->irq[1]);
		head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
		if (head != ps2if->tail) {
			ps2if->buf[ps2if->head] = val;
			ps2if->head = head;
		}
	}
121

122 123
	spin_unlock_irqrestore(&ps2if->lock, flags);
	return 0;
124 125 126 127
}

static int ps2_open(struct serio *io)
{
128
	struct ps2if *ps2if = io->driver;
129 130
	int ret;

131
	sa1111_enable_device(ps2if->dev);
132

133 134
	ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
			  SA1111_DRIVER_NAME(ps2if->dev), ps2if);
135 136
	if (ret) {
		printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
137
			ps2if->dev->irq[0], ret);
138 139 140
		return ret;
	}

141 142 143 144 145 146 147 148
	ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
			  SA1111_DRIVER_NAME(ps2if->dev), ps2if);
	if (ret) {
		printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
			ps2if->dev->irq[1], ret);
		free_irq(ps2if->dev->irq[0], ps2if);
		return ret;
	}
149

150 151
	ps2if->open = 1;

152 153
	enable_irq_wake(ps2if->dev->irq[0]);

154
	sa1111_writel(PS2CR_ENA, ps2if->base + SA1111_PS2CR);
155 156 157 158 159
	return 0;
}

static void ps2_close(struct serio *io)
{
160 161 162
	struct ps2if *ps2if = io->driver;

	sa1111_writel(0, ps2if->base + SA1111_PS2CR);
163

164 165
	disable_irq_wake(ps2if->dev->irq[0]);

166
	ps2if->open = 0;
167

168 169
	free_irq(ps2if->dev->irq[1], ps2if);
	free_irq(ps2if->dev->irq[0], ps2if);
170

171
	sa1111_disable_device(ps2if->dev);
172 173 174 175 176
}

/*
 * Clear the input buffer.
 */
177
static void __init ps2_clear_input(struct ps2if *ps2if)
178 179 180 181
{
	int maxread = 100;

	while (maxread--) {
182
		if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
183 184 185 186 187
			break;
	}
}

static inline unsigned int
188
ps2_test_one(struct ps2if *ps2if, unsigned int mask)
189 190 191
{
	unsigned int val;

192
	sa1111_writel(PS2CR_ENA | mask, ps2if->base + SA1111_PS2CR);
193 194 195

	udelay(2);

196
	val = sa1111_readl(ps2if->base + SA1111_PS2STAT);
197 198 199 200 201 202 203
	return val & (PS2STAT_KBC | PS2STAT_KBD);
}

/*
 * Test the keyboard interface.  We basically check to make sure that
 * we can drive each line to the keyboard independently of each other.
 */
204
static int __init ps2_test(struct ps2if *ps2if)
205 206 207 208
{
	unsigned int stat;
	int ret = 0;

209
	stat = ps2_test_one(ps2if, PS2CR_FKC);
210
	if (stat != PS2STAT_KBD) {
211
		printk("PS/2 interface test failed[1]: %02x\n", stat);
212 213 214
		ret = -ENODEV;
	}

215
	stat = ps2_test_one(ps2if, 0);
216
	if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
217
		printk("PS/2 interface test failed[2]: %02x\n", stat);
218 219 220
		ret = -ENODEV;
	}

221
	stat = ps2_test_one(ps2if, PS2CR_FKD);
222
	if (stat != PS2STAT_KBC) {
223
		printk("PS/2 interface test failed[3]: %02x\n", stat);
224 225 226
		ret = -ENODEV;
	}

227
	sa1111_writel(0, ps2if->base + SA1111_PS2CR);
228 229 230 231 232

	return ret;
}

/*
233
 * Add one device to this driver.
234
 */
235
static int ps2_probe(struct sa1111_dev *dev)
236
{
237
	struct ps2if *ps2if;
238 239
	int ret;

240 241 242 243 244 245 246 247 248 249 250
	ps2if = kmalloc(sizeof(struct ps2if), GFP_KERNEL);
	if (!ps2if) {
		return -ENOMEM;
	}

	memset(ps2if, 0, sizeof(struct ps2if));

	ps2if->io.type		= SERIO_8042;
	ps2if->io.write		= ps2_write;
	ps2if->io.open		= ps2_open;
	ps2if->io.close		= ps2_close;
251 252
	ps2if->io.name		= dev->dev.bus_id;
	ps2if->io.phys		= dev->dev.bus_id;
253
	ps2if->io.driver	= ps2if;
254 255
	ps2if->dev		= dev;
	sa1111_set_drvdata(dev, ps2if);
256 257 258

	spin_lock_init(&ps2if->lock);

259 260 261
	/*
	 * Request the physical region for this PS2 port.
	 */
262 263 264
	if (!request_mem_region(dev->res.start,
				dev->res.end - dev->res.start + 1,
				SA1111_DRIVER_NAME(dev))) {
265 266 267
		ret = -EBUSY;
		goto free;
	}
268 269

	/*
270
	 * Our parent device has already mapped the region.
271
	 */
272
	ps2if->base = (unsigned long)dev->mapbase;
273

274
	sa1111_enable_device(ps2if->dev);
275 276

	/* Incoming clock is 8MHz */
277 278
	sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
	sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
279 280 281 282

	/*
	 * Flush any pending input.
	 */
283
	ps2_clear_input(ps2if);
284 285 286 287

	/*
	 * Test the keyboard interface.
	 */
288
	ret = ps2_test(ps2if);
289 290 291 292 293 294
	if (ret)
		goto out;

	/*
	 * Flush any pending input.
	 */
295
	ps2_clear_input(ps2if);
296

297 298
	sa1111_disable_device(ps2if->dev);
	serio_register_port(&ps2if->io);
299 300 301
	return 0;

 out:
302
	sa1111_disable_device(ps2if->dev);
303 304
	release_mem_region(dev->res.start,
			   dev->res.end - dev->res.start + 1);
305
 free:
306
	sa1111_set_drvdata(dev, NULL);
307
	kfree(ps2if);
308 309 310 311
	return ret;
}

/*
312
 * Remove one device from this driver.
313
 */
314
static int ps2_remove(struct sa1111_dev *dev)
315
{
316
	struct ps2if *ps2if = sa1111_get_drvdata(dev);
317 318

	serio_unregister_port(&ps2if->io);
319 320 321
	release_mem_region(dev->res.start,
			   dev->res.end - dev->res.start + 1);
	sa1111_set_drvdata(dev, NULL);
322

323
	kfree(ps2if);
324

325 326 327 328 329 330 331 332
	return 0;
}

/*
 * Our device driver structure
 */
static struct sa1111_driver ps2_driver = {
	.drv = {
333
		.name	= "sa1111-ps2",
334
	},
335 336 337
	.devid		= SA1111_DEVID_PS2,
	.probe		= ps2_probe,
	.remove		= ps2_remove,
338 339 340 341
};

static int __init ps2_init(void)
{
342
	return sa1111_driver_register(&ps2_driver);
343 344 345 346
}

static void __exit ps2_exit(void)
{
347
	sa1111_driver_unregister(&ps2_driver);
348 349 350 351 352 353 354 355
}

module_init(ps2_init);
module_exit(ps2_exit);

MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
MODULE_DESCRIPTION("SA1111 PS2 controller driver");
MODULE_LICENSE("GPL");