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