arc_ps2.c 6.5 KB
Newer Older
M
Mischa Jonker 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Driver is originally developed by Pavel Sokolov <psokolov@synopsys.com>
 */

11
#include <linux/err.h>
M
Mischa Jonker 已提交
12 13 14 15 16
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/serio.h>
#include <linux/platform_device.h>
17
#include <linux/of.h>
M
Mischa Jonker 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/slab.h>

#define ARC_PS2_PORTS                   2

#define ARC_ARC_PS2_ID                  0x0001f609

#define STAT_TIMEOUT                    128

#define PS2_STAT_RX_FRM_ERR             (1)
#define PS2_STAT_RX_BUF_OVER            (1 << 1)
#define PS2_STAT_RX_INT_EN              (1 << 2)
#define PS2_STAT_RX_VAL                 (1 << 3)
#define PS2_STAT_TX_ISNOT_FUL           (1 << 4)
#define PS2_STAT_TX_INT_EN              (1 << 5)

struct arc_ps2_port {
	void __iomem *data_addr;
	void __iomem *status_addr;
	struct serio *io;
};

struct arc_ps2_data {
	struct arc_ps2_port port[ARC_PS2_PORTS];
	void __iomem *addr;
	unsigned int frame_error;
	unsigned int buf_overflow;
	unsigned int total_int;
};

static void arc_ps2_check_rx(struct arc_ps2_data *arc_ps2,
			     struct arc_ps2_port *port)
{
	unsigned int timeout = 1000;
	unsigned int flag, status;
	unsigned char data;

	do {
		status = ioread32(port->status_addr);
		if (!(status & PS2_STAT_RX_VAL))
			return;

		data = ioread32(port->data_addr) & 0xff;

		flag = 0;
		arc_ps2->total_int++;
		if (status & PS2_STAT_RX_FRM_ERR) {
			arc_ps2->frame_error++;
			flag |= SERIO_PARITY;
		} else if (status & PS2_STAT_RX_BUF_OVER) {
			arc_ps2->buf_overflow++;
			flag |= SERIO_FRAME;
		}

		serio_interrupt(port->io, data, flag);
	} while (--timeout);

	dev_err(&port->io->dev, "PS/2 hardware stuck\n");
}

static irqreturn_t arc_ps2_interrupt(int irq, void *dev)
{
	struct arc_ps2_data *arc_ps2 = dev;
	int i;

	for (i = 0; i < ARC_PS2_PORTS; i++)
		arc_ps2_check_rx(arc_ps2, &arc_ps2->port[i]);

	return IRQ_HANDLED;
}

static int arc_ps2_write(struct serio *io, unsigned char val)
{
	unsigned status;
	struct arc_ps2_port *port = io->port_data;
	int timeout = STAT_TIMEOUT;

	do {
		status = ioread32(port->status_addr);
		cpu_relax();

		if (status & PS2_STAT_TX_ISNOT_FUL) {
			iowrite32(val & 0xff, port->data_addr);
			return 0;
		}

	} while (--timeout);

	dev_err(&io->dev, "write timeout\n");
	return -ETIMEDOUT;
}

static int arc_ps2_open(struct serio *io)
{
	struct arc_ps2_port *port = io->port_data;

	iowrite32(PS2_STAT_RX_INT_EN, port->status_addr);

	return 0;
}

static void arc_ps2_close(struct serio *io)
{
	struct arc_ps2_port *port = io->port_data;

	iowrite32(ioread32(port->status_addr) & ~PS2_STAT_RX_INT_EN,
		  port->status_addr);
}

B
Bill Pemberton 已提交
128
static void __iomem *arc_ps2_calc_addr(struct arc_ps2_data *arc_ps2,
129 130 131 132 133 134 135 136 137 138 139
						  int index, bool status)
{
	void __iomem *addr;

	addr = arc_ps2->addr + 4 + 4 * index;
	if (status)
		addr += ARC_PS2_PORTS * 4;

	return addr;
}

B
Bill Pemberton 已提交
140
static void arc_ps2_inhibit_ports(struct arc_ps2_data *arc_ps2)
141 142 143 144 145 146 147 148 149 150 151 152 153
{
	void __iomem *addr;
	u32 val;
	int i;

	for (i = 0; i < ARC_PS2_PORTS; i++) {
		addr = arc_ps2_calc_addr(arc_ps2, i, true);
		val = ioread32(addr);
		val &= ~(PS2_STAT_RX_INT_EN | PS2_STAT_TX_INT_EN);
		iowrite32(val, addr);
	}
}

B
Bill Pemberton 已提交
154
static int arc_ps2_create_port(struct platform_device *pdev,
M
Mischa Jonker 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
					 struct arc_ps2_data *arc_ps2,
					 int index)
{
	struct arc_ps2_port *port = &arc_ps2->port[index];
	struct serio *io;

	io = kzalloc(sizeof(struct serio), GFP_KERNEL);
	if (!io)
		return -ENOMEM;

	io->id.type = SERIO_8042;
	io->write = arc_ps2_write;
	io->open = arc_ps2_open;
	io->close = arc_ps2_close;
	snprintf(io->name, sizeof(io->name), "ARC PS/2 port%d", index);
	snprintf(io->phys, sizeof(io->phys), "arc/serio%d", index);
	io->port_data = port;

	port->io = io;

175 176
	port->data_addr = arc_ps2_calc_addr(arc_ps2, index, false);
	port->status_addr = arc_ps2_calc_addr(arc_ps2, index, true);
M
Mischa Jonker 已提交
177 178 179 180 181 182 183 184

	dev_dbg(&pdev->dev, "port%d is allocated (data = 0x%p, status = 0x%p)\n",
		index, port->data_addr, port->status_addr);

	serio_register_port(port->io);
	return 0;
}

B
Bill Pemberton 已提交
185
static int arc_ps2_probe(struct platform_device *pdev)
M
Mischa Jonker 已提交
186 187
{
	struct arc_ps2_data *arc_ps2;
188 189
	struct resource *res;
	int irq;
M
Mischa Jonker 已提交
190 191
	int error, id, i;

192 193
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
M
Mischa Jonker 已提交
194
		dev_err(&pdev->dev, "no IO memory defined\n");
195
		return -EINVAL;
M
Mischa Jonker 已提交
196 197
	}

198 199
	irq = platform_get_irq_byname(pdev, "arc_ps2_irq");
	if (irq < 0) {
M
Mischa Jonker 已提交
200
		dev_err(&pdev->dev, "no IRQ defined\n");
201
		return -EINVAL;
M
Mischa Jonker 已提交
202 203
	}

204 205 206 207 208
	arc_ps2 = devm_kzalloc(&pdev->dev, sizeof(struct arc_ps2_data),
				GFP_KERNEL);
	if (!arc_ps2) {
		dev_err(&pdev->dev, "out of memory\n");
		return -ENOMEM;
M
Mischa Jonker 已提交
209 210
	}

211 212 213
	arc_ps2->addr = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(arc_ps2->addr))
		return PTR_ERR(arc_ps2->addr);
M
Mischa Jonker 已提交
214 215

	dev_info(&pdev->dev, "irq = %d, address = 0x%p, ports = %i\n",
216
		 irq, arc_ps2->addr, ARC_PS2_PORTS);
M
Mischa Jonker 已提交
217 218 219 220

	id = ioread32(arc_ps2->addr);
	if (id != ARC_ARC_PS2_ID) {
		dev_err(&pdev->dev, "device id does not match\n");
221
		return -ENXIO;
M
Mischa Jonker 已提交
222 223
	}

224
	arc_ps2_inhibit_ports(arc_ps2);
M
Mischa Jonker 已提交
225

226 227
	error = devm_request_irq(&pdev->dev, irq, arc_ps2_interrupt,
				 0, "arc_ps2", arc_ps2);
M
Mischa Jonker 已提交
228 229
	if (error) {
		dev_err(&pdev->dev, "Could not allocate IRQ\n");
230 231 232 233 234 235 236 237 238 239
		return error;
	}

	for (i = 0; i < ARC_PS2_PORTS; i++) {
		error = arc_ps2_create_port(pdev, arc_ps2, i);
		if (error) {
			while (--i >= 0)
				serio_unregister_port(arc_ps2->port[i].io);
			return error;
		}
M
Mischa Jonker 已提交
240 241 242 243 244 245 246
	}

	platform_set_drvdata(pdev, arc_ps2);

	return 0;
}

B
Bill Pemberton 已提交
247
static int arc_ps2_remove(struct platform_device *pdev)
M
Mischa Jonker 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
{
	struct arc_ps2_data *arc_ps2 = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < ARC_PS2_PORTS; i++)
		serio_unregister_port(arc_ps2->port[i].io);

	dev_dbg(&pdev->dev, "interrupt count = %i\n", arc_ps2->total_int);
	dev_dbg(&pdev->dev, "frame error count = %i\n", arc_ps2->frame_error);
	dev_dbg(&pdev->dev, "buffer overflow count = %i\n",
		arc_ps2->buf_overflow);

	return 0;
}

263 264 265 266 267 268 269 270
#ifdef CONFIG_OF
static const struct of_device_id arc_ps2_match[] = {
	{ .compatible = "snps,arc_ps2" },
	{ },
};
MODULE_DEVICE_TABLE(of, arc_ps2_match);
#endif

M
Mischa Jonker 已提交
271 272
static struct platform_driver arc_ps2_driver = {
	.driver	= {
273 274 275
		.name		= "arc_ps2",
		.owner		= THIS_MODULE,
		.of_match_table	= of_match_ptr(arc_ps2_match),
M
Mischa Jonker 已提交
276 277
	},
	.probe	= arc_ps2_probe,
B
Bill Pemberton 已提交
278
	.remove	= arc_ps2_remove,
M
Mischa Jonker 已提交
279 280 281 282 283 284 285
};

module_platform_driver(arc_ps2_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pavel Sokolov <psokolov@synopsys.com>");
MODULE_DESCRIPTION("ARC PS/2 Driver");