hp_sdc.c 27.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * HP i8042-based System Device Controller driver.
 *
 * Copyright (c) 2001 Brian S. Julin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL").
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *
 * References:
 * System Device Controller Microprocessor Firmware Theory of Operation
 *      for Part Number 1820-4784 Revision B.  Dwg No. A-1820-4784-2
 * Helge Deller's original hilkbd.c port for PA-RISC.
 *
 *
 * Driver theory of operation:
 *
H
Helge Deller 已提交
37 38
 * hp_sdc_put does all writing to the SDC.  ISR can run on a different
 * CPU than hp_sdc_put, but only one CPU runs hp_sdc_put at a time
L
Linus Torvalds 已提交
39 40
 * (it cannot really benefit from SMP anyway.)  A tasket fit this perfectly.
 *
H
Helge Deller 已提交
41 42 43 44 45 46
 * All data coming back from the SDC is sent via interrupt and can be read
 * fully in the ISR, so there are no latency/throughput problems there.
 * The problem is with output, due to the slow clock speed of the SDC
 * compared to the CPU.  This should not be too horrible most of the time,
 * but if used with HIL devices that support the multibyte transfer command,
 * keeping outbound throughput flowing at the 6500KBps that the HIL is
L
Linus Torvalds 已提交
47 48
 * capable of is more than can be done at HZ=100.
 *
H
Helge Deller 已提交
49 50 51 52
 * Busy polling for IBF clear wastes CPU cycles and bus cycles.  hp_sdc.ibf
 * is set to 0 when the IBF flag in the status register has cleared.  ISR
 * may do this, and may also access the parts of queued transactions related
 * to reading data back from the SDC, but otherwise will not touch the
L
Linus Torvalds 已提交
53 54 55 56
 * hp_sdc state. Whenever a register is written hp_sdc.ibf is set to 1.
 *
 * The i8042 write index and the values in the 4-byte input buffer
 * starting at 0x70 are kept track of in hp_sdc.wi, and .r7[], respectively,
H
Helge Deller 已提交
57
 * to minimize the amount of IO needed to the SDC.  However these values
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 * do not need to be locked since they are only ever accessed by hp_sdc_put.
 *
 * A timer task schedules the tasklet once per second just to make
 * sure it doesn't freeze up and to allow for bad reads to time out.
 */

#include <linux/hp_sdc.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/time.h>
#include <linux/slab.h>
#include <linux/hil.h>
#include <asm/io.h>
#include <asm/system.h>

/* Machine-specific abstraction */

#if defined(__hppa__)
# include <asm/parisc-device.h>
# define sdc_readb(p)		gsc_readb(p)
# define sdc_writeb(v,p)	gsc_writeb((v),(p))
#elif defined(__mc68000__)
# include <asm/uaccess.h>
# define sdc_readb(p)		in_8(p)
# define sdc_writeb(v,p)	out_8((p),(v))
#else
# error "HIL is not supported on this platform"
#endif

#define PREFIX "HP SDC: "

MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
MODULE_DESCRIPTION("HP i8042-based SDC Driver");
MODULE_LICENSE("Dual BSD/GPL");

EXPORT_SYMBOL(hp_sdc_request_timer_irq);
EXPORT_SYMBOL(hp_sdc_request_hil_irq);
EXPORT_SYMBOL(hp_sdc_request_cooked_irq);

EXPORT_SYMBOL(hp_sdc_release_timer_irq);
EXPORT_SYMBOL(hp_sdc_release_hil_irq);
EXPORT_SYMBOL(hp_sdc_release_cooked_irq);

103
EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
L
Linus Torvalds 已提交
104 105 106 107 108 109
EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
EXPORT_SYMBOL(hp_sdc_dequeue_transaction);

static hp_i8042_sdc	hp_sdc;	/* All driver state is kept in here. */

/*************** primitives for use in any context *********************/
H
Helge Deller 已提交
110 111
static inline uint8_t hp_sdc_status_in8(void)
{
L
Linus Torvalds 已提交
112 113 114 115 116
	uint8_t status;
	unsigned long flags;

	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
	status = sdc_readb(hp_sdc.status_io);
H
Helge Deller 已提交
117 118
	if (!(status & HP_SDC_STATUS_IBF))
		hp_sdc.ibf = 0;
L
Linus Torvalds 已提交
119 120 121 122 123
	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);

	return status;
}

H
Helge Deller 已提交
124 125 126
static inline uint8_t hp_sdc_data_in8(void)
{
	return sdc_readb(hp_sdc.data_io);
L
Linus Torvalds 已提交
127 128
}

H
Helge Deller 已提交
129 130
static inline void hp_sdc_status_out8(uint8_t val)
{
L
Linus Torvalds 已提交
131 132 133 134
	unsigned long flags;

	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
	hp_sdc.ibf = 1;
H
Helge Deller 已提交
135 136
	if ((val & 0xf0) == 0xe0)
		hp_sdc.wi = 0xff;
L
Linus Torvalds 已提交
137 138 139 140
	sdc_writeb(val, hp_sdc.status_io);
	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
}

H
Helge Deller 已提交
141 142
static inline void hp_sdc_data_out8(uint8_t val)
{
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150
	unsigned long flags;

	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
	hp_sdc.ibf = 1;
	sdc_writeb(val, hp_sdc.data_io);
	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
}

H
Helge Deller 已提交
151 152 153
/*	Care must be taken to only invoke hp_sdc_spin_ibf when
 *	absolutely needed, or in rarely invoked subroutines.
 *	Not only does it waste CPU cycles, it also wastes bus cycles.
L
Linus Torvalds 已提交
154
 */
H
Helge Deller 已提交
155 156
static inline void hp_sdc_spin_ibf(void)
{
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164 165 166 167 168
	unsigned long flags;
	rwlock_t *lock;

	lock = &hp_sdc.ibf_lock;

	read_lock_irqsave(lock, flags);
	if (!hp_sdc.ibf) {
		read_unlock_irqrestore(lock, flags);
		return;
	}
	read_unlock(lock);
	write_lock(lock);
H
Helge Deller 已提交
169 170
	while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
		{ }
L
Linus Torvalds 已提交
171 172 173 174 175 176
	hp_sdc.ibf = 0;
	write_unlock_irqrestore(lock, flags);
}


/************************ Interrupt context functions ************************/
H
Helge Deller 已提交
177 178
static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
{
L
Linus Torvalds 已提交
179 180 181 182
	hp_sdc_transaction *curr;

	read_lock(&hp_sdc.rtq_lock);
	if (hp_sdc.rcurr < 0) {
H
Helge Deller 已提交
183
		read_unlock(&hp_sdc.rtq_lock);
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195
		return;
	}
	curr = hp_sdc.tq[hp_sdc.rcurr];
	read_unlock(&hp_sdc.rtq_lock);

	curr->seq[curr->idx++] = status;
	curr->seq[curr->idx++] = data;
	hp_sdc.rqty -= 2;
	do_gettimeofday(&hp_sdc.rtv);

	if (hp_sdc.rqty <= 0) {
		/* All data has been gathered. */
H
Helge Deller 已提交
196 197 198 199 200
		if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
			if (curr->act.semaphore)
				up(curr->act.semaphore);

		if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
L
Linus Torvalds 已提交
201 202
			if (curr->act.irqhook)
				curr->act.irqhook(irq, dev_id, status, data);
H
Helge Deller 已提交
203

L
Linus Torvalds 已提交
204 205 206 207
		curr->actidx = curr->idx;
		curr->idx++;
		/* Return control of this transaction */
		write_lock(&hp_sdc.rtq_lock);
H
Helge Deller 已提交
208
		hp_sdc.rcurr = -1;
L
Linus Torvalds 已提交
209 210 211 212 213 214
		hp_sdc.rqty = 0;
		write_unlock(&hp_sdc.rtq_lock);
		tasklet_schedule(&hp_sdc.task);
	}
}

H
Helge Deller 已提交
215 216
static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
{
L
Linus Torvalds 已提交
217 218 219 220 221 222 223
	uint8_t status, data;

	status = hp_sdc_status_in8();
	/* Read data unconditionally to advance i8042. */
	data =   hp_sdc_data_in8();

	/* For now we are ignoring these until we get the SDC to behave. */
H
Helge Deller 已提交
224 225
	if (((status & 0xf1) == 0x51) && data == 0x82)
		return IRQ_HANDLED;
L
Linus Torvalds 已提交
226

H
Helge Deller 已提交
227 228
	switch (status & HP_SDC_STATUS_IRQMASK) {
	case 0: /* This case is not documented. */
L
Linus Torvalds 已提交
229
		break;
H
Helge Deller 已提交
230 231 232 233

	case HP_SDC_STATUS_USERTIMER:
	case HP_SDC_STATUS_PERIODIC:
	case HP_SDC_STATUS_TIMER:
L
Linus Torvalds 已提交
234
		read_lock(&hp_sdc.hook_lock);
H
Helge Deller 已提交
235
		if (hp_sdc.timer != NULL)
L
Linus Torvalds 已提交
236 237 238
			hp_sdc.timer(irq, dev_id, status, data);
		read_unlock(&hp_sdc.hook_lock);
		break;
H
Helge Deller 已提交
239 240

	case HP_SDC_STATUS_REG:
L
Linus Torvalds 已提交
241 242
		hp_sdc_take(irq, dev_id, status, data);
		break;
H
Helge Deller 已提交
243 244 245

	case HP_SDC_STATUS_HILCMD:
	case HP_SDC_STATUS_HILDATA:
L
Linus Torvalds 已提交
246 247 248 249 250
		read_lock(&hp_sdc.hook_lock);
		if (hp_sdc.hil != NULL)
			hp_sdc.hil(irq, dev_id, status, data);
		read_unlock(&hp_sdc.hook_lock);
		break;
H
Helge Deller 已提交
251 252

	case HP_SDC_STATUS_PUP:
L
Linus Torvalds 已提交
253 254 255
		read_lock(&hp_sdc.hook_lock);
		if (hp_sdc.pup != NULL)
			hp_sdc.pup(irq, dev_id, status, data);
H
Helge Deller 已提交
256 257
		else
			printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
L
Linus Torvalds 已提交
258 259
		read_unlock(&hp_sdc.hook_lock);
		break;
H
Helge Deller 已提交
260 261

	default:
L
Linus Torvalds 已提交
262 263 264 265 266 267
		read_lock(&hp_sdc.hook_lock);
		if (hp_sdc.cooked != NULL)
			hp_sdc.cooked(irq, dev_id, status, data);
		read_unlock(&hp_sdc.hook_lock);
		break;
	}
H
Helge Deller 已提交
268

L
Linus Torvalds 已提交
269 270 271 272
	return IRQ_HANDLED;
}


H
Helge Deller 已提交
273 274
static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
{
L
Linus Torvalds 已提交
275
	int status;
H
Helge Deller 已提交
276

L
Linus Torvalds 已提交
277 278 279
	status = hp_sdc_status_in8();
	printk(KERN_WARNING PREFIX "NMI !\n");

H
Helge Deller 已提交
280
#if 0
L
Linus Torvalds 已提交
281 282
	if (status & HP_SDC_NMISTATUS_FHS) {
		read_lock(&hp_sdc.hook_lock);
H
Helge Deller 已提交
283
		if (hp_sdc.timer != NULL)
L
Linus Torvalds 已提交
284 285
			hp_sdc.timer(irq, dev_id, status, 0);
		read_unlock(&hp_sdc.hook_lock);
H
Helge Deller 已提交
286
	} else {
L
Linus Torvalds 已提交
287 288 289 290
		/* TODO: pass this on to the HIL handler, or do SAK here? */
		printk(KERN_WARNING PREFIX "HIL NMI\n");
	}
#endif
H
Helge Deller 已提交
291

L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299
	return IRQ_HANDLED;
}


/***************** Kernel (tasklet) context functions ****************/

unsigned long hp_sdc_put(void);

H
Helge Deller 已提交
300 301
static void hp_sdc_tasklet(unsigned long foo)
{
L
Linus Torvalds 已提交
302
	write_lock_irq(&hp_sdc.rtq_lock);
H
Helge Deller 已提交
303

L
Linus Torvalds 已提交
304 305
	if (hp_sdc.rcurr >= 0) {
		struct timeval tv;
H
Helge Deller 已提交
306

L
Linus Torvalds 已提交
307
		do_gettimeofday(&tv);
H
Helge Deller 已提交
308 309 310
		if (tv.tv_sec > hp_sdc.rtv.tv_sec)
			tv.tv_usec += USEC_PER_SEC;

L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
			hp_sdc_transaction *curr;
			uint8_t tmp;

			curr = hp_sdc.tq[hp_sdc.rcurr];
			/* If this turns out to be a normal failure mode
			 * we'll need to figure out a way to communicate
			 * it back to the application. and be less verbose.
			 */
			printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
			       tv.tv_usec - hp_sdc.rtv.tv_usec);
			curr->idx += hp_sdc.rqty;
			hp_sdc.rqty = 0;
			tmp = curr->seq[curr->actidx];
			curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
H
Helge Deller 已提交
326 327
			if (tmp & HP_SDC_ACT_SEMAPHORE)
				if (curr->act.semaphore)
L
Linus Torvalds 已提交
328
					up(curr->act.semaphore);
H
Helge Deller 已提交
329 330

			if (tmp & HP_SDC_ACT_CALLBACK) {
L
Linus Torvalds 已提交
331 332 333
				/* Note this means that irqhooks may be called
				 * in tasklet/bh context.
				 */
H
Helge Deller 已提交
334
				if (curr->act.irqhook)
335
					curr->act.irqhook(0, NULL, 0, 0);
L
Linus Torvalds 已提交
336
			}
H
Helge Deller 已提交
337

L
Linus Torvalds 已提交
338 339
			curr->actidx = curr->idx;
			curr->idx++;
H
Helge Deller 已提交
340
			hp_sdc.rcurr = -1;
L
Linus Torvalds 已提交
341 342 343 344 345 346
		}
	}
	write_unlock_irq(&hp_sdc.rtq_lock);
	hp_sdc_put();
}

H
Helge Deller 已提交
347 348
unsigned long hp_sdc_put(void)
{
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359 360
	hp_sdc_transaction *curr;
	uint8_t act;
	int idx, curridx;

	int limit = 0;

	write_lock(&hp_sdc.lock);

	/* If i8042 buffers are full, we cannot do anything that
	   requires output, so we skip to the administrativa. */
	if (hp_sdc.ibf) {
		hp_sdc_status_in8();
H
Helge Deller 已提交
361 362
		if (hp_sdc.ibf)
			goto finish;
L
Linus Torvalds 已提交
363 364 365 366
	}

 anew:
	/* See if we are in the middle of a sequence. */
H
Helge Deller 已提交
367 368
	if (hp_sdc.wcurr < 0)
		hp_sdc.wcurr = 0;
L
Linus Torvalds 已提交
369
	read_lock_irq(&hp_sdc.rtq_lock);
H
Helge Deller 已提交
370 371
	if (hp_sdc.rcurr == hp_sdc.wcurr)
		hp_sdc.wcurr++;
L
Linus Torvalds 已提交
372
	read_unlock_irq(&hp_sdc.rtq_lock);
H
Helge Deller 已提交
373 374
	if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
		hp_sdc.wcurr = 0;
L
Linus Torvalds 已提交
375 376
	curridx = hp_sdc.wcurr;

H
Helge Deller 已提交
377 378
	if (hp_sdc.tq[curridx] != NULL)
		goto start;
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390

	while (++curridx != hp_sdc.wcurr) {
		if (curridx >= HP_SDC_QUEUE_LEN) {
			curridx = -1; /* Wrap to top */
			continue;
		}
		read_lock_irq(&hp_sdc.rtq_lock);
		if (hp_sdc.rcurr == curridx) {
			read_unlock_irq(&hp_sdc.rtq_lock);
			continue;
		}
		read_unlock_irq(&hp_sdc.rtq_lock);
H
Helge Deller 已提交
391 392
		if (hp_sdc.tq[curridx] != NULL)
			break; /* Found one. */
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	}
	if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
		curridx = -1;
	}
	hp_sdc.wcurr = curridx;

 start:

	/* Check to see if the interrupt mask needs to be set. */
	if (hp_sdc.set_im) {
		hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
		hp_sdc.set_im = 0;
		goto finish;
	}

H
Helge Deller 已提交
408 409
	if (hp_sdc.wcurr == -1)
		goto done;
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417

	curr = hp_sdc.tq[curridx];
	idx = curr->actidx;

	if (curr->actidx >= curr->endidx) {
		hp_sdc.tq[curridx] = NULL;
		/* Interleave outbound data between the transactions. */
		hp_sdc.wcurr++;
H
Helge Deller 已提交
418 419 420
		if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
			hp_sdc.wcurr = 0;
		goto finish;
L
Linus Torvalds 已提交
421 422 423 424 425 426
	}

	act = curr->seq[idx];
	idx++;

	if (curr->idx >= curr->endidx) {
H
Helge Deller 已提交
427 428
		if (act & HP_SDC_ACT_DEALLOC)
			kfree(curr);
L
Linus Torvalds 已提交
429 430 431
		hp_sdc.tq[curridx] = NULL;
		/* Interleave outbound data between the transactions. */
		hp_sdc.wcurr++;
H
Helge Deller 已提交
432 433 434
		if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
			hp_sdc.wcurr = 0;
		goto finish;
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446
	}

	while (act & HP_SDC_ACT_PRECMD) {
		if (curr->idx != idx) {
			idx++;
			act &= ~HP_SDC_ACT_PRECMD;
			break;
		}
		hp_sdc_status_out8(curr->seq[idx]);
		curr->idx++;
		/* act finished? */
		if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
H
Helge Deller 已提交
447
			goto actdone;
L
Linus Torvalds 已提交
448
		/* skip quantity field if data-out sequence follows. */
H
Helge Deller 已提交
449 450
		if (act & HP_SDC_ACT_DATAOUT)
			curr->idx++;
L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458 459 460 461
		goto finish;
	}
	if (act & HP_SDC_ACT_DATAOUT) {
		int qty;

		qty = curr->seq[idx];
		idx++;
		if (curr->idx - idx < qty) {
			hp_sdc_data_out8(curr->seq[curr->idx]);
			curr->idx++;
			/* act finished? */
H
Helge Deller 已提交
462 463
			if (curr->idx - idx >= qty &&
			    (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
L
Linus Torvalds 已提交
464 465 466 467 468
				goto actdone;
			goto finish;
		}
		idx += qty;
		act &= ~HP_SDC_ACT_DATAOUT;
H
Helge Deller 已提交
469 470
	} else
	    while (act & HP_SDC_ACT_DATAREG) {
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483
		int mask;
		uint8_t w7[4];

		mask = curr->seq[idx];
		if (idx != curr->idx) {
			idx++;
			idx += !!(mask & 1);
			idx += !!(mask & 2);
			idx += !!(mask & 4);
			idx += !!(mask & 8);
			act &= ~HP_SDC_ACT_DATAREG;
			break;
		}
H
Helge Deller 已提交
484

L
Linus Torvalds 已提交
485 486 487 488
		w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
		w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
		w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
		w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
H
Helge Deller 已提交
489

L
Linus Torvalds 已提交
490
		if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
H
Helge Deller 已提交
491
		    w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
L
Linus Torvalds 已提交
492 493
			int i = 0;

H
Helge Deller 已提交
494 495 496 497
			/* Need to point the write index register */
			while (i < 4 && w7[i] == hp_sdc.r7[i])
				i++;

L
Linus Torvalds 已提交
498 499 500 501 502
			if (i < 4) {
				hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
				hp_sdc.wi = 0x70 + i;
				goto finish;
			}
H
Helge Deller 已提交
503

L
Linus Torvalds 已提交
504 505 506
			idx++;
			if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
				goto actdone;
H
Helge Deller 已提交
507

L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518
			curr->idx = idx;
			act &= ~HP_SDC_ACT_DATAREG;
			break;
		}

		hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
		hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
		hp_sdc.wi++; /* write index register autoincrements */
		{
			int i = 0;

H
Helge Deller 已提交
519 520
			while ((i < 4) && w7[i] == hp_sdc.r7[i])
				i++;
L
Linus Torvalds 已提交
521 522
			if (i >= 4) {
				curr->idx = idx + 1;
H
Helge Deller 已提交
523
				if ((act & HP_SDC_ACT_DURING) ==
L
Linus Torvalds 已提交
524
				    HP_SDC_ACT_DATAREG)
H
Helge Deller 已提交
525
					goto actdone;
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
			}
		}
		goto finish;
	}
	/* We don't go any further in the command if there is a pending read,
	   because we don't want interleaved results. */
	read_lock_irq(&hp_sdc.rtq_lock);
	if (hp_sdc.rcurr >= 0) {
		read_unlock_irq(&hp_sdc.rtq_lock);
		goto finish;
	}
	read_unlock_irq(&hp_sdc.rtq_lock);


	if (act & HP_SDC_ACT_POSTCMD) {
H
Helge Deller 已提交
541
		uint8_t postcmd;
L
Linus Torvalds 已提交
542 543 544 545 546 547 548

		/* curr->idx should == idx at this point. */
		postcmd = curr->seq[idx];
		curr->idx++;
		if (act & HP_SDC_ACT_DATAIN) {

			/* Start a new read */
H
Helge Deller 已提交
549
			hp_sdc.rqty = curr->seq[curr->idx];
L
Linus Torvalds 已提交
550 551 552 553
			do_gettimeofday(&hp_sdc.rtv);
			curr->idx++;
			/* Still need to lock here in case of spurious irq. */
			write_lock_irq(&hp_sdc.rtq_lock);
H
Helge Deller 已提交
554
			hp_sdc.rcurr = curridx;
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562
			write_unlock_irq(&hp_sdc.rtq_lock);
			hp_sdc_status_out8(postcmd);
			goto finish;
		}
		hp_sdc_status_out8(postcmd);
		goto actdone;
	}

H
Helge Deller 已提交
563 564
 actdone:
	if (act & HP_SDC_ACT_SEMAPHORE)
L
Linus Torvalds 已提交
565
		up(curr->act.semaphore);
H
Helge Deller 已提交
566
	else if (act & HP_SDC_ACT_CALLBACK)
567
		curr->act.irqhook(0,NULL,0,0);
H
Helge Deller 已提交
568

L
Linus Torvalds 已提交
569
	if (curr->idx >= curr->endidx) { /* This transaction is over. */
H
Helge Deller 已提交
570 571
		if (act & HP_SDC_ACT_DEALLOC)
			kfree(curr);
L
Linus Torvalds 已提交
572
		hp_sdc.tq[curridx] = NULL;
H
Helge Deller 已提交
573
	} else {
L
Linus Torvalds 已提交
574 575 576 577 578
		curr->actidx = idx + 1;
		curr->idx = idx + 2;
	}
	/* Interleave outbound data between the transactions. */
	hp_sdc.wcurr++;
H
Helge Deller 已提交
579 580
	if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
		hp_sdc.wcurr = 0;
L
Linus Torvalds 已提交
581 582

 finish:
H
Helge Deller 已提交
583
	/* If by some quirk IBF has cleared and our ISR has run to
L
Linus Torvalds 已提交
584
	   see that that has happened, do it all again. */
H
Helge Deller 已提交
585 586
	if (!hp_sdc.ibf && limit++ < 20)
		goto anew;
L
Linus Torvalds 已提交
587 588

 done:
H
Helge Deller 已提交
589 590
	if (hp_sdc.wcurr >= 0)
		tasklet_schedule(&hp_sdc.task);
L
Linus Torvalds 已提交
591
	write_unlock(&hp_sdc.lock);
H
Helge Deller 已提交
592

L
Linus Torvalds 已提交
593 594 595 596
	return 0;
}

/******* Functions called in either user or kernel context ****/
597
int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
H
Helge Deller 已提交
598
{
L
Linus Torvalds 已提交
599 600 601
	int i;

	if (this == NULL) {
602
		BUG();
L
Linus Torvalds 已提交
603
		return -EINVAL;
H
Helge Deller 已提交
604
	}
L
Linus Torvalds 已提交
605 606

	/* Can't have same transaction on queue twice */
H
Helge Deller 已提交
607 608 609
	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
		if (hp_sdc.tq[i] == this)
			goto fail;
L
Linus Torvalds 已提交
610 611 612 613 614

	this->actidx = 0;
	this->idx = 1;

	/* Search for empty slot */
H
Helge Deller 已提交
615
	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
L
Linus Torvalds 已提交
616 617 618 619 620
		if (hp_sdc.tq[i] == NULL) {
			hp_sdc.tq[i] = this;
			tasklet_schedule(&hp_sdc.task);
			return 0;
		}
H
Helge Deller 已提交
621

L
Linus Torvalds 已提交
622 623 624 625 626 627 628 629
	printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
	return -EBUSY;

 fail:
	printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
	return -EINVAL;
}

630 631 632 633 634 635 636 637 638 639 640
int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
	unsigned long flags;
	int ret;

	write_lock_irqsave(&hp_sdc.lock, flags);
	ret = __hp_sdc_enqueue_transaction(this);
	write_unlock_irqrestore(&hp_sdc.lock,flags);

	return ret;
}

H
Helge Deller 已提交
641 642
int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
{
L
Linus Torvalds 已提交
643 644 645 646 647 648 649
	unsigned long flags;
	int i;

	write_lock_irqsave(&hp_sdc.lock, flags);

	/* TODO: don't remove it if it's not done. */

H
Helge Deller 已提交
650 651 652
	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
		if (hp_sdc.tq[i] == this)
			hp_sdc.tq[i] = NULL;
L
Linus Torvalds 已提交
653 654 655 656 657 658 659 660

	write_unlock_irqrestore(&hp_sdc.lock, flags);
	return 0;
}



/********************** User context functions **************************/
H
Helge Deller 已提交
661 662 663
int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
{
	if (callback == NULL || hp_sdc.dev == NULL)
L
Linus Torvalds 已提交
664
		return -EINVAL;
H
Helge Deller 已提交
665

L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	write_lock_irq(&hp_sdc.hook_lock);
	if (hp_sdc.timer != NULL) {
		write_unlock_irq(&hp_sdc.hook_lock);
		return -EBUSY;
	}

	hp_sdc.timer = callback;
	/* Enable interrupts from the timers */
	hp_sdc.im &= ~HP_SDC_IM_FH;
        hp_sdc.im &= ~HP_SDC_IM_PT;
	hp_sdc.im &= ~HP_SDC_IM_TIMERS;
	hp_sdc.set_im = 1;
	write_unlock_irq(&hp_sdc.hook_lock);

	tasklet_schedule(&hp_sdc.task);

	return 0;
}

H
Helge Deller 已提交
685 686 687
int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
{
	if (callback == NULL || hp_sdc.dev == NULL)
L
Linus Torvalds 已提交
688
		return -EINVAL;
H
Helge Deller 已提交
689

L
Linus Torvalds 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	write_lock_irq(&hp_sdc.hook_lock);
	if (hp_sdc.hil != NULL) {
		write_unlock_irq(&hp_sdc.hook_lock);
		return -EBUSY;
	}

	hp_sdc.hil = callback;
	hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
	hp_sdc.set_im = 1;
	write_unlock_irq(&hp_sdc.hook_lock);

	tasklet_schedule(&hp_sdc.task);

	return 0;
}

H
Helge Deller 已提交
706 707 708
int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
{
	if (callback == NULL || hp_sdc.dev == NULL)
L
Linus Torvalds 已提交
709
		return -EINVAL;
H
Helge Deller 已提交
710

L
Linus Torvalds 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	write_lock_irq(&hp_sdc.hook_lock);
	if (hp_sdc.cooked != NULL) {
		write_unlock_irq(&hp_sdc.hook_lock);
		return -EBUSY;
	}

	/* Enable interrupts from the HIL MLC */
	hp_sdc.cooked = callback;
	hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
	hp_sdc.set_im = 1;
	write_unlock_irq(&hp_sdc.hook_lock);

	tasklet_schedule(&hp_sdc.task);

	return 0;
}

H
Helge Deller 已提交
728 729
int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
{
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	write_lock_irq(&hp_sdc.hook_lock);
	if ((callback != hp_sdc.timer) ||
	    (hp_sdc.timer == NULL)) {
		write_unlock_irq(&hp_sdc.hook_lock);
		return -EINVAL;
	}

	/* Disable interrupts from the timers */
	hp_sdc.timer = NULL;
	hp_sdc.im |= HP_SDC_IM_TIMERS;
	hp_sdc.im |= HP_SDC_IM_FH;
	hp_sdc.im |= HP_SDC_IM_PT;
	hp_sdc.set_im = 1;
	write_unlock_irq(&hp_sdc.hook_lock);
	tasklet_schedule(&hp_sdc.task);

	return 0;
}

H
Helge Deller 已提交
749 750
int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
{
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	write_lock_irq(&hp_sdc.hook_lock);
	if ((callback != hp_sdc.hil) ||
	    (hp_sdc.hil == NULL)) {
		write_unlock_irq(&hp_sdc.hook_lock);
		return -EINVAL;
	}

	hp_sdc.hil = NULL;
	/* Disable interrupts from HIL only if there is no cooked driver. */
	if(hp_sdc.cooked == NULL) {
		hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
		hp_sdc.set_im = 1;
	}
	write_unlock_irq(&hp_sdc.hook_lock);
	tasklet_schedule(&hp_sdc.task);

	return 0;
}

H
Helge Deller 已提交
770 771
int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
{
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
	write_lock_irq(&hp_sdc.hook_lock);
	if ((callback != hp_sdc.cooked) ||
	    (hp_sdc.cooked == NULL)) {
		write_unlock_irq(&hp_sdc.hook_lock);
		return -EINVAL;
	}

	hp_sdc.cooked = NULL;
	/* Disable interrupts from HIL only if there is no raw HIL driver. */
	if(hp_sdc.hil == NULL) {
		hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
		hp_sdc.set_im = 1;
	}
	write_unlock_irq(&hp_sdc.hook_lock);
	tasklet_schedule(&hp_sdc.task);

	return 0;
}

/************************* Keepalive timer task *********************/

H
Helge Deller 已提交
793 794
void hp_sdc_kicker (unsigned long data)
{
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803
	tasklet_schedule(&hp_sdc.task);
	/* Re-insert the periodic task. */
	mod_timer(&hp_sdc.kicker, jiffies + HZ);
}

/************************** Module Initialization ***************************/

#if defined(__hppa__)

804
static const struct parisc_device_id hp_sdc_tbl[] = {
L
Linus Torvalds 已提交
805
	{
H
Helge Deller 已提交
806
		.hw_type =	HPHW_FIO,
L
Linus Torvalds 已提交
807 808
		.hversion_rev =	HVERSION_REV_ANY_ID,
		.hversion =	HVERSION_ANY_ID,
H
Helge Deller 已提交
809
		.sversion =	0x73,
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818
	 },
	{ 0, }
};

MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);

static int __init hp_sdc_init_hppa(struct parisc_device *d);

static struct parisc_driver hp_sdc_driver = {
819
	.name =		"hp_sdc",
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832
	.id_table =	hp_sdc_tbl,
	.probe =	hp_sdc_init_hppa,
};

#endif /* __hppa__ */

static int __init hp_sdc_init(void)
{
	char *errstr;
	hp_sdc_transaction t_sync;
	uint8_t ts_sync[6];
	struct semaphore s_sync;

H
Helge Deller 已提交
833 834 835 836
	rwlock_init(&hp_sdc.lock);
	rwlock_init(&hp_sdc.ibf_lock);
	rwlock_init(&hp_sdc.rtq_lock);
	rwlock_init(&hp_sdc.hook_lock);
L
Linus Torvalds 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850

	hp_sdc.timer		= NULL;
	hp_sdc.hil		= NULL;
	hp_sdc.pup		= NULL;
	hp_sdc.cooked		= NULL;
	hp_sdc.im		= HP_SDC_IM_MASK;  /* Mask maskable irqs */
	hp_sdc.set_im		= 1;
	hp_sdc.wi		= 0xff;
	hp_sdc.r7[0]		= 0xff;
	hp_sdc.r7[1]		= 0xff;
	hp_sdc.r7[2]		= 0xff;
	hp_sdc.r7[3]		= 0xff;
	hp_sdc.ibf		= 1;

H
Helge Deller 已提交
851 852
	memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));

L
Linus Torvalds 已提交
853 854 855 856 857 858 859
	hp_sdc.wcurr		= -1;
        hp_sdc.rcurr		= -1;
	hp_sdc.rqty		= 0;

	hp_sdc.dev_err = -ENODEV;

	errstr = "IO not found for";
H
Helge Deller 已提交
860 861
	if (!hp_sdc.base_io)
		goto err0;
L
Linus Torvalds 已提交
862 863

	errstr = "IRQ not found for";
H
Helge Deller 已提交
864 865
	if (!hp_sdc.irq)
		goto err0;
L
Linus Torvalds 已提交
866 867 868 869 870

	hp_sdc.dev_err = -EBUSY;

#if defined(__hppa__)
	errstr = "IO not available for";
H
Helge Deller 已提交
871 872 873
        if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
		goto err0;
#endif
L
Linus Torvalds 已提交
874 875

	errstr = "IRQ not available for";
876
	if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
H
Helge Deller 已提交
877 878
			"HP SDC", &hp_sdc))
		goto err1;
L
Linus Torvalds 已提交
879 880

	errstr = "NMI not available for";
881
	if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
H
Helge Deller 已提交
882 883
			"HP SDC NMI", &hp_sdc))
		goto err2;
L
Linus Torvalds 已提交
884

H
Helge Deller 已提交
885
	printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
L
Linus Torvalds 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	       (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);

	hp_sdc_status_in8();
	hp_sdc_data_in8();

	tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);

	/* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
	t_sync.actidx	= 0;
	t_sync.idx	= 1;
	t_sync.endidx	= 6;
	t_sync.seq	= ts_sync;
	ts_sync[0]	= HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
	ts_sync[1]	= 0x0f;
	ts_sync[2] = ts_sync[3]	= ts_sync[4] = ts_sync[5] = 0;
	t_sync.act.semaphore = &s_sync;
	init_MUTEX_LOCKED(&s_sync);
	hp_sdc_enqueue_transaction(&t_sync);
	down(&s_sync); /* Wait for t_sync to complete */

	/* Create the keepalive task */
	init_timer(&hp_sdc.kicker);
	hp_sdc.kicker.expires = jiffies + HZ;
	hp_sdc.kicker.function = &hp_sdc_kicker;
	add_timer(&hp_sdc.kicker);

	hp_sdc.dev_err = 0;
	return 0;
 err2:
915
	free_irq(hp_sdc.irq, &hp_sdc);
L
Linus Torvalds 已提交
916 917 918
 err1:
	release_region(hp_sdc.data_io, 2);
 err0:
H
Helge Deller 已提交
919
	printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
L
Linus Torvalds 已提交
920 921
		errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
	hp_sdc.dev = NULL;
H
Helge Deller 已提交
922

L
Linus Torvalds 已提交
923 924 925 926 927 928 929
	return hp_sdc.dev_err;
}

#if defined(__hppa__)

static int __init hp_sdc_init_hppa(struct parisc_device *d)
{
H
Helge Deller 已提交
930 931 932 933
	if (!d)
		return 1;
	if (hp_sdc.dev != NULL)
		return 1;	/* We only expect one SDC */
L
Linus Torvalds 已提交
934 935 936 937

	hp_sdc.dev		= d;
	hp_sdc.irq		= d->irq;
	hp_sdc.nmi		= d->aux_irq;
938 939 940
	hp_sdc.base_io		= d->hpa.start;
	hp_sdc.data_io		= d->hpa.start + 0x800;
	hp_sdc.status_io	= d->hpa.start + 0x801;
L
Linus Torvalds 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

	return hp_sdc_init();
}

#endif /* __hppa__ */

static void hp_sdc_exit(void)
{
	write_lock_irq(&hp_sdc.lock);

	/* Turn off all maskable "sub-function" irq's. */
	hp_sdc_spin_ibf();
	sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);

	/* Wait until we know this has been processed by the i8042 */
	hp_sdc_spin_ibf();

958 959
	free_irq(hp_sdc.nmi, &hp_sdc);
	free_irq(hp_sdc.irq, &hp_sdc);
L
Linus Torvalds 已提交
960 961 962 963 964 965 966
	write_unlock_irq(&hp_sdc.lock);

	del_timer(&hp_sdc.kicker);

	tasklet_kill(&hp_sdc.task);

#if defined(__hppa__)
H
Helge Deller 已提交
967
	if (unregister_parisc_driver(&hp_sdc_driver))
L
Linus Torvalds 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980
		printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
#endif
}

static int __init hp_sdc_register(void)
{
	hp_sdc_transaction tq_init;
	uint8_t tq_init_seq[5];
	struct semaphore tq_init_sem;
#if defined(__mc68000__)
	mm_segment_t fs;
	unsigned char i;
#endif
H
Helge Deller 已提交
981

L
Linus Torvalds 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	hp_sdc.dev = NULL;
	hp_sdc.dev_err = 0;
#if defined(__hppa__)
	if (register_parisc_driver(&hp_sdc_driver)) {
		printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
		return -ENODEV;
	}
#elif defined(__mc68000__)
	if (!MACH_IS_HP300)
	    return -ENODEV;

	hp_sdc.irq	 = 1;
	hp_sdc.nmi	 = 7;
	hp_sdc.base_io	 = (unsigned long) 0xf0428000;
	hp_sdc.data_io	 = (unsigned long) hp_sdc.base_io + 1;
	hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
	fs = get_fs();
	set_fs(KERNEL_DS);
	if (!get_user(i, (unsigned char *)hp_sdc.data_io))
		hp_sdc.dev = (void *)1;
	set_fs(fs);
	hp_sdc.dev_err   = hp_sdc_init();
#endif
	if (hp_sdc.dev == NULL) {
		printk(KERN_WARNING PREFIX "No SDC found.\n");
		return hp_sdc.dev_err;
	}

	init_MUTEX_LOCKED(&tq_init_sem);

	tq_init.actidx		= 0;
	tq_init.idx		= 1;
	tq_init.endidx		= 5;
	tq_init.seq		= tq_init_seq;
	tq_init.act.semaphore	= &tq_init_sem;

H
Helge Deller 已提交
1018 1019
	tq_init_seq[0] =
		HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
	tq_init_seq[2] = 1;
	tq_init_seq[3] = 0;
	tq_init_seq[4] = 0;

	hp_sdc_enqueue_transaction(&tq_init);

	down(&tq_init_sem);
	up(&tq_init_sem);

	if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
		printk(KERN_WARNING PREFIX "Error reading config byte.\n");
		hp_sdc_exit();
		return -ENODEV;
	}
	hp_sdc.r11 = tq_init_seq[4];
	if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
H
Helge Deller 已提交
1037
		const char *str;
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042
		printk(KERN_INFO PREFIX "New style SDC\n");
		tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
		tq_init.actidx		= 0;
		tq_init.idx		= 1;
		down(&tq_init_sem);
H
Helge Deller 已提交
1043
		hp_sdc_enqueue_transaction(&tq_init);
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052
		down(&tq_init_sem);
		up(&tq_init_sem);
		if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
			printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
			return -ENODEV;
		}
		hp_sdc.r7e = tq_init_seq[4];
		HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
		printk(KERN_INFO PREFIX "Revision: %s\n", str);
H
Helge Deller 已提交
1053
		if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
L
Linus Torvalds 已提交
1054
			printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
H
Helge Deller 已提交
1055
		if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
L
Linus Torvalds 已提交
1056 1057 1058
			printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
		printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
		       "on next firmware reset.\n");
H
Helge Deller 已提交
1059
		tq_init_seq[0] = HP_SDC_ACT_PRECMD |
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065 1066 1067
			HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
		tq_init_seq[1] = HP_SDC_CMD_SET_STR;
		tq_init_seq[2] = 1;
		tq_init_seq[3] = 0;
		tq_init.actidx		= 0;
		tq_init.idx		= 1;
		tq_init.endidx		= 4;
		down(&tq_init_sem);
H
Helge Deller 已提交
1068
		hp_sdc_enqueue_transaction(&tq_init);
L
Linus Torvalds 已提交
1069 1070
		down(&tq_init_sem);
		up(&tq_init_sem);
H
Helge Deller 已提交
1071 1072
	} else
		printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
L
Linus Torvalds 已提交
1073 1074 1075 1076 1077 1078 1079 1080
		       (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");

        return 0;
}

module_init(hp_sdc_register);
module_exit(hp_sdc_exit);

H
Helge Deller 已提交
1081
/* Timing notes:  These measurements taken on my 64MHz 7100-LC (715/64)
L
Linus Torvalds 已提交
1082 1083 1084 1085 1086
 *                                              cycles cycles-adj    time
 * between two consecutive mfctl(16)'s:              4        n/a    63ns
 * hp_sdc_spin_ibf when idle:                      119        115   1.7us
 * gsc_writeb status register:                      83         79   1.2us
 * IBF to clear after sending SET_IM:             6204       6006    93us
H
Helge Deller 已提交
1087
 * IBF to clear after sending LOAD_RT:            4467       4352    68us
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
 * IBF to clear after sending two LOAD_RTs:      18974      18859   295us
 * READ_T1, read status/data, IRQ, call handler: 35564        n/a   556us
 * cmd to ~IBF READ_T1 2nd time right after:   5158403        n/a    81ms
 * between IRQ received and ~IBF for above:    2578877        n/a    40ms
 *
 * Performance stats after a run of this module configuring HIL and
 * receiving a few mouse events:
 *
 * status in8  282508 cycles 7128 calls
 * status out8   8404 cycles  341 calls
 * data out8     1734 cycles   78 calls
 * isr         174324 cycles  617 calls (includes take)
 * take          1241 cycles    2 calls
 * put        1411504 cycles 6937 calls
 * task       1655209 cycles 6937 calls (includes put)
 *
 */