xref: /openbmc/linux/drivers/input/serio/hp_sdc_mlc.c (revision ffd51f46)
1 /*
2  * Access to HP-HIL MLC through HP System Device Controller.
3  *
4  * Copyright (c) 2001 Brian S. Julin
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL").
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  *
29  * References:
30  * HP-HIL Technical Reference Manual.  Hewlett Packard Product No. 45918A
31  * System Device Controller Microprocessor Firmware Theory of Operation
32  *      for Part Number 1820-4784 Revision B.  Dwg No. A-1820-4784-2
33  *
34  */
35 
36 #include <linux/hil_mlc.h>
37 #include <linux/hp_sdc.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/string.h>
43 #include <asm/semaphore.h>
44 
45 #define PREFIX "HP SDC MLC: "
46 
47 static hil_mlc hp_sdc_mlc;
48 
49 MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
50 MODULE_DESCRIPTION("Glue for onboard HIL MLC in HP-PARISC machines");
51 MODULE_LICENSE("Dual BSD/GPL");
52 
53 struct hp_sdc_mlc_priv_s {
54 	int emtestmode;
55 	hp_sdc_transaction trans;
56 	u8 tseq[16];
57 	int got5x;
58 } hp_sdc_mlc_priv;
59 
60 /************************* Interrupt context ******************************/
61 static void hp_sdc_mlc_isr (int irq, void *dev_id,
62 			    uint8_t status, uint8_t data)
63 {
64 	int idx;
65 	hil_mlc *mlc = &hp_sdc_mlc;
66 
67 	write_lock(&mlc->lock);
68 	if (mlc->icount < 0) {
69 		printk(KERN_WARNING PREFIX "HIL Overflow!\n");
70 		up(&mlc->isem);
71 		goto out;
72 	}
73 	idx = 15 - mlc->icount;
74 	if ((status & HP_SDC_STATUS_IRQMASK) == HP_SDC_STATUS_HILDATA) {
75 		mlc->ipacket[idx] |= data | HIL_ERR_INT;
76 		mlc->icount--;
77 		if (hp_sdc_mlc_priv.got5x || !idx)
78 			goto check;
79 		if ((mlc->ipacket[idx - 1] & HIL_PKT_ADDR_MASK) !=
80 		    (mlc->ipacket[idx] & HIL_PKT_ADDR_MASK)) {
81 			mlc->ipacket[idx] &= ~HIL_PKT_ADDR_MASK;
82 			mlc->ipacket[idx] |= (mlc->ipacket[idx - 1]
83 						& HIL_PKT_ADDR_MASK);
84 		}
85 		goto check;
86 	}
87 	/* We know status is 5X */
88 	if (data & HP_SDC_HIL_ISERR)
89 		goto err;
90 	mlc->ipacket[idx] =
91 		(data & HP_SDC_HIL_R1MASK) << HIL_PKT_ADDR_SHIFT;
92 	hp_sdc_mlc_priv.got5x = 1;
93 	goto out;
94 
95  check:
96 	hp_sdc_mlc_priv.got5x = 0;
97 	if (mlc->imatch == 0)
98 		goto done;
99 	if ((mlc->imatch == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL))
100 	    && (mlc->ipacket[idx] == (mlc->imatch | idx)))
101 		goto done;
102 	if (mlc->ipacket[idx] == mlc->imatch)
103 		goto done;
104 	goto out;
105 
106  err:
107 	printk(KERN_DEBUG PREFIX "err code %x\n", data);
108 
109 	switch (data) {
110 	case HP_SDC_HIL_RC_DONE:
111 		printk(KERN_WARNING PREFIX "Bastard SDC reconfigured loop!\n");
112 		break;
113 
114 	case HP_SDC_HIL_ERR:
115 		mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_PERR |
116 					HIL_ERR_FERR | HIL_ERR_FOF;
117 		break;
118 
119 	case HP_SDC_HIL_TO:
120 		mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_LERR;
121 		break;
122 
123 	case HP_SDC_HIL_RC:
124 		printk(KERN_WARNING PREFIX "Bastard SDC decided to reconfigure loop!\n");
125 		break;
126 
127 	default:
128 		printk(KERN_WARNING PREFIX "Unkown HIL Error status (%x)!\n", data);
129 		break;
130 	}
131 
132 	/* No more data will be coming due to an error. */
133  done:
134 	tasklet_schedule(mlc->tasklet);
135 	up(&mlc->isem);
136  out:
137 	write_unlock(&mlc->lock);
138 }
139 
140 
141 /******************** Tasklet or userspace context functions ****************/
142 
143 static int hp_sdc_mlc_in(hil_mlc *mlc, suseconds_t timeout)
144 {
145 	unsigned long flags;
146 	struct hp_sdc_mlc_priv_s *priv;
147 	int rc = 2;
148 
149 	priv = mlc->priv;
150 
151 	write_lock_irqsave(&mlc->lock, flags);
152 
153 	/* Try to down the semaphore */
154 	if (down_trylock(&mlc->isem)) {
155 		struct timeval tv;
156 		if (priv->emtestmode) {
157 			mlc->ipacket[0] =
158 				HIL_ERR_INT | (mlc->opacket &
159 					       (HIL_PKT_CMD |
160 						HIL_PKT_ADDR_MASK |
161 						HIL_PKT_DATA_MASK));
162 			mlc->icount = 14;
163 			/* printk(KERN_DEBUG PREFIX ">[%x]\n", mlc->ipacket[0]); */
164 			goto wasup;
165 		}
166 		do_gettimeofday(&tv);
167 		tv.tv_usec += USEC_PER_SEC * (tv.tv_sec - mlc->instart.tv_sec);
168 		if (tv.tv_usec - mlc->instart.tv_usec > mlc->intimeout) {
169 			/*	printk("!%i %i",
170 				tv.tv_usec - mlc->instart.tv_usec,
171 				mlc->intimeout);
172 			 */
173 			rc = 1;
174 			up(&mlc->isem);
175 		}
176 		goto done;
177 	}
178  wasup:
179 	up(&mlc->isem);
180 	rc = 0;
181 	goto done;
182  done:
183 	write_unlock_irqrestore(&mlc->lock, flags);
184 	return rc;
185 }
186 
187 static int hp_sdc_mlc_cts(hil_mlc *mlc)
188 {
189 	struct hp_sdc_mlc_priv_s *priv;
190 	unsigned long flags;
191 
192 	priv = mlc->priv;
193 
194 	write_lock_irqsave(&mlc->lock, flags);
195 
196 	/* Try to down the semaphores -- they should be up. */
197 	BUG_ON(down_trylock(&mlc->isem));
198 	BUG_ON(down_trylock(&mlc->osem));
199 
200 	up(&mlc->isem);
201 	up(&mlc->osem);
202 
203 	if (down_trylock(&mlc->csem)) {
204 		if (priv->trans.act.semaphore != &mlc->csem)
205 			goto poll;
206 		else
207 			goto busy;
208 	}
209 
210 	if (!(priv->tseq[4] & HP_SDC_USE_LOOP))
211 		goto done;
212 
213  poll:
214 	priv->trans.act.semaphore = &mlc->csem;
215 	priv->trans.actidx = 0;
216 	priv->trans.idx = 1;
217 	priv->trans.endidx = 5;
218 	priv->tseq[0] =
219 		HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
220 	priv->tseq[1] = HP_SDC_CMD_READ_USE;
221 	priv->tseq[2] = 1;
222 	priv->tseq[3] = 0;
223 	priv->tseq[4] = 0;
224 	hp_sdc_enqueue_transaction(&priv->trans);
225  busy:
226 	write_unlock_irqrestore(&mlc->lock, flags);
227 	return 1;
228  done:
229 	priv->trans.act.semaphore = &mlc->osem;
230 	up(&mlc->csem);
231 	write_unlock_irqrestore(&mlc->lock, flags);
232 	return 0;
233 }
234 
235 static void hp_sdc_mlc_out(hil_mlc *mlc)
236 {
237 	struct hp_sdc_mlc_priv_s *priv;
238 	unsigned long flags;
239 
240 	priv = mlc->priv;
241 
242 	write_lock_irqsave(&mlc->lock, flags);
243 
244 	/* Try to down the semaphore -- it should be up. */
245 	BUG_ON(down_trylock(&mlc->osem));
246 
247 	if (mlc->opacket & HIL_DO_ALTER_CTRL)
248 		goto do_control;
249 
250  do_data:
251 	if (priv->emtestmode) {
252 		up(&mlc->osem);
253 		goto done;
254 	}
255 	/* Shouldn't be sending commands when loop may be busy */
256 	BUG_ON(down_trylock(&mlc->csem));
257 	up(&mlc->csem);
258 
259 	priv->trans.actidx = 0;
260 	priv->trans.idx = 1;
261 	priv->trans.act.semaphore = &mlc->osem;
262 	priv->trans.endidx = 6;
263 	priv->tseq[0] =
264 		HP_SDC_ACT_DATAREG | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_SEMAPHORE;
265 	priv->tseq[1] = 0x7;
266 	priv->tseq[2] =
267 		(mlc->opacket &
268 		 (HIL_PKT_ADDR_MASK | HIL_PKT_CMD))
269 		   >> HIL_PKT_ADDR_SHIFT;
270 	priv->tseq[3] =
271 		(mlc->opacket & HIL_PKT_DATA_MASK)
272 		  >> HIL_PKT_DATA_SHIFT;
273 	priv->tseq[4] = 0;  /* No timeout */
274 	if (priv->tseq[3] == HIL_CMD_DHR)
275 		priv->tseq[4] = 1;
276 	priv->tseq[5] = HP_SDC_CMD_DO_HIL;
277 	goto enqueue;
278 
279  do_control:
280 	priv->emtestmode = mlc->opacket & HIL_CTRL_TEST;
281 
282 	/* we cannot emulate this, it should not be used. */
283 	BUG_ON((mlc->opacket & (HIL_CTRL_APE | HIL_CTRL_IPF)) == HIL_CTRL_APE);
284 
285 	if ((mlc->opacket & HIL_CTRL_ONLY) == HIL_CTRL_ONLY)
286 		goto control_only;
287 
288 	/* Should not send command/data after engaging APE */
289 	BUG_ON(mlc->opacket & HIL_CTRL_APE);
290 
291 	/* Disengaging APE this way would not be valid either since
292 	 * the loop must be allowed to idle.
293 	 *
294 	 * So, it works out that we really never actually send control
295 	 * and data when using SDC, we just send the data.
296 	 */
297 	goto do_data;
298 
299  control_only:
300 	priv->trans.actidx = 0;
301 	priv->trans.idx = 1;
302 	priv->trans.act.semaphore = &mlc->osem;
303 	priv->trans.endidx = 4;
304 	priv->tseq[0] =
305 	  HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
306 	priv->tseq[1] = HP_SDC_CMD_SET_LPC;
307 	priv->tseq[2] = 1;
308 	/* priv->tseq[3] = (mlc->ddc + 1) | HP_SDC_LPS_ACSUCC; */
309 	priv->tseq[3] = 0;
310 	if (mlc->opacket & HIL_CTRL_APE) {
311 		priv->tseq[3] |= HP_SDC_LPC_APE_IPF;
312 		down_trylock(&mlc->csem);
313 	}
314  enqueue:
315 	hp_sdc_enqueue_transaction(&priv->trans);
316  done:
317 	write_unlock_irqrestore(&mlc->lock, flags);
318 }
319 
320 static int __init hp_sdc_mlc_init(void)
321 {
322 	hil_mlc *mlc = &hp_sdc_mlc;
323 
324 	printk(KERN_INFO PREFIX "Registering the System Domain Controller's HIL MLC.\n");
325 
326 	hp_sdc_mlc_priv.emtestmode = 0;
327 	hp_sdc_mlc_priv.trans.seq = hp_sdc_mlc_priv.tseq;
328 	hp_sdc_mlc_priv.trans.act.semaphore = &mlc->osem;
329 	hp_sdc_mlc_priv.got5x = 0;
330 
331 	mlc->cts = &hp_sdc_mlc_cts;
332 	mlc->in	= &hp_sdc_mlc_in;
333 	mlc->out = &hp_sdc_mlc_out;
334 	mlc->priv = &hp_sdc_mlc_priv;
335 
336 	if (hil_mlc_register(mlc)) {
337 		printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
338 		goto err0;
339 	}
340 
341 	if (hp_sdc_request_hil_irq(&hp_sdc_mlc_isr)) {
342 		printk(KERN_WARNING PREFIX "Request for raw HIL ISR hook denied\n");
343 		goto err1;
344 	}
345 	return 0;
346  err1:
347 	if (hil_mlc_unregister(mlc))
348 		printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
349 			"This is bad.  Could cause an oops.\n");
350  err0:
351 	return -EBUSY;
352 }
353 
354 static void __exit hp_sdc_mlc_exit(void)
355 {
356 	hil_mlc *mlc = &hp_sdc_mlc;
357 
358 	if (hp_sdc_release_hil_irq(&hp_sdc_mlc_isr))
359 		printk(KERN_ERR PREFIX "Failed to release the raw HIL ISR hook.\n"
360 			"This is bad.  Could cause an oops.\n");
361 
362 	if (hil_mlc_unregister(mlc))
363 		printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
364 			"This is bad.  Could cause an oops.\n");
365 }
366 
367 module_init(hp_sdc_mlc_init);
368 module_exit(hp_sdc_mlc_exit);
369