2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
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.
8 * Driver is originally developed by Pavel Sokolov <psokolov@synopsys.com>
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>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
20 #define ARC_PS2_PORTS 2
22 #define ARC_ARC_PS2_ID 0x0001f609
24 #define STAT_TIMEOUT 128
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)
34 void __iomem
*data_addr
;
35 void __iomem
*status_addr
;
40 struct arc_ps2_port port
[ARC_PS2_PORTS
];
42 unsigned int frame_error
;
43 unsigned int buf_overflow
;
44 unsigned int total_int
;
47 static void arc_ps2_check_rx(struct arc_ps2_data
*arc_ps2
,
48 struct arc_ps2_port
*port
)
50 unsigned int timeout
= 1000;
51 unsigned int flag
, status
;
55 status
= ioread32(port
->status_addr
);
56 if (!(status
& PS2_STAT_RX_VAL
))
59 data
= ioread32(port
->data_addr
) & 0xff;
63 if (status
& PS2_STAT_RX_FRM_ERR
) {
64 arc_ps2
->frame_error
++;
66 } else if (status
& PS2_STAT_RX_BUF_OVER
) {
67 arc_ps2
->buf_overflow
++;
71 serio_interrupt(port
->io
, data
, flag
);
74 dev_err(&port
->io
->dev
, "PS/2 hardware stuck\n");
77 static irqreturn_t
arc_ps2_interrupt(int irq
, void *dev
)
79 struct arc_ps2_data
*arc_ps2
= dev
;
82 for (i
= 0; i
< ARC_PS2_PORTS
; i
++)
83 arc_ps2_check_rx(arc_ps2
, &arc_ps2
->port
[i
]);
88 static int arc_ps2_write(struct serio
*io
, unsigned char val
)
91 struct arc_ps2_port
*port
= io
->port_data
;
92 int timeout
= STAT_TIMEOUT
;
95 status
= ioread32(port
->status_addr
);
98 if (status
& PS2_STAT_TX_ISNOT_FUL
) {
99 iowrite32(val
& 0xff, port
->data_addr
);
105 dev_err(&io
->dev
, "write timeout\n");
109 static int arc_ps2_open(struct serio
*io
)
111 struct arc_ps2_port
*port
= io
->port_data
;
113 iowrite32(PS2_STAT_RX_INT_EN
, port
->status_addr
);
118 static void arc_ps2_close(struct serio
*io
)
120 struct arc_ps2_port
*port
= io
->port_data
;
122 iowrite32(ioread32(port
->status_addr
) & ~PS2_STAT_RX_INT_EN
,
126 static void __iomem
*arc_ps2_calc_addr(struct arc_ps2_data
*arc_ps2
,
127 int index
, bool status
)
131 addr
= arc_ps2
->addr
+ 4 + 4 * index
;
133 addr
+= ARC_PS2_PORTS
* 4;
138 static void arc_ps2_inhibit_ports(struct arc_ps2_data
*arc_ps2
)
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
);
152 static int arc_ps2_create_port(struct platform_device
*pdev
,
153 struct arc_ps2_data
*arc_ps2
,
156 struct arc_ps2_port
*port
= &arc_ps2
->port
[index
];
159 io
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
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
;
173 port
->data_addr
= arc_ps2_calc_addr(arc_ps2
, index
, false);
174 port
->status_addr
= arc_ps2_calc_addr(arc_ps2
, index
, true);
176 dev_dbg(&pdev
->dev
, "port%d is allocated (data = 0x%p, status = 0x%p)\n",
177 index
, port
->data_addr
, port
->status_addr
);
179 serio_register_port(port
->io
);
183 static int arc_ps2_probe(struct platform_device
*pdev
)
185 struct arc_ps2_data
*arc_ps2
;
186 struct resource
*res
;
190 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
192 dev_err(&pdev
->dev
, "no IO memory defined\n");
196 irq
= platform_get_irq_byname(pdev
, "arc_ps2_irq");
198 dev_err(&pdev
->dev
, "no IRQ defined\n");
202 arc_ps2
= devm_kzalloc(&pdev
->dev
, sizeof(struct arc_ps2_data
),
205 dev_err(&pdev
->dev
, "out of memory\n");
209 arc_ps2
->addr
= devm_request_and_ioremap(&pdev
->dev
, res
);
213 dev_info(&pdev
->dev
, "irq = %d, address = 0x%p, ports = %i\n",
214 irq
, arc_ps2
->addr
, ARC_PS2_PORTS
);
216 id
= ioread32(arc_ps2
->addr
);
217 if (id
!= ARC_ARC_PS2_ID
) {
218 dev_err(&pdev
->dev
, "device id does not match\n");
222 arc_ps2_inhibit_ports(arc_ps2
);
224 error
= devm_request_irq(&pdev
->dev
, irq
, arc_ps2_interrupt
,
225 0, "arc_ps2", arc_ps2
);
227 dev_err(&pdev
->dev
, "Could not allocate IRQ\n");
231 for (i
= 0; i
< ARC_PS2_PORTS
; i
++) {
232 error
= arc_ps2_create_port(pdev
, arc_ps2
, i
);
235 serio_unregister_port(arc_ps2
->port
[i
].io
);
240 platform_set_drvdata(pdev
, arc_ps2
);
245 static int arc_ps2_remove(struct platform_device
*pdev
)
247 struct arc_ps2_data
*arc_ps2
= platform_get_drvdata(pdev
);
250 for (i
= 0; i
< ARC_PS2_PORTS
; i
++)
251 serio_unregister_port(arc_ps2
->port
[i
].io
);
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
);
261 static struct platform_driver arc_ps2_driver
= {
264 .owner
= THIS_MODULE
,
266 .probe
= arc_ps2_probe
,
267 .remove
= arc_ps2_remove
,
270 module_platform_driver(arc_ps2_driver
);
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Pavel Sokolov <psokolov@synopsys.com>");
274 MODULE_DESCRIPTION("ARC PS/2 Driver");