xref: /openbmc/linux/drivers/input/serio/hp_sdc.c (revision 0f17e4c796e89d1f69f13b653aba60e6ccfb8ae0)
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>
70*0f17e4c7SMatthew Wilcox #include <linux/semaphore.h>
711da177e4SLinus Torvalds #include <linux/slab.h>
721da177e4SLinus Torvalds #include <linux/hil.h>
734933d075SGeert Uytterhoeven #include <linux/semaphore.h>
741da177e4SLinus Torvalds #include <asm/io.h>
751da177e4SLinus Torvalds #include <asm/system.h>
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* Machine-specific abstraction */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #if defined(__hppa__)
801da177e4SLinus Torvalds # include <asm/parisc-device.h>
811da177e4SLinus Torvalds # define sdc_readb(p)		gsc_readb(p)
821da177e4SLinus Torvalds # define sdc_writeb(v,p)	gsc_writeb((v),(p))
831da177e4SLinus Torvalds #elif defined(__mc68000__)
841da177e4SLinus Torvalds # include <asm/uaccess.h>
851da177e4SLinus Torvalds # define sdc_readb(p)		in_8(p)
861da177e4SLinus Torvalds # define sdc_writeb(v,p)	out_8((p),(v))
871da177e4SLinus Torvalds #else
881da177e4SLinus Torvalds # error "HIL is not supported on this platform"
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #define PREFIX "HP SDC: "
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
941da177e4SLinus Torvalds MODULE_DESCRIPTION("HP i8042-based SDC Driver");
951da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_request_timer_irq);
981da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_request_hil_irq);
991da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_request_cooked_irq);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_release_timer_irq);
1021da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_release_hil_irq);
1031da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_release_cooked_irq);
1041da177e4SLinus Torvalds 
1059575499dSHelge Deller EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
1061da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
1071da177e4SLinus Torvalds EXPORT_SYMBOL(hp_sdc_dequeue_transaction);
1081da177e4SLinus Torvalds 
109305e342bSHelge Deller static unsigned int hp_sdc_disabled;
110305e342bSHelge Deller module_param_named(no_hpsdc, hp_sdc_disabled, bool, 0);
111305e342bSHelge Deller MODULE_PARM_DESC(no_hpsdc, "Do not enable HP SDC driver.");
112305e342bSHelge Deller 
1131da177e4SLinus Torvalds static hp_i8042_sdc	hp_sdc;	/* All driver state is kept in here. */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*************** primitives for use in any context *********************/
116ffd51f46SHelge Deller static inline uint8_t hp_sdc_status_in8(void)
117ffd51f46SHelge Deller {
1181da177e4SLinus Torvalds 	uint8_t status;
1191da177e4SLinus Torvalds 	unsigned long flags;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
1221da177e4SLinus Torvalds 	status = sdc_readb(hp_sdc.status_io);
123ffd51f46SHelge Deller 	if (!(status & HP_SDC_STATUS_IBF))
124ffd51f46SHelge Deller 		hp_sdc.ibf = 0;
1251da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	return status;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
130ffd51f46SHelge Deller static inline uint8_t hp_sdc_data_in8(void)
131ffd51f46SHelge Deller {
1321da177e4SLinus Torvalds 	return sdc_readb(hp_sdc.data_io);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
135ffd51f46SHelge Deller static inline void hp_sdc_status_out8(uint8_t val)
136ffd51f46SHelge Deller {
1371da177e4SLinus Torvalds 	unsigned long flags;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
1401da177e4SLinus Torvalds 	hp_sdc.ibf = 1;
141ffd51f46SHelge Deller 	if ((val & 0xf0) == 0xe0)
142ffd51f46SHelge Deller 		hp_sdc.wi = 0xff;
1431da177e4SLinus Torvalds 	sdc_writeb(val, hp_sdc.status_io);
1441da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
147ffd51f46SHelge Deller static inline void hp_sdc_data_out8(uint8_t val)
148ffd51f46SHelge Deller {
1491da177e4SLinus Torvalds 	unsigned long flags;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.ibf_lock, flags);
1521da177e4SLinus Torvalds 	hp_sdc.ibf = 1;
1531da177e4SLinus Torvalds 	sdc_writeb(val, hp_sdc.data_io);
1541da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*	Care must be taken to only invoke hp_sdc_spin_ibf when
1581da177e4SLinus Torvalds  *	absolutely needed, or in rarely invoked subroutines.
1591da177e4SLinus Torvalds  *	Not only does it waste CPU cycles, it also wastes bus cycles.
1601da177e4SLinus Torvalds  */
161ffd51f46SHelge Deller static inline void hp_sdc_spin_ibf(void)
162ffd51f46SHelge Deller {
1631da177e4SLinus Torvalds 	unsigned long flags;
1641da177e4SLinus Torvalds 	rwlock_t *lock;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	lock = &hp_sdc.ibf_lock;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	read_lock_irqsave(lock, flags);
1691da177e4SLinus Torvalds 	if (!hp_sdc.ibf) {
1701da177e4SLinus Torvalds 		read_unlock_irqrestore(lock, flags);
1711da177e4SLinus Torvalds 		return;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 	read_unlock(lock);
1741da177e4SLinus Torvalds 	write_lock(lock);
175ffd51f46SHelge Deller 	while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
176ffd51f46SHelge Deller 		{ }
1771da177e4SLinus Torvalds 	hp_sdc.ibf = 0;
1781da177e4SLinus Torvalds 	write_unlock_irqrestore(lock, flags);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /************************ Interrupt context functions ************************/
183ffd51f46SHelge Deller static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
184ffd51f46SHelge Deller {
1851da177e4SLinus Torvalds 	hp_sdc_transaction *curr;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	read_lock(&hp_sdc.rtq_lock);
1881da177e4SLinus Torvalds 	if (hp_sdc.rcurr < 0) {
1891da177e4SLinus Torvalds 		read_unlock(&hp_sdc.rtq_lock);
1901da177e4SLinus Torvalds 		return;
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 	curr = hp_sdc.tq[hp_sdc.rcurr];
1931da177e4SLinus Torvalds 	read_unlock(&hp_sdc.rtq_lock);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	curr->seq[curr->idx++] = status;
1961da177e4SLinus Torvalds 	curr->seq[curr->idx++] = data;
1971da177e4SLinus Torvalds 	hp_sdc.rqty -= 2;
1981da177e4SLinus Torvalds 	do_gettimeofday(&hp_sdc.rtv);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	if (hp_sdc.rqty <= 0) {
2011da177e4SLinus Torvalds 		/* All data has been gathered. */
202ffd51f46SHelge Deller 		if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
203ffd51f46SHelge Deller 			if (curr->act.semaphore)
204ffd51f46SHelge Deller 				up(curr->act.semaphore);
205ffd51f46SHelge Deller 
206ffd51f46SHelge Deller 		if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
2071da177e4SLinus Torvalds 			if (curr->act.irqhook)
2081da177e4SLinus Torvalds 				curr->act.irqhook(irq, dev_id, status, data);
209ffd51f46SHelge Deller 
2101da177e4SLinus Torvalds 		curr->actidx = curr->idx;
2111da177e4SLinus Torvalds 		curr->idx++;
2121da177e4SLinus Torvalds 		/* Return control of this transaction */
2131da177e4SLinus Torvalds 		write_lock(&hp_sdc.rtq_lock);
2141da177e4SLinus Torvalds 		hp_sdc.rcurr = -1;
2151da177e4SLinus Torvalds 		hp_sdc.rqty = 0;
2161da177e4SLinus Torvalds 		write_unlock(&hp_sdc.rtq_lock);
2171da177e4SLinus Torvalds 		tasklet_schedule(&hp_sdc.task);
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
221ffd51f46SHelge Deller static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
222ffd51f46SHelge Deller {
2231da177e4SLinus Torvalds 	uint8_t status, data;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	status = hp_sdc_status_in8();
2261da177e4SLinus Torvalds 	/* Read data unconditionally to advance i8042. */
2271da177e4SLinus Torvalds 	data =   hp_sdc_data_in8();
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* For now we are ignoring these until we get the SDC to behave. */
230ffd51f46SHelge Deller 	if (((status & 0xf1) == 0x51) && data == 0x82)
2311da177e4SLinus Torvalds 		return IRQ_HANDLED;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	switch (status & HP_SDC_STATUS_IRQMASK) {
2341da177e4SLinus Torvalds 	case 0: /* This case is not documented. */
2351da177e4SLinus Torvalds 		break;
236ffd51f46SHelge Deller 
2371da177e4SLinus Torvalds 	case HP_SDC_STATUS_USERTIMER:
2381da177e4SLinus Torvalds 	case HP_SDC_STATUS_PERIODIC:
2391da177e4SLinus Torvalds 	case HP_SDC_STATUS_TIMER:
2401da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2411da177e4SLinus Torvalds 		if (hp_sdc.timer != NULL)
2421da177e4SLinus Torvalds 			hp_sdc.timer(irq, dev_id, status, data);
2431da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2441da177e4SLinus Torvalds 		break;
245ffd51f46SHelge Deller 
2461da177e4SLinus Torvalds 	case HP_SDC_STATUS_REG:
2471da177e4SLinus Torvalds 		hp_sdc_take(irq, dev_id, status, data);
2481da177e4SLinus Torvalds 		break;
249ffd51f46SHelge Deller 
2501da177e4SLinus Torvalds 	case HP_SDC_STATUS_HILCMD:
2511da177e4SLinus Torvalds 	case HP_SDC_STATUS_HILDATA:
2521da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2531da177e4SLinus Torvalds 		if (hp_sdc.hil != NULL)
2541da177e4SLinus Torvalds 			hp_sdc.hil(irq, dev_id, status, data);
2551da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2561da177e4SLinus Torvalds 		break;
257ffd51f46SHelge Deller 
2581da177e4SLinus Torvalds 	case HP_SDC_STATUS_PUP:
2591da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2601da177e4SLinus Torvalds 		if (hp_sdc.pup != NULL)
2611da177e4SLinus Torvalds 			hp_sdc.pup(irq, dev_id, status, data);
262ffd51f46SHelge Deller 		else
263ffd51f46SHelge Deller 			printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
2641da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2651da177e4SLinus Torvalds 		break;
266ffd51f46SHelge Deller 
2671da177e4SLinus Torvalds 	default:
2681da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2691da177e4SLinus Torvalds 		if (hp_sdc.cooked != NULL)
2701da177e4SLinus Torvalds 			hp_sdc.cooked(irq, dev_id, status, data);
2711da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
2721da177e4SLinus Torvalds 		break;
2731da177e4SLinus Torvalds 	}
274ffd51f46SHelge Deller 
2751da177e4SLinus Torvalds 	return IRQ_HANDLED;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 
279ffd51f46SHelge Deller static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
280ffd51f46SHelge Deller {
2811da177e4SLinus Torvalds 	int status;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	status = hp_sdc_status_in8();
2841da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX "NMI !\n");
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds #if 0
2871da177e4SLinus Torvalds 	if (status & HP_SDC_NMISTATUS_FHS) {
2881da177e4SLinus Torvalds 		read_lock(&hp_sdc.hook_lock);
2891da177e4SLinus Torvalds 		if (hp_sdc.timer != NULL)
2901da177e4SLinus Torvalds 			hp_sdc.timer(irq, dev_id, status, 0);
2911da177e4SLinus Torvalds 		read_unlock(&hp_sdc.hook_lock);
292ffd51f46SHelge Deller 	} else {
2931da177e4SLinus Torvalds 		/* TODO: pass this on to the HIL handler, or do SAK here? */
2941da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "HIL NMI\n");
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds #endif
297ffd51f46SHelge Deller 
2981da177e4SLinus Torvalds 	return IRQ_HANDLED;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /***************** Kernel (tasklet) context functions ****************/
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds unsigned long hp_sdc_put(void);
3051da177e4SLinus Torvalds 
306ffd51f46SHelge Deller static void hp_sdc_tasklet(unsigned long foo)
307ffd51f46SHelge Deller {
3081da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.rtq_lock);
309ffd51f46SHelge Deller 
3101da177e4SLinus Torvalds 	if (hp_sdc.rcurr >= 0) {
3111da177e4SLinus Torvalds 		struct timeval tv;
312ffd51f46SHelge Deller 
3131da177e4SLinus Torvalds 		do_gettimeofday(&tv);
314ffd51f46SHelge Deller 		if (tv.tv_sec > hp_sdc.rtv.tv_sec)
315ffd51f46SHelge Deller 			tv.tv_usec += USEC_PER_SEC;
316ffd51f46SHelge Deller 
3171da177e4SLinus Torvalds 		if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
3181da177e4SLinus Torvalds 			hp_sdc_transaction *curr;
3191da177e4SLinus Torvalds 			uint8_t tmp;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 			curr = hp_sdc.tq[hp_sdc.rcurr];
3221da177e4SLinus Torvalds 			/* If this turns out to be a normal failure mode
3231da177e4SLinus Torvalds 			 * we'll need to figure out a way to communicate
3241da177e4SLinus Torvalds 			 * it back to the application. and be less verbose.
3251da177e4SLinus Torvalds 			 */
3261da177e4SLinus Torvalds 			printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
3271da177e4SLinus Torvalds 			       tv.tv_usec - hp_sdc.rtv.tv_usec);
3281da177e4SLinus Torvalds 			curr->idx += hp_sdc.rqty;
3291da177e4SLinus Torvalds 			hp_sdc.rqty = 0;
3301da177e4SLinus Torvalds 			tmp = curr->seq[curr->actidx];
3311da177e4SLinus Torvalds 			curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
332ffd51f46SHelge Deller 			if (tmp & HP_SDC_ACT_SEMAPHORE)
3331da177e4SLinus Torvalds 				if (curr->act.semaphore)
3341da177e4SLinus Torvalds 					up(curr->act.semaphore);
335ffd51f46SHelge Deller 
3361da177e4SLinus Torvalds 			if (tmp & HP_SDC_ACT_CALLBACK) {
3371da177e4SLinus Torvalds 				/* Note this means that irqhooks may be called
3381da177e4SLinus Torvalds 				 * in tasklet/bh context.
3391da177e4SLinus Torvalds 				 */
3401da177e4SLinus Torvalds 				if (curr->act.irqhook)
3416ce6b3aeSAl Viro 					curr->act.irqhook(0, NULL, 0, 0);
3421da177e4SLinus Torvalds 			}
343ffd51f46SHelge Deller 
3441da177e4SLinus Torvalds 			curr->actidx = curr->idx;
3451da177e4SLinus Torvalds 			curr->idx++;
3461da177e4SLinus Torvalds 			hp_sdc.rcurr = -1;
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.rtq_lock);
3501da177e4SLinus Torvalds 	hp_sdc_put();
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
353ffd51f46SHelge Deller unsigned long hp_sdc_put(void)
354ffd51f46SHelge Deller {
3551da177e4SLinus Torvalds 	hp_sdc_transaction *curr;
3561da177e4SLinus Torvalds 	uint8_t act;
3571da177e4SLinus Torvalds 	int idx, curridx;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	int limit = 0;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	write_lock(&hp_sdc.lock);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	/* If i8042 buffers are full, we cannot do anything that
3641da177e4SLinus Torvalds 	   requires output, so we skip to the administrativa. */
3651da177e4SLinus Torvalds 	if (hp_sdc.ibf) {
3661da177e4SLinus Torvalds 		hp_sdc_status_in8();
367ffd51f46SHelge Deller 		if (hp_sdc.ibf)
368ffd51f46SHelge Deller 			goto finish;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds  anew:
3721da177e4SLinus Torvalds 	/* See if we are in the middle of a sequence. */
373ffd51f46SHelge Deller 	if (hp_sdc.wcurr < 0)
374ffd51f46SHelge Deller 		hp_sdc.wcurr = 0;
3751da177e4SLinus Torvalds 	read_lock_irq(&hp_sdc.rtq_lock);
376ffd51f46SHelge Deller 	if (hp_sdc.rcurr == hp_sdc.wcurr)
377ffd51f46SHelge Deller 		hp_sdc.wcurr++;
3781da177e4SLinus Torvalds 	read_unlock_irq(&hp_sdc.rtq_lock);
379ffd51f46SHelge Deller 	if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
380ffd51f46SHelge Deller 		hp_sdc.wcurr = 0;
3811da177e4SLinus Torvalds 	curridx = hp_sdc.wcurr;
3821da177e4SLinus Torvalds 
383ffd51f46SHelge Deller 	if (hp_sdc.tq[curridx] != NULL)
384ffd51f46SHelge Deller 		goto start;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	while (++curridx != hp_sdc.wcurr) {
3871da177e4SLinus Torvalds 		if (curridx >= HP_SDC_QUEUE_LEN) {
3881da177e4SLinus Torvalds 			curridx = -1; /* Wrap to top */
3891da177e4SLinus Torvalds 			continue;
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 		read_lock_irq(&hp_sdc.rtq_lock);
3921da177e4SLinus Torvalds 		if (hp_sdc.rcurr == curridx) {
3931da177e4SLinus Torvalds 			read_unlock_irq(&hp_sdc.rtq_lock);
3941da177e4SLinus Torvalds 			continue;
3951da177e4SLinus Torvalds 		}
3961da177e4SLinus Torvalds 		read_unlock_irq(&hp_sdc.rtq_lock);
397ffd51f46SHelge Deller 		if (hp_sdc.tq[curridx] != NULL)
398ffd51f46SHelge Deller 			break; /* Found one. */
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 	if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
4011da177e4SLinus Torvalds 		curridx = -1;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	hp_sdc.wcurr = curridx;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds  start:
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Check to see if the interrupt mask needs to be set. */
4081da177e4SLinus Torvalds 	if (hp_sdc.set_im) {
4091da177e4SLinus Torvalds 		hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
4101da177e4SLinus Torvalds 		hp_sdc.set_im = 0;
4111da177e4SLinus Torvalds 		goto finish;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
414ffd51f46SHelge Deller 	if (hp_sdc.wcurr == -1)
415ffd51f46SHelge Deller 		goto done;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	curr = hp_sdc.tq[curridx];
4181da177e4SLinus Torvalds 	idx = curr->actidx;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (curr->actidx >= curr->endidx) {
4211da177e4SLinus Torvalds 		hp_sdc.tq[curridx] = NULL;
4221da177e4SLinus Torvalds 		/* Interleave outbound data between the transactions. */
4231da177e4SLinus Torvalds 		hp_sdc.wcurr++;
424ffd51f46SHelge Deller 		if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
425ffd51f46SHelge Deller 			hp_sdc.wcurr = 0;
4261da177e4SLinus Torvalds 		goto finish;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	act = curr->seq[idx];
4301da177e4SLinus Torvalds 	idx++;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	if (curr->idx >= curr->endidx) {
433ffd51f46SHelge Deller 		if (act & HP_SDC_ACT_DEALLOC)
434ffd51f46SHelge Deller 			kfree(curr);
4351da177e4SLinus Torvalds 		hp_sdc.tq[curridx] = NULL;
4361da177e4SLinus Torvalds 		/* Interleave outbound data between the transactions. */
4371da177e4SLinus Torvalds 		hp_sdc.wcurr++;
438ffd51f46SHelge Deller 		if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
439ffd51f46SHelge Deller 			hp_sdc.wcurr = 0;
4401da177e4SLinus Torvalds 		goto finish;
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	while (act & HP_SDC_ACT_PRECMD) {
4441da177e4SLinus Torvalds 		if (curr->idx != idx) {
4451da177e4SLinus Torvalds 			idx++;
4461da177e4SLinus Torvalds 			act &= ~HP_SDC_ACT_PRECMD;
4471da177e4SLinus Torvalds 			break;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 		hp_sdc_status_out8(curr->seq[idx]);
4501da177e4SLinus Torvalds 		curr->idx++;
4511da177e4SLinus Torvalds 		/* act finished? */
4521da177e4SLinus Torvalds 		if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
4531da177e4SLinus Torvalds 			goto actdone;
4541da177e4SLinus Torvalds 		/* skip quantity field if data-out sequence follows. */
455ffd51f46SHelge Deller 		if (act & HP_SDC_ACT_DATAOUT)
456ffd51f46SHelge Deller 			curr->idx++;
4571da177e4SLinus Torvalds 		goto finish;
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 	if (act & HP_SDC_ACT_DATAOUT) {
4601da177e4SLinus Torvalds 		int qty;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		qty = curr->seq[idx];
4631da177e4SLinus Torvalds 		idx++;
4641da177e4SLinus Torvalds 		if (curr->idx - idx < qty) {
4651da177e4SLinus Torvalds 			hp_sdc_data_out8(curr->seq[curr->idx]);
4661da177e4SLinus Torvalds 			curr->idx++;
4671da177e4SLinus Torvalds 			/* act finished? */
468ffd51f46SHelge Deller 			if (curr->idx - idx >= qty &&
469ffd51f46SHelge Deller 			    (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
4701da177e4SLinus Torvalds 				goto actdone;
4711da177e4SLinus Torvalds 			goto finish;
4721da177e4SLinus Torvalds 		}
4731da177e4SLinus Torvalds 		idx += qty;
4741da177e4SLinus Torvalds 		act &= ~HP_SDC_ACT_DATAOUT;
475ffd51f46SHelge Deller 	} else
476ffd51f46SHelge Deller 	    while (act & HP_SDC_ACT_DATAREG) {
4771da177e4SLinus Torvalds 		int mask;
4781da177e4SLinus Torvalds 		uint8_t w7[4];
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 		mask = curr->seq[idx];
4811da177e4SLinus Torvalds 		if (idx != curr->idx) {
4821da177e4SLinus Torvalds 			idx++;
4831da177e4SLinus Torvalds 			idx += !!(mask & 1);
4841da177e4SLinus Torvalds 			idx += !!(mask & 2);
4851da177e4SLinus Torvalds 			idx += !!(mask & 4);
4861da177e4SLinus Torvalds 			idx += !!(mask & 8);
4871da177e4SLinus Torvalds 			act &= ~HP_SDC_ACT_DATAREG;
4881da177e4SLinus Torvalds 			break;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 		w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
4921da177e4SLinus Torvalds 		w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
4931da177e4SLinus Torvalds 		w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
4941da177e4SLinus Torvalds 		w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 		if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
4971da177e4SLinus Torvalds 		    w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
4981da177e4SLinus Torvalds 			int i = 0;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 			/* Need to point the write index register */
501ffd51f46SHelge Deller 			while (i < 4 && w7[i] == hp_sdc.r7[i])
502ffd51f46SHelge Deller 				i++;
503ffd51f46SHelge Deller 
5041da177e4SLinus Torvalds 			if (i < 4) {
5051da177e4SLinus Torvalds 				hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
5061da177e4SLinus Torvalds 				hp_sdc.wi = 0x70 + i;
5071da177e4SLinus Torvalds 				goto finish;
5081da177e4SLinus Torvalds 			}
509ffd51f46SHelge Deller 
5101da177e4SLinus Torvalds 			idx++;
5111da177e4SLinus Torvalds 			if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
5121da177e4SLinus Torvalds 				goto actdone;
513ffd51f46SHelge Deller 
5141da177e4SLinus Torvalds 			curr->idx = idx;
5151da177e4SLinus Torvalds 			act &= ~HP_SDC_ACT_DATAREG;
5161da177e4SLinus Torvalds 			break;
5171da177e4SLinus Torvalds 		}
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 		hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
5201da177e4SLinus Torvalds 		hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
5211da177e4SLinus Torvalds 		hp_sdc.wi++; /* write index register autoincrements */
5221da177e4SLinus Torvalds 		{
5231da177e4SLinus Torvalds 			int i = 0;
5241da177e4SLinus Torvalds 
525ffd51f46SHelge Deller 			while ((i < 4) && w7[i] == hp_sdc.r7[i])
526ffd51f46SHelge Deller 				i++;
5271da177e4SLinus Torvalds 			if (i >= 4) {
5281da177e4SLinus Torvalds 				curr->idx = idx + 1;
5291da177e4SLinus Torvalds 				if ((act & HP_SDC_ACT_DURING) ==
5301da177e4SLinus Torvalds 				    HP_SDC_ACT_DATAREG)
5311da177e4SLinus Torvalds 					goto actdone;
5321da177e4SLinus Torvalds 			}
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 		goto finish;
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 	/* We don't go any further in the command if there is a pending read,
5371da177e4SLinus Torvalds 	   because we don't want interleaved results. */
5381da177e4SLinus Torvalds 	read_lock_irq(&hp_sdc.rtq_lock);
5391da177e4SLinus Torvalds 	if (hp_sdc.rcurr >= 0) {
5401da177e4SLinus Torvalds 		read_unlock_irq(&hp_sdc.rtq_lock);
5411da177e4SLinus Torvalds 		goto finish;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 	read_unlock_irq(&hp_sdc.rtq_lock);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	if (act & HP_SDC_ACT_POSTCMD) {
5471da177e4SLinus Torvalds 		uint8_t postcmd;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 		/* curr->idx should == idx at this point. */
5501da177e4SLinus Torvalds 		postcmd = curr->seq[idx];
5511da177e4SLinus Torvalds 		curr->idx++;
5521da177e4SLinus Torvalds 		if (act & HP_SDC_ACT_DATAIN) {
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 			/* Start a new read */
5551da177e4SLinus Torvalds 			hp_sdc.rqty = curr->seq[curr->idx];
5561da177e4SLinus Torvalds 			do_gettimeofday(&hp_sdc.rtv);
5571da177e4SLinus Torvalds 			curr->idx++;
5581da177e4SLinus Torvalds 			/* Still need to lock here in case of spurious irq. */
5591da177e4SLinus Torvalds 			write_lock_irq(&hp_sdc.rtq_lock);
5601da177e4SLinus Torvalds 			hp_sdc.rcurr = curridx;
5611da177e4SLinus Torvalds 			write_unlock_irq(&hp_sdc.rtq_lock);
5621da177e4SLinus Torvalds 			hp_sdc_status_out8(postcmd);
5631da177e4SLinus Torvalds 			goto finish;
5641da177e4SLinus Torvalds 		}
5651da177e4SLinus Torvalds 		hp_sdc_status_out8(postcmd);
5661da177e4SLinus Torvalds 		goto actdone;
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds  actdone:
570ffd51f46SHelge Deller 	if (act & HP_SDC_ACT_SEMAPHORE)
5711da177e4SLinus Torvalds 		up(curr->act.semaphore);
572ffd51f46SHelge Deller 	else if (act & HP_SDC_ACT_CALLBACK)
5736ce6b3aeSAl Viro 		curr->act.irqhook(0,NULL,0,0);
574ffd51f46SHelge Deller 
5751da177e4SLinus Torvalds 	if (curr->idx >= curr->endidx) { /* This transaction is over. */
576ffd51f46SHelge Deller 		if (act & HP_SDC_ACT_DEALLOC)
577ffd51f46SHelge Deller 			kfree(curr);
5781da177e4SLinus Torvalds 		hp_sdc.tq[curridx] = NULL;
579ffd51f46SHelge Deller 	} else {
5801da177e4SLinus Torvalds 		curr->actidx = idx + 1;
5811da177e4SLinus Torvalds 		curr->idx = idx + 2;
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 	/* Interleave outbound data between the transactions. */
5841da177e4SLinus Torvalds 	hp_sdc.wcurr++;
585ffd51f46SHelge Deller 	if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
586ffd51f46SHelge Deller 		hp_sdc.wcurr = 0;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds  finish:
5891da177e4SLinus Torvalds 	/* If by some quirk IBF has cleared and our ISR has run to
5901da177e4SLinus Torvalds 	   see that that has happened, do it all again. */
591ffd51f46SHelge Deller 	if (!hp_sdc.ibf && limit++ < 20)
592ffd51f46SHelge Deller 		goto anew;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds  done:
595ffd51f46SHelge Deller 	if (hp_sdc.wcurr >= 0)
596ffd51f46SHelge Deller 		tasklet_schedule(&hp_sdc.task);
5971da177e4SLinus Torvalds 	write_unlock(&hp_sdc.lock);
598ffd51f46SHelge Deller 
5991da177e4SLinus Torvalds 	return 0;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds /******* Functions called in either user or kernel context ****/
6039575499dSHelge Deller int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
604ffd51f46SHelge Deller {
6051da177e4SLinus Torvalds 	int i;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (this == NULL) {
6089575499dSHelge Deller 		BUG();
6091da177e4SLinus Torvalds 		return -EINVAL;
610ffd51f46SHelge Deller 	}
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	/* Can't have same transaction on queue twice */
6131da177e4SLinus Torvalds 	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
614ffd51f46SHelge Deller 		if (hp_sdc.tq[i] == this)
615ffd51f46SHelge Deller 			goto fail;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	this->actidx = 0;
6181da177e4SLinus Torvalds 	this->idx = 1;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	/* Search for empty slot */
621ffd51f46SHelge Deller 	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
6221da177e4SLinus Torvalds 		if (hp_sdc.tq[i] == NULL) {
6231da177e4SLinus Torvalds 			hp_sdc.tq[i] = this;
6241da177e4SLinus Torvalds 			tasklet_schedule(&hp_sdc.task);
6251da177e4SLinus Torvalds 			return 0;
6261da177e4SLinus Torvalds 		}
627ffd51f46SHelge Deller 
6281da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
6291da177e4SLinus Torvalds 	return -EBUSY;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds  fail:
6321da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
6331da177e4SLinus Torvalds 	return -EINVAL;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6369575499dSHelge Deller int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
6379575499dSHelge Deller 	unsigned long flags;
6389575499dSHelge Deller 	int ret;
6399575499dSHelge Deller 
6409575499dSHelge Deller 	write_lock_irqsave(&hp_sdc.lock, flags);
6419575499dSHelge Deller 	ret = __hp_sdc_enqueue_transaction(this);
6429575499dSHelge Deller 	write_unlock_irqrestore(&hp_sdc.lock,flags);
6439575499dSHelge Deller 
6449575499dSHelge Deller 	return ret;
6459575499dSHelge Deller }
6469575499dSHelge Deller 
647ffd51f46SHelge Deller int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
648ffd51f46SHelge Deller {
6491da177e4SLinus Torvalds 	unsigned long flags;
6501da177e4SLinus Torvalds 	int i;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	write_lock_irqsave(&hp_sdc.lock, flags);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	/* TODO: don't remove it if it's not done. */
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
657ffd51f46SHelge Deller 		if (hp_sdc.tq[i] == this)
658ffd51f46SHelge Deller 			hp_sdc.tq[i] = NULL;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	write_unlock_irqrestore(&hp_sdc.lock, flags);
6611da177e4SLinus Torvalds 	return 0;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds /********************** User context functions **************************/
667ffd51f46SHelge Deller int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
668ffd51f46SHelge Deller {
669ffd51f46SHelge Deller 	if (callback == NULL || hp_sdc.dev == NULL)
6701da177e4SLinus Torvalds 		return -EINVAL;
671ffd51f46SHelge Deller 
6721da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
6731da177e4SLinus Torvalds 	if (hp_sdc.timer != NULL) {
6741da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
6751da177e4SLinus Torvalds 		return -EBUSY;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	hp_sdc.timer = callback;
6791da177e4SLinus Torvalds 	/* Enable interrupts from the timers */
6801da177e4SLinus Torvalds 	hp_sdc.im &= ~HP_SDC_IM_FH;
6811da177e4SLinus Torvalds         hp_sdc.im &= ~HP_SDC_IM_PT;
6821da177e4SLinus Torvalds 	hp_sdc.im &= ~HP_SDC_IM_TIMERS;
6831da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
6841da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	return 0;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
691ffd51f46SHelge Deller int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
692ffd51f46SHelge Deller {
693ffd51f46SHelge Deller 	if (callback == NULL || hp_sdc.dev == NULL)
6941da177e4SLinus Torvalds 		return -EINVAL;
695ffd51f46SHelge Deller 
6961da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
6971da177e4SLinus Torvalds 	if (hp_sdc.hil != NULL) {
6981da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
6991da177e4SLinus Torvalds 		return -EBUSY;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	hp_sdc.hil = callback;
7031da177e4SLinus Torvalds 	hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7041da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
7051da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	return 0;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
712ffd51f46SHelge Deller int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
713ffd51f46SHelge Deller {
714ffd51f46SHelge Deller 	if (callback == NULL || hp_sdc.dev == NULL)
7151da177e4SLinus Torvalds 		return -EINVAL;
716ffd51f46SHelge Deller 
7171da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7181da177e4SLinus Torvalds 	if (hp_sdc.cooked != NULL) {
7191da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7201da177e4SLinus Torvalds 		return -EBUSY;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	/* Enable interrupts from the HIL MLC */
7241da177e4SLinus Torvalds 	hp_sdc.cooked = callback;
7251da177e4SLinus Torvalds 	hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7261da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
7271da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	return 0;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734ffd51f46SHelge Deller int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
735ffd51f46SHelge Deller {
7361da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7371da177e4SLinus Torvalds 	if ((callback != hp_sdc.timer) ||
7381da177e4SLinus Torvalds 	    (hp_sdc.timer == NULL)) {
7391da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7401da177e4SLinus Torvalds 		return -EINVAL;
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	/* Disable interrupts from the timers */
7441da177e4SLinus Torvalds 	hp_sdc.timer = NULL;
7451da177e4SLinus Torvalds 	hp_sdc.im |= HP_SDC_IM_TIMERS;
7461da177e4SLinus Torvalds 	hp_sdc.im |= HP_SDC_IM_FH;
7471da177e4SLinus Torvalds 	hp_sdc.im |= HP_SDC_IM_PT;
7481da177e4SLinus Torvalds 	hp_sdc.set_im = 1;
7491da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7501da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	return 0;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
755ffd51f46SHelge Deller int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
756ffd51f46SHelge Deller {
7571da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7581da177e4SLinus Torvalds 	if ((callback != hp_sdc.hil) ||
7591da177e4SLinus Torvalds 	    (hp_sdc.hil == NULL)) {
7601da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7611da177e4SLinus Torvalds 		return -EINVAL;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	hp_sdc.hil = NULL;
7651da177e4SLinus Torvalds 	/* Disable interrupts from HIL only if there is no cooked driver. */
7661da177e4SLinus Torvalds 	if(hp_sdc.cooked == NULL) {
7671da177e4SLinus Torvalds 		hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7681da177e4SLinus Torvalds 		hp_sdc.set_im = 1;
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7711da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	return 0;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
776ffd51f46SHelge Deller int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
777ffd51f46SHelge Deller {
7781da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.hook_lock);
7791da177e4SLinus Torvalds 	if ((callback != hp_sdc.cooked) ||
7801da177e4SLinus Torvalds 	    (hp_sdc.cooked == NULL)) {
7811da177e4SLinus Torvalds 		write_unlock_irq(&hp_sdc.hook_lock);
7821da177e4SLinus Torvalds 		return -EINVAL;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	hp_sdc.cooked = NULL;
7861da177e4SLinus Torvalds 	/* Disable interrupts from HIL only if there is no raw HIL driver. */
7871da177e4SLinus Torvalds 	if(hp_sdc.hil == NULL) {
7881da177e4SLinus Torvalds 		hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
7891da177e4SLinus Torvalds 		hp_sdc.set_im = 1;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.hook_lock);
7921da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	return 0;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds /************************* Keepalive timer task *********************/
7981da177e4SLinus Torvalds 
799ffd51f46SHelge Deller void hp_sdc_kicker (unsigned long data)
800ffd51f46SHelge Deller {
8011da177e4SLinus Torvalds 	tasklet_schedule(&hp_sdc.task);
8021da177e4SLinus Torvalds 	/* Re-insert the periodic task. */
8031da177e4SLinus Torvalds 	mod_timer(&hp_sdc.kicker, jiffies + HZ);
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /************************** Module Initialization ***************************/
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds #if defined(__hppa__)
8091da177e4SLinus Torvalds 
8103acaf540SHelge Deller static const struct parisc_device_id hp_sdc_tbl[] = {
8111da177e4SLinus Torvalds 	{
8121da177e4SLinus Torvalds 		.hw_type =	HPHW_FIO,
8131da177e4SLinus Torvalds 		.hversion_rev =	HVERSION_REV_ANY_ID,
8141da177e4SLinus Torvalds 		.hversion =	HVERSION_ANY_ID,
8151da177e4SLinus Torvalds 		.sversion =	0x73,
8161da177e4SLinus Torvalds 	 },
8171da177e4SLinus Torvalds 	{ 0, }
8181da177e4SLinus Torvalds };
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds static int __init hp_sdc_init_hppa(struct parisc_device *d);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds static struct parisc_driver hp_sdc_driver = {
825bdad1f83SMatthew Wilcox 	.name =		"hp_sdc",
8261da177e4SLinus Torvalds 	.id_table =	hp_sdc_tbl,
8271da177e4SLinus Torvalds 	.probe =	hp_sdc_init_hppa,
8281da177e4SLinus Torvalds };
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds #endif /* __hppa__ */
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds static int __init hp_sdc_init(void)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	char *errstr;
8351da177e4SLinus Torvalds 	hp_sdc_transaction t_sync;
8361da177e4SLinus Torvalds 	uint8_t ts_sync[6];
8371da177e4SLinus Torvalds 	struct semaphore s_sync;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.lock);
8401da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.ibf_lock);
8411da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.rtq_lock);
8421da177e4SLinus Torvalds 	rwlock_init(&hp_sdc.hook_lock);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	hp_sdc.timer		= NULL;
8451da177e4SLinus Torvalds 	hp_sdc.hil		= NULL;
8461da177e4SLinus Torvalds 	hp_sdc.pup		= NULL;
8471da177e4SLinus Torvalds 	hp_sdc.cooked		= NULL;
8481da177e4SLinus Torvalds 	hp_sdc.im		= HP_SDC_IM_MASK;  /* Mask maskable irqs */
8491da177e4SLinus Torvalds 	hp_sdc.set_im		= 1;
8501da177e4SLinus Torvalds 	hp_sdc.wi		= 0xff;
8511da177e4SLinus Torvalds 	hp_sdc.r7[0]		= 0xff;
8521da177e4SLinus Torvalds 	hp_sdc.r7[1]		= 0xff;
8531da177e4SLinus Torvalds 	hp_sdc.r7[2]		= 0xff;
8541da177e4SLinus Torvalds 	hp_sdc.r7[3]		= 0xff;
8551da177e4SLinus Torvalds 	hp_sdc.ibf		= 1;
8561da177e4SLinus Torvalds 
857ffd51f46SHelge Deller 	memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
858ffd51f46SHelge Deller 
8591da177e4SLinus Torvalds 	hp_sdc.wcurr		= -1;
8601da177e4SLinus Torvalds         hp_sdc.rcurr		= -1;
8611da177e4SLinus Torvalds 	hp_sdc.rqty		= 0;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	hp_sdc.dev_err = -ENODEV;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	errstr = "IO not found for";
866ffd51f46SHelge Deller 	if (!hp_sdc.base_io)
867ffd51f46SHelge Deller 		goto err0;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	errstr = "IRQ not found for";
870ffd51f46SHelge Deller 	if (!hp_sdc.irq)
871ffd51f46SHelge Deller 		goto err0;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	hp_sdc.dev_err = -EBUSY;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds #if defined(__hppa__)
8761da177e4SLinus Torvalds 	errstr = "IO not available for";
877ffd51f46SHelge Deller         if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
878ffd51f46SHelge Deller 		goto err0;
8791da177e4SLinus Torvalds #endif
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	errstr = "IRQ not available for";
8823acaf540SHelge Deller 	if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
883ffd51f46SHelge Deller 			"HP SDC", &hp_sdc))
884ffd51f46SHelge Deller 		goto err1;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	errstr = "NMI not available for";
8873acaf540SHelge Deller 	if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
888ffd51f46SHelge Deller 			"HP SDC NMI", &hp_sdc))
889ffd51f46SHelge Deller 		goto err2;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
8921da177e4SLinus Torvalds 	       (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	hp_sdc_status_in8();
8951da177e4SLinus Torvalds 	hp_sdc_data_in8();
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	/* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
9001da177e4SLinus Torvalds 	t_sync.actidx	= 0;
9011da177e4SLinus Torvalds 	t_sync.idx	= 1;
9021da177e4SLinus Torvalds 	t_sync.endidx	= 6;
9031da177e4SLinus Torvalds 	t_sync.seq	= ts_sync;
9041da177e4SLinus Torvalds 	ts_sync[0]	= HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
9051da177e4SLinus Torvalds 	ts_sync[1]	= 0x0f;
9061da177e4SLinus Torvalds 	ts_sync[2] = ts_sync[3]	= ts_sync[4] = ts_sync[5] = 0;
9071da177e4SLinus Torvalds 	t_sync.act.semaphore = &s_sync;
9081da177e4SLinus Torvalds 	init_MUTEX_LOCKED(&s_sync);
9091da177e4SLinus Torvalds 	hp_sdc_enqueue_transaction(&t_sync);
9101da177e4SLinus Torvalds 	down(&s_sync); /* Wait for t_sync to complete */
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	/* Create the keepalive task */
9131da177e4SLinus Torvalds 	init_timer(&hp_sdc.kicker);
9141da177e4SLinus Torvalds 	hp_sdc.kicker.expires = jiffies + HZ;
9151da177e4SLinus Torvalds 	hp_sdc.kicker.function = &hp_sdc_kicker;
9161da177e4SLinus Torvalds 	add_timer(&hp_sdc.kicker);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	hp_sdc.dev_err = 0;
9191da177e4SLinus Torvalds 	return 0;
9201da177e4SLinus Torvalds  err2:
9213acaf540SHelge Deller 	free_irq(hp_sdc.irq, &hp_sdc);
9221da177e4SLinus Torvalds  err1:
9231da177e4SLinus Torvalds 	release_region(hp_sdc.data_io, 2);
9241da177e4SLinus Torvalds  err0:
9251da177e4SLinus Torvalds 	printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
9261da177e4SLinus Torvalds 		errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
9271da177e4SLinus Torvalds 	hp_sdc.dev = NULL;
928ffd51f46SHelge Deller 
9291da177e4SLinus Torvalds 	return hp_sdc.dev_err;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds #if defined(__hppa__)
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds static int __init hp_sdc_init_hppa(struct parisc_device *d)
9351da177e4SLinus Torvalds {
936ffd51f46SHelge Deller 	if (!d)
937ffd51f46SHelge Deller 		return 1;
938ffd51f46SHelge Deller 	if (hp_sdc.dev != NULL)
939ffd51f46SHelge Deller 		return 1;	/* We only expect one SDC */
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	hp_sdc.dev		= d;
9421da177e4SLinus Torvalds 	hp_sdc.irq		= d->irq;
9431da177e4SLinus Torvalds 	hp_sdc.nmi		= d->aux_irq;
94453f01bbaSMatthew Wilcox 	hp_sdc.base_io		= d->hpa.start;
94553f01bbaSMatthew Wilcox 	hp_sdc.data_io		= d->hpa.start + 0x800;
94653f01bbaSMatthew Wilcox 	hp_sdc.status_io	= d->hpa.start + 0x801;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	return hp_sdc_init();
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds #endif /* __hppa__ */
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds static void hp_sdc_exit(void)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	write_lock_irq(&hp_sdc.lock);
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	/* Turn off all maskable "sub-function" irq's. */
9581da177e4SLinus Torvalds 	hp_sdc_spin_ibf();
9591da177e4SLinus Torvalds 	sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	/* Wait until we know this has been processed by the i8042 */
9621da177e4SLinus Torvalds 	hp_sdc_spin_ibf();
9631da177e4SLinus Torvalds 
9643acaf540SHelge Deller 	free_irq(hp_sdc.nmi, &hp_sdc);
9653acaf540SHelge Deller 	free_irq(hp_sdc.irq, &hp_sdc);
9661da177e4SLinus Torvalds 	write_unlock_irq(&hp_sdc.lock);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	del_timer(&hp_sdc.kicker);
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	tasklet_kill(&hp_sdc.task);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds #if defined(__hppa__)
9731da177e4SLinus Torvalds 	if (unregister_parisc_driver(&hp_sdc_driver))
9741da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
9751da177e4SLinus Torvalds #endif
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds static int __init hp_sdc_register(void)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	hp_sdc_transaction tq_init;
9811da177e4SLinus Torvalds 	uint8_t tq_init_seq[5];
9821da177e4SLinus Torvalds 	struct semaphore tq_init_sem;
9831da177e4SLinus Torvalds #if defined(__mc68000__)
9841da177e4SLinus Torvalds 	mm_segment_t fs;
9851da177e4SLinus Torvalds 	unsigned char i;
9861da177e4SLinus Torvalds #endif
9871da177e4SLinus Torvalds 
988305e342bSHelge Deller 	if (hp_sdc_disabled) {
989305e342bSHelge Deller 		printk(KERN_WARNING PREFIX "HP SDC driver disabled by no_hpsdc=1.\n");
990305e342bSHelge Deller 		return -ENODEV;
991305e342bSHelge Deller 	}
992305e342bSHelge Deller 
9931da177e4SLinus Torvalds 	hp_sdc.dev = NULL;
9941da177e4SLinus Torvalds 	hp_sdc.dev_err = 0;
9951da177e4SLinus Torvalds #if defined(__hppa__)
9961da177e4SLinus Torvalds 	if (register_parisc_driver(&hp_sdc_driver)) {
9971da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
9981da177e4SLinus Torvalds 		return -ENODEV;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds #elif defined(__mc68000__)
10011da177e4SLinus Torvalds 	if (!MACH_IS_HP300)
10021da177e4SLinus Torvalds 	    return -ENODEV;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	hp_sdc.irq	 = 1;
10051da177e4SLinus Torvalds 	hp_sdc.nmi	 = 7;
10061da177e4SLinus Torvalds 	hp_sdc.base_io	 = (unsigned long) 0xf0428000;
10071da177e4SLinus Torvalds 	hp_sdc.data_io	 = (unsigned long) hp_sdc.base_io + 1;
10081da177e4SLinus Torvalds 	hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
10091da177e4SLinus Torvalds 	fs = get_fs();
10101da177e4SLinus Torvalds 	set_fs(KERNEL_DS);
10111da177e4SLinus Torvalds 	if (!get_user(i, (unsigned char *)hp_sdc.data_io))
10121da177e4SLinus Torvalds 		hp_sdc.dev = (void *)1;
10131da177e4SLinus Torvalds 	set_fs(fs);
10141da177e4SLinus Torvalds 	hp_sdc.dev_err   = hp_sdc_init();
10151da177e4SLinus Torvalds #endif
10161da177e4SLinus Torvalds 	if (hp_sdc.dev == NULL) {
10171da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "No SDC found.\n");
10181da177e4SLinus Torvalds 		return hp_sdc.dev_err;
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	init_MUTEX_LOCKED(&tq_init_sem);
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	tq_init.actidx		= 0;
10241da177e4SLinus Torvalds 	tq_init.idx		= 1;
10251da177e4SLinus Torvalds 	tq_init.endidx		= 5;
10261da177e4SLinus Torvalds 	tq_init.seq		= tq_init_seq;
10271da177e4SLinus Torvalds 	tq_init.act.semaphore	= &tq_init_sem;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	tq_init_seq[0] =
10301da177e4SLinus Torvalds 		HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
10311da177e4SLinus Torvalds 	tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
10321da177e4SLinus Torvalds 	tq_init_seq[2] = 1;
10331da177e4SLinus Torvalds 	tq_init_seq[3] = 0;
10341da177e4SLinus Torvalds 	tq_init_seq[4] = 0;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	hp_sdc_enqueue_transaction(&tq_init);
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	down(&tq_init_sem);
10391da177e4SLinus Torvalds 	up(&tq_init_sem);
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
10421da177e4SLinus Torvalds 		printk(KERN_WARNING PREFIX "Error reading config byte.\n");
10431da177e4SLinus Torvalds 		hp_sdc_exit();
10441da177e4SLinus Torvalds 		return -ENODEV;
10451da177e4SLinus Torvalds 	}
10461da177e4SLinus Torvalds 	hp_sdc.r11 = tq_init_seq[4];
10471da177e4SLinus Torvalds 	if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
1048ffd51f46SHelge Deller 		const char *str;
10491da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "New style SDC\n");
10501da177e4SLinus Torvalds 		tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
10511da177e4SLinus Torvalds 		tq_init.actidx		= 0;
10521da177e4SLinus Torvalds 		tq_init.idx		= 1;
10531da177e4SLinus Torvalds 		down(&tq_init_sem);
10541da177e4SLinus Torvalds 		hp_sdc_enqueue_transaction(&tq_init);
10551da177e4SLinus Torvalds 		down(&tq_init_sem);
10561da177e4SLinus Torvalds 		up(&tq_init_sem);
10571da177e4SLinus Torvalds 		if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
10581da177e4SLinus Torvalds 			printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
10591da177e4SLinus Torvalds 			return -ENODEV;
10601da177e4SLinus Torvalds 		}
10611da177e4SLinus Torvalds 		hp_sdc.r7e = tq_init_seq[4];
10621da177e4SLinus Torvalds 		HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
10631da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "Revision: %s\n", str);
1064ffd51f46SHelge Deller 		if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
10651da177e4SLinus Torvalds 			printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
1066ffd51f46SHelge Deller 		if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
10671da177e4SLinus Torvalds 			printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
10681da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
10691da177e4SLinus Torvalds 		       "on next firmware reset.\n");
10701da177e4SLinus Torvalds 		tq_init_seq[0] = HP_SDC_ACT_PRECMD |
10711da177e4SLinus Torvalds 			HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
10721da177e4SLinus Torvalds 		tq_init_seq[1] = HP_SDC_CMD_SET_STR;
10731da177e4SLinus Torvalds 		tq_init_seq[2] = 1;
10741da177e4SLinus Torvalds 		tq_init_seq[3] = 0;
10751da177e4SLinus Torvalds 		tq_init.actidx		= 0;
10761da177e4SLinus Torvalds 		tq_init.idx		= 1;
10771da177e4SLinus Torvalds 		tq_init.endidx		= 4;
10781da177e4SLinus Torvalds 		down(&tq_init_sem);
10791da177e4SLinus Torvalds 		hp_sdc_enqueue_transaction(&tq_init);
10801da177e4SLinus Torvalds 		down(&tq_init_sem);
10811da177e4SLinus Torvalds 		up(&tq_init_sem);
1082ffd51f46SHelge Deller 	} else
10831da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
10841da177e4SLinus Torvalds 		       (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds         return 0;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds module_init(hp_sdc_register);
10901da177e4SLinus Torvalds module_exit(hp_sdc_exit);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds /* Timing notes:  These measurements taken on my 64MHz 7100-LC (715/64)
10931da177e4SLinus Torvalds  *                                              cycles cycles-adj    time
10941da177e4SLinus Torvalds  * between two consecutive mfctl(16)'s:              4        n/a    63ns
10951da177e4SLinus Torvalds  * hp_sdc_spin_ibf when idle:                      119        115   1.7us
10961da177e4SLinus Torvalds  * gsc_writeb status register:                      83         79   1.2us
10971da177e4SLinus Torvalds  * IBF to clear after sending SET_IM:             6204       6006    93us
10981da177e4SLinus Torvalds  * IBF to clear after sending LOAD_RT:            4467       4352    68us
10991da177e4SLinus Torvalds  * IBF to clear after sending two LOAD_RTs:      18974      18859   295us
11001da177e4SLinus Torvalds  * READ_T1, read status/data, IRQ, call handler: 35564        n/a   556us
11011da177e4SLinus Torvalds  * cmd to ~IBF READ_T1 2nd time right after:   5158403        n/a    81ms
11021da177e4SLinus Torvalds  * between IRQ received and ~IBF for above:    2578877        n/a    40ms
11031da177e4SLinus Torvalds  *
11041da177e4SLinus Torvalds  * Performance stats after a run of this module configuring HIL and
11051da177e4SLinus Torvalds  * receiving a few mouse events:
11061da177e4SLinus Torvalds  *
11071da177e4SLinus Torvalds  * status in8  282508 cycles 7128 calls
11081da177e4SLinus Torvalds  * status out8   8404 cycles  341 calls
11091da177e4SLinus Torvalds  * data out8     1734 cycles   78 calls
11101da177e4SLinus Torvalds  * isr         174324 cycles  617 calls (includes take)
11111da177e4SLinus Torvalds  * take          1241 cycles    2 calls
11121da177e4SLinus Torvalds  * put        1411504 cycles 6937 calls
11131da177e4SLinus Torvalds  * task       1655209 cycles 6937 calls (includes put)
11141da177e4SLinus Torvalds  *
11151da177e4SLinus Torvalds  */
1116