1/*
2 * Copyright (c) 1999-2001 Vojtech Pavlik
3 */
4
5/*
6 * 82C710 C&T mouse port chip driver for Linux
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/delay.h>
26#include <linux/module.h>
27#include <linux/ioport.h>
28#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/serio.h>
31#include <linux/errno.h>
32#include <linux/err.h>
33#include <linux/platform_device.h>
34#include <linux/slab.h>
35
36#include <asm/io.h>
37
38MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
40MODULE_LICENSE("GPL");
41
42/*
43 * ct82c710 interface
44 */
45
46#define CT82C710_DEV_IDLE 0x01 /* Device Idle */
47#define CT82C710_RX_FULL 0x02 /* Device Char received */
48#define CT82C710_TX_IDLE 0x04 /* Device XMIT Idle */
49#define CT82C710_RESET 0x08 /* Device Reset */
50#define CT82C710_INTS_ON 0x10 /* Device Interrupt On */
51#define CT82C710_ERROR_FLAG 0x20 /* Device Error */
52#define CT82C710_CLEAR 0x40 /* Device Clear */
53#define CT82C710_ENABLE 0x80 /* Device Enable */
54
55#define CT82C710_IRQ 12
56
57#define CT82C710_DATA ct82c710_iores.start
58#define CT82C710_STATUS (ct82c710_iores.start + 1)
59
60static struct serio *ct82c710_port;
61static struct platform_device *ct82c710_device;
62static struct resource ct82c710_iores;
63
64/*
65 * Interrupt handler for the 82C710 mouse port. A character
66 * is waiting in the 82C710.
67 */
68
69static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
70{
71 return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
72}
73
74/*
75 * Wait for device to send output char and flush any input char.
76 */
77
78static int ct82c170_wait(void)
79{
80 int timeout = 60000;
81
82 while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
83 != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
84
85 if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
86
87 udelay(1);
88 timeout--;
89 }
90
91 return !timeout;
92}
93
94static void ct82c710_close(struct serio *serio)
95{
96 if (ct82c170_wait())
97 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
98
99 outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
100
101 if (ct82c170_wait())
102 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
103
104 free_irq(CT82C710_IRQ, NULL);
105}
106
107static int ct82c710_open(struct serio *serio)
108{
109 unsigned char status;
110 int err;
111
112 err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
113 if (err)
114 return err;
115
116 status = inb_p(CT82C710_STATUS);
117
118 status |= (CT82C710_ENABLE | CT82C710_RESET);
119 outb_p(status, CT82C710_STATUS);
120
121 status &= ~(CT82C710_RESET);
122 outb_p(status, CT82C710_STATUS);
123
124 status |= CT82C710_INTS_ON;
125 outb_p(status, CT82C710_STATUS); /* Enable interrupts */
126
127 while (ct82c170_wait()) {
128 printk(KERN_ERR "ct82c710: Device busy in open()\n");
129 status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
130 outb_p(status, CT82C710_STATUS);
131 free_irq(CT82C710_IRQ, NULL);
132 return -EBUSY;
133 }
134
135 return 0;
136}
137
138/*
139 * Write to the 82C710 mouse device.
140 */
141
142static int ct82c710_write(struct serio *port, unsigned char c)
143{
144 if (ct82c170_wait()) return -1;
145 outb_p(c, CT82C710_DATA);
146 return 0;
147}
148
149/*
150 * See if we can find a 82C710 device. Read mouse address.
151 */
152
153static int __init ct82c710_detect(void)
154{
155 outb_p(0x55, 0x2fa); /* Any value except 9, ff or 36 */
156 outb_p(0xaa, 0x3fa); /* Inverse of 55 */
157 outb_p(0x36, 0x3fa); /* Address the chip */
158 outb_p(0xe4, 0x3fa); /* 390/4; 390 = config address */
159 outb_p(0x1b, 0x2fa); /* Inverse of e4 */
160 outb_p(0x0f, 0x390); /* Write index */
161 if (inb_p(0x391) != 0xe4) /* Config address found? */
162 return -ENODEV; /* No: no 82C710 here */
163
164 outb_p(0x0d, 0x390); /* Write index */
165 ct82c710_iores.start = inb_p(0x391) << 2; /* Get mouse I/O address */
166 ct82c710_iores.end = ct82c710_iores.start + 1;
167 ct82c710_iores.flags = IORESOURCE_IO;
168 outb_p(0x0f, 0x390);
169 outb_p(0x0f, 0x391); /* Close config mode */
170
171 return 0;
172}
173
174static int ct82c710_probe(struct platform_device *dev)
175{
176 ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
177 if (!ct82c710_port)
178 return -ENOMEM;
179
180 ct82c710_port->id.type = SERIO_8042;
181 ct82c710_port->dev.parent = &dev->dev;
182 ct82c710_port->open = ct82c710_open;
183 ct82c710_port->close = ct82c710_close;
184 ct82c710_port->write = ct82c710_write;
185 strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
186 sizeof(ct82c710_port->name));
187 snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
188 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
189
190 serio_register_port(ct82c710_port);
191
192 printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
193 (unsigned long long)CT82C710_DATA, CT82C710_IRQ);
194
195 return 0;
196}
197
198static int ct82c710_remove(struct platform_device *dev)
199{
200 serio_unregister_port(ct82c710_port);
201
202 return 0;
203}
204
205static struct platform_driver ct82c710_driver = {
206 .driver = {
207 .name = "ct82c710",
208 },
209 .probe = ct82c710_probe,
210 .remove = ct82c710_remove,
211};
212
213
214static int __init ct82c710_init(void)
215{
216 int error;
217
218 error = ct82c710_detect();
219 if (error)
220 return error;
221
222 error = platform_driver_register(&ct82c710_driver);
223 if (error)
224 return error;
225
226 ct82c710_device = platform_device_alloc("ct82c710", -1);
227 if (!ct82c710_device) {
228 error = -ENOMEM;
229 goto err_unregister_driver;
230 }
231
232 error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
233 if (error)
234 goto err_free_device;
235
236 error = platform_device_add(ct82c710_device);
237 if (error)
238 goto err_free_device;
239
240 return 0;
241
242 err_free_device:
243 platform_device_put(ct82c710_device);
244 err_unregister_driver:
245 platform_driver_unregister(&ct82c710_driver);
246 return error;
247}
248
249static void __exit ct82c710_exit(void)
250{
251 platform_device_unregister(ct82c710_device);
252 platform_driver_unregister(&ct82c710_driver);
253}
254
255module_init(ct82c710_init);
256module_exit(ct82c710_exit);
257