summaryrefslogtreecommitdiffstats
path: root/drivers/input/serio/hp_sdc_mlc.c
blob: aa4a8a4ccfdbed17d4c9ce5c4c58ba32cb45f3cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Access to HP-HIL MLC through HP System Device Controller.
 *
 * Copyright (c) 2001 Brian S. Julin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL").
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *
 * References:
 * HP-HIL Technical Reference Manual.  Hewlett Packard Product No. 45918A
 * System Device Controller Microprocessor Firmware Theory of Operation
 *      for Part Number 1820-4784 Revision B.  Dwg No. A-1820-4784-2
 *
 */

#include <linux/hil_mlc.h>
#include <linux/hp_sdc.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <asm/semaphore.h>

#define PREFIX "HP SDC MLC: "

static hil_mlc hp_sdc_mlc;

MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
MODULE_DESCRIPTION("Glue for onboard HIL MLC in HP-PARISC machines");
MODULE_LICENSE("Dual BSD/GPL");

struct hp_sdc_mlc_priv_s {
	int emtestmode;
	hp_sdc_transaction trans;
	u8 tseq[16];
	int got5x;
} hp_sdc_mlc_priv;

/************************* Interrupt context ******************************/
static void hp_sdc_mlc_isr (int irq, void *dev_id, 
			    uint8_t status, uint8_t data) {
  	int idx;
	hil_mlc *mlc = &hp_sdc_mlc;

	write_lock(&(mlc->lock));
	if (mlc->icount < 0) {
		printk(KERN_WARNING PREFIX "HIL Overflow!\n");
		up(&mlc->isem);
		goto out;
	}
	idx = 15 - mlc->icount;
	if ((status & HP_SDC_STATUS_IRQMASK) == HP_SDC_STATUS_HILDATA) {
		mlc->ipacket[idx] |= data | HIL_ERR_INT;
		mlc->icount--;
		if (hp_sdc_mlc_priv.got5x) goto check;
		if (!idx) goto check;
		if ((mlc->ipacket[idx-1] & HIL_PKT_ADDR_MASK) !=
		    (mlc->ipacket[idx] & HIL_PKT_ADDR_MASK)) {
			mlc->ipacket[idx] &= ~HIL_PKT_ADDR_MASK;
			mlc->ipacket[idx] |= (mlc->ipacket[idx-1] 
						    & HIL_PKT_ADDR_MASK);
		}
		goto check;
	}
	/* We know status is 5X */
	if (data & HP_SDC_HIL_ISERR) goto err;
	mlc->ipacket[idx] = 
		(data & HP_SDC_HIL_R1MASK) << HIL_PKT_ADDR_SHIFT;
	hp_sdc_mlc_priv.got5x = 1;
	goto out;

 check:
	hp_sdc_mlc_priv.got5x = 0;
	if (mlc->imatch == 0) goto done;
	if ((mlc->imatch == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL)) 
	    && (mlc->ipacket[idx] == (mlc->imatch | idx))) goto done;
	if (mlc->ipacket[idx] == mlc->imatch) goto done;
	goto out;

 err:				
	printk(KERN_DEBUG PREFIX "err code %x\n", data);
	switch (data) {
	case HP_SDC_HIL_RC_DONE:
		printk(KERN_WARNING PREFIX "Bastard SDC reconfigured loop!\n");
		break;
	case HP_SDC_HIL_ERR:
		mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_PERR | 
		  HIL_ERR_FERR | HIL_ERR_FOF;
		break;
	case HP_SDC_HIL_TO:
		mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_LERR;
		break;
	case HP_SDC_HIL_RC:
		printk(KERN_WARNING PREFIX "Bastard SDC decided to reconfigure loop!\n");
		break;
	default:
		printk(KERN_WARNING PREFIX "Unkown HIL Error status (%x)!\n", data);
		break;
	}
	/* No more data will be coming due to an error. */
 done:
	tasklet_schedule(mlc->tasklet);
	up(&(mlc->isem));
 out:
	write_unlock(&(mlc->lock));
}


/******************** Tasklet or userspace context functions ****************/

static int hp_sdc_mlc_in (hil_mlc *mlc, suseconds_t timeout) {
	unsigned long flags;
	struct hp_sdc_mlc_priv_s *priv;
	int rc = 2;

	priv = mlc->priv;

	write_lock_irqsave(&(mlc->lock), flags);

	/* Try to down the semaphore */
	if (down_trylock(&(mlc->isem))) {
		struct timeval tv;
		if (priv->emtestmode) {
			mlc->ipacket[0] = 
				HIL_ERR_INT | (mlc->opacket & 
					       (HIL_PKT_CMD | 
						HIL_PKT_ADDR_MASK | 
						HIL_PKT_DATA_MASK));
			mlc->icount = 14;
			/* printk(KERN_DEBUG PREFIX ">[%x]\n", mlc->ipacket[0]); */
			goto wasup;
		}
		do_gettimeofday(&tv);
		tv.tv_usec += 1000000 * (tv.tv_sec - mlc->instart.tv_sec);
		if (tv.tv_usec - mlc->instart.tv_usec > mlc->intimeout) {
		  /*		  printk("!%i %i", 
				  tv.tv_usec - mlc->instart.tv_usec, 
				  mlc->intimeout);
		  */
			rc = 1;
			up(&(mlc->isem));
		}
		goto done;
	}
 wasup:
	up(&(mlc->isem));
	rc = 0;
	goto done;
 done:
	write_unlock_irqrestore(&(mlc->lock), flags);
	return rc;
}

static int hp_sdc_mlc_cts (hil_mlc *mlc) {
	struct hp_sdc_mlc_priv_s *priv;
	unsigned long flags;

	priv = mlc->priv;	

	write_lock_irqsave(&(mlc->lock), flags);

	/* Try to down the semaphores -- they should be up. */
	if (down_trylock(&(mlc->isem))) {
		BUG();
		goto busy;
	}
	if (down_trylock(&(mlc->osem))) {
	 	BUG();
		up(&(mlc->isem));
		goto busy;
	}
	up(&(mlc->isem));
	up(&(mlc->osem));

	if (down_trylock(&(mlc->csem))) {
		if (priv->trans.act.semaphore != &(mlc->csem)) goto poll;
		goto busy;
	}
	if (!(priv->tseq[4] & HP_SDC_USE_LOOP)) goto done;

 poll:
	priv->trans.act.semaphore = &(mlc->csem);
	priv->trans.actidx = 0;
	priv->trans.idx = 1;
	priv->trans.endidx = 5;
	priv->tseq[0] = 
		HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
	priv->tseq[1] = HP_SDC_CMD_READ_USE;
	priv->tseq[2] = 1;
	priv->tseq[3] = 0;
	priv->tseq[4] = 0;
	hp_sdc_enqueue_transaction(&(priv->trans));
 busy:
	write_unlock_irqrestore(&(mlc->lock), flags);
	return 1;
 done:
	priv->trans.act.semaphore = &(mlc->osem);
	up(&(mlc->csem));
	write_unlock_irqrestore(&(mlc->lock), flags);
	return 0;
}

static void hp_sdc_mlc_out (hil_mlc *mlc) {
	struct hp_sdc_mlc_priv_s *priv;
	unsigned long flags;

	priv = mlc->priv;

	write_lock_irqsave(&(mlc->lock), flags);
	
	/* Try to down the semaphore -- it should be up. */
	if (down_trylock(&(mlc->osem))) {
	 	BUG();
		goto done;
	}

	if (mlc->opacket & HIL_DO_ALTER_CTRL) goto do_control;

 do_data:
	if (priv->emtestmode) {
		up(&(mlc->osem));
		goto done;
	}
	/* Shouldn't be sending commands when loop may be busy */
	if (down_trylock(&(mlc->csem))) {
	 	BUG();
		goto done;
	}
	up(&(mlc->csem));

	priv->trans.actidx = 0;
	priv->trans.idx = 1;
	priv->trans.act.semaphore = &(mlc->osem);
	priv->trans.endidx = 6;
	priv->tseq[0] = 
		HP_SDC_ACT_DATAREG | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_SEMAPHORE;
	priv->tseq[1] = 0x7;
	priv->tseq[2] = 
		(mlc->opacket & 
		 (HIL_PKT_ADDR_MASK | HIL_PKT_CMD))
		   >> HIL_PKT_ADDR_SHIFT;
	priv->tseq[3] = 
		(mlc->opacket & HIL_PKT_DATA_MASK) 
		  >> HIL_PKT_DATA_SHIFT;
	priv->tseq[4] = 0;  /* No timeout */
	if (priv->tseq[3] == HIL_CMD_DHR) priv->tseq[4] = 1;
	priv->tseq[5] = HP_SDC_CMD_DO_HIL;
	goto enqueue;

 do_control:
	priv->emtestmode = mlc->opacket & HIL_CTRL_TEST;
	
	/* we cannot emulate this, it should not be used. */
	BUG_ON((mlc->opacket & (HIL_CTRL_APE | HIL_CTRL_IPF)) == HIL_CTRL_APE);
	
	if ((mlc->opacket & HIL_CTRL_ONLY) == HIL_CTRL_ONLY) goto control_only;
	if (mlc->opacket & HIL_CTRL_APE) { 
		BUG(); /* Should not send command/data after engaging APE */
		goto done;
	}
	/* Disengaging APE this way would not be valid either since 
	 * the loop must be allowed to idle.
	 *
	 * So, it works out that we really never actually send control 
	 * and data when using SDC, we just send the data. 
	 */
	goto do_data;

 control_only:
	priv->trans.actidx = 0;
	priv->trans.idx = 1;
	priv->trans.act.semaphore = &(mlc->osem);
	priv->trans.endidx = 4;
	priv->tseq[0] = 
	  HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
	priv->tseq[1] = HP_SDC_CMD_SET_LPC;
	priv->tseq[2] = 1;
	//	priv->tseq[3] = (mlc->ddc + 1) | HP_SDC_LPS_ACSUCC;
	priv->tseq[3] = 0;
	if (mlc->opacket & HIL_CTRL_APE) {
		priv->tseq[3] |= HP_SDC_LPC_APE_IPF;
		down_trylock(&(mlc->csem));
	} 
 enqueue:
	hp_sdc_enqueue_transaction(&(priv->trans));
 done:
	write_unlock_irqrestore(&(mlc->lock), flags);
}

static int __init hp_sdc_mlc_init(void)
{
	hil_mlc *mlc = &hp_sdc_mlc;

	printk(KERN_INFO PREFIX "Registering the System Domain Controller's HIL MLC.\n");

	hp_sdc_mlc_priv.emtestmode = 0;
	hp_sdc_mlc_priv.trans.seq = hp_sdc_mlc_priv.tseq;
	hp_sdc_mlc_priv.trans.act.semaphore = &(mlc->osem);
	hp_sdc_mlc_priv.got5x = 0;

	mlc->cts		= &hp_sdc_mlc_cts;
	mlc->in			= &hp_sdc_mlc_in;
	mlc->out		= &hp_sdc_mlc_out;

	if (hil_mlc_register(mlc)) {
		printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
		goto err0;
	}
	mlc->priv		= &hp_sdc_mlc_priv;

	if (hp_sdc_request_hil_irq(&hp_sdc_mlc_isr)) {
		printk(KERN_WARNING PREFIX "Request for raw HIL ISR hook denied\n");
		goto err1;
	}
	return 0;
 err1:
	if (hil_mlc_unregister(mlc)) {
		printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
			"This is bad.  Could cause an oops.\n");
	}
 err0:
	return -EBUSY;
}

static void __exit hp_sdc_mlc_exit(void)
{
	hil_mlc *mlc = &hp_sdc_mlc;
	if (hp_sdc_release_hil_irq(&hp_sdc_mlc_isr)) {
		printk(KERN_ERR PREFIX "Failed to release the raw HIL ISR hook.\n"
			"This is bad.  Could cause an oops.\n");
	}
	if (hil_mlc_unregister(mlc)) {
		printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
			"This is bad.  Could cause an oops.\n");
	}
}

module_init(hp_sdc_mlc_init);
module_exit(hp_sdc_mlc_exit);