blob: aad664d5259fd279a17b2d176aaa91e2db267d2c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * HP i8042-based System Device Controller driver.
3 *
4 * Copyright (c) 2001 Brian S. Julin
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL").
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 *
29 * References:
30 * System Device Controller Microprocessor Firmware Theory of Operation
31 * for Part Number 1820-4784 Revision B. Dwg No. A-1820-4784-2
32 * Helge Deller's original hilkbd.c port for PA-RISC.
33 *
34 *
35 * Driver theory of operation:
36 *
Helge Dellerffd51f42007-02-28 23:51:29 -050037 * hp_sdc_put does all writing to the SDC. ISR can run on a different
38 * CPU than hp_sdc_put, but only one CPU runs hp_sdc_put at a time
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * (it cannot really benefit from SMP anyway.) A tasket fit this perfectly.
40 *
Helge Dellerffd51f42007-02-28 23:51:29 -050041 * All data coming back from the SDC is sent via interrupt and can be read
42 * fully in the ISR, so there are no latency/throughput problems there.
43 * The problem is with output, due to the slow clock speed of the SDC
44 * compared to the CPU. This should not be too horrible most of the time,
45 * but if used with HIL devices that support the multibyte transfer command,
46 * keeping outbound throughput flowing at the 6500KBps that the HIL is
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * capable of is more than can be done at HZ=100.
48 *
Helge Dellerffd51f42007-02-28 23:51:29 -050049 * Busy polling for IBF clear wastes CPU cycles and bus cycles. hp_sdc.ibf
50 * is set to 0 when the IBF flag in the status register has cleared. ISR
51 * may do this, and may also access the parts of queued transactions related
52 * to reading data back from the SDC, but otherwise will not touch the
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * hp_sdc state. Whenever a register is written hp_sdc.ibf is set to 1.
54 *
55 * The i8042 write index and the values in the 4-byte input buffer
56 * starting at 0x70 are kept track of in hp_sdc.wi, and .r7[], respectively,
Helge Dellerffd51f42007-02-28 23:51:29 -050057 * to minimize the amount of IO needed to the SDC. However these values
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * do not need to be locked since they are only ever accessed by hp_sdc_put.
59 *
60 * A timer task schedules the tasklet once per second just to make
61 * sure it doesn't freeze up and to allow for bad reads to time out.
62 */
63
64#include <linux/hp_sdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/errno.h>
66#include <linux/init.h>
67#include <linux/module.h>
68#include <linux/ioport.h>
69#include <linux/time.h>
Matthew Wilcox0f17e4c2008-07-24 08:30:48 -040070#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/hil.h>
Geert Uytterhoeven4933d072008-05-05 21:16:13 +020073#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/io.h>
75#include <asm/system.h>
76
77/* Machine-specific abstraction */
78
79#if defined(__hppa__)
80# include <asm/parisc-device.h>
81# define sdc_readb(p) gsc_readb(p)
82# define sdc_writeb(v,p) gsc_writeb((v),(p))
83#elif defined(__mc68000__)
84# include <asm/uaccess.h>
85# define sdc_readb(p) in_8(p)
86# define sdc_writeb(v,p) out_8((p),(v))
87#else
88# error "HIL is not supported on this platform"
89#endif
90
91#define PREFIX "HP SDC: "
92
93MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
94MODULE_DESCRIPTION("HP i8042-based SDC Driver");
95MODULE_LICENSE("Dual BSD/GPL");
96
97EXPORT_SYMBOL(hp_sdc_request_timer_irq);
98EXPORT_SYMBOL(hp_sdc_request_hil_irq);
99EXPORT_SYMBOL(hp_sdc_request_cooked_irq);
100
101EXPORT_SYMBOL(hp_sdc_release_timer_irq);
102EXPORT_SYMBOL(hp_sdc_release_hil_irq);
103EXPORT_SYMBOL(hp_sdc_release_cooked_irq);
104
Helge Deller95754992007-03-16 00:59:29 -0400105EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
107EXPORT_SYMBOL(hp_sdc_dequeue_transaction);
108
Helge Deller305e3422008-07-14 10:28:32 -0400109static unsigned int hp_sdc_disabled;
110module_param_named(no_hpsdc, hp_sdc_disabled, bool, 0);
111MODULE_PARM_DESC(no_hpsdc, "Do not enable HP SDC driver.");
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static hp_i8042_sdc hp_sdc; /* All driver state is kept in here. */
114
115/*************** primitives for use in any context *********************/
Helge Dellerffd51f42007-02-28 23:51:29 -0500116static inline uint8_t hp_sdc_status_in8(void)
117{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 uint8_t status;
119 unsigned long flags;
120
121 write_lock_irqsave(&hp_sdc.ibf_lock, flags);
122 status = sdc_readb(hp_sdc.status_io);
Helge Dellerffd51f42007-02-28 23:51:29 -0500123 if (!(status & HP_SDC_STATUS_IBF))
124 hp_sdc.ibf = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
126
127 return status;
128}
129
Helge Dellerffd51f42007-02-28 23:51:29 -0500130static inline uint8_t hp_sdc_data_in8(void)
131{
132 return sdc_readb(hp_sdc.data_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Helge Dellerffd51f42007-02-28 23:51:29 -0500135static inline void hp_sdc_status_out8(uint8_t val)
136{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 unsigned long flags;
138
139 write_lock_irqsave(&hp_sdc.ibf_lock, flags);
140 hp_sdc.ibf = 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500141 if ((val & 0xf0) == 0xe0)
142 hp_sdc.wi = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 sdc_writeb(val, hp_sdc.status_io);
144 write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
145}
146
Helge Dellerffd51f42007-02-28 23:51:29 -0500147static inline void hp_sdc_data_out8(uint8_t val)
148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 unsigned long flags;
150
151 write_lock_irqsave(&hp_sdc.ibf_lock, flags);
152 hp_sdc.ibf = 1;
153 sdc_writeb(val, hp_sdc.data_io);
154 write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
155}
156
Helge Dellerffd51f42007-02-28 23:51:29 -0500157/* Care must be taken to only invoke hp_sdc_spin_ibf when
158 * absolutely needed, or in rarely invoked subroutines.
159 * Not only does it waste CPU cycles, it also wastes bus cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Helge Dellerffd51f42007-02-28 23:51:29 -0500161static inline void hp_sdc_spin_ibf(void)
162{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned long flags;
164 rwlock_t *lock;
165
166 lock = &hp_sdc.ibf_lock;
167
168 read_lock_irqsave(lock, flags);
169 if (!hp_sdc.ibf) {
170 read_unlock_irqrestore(lock, flags);
171 return;
172 }
173 read_unlock(lock);
174 write_lock(lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500175 while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
176 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 hp_sdc.ibf = 0;
178 write_unlock_irqrestore(lock, flags);
179}
180
181
182/************************ Interrupt context functions ************************/
Helge Dellerffd51f42007-02-28 23:51:29 -0500183static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
184{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 hp_sdc_transaction *curr;
186
187 read_lock(&hp_sdc.rtq_lock);
188 if (hp_sdc.rcurr < 0) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500189 read_unlock(&hp_sdc.rtq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return;
191 }
192 curr = hp_sdc.tq[hp_sdc.rcurr];
193 read_unlock(&hp_sdc.rtq_lock);
194
195 curr->seq[curr->idx++] = status;
196 curr->seq[curr->idx++] = data;
197 hp_sdc.rqty -= 2;
198 do_gettimeofday(&hp_sdc.rtv);
199
200 if (hp_sdc.rqty <= 0) {
201 /* All data has been gathered. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500202 if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
203 if (curr->act.semaphore)
204 up(curr->act.semaphore);
205
206 if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (curr->act.irqhook)
208 curr->act.irqhook(irq, dev_id, status, data);
Helge Dellerffd51f42007-02-28 23:51:29 -0500209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 curr->actidx = curr->idx;
211 curr->idx++;
212 /* Return control of this transaction */
213 write_lock(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500214 hp_sdc.rcurr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 hp_sdc.rqty = 0;
216 write_unlock(&hp_sdc.rtq_lock);
217 tasklet_schedule(&hp_sdc.task);
218 }
219}
220
Helge Dellerffd51f42007-02-28 23:51:29 -0500221static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
222{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 uint8_t status, data;
224
225 status = hp_sdc_status_in8();
226 /* Read data unconditionally to advance i8042. */
227 data = hp_sdc_data_in8();
228
229 /* For now we are ignoring these until we get the SDC to behave. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500230 if (((status & 0xf1) == 0x51) && data == 0x82)
231 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Helge Dellerffd51f42007-02-28 23:51:29 -0500233 switch (status & HP_SDC_STATUS_IRQMASK) {
234 case 0: /* This case is not documented. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500236
237 case HP_SDC_STATUS_USERTIMER:
238 case HP_SDC_STATUS_PERIODIC:
239 case HP_SDC_STATUS_TIMER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 read_lock(&hp_sdc.hook_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500241 if (hp_sdc.timer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 hp_sdc.timer(irq, dev_id, status, data);
243 read_unlock(&hp_sdc.hook_lock);
244 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500245
246 case HP_SDC_STATUS_REG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 hp_sdc_take(irq, dev_id, status, data);
248 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500249
250 case HP_SDC_STATUS_HILCMD:
251 case HP_SDC_STATUS_HILDATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 read_lock(&hp_sdc.hook_lock);
253 if (hp_sdc.hil != NULL)
254 hp_sdc.hil(irq, dev_id, status, data);
255 read_unlock(&hp_sdc.hook_lock);
256 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500257
258 case HP_SDC_STATUS_PUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 read_lock(&hp_sdc.hook_lock);
260 if (hp_sdc.pup != NULL)
261 hp_sdc.pup(irq, dev_id, status, data);
Helge Dellerffd51f42007-02-28 23:51:29 -0500262 else
263 printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 read_unlock(&hp_sdc.hook_lock);
265 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500266
267 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 read_lock(&hp_sdc.hook_lock);
269 if (hp_sdc.cooked != NULL)
270 hp_sdc.cooked(irq, dev_id, status, data);
271 read_unlock(&hp_sdc.hook_lock);
272 break;
273 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return IRQ_HANDLED;
276}
277
278
Helge Dellerffd51f42007-02-28 23:51:29 -0500279static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int status;
Helge Dellerffd51f42007-02-28 23:51:29 -0500282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 status = hp_sdc_status_in8();
284 printk(KERN_WARNING PREFIX "NMI !\n");
285
Helge Dellerffd51f42007-02-28 23:51:29 -0500286#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (status & HP_SDC_NMISTATUS_FHS) {
288 read_lock(&hp_sdc.hook_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500289 if (hp_sdc.timer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 hp_sdc.timer(irq, dev_id, status, 0);
291 read_unlock(&hp_sdc.hook_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500292 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* TODO: pass this on to the HIL handler, or do SAK here? */
294 printk(KERN_WARNING PREFIX "HIL NMI\n");
295 }
296#endif
Helge Dellerffd51f42007-02-28 23:51:29 -0500297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return IRQ_HANDLED;
299}
300
301
302/***************** Kernel (tasklet) context functions ****************/
303
304unsigned long hp_sdc_put(void);
305
Helge Dellerffd51f42007-02-28 23:51:29 -0500306static void hp_sdc_tasklet(unsigned long foo)
307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 write_lock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (hp_sdc.rcurr >= 0) {
311 struct timeval tv;
Helge Dellerffd51f42007-02-28 23:51:29 -0500312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 do_gettimeofday(&tv);
Helge Dellerffd51f42007-02-28 23:51:29 -0500314 if (tv.tv_sec > hp_sdc.rtv.tv_sec)
315 tv.tv_usec += USEC_PER_SEC;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
318 hp_sdc_transaction *curr;
319 uint8_t tmp;
320
321 curr = hp_sdc.tq[hp_sdc.rcurr];
322 /* If this turns out to be a normal failure mode
323 * we'll need to figure out a way to communicate
324 * it back to the application. and be less verbose.
325 */
326 printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
327 tv.tv_usec - hp_sdc.rtv.tv_usec);
328 curr->idx += hp_sdc.rqty;
329 hp_sdc.rqty = 0;
330 tmp = curr->seq[curr->actidx];
331 curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
Helge Dellerffd51f42007-02-28 23:51:29 -0500332 if (tmp & HP_SDC_ACT_SEMAPHORE)
333 if (curr->act.semaphore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 up(curr->act.semaphore);
Helge Dellerffd51f42007-02-28 23:51:29 -0500335
336 if (tmp & HP_SDC_ACT_CALLBACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Note this means that irqhooks may be called
338 * in tasklet/bh context.
339 */
Helge Dellerffd51f42007-02-28 23:51:29 -0500340 if (curr->act.irqhook)
Al Viro6ce6b3a2006-10-14 16:52:36 +0100341 curr->act.irqhook(0, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 curr->actidx = curr->idx;
345 curr->idx++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500346 hp_sdc.rcurr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
349 write_unlock_irq(&hp_sdc.rtq_lock);
350 hp_sdc_put();
351}
352
Helge Dellerffd51f42007-02-28 23:51:29 -0500353unsigned long hp_sdc_put(void)
354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 hp_sdc_transaction *curr;
356 uint8_t act;
357 int idx, curridx;
358
359 int limit = 0;
360
361 write_lock(&hp_sdc.lock);
362
363 /* If i8042 buffers are full, we cannot do anything that
364 requires output, so we skip to the administrativa. */
365 if (hp_sdc.ibf) {
366 hp_sdc_status_in8();
Helge Dellerffd51f42007-02-28 23:51:29 -0500367 if (hp_sdc.ibf)
368 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 anew:
372 /* See if we are in the middle of a sequence. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500373 if (hp_sdc.wcurr < 0)
374 hp_sdc.wcurr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 read_lock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500376 if (hp_sdc.rcurr == hp_sdc.wcurr)
377 hp_sdc.wcurr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 read_unlock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500379 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
380 hp_sdc.wcurr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 curridx = hp_sdc.wcurr;
382
Helge Dellerffd51f42007-02-28 23:51:29 -0500383 if (hp_sdc.tq[curridx] != NULL)
384 goto start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 while (++curridx != hp_sdc.wcurr) {
387 if (curridx >= HP_SDC_QUEUE_LEN) {
388 curridx = -1; /* Wrap to top */
389 continue;
390 }
391 read_lock_irq(&hp_sdc.rtq_lock);
392 if (hp_sdc.rcurr == curridx) {
393 read_unlock_irq(&hp_sdc.rtq_lock);
394 continue;
395 }
396 read_unlock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500397 if (hp_sdc.tq[curridx] != NULL)
398 break; /* Found one. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
401 curridx = -1;
402 }
403 hp_sdc.wcurr = curridx;
404
405 start:
406
407 /* Check to see if the interrupt mask needs to be set. */
408 if (hp_sdc.set_im) {
409 hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
410 hp_sdc.set_im = 0;
411 goto finish;
412 }
413
Helge Dellerffd51f42007-02-28 23:51:29 -0500414 if (hp_sdc.wcurr == -1)
415 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 curr = hp_sdc.tq[curridx];
418 idx = curr->actidx;
419
420 if (curr->actidx >= curr->endidx) {
421 hp_sdc.tq[curridx] = NULL;
422 /* Interleave outbound data between the transactions. */
423 hp_sdc.wcurr++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500424 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
425 hp_sdc.wcurr = 0;
426 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 act = curr->seq[idx];
430 idx++;
431
432 if (curr->idx >= curr->endidx) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500433 if (act & HP_SDC_ACT_DEALLOC)
434 kfree(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 hp_sdc.tq[curridx] = NULL;
436 /* Interleave outbound data between the transactions. */
437 hp_sdc.wcurr++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500438 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
439 hp_sdc.wcurr = 0;
440 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 while (act & HP_SDC_ACT_PRECMD) {
444 if (curr->idx != idx) {
445 idx++;
446 act &= ~HP_SDC_ACT_PRECMD;
447 break;
448 }
449 hp_sdc_status_out8(curr->seq[idx]);
450 curr->idx++;
451 /* act finished? */
452 if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
Helge Dellerffd51f42007-02-28 23:51:29 -0500453 goto actdone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* skip quantity field if data-out sequence follows. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500455 if (act & HP_SDC_ACT_DATAOUT)
456 curr->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto finish;
458 }
459 if (act & HP_SDC_ACT_DATAOUT) {
460 int qty;
461
462 qty = curr->seq[idx];
463 idx++;
464 if (curr->idx - idx < qty) {
465 hp_sdc_data_out8(curr->seq[curr->idx]);
466 curr->idx++;
467 /* act finished? */
Helge Dellerffd51f42007-02-28 23:51:29 -0500468 if (curr->idx - idx >= qty &&
469 (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto actdone;
471 goto finish;
472 }
473 idx += qty;
474 act &= ~HP_SDC_ACT_DATAOUT;
Helge Dellerffd51f42007-02-28 23:51:29 -0500475 } else
476 while (act & HP_SDC_ACT_DATAREG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int mask;
478 uint8_t w7[4];
479
480 mask = curr->seq[idx];
481 if (idx != curr->idx) {
482 idx++;
483 idx += !!(mask & 1);
484 idx += !!(mask & 2);
485 idx += !!(mask & 4);
486 idx += !!(mask & 8);
487 act &= ~HP_SDC_ACT_DATAREG;
488 break;
489 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
492 w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
493 w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
494 w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
Helge Dellerffd51f42007-02-28 23:51:29 -0500495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
Helge Dellerffd51f42007-02-28 23:51:29 -0500497 w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int i = 0;
499
Helge Dellerffd51f42007-02-28 23:51:29 -0500500 /* Need to point the write index register */
501 while (i < 4 && w7[i] == hp_sdc.r7[i])
502 i++;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (i < 4) {
505 hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
506 hp_sdc.wi = 0x70 + i;
507 goto finish;
508 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 idx++;
511 if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
512 goto actdone;
Helge Dellerffd51f42007-02-28 23:51:29 -0500513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 curr->idx = idx;
515 act &= ~HP_SDC_ACT_DATAREG;
516 break;
517 }
518
519 hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
520 hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
521 hp_sdc.wi++; /* write index register autoincrements */
522 {
523 int i = 0;
524
Helge Dellerffd51f42007-02-28 23:51:29 -0500525 while ((i < 4) && w7[i] == hp_sdc.r7[i])
526 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (i >= 4) {
528 curr->idx = idx + 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500529 if ((act & HP_SDC_ACT_DURING) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 HP_SDC_ACT_DATAREG)
Helge Dellerffd51f42007-02-28 23:51:29 -0500531 goto actdone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 }
534 goto finish;
535 }
536 /* We don't go any further in the command if there is a pending read,
537 because we don't want interleaved results. */
538 read_lock_irq(&hp_sdc.rtq_lock);
539 if (hp_sdc.rcurr >= 0) {
540 read_unlock_irq(&hp_sdc.rtq_lock);
541 goto finish;
542 }
543 read_unlock_irq(&hp_sdc.rtq_lock);
544
545
546 if (act & HP_SDC_ACT_POSTCMD) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500547 uint8_t postcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* curr->idx should == idx at this point. */
550 postcmd = curr->seq[idx];
551 curr->idx++;
552 if (act & HP_SDC_ACT_DATAIN) {
553
554 /* Start a new read */
Helge Dellerffd51f42007-02-28 23:51:29 -0500555 hp_sdc.rqty = curr->seq[curr->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 do_gettimeofday(&hp_sdc.rtv);
557 curr->idx++;
558 /* Still need to lock here in case of spurious irq. */
559 write_lock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500560 hp_sdc.rcurr = curridx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 write_unlock_irq(&hp_sdc.rtq_lock);
562 hp_sdc_status_out8(postcmd);
563 goto finish;
564 }
565 hp_sdc_status_out8(postcmd);
566 goto actdone;
567 }
568
Helge Dellerffd51f42007-02-28 23:51:29 -0500569 actdone:
570 if (act & HP_SDC_ACT_SEMAPHORE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 up(curr->act.semaphore);
Helge Dellerffd51f42007-02-28 23:51:29 -0500572 else if (act & HP_SDC_ACT_CALLBACK)
Al Viro6ce6b3a2006-10-14 16:52:36 +0100573 curr->act.irqhook(0,NULL,0,0);
Helge Dellerffd51f42007-02-28 23:51:29 -0500574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (curr->idx >= curr->endidx) { /* This transaction is over. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500576 if (act & HP_SDC_ACT_DEALLOC)
577 kfree(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 hp_sdc.tq[curridx] = NULL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500579 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 curr->actidx = idx + 1;
581 curr->idx = idx + 2;
582 }
583 /* Interleave outbound data between the transactions. */
584 hp_sdc.wcurr++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500585 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
586 hp_sdc.wcurr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 finish:
Helge Dellerffd51f42007-02-28 23:51:29 -0500589 /* If by some quirk IBF has cleared and our ISR has run to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 see that that has happened, do it all again. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500591 if (!hp_sdc.ibf && limit++ < 20)
592 goto anew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 done:
Helge Dellerffd51f42007-02-28 23:51:29 -0500595 if (hp_sdc.wcurr >= 0)
596 tasklet_schedule(&hp_sdc.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 write_unlock(&hp_sdc.lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600}
601
602/******* Functions called in either user or kernel context ****/
Helge Deller95754992007-03-16 00:59:29 -0400603int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
Helge Dellerffd51f42007-02-28 23:51:29 -0500604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int i;
606
607 if (this == NULL) {
Helge Deller95754992007-03-16 00:59:29 -0400608 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Can't have same transaction on queue twice */
Helge Dellerffd51f42007-02-28 23:51:29 -0500613 for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
614 if (hp_sdc.tq[i] == this)
615 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 this->actidx = 0;
618 this->idx = 1;
619
620 /* Search for empty slot */
Helge Dellerffd51f42007-02-28 23:51:29 -0500621 for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (hp_sdc.tq[i] == NULL) {
623 hp_sdc.tq[i] = this;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 tasklet_schedule(&hp_sdc.task);
625 return 0;
626 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
629 return -EBUSY;
630
631 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
633 return -EINVAL;
634}
635
Helge Deller95754992007-03-16 00:59:29 -0400636int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
637 unsigned long flags;
638 int ret;
639
640 write_lock_irqsave(&hp_sdc.lock, flags);
641 ret = __hp_sdc_enqueue_transaction(this);
642 write_unlock_irqrestore(&hp_sdc.lock,flags);
643
644 return ret;
645}
646
Helge Dellerffd51f42007-02-28 23:51:29 -0500647int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 unsigned long flags;
650 int i;
651
652 write_lock_irqsave(&hp_sdc.lock, flags);
653
654 /* TODO: don't remove it if it's not done. */
655
Helge Dellerffd51f42007-02-28 23:51:29 -0500656 for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
657 if (hp_sdc.tq[i] == this)
658 hp_sdc.tq[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 write_unlock_irqrestore(&hp_sdc.lock, flags);
661 return 0;
662}
663
664
665
666/********************** User context functions **************************/
Helge Dellerffd51f42007-02-28 23:51:29 -0500667int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
668{
669 if (callback == NULL || hp_sdc.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 write_lock_irq(&hp_sdc.hook_lock);
673 if (hp_sdc.timer != NULL) {
674 write_unlock_irq(&hp_sdc.hook_lock);
675 return -EBUSY;
676 }
677
678 hp_sdc.timer = callback;
679 /* Enable interrupts from the timers */
680 hp_sdc.im &= ~HP_SDC_IM_FH;
681 hp_sdc.im &= ~HP_SDC_IM_PT;
682 hp_sdc.im &= ~HP_SDC_IM_TIMERS;
683 hp_sdc.set_im = 1;
684 write_unlock_irq(&hp_sdc.hook_lock);
685
686 tasklet_schedule(&hp_sdc.task);
687
688 return 0;
689}
690
Helge Dellerffd51f42007-02-28 23:51:29 -0500691int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
692{
693 if (callback == NULL || hp_sdc.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 write_lock_irq(&hp_sdc.hook_lock);
697 if (hp_sdc.hil != NULL) {
698 write_unlock_irq(&hp_sdc.hook_lock);
699 return -EBUSY;
700 }
701
702 hp_sdc.hil = callback;
703 hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
704 hp_sdc.set_im = 1;
705 write_unlock_irq(&hp_sdc.hook_lock);
706
707 tasklet_schedule(&hp_sdc.task);
708
709 return 0;
710}
711
Helge Dellerffd51f42007-02-28 23:51:29 -0500712int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
713{
714 if (callback == NULL || hp_sdc.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 write_lock_irq(&hp_sdc.hook_lock);
718 if (hp_sdc.cooked != NULL) {
719 write_unlock_irq(&hp_sdc.hook_lock);
720 return -EBUSY;
721 }
722
723 /* Enable interrupts from the HIL MLC */
724 hp_sdc.cooked = callback;
725 hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
726 hp_sdc.set_im = 1;
727 write_unlock_irq(&hp_sdc.hook_lock);
728
729 tasklet_schedule(&hp_sdc.task);
730
731 return 0;
732}
733
Helge Dellerffd51f42007-02-28 23:51:29 -0500734int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
735{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 write_lock_irq(&hp_sdc.hook_lock);
737 if ((callback != hp_sdc.timer) ||
738 (hp_sdc.timer == NULL)) {
739 write_unlock_irq(&hp_sdc.hook_lock);
740 return -EINVAL;
741 }
742
743 /* Disable interrupts from the timers */
744 hp_sdc.timer = NULL;
745 hp_sdc.im |= HP_SDC_IM_TIMERS;
746 hp_sdc.im |= HP_SDC_IM_FH;
747 hp_sdc.im |= HP_SDC_IM_PT;
748 hp_sdc.set_im = 1;
749 write_unlock_irq(&hp_sdc.hook_lock);
750 tasklet_schedule(&hp_sdc.task);
751
752 return 0;
753}
754
Helge Dellerffd51f42007-02-28 23:51:29 -0500755int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 write_lock_irq(&hp_sdc.hook_lock);
758 if ((callback != hp_sdc.hil) ||
759 (hp_sdc.hil == NULL)) {
760 write_unlock_irq(&hp_sdc.hook_lock);
761 return -EINVAL;
762 }
763
764 hp_sdc.hil = NULL;
765 /* Disable interrupts from HIL only if there is no cooked driver. */
766 if(hp_sdc.cooked == NULL) {
767 hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
768 hp_sdc.set_im = 1;
769 }
770 write_unlock_irq(&hp_sdc.hook_lock);
771 tasklet_schedule(&hp_sdc.task);
772
773 return 0;
774}
775
Helge Dellerffd51f42007-02-28 23:51:29 -0500776int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 write_lock_irq(&hp_sdc.hook_lock);
779 if ((callback != hp_sdc.cooked) ||
780 (hp_sdc.cooked == NULL)) {
781 write_unlock_irq(&hp_sdc.hook_lock);
782 return -EINVAL;
783 }
784
785 hp_sdc.cooked = NULL;
786 /* Disable interrupts from HIL only if there is no raw HIL driver. */
787 if(hp_sdc.hil == NULL) {
788 hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
789 hp_sdc.set_im = 1;
790 }
791 write_unlock_irq(&hp_sdc.hook_lock);
792 tasklet_schedule(&hp_sdc.task);
793
794 return 0;
795}
796
797/************************* Keepalive timer task *********************/
798
Helge Dellerffd51f42007-02-28 23:51:29 -0500799void hp_sdc_kicker (unsigned long data)
800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 tasklet_schedule(&hp_sdc.task);
802 /* Re-insert the periodic task. */
803 mod_timer(&hp_sdc.kicker, jiffies + HZ);
804}
805
806/************************** Module Initialization ***************************/
807
808#if defined(__hppa__)
809
Helge Deller3acaf542007-02-28 23:51:19 -0500810static const struct parisc_device_id hp_sdc_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 {
Helge Dellerffd51f42007-02-28 23:51:29 -0500812 .hw_type = HPHW_FIO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .hversion_rev = HVERSION_REV_ANY_ID,
814 .hversion = HVERSION_ANY_ID,
Helge Dellerffd51f42007-02-28 23:51:29 -0500815 .sversion = 0x73,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 },
817 { 0, }
818};
819
820MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
821
822static int __init hp_sdc_init_hppa(struct parisc_device *d);
823
824static struct parisc_driver hp_sdc_driver = {
Matthew Wilcoxbdad1f82005-10-21 22:36:23 -0400825 .name = "hp_sdc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .id_table = hp_sdc_tbl,
827 .probe = hp_sdc_init_hppa,
828};
829
830#endif /* __hppa__ */
831
832static int __init hp_sdc_init(void)
833{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 char *errstr;
835 hp_sdc_transaction t_sync;
836 uint8_t ts_sync[6];
837 struct semaphore s_sync;
838
Helge Dellerffd51f42007-02-28 23:51:29 -0500839 rwlock_init(&hp_sdc.lock);
840 rwlock_init(&hp_sdc.ibf_lock);
841 rwlock_init(&hp_sdc.rtq_lock);
842 rwlock_init(&hp_sdc.hook_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 hp_sdc.timer = NULL;
845 hp_sdc.hil = NULL;
846 hp_sdc.pup = NULL;
847 hp_sdc.cooked = NULL;
848 hp_sdc.im = HP_SDC_IM_MASK; /* Mask maskable irqs */
849 hp_sdc.set_im = 1;
850 hp_sdc.wi = 0xff;
851 hp_sdc.r7[0] = 0xff;
852 hp_sdc.r7[1] = 0xff;
853 hp_sdc.r7[2] = 0xff;
854 hp_sdc.r7[3] = 0xff;
855 hp_sdc.ibf = 1;
856
Helge Dellerffd51f42007-02-28 23:51:29 -0500857 memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 hp_sdc.wcurr = -1;
860 hp_sdc.rcurr = -1;
861 hp_sdc.rqty = 0;
862
863 hp_sdc.dev_err = -ENODEV;
864
865 errstr = "IO not found for";
Helge Dellerffd51f42007-02-28 23:51:29 -0500866 if (!hp_sdc.base_io)
867 goto err0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 errstr = "IRQ not found for";
Helge Dellerffd51f42007-02-28 23:51:29 -0500870 if (!hp_sdc.irq)
871 goto err0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 hp_sdc.dev_err = -EBUSY;
874
875#if defined(__hppa__)
876 errstr = "IO not available for";
Helge Dellerffd51f42007-02-28 23:51:29 -0500877 if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
878 goto err0;
879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 errstr = "IRQ not available for";
Helge Deller3acaf542007-02-28 23:51:19 -0500882 if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
Helge Dellerffd51f42007-02-28 23:51:29 -0500883 "HP SDC", &hp_sdc))
884 goto err1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 errstr = "NMI not available for";
Helge Deller3acaf542007-02-28 23:51:19 -0500887 if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
Helge Dellerffd51f42007-02-28 23:51:29 -0500888 "HP SDC NMI", &hp_sdc))
889 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Helge Dellerffd51f42007-02-28 23:51:29 -0500891 printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
893
894 hp_sdc_status_in8();
895 hp_sdc_data_in8();
896
897 tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
898
899 /* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
900 t_sync.actidx = 0;
901 t_sync.idx = 1;
902 t_sync.endidx = 6;
903 t_sync.seq = ts_sync;
904 ts_sync[0] = HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
905 ts_sync[1] = 0x0f;
906 ts_sync[2] = ts_sync[3] = ts_sync[4] = ts_sync[5] = 0;
907 t_sync.act.semaphore = &s_sync;
908 init_MUTEX_LOCKED(&s_sync);
909 hp_sdc_enqueue_transaction(&t_sync);
910 down(&s_sync); /* Wait for t_sync to complete */
911
912 /* Create the keepalive task */
913 init_timer(&hp_sdc.kicker);
914 hp_sdc.kicker.expires = jiffies + HZ;
915 hp_sdc.kicker.function = &hp_sdc_kicker;
916 add_timer(&hp_sdc.kicker);
917
918 hp_sdc.dev_err = 0;
919 return 0;
920 err2:
Helge Deller3acaf542007-02-28 23:51:19 -0500921 free_irq(hp_sdc.irq, &hp_sdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 err1:
923 release_region(hp_sdc.data_io, 2);
924 err0:
Helge Dellerffd51f42007-02-28 23:51:29 -0500925 printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
927 hp_sdc.dev = NULL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return hp_sdc.dev_err;
930}
931
932#if defined(__hppa__)
933
934static int __init hp_sdc_init_hppa(struct parisc_device *d)
935{
Helge Dellerffd51f42007-02-28 23:51:29 -0500936 if (!d)
937 return 1;
938 if (hp_sdc.dev != NULL)
939 return 1; /* We only expect one SDC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 hp_sdc.dev = d;
942 hp_sdc.irq = d->irq;
943 hp_sdc.nmi = d->aux_irq;
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400944 hp_sdc.base_io = d->hpa.start;
945 hp_sdc.data_io = d->hpa.start + 0x800;
946 hp_sdc.status_io = d->hpa.start + 0x801;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 return hp_sdc_init();
949}
950
951#endif /* __hppa__ */
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static void hp_sdc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 write_lock_irq(&hp_sdc.lock);
956
957 /* Turn off all maskable "sub-function" irq's. */
958 hp_sdc_spin_ibf();
959 sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
960
961 /* Wait until we know this has been processed by the i8042 */
962 hp_sdc_spin_ibf();
963
Helge Deller3acaf542007-02-28 23:51:19 -0500964 free_irq(hp_sdc.nmi, &hp_sdc);
965 free_irq(hp_sdc.irq, &hp_sdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 write_unlock_irq(&hp_sdc.lock);
967
968 del_timer(&hp_sdc.kicker);
969
970 tasklet_kill(&hp_sdc.task);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972#if defined(__hppa__)
Helge Dellerffd51f42007-02-28 23:51:29 -0500973 if (unregister_parisc_driver(&hp_sdc_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
975#endif
976}
977
978static int __init hp_sdc_register(void)
979{
980 hp_sdc_transaction tq_init;
981 uint8_t tq_init_seq[5];
982 struct semaphore tq_init_sem;
983#if defined(__mc68000__)
984 mm_segment_t fs;
985 unsigned char i;
986#endif
Helge Dellerffd51f42007-02-28 23:51:29 -0500987
Helge Deller305e3422008-07-14 10:28:32 -0400988 if (hp_sdc_disabled) {
989 printk(KERN_WARNING PREFIX "HP SDC driver disabled by no_hpsdc=1.\n");
990 return -ENODEV;
991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 hp_sdc.dev = NULL;
994 hp_sdc.dev_err = 0;
995#if defined(__hppa__)
996 if (register_parisc_driver(&hp_sdc_driver)) {
997 printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
998 return -ENODEV;
999 }
1000#elif defined(__mc68000__)
1001 if (!MACH_IS_HP300)
1002 return -ENODEV;
1003
1004 hp_sdc.irq = 1;
1005 hp_sdc.nmi = 7;
1006 hp_sdc.base_io = (unsigned long) 0xf0428000;
1007 hp_sdc.data_io = (unsigned long) hp_sdc.base_io + 1;
1008 hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
1009 fs = get_fs();
1010 set_fs(KERNEL_DS);
1011 if (!get_user(i, (unsigned char *)hp_sdc.data_io))
1012 hp_sdc.dev = (void *)1;
1013 set_fs(fs);
1014 hp_sdc.dev_err = hp_sdc_init();
1015#endif
1016 if (hp_sdc.dev == NULL) {
1017 printk(KERN_WARNING PREFIX "No SDC found.\n");
1018 return hp_sdc.dev_err;
1019 }
1020
1021 init_MUTEX_LOCKED(&tq_init_sem);
1022
1023 tq_init.actidx = 0;
1024 tq_init.idx = 1;
1025 tq_init.endidx = 5;
1026 tq_init.seq = tq_init_seq;
1027 tq_init.act.semaphore = &tq_init_sem;
1028
Helge Dellerffd51f42007-02-28 23:51:29 -05001029 tq_init_seq[0] =
1030 HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
1032 tq_init_seq[2] = 1;
1033 tq_init_seq[3] = 0;
1034 tq_init_seq[4] = 0;
1035
1036 hp_sdc_enqueue_transaction(&tq_init);
1037
1038 down(&tq_init_sem);
1039 up(&tq_init_sem);
1040
1041 if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
1042 printk(KERN_WARNING PREFIX "Error reading config byte.\n");
1043 hp_sdc_exit();
1044 return -ENODEV;
1045 }
1046 hp_sdc.r11 = tq_init_seq[4];
1047 if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
Helge Dellerffd51f42007-02-28 23:51:29 -05001048 const char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 printk(KERN_INFO PREFIX "New style SDC\n");
1050 tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
1051 tq_init.actidx = 0;
1052 tq_init.idx = 1;
1053 down(&tq_init_sem);
Helge Dellerffd51f42007-02-28 23:51:29 -05001054 hp_sdc_enqueue_transaction(&tq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 down(&tq_init_sem);
1056 up(&tq_init_sem);
1057 if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
1058 printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
1059 return -ENODEV;
1060 }
1061 hp_sdc.r7e = tq_init_seq[4];
1062 HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
1063 printk(KERN_INFO PREFIX "Revision: %s\n", str);
Helge Dellerffd51f42007-02-28 23:51:29 -05001064 if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
Helge Dellerffd51f42007-02-28 23:51:29 -05001066 if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
1069 "on next firmware reset.\n");
Helge Dellerffd51f42007-02-28 23:51:29 -05001070 tq_init_seq[0] = HP_SDC_ACT_PRECMD |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
1072 tq_init_seq[1] = HP_SDC_CMD_SET_STR;
1073 tq_init_seq[2] = 1;
1074 tq_init_seq[3] = 0;
1075 tq_init.actidx = 0;
1076 tq_init.idx = 1;
1077 tq_init.endidx = 4;
1078 down(&tq_init_sem);
Helge Dellerffd51f42007-02-28 23:51:29 -05001079 hp_sdc_enqueue_transaction(&tq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 down(&tq_init_sem);
1081 up(&tq_init_sem);
Helge Dellerffd51f42007-02-28 23:51:29 -05001082 } else
1083 printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 return 0;
1087}
1088
1089module_init(hp_sdc_register);
1090module_exit(hp_sdc_exit);
1091
Helge Dellerffd51f42007-02-28 23:51:29 -05001092/* Timing notes: These measurements taken on my 64MHz 7100-LC (715/64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * cycles cycles-adj time
1094 * between two consecutive mfctl(16)'s: 4 n/a 63ns
1095 * hp_sdc_spin_ibf when idle: 119 115 1.7us
1096 * gsc_writeb status register: 83 79 1.2us
1097 * IBF to clear after sending SET_IM: 6204 6006 93us
Helge Dellerffd51f42007-02-28 23:51:29 -05001098 * IBF to clear after sending LOAD_RT: 4467 4352 68us
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * IBF to clear after sending two LOAD_RTs: 18974 18859 295us
1100 * READ_T1, read status/data, IRQ, call handler: 35564 n/a 556us
1101 * cmd to ~IBF READ_T1 2nd time right after: 5158403 n/a 81ms
1102 * between IRQ received and ~IBF for above: 2578877 n/a 40ms
1103 *
1104 * Performance stats after a run of this module configuring HIL and
1105 * receiving a few mouse events:
1106 *
1107 * status in8 282508 cycles 7128 calls
1108 * status out8 8404 cycles 341 calls
1109 * data out8 1734 cycles 78 calls
1110 * isr 174324 cycles 617 calls (includes take)
1111 * take 1241 cycles 2 calls
1112 * put 1411504 cycles 6937 calls
1113 * task 1655209 cycles 6937 calls (includes put)
1114 *
1115 */