xref: /openbmc/linux/drivers/input/serio/arc_ps2.c (revision a4d6983c)
1 /*
2  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Driver is originally developed by Pavel Sokolov <psokolov@synopsys.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/input.h>
14 #include <linux/serio.h>
15 #include <linux/platform_device.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 
20 #define ARC_PS2_PORTS                   2
21 
22 #define ARC_ARC_PS2_ID                  0x0001f609
23 
24 #define STAT_TIMEOUT                    128
25 
26 #define PS2_STAT_RX_FRM_ERR             (1)
27 #define PS2_STAT_RX_BUF_OVER            (1 << 1)
28 #define PS2_STAT_RX_INT_EN              (1 << 2)
29 #define PS2_STAT_RX_VAL                 (1 << 3)
30 #define PS2_STAT_TX_ISNOT_FUL           (1 << 4)
31 #define PS2_STAT_TX_INT_EN              (1 << 5)
32 
33 struct arc_ps2_port {
34 	void __iomem *data_addr;
35 	void __iomem *status_addr;
36 	struct serio *io;
37 };
38 
39 struct arc_ps2_data {
40 	struct arc_ps2_port port[ARC_PS2_PORTS];
41 	void __iomem *addr;
42 	unsigned int frame_error;
43 	unsigned int buf_overflow;
44 	unsigned int total_int;
45 };
46 
47 static void arc_ps2_check_rx(struct arc_ps2_data *arc_ps2,
48 			     struct arc_ps2_port *port)
49 {
50 	unsigned int timeout = 1000;
51 	unsigned int flag, status;
52 	unsigned char data;
53 
54 	do {
55 		status = ioread32(port->status_addr);
56 		if (!(status & PS2_STAT_RX_VAL))
57 			return;
58 
59 		data = ioread32(port->data_addr) & 0xff;
60 
61 		flag = 0;
62 		arc_ps2->total_int++;
63 		if (status & PS2_STAT_RX_FRM_ERR) {
64 			arc_ps2->frame_error++;
65 			flag |= SERIO_PARITY;
66 		} else if (status & PS2_STAT_RX_BUF_OVER) {
67 			arc_ps2->buf_overflow++;
68 			flag |= SERIO_FRAME;
69 		}
70 
71 		serio_interrupt(port->io, data, flag);
72 	} while (--timeout);
73 
74 	dev_err(&port->io->dev, "PS/2 hardware stuck\n");
75 }
76 
77 static irqreturn_t arc_ps2_interrupt(int irq, void *dev)
78 {
79 	struct arc_ps2_data *arc_ps2 = dev;
80 	int i;
81 
82 	for (i = 0; i < ARC_PS2_PORTS; i++)
83 		arc_ps2_check_rx(arc_ps2, &arc_ps2->port[i]);
84 
85 	return IRQ_HANDLED;
86 }
87 
88 static int arc_ps2_write(struct serio *io, unsigned char val)
89 {
90 	unsigned status;
91 	struct arc_ps2_port *port = io->port_data;
92 	int timeout = STAT_TIMEOUT;
93 
94 	do {
95 		status = ioread32(port->status_addr);
96 		cpu_relax();
97 
98 		if (status & PS2_STAT_TX_ISNOT_FUL) {
99 			iowrite32(val & 0xff, port->data_addr);
100 			return 0;
101 		}
102 
103 	} while (--timeout);
104 
105 	dev_err(&io->dev, "write timeout\n");
106 	return -ETIMEDOUT;
107 }
108 
109 static int arc_ps2_open(struct serio *io)
110 {
111 	struct arc_ps2_port *port = io->port_data;
112 
113 	iowrite32(PS2_STAT_RX_INT_EN, port->status_addr);
114 
115 	return 0;
116 }
117 
118 static void arc_ps2_close(struct serio *io)
119 {
120 	struct arc_ps2_port *port = io->port_data;
121 
122 	iowrite32(ioread32(port->status_addr) & ~PS2_STAT_RX_INT_EN,
123 		  port->status_addr);
124 }
125 
126 static void __iomem * __devinit arc_ps2_calc_addr(struct arc_ps2_data *arc_ps2,
127 						  int index, bool status)
128 {
129 	void __iomem *addr;
130 
131 	addr = arc_ps2->addr + 4 + 4 * index;
132 	if (status)
133 		addr += ARC_PS2_PORTS * 4;
134 
135 	return addr;
136 }
137 
138 static void __devinit arc_ps2_inhibit_ports(struct arc_ps2_data *arc_ps2)
139 {
140 	void __iomem *addr;
141 	u32 val;
142 	int i;
143 
144 	for (i = 0; i < ARC_PS2_PORTS; i++) {
145 		addr = arc_ps2_calc_addr(arc_ps2, i, true);
146 		val = ioread32(addr);
147 		val &= ~(PS2_STAT_RX_INT_EN | PS2_STAT_TX_INT_EN);
148 		iowrite32(val, addr);
149 	}
150 }
151 
152 static int __devinit arc_ps2_create_port(struct platform_device *pdev,
153 					 struct arc_ps2_data *arc_ps2,
154 					 int index)
155 {
156 	struct arc_ps2_port *port = &arc_ps2->port[index];
157 	struct serio *io;
158 
159 	io = kzalloc(sizeof(struct serio), GFP_KERNEL);
160 	if (!io)
161 		return -ENOMEM;
162 
163 	io->id.type = SERIO_8042;
164 	io->write = arc_ps2_write;
165 	io->open = arc_ps2_open;
166 	io->close = arc_ps2_close;
167 	snprintf(io->name, sizeof(io->name), "ARC PS/2 port%d", index);
168 	snprintf(io->phys, sizeof(io->phys), "arc/serio%d", index);
169 	io->port_data = port;
170 
171 	port->io = io;
172 
173 	port->data_addr = arc_ps2_calc_addr(arc_ps2, index, false);
174 	port->status_addr = arc_ps2_calc_addr(arc_ps2, index, true);
175 
176 	dev_dbg(&pdev->dev, "port%d is allocated (data = 0x%p, status = 0x%p)\n",
177 		index, port->data_addr, port->status_addr);
178 
179 	serio_register_port(port->io);
180 	return 0;
181 }
182 
183 static int __devinit arc_ps2_probe(struct platform_device *pdev)
184 {
185 	struct arc_ps2_data *arc_ps2;
186 	struct resource *res;
187 	int irq;
188 	int error, id, i;
189 
190 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 	if (!res) {
192 		dev_err(&pdev->dev, "no IO memory defined\n");
193 		return -EINVAL;
194 	}
195 
196 	irq = platform_get_irq_byname(pdev, "arc_ps2_irq");
197 	if (irq < 0) {
198 		dev_err(&pdev->dev, "no IRQ defined\n");
199 		return -EINVAL;
200 	}
201 
202 	arc_ps2 = devm_kzalloc(&pdev->dev, sizeof(struct arc_ps2_data),
203 				GFP_KERNEL);
204 	if (!arc_ps2) {
205 		dev_err(&pdev->dev, "out of memory\n");
206 		return -ENOMEM;
207 	}
208 
209 	arc_ps2->addr = devm_request_and_ioremap(&pdev->dev, res);
210 	if (!arc_ps2->addr)
211 		return -EBUSY;
212 
213 	dev_info(&pdev->dev, "irq = %d, address = 0x%p, ports = %i\n",
214 		 irq, arc_ps2->addr, ARC_PS2_PORTS);
215 
216 	id = ioread32(arc_ps2->addr);
217 	if (id != ARC_ARC_PS2_ID) {
218 		dev_err(&pdev->dev, "device id does not match\n");
219 		return -ENXIO;
220 	}
221 
222 	arc_ps2_inhibit_ports(arc_ps2);
223 
224 	error = devm_request_irq(&pdev->dev, irq, arc_ps2_interrupt,
225 				 0, "arc_ps2", arc_ps2);
226 	if (error) {
227 		dev_err(&pdev->dev, "Could not allocate IRQ\n");
228 		return error;
229 	}
230 
231 	for (i = 0; i < ARC_PS2_PORTS; i++) {
232 		error = arc_ps2_create_port(pdev, arc_ps2, i);
233 		if (error) {
234 			while (--i >= 0)
235 				serio_unregister_port(arc_ps2->port[i].io);
236 			return error;
237 		}
238 	}
239 
240 	platform_set_drvdata(pdev, arc_ps2);
241 
242 	return 0;
243 }
244 
245 static int __devexit arc_ps2_remove(struct platform_device *pdev)
246 {
247 	struct arc_ps2_data *arc_ps2 = platform_get_drvdata(pdev);
248 	int i;
249 
250 	for (i = 0; i < ARC_PS2_PORTS; i++)
251 		serio_unregister_port(arc_ps2->port[i].io);
252 
253 	dev_dbg(&pdev->dev, "interrupt count = %i\n", arc_ps2->total_int);
254 	dev_dbg(&pdev->dev, "frame error count = %i\n", arc_ps2->frame_error);
255 	dev_dbg(&pdev->dev, "buffer overflow count = %i\n",
256 		arc_ps2->buf_overflow);
257 
258 	return 0;
259 }
260 
261 static struct platform_driver arc_ps2_driver = {
262 	.driver	= {
263 		.name	= "arc_ps2",
264 		.owner	= THIS_MODULE,
265 	},
266 	.probe	= arc_ps2_probe,
267 	.remove	= __devexit_p(arc_ps2_remove),
268 };
269 
270 module_platform_driver(arc_ps2_driver);
271 
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Pavel Sokolov <psokolov@synopsys.com>");
274 MODULE_DESCRIPTION("ARC PS/2 Driver");
275