xref: /openbmc/linux/drivers/input/serio/hp_sdc.c (revision 305e342bba16730a0821131af6d7d0df43bfff4c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * HP i8042-based System Device Controller driver.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2001 Brian S. Julin
51da177e4SLinus Torvalds  * All rights reserved.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Redistribution and use in source and binary forms, with or without
81da177e4SLinus Torvalds  * modification, are permitted provided that the following conditions
91da177e4SLinus Torvalds  * are met:
101da177e4SLinus Torvalds  * 1. Redistributions of source code must retain the above copyright
111da177e4SLinus Torvalds  *    notice, this list of conditions, and the following disclaimer,
121da177e4SLinus Torvalds  *    without modification.
131da177e4SLinus Torvalds  * 2. The name of the author may not be used to endorse or promote products
141da177e4SLinus Torvalds  *    derived from this software without specific prior written permission.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * Alternatively, this software may be distributed under the terms of the
171da177e4SLinus Torvalds  * GNU General Public License ("GPL").
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
201da177e4SLinus Torvalds  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211da177e4SLinus Torvalds  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221da177e4SLinus Torvalds  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
231da177e4SLinus Torvalds  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241da177e4SLinus Torvalds  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251da177e4SLinus Torvalds  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261da177e4SLinus Torvalds  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271da177e4SLinus Torvalds  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * References:
301da177e4SLinus Torvalds  * System Device Controller Microprocessor Firmware Theory of Operation
311da177e4SLinus Torvalds  *      for Part Number 1820-4784 Revision B.  Dwg No. A-1820-4784-2
321da177e4SLinus Torvalds  * Helge Deller's original hilkbd.c port for PA-RISC.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  * Driver theory of operation:
361da177e4SLinus Torvalds  *
371da177e4SLinus Torvalds  * hp_sdc_put does all writing to the SDC.  ISR can run on a different
381da177e4SLinus Torvalds  * CPU than hp_sdc_put, but only one CPU runs hp_sdc_put at a time
391da177e4SLinus Torvalds  * (it cannot really benefit from SMP anyway.)  A tasket fit this perfectly.
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  * All data coming back from the SDC is sent via interrupt and can be read
421da177e4SLinus Torvalds  * fully in the ISR, so there are no latency/throughput problems there.
431da177e4SLinus Torvalds  * The problem is with output, due to the slow clock speed of the SDC
441da177e4SLinus Torvalds  * compared to the CPU.  This should not be too horrible most of the time,
451da177e4SLinus Torvalds  * but if used with HIL devices that support the multibyte transfer command,
461da177e4SLinus Torvalds  * keeping outbound throughput flowing at the 6500KBps that the HIL is
471da177e4SLinus Torvalds  * capable of is more than can be done at HZ=100.
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * Busy polling for IBF clear wastes CPU cycles and bus cycles.  hp_sdc.ibf
501da177e4SLinus Torvalds  * is set to 0 when the IBF flag in the status register has cleared.  ISR
511da177e4SLinus Torvalds  * may do this, and may also access the parts of queued transactions related
521da177e4SLinus Torvalds  * to reading data back from the SDC, but otherwise will not touch the
531da177e4SLinus Torvalds  * hp_sdc state. Whenever a register is written hp_sdc.ibf is set to 1.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * The i8042 write index and the values in the 4-byte input buffer
561da177e4SLinus Torvalds  * starting at 0x70 are kept track of in hp_sdc.wi, and .r7[], respectively,
571da177e4SLinus Torvalds  * to minimize the amount of IO needed to the SDC.  However these values
581da177e4SLinus Torvalds  * do not need to be locked since they are only ever accessed by hp_sdc_put.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * A timer task schedules the tasklet once per second just to make
611da177e4SLinus Torvalds  * sure it doesn't freeze up and to allow for bad reads to time out.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #include <linux/hp_sdc.h>
651da177e4SLinus Torvalds #include <linux/errno.h>
661da177e4SLinus Torvalds #include <linux/init.h>
671da177e4SLinus Torvalds #include <linux/module.h>
681da177e4SLinus Torvalds #include <linux/ioport.h>
691da177e4SLinus Torvalds #include <linux/time.h>
701da177e4SLinus Torvalds #include <linux/slab.h>
711da177e4SLinus Torvalds #include <linux/hil.h>
724933d075SGeert Uytterhoeven #include <linux/semaphore.h>
731da177e4SLinus Torvalds #include <asm/io.h>
741da177e4SLinus Torvalds #include <asm/system.h>
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* Machine-specific abstraction */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #if defined(__hppa__)
791da177e4SLinus Torvalds # include <asm/parisc-device.h>
801da177e4SLinus Torvalds # define sdc_readb(p)		gsc_readb(p)
811da177e4SLinus Torvalds # define sdc_writeb(v,p)	gsc_writeb((v),(p))
821da177e4SLinus Torvalds #elif defined(__mc68000__)
831da177e4SLinus Torvalds # include <asm/uaccess.h>
841da177e4SLinus Torvalds # define sdc_readb(p)		in_8(p)
851da177e4SLinus Torvalds # define sdc_writeb(v,p)	out_8((p),(v))
861da177e4SLinus Torvalds #else
871da177e4SLinus Torvalds # error "HIL is not supported on this platform"
881da177e4SLinus Torvalds #endif
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define PREFIX "HP SDC: "
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
931da177e4SLinus Torvalds MODULE_DESCRIPTION("HP i8042-based SDC Driver");
941da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_request_timer_irq);
971da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_request_hil_irq);
981da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_request_cooked_irq);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_release_timer_irq);
1011da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_release_hil_irq);
1021da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_release_cooked_irq);
1031da177e4SLinus Torvalds 
1049575499dSHelge Deller EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
1051da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
1061da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_dequeue_transaction);
1071da177e4SLinus Torvalds 
108*305e342bSHelge Deller static unsigned int hp_sdc_disabled;
109*305e342bSHelge Deller module_param_named(no_hpsdc, hp_sdc_disabled, bool, 0);
110*305e342bSHelge Deller MODULE_PARM_DESC(no_hpsdc, "Do not enable HP SDC driver.");
111*305e342bSHelge Deller 
1121da177e4SLinus Torvalds static hp_i8042_sdc	hp_sdc;	/* All driver state is kept in here. */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*************** primitives for use in any context *********************/
115ffd51f46SHelge Deller static inline uint8_t hp_sdc_status_in8(void)
116ffd51f46SHelge Deller {
1171da177e4SLinus Torvalds 	uint8_t status;
1181da177e4SLinus Torvalds 	unsigned long flags;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
1211da177e4SLinus Torvalds 	status = sdc_readb(hp_sdc.status_io);
122ffd51f46SHelge Deller 	if (!(status & HP_SDC_STATUS_IBF))
123ffd51f46SHelge Deller 		hp_sdc.ibf = 0;
1241da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	return status;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129ffd51f46SHelge Deller static inline uint8_t hp_sdc_data_in8(void)
130ffd51f46SHelge Deller {
1311da177e4SLinus Torvalds 	return sdc_readb(hp_sdc.data_io);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
134ffd51f46SHelge Deller static inline void hp_sdc_status_out8(uint8_t val)
135ffd51f46SHelge Deller {
1361da177e4SLinus Torvalds 	unsigned long flags;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
1391da177e4SLinus Torvalds 	hp_sdc.ibf = 1;
140ffd51f46SHelge Deller 	if ((val & 0xf0) == 0xe0)
141ffd51f46SHelge Deller 		hp_sdc.wi = 0xff;
1421da177e4SLinus Torvalds 	sdc_writeb(val, hp_sdc.status_io);
1431da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
146ffd51f46SHelge Deller static inline void hp_sdc_data_out8(uint8_t val)
147ffd51f46SHelge Deller {
1481da177e4SLinus Torvalds 	unsigned long flags;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
1511da177e4SLinus Torvalds 	hp_sdc.ibf = 1;
1521da177e4SLinus Torvalds 	sdc_writeb(val, hp_sdc.data_io);
1531da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /*	Care must be taken to only invoke hp_sdc_spin_ibf when
1571da177e4SLinus Torvalds  *	absolutely needed, or in rarely invoked subroutines.
1581da177e4SLinus Torvalds  *	Not only does it waste CPU cycles, it also wastes bus cycles.
1591da177e4SLinus Torvalds  */
160ffd51f46SHelge Deller static inline void hp_sdc_spin_ibf(void)
161ffd51f46SHelge Deller {
1621da177e4SLinus Torvalds 	unsigned long flags;
1631da177e4SLinus Torvalds 	rwlock_t *lock;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	lock = &hp_sdc.ibf_lock;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	read_lock_irqsave(lock, flags);
1681da177e4SLinus Torvalds 	if (!hp_sdc.ibf) {
1691da177e4SLinus Torvalds 		read_unlock_irqrestore(lock, flags);
1701da177e4SLinus Torvalds 		return;
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 	read_unlock(lock);
1731da177e4SLinus Torvalds 	write_lock(lock);
174ffd51f46SHelge Deller 	while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
175ffd51f46SHelge Deller 		{ }
1761da177e4SLinus Torvalds 	hp_sdc.ibf = 0;
1771da177e4SLinus Torvalds 	write_unlock_irqrestore(lock, flags);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /************************ Interrupt context functions ************************/
182ffd51f46SHelge Deller static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
183ffd51f46SHelge Deller {
1841da177e4SLinus Torvalds 	hp_sdc_transaction *curr;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	read_lock(&hp_sdc.rtq_lock);
1871da177e4SLinus Torvalds 	if (hp_sdc.rcurr < 0) {
1881da177e4SLinus Torvalds 		read_unlock(&hp_sdc.rtq_lock);
1891da177e4SLinus Torvalds 		return;
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 	curr = hp_sdc.tq[hp_sdc.rcurr];
1921da177e4SLinus Torvalds 	read_unlock(&hp_sdc.rtq_lock);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	curr->seq[curr->idx++] = status;
1951da177e4SLinus Torvalds 	curr->seq[curr->idx++] = data;
1961da177e4SLinus Torvalds 	hp_sdc.rqty -= 2;
1971da177e4SLinus Torvalds 	do_gettimeofday(&hp_sdc.rtv);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (hp_sdc.rqty <= 0) {
2001da177e4SLinus Torvalds 		/* All data has been gathered. */
201ffd51f46SHelge Deller 		if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
202ffd51f46SHelge Deller 			if (curr->act.semaphore)
203ffd51f46SHelge Deller 				up(curr->act.semaphore);
204ffd51f46SHelge Deller 
205ffd51f46SHelge Deller 		if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
2061da177e4SLinus Torvalds 			if (curr->act.irqhook)
2071da177e4SLinus Torvalds 				curr->act.irqhook(irq, dev_id, status, data);
208ffd51f46SHelge Deller 
2091da177e4SLinus Torvalds 		curr->actidx = curr->idx;
2101da177e4SLinus Torvalds 		curr->idx++;
2111da177e4SLinus Torvalds 		/* Return control of this transaction */
2121da177e4SLinus Torvalds 		write_lock(&hp_sdc.rtq_lock);
2131da177e4SLinus Torvalds 		hp_sdc.rcurr = -1;
2141da177e4SLinus Torvalds 		hp_sdc.rqty = 0;
2151da177e4SLinus Torvalds 		write_unlock(&hp_sdc.rtq_lock);
2161da177e4SLinus Torvalds 		tasklet_schedule(&hp_sdc.task);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220ffd51f46SHelge Deller static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
221ffd51f46SHelge Deller {
2221da177e4SLinus Torvalds 	uint8_t status, data;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	status = hp_sdc_status_in8();
2251da177e4SLinus Torvalds 	/* Read data unconditionally to advance i8042. */
2261da177e4SLinus Torvalds 	data =   hp_sdc_data_in8();
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* For now we are ignoring these until we get the SDC to behave. */
229ffd51f46SHelge Deller 	if (((status & 0xf1) == 0x51) && data == 0x82)
2301da177e4SLinus Torvalds 		return IRQ_HANDLED;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	switch (status & HP_SDC_STATUS_IRQMASK) {
2331da177e4SLinus Torvalds 	case 0: /* This case is not documented. */
2341da177e4SLinus Torvalds 		break;
235ffd51f46SHelge Deller 
2361da177e4SLinus Torvalds 	case HP_SDC_STATUS_USERTIMER:
2371da177e4SLinus Torvalds 	case HP_SDC_STATUS_PERIODIC:
2381da177e4SLinus Torvalds 	case HP_SDC_STATUS_TIMER:
2391da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2401da177e4SLinus Torvalds 		if (hp_sdc.timer != NULL)
2411da177e4SLinus Torvalds 			hp_sdc.timer(irq, dev_id, status, data);
2421da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2431da177e4SLinus Torvalds 		break;
244ffd51f46SHelge Deller 
2451da177e4SLinus Torvalds 	case HP_SDC_STATUS_REG:
2461da177e4SLinus Torvalds 		hp_sdc_take(irq, dev_id, status, data);
2471da177e4SLinus Torvalds 		break;
248ffd51f46SHelge Deller 
2491da177e4SLinus Torvalds 	case HP_SDC_STATUS_HILCMD:
2501da177e4SLinus Torvalds 	case HP_SDC_STATUS_HILDATA:
2511da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2521da177e4SLinus Torvalds 		if (hp_sdc.hil != NULL)
2531da177e4SLinus Torvalds 			hp_sdc.hil(irq, dev_id, status, data);
2541da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2551da177e4SLinus Torvalds 		break;
256ffd51f46SHelge Deller 
2571da177e4SLinus Torvalds 	case HP_SDC_STATUS_PUP:
2581da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2591da177e4SLinus Torvalds 		if (hp_sdc.pup != NULL)
2601da177e4SLinus Torvalds 			hp_sdc.pup(irq, dev_id, status, data);
261ffd51f46SHelge Deller 		else
262ffd51f46SHelge Deller 			printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
2631da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2641da177e4SLinus Torvalds 		break;
265ffd51f46SHelge Deller 
2661da177e4SLinus Torvalds 	default:
2671da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2681da177e4SLinus Torvalds 		if (hp_sdc.cooked != NULL)
2691da177e4SLinus Torvalds 			hp_sdc.cooked(irq, dev_id, status, data);
2701da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2711da177e4SLinus Torvalds 		break;
2721da177e4SLinus Torvalds 	}
273ffd51f46SHelge Deller 
2741da177e4SLinus Torvalds 	return IRQ_HANDLED;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 
278ffd51f46SHelge Deller static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
279ffd51f46SHelge Deller {
2801da177e4SLinus Torvalds 	int status;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	status = hp_sdc_status_in8();
2831da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX "NMI !\n");
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds #if 0
2861da177e4SLinus Torvalds 	if (status & HP_SDC_NMISTATUS_FHS) {
2871da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2881da177e4SLinus Torvalds 		if (hp_sdc.timer != NULL)
2891da177e4SLinus Torvalds 			hp_sdc.timer(irq, dev_id, status, 0);
2901da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
291ffd51f46SHelge Deller 	} else {
2921da177e4SLinus Torvalds 		/* TODO: pass this on to the HIL handler, or do SAK here? */
2931da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "HIL NMI\n");
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds #endif
296ffd51f46SHelge Deller 
2971da177e4SLinus Torvalds 	return IRQ_HANDLED;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds /***************** Kernel (tasklet) context functions ****************/
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds unsigned long hp_sdc_put(void);
3041da177e4SLinus Torvalds 
305ffd51f46SHelge Deller static void hp_sdc_tasklet(unsigned long foo)
306ffd51f46SHelge Deller {
3071da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.rtq_lock);
308ffd51f46SHelge Deller 
3091da177e4SLinus Torvalds 	if (hp_sdc.rcurr >= 0) {
3101da177e4SLinus Torvalds 		struct timeval tv;
311ffd51f46SHelge Deller 
3121da177e4SLinus Torvalds 		do_gettimeofday(&tv);
313ffd51f46SHelge Deller 		if (tv.tv_sec > hp_sdc.rtv.tv_sec)
314ffd51f46SHelge Deller 			tv.tv_usec += USEC_PER_SEC;
315ffd51f46SHelge Deller 
3161da177e4SLinus Torvalds 		if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
3171da177e4SLinus Torvalds 			hp_sdc_transaction *curr;
3181da177e4SLinus Torvalds 			uint8_t tmp;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 			curr = hp_sdc.tq[hp_sdc.rcurr];
3211da177e4SLinus Torvalds 			/* If this turns out to be a normal failure mode
3221da177e4SLinus Torvalds 			 * we'll need to figure out a way to communicate
3231da177e4SLinus Torvalds 			 * it back to the application. and be less verbose.
3241da177e4SLinus Torvalds 			 */
3251da177e4SLinus Torvalds 			printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
3261da177e4SLinus Torvalds 			       tv.tv_usec - hp_sdc.rtv.tv_usec);
3271da177e4SLinus Torvalds 			curr->idx += hp_sdc.rqty;
3281da177e4SLinus Torvalds 			hp_sdc.rqty = 0;
3291da177e4SLinus Torvalds 			tmp = curr->seq[curr->actidx];
3301da177e4SLinus Torvalds 			curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
331ffd51f46SHelge Deller 			if (tmp & HP_SDC_ACT_SEMAPHORE)
3321da177e4SLinus Torvalds 				if (curr->act.semaphore)
3331da177e4SLinus Torvalds 					up(curr->act.semaphore);
334ffd51f46SHelge Deller 
3351da177e4SLinus Torvalds 			if (tmp & HP_SDC_ACT_CALLBACK) {
3361da177e4SLinus Torvalds 				/* Note this means that irqhooks may be called
3371da177e4SLinus Torvalds 				 * in tasklet/bh context.
3381da177e4SLinus Torvalds 				 */
3391da177e4SLinus Torvalds 				if (curr->act.irqhook)
3406ce6b3aeSAl Viro 					curr->act.irqhook(0, NULL, 0, 0);
3411da177e4SLinus Torvalds 			}
342ffd51f46SHelge Deller 
3431da177e4SLinus Torvalds 			curr->actidx = curr->idx;
3441da177e4SLinus Torvalds 			curr->idx++;
3451da177e4SLinus Torvalds 			hp_sdc.rcurr = -1;
3461da177e4SLinus Torvalds 		}
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.rtq_lock);
3491da177e4SLinus Torvalds 	hp_sdc_put();
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352ffd51f46SHelge Deller unsigned long hp_sdc_put(void)
353ffd51f46SHelge Deller {
3541da177e4SLinus Torvalds 	hp_sdc_transaction *curr;
3551da177e4SLinus Torvalds 	uint8_t act;
3561da177e4SLinus Torvalds 	int idx, curridx;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	int limit = 0;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	write_lock(&hp_sdc.lock);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* If i8042 buffers are full, we cannot do anything that
3631da177e4SLinus Torvalds 	   requires output, so we skip to the administrativa. */
3641da177e4SLinus Torvalds 	if (hp_sdc.ibf) {
3651da177e4SLinus Torvalds 		hp_sdc_status_in8();
366ffd51f46SHelge Deller 		if (hp_sdc.ibf)
367ffd51f46SHelge Deller 			goto finish;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds  anew:
3711da177e4SLinus Torvalds 	/* See if we are in the middle of a sequence. */
372ffd51f46SHelge Deller 	if (hp_sdc.wcurr < 0)
373ffd51f46SHelge Deller 		hp_sdc.wcurr = 0;
3741da177e4SLinus Torvalds 	read_lock_irq(&hp_sdc.rtq_lock);
375ffd51f46SHelge Deller 	if (hp_sdc.rcurr == hp_sdc.wcurr)
376ffd51f46SHelge Deller 		hp_sdc.wcurr++;
3771da177e4SLinus Torvalds 	read_unlock_irq(&hp_sdc.rtq_lock);
378ffd51f46SHelge Deller 	if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
379ffd51f46SHelge Deller 		hp_sdc.wcurr = 0;
3801da177e4SLinus Torvalds 	curridx = hp_sdc.wcurr;
3811da177e4SLinus Torvalds 
382ffd51f46SHelge Deller 	if (hp_sdc.tq[curridx] != NULL)
383ffd51f46SHelge Deller 		goto start;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	while (++curridx != hp_sdc.wcurr) {
3861da177e4SLinus Torvalds 		if (curridx >= HP_SDC_QUEUE_LEN) {
3871da177e4SLinus Torvalds 			curridx = -1; /* Wrap to top */
3881da177e4SLinus Torvalds 			continue;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 		read_lock_irq(&hp_sdc.rtq_lock);
3911da177e4SLinus Torvalds 		if (hp_sdc.rcurr == curridx) {
3921da177e4SLinus Torvalds 			read_unlock_irq(&hp_sdc.rtq_lock);
3931da177e4SLinus Torvalds 			continue;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 		read_unlock_irq(&hp_sdc.rtq_lock);
396ffd51f46SHelge Deller 		if (hp_sdc.tq[curridx] != NULL)
397ffd51f46SHelge Deller 			break; /* Found one. */
3981da177e4SLinus Torvalds 	}
3991da177e4SLinus Torvalds 	if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
4001da177e4SLinus Torvalds 		curridx = -1;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	hp_sdc.wcurr = curridx;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds  start:
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	/* Check to see if the interrupt mask needs to be set. */
4071da177e4SLinus Torvalds 	if (hp_sdc.set_im) {
4081da177e4SLinus Torvalds 		hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
4091da177e4SLinus Torvalds 		hp_sdc.set_im = 0;
4101da177e4SLinus Torvalds 		goto finish;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
413ffd51f46SHelge Deller 	if (hp_sdc.wcurr == -1)
414ffd51f46SHelge Deller 		goto done;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	curr = hp_sdc.tq[curridx];
4171da177e4SLinus Torvalds 	idx = curr->actidx;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (curr->actidx >= curr->endidx) {
4201da177e4SLinus Torvalds 		hp_sdc.tq[curridx] = NULL;
4211da177e4SLinus Torvalds 		/* Interleave outbound data between the transactions. */
4221da177e4SLinus Torvalds 		hp_sdc.wcurr++;
423ffd51f46SHelge Deller 		if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
424ffd51f46SHelge Deller 			hp_sdc.wcurr = 0;
4251da177e4SLinus Torvalds 		goto finish;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	act = curr->seq[idx];
4291da177e4SLinus Torvalds 	idx++;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	if (curr->idx >= curr->endidx) {
432ffd51f46SHelge Deller 		if (act & HP_SDC_ACT_DEALLOC)
433ffd51f46SHelge Deller 			kfree(curr);
4341da177e4SLinus Torvalds 		hp_sdc.tq[curridx] = NULL;
4351da177e4SLinus Torvalds 		/* Interleave outbound data between the transactions. */
4361da177e4SLinus Torvalds 		hp_sdc.wcurr++;
437ffd51f46SHelge Deller 		if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
438ffd51f46SHelge Deller 			hp_sdc.wcurr = 0;
4391da177e4SLinus Torvalds 		goto finish;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	while (act & HP_SDC_ACT_PRECMD) {
4431da177e4SLinus Torvalds 		if (curr->idx != idx) {
4441da177e4SLinus Torvalds 			idx++;
4451da177e4SLinus Torvalds 			act &= ~HP_SDC_ACT_PRECMD;
4461da177e4SLinus Torvalds 			break;
4471da177e4SLinus Torvalds 		}
4481da177e4SLinus Torvalds 		hp_sdc_status_out8(curr->seq[idx]);
4491da177e4SLinus Torvalds 		curr->idx++;
4501da177e4SLinus Torvalds 		/* act finished? */
4511da177e4SLinus Torvalds 		if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
4521da177e4SLinus Torvalds 			goto actdone;
4531da177e4SLinus Torvalds 		/* skip quantity field if data-out sequence follows. */
454ffd51f46SHelge Deller 		if (act & HP_SDC_ACT_DATAOUT)
455ffd51f46SHelge Deller 			curr->idx++;
4561da177e4SLinus Torvalds 		goto finish;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 	if (act & HP_SDC_ACT_DATAOUT) {
4591da177e4SLinus Torvalds 		int qty;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 		qty = curr->seq[idx];
4621da177e4SLinus Torvalds 		idx++;
4631da177e4SLinus Torvalds 		if (curr->idx - idx < qty) {
4641da177e4SLinus Torvalds 			hp_sdc_data_out8(curr->seq[curr->idx]);
4651da177e4SLinus Torvalds 			curr->idx++;
4661da177e4SLinus Torvalds 			/* act finished? */
467ffd51f46SHelge Deller 			if (curr->idx - idx >= qty &&
468ffd51f46SHelge Deller 			    (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
4691da177e4SLinus Torvalds 				goto actdone;
4701da177e4SLinus Torvalds 			goto finish;
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 		idx += qty;
4731da177e4SLinus Torvalds 		act &= ~HP_SDC_ACT_DATAOUT;
474ffd51f46SHelge Deller 	} else
475ffd51f46SHelge Deller 	    while (act & HP_SDC_ACT_DATAREG) {
4761da177e4SLinus Torvalds 		int mask;
4771da177e4SLinus Torvalds 		uint8_t w7[4];
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 		mask = curr->seq[idx];
4801da177e4SLinus Torvalds 		if (idx != curr->idx) {
4811da177e4SLinus Torvalds 			idx++;
4821da177e4SLinus Torvalds 			idx += !!(mask & 1);
4831da177e4SLinus Torvalds 			idx += !!(mask & 2);
4841da177e4SLinus Torvalds 			idx += !!(mask & 4);
4851da177e4SLinus Torvalds 			idx += !!(mask & 8);
4861da177e4SLinus Torvalds 			act &= ~HP_SDC_ACT_DATAREG;
4871da177e4SLinus Torvalds 			break;
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 		w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
4911da177e4SLinus Torvalds 		w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
4921da177e4SLinus Torvalds 		w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
4931da177e4SLinus Torvalds 		w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 		if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
4961da177e4SLinus Torvalds 		    w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
4971da177e4SLinus Torvalds 			int i = 0;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 			/* Need to point the write index register */
500ffd51f46SHelge Deller 			while (i < 4 && w7[i] == hp_sdc.r7[i])
501ffd51f46SHelge Deller 				i++;
502ffd51f46SHelge Deller 
5031da177e4SLinus Torvalds 			if (i < 4) {
5041da177e4SLinus Torvalds 				hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
5051da177e4SLinus Torvalds 				hp_sdc.wi = 0x70 + i;
5061da177e4SLinus Torvalds 				goto finish;
5071da177e4SLinus Torvalds 			}
508ffd51f46SHelge Deller 
5091da177e4SLinus Torvalds 			idx++;
5101da177e4SLinus Torvalds 			if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
5111da177e4SLinus Torvalds 				goto actdone;
512ffd51f46SHelge Deller 
5131da177e4SLinus Torvalds 			curr->idx = idx;
5141da177e4SLinus Torvalds 			act &= ~HP_SDC_ACT_DATAREG;
5151da177e4SLinus Torvalds 			break;
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 		hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
5191da177e4SLinus Torvalds 		hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
5201da177e4SLinus Torvalds 		hp_sdc.wi++; /* write index register autoincrements */
5211da177e4SLinus Torvalds 		{
5221da177e4SLinus Torvalds 			int i = 0;
5231da177e4SLinus Torvalds 
524ffd51f46SHelge Deller 			while ((i < 4) && w7[i] == hp_sdc.r7[i])
525ffd51f46SHelge Deller 				i++;
5261da177e4SLinus Torvalds 			if (i >= 4) {
5271da177e4SLinus Torvalds 				curr->idx = idx + 1;
5281da177e4SLinus Torvalds 				if ((act & HP_SDC_ACT_DURING) ==
5291da177e4SLinus Torvalds 				    HP_SDC_ACT_DATAREG)
5301da177e4SLinus Torvalds 					goto actdone;
5311da177e4SLinus Torvalds 			}
5321da177e4SLinus Torvalds 		}
5331da177e4SLinus Torvalds 		goto finish;
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 	/* We don't go any further in the command if there is a pending read,
5361da177e4SLinus Torvalds 	   because we don't want interleaved results. */
5371da177e4SLinus Torvalds 	read_lock_irq(&hp_sdc.rtq_lock);
5381da177e4SLinus Torvalds 	if (hp_sdc.rcurr >= 0) {
5391da177e4SLinus Torvalds 		read_unlock_irq(&hp_sdc.rtq_lock);
5401da177e4SLinus Torvalds 		goto finish;
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 	read_unlock_irq(&hp_sdc.rtq_lock);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	if (act & HP_SDC_ACT_POSTCMD) {
5461da177e4SLinus Torvalds 		uint8_t postcmd;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 		/* curr->idx should == idx at this point. */
5491da177e4SLinus Torvalds 		postcmd = curr->seq[idx];
5501da177e4SLinus Torvalds 		curr->idx++;
5511da177e4SLinus Torvalds 		if (act & HP_SDC_ACT_DATAIN) {
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 			/* Start a new read */
5541da177e4SLinus Torvalds 			hp_sdc.rqty = curr->seq[curr->idx];
5551da177e4SLinus Torvalds 			do_gettimeofday(&hp_sdc.rtv);
5561da177e4SLinus Torvalds 			curr->idx++;
5571da177e4SLinus Torvalds 			/* Still need to lock here in case of spurious irq. */
5581da177e4SLinus Torvalds 			write_lock_irq(&hp_sdc.rtq_lock);
5591da177e4SLinus Torvalds 			hp_sdc.rcurr = curridx;
5601da177e4SLinus Torvalds 			write_unlock_irq(&hp_sdc.rtq_lock);
5611da177e4SLinus Torvalds 			hp_sdc_status_out8(postcmd);
5621da177e4SLinus Torvalds 			goto finish;
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 		hp_sdc_status_out8(postcmd);
5651da177e4SLinus Torvalds 		goto actdone;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds  actdone:
569ffd51f46SHelge Deller 	if (act & HP_SDC_ACT_SEMAPHORE)
5701da177e4SLinus Torvalds 		up(curr->act.semaphore);
571ffd51f46SHelge Deller 	else if (act & HP_SDC_ACT_CALLBACK)
5726ce6b3aeSAl Viro 		curr->act.irqhook(0,NULL,0,0);
573ffd51f46SHelge Deller 
5741da177e4SLinus Torvalds 	if (curr->idx >= curr->endidx) { /* This transaction is over. */
575ffd51f46SHelge Deller 		if (act & HP_SDC_ACT_DEALLOC)
576ffd51f46SHelge Deller 			kfree(curr);
5771da177e4SLinus Torvalds 		hp_sdc.tq[curridx] = NULL;
578ffd51f46SHelge Deller 	} else {
5791da177e4SLinus Torvalds 		curr->actidx = idx + 1;
5801da177e4SLinus Torvalds 		curr->idx = idx + 2;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 	/* Interleave outbound data between the transactions. */
5831da177e4SLinus Torvalds 	hp_sdc.wcurr++;
584ffd51f46SHelge Deller 	if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
585ffd51f46SHelge Deller 		hp_sdc.wcurr = 0;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds  finish:
5881da177e4SLinus Torvalds 	/* If by some quirk IBF has cleared and our ISR has run to
5891da177e4SLinus Torvalds 	   see that that has happened, do it all again. */
590ffd51f46SHelge Deller 	if (!hp_sdc.ibf && limit++ < 20)
591ffd51f46SHelge Deller 		goto anew;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds  done:
594ffd51f46SHelge Deller 	if (hp_sdc.wcurr >= 0)
595ffd51f46SHelge Deller 		tasklet_schedule(&hp_sdc.task);
5961da177e4SLinus Torvalds 	write_unlock(&hp_sdc.lock);
597ffd51f46SHelge Deller 
5981da177e4SLinus Torvalds 	return 0;
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds /******* Functions called in either user or kernel context ****/
6029575499dSHelge Deller int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
603ffd51f46SHelge Deller {
6041da177e4SLinus Torvalds 	int i;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	if (this == NULL) {
6079575499dSHelge Deller 		BUG();
6081da177e4SLinus Torvalds 		return -EINVAL;
609ffd51f46SHelge Deller 	}
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	/* Can't have same transaction on queue twice */
6121da177e4SLinus Torvalds 	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
613ffd51f46SHelge Deller 		if (hp_sdc.tq[i] == this)
614ffd51f46SHelge Deller 			goto fail;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	this->actidx = 0;
6171da177e4SLinus Torvalds 	this->idx = 1;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/* Search for empty slot */
620ffd51f46SHelge Deller 	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
6211da177e4SLinus Torvalds 		if (hp_sdc.tq[i] == NULL) {
6221da177e4SLinus Torvalds 			hp_sdc.tq[i] = this;
6231da177e4SLinus Torvalds 			tasklet_schedule(&hp_sdc.task);
6241da177e4SLinus Torvalds 			return 0;
6251da177e4SLinus Torvalds 		}
626ffd51f46SHelge Deller 
6271da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
6281da177e4SLinus Torvalds 	return -EBUSY;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds  fail:
6311da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
6321da177e4SLinus Torvalds 	return -EINVAL;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6359575499dSHelge Deller int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
6369575499dSHelge Deller 	unsigned long flags;
6379575499dSHelge Deller 	int ret;
6389575499dSHelge Deller 
6399575499dSHelge Deller 	write_lock_irqsave(&hp_sdc.lock, flags);
6409575499dSHelge Deller 	ret = __hp_sdc_enqueue_transaction(this);
6419575499dSHelge Deller 	write_unlock_irqrestore(&hp_sdc.lock,flags);
6429575499dSHelge Deller 
6439575499dSHelge Deller 	return ret;
6449575499dSHelge Deller }
6459575499dSHelge Deller 
646ffd51f46SHelge Deller int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
647ffd51f46SHelge Deller {
6481da177e4SLinus Torvalds 	unsigned long flags;
6491da177e4SLinus Torvalds 	int i;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.lock, flags);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/* TODO: don't remove it if it's not done. */
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
656ffd51f46SHelge Deller 		if (hp_sdc.tq[i] == this)
657ffd51f46SHelge Deller 			hp_sdc.tq[i] = NULL;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.lock, flags);
6601da177e4SLinus Torvalds 	return 0;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds /********************** User context functions **************************/
666ffd51f46SHelge Deller int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
667ffd51f46SHelge Deller {
668ffd51f46SHelge Deller 	if (callback == NULL || hp_sdc.dev == NULL)
6691da177e4SLinus Torvalds 		return -EINVAL;
670ffd51f46SHelge Deller 
6711da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
6721da177e4SLinus Torvalds 	if (hp_sdc.timer != NULL) {
6731da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
6741da177e4SLinus Torvalds 		return -EBUSY;
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	hp_sdc.timer = callback;
6781da177e4SLinus Torvalds 	/* Enable interrupts from the timers */
6791da177e4SLinus Torvalds 	hp_sdc.im &= ~HP_SDC_IM_FH;
6801da177e4SLinus Torvalds         hp_sdc.im &= ~HP_SDC_IM_PT;
6811da177e4SLinus Torvalds 	hp_sdc.im &= ~HP_SDC_IM_TIMERS;
6821da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
6831da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	return 0;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
690ffd51f46SHelge Deller int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
691ffd51f46SHelge Deller {
692ffd51f46SHelge Deller 	if (callback == NULL || hp_sdc.dev == NULL)
6931da177e4SLinus Torvalds 		return -EINVAL;
694ffd51f46SHelge Deller 
6951da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
6961da177e4SLinus Torvalds 	if (hp_sdc.hil != NULL) {
6971da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
6981da177e4SLinus Torvalds 		return -EBUSY;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	hp_sdc.hil = callback;
7021da177e4SLinus Torvalds 	hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7031da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
7041da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	return 0;
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
711ffd51f46SHelge Deller int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
712ffd51f46SHelge Deller {
713ffd51f46SHelge Deller 	if (callback == NULL || hp_sdc.dev == NULL)
7141da177e4SLinus Torvalds 		return -EINVAL;
715ffd51f46SHelge Deller 
7161da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7171da177e4SLinus Torvalds 	if (hp_sdc.cooked != NULL) {
7181da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7191da177e4SLinus Torvalds 		return -EBUSY;
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	/* Enable interrupts from the HIL MLC */
7231da177e4SLinus Torvalds 	hp_sdc.cooked = callback;
7241da177e4SLinus Torvalds 	hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7251da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
7261da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	return 0;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
733ffd51f46SHelge Deller int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
734ffd51f46SHelge Deller {
7351da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7361da177e4SLinus Torvalds 	if ((callback != hp_sdc.timer) ||
7371da177e4SLinus Torvalds 	    (hp_sdc.timer == NULL)) {
7381da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7391da177e4SLinus Torvalds 		return -EINVAL;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* Disable interrupts from the timers */
7431da177e4SLinus Torvalds 	hp_sdc.timer = NULL;
7441da177e4SLinus Torvalds 	hp_sdc.im |= HP_SDC_IM_TIMERS;
7451da177e4SLinus Torvalds 	hp_sdc.im |= HP_SDC_IM_FH;
7461da177e4SLinus Torvalds 	hp_sdc.im |= HP_SDC_IM_PT;
7471da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
7481da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7491da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	return 0;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
754ffd51f46SHelge Deller int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
755ffd51f46SHelge Deller {
7561da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7571da177e4SLinus Torvalds 	if ((callback != hp_sdc.hil) ||
7581da177e4SLinus Torvalds 	    (hp_sdc.hil == NULL)) {
7591da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7601da177e4SLinus Torvalds 		return -EINVAL;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	hp_sdc.hil = NULL;
7641da177e4SLinus Torvalds 	/* Disable interrupts from HIL only if there is no cooked driver. */
7651da177e4SLinus Torvalds 	if(hp_sdc.cooked == NULL) {
7661da177e4SLinus Torvalds 		hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7671da177e4SLinus Torvalds 		hp_sdc.set_im = 1;
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7701da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	return 0;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
775ffd51f46SHelge Deller int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
776ffd51f46SHelge Deller {
7771da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7781da177e4SLinus Torvalds 	if ((callback != hp_sdc.cooked) ||
7791da177e4SLinus Torvalds 	    (hp_sdc.cooked == NULL)) {
7801da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7811da177e4SLinus Torvalds 		return -EINVAL;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	hp_sdc.cooked = NULL;
7851da177e4SLinus Torvalds 	/* Disable interrupts from HIL only if there is no raw HIL driver. */
7861da177e4SLinus Torvalds 	if(hp_sdc.hil == NULL) {
7871da177e4SLinus Torvalds 		hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7881da177e4SLinus Torvalds 		hp_sdc.set_im = 1;
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7911da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	return 0;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /************************* Keepalive timer task *********************/
7971da177e4SLinus Torvalds 
798ffd51f46SHelge Deller void hp_sdc_kicker (unsigned long data)
799ffd51f46SHelge Deller {
8001da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
8011da177e4SLinus Torvalds 	/* Re-insert the periodic task. */
8021da177e4SLinus Torvalds 	mod_timer(&hp_sdc.kicker, jiffies + HZ);
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds /************************** Module Initialization ***************************/
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds #if defined(__hppa__)
8081da177e4SLinus Torvalds 
8093acaf540SHelge Deller static const struct parisc_device_id hp_sdc_tbl[] = {
8101da177e4SLinus Torvalds 	{
8111da177e4SLinus Torvalds 		.hw_type =	HPHW_FIO,
8121da177e4SLinus Torvalds 		.hversion_rev =	HVERSION_REV_ANY_ID,
8131da177e4SLinus Torvalds 		.hversion =	HVERSION_ANY_ID,
8141da177e4SLinus Torvalds 		.sversion =	0x73,
8151da177e4SLinus Torvalds 	 },
8161da177e4SLinus Torvalds 	{ 0, }
8171da177e4SLinus Torvalds };
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds static int __init hp_sdc_init_hppa(struct parisc_device *d);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds static struct parisc_driver hp_sdc_driver = {
824bdad1f83SMatthew Wilcox 	.name =		"hp_sdc",
8251da177e4SLinus Torvalds 	.id_table =	hp_sdc_tbl,
8261da177e4SLinus Torvalds 	.probe =	hp_sdc_init_hppa,
8271da177e4SLinus Torvalds };
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds #endif /* __hppa__ */
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds static int __init hp_sdc_init(void)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	char *errstr;
8341da177e4SLinus Torvalds 	hp_sdc_transaction t_sync;
8351da177e4SLinus Torvalds 	uint8_t ts_sync[6];
8361da177e4SLinus Torvalds 	struct semaphore s_sync;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.lock);
8391da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.ibf_lock);
8401da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.rtq_lock);
8411da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.hook_lock);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	hp_sdc.timer		= NULL;
8441da177e4SLinus Torvalds 	hp_sdc.hil		= NULL;
8451da177e4SLinus Torvalds 	hp_sdc.pup		= NULL;
8461da177e4SLinus Torvalds 	hp_sdc.cooked		= NULL;
8471da177e4SLinus Torvalds 	hp_sdc.im		= HP_SDC_IM_MASK;  /* Mask maskable irqs */
8481da177e4SLinus Torvalds 	hp_sdc.set_im		= 1;
8491da177e4SLinus Torvalds 	hp_sdc.wi		= 0xff;
8501da177e4SLinus Torvalds 	hp_sdc.r7[0]		= 0xff;
8511da177e4SLinus Torvalds 	hp_sdc.r7[1]		= 0xff;
8521da177e4SLinus Torvalds 	hp_sdc.r7[2]		= 0xff;
8531da177e4SLinus Torvalds 	hp_sdc.r7[3]		= 0xff;
8541da177e4SLinus Torvalds 	hp_sdc.ibf		= 1;
8551da177e4SLinus Torvalds 
856ffd51f46SHelge Deller 	memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
857ffd51f46SHelge Deller 
8581da177e4SLinus Torvalds 	hp_sdc.wcurr		= -1;
8591da177e4SLinus Torvalds         hp_sdc.rcurr		= -1;
8601da177e4SLinus Torvalds 	hp_sdc.rqty		= 0;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	hp_sdc.dev_err = -ENODEV;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	errstr = "IO not found for";
865ffd51f46SHelge Deller 	if (!hp_sdc.base_io)
866ffd51f46SHelge Deller 		goto err0;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	errstr = "IRQ not found for";
869ffd51f46SHelge Deller 	if (!hp_sdc.irq)
870ffd51f46SHelge Deller 		goto err0;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	hp_sdc.dev_err = -EBUSY;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds #if defined(__hppa__)
8751da177e4SLinus Torvalds 	errstr = "IO not available for";
876ffd51f46SHelge Deller         if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
877ffd51f46SHelge Deller 		goto err0;
8781da177e4SLinus Torvalds #endif
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	errstr = "IRQ not available for";
8813acaf540SHelge Deller 	if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
882ffd51f46SHelge Deller 			"HP SDC", &hp_sdc))
883ffd51f46SHelge Deller 		goto err1;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	errstr = "NMI not available for";
8863acaf540SHelge Deller 	if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
887ffd51f46SHelge Deller 			"HP SDC NMI", &hp_sdc))
888ffd51f46SHelge Deller 		goto err2;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
8911da177e4SLinus Torvalds 	       (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	hp_sdc_status_in8();
8941da177e4SLinus Torvalds 	hp_sdc_data_in8();
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
8991da177e4SLinus Torvalds 	t_sync.actidx	= 0;
9001da177e4SLinus Torvalds 	t_sync.idx	= 1;
9011da177e4SLinus Torvalds 	t_sync.endidx	= 6;
9021da177e4SLinus Torvalds 	t_sync.seq	= ts_sync;
9031da177e4SLinus Torvalds 	ts_sync[0]	= HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
9041da177e4SLinus Torvalds 	ts_sync[1]	= 0x0f;
9051da177e4SLinus Torvalds 	ts_sync[2] = ts_sync[3]	= ts_sync[4] = ts_sync[5] = 0;
9061da177e4SLinus Torvalds 	t_sync.act.semaphore = &s_sync;
9071da177e4SLinus Torvalds 	init_MUTEX_LOCKED(&s_sync);
9081da177e4SLinus Torvalds 	hp_sdc_enqueue_transaction(&t_sync);
9091da177e4SLinus Torvalds 	down(&s_sync); /* Wait for t_sync to complete */
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/* Create the keepalive task */
9121da177e4SLinus Torvalds 	init_timer(&hp_sdc.kicker);
9131da177e4SLinus Torvalds 	hp_sdc.kicker.expires = jiffies + HZ;
9141da177e4SLinus Torvalds 	hp_sdc.kicker.function = &hp_sdc_kicker;
9151da177e4SLinus Torvalds 	add_timer(&hp_sdc.kicker);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	hp_sdc.dev_err = 0;
9181da177e4SLinus Torvalds 	return 0;
9191da177e4SLinus Torvalds  err2:
9203acaf540SHelge Deller 	free_irq(hp_sdc.irq, &hp_sdc);
9211da177e4SLinus Torvalds  err1:
9221da177e4SLinus Torvalds 	release_region(hp_sdc.data_io, 2);
9231da177e4SLinus Torvalds  err0:
9241da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
9251da177e4SLinus Torvalds 		errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
9261da177e4SLinus Torvalds 	hp_sdc.dev = NULL;
927ffd51f46SHelge Deller 
9281da177e4SLinus Torvalds 	return hp_sdc.dev_err;
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds #if defined(__hppa__)
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds static int __init hp_sdc_init_hppa(struct parisc_device *d)
9341da177e4SLinus Torvalds {
935ffd51f46SHelge Deller 	if (!d)
936ffd51f46SHelge Deller 		return 1;
937ffd51f46SHelge Deller 	if (hp_sdc.dev != NULL)
938ffd51f46SHelge Deller 		return 1;	/* We only expect one SDC */
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	hp_sdc.dev		= d;
9411da177e4SLinus Torvalds 	hp_sdc.irq		= d->irq;
9421da177e4SLinus Torvalds 	hp_sdc.nmi		= d->aux_irq;
94353f01bbaSMatthew Wilcox 	hp_sdc.base_io		= d->hpa.start;
94453f01bbaSMatthew Wilcox 	hp_sdc.data_io		= d->hpa.start + 0x800;
94553f01bbaSMatthew Wilcox 	hp_sdc.status_io	= d->hpa.start + 0x801;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	return hp_sdc_init();
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds #endif /* __hppa__ */
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds static void hp_sdc_exit(void)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.lock);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	/* Turn off all maskable "sub-function" irq's. */
9571da177e4SLinus Torvalds 	hp_sdc_spin_ibf();
9581da177e4SLinus Torvalds 	sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/* Wait until we know this has been processed by the i8042 */
9611da177e4SLinus Torvalds 	hp_sdc_spin_ibf();
9621da177e4SLinus Torvalds 
9633acaf540SHelge Deller 	free_irq(hp_sdc.nmi, &hp_sdc);
9643acaf540SHelge Deller 	free_irq(hp_sdc.irq, &hp_sdc);
9651da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.lock);
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	del_timer(&hp_sdc.kicker);
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	tasklet_kill(&hp_sdc.task);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds #if defined(__hppa__)
9721da177e4SLinus Torvalds 	if (unregister_parisc_driver(&hp_sdc_driver))
9731da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
9741da177e4SLinus Torvalds #endif
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds static int __init hp_sdc_register(void)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	hp_sdc_transaction tq_init;
9801da177e4SLinus Torvalds 	uint8_t tq_init_seq[5];
9811da177e4SLinus Torvalds 	struct semaphore tq_init_sem;
9821da177e4SLinus Torvalds #if defined(__mc68000__)
9831da177e4SLinus Torvalds 	mm_segment_t fs;
9841da177e4SLinus Torvalds 	unsigned char i;
9851da177e4SLinus Torvalds #endif
9861da177e4SLinus Torvalds 
987*305e342bSHelge Deller 	if (hp_sdc_disabled) {
988*305e342bSHelge Deller 		printk(KERN_WARNING PREFIX "HP SDC driver disabled by no_hpsdc=1.\n");
989*305e342bSHelge Deller 		return -ENODEV;
990*305e342bSHelge Deller 	}
991*305e342bSHelge Deller 
9921da177e4SLinus Torvalds 	hp_sdc.dev = NULL;
9931da177e4SLinus Torvalds 	hp_sdc.dev_err = 0;
9941da177e4SLinus Torvalds #if defined(__hppa__)
9951da177e4SLinus Torvalds 	if (register_parisc_driver(&hp_sdc_driver)) {
9961da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
9971da177e4SLinus Torvalds 		return -ENODEV;
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds #elif defined(__mc68000__)
10001da177e4SLinus Torvalds 	if (!MACH_IS_HP300)
10011da177e4SLinus Torvalds 	    return -ENODEV;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	hp_sdc.irq	 = 1;
10041da177e4SLinus Torvalds 	hp_sdc.nmi	 = 7;
10051da177e4SLinus Torvalds 	hp_sdc.base_io	 = (unsigned long) 0xf0428000;
10061da177e4SLinus Torvalds 	hp_sdc.data_io	 = (unsigned long) hp_sdc.base_io + 1;
10071da177e4SLinus Torvalds 	hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
10081da177e4SLinus Torvalds 	fs = get_fs();
10091da177e4SLinus Torvalds 	set_fs(KERNEL_DS);
10101da177e4SLinus Torvalds 	if (!get_user(i, (unsigned char *)hp_sdc.data_io))
10111da177e4SLinus Torvalds 		hp_sdc.dev = (void *)1;
10121da177e4SLinus Torvalds 	set_fs(fs);
10131da177e4SLinus Torvalds 	hp_sdc.dev_err   = hp_sdc_init();
10141da177e4SLinus Torvalds #endif
10151da177e4SLinus Torvalds 	if (hp_sdc.dev == NULL) {
10161da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "No SDC found.\n");
10171da177e4SLinus Torvalds 		return hp_sdc.dev_err;
10181da177e4SLinus Torvalds 	}
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	init_MUTEX_LOCKED(&tq_init_sem);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	tq_init.actidx		= 0;
10231da177e4SLinus Torvalds 	tq_init.idx		= 1;
10241da177e4SLinus Torvalds 	tq_init.endidx		= 5;
10251da177e4SLinus Torvalds 	tq_init.seq		= tq_init_seq;
10261da177e4SLinus Torvalds 	tq_init.act.semaphore	= &tq_init_sem;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	tq_init_seq[0] =
10291da177e4SLinus Torvalds 		HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
10301da177e4SLinus Torvalds 	tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
10311da177e4SLinus Torvalds 	tq_init_seq[2] = 1;
10321da177e4SLinus Torvalds 	tq_init_seq[3] = 0;
10331da177e4SLinus Torvalds 	tq_init_seq[4] = 0;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	hp_sdc_enqueue_transaction(&tq_init);
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	down(&tq_init_sem);
10381da177e4SLinus Torvalds 	up(&tq_init_sem);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
10411da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "Error reading config byte.\n");
10421da177e4SLinus Torvalds 		hp_sdc_exit();
10431da177e4SLinus Torvalds 		return -ENODEV;
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 	hp_sdc.r11 = tq_init_seq[4];
10461da177e4SLinus Torvalds 	if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
1047ffd51f46SHelge Deller 		const char *str;
10481da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "New style SDC\n");
10491da177e4SLinus Torvalds 		tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
10501da177e4SLinus Torvalds 		tq_init.actidx		= 0;
10511da177e4SLinus Torvalds 		tq_init.idx		= 1;
10521da177e4SLinus Torvalds 		down(&tq_init_sem);
10531da177e4SLinus Torvalds 		hp_sdc_enqueue_transaction(&tq_init);
10541da177e4SLinus Torvalds 		down(&tq_init_sem);
10551da177e4SLinus Torvalds 		up(&tq_init_sem);
10561da177e4SLinus Torvalds 		if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
10571da177e4SLinus Torvalds 			printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
10581da177e4SLinus Torvalds 			return -ENODEV;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 		hp_sdc.r7e = tq_init_seq[4];
10611da177e4SLinus Torvalds 		HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
10621da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "Revision: %s\n", str);
1063ffd51f46SHelge Deller 		if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
10641da177e4SLinus Torvalds 			printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
1065ffd51f46SHelge Deller 		if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
10661da177e4SLinus Torvalds 			printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
10671da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
10681da177e4SLinus Torvalds 		       "on next firmware reset.\n");
10691da177e4SLinus Torvalds 		tq_init_seq[0] = HP_SDC_ACT_PRECMD |
10701da177e4SLinus Torvalds 			HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
10711da177e4SLinus Torvalds 		tq_init_seq[1] = HP_SDC_CMD_SET_STR;
10721da177e4SLinus Torvalds 		tq_init_seq[2] = 1;
10731da177e4SLinus Torvalds 		tq_init_seq[3] = 0;
10741da177e4SLinus Torvalds 		tq_init.actidx		= 0;
10751da177e4SLinus Torvalds 		tq_init.idx		= 1;
10761da177e4SLinus Torvalds 		tq_init.endidx		= 4;
10771da177e4SLinus Torvalds 		down(&tq_init_sem);
10781da177e4SLinus Torvalds 		hp_sdc_enqueue_transaction(&tq_init);
10791da177e4SLinus Torvalds 		down(&tq_init_sem);
10801da177e4SLinus Torvalds 		up(&tq_init_sem);
1081ffd51f46SHelge Deller 	} else
10821da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
10831da177e4SLinus Torvalds 		       (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds         return 0;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds module_init(hp_sdc_register);
10891da177e4SLinus Torvalds module_exit(hp_sdc_exit);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds /* Timing notes:  These measurements taken on my 64MHz 7100-LC (715/64)
10921da177e4SLinus Torvalds  *                                              cycles cycles-adj    time
10931da177e4SLinus Torvalds  * between two consecutive mfctl(16)'s:              4        n/a    63ns
10941da177e4SLinus Torvalds  * hp_sdc_spin_ibf when idle:                      119        115   1.7us
10951da177e4SLinus Torvalds  * gsc_writeb status register:                      83         79   1.2us
10961da177e4SLinus Torvalds  * IBF to clear after sending SET_IM:             6204       6006    93us
10971da177e4SLinus Torvalds  * IBF to clear after sending LOAD_RT:            4467       4352    68us
10981da177e4SLinus Torvalds  * IBF to clear after sending two LOAD_RTs:      18974      18859   295us
10991da177e4SLinus Torvalds  * READ_T1, read status/data, IRQ, call handler: 35564        n/a   556us
11001da177e4SLinus Torvalds  * cmd to ~IBF READ_T1 2nd time right after:   5158403        n/a    81ms
11011da177e4SLinus Torvalds  * between IRQ received and ~IBF for above:    2578877        n/a    40ms
11021da177e4SLinus Torvalds  *
11031da177e4SLinus Torvalds  * Performance stats after a run of this module configuring HIL and
11041da177e4SLinus Torvalds  * receiving a few mouse events:
11051da177e4SLinus Torvalds  *
11061da177e4SLinus Torvalds  * status in8  282508 cycles 7128 calls
11071da177e4SLinus Torvalds  * status out8   8404 cycles  341 calls
11081da177e4SLinus Torvalds  * data out8     1734 cycles   78 calls
11091da177e4SLinus Torvalds  * isr         174324 cycles  617 calls (includes take)
11101da177e4SLinus Torvalds  * take          1241 cycles    2 calls
11111da177e4SLinus Torvalds  * put        1411504 cycles 6937 calls
11121da177e4SLinus Torvalds  * task       1655209 cycles 6937 calls (includes put)
11131da177e4SLinus Torvalds  *
11141da177e4SLinus Torvalds  */
1115