Merge branch 'master'
[linux-2.6] / drivers / input / serio / hp_sdc_mlc.c
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         int idx;
64         hil_mlc *mlc = &hp_sdc_mlc;
65
66         write_lock(&(mlc->lock));
67         if (mlc->icount < 0) {
68                 printk(KERN_WARNING PREFIX "HIL Overflow!\n");
69                 up(&mlc->isem);
70                 goto out;
71         }
72         idx = 15 - mlc->icount;
73         if ((status & HP_SDC_STATUS_IRQMASK) == HP_SDC_STATUS_HILDATA) {
74                 mlc->ipacket[idx] |= data | HIL_ERR_INT;
75                 mlc->icount--;
76                 if (hp_sdc_mlc_priv.got5x) goto check;
77                 if (!idx) goto check;
78                 if ((mlc->ipacket[idx-1] & HIL_PKT_ADDR_MASK) !=
79                     (mlc->ipacket[idx] & HIL_PKT_ADDR_MASK)) {
80                         mlc->ipacket[idx] &= ~HIL_PKT_ADDR_MASK;
81                         mlc->ipacket[idx] |= (mlc->ipacket[idx-1] 
82                                                     & HIL_PKT_ADDR_MASK);
83                 }
84                 goto check;
85         }
86         /* We know status is 5X */
87         if (data & HP_SDC_HIL_ISERR) goto err;
88         mlc->ipacket[idx] = 
89                 (data & HP_SDC_HIL_R1MASK) << HIL_PKT_ADDR_SHIFT;
90         hp_sdc_mlc_priv.got5x = 1;
91         goto out;
92
93  check:
94         hp_sdc_mlc_priv.got5x = 0;
95         if (mlc->imatch == 0) goto done;
96         if ((mlc->imatch == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL)) 
97             && (mlc->ipacket[idx] == (mlc->imatch | idx))) goto done;
98         if (mlc->ipacket[idx] == mlc->imatch) goto done;
99         goto out;
100
101  err:                           
102         printk(KERN_DEBUG PREFIX "err code %x\n", data);
103         switch (data) {
104         case HP_SDC_HIL_RC_DONE:
105                 printk(KERN_WARNING PREFIX "Bastard SDC reconfigured loop!\n");
106                 break;
107         case HP_SDC_HIL_ERR:
108                 mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_PERR | 
109                   HIL_ERR_FERR | HIL_ERR_FOF;
110                 break;
111         case HP_SDC_HIL_TO:
112                 mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_LERR;
113                 break;
114         case HP_SDC_HIL_RC:
115                 printk(KERN_WARNING PREFIX "Bastard SDC decided to reconfigure loop!\n");
116                 break;
117         default:
118                 printk(KERN_WARNING PREFIX "Unkown HIL Error status (%x)!\n", data);
119                 break;
120         }
121         /* No more data will be coming due to an error. */
122  done:
123         tasklet_schedule(mlc->tasklet);
124         up(&(mlc->isem));
125  out:
126         write_unlock(&(mlc->lock));
127 }
128
129
130 /******************** Tasklet or userspace context functions ****************/
131
132 static int hp_sdc_mlc_in (hil_mlc *mlc, suseconds_t timeout) {
133         unsigned long flags;
134         struct hp_sdc_mlc_priv_s *priv;
135         int rc = 2;
136
137         priv = mlc->priv;
138
139         write_lock_irqsave(&(mlc->lock), flags);
140
141         /* Try to down the semaphore */
142         if (down_trylock(&(mlc->isem))) {
143                 struct timeval tv;
144                 if (priv->emtestmode) {
145                         mlc->ipacket[0] = 
146                                 HIL_ERR_INT | (mlc->opacket & 
147                                                (HIL_PKT_CMD | 
148                                                 HIL_PKT_ADDR_MASK | 
149                                                 HIL_PKT_DATA_MASK));
150                         mlc->icount = 14;
151                         /* printk(KERN_DEBUG PREFIX ">[%x]\n", mlc->ipacket[0]); */
152                         goto wasup;
153                 }
154                 do_gettimeofday(&tv);
155                 tv.tv_usec += 1000000 * (tv.tv_sec - mlc->instart.tv_sec);
156                 if (tv.tv_usec - mlc->instart.tv_usec > mlc->intimeout) {
157                   /*              printk("!%i %i", 
158                                   tv.tv_usec - mlc->instart.tv_usec, 
159                                   mlc->intimeout);
160                   */
161                         rc = 1;
162                         up(&(mlc->isem));
163                 }
164                 goto done;
165         }
166  wasup:
167         up(&(mlc->isem));
168         rc = 0;
169         goto done;
170  done:
171         write_unlock_irqrestore(&(mlc->lock), flags);
172         return rc;
173 }
174
175 static int hp_sdc_mlc_cts (hil_mlc *mlc) {
176         struct hp_sdc_mlc_priv_s *priv;
177         unsigned long flags;
178
179         priv = mlc->priv;       
180
181         write_lock_irqsave(&(mlc->lock), flags);
182
183         /* Try to down the semaphores -- they should be up. */
184         if (down_trylock(&(mlc->isem))) {
185                 BUG();
186                 goto busy;
187         }
188         if (down_trylock(&(mlc->osem))) {
189                 BUG();
190                 up(&(mlc->isem));
191                 goto busy;
192         }
193         up(&(mlc->isem));
194         up(&(mlc->osem));
195
196         if (down_trylock(&(mlc->csem))) {
197                 if (priv->trans.act.semaphore != &(mlc->csem)) goto poll;
198                 goto busy;
199         }
200         if (!(priv->tseq[4] & HP_SDC_USE_LOOP)) goto done;
201
202  poll:
203         priv->trans.act.semaphore = &(mlc->csem);
204         priv->trans.actidx = 0;
205         priv->trans.idx = 1;
206         priv->trans.endidx = 5;
207         priv->tseq[0] = 
208                 HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
209         priv->tseq[1] = HP_SDC_CMD_READ_USE;
210         priv->tseq[2] = 1;
211         priv->tseq[3] = 0;
212         priv->tseq[4] = 0;
213         hp_sdc_enqueue_transaction(&(priv->trans));
214  busy:
215         write_unlock_irqrestore(&(mlc->lock), flags);
216         return 1;
217  done:
218         priv->trans.act.semaphore = &(mlc->osem);
219         up(&(mlc->csem));
220         write_unlock_irqrestore(&(mlc->lock), flags);
221         return 0;
222 }
223
224 static void hp_sdc_mlc_out (hil_mlc *mlc) {
225         struct hp_sdc_mlc_priv_s *priv;
226         unsigned long flags;
227
228         priv = mlc->priv;
229
230         write_lock_irqsave(&(mlc->lock), flags);
231         
232         /* Try to down the semaphore -- it should be up. */
233         if (down_trylock(&(mlc->osem))) {
234                 BUG();
235                 goto done;
236         }
237
238         if (mlc->opacket & HIL_DO_ALTER_CTRL) goto do_control;
239
240  do_data:
241         if (priv->emtestmode) {
242                 up(&(mlc->osem));
243                 goto done;
244         }
245         /* Shouldn't be sending commands when loop may be busy */
246         if (down_trylock(&(mlc->csem))) {
247                 BUG();
248                 goto done;
249         }
250         up(&(mlc->csem));
251
252         priv->trans.actidx = 0;
253         priv->trans.idx = 1;
254         priv->trans.act.semaphore = &(mlc->osem);
255         priv->trans.endidx = 6;
256         priv->tseq[0] = 
257                 HP_SDC_ACT_DATAREG | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_SEMAPHORE;
258         priv->tseq[1] = 0x7;
259         priv->tseq[2] = 
260                 (mlc->opacket & 
261                  (HIL_PKT_ADDR_MASK | HIL_PKT_CMD))
262                    >> HIL_PKT_ADDR_SHIFT;
263         priv->tseq[3] = 
264                 (mlc->opacket & HIL_PKT_DATA_MASK) 
265                   >> HIL_PKT_DATA_SHIFT;
266         priv->tseq[4] = 0;  /* No timeout */
267         if (priv->tseq[3] == HIL_CMD_DHR) priv->tseq[4] = 1;
268         priv->tseq[5] = HP_SDC_CMD_DO_HIL;
269         goto enqueue;
270
271  do_control:
272         priv->emtestmode = mlc->opacket & HIL_CTRL_TEST;
273         if ((mlc->opacket & (HIL_CTRL_APE | HIL_CTRL_IPF)) == HIL_CTRL_APE) {
274                 BUG(); /* we cannot emulate this, it should not be used. */
275         }
276         if ((mlc->opacket & HIL_CTRL_ONLY) == HIL_CTRL_ONLY) goto control_only;
277         if (mlc->opacket & HIL_CTRL_APE) { 
278                 BUG(); /* Should not send command/data after engaging APE */
279                 goto done;
280         }
281         /* Disengaging APE this way would not be valid either since 
282          * the loop must be allowed to idle.
283          *
284          * So, it works out that we really never actually send control 
285          * and data when using SDC, we just send the data. 
286          */
287         goto do_data;
288
289  control_only:
290         priv->trans.actidx = 0;
291         priv->trans.idx = 1;
292         priv->trans.act.semaphore = &(mlc->osem);
293         priv->trans.endidx = 4;
294         priv->tseq[0] = 
295           HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
296         priv->tseq[1] = HP_SDC_CMD_SET_LPC;
297         priv->tseq[2] = 1;
298         //      priv->tseq[3] = (mlc->ddc + 1) | HP_SDC_LPS_ACSUCC;
299         priv->tseq[3] = 0;
300         if (mlc->opacket & HIL_CTRL_APE) {
301                 priv->tseq[3] |= HP_SDC_LPC_APE_IPF;
302                 down_trylock(&(mlc->csem));
303         } 
304  enqueue:
305         hp_sdc_enqueue_transaction(&(priv->trans));
306  done:
307         write_unlock_irqrestore(&(mlc->lock), flags);
308 }
309
310 static int __init hp_sdc_mlc_init(void)
311 {
312         hil_mlc *mlc = &hp_sdc_mlc;
313
314         printk(KERN_INFO PREFIX "Registering the System Domain Controller's HIL MLC.\n");
315
316         hp_sdc_mlc_priv.emtestmode = 0;
317         hp_sdc_mlc_priv.trans.seq = hp_sdc_mlc_priv.tseq;
318         hp_sdc_mlc_priv.trans.act.semaphore = &(mlc->osem);
319         hp_sdc_mlc_priv.got5x = 0;
320
321         mlc->cts                = &hp_sdc_mlc_cts;
322         mlc->in                 = &hp_sdc_mlc_in;
323         mlc->out                = &hp_sdc_mlc_out;
324
325         if (hil_mlc_register(mlc)) {
326                 printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
327                 goto err0;
328         }
329         mlc->priv               = &hp_sdc_mlc_priv;
330
331         if (hp_sdc_request_hil_irq(&hp_sdc_mlc_isr)) {
332                 printk(KERN_WARNING PREFIX "Request for raw HIL ISR hook denied\n");
333                 goto err1;
334         }
335         return 0;
336  err1:
337         if (hil_mlc_unregister(mlc)) {
338                 printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
339                         "This is bad.  Could cause an oops.\n");
340         }
341  err0:
342         return -EBUSY;
343 }
344
345 static void __exit hp_sdc_mlc_exit(void)
346 {
347         hil_mlc *mlc = &hp_sdc_mlc;
348         if (hp_sdc_release_hil_irq(&hp_sdc_mlc_isr)) {
349                 printk(KERN_ERR PREFIX "Failed to release the raw HIL ISR hook.\n"
350                         "This is bad.  Could cause an oops.\n");
351         }
352         if (hil_mlc_unregister(mlc)) {
353                 printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
354                         "This is bad.  Could cause an oops.\n");
355         }
356 }
357
358 module_init(hp_sdc_mlc_init);
359 module_exit(hp_sdc_mlc_exit);