blob: cb0b28877e05f65c5d0904f9fc480d4979ae242f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080043#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define PREFIX "HP SDC MLC: "
46
47static hil_mlc hp_sdc_mlc;
48
49MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
50MODULE_DESCRIPTION("Glue for onboard HIL MLC in HP-PARISC machines");
51MODULE_LICENSE("Dual BSD/GPL");
52
53struct 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 ******************************/
Helge Dellerffd51f42007-02-28 23:51:29 -050061static void hp_sdc_mlc_isr (int irq, void *dev_id,
62 uint8_t status, uint8_t data)
63{
64 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 hil_mlc *mlc = &hp_sdc_mlc;
66
Helge Dellerffd51f42007-02-28 23:51:29 -050067 write_lock(&mlc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 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--;
Helge Dellerffd51f42007-02-28 23:51:29 -050077 if (hp_sdc_mlc_priv.got5x || !idx)
78 goto check;
79 if ((mlc->ipacket[idx - 1] & HIL_PKT_ADDR_MASK) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 (mlc->ipacket[idx] & HIL_PKT_ADDR_MASK)) {
81 mlc->ipacket[idx] &= ~HIL_PKT_ADDR_MASK;
Helge Dellerffd51f42007-02-28 23:51:29 -050082 mlc->ipacket[idx] |= (mlc->ipacket[idx - 1]
83 & HIL_PKT_ADDR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 goto check;
86 }
87 /* We know status is 5X */
Helge Dellerffd51f42007-02-28 23:51:29 -050088 if (data & HP_SDC_HIL_ISERR)
89 goto err;
90 mlc->ipacket[idx] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 (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;
Helge Dellerffd51f42007-02-28 23:51:29 -050097 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 goto out;
105
Helge Dellerffd51f42007-02-28 23:51:29 -0500106 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 printk(KERN_DEBUG PREFIX "err code %x\n", data);
Helge Dellerffd51f42007-02-28 23:51:29 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 switch (data) {
110 case HP_SDC_HIL_RC_DONE:
111 printk(KERN_WARNING PREFIX "Bastard SDC reconfigured loop!\n");
112 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 case HP_SDC_HIL_ERR:
Helge Dellerffd51f42007-02-28 23:51:29 -0500115 mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_PERR |
116 HIL_ERR_FERR | HIL_ERR_FOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 case HP_SDC_HIL_TO:
120 mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_LERR;
121 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 case HP_SDC_HIL_RC:
124 printk(KERN_WARNING PREFIX "Bastard SDC decided to reconfigure loop!\n");
125 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 default:
128 printk(KERN_WARNING PREFIX "Unkown HIL Error status (%x)!\n", data);
129 break;
130 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /* No more data will be coming due to an error. */
133 done:
134 tasklet_schedule(mlc->tasklet);
Helge Dellerffd51f42007-02-28 23:51:29 -0500135 up(&mlc->isem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 out:
Helge Dellerffd51f42007-02-28 23:51:29 -0500137 write_unlock(&mlc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140
141/******************** Tasklet or userspace context functions ****************/
142
Helge Dellerffd51f42007-02-28 23:51:29 -0500143static int hp_sdc_mlc_in(hil_mlc *mlc, suseconds_t timeout)
144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned long flags;
146 struct hp_sdc_mlc_priv_s *priv;
147 int rc = 2;
148
149 priv = mlc->priv;
150
Helge Dellerffd51f42007-02-28 23:51:29 -0500151 write_lock_irqsave(&mlc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Try to down the semaphore */
Helge Dellerffd51f42007-02-28 23:51:29 -0500154 if (down_trylock(&mlc->isem)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct timeval tv;
156 if (priv->emtestmode) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500157 mlc->ipacket[0] =
158 HIL_ERR_INT | (mlc->opacket &
159 (HIL_PKT_CMD |
160 HIL_PKT_ADDR_MASK |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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);
Helge Dellerffd51f42007-02-28 23:51:29 -0500167 tv.tv_usec += USEC_PER_SEC * (tv.tv_sec - mlc->instart.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (tv.tv_usec - mlc->instart.tv_usec > mlc->intimeout) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500169 /* printk("!%i %i",
170 tv.tv_usec - mlc->instart.tv_usec,
171 mlc->intimeout);
172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 rc = 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500174 up(&mlc->isem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 goto done;
177 }
178 wasup:
Helge Dellerffd51f42007-02-28 23:51:29 -0500179 up(&mlc->isem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 rc = 0;
181 goto done;
182 done:
Helge Dellerffd51f42007-02-28 23:51:29 -0500183 write_unlock_irqrestore(&mlc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return rc;
185}
186
Helge Dellerffd51f42007-02-28 23:51:29 -0500187static int hp_sdc_mlc_cts(hil_mlc *mlc)
188{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct hp_sdc_mlc_priv_s *priv;
190 unsigned long flags;
191
192 priv = mlc->priv;
193
Helge Dellerffd51f42007-02-28 23:51:29 -0500194 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
Helge Dellerffd51f42007-02-28 23:51:29 -0500210 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
235static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 do_data:
251 if (priv->emtestmode) {
Helge Dellerffd51f42007-02-28 23:51:29 -0500252 up(&mlc->osem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto done;
254 }
255 /* Shouldn't be sending commands when loop may be busy */
Helge Dellerffd51f42007-02-28 23:51:29 -0500256 BUG_ON(down_trylock(&mlc->csem));
257 up(&mlc->csem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 priv->trans.actidx = 0;
260 priv->trans.idx = 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500261 priv->trans.act.semaphore = &mlc->osem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 priv->trans.endidx = 6;
Helge Dellerffd51f42007-02-28 23:51:29 -0500263 priv->tseq[0] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 HP_SDC_ACT_DATAREG | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_SEMAPHORE;
265 priv->tseq[1] = 0x7;
Helge Dellerffd51f42007-02-28 23:51:29 -0500266 priv->tseq[2] =
267 (mlc->opacket &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 (HIL_PKT_ADDR_MASK | HIL_PKT_CMD))
269 >> HIL_PKT_ADDR_SHIFT;
Helge Dellerffd51f42007-02-28 23:51:29 -0500270 priv->tseq[3] =
271 (mlc->opacket & HIL_PKT_DATA_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 >> HIL_PKT_DATA_SHIFT;
273 priv->tseq[4] = 0; /* No timeout */
Helge Dellerffd51f42007-02-28 23:51:29 -0500274 if (priv->tseq[3] == HIL_CMD_DHR)
275 priv->tseq[4] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 priv->tseq[5] = HP_SDC_CMD_DO_HIL;
277 goto enqueue;
278
279 do_control:
280 priv->emtestmode = mlc->opacket & HIL_CTRL_TEST;
Helge Dellerffd51f42007-02-28 23:51:29 -0500281
Eric Sesterhennfddaaae2006-03-26 18:23:47 +0200282 /* we cannot emulate this, it should not be used. */
283 BUG_ON((mlc->opacket & (HIL_CTRL_APE | HIL_CTRL_IPF)) == HIL_CTRL_APE);
Helge Dellerffd51f42007-02-28 23:51:29 -0500284
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * the loop must be allowed to idle.
293 *
Helge Dellerffd51f42007-02-28 23:51:29 -0500294 * So, it works out that we really never actually send control
295 * and data when using SDC, we just send the data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
297 goto do_data;
298
299 control_only:
300 priv->trans.actidx = 0;
301 priv->trans.idx = 1;
Helge Dellerffd51f42007-02-28 23:51:29 -0500302 priv->trans.act.semaphore = &mlc->osem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 priv->trans.endidx = 4;
Helge Dellerffd51f42007-02-28 23:51:29 -0500304 priv->tseq[0] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 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;
Helge Dellerffd51f42007-02-28 23:51:29 -0500308 /* priv->tseq[3] = (mlc->ddc + 1) | HP_SDC_LPS_ACSUCC; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 priv->tseq[3] = 0;
310 if (mlc->opacket & HIL_CTRL_APE) {
311 priv->tseq[3] |= HP_SDC_LPC_APE_IPF;
Helge Dellerffd51f42007-02-28 23:51:29 -0500312 down_trylock(&mlc->csem);
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 enqueue:
Helge Dellerffd51f42007-02-28 23:51:29 -0500315 hp_sdc_enqueue_transaction(&priv->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 done:
Helge Dellerffd51f42007-02-28 23:51:29 -0500317 write_unlock_irqrestore(&mlc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320static 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;
Helge Dellerffd51f42007-02-28 23:51:29 -0500328 hp_sdc_mlc_priv.trans.act.semaphore = &mlc->osem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 hp_sdc_mlc_priv.got5x = 0;
330
Helge Dellerffd51f42007-02-28 23:51:29 -0500331 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;
Helge Deller3acaf542007-02-28 23:51:19 -0500335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (hil_mlc_register(mlc)) {
337 printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
338 goto err0;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
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:
Helge Dellerffd51f42007-02-28 23:51:29 -0500347 if (hil_mlc_unregister(mlc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
349 "This is bad. Could cause an oops.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 err0:
351 return -EBUSY;
352}
353
354static void __exit hp_sdc_mlc_exit(void)
355{
356 hil_mlc *mlc = &hp_sdc_mlc;
Helge Dellerffd51f42007-02-28 23:51:29 -0500357
358 if (hp_sdc_release_hil_irq(&hp_sdc_mlc_isr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 printk(KERN_ERR PREFIX "Failed to release the raw HIL ISR hook.\n"
360 "This is bad. Could cause an oops.\n");
Helge Dellerffd51f42007-02-28 23:51:29 -0500361
362 if (hil_mlc_unregister(mlc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
364 "This is bad. Could cause an oops.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367module_init(hp_sdc_mlc_init);
368module_exit(hp_sdc_mlc_exit);