blob: edfedd9a166cef53a42d5c17846fe9adbb77bb21 [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>
70#include <linux/slab.h>
71#include <linux/hil.h>
Geert Uytterhoeven4933d072008-05-05 21:16:13 +020072#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <asm/io.h>
74#include <asm/system.h>
75
76/* Machine-specific abstraction */
77
78#if defined(__hppa__)
79# include <asm/parisc-device.h>
80# define sdc_readb(p) gsc_readb(p)
81# define sdc_writeb(v,p) gsc_writeb((v),(p))
82#elif defined(__mc68000__)
83# include <asm/uaccess.h>
84# define sdc_readb(p) in_8(p)
85# define sdc_writeb(v,p) out_8((p),(v))
86#else
87# error "HIL is not supported on this platform"
88#endif
89
90#define PREFIX "HP SDC: "
91
92MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
93MODULE_DESCRIPTION("HP i8042-based SDC Driver");
94MODULE_LICENSE("Dual BSD/GPL");
95
96EXPORT_SYMBOL(hp_sdc_request_timer_irq);
97EXPORT_SYMBOL(hp_sdc_request_hil_irq);
98EXPORT_SYMBOL(hp_sdc_request_cooked_irq);
99
100EXPORT_SYMBOL(hp_sdc_release_timer_irq);
101EXPORT_SYMBOL(hp_sdc_release_hil_irq);
102EXPORT_SYMBOL(hp_sdc_release_cooked_irq);
103
Helge Deller95754992007-03-16 00:59:29 -0400104EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
106EXPORT_SYMBOL(hp_sdc_dequeue_transaction);
107
108static hp_i8042_sdc hp_sdc; /* All driver state is kept in here. */
109
110/*************** primitives for use in any context *********************/
Helge Dellerffd51f42007-02-28 23:51:29 -0500111static inline uint8_t hp_sdc_status_in8(void)
112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 uint8_t status;
114 unsigned long flags;
115
116 write_lock_irqsave(&hp_sdc.ibf_lock, flags);
117 status = sdc_readb(hp_sdc.status_io);
Helge Dellerffd51f42007-02-28 23:51:29 -0500118 if (!(status & HP_SDC_STATUS_IBF))
119 hp_sdc.ibf = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
121
122 return status;
123}
124
Helge Dellerffd51f42007-02-28 23:51:29 -0500125static inline uint8_t hp_sdc_data_in8(void)
126{
127 return sdc_readb(hp_sdc.data_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Helge Dellerffd51f42007-02-28 23:51:29 -0500130static inline void hp_sdc_status_out8(uint8_t val)
131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long flags;
133
134 write_lock_irqsave(&hp_sdc.ibf_lock, flags);
135 hp_sdc.ibf = 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500136 if ((val & 0xf0) == 0xe0)
137 hp_sdc.wi = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sdc_writeb(val, hp_sdc.status_io);
139 write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
140}
141
Helge Dellerffd51f42007-02-28 23:51:29 -0500142static inline void hp_sdc_data_out8(uint8_t val)
143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long flags;
145
146 write_lock_irqsave(&hp_sdc.ibf_lock, flags);
147 hp_sdc.ibf = 1;
148 sdc_writeb(val, hp_sdc.data_io);
149 write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
150}
151
Helge Dellerffd51f42007-02-28 23:51:29 -0500152/* Care must be taken to only invoke hp_sdc_spin_ibf when
153 * absolutely needed, or in rarely invoked subroutines.
154 * Not only does it waste CPU cycles, it also wastes bus cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
Helge Dellerffd51f42007-02-28 23:51:29 -0500156static inline void hp_sdc_spin_ibf(void)
157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long flags;
159 rwlock_t *lock;
160
161 lock = &hp_sdc.ibf_lock;
162
163 read_lock_irqsave(lock, flags);
164 if (!hp_sdc.ibf) {
165 read_unlock_irqrestore(lock, flags);
166 return;
167 }
168 read_unlock(lock);
169 write_lock(lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500170 while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
171 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 hp_sdc.ibf = 0;
173 write_unlock_irqrestore(lock, flags);
174}
175
176
177/************************ Interrupt context functions ************************/
Helge Dellerffd51f42007-02-28 23:51:29 -0500178static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
179{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 hp_sdc_transaction *curr;
181
182 read_lock(&hp_sdc.rtq_lock);
183 if (hp_sdc.rcurr < 0) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500184 read_unlock(&hp_sdc.rtq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return;
186 }
187 curr = hp_sdc.tq[hp_sdc.rcurr];
188 read_unlock(&hp_sdc.rtq_lock);
189
190 curr->seq[curr->idx++] = status;
191 curr->seq[curr->idx++] = data;
192 hp_sdc.rqty -= 2;
193 do_gettimeofday(&hp_sdc.rtv);
194
195 if (hp_sdc.rqty <= 0) {
196 /* All data has been gathered. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500197 if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
198 if (curr->act.semaphore)
199 up(curr->act.semaphore);
200
201 if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (curr->act.irqhook)
203 curr->act.irqhook(irq, dev_id, status, data);
Helge Dellerffd51f42007-02-28 23:51:29 -0500204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 curr->actidx = curr->idx;
206 curr->idx++;
207 /* Return control of this transaction */
208 write_lock(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500209 hp_sdc.rcurr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 hp_sdc.rqty = 0;
211 write_unlock(&hp_sdc.rtq_lock);
212 tasklet_schedule(&hp_sdc.task);
213 }
214}
215
Helge Dellerffd51f42007-02-28 23:51:29 -0500216static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 uint8_t status, data;
219
220 status = hp_sdc_status_in8();
221 /* Read data unconditionally to advance i8042. */
222 data = hp_sdc_data_in8();
223
224 /* For now we are ignoring these until we get the SDC to behave. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500225 if (((status & 0xf1) == 0x51) && data == 0x82)
226 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Helge Dellerffd51f42007-02-28 23:51:29 -0500228 switch (status & HP_SDC_STATUS_IRQMASK) {
229 case 0: /* This case is not documented. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500231
232 case HP_SDC_STATUS_USERTIMER:
233 case HP_SDC_STATUS_PERIODIC:
234 case HP_SDC_STATUS_TIMER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 read_lock(&hp_sdc.hook_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500236 if (hp_sdc.timer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 hp_sdc.timer(irq, dev_id, status, data);
238 read_unlock(&hp_sdc.hook_lock);
239 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500240
241 case HP_SDC_STATUS_REG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 hp_sdc_take(irq, dev_id, status, data);
243 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500244
245 case HP_SDC_STATUS_HILCMD:
246 case HP_SDC_STATUS_HILDATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 read_lock(&hp_sdc.hook_lock);
248 if (hp_sdc.hil != NULL)
249 hp_sdc.hil(irq, dev_id, status, data);
250 read_unlock(&hp_sdc.hook_lock);
251 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500252
253 case HP_SDC_STATUS_PUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 read_lock(&hp_sdc.hook_lock);
255 if (hp_sdc.pup != NULL)
256 hp_sdc.pup(irq, dev_id, status, data);
Helge Dellerffd51f42007-02-28 23:51:29 -0500257 else
258 printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 read_unlock(&hp_sdc.hook_lock);
260 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500261
262 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 read_lock(&hp_sdc.hook_lock);
264 if (hp_sdc.cooked != NULL)
265 hp_sdc.cooked(irq, dev_id, status, data);
266 read_unlock(&hp_sdc.hook_lock);
267 break;
268 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return IRQ_HANDLED;
271}
272
273
Helge Dellerffd51f42007-02-28 23:51:29 -0500274static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int status;
Helge Dellerffd51f42007-02-28 23:51:29 -0500277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 status = hp_sdc_status_in8();
279 printk(KERN_WARNING PREFIX "NMI !\n");
280
Helge Dellerffd51f42007-02-28 23:51:29 -0500281#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (status & HP_SDC_NMISTATUS_FHS) {
283 read_lock(&hp_sdc.hook_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500284 if (hp_sdc.timer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 hp_sdc.timer(irq, dev_id, status, 0);
286 read_unlock(&hp_sdc.hook_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500287 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* TODO: pass this on to the HIL handler, or do SAK here? */
289 printk(KERN_WARNING PREFIX "HIL NMI\n");
290 }
291#endif
Helge Dellerffd51f42007-02-28 23:51:29 -0500292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return IRQ_HANDLED;
294}
295
296
297/***************** Kernel (tasklet) context functions ****************/
298
299unsigned long hp_sdc_put(void);
300
Helge Dellerffd51f42007-02-28 23:51:29 -0500301static void hp_sdc_tasklet(unsigned long foo)
302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 write_lock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (hp_sdc.rcurr >= 0) {
306 struct timeval tv;
Helge Dellerffd51f42007-02-28 23:51:29 -0500307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 do_gettimeofday(&tv);
Helge Dellerffd51f42007-02-28 23:51:29 -0500309 if (tv.tv_sec > hp_sdc.rtv.tv_sec)
310 tv.tv_usec += USEC_PER_SEC;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
313 hp_sdc_transaction *curr;
314 uint8_t tmp;
315
316 curr = hp_sdc.tq[hp_sdc.rcurr];
317 /* If this turns out to be a normal failure mode
318 * we'll need to figure out a way to communicate
319 * it back to the application. and be less verbose.
320 */
321 printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
322 tv.tv_usec - hp_sdc.rtv.tv_usec);
323 curr->idx += hp_sdc.rqty;
324 hp_sdc.rqty = 0;
325 tmp = curr->seq[curr->actidx];
326 curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
Helge Dellerffd51f42007-02-28 23:51:29 -0500327 if (tmp & HP_SDC_ACT_SEMAPHORE)
328 if (curr->act.semaphore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 up(curr->act.semaphore);
Helge Dellerffd51f42007-02-28 23:51:29 -0500330
331 if (tmp & HP_SDC_ACT_CALLBACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Note this means that irqhooks may be called
333 * in tasklet/bh context.
334 */
Helge Dellerffd51f42007-02-28 23:51:29 -0500335 if (curr->act.irqhook)
Al Viro6ce6b3a2006-10-14 16:52:36 +0100336 curr->act.irqhook(0, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 curr->actidx = curr->idx;
340 curr->idx++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500341 hp_sdc.rcurr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 }
344 write_unlock_irq(&hp_sdc.rtq_lock);
345 hp_sdc_put();
346}
347
Helge Dellerffd51f42007-02-28 23:51:29 -0500348unsigned long hp_sdc_put(void)
349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 hp_sdc_transaction *curr;
351 uint8_t act;
352 int idx, curridx;
353
354 int limit = 0;
355
356 write_lock(&hp_sdc.lock);
357
358 /* If i8042 buffers are full, we cannot do anything that
359 requires output, so we skip to the administrativa. */
360 if (hp_sdc.ibf) {
361 hp_sdc_status_in8();
Helge Dellerffd51f42007-02-28 23:51:29 -0500362 if (hp_sdc.ibf)
363 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 anew:
367 /* See if we are in the middle of a sequence. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500368 if (hp_sdc.wcurr < 0)
369 hp_sdc.wcurr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 read_lock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500371 if (hp_sdc.rcurr == hp_sdc.wcurr)
372 hp_sdc.wcurr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 read_unlock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500374 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
375 hp_sdc.wcurr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 curridx = hp_sdc.wcurr;
377
Helge Dellerffd51f42007-02-28 23:51:29 -0500378 if (hp_sdc.tq[curridx] != NULL)
379 goto start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 while (++curridx != hp_sdc.wcurr) {
382 if (curridx >= HP_SDC_QUEUE_LEN) {
383 curridx = -1; /* Wrap to top */
384 continue;
385 }
386 read_lock_irq(&hp_sdc.rtq_lock);
387 if (hp_sdc.rcurr == curridx) {
388 read_unlock_irq(&hp_sdc.rtq_lock);
389 continue;
390 }
391 read_unlock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500392 if (hp_sdc.tq[curridx] != NULL)
393 break; /* Found one. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
396 curridx = -1;
397 }
398 hp_sdc.wcurr = curridx;
399
400 start:
401
402 /* Check to see if the interrupt mask needs to be set. */
403 if (hp_sdc.set_im) {
404 hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
405 hp_sdc.set_im = 0;
406 goto finish;
407 }
408
Helge Dellerffd51f42007-02-28 23:51:29 -0500409 if (hp_sdc.wcurr == -1)
410 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 curr = hp_sdc.tq[curridx];
413 idx = curr->actidx;
414
415 if (curr->actidx >= curr->endidx) {
416 hp_sdc.tq[curridx] = NULL;
417 /* Interleave outbound data between the transactions. */
418 hp_sdc.wcurr++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500419 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
420 hp_sdc.wcurr = 0;
421 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 act = curr->seq[idx];
425 idx++;
426
427 if (curr->idx >= curr->endidx) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500428 if (act & HP_SDC_ACT_DEALLOC)
429 kfree(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 hp_sdc.tq[curridx] = NULL;
431 /* Interleave outbound data between the transactions. */
432 hp_sdc.wcurr++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500433 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
434 hp_sdc.wcurr = 0;
435 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
438 while (act & HP_SDC_ACT_PRECMD) {
439 if (curr->idx != idx) {
440 idx++;
441 act &= ~HP_SDC_ACT_PRECMD;
442 break;
443 }
444 hp_sdc_status_out8(curr->seq[idx]);
445 curr->idx++;
446 /* act finished? */
447 if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
Helge Dellerffd51f42007-02-28 23:51:29 -0500448 goto actdone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* skip quantity field if data-out sequence follows. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500450 if (act & HP_SDC_ACT_DATAOUT)
451 curr->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto finish;
453 }
454 if (act & HP_SDC_ACT_DATAOUT) {
455 int qty;
456
457 qty = curr->seq[idx];
458 idx++;
459 if (curr->idx - idx < qty) {
460 hp_sdc_data_out8(curr->seq[curr->idx]);
461 curr->idx++;
462 /* act finished? */
Helge Dellerffd51f42007-02-28 23:51:29 -0500463 if (curr->idx - idx >= qty &&
464 (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto actdone;
466 goto finish;
467 }
468 idx += qty;
469 act &= ~HP_SDC_ACT_DATAOUT;
Helge Dellerffd51f42007-02-28 23:51:29 -0500470 } else
471 while (act & HP_SDC_ACT_DATAREG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int mask;
473 uint8_t w7[4];
474
475 mask = curr->seq[idx];
476 if (idx != curr->idx) {
477 idx++;
478 idx += !!(mask & 1);
479 idx += !!(mask & 2);
480 idx += !!(mask & 4);
481 idx += !!(mask & 8);
482 act &= ~HP_SDC_ACT_DATAREG;
483 break;
484 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
487 w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
488 w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
489 w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
Helge Dellerffd51f42007-02-28 23:51:29 -0500490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
Helge Dellerffd51f42007-02-28 23:51:29 -0500492 w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int i = 0;
494
Helge Dellerffd51f42007-02-28 23:51:29 -0500495 /* Need to point the write index register */
496 while (i < 4 && w7[i] == hp_sdc.r7[i])
497 i++;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (i < 4) {
500 hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
501 hp_sdc.wi = 0x70 + i;
502 goto finish;
503 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 idx++;
506 if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
507 goto actdone;
Helge Dellerffd51f42007-02-28 23:51:29 -0500508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 curr->idx = idx;
510 act &= ~HP_SDC_ACT_DATAREG;
511 break;
512 }
513
514 hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
515 hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
516 hp_sdc.wi++; /* write index register autoincrements */
517 {
518 int i = 0;
519
Helge Dellerffd51f42007-02-28 23:51:29 -0500520 while ((i < 4) && w7[i] == hp_sdc.r7[i])
521 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (i >= 4) {
523 curr->idx = idx + 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500524 if ((act & HP_SDC_ACT_DURING) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 HP_SDC_ACT_DATAREG)
Helge Dellerffd51f42007-02-28 23:51:29 -0500526 goto actdone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 }
529 goto finish;
530 }
531 /* We don't go any further in the command if there is a pending read,
532 because we don't want interleaved results. */
533 read_lock_irq(&hp_sdc.rtq_lock);
534 if (hp_sdc.rcurr >= 0) {
535 read_unlock_irq(&hp_sdc.rtq_lock);
536 goto finish;
537 }
538 read_unlock_irq(&hp_sdc.rtq_lock);
539
540
541 if (act & HP_SDC_ACT_POSTCMD) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500542 uint8_t postcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* curr->idx should == idx at this point. */
545 postcmd = curr->seq[idx];
546 curr->idx++;
547 if (act & HP_SDC_ACT_DATAIN) {
548
549 /* Start a new read */
Helge Dellerffd51f42007-02-28 23:51:29 -0500550 hp_sdc.rqty = curr->seq[curr->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 do_gettimeofday(&hp_sdc.rtv);
552 curr->idx++;
553 /* Still need to lock here in case of spurious irq. */
554 write_lock_irq(&hp_sdc.rtq_lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500555 hp_sdc.rcurr = curridx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 write_unlock_irq(&hp_sdc.rtq_lock);
557 hp_sdc_status_out8(postcmd);
558 goto finish;
559 }
560 hp_sdc_status_out8(postcmd);
561 goto actdone;
562 }
563
Helge Dellerffd51f42007-02-28 23:51:29 -0500564 actdone:
565 if (act & HP_SDC_ACT_SEMAPHORE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 up(curr->act.semaphore);
Helge Dellerffd51f42007-02-28 23:51:29 -0500567 else if (act & HP_SDC_ACT_CALLBACK)
Al Viro6ce6b3a2006-10-14 16:52:36 +0100568 curr->act.irqhook(0,NULL,0,0);
Helge Dellerffd51f42007-02-28 23:51:29 -0500569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (curr->idx >= curr->endidx) { /* This transaction is over. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500571 if (act & HP_SDC_ACT_DEALLOC)
572 kfree(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 hp_sdc.tq[curridx] = NULL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500574 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 curr->actidx = idx + 1;
576 curr->idx = idx + 2;
577 }
578 /* Interleave outbound data between the transactions. */
579 hp_sdc.wcurr++;
Helge Dellerffd51f42007-02-28 23:51:29 -0500580 if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
581 hp_sdc.wcurr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 finish:
Helge Dellerffd51f42007-02-28 23:51:29 -0500584 /* If by some quirk IBF has cleared and our ISR has run to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 see that that has happened, do it all again. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500586 if (!hp_sdc.ibf && limit++ < 20)
587 goto anew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 done:
Helge Dellerffd51f42007-02-28 23:51:29 -0500590 if (hp_sdc.wcurr >= 0)
591 tasklet_schedule(&hp_sdc.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 write_unlock(&hp_sdc.lock);
Helge Dellerffd51f42007-02-28 23:51:29 -0500593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
596
597/******* Functions called in either user or kernel context ****/
Helge Deller95754992007-03-16 00:59:29 -0400598int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
Helge Dellerffd51f42007-02-28 23:51:29 -0500599{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int i;
601
602 if (this == NULL) {
Helge Deller95754992007-03-16 00:59:29 -0400603 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Can't have same transaction on queue twice */
Helge Dellerffd51f42007-02-28 23:51:29 -0500608 for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
609 if (hp_sdc.tq[i] == this)
610 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 this->actidx = 0;
613 this->idx = 1;
614
615 /* Search for empty slot */
Helge Dellerffd51f42007-02-28 23:51:29 -0500616 for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (hp_sdc.tq[i] == NULL) {
618 hp_sdc.tq[i] = this;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 tasklet_schedule(&hp_sdc.task);
620 return 0;
621 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
624 return -EBUSY;
625
626 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
628 return -EINVAL;
629}
630
Helge Deller95754992007-03-16 00:59:29 -0400631int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
632 unsigned long flags;
633 int ret;
634
635 write_lock_irqsave(&hp_sdc.lock, flags);
636 ret = __hp_sdc_enqueue_transaction(this);
637 write_unlock_irqrestore(&hp_sdc.lock,flags);
638
639 return ret;
640}
641
Helge Dellerffd51f42007-02-28 23:51:29 -0500642int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
643{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned long flags;
645 int i;
646
647 write_lock_irqsave(&hp_sdc.lock, flags);
648
649 /* TODO: don't remove it if it's not done. */
650
Helge Dellerffd51f42007-02-28 23:51:29 -0500651 for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
652 if (hp_sdc.tq[i] == this)
653 hp_sdc.tq[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 write_unlock_irqrestore(&hp_sdc.lock, flags);
656 return 0;
657}
658
659
660
661/********************** User context functions **************************/
Helge Dellerffd51f42007-02-28 23:51:29 -0500662int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
663{
664 if (callback == NULL || hp_sdc.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 write_lock_irq(&hp_sdc.hook_lock);
668 if (hp_sdc.timer != NULL) {
669 write_unlock_irq(&hp_sdc.hook_lock);
670 return -EBUSY;
671 }
672
673 hp_sdc.timer = callback;
674 /* Enable interrupts from the timers */
675 hp_sdc.im &= ~HP_SDC_IM_FH;
676 hp_sdc.im &= ~HP_SDC_IM_PT;
677 hp_sdc.im &= ~HP_SDC_IM_TIMERS;
678 hp_sdc.set_im = 1;
679 write_unlock_irq(&hp_sdc.hook_lock);
680
681 tasklet_schedule(&hp_sdc.task);
682
683 return 0;
684}
685
Helge Dellerffd51f42007-02-28 23:51:29 -0500686int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
687{
688 if (callback == NULL || hp_sdc.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 write_lock_irq(&hp_sdc.hook_lock);
692 if (hp_sdc.hil != NULL) {
693 write_unlock_irq(&hp_sdc.hook_lock);
694 return -EBUSY;
695 }
696
697 hp_sdc.hil = callback;
698 hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
699 hp_sdc.set_im = 1;
700 write_unlock_irq(&hp_sdc.hook_lock);
701
702 tasklet_schedule(&hp_sdc.task);
703
704 return 0;
705}
706
Helge Dellerffd51f42007-02-28 23:51:29 -0500707int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
708{
709 if (callback == NULL || hp_sdc.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EINVAL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 write_lock_irq(&hp_sdc.hook_lock);
713 if (hp_sdc.cooked != NULL) {
714 write_unlock_irq(&hp_sdc.hook_lock);
715 return -EBUSY;
716 }
717
718 /* Enable interrupts from the HIL MLC */
719 hp_sdc.cooked = callback;
720 hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
721 hp_sdc.set_im = 1;
722 write_unlock_irq(&hp_sdc.hook_lock);
723
724 tasklet_schedule(&hp_sdc.task);
725
726 return 0;
727}
728
Helge Dellerffd51f42007-02-28 23:51:29 -0500729int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
730{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 write_lock_irq(&hp_sdc.hook_lock);
732 if ((callback != hp_sdc.timer) ||
733 (hp_sdc.timer == NULL)) {
734 write_unlock_irq(&hp_sdc.hook_lock);
735 return -EINVAL;
736 }
737
738 /* Disable interrupts from the timers */
739 hp_sdc.timer = NULL;
740 hp_sdc.im |= HP_SDC_IM_TIMERS;
741 hp_sdc.im |= HP_SDC_IM_FH;
742 hp_sdc.im |= HP_SDC_IM_PT;
743 hp_sdc.set_im = 1;
744 write_unlock_irq(&hp_sdc.hook_lock);
745 tasklet_schedule(&hp_sdc.task);
746
747 return 0;
748}
749
Helge Dellerffd51f42007-02-28 23:51:29 -0500750int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 write_lock_irq(&hp_sdc.hook_lock);
753 if ((callback != hp_sdc.hil) ||
754 (hp_sdc.hil == NULL)) {
755 write_unlock_irq(&hp_sdc.hook_lock);
756 return -EINVAL;
757 }
758
759 hp_sdc.hil = NULL;
760 /* Disable interrupts from HIL only if there is no cooked driver. */
761 if(hp_sdc.cooked == NULL) {
762 hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
763 hp_sdc.set_im = 1;
764 }
765 write_unlock_irq(&hp_sdc.hook_lock);
766 tasklet_schedule(&hp_sdc.task);
767
768 return 0;
769}
770
Helge Dellerffd51f42007-02-28 23:51:29 -0500771int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
772{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 write_lock_irq(&hp_sdc.hook_lock);
774 if ((callback != hp_sdc.cooked) ||
775 (hp_sdc.cooked == NULL)) {
776 write_unlock_irq(&hp_sdc.hook_lock);
777 return -EINVAL;
778 }
779
780 hp_sdc.cooked = NULL;
781 /* Disable interrupts from HIL only if there is no raw HIL driver. */
782 if(hp_sdc.hil == NULL) {
783 hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
784 hp_sdc.set_im = 1;
785 }
786 write_unlock_irq(&hp_sdc.hook_lock);
787 tasklet_schedule(&hp_sdc.task);
788
789 return 0;
790}
791
792/************************* Keepalive timer task *********************/
793
Helge Dellerffd51f42007-02-28 23:51:29 -0500794void hp_sdc_kicker (unsigned long data)
795{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 tasklet_schedule(&hp_sdc.task);
797 /* Re-insert the periodic task. */
798 mod_timer(&hp_sdc.kicker, jiffies + HZ);
799}
800
801/************************** Module Initialization ***************************/
802
803#if defined(__hppa__)
804
Helge Deller3acaf542007-02-28 23:51:19 -0500805static const struct parisc_device_id hp_sdc_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 {
Helge Dellerffd51f42007-02-28 23:51:29 -0500807 .hw_type = HPHW_FIO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .hversion_rev = HVERSION_REV_ANY_ID,
809 .hversion = HVERSION_ANY_ID,
Helge Dellerffd51f42007-02-28 23:51:29 -0500810 .sversion = 0x73,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 },
812 { 0, }
813};
814
815MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
816
817static int __init hp_sdc_init_hppa(struct parisc_device *d);
818
819static struct parisc_driver hp_sdc_driver = {
Matthew Wilcoxbdad1f82005-10-21 22:36:23 -0400820 .name = "hp_sdc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .id_table = hp_sdc_tbl,
822 .probe = hp_sdc_init_hppa,
823};
824
825#endif /* __hppa__ */
826
827static int __init hp_sdc_init(void)
828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 char *errstr;
830 hp_sdc_transaction t_sync;
831 uint8_t ts_sync[6];
832 struct semaphore s_sync;
833
Helge Dellerffd51f42007-02-28 23:51:29 -0500834 rwlock_init(&hp_sdc.lock);
835 rwlock_init(&hp_sdc.ibf_lock);
836 rwlock_init(&hp_sdc.rtq_lock);
837 rwlock_init(&hp_sdc.hook_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 hp_sdc.timer = NULL;
840 hp_sdc.hil = NULL;
841 hp_sdc.pup = NULL;
842 hp_sdc.cooked = NULL;
843 hp_sdc.im = HP_SDC_IM_MASK; /* Mask maskable irqs */
844 hp_sdc.set_im = 1;
845 hp_sdc.wi = 0xff;
846 hp_sdc.r7[0] = 0xff;
847 hp_sdc.r7[1] = 0xff;
848 hp_sdc.r7[2] = 0xff;
849 hp_sdc.r7[3] = 0xff;
850 hp_sdc.ibf = 1;
851
Helge Dellerffd51f42007-02-28 23:51:29 -0500852 memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 hp_sdc.wcurr = -1;
855 hp_sdc.rcurr = -1;
856 hp_sdc.rqty = 0;
857
858 hp_sdc.dev_err = -ENODEV;
859
860 errstr = "IO not found for";
Helge Dellerffd51f42007-02-28 23:51:29 -0500861 if (!hp_sdc.base_io)
862 goto err0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 errstr = "IRQ not found for";
Helge Dellerffd51f42007-02-28 23:51:29 -0500865 if (!hp_sdc.irq)
866 goto err0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 hp_sdc.dev_err = -EBUSY;
869
870#if defined(__hppa__)
871 errstr = "IO not available for";
Helge Dellerffd51f42007-02-28 23:51:29 -0500872 if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
873 goto err0;
874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 errstr = "IRQ not available for";
Helge Deller3acaf542007-02-28 23:51:19 -0500877 if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
Helge Dellerffd51f42007-02-28 23:51:29 -0500878 "HP SDC", &hp_sdc))
879 goto err1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 errstr = "NMI not available for";
Helge Deller3acaf542007-02-28 23:51:19 -0500882 if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
Helge Dellerffd51f42007-02-28 23:51:29 -0500883 "HP SDC NMI", &hp_sdc))
884 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Helge Dellerffd51f42007-02-28 23:51:29 -0500886 printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
888
889 hp_sdc_status_in8();
890 hp_sdc_data_in8();
891
892 tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
893
894 /* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
895 t_sync.actidx = 0;
896 t_sync.idx = 1;
897 t_sync.endidx = 6;
898 t_sync.seq = ts_sync;
899 ts_sync[0] = HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
900 ts_sync[1] = 0x0f;
901 ts_sync[2] = ts_sync[3] = ts_sync[4] = ts_sync[5] = 0;
902 t_sync.act.semaphore = &s_sync;
903 init_MUTEX_LOCKED(&s_sync);
904 hp_sdc_enqueue_transaction(&t_sync);
905 down(&s_sync); /* Wait for t_sync to complete */
906
907 /* Create the keepalive task */
908 init_timer(&hp_sdc.kicker);
909 hp_sdc.kicker.expires = jiffies + HZ;
910 hp_sdc.kicker.function = &hp_sdc_kicker;
911 add_timer(&hp_sdc.kicker);
912
913 hp_sdc.dev_err = 0;
914 return 0;
915 err2:
Helge Deller3acaf542007-02-28 23:51:19 -0500916 free_irq(hp_sdc.irq, &hp_sdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 err1:
918 release_region(hp_sdc.data_io, 2);
919 err0:
Helge Dellerffd51f42007-02-28 23:51:29 -0500920 printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
922 hp_sdc.dev = NULL;
Helge Dellerffd51f42007-02-28 23:51:29 -0500923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return hp_sdc.dev_err;
925}
926
927#if defined(__hppa__)
928
929static int __init hp_sdc_init_hppa(struct parisc_device *d)
930{
Helge Dellerffd51f42007-02-28 23:51:29 -0500931 if (!d)
932 return 1;
933 if (hp_sdc.dev != NULL)
934 return 1; /* We only expect one SDC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 hp_sdc.dev = d;
937 hp_sdc.irq = d->irq;
938 hp_sdc.nmi = d->aux_irq;
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400939 hp_sdc.base_io = d->hpa.start;
940 hp_sdc.data_io = d->hpa.start + 0x800;
941 hp_sdc.status_io = d->hpa.start + 0x801;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 return hp_sdc_init();
944}
945
946#endif /* __hppa__ */
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948static void hp_sdc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 write_lock_irq(&hp_sdc.lock);
951
952 /* Turn off all maskable "sub-function" irq's. */
953 hp_sdc_spin_ibf();
954 sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
955
956 /* Wait until we know this has been processed by the i8042 */
957 hp_sdc_spin_ibf();
958
Helge Deller3acaf542007-02-28 23:51:19 -0500959 free_irq(hp_sdc.nmi, &hp_sdc);
960 free_irq(hp_sdc.irq, &hp_sdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 write_unlock_irq(&hp_sdc.lock);
962
963 del_timer(&hp_sdc.kicker);
964
965 tasklet_kill(&hp_sdc.task);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967#if defined(__hppa__)
Helge Dellerffd51f42007-02-28 23:51:29 -0500968 if (unregister_parisc_driver(&hp_sdc_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
970#endif
971}
972
973static int __init hp_sdc_register(void)
974{
975 hp_sdc_transaction tq_init;
976 uint8_t tq_init_seq[5];
977 struct semaphore tq_init_sem;
978#if defined(__mc68000__)
979 mm_segment_t fs;
980 unsigned char i;
981#endif
Helge Dellerffd51f42007-02-28 23:51:29 -0500982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 hp_sdc.dev = NULL;
984 hp_sdc.dev_err = 0;
985#if defined(__hppa__)
986 if (register_parisc_driver(&hp_sdc_driver)) {
987 printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
988 return -ENODEV;
989 }
990#elif defined(__mc68000__)
991 if (!MACH_IS_HP300)
992 return -ENODEV;
993
994 hp_sdc.irq = 1;
995 hp_sdc.nmi = 7;
996 hp_sdc.base_io = (unsigned long) 0xf0428000;
997 hp_sdc.data_io = (unsigned long) hp_sdc.base_io + 1;
998 hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
999 fs = get_fs();
1000 set_fs(KERNEL_DS);
1001 if (!get_user(i, (unsigned char *)hp_sdc.data_io))
1002 hp_sdc.dev = (void *)1;
1003 set_fs(fs);
1004 hp_sdc.dev_err = hp_sdc_init();
1005#endif
1006 if (hp_sdc.dev == NULL) {
1007 printk(KERN_WARNING PREFIX "No SDC found.\n");
1008 return hp_sdc.dev_err;
1009 }
1010
1011 init_MUTEX_LOCKED(&tq_init_sem);
1012
1013 tq_init.actidx = 0;
1014 tq_init.idx = 1;
1015 tq_init.endidx = 5;
1016 tq_init.seq = tq_init_seq;
1017 tq_init.act.semaphore = &tq_init_sem;
1018
Helge Dellerffd51f42007-02-28 23:51:29 -05001019 tq_init_seq[0] =
1020 HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
1022 tq_init_seq[2] = 1;
1023 tq_init_seq[3] = 0;
1024 tq_init_seq[4] = 0;
1025
1026 hp_sdc_enqueue_transaction(&tq_init);
1027
1028 down(&tq_init_sem);
1029 up(&tq_init_sem);
1030
1031 if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
1032 printk(KERN_WARNING PREFIX "Error reading config byte.\n");
1033 hp_sdc_exit();
1034 return -ENODEV;
1035 }
1036 hp_sdc.r11 = tq_init_seq[4];
1037 if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
Helge Dellerffd51f42007-02-28 23:51:29 -05001038 const char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk(KERN_INFO PREFIX "New style SDC\n");
1040 tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
1041 tq_init.actidx = 0;
1042 tq_init.idx = 1;
1043 down(&tq_init_sem);
Helge Dellerffd51f42007-02-28 23:51:29 -05001044 hp_sdc_enqueue_transaction(&tq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 down(&tq_init_sem);
1046 up(&tq_init_sem);
1047 if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
1048 printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
1049 return -ENODEV;
1050 }
1051 hp_sdc.r7e = tq_init_seq[4];
1052 HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
1053 printk(KERN_INFO PREFIX "Revision: %s\n", str);
Helge Dellerffd51f42007-02-28 23:51:29 -05001054 if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
Helge Dellerffd51f42007-02-28 23:51:29 -05001056 if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
1059 "on next firmware reset.\n");
Helge Dellerffd51f42007-02-28 23:51:29 -05001060 tq_init_seq[0] = HP_SDC_ACT_PRECMD |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
1062 tq_init_seq[1] = HP_SDC_CMD_SET_STR;
1063 tq_init_seq[2] = 1;
1064 tq_init_seq[3] = 0;
1065 tq_init.actidx = 0;
1066 tq_init.idx = 1;
1067 tq_init.endidx = 4;
1068 down(&tq_init_sem);
Helge Dellerffd51f42007-02-28 23:51:29 -05001069 hp_sdc_enqueue_transaction(&tq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 down(&tq_init_sem);
1071 up(&tq_init_sem);
Helge Dellerffd51f42007-02-28 23:51:29 -05001072 } else
1073 printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return 0;
1077}
1078
1079module_init(hp_sdc_register);
1080module_exit(hp_sdc_exit);
1081
Helge Dellerffd51f42007-02-28 23:51:29 -05001082/* Timing notes: These measurements taken on my 64MHz 7100-LC (715/64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * cycles cycles-adj time
1084 * between two consecutive mfctl(16)'s: 4 n/a 63ns
1085 * hp_sdc_spin_ibf when idle: 119 115 1.7us
1086 * gsc_writeb status register: 83 79 1.2us
1087 * IBF to clear after sending SET_IM: 6204 6006 93us
Helge Dellerffd51f42007-02-28 23:51:29 -05001088 * IBF to clear after sending LOAD_RT: 4467 4352 68us
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * IBF to clear after sending two LOAD_RTs: 18974 18859 295us
1090 * READ_T1, read status/data, IRQ, call handler: 35564 n/a 556us
1091 * cmd to ~IBF READ_T1 2nd time right after: 5158403 n/a 81ms
1092 * between IRQ received and ~IBF for above: 2578877 n/a 40ms
1093 *
1094 * Performance stats after a run of this module configuring HIL and
1095 * receiving a few mouse events:
1096 *
1097 * status in8 282508 cycles 7128 calls
1098 * status out8 8404 cycles 341 calls
1099 * data out8 1734 cycles 78 calls
1100 * isr 174324 cycles 617 calls (includes take)
1101 * take 1241 cycles 2 calls
1102 * put 1411504 cycles 6937 calls
1103 * task 1655209 cycles 6937 calls (includes put)
1104 *
1105 */