No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / dev / nhpib.c
blob3026aff11b6dff194da4a74a0d04f4e42c26f010
1 /* $NetBSD: nhpib.c,v 1.39 2008/03/29 06:47:08 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1982, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
60 * @(#)nhpib.c 8.2 (Berkeley) 1/12/94
64 * Internal/98624 HPIB driver
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: nhpib.c,v 1.39 2008/03/29 06:47:08 tsutsui Exp $");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/callout.h>
73 #include <sys/kernel.h>
74 #include <sys/buf.h>
75 #include <sys/device.h>
77 #include <machine/bus.h>
79 #include <hp300/dev/intiovar.h>
80 #include <hp300/dev/diovar.h>
81 #include <hp300/dev/diodevs.h>
82 #include <hp300/dev/dmavar.h>
84 #include <hp300/dev/nhpibreg.h>
85 #include <hp300/dev/hpibvar.h>
88 * ODD parity table for listen and talk addresses and secondary commands.
89 * The TI9914A doesn't produce the parity bit.
91 static const u_char listnr_par[] = {
92 0040,0241,0242,0043,0244,0045,0046,0247,
93 0250,0051,0052,0253,0054,0255,0256,0057,
94 0260,0061,0062,0263,0064,0265,0266,0067,
95 0070,0271,0272,0073,0274,0075,0076,0277,
97 static const u_char talker_par[] = {
98 0100,0301,0302,0103,0304,0105,0106,0307,
99 0310,0111,0112,0313,0114,0315,0316,0117,
100 0320,0121,0122,0323,0124,0325,0326,0127,
101 0130,0331,0332,0133,0334,0135,0136,0337,
103 static const u_char sec_par[] = {
104 0340,0141,0142,0343,0144,0345,0346,0147,
105 0150,0351,0352,0153,0354,0155,0156,0357,
106 0160,0361,0362,0163,0364,0165,0166,0367,
107 0370,0171,0172,0373,0174,0375,0376,0177
110 static void nhpibifc(struct nhpibdevice *);
111 static void nhpibreadtimo(void *);
112 static int nhpibwait(struct nhpibdevice *, int);
114 static void nhpibreset(struct hpibbus_softc *);
115 static int nhpibsend(struct hpibbus_softc *, int, int, void *, int);
116 static int nhpibrecv(struct hpibbus_softc *, int, int, void *, int);
117 static int nhpibppoll(struct hpibbus_softc *);
118 static void nhpibppwatch(void *);
119 static void nhpibgo(struct hpibbus_softc *, int, int, void *, int, int,
120 int);
121 static void nhpibdone(struct hpibbus_softc *);
122 static int nhpibintr(void *);
125 * Our controller ops structure.
127 static struct hpib_controller nhpib_controller = {
128 nhpibreset,
129 nhpibsend,
130 nhpibrecv,
131 nhpibppoll,
132 nhpibppwatch,
133 nhpibgo,
134 nhpibdone,
135 nhpibintr
138 struct nhpib_softc {
139 device_t sc_dev; /* generic device glue */
141 bus_space_tag_t sc_bst;
142 bus_space_handle_t sc_bsh;
144 struct nhpibdevice *sc_regs; /* device registers */
145 struct hpibbus_softc *sc_hpibbus; /* XXX */
147 int sc_myaddr;
148 int sc_type;
150 struct callout sc_read_ch;
151 struct callout sc_ppwatch_ch;
154 static int nhpib_dio_match(device_t, cfdata_t, void *);
155 static void nhpib_dio_attach(device_t, device_t, void *);
156 static int nhpib_intio_match(device_t, cfdata_t, void *);
157 static void nhpib_intio_attach(device_t, device_t, void *);
159 static void nhpib_common_attach(struct nhpib_softc *, const char *);
161 CFATTACH_DECL_NEW(nhpib_dio, sizeof(struct nhpib_softc),
162 nhpib_dio_match, nhpib_dio_attach, NULL, NULL);
164 CFATTACH_DECL_NEW(nhpib_intio, sizeof(struct nhpib_softc),
165 nhpib_intio_match, nhpib_intio_attach, NULL, NULL);
167 static int
168 nhpib_intio_match(device_t parent, cfdata_t cf, void *aux)
170 struct intio_attach_args *ia = aux;
172 if (strcmp("hpib", ia->ia_modname) == 0)
173 return 1;
175 return 0;
178 static int
179 nhpib_dio_match(device_t parent, cfdata_t cf, void *aux)
181 struct dio_attach_args *da = aux;
183 if (da->da_id == DIO_DEVICE_ID_NHPIB)
184 return 1;
186 return 0;
189 static void
190 nhpib_intio_attach(device_t parent, device_t self, void *aux)
192 struct nhpib_softc *sc = device_private(self);
193 struct intio_attach_args *ia = aux;
194 bus_space_tag_t bst = ia->ia_bst;
195 const char *desc = "internal HP-IB";
197 sc->sc_dev = self;
198 if (bus_space_map(bst, ia->ia_iobase, INTIO_DEVSIZE, 0, &sc->sc_bsh)) {
199 aprint_error(": can't map registers\n");
200 return;
203 sc->sc_bst = bst;
204 sc->sc_myaddr = HPIBA_BA;
205 sc->sc_type = HPIBA;
207 nhpib_common_attach(sc, desc);
209 /* establish the interrupt handler */
210 (void)intio_intr_establish(nhpibintr, sc, ia->ia_ipl, IPL_BIO);
213 static void
214 nhpib_dio_attach(device_t parent, device_t self, void *aux)
216 struct nhpib_softc *sc = device_private(self);
217 struct dio_attach_args *da = aux;
218 bus_space_tag_t bst = da->da_bst;
219 const char *desc = DIO_DEVICE_DESC_NHPIB;
221 sc->sc_dev = self;
222 if (bus_space_map(bst, da->da_addr, da->da_size, 0, &sc->sc_bsh)) {
223 aprint_error(": can't map registers\n");
224 return;
227 sc->sc_dev = self;
228 sc->sc_bst = bst;
229 /* read address off switches */
230 sc->sc_myaddr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, 5);
231 sc->sc_type = HPIBB;
233 nhpib_common_attach(sc, desc);
235 /* establish the interrupt handler */
236 (void)dio_intr_establish(nhpibintr, sc, da->da_ipl, IPL_BIO);
239 static void
240 nhpib_common_attach(struct nhpib_softc *sc, const char *desc)
242 struct hpibdev_attach_args ha;
244 aprint_normal(": %s\n", desc);
246 sc->sc_regs = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
248 callout_init(&sc->sc_read_ch, 0);
249 callout_init(&sc->sc_ppwatch_ch, 0);
251 ha.ha_ops = &nhpib_controller;
252 ha.ha_type = sc->sc_type; /* XXX */
253 ha.ha_ba = sc->sc_myaddr;
254 ha.ha_softcpp = &sc->sc_hpibbus; /* XXX */
255 (void)config_found(sc->sc_dev, &ha, hpibdevprint);
258 static void
259 nhpibreset(struct hpibbus_softc *hs)
261 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
262 struct nhpibdevice *hd = sc->sc_regs;
264 hd->hpib_acr = AUX_SSWRST;
265 hd->hpib_ar = hs->sc_ba;
266 hd->hpib_lim = LIS_ERR;
267 hd->hpib_mim = 0;
268 hd->hpib_acr = AUX_CDAI;
269 hd->hpib_acr = AUX_CSHDW;
270 hd->hpib_acr = AUX_SSTD1;
271 hd->hpib_acr = AUX_SVSTD1;
272 hd->hpib_acr = AUX_CPP;
273 hd->hpib_acr = AUX_CHDFA;
274 hd->hpib_acr = AUX_CHDFE;
275 hd->hpib_acr = AUX_RHDF;
276 hd->hpib_acr = AUX_CSWRST;
277 nhpibifc(hd);
278 hd->hpib_ie = IDS_IE;
279 hd->hpib_data = C_DCL_P;
280 DELAY(100000);
283 static void
284 nhpibifc(struct nhpibdevice *hd)
287 hd->hpib_acr = AUX_TCA;
288 hd->hpib_acr = AUX_CSRE;
289 hd->hpib_acr = AUX_SSIC;
290 DELAY(100);
291 hd->hpib_acr = AUX_CSIC;
292 hd->hpib_acr = AUX_SSRE;
295 static int
296 nhpibsend(struct hpibbus_softc *hs, int slave, int sec, void *ptr, int origcnt)
298 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
299 struct nhpibdevice *hd = sc->sc_regs;
300 int cnt = origcnt;
301 char *addr = ptr;
303 hd->hpib_acr = AUX_TCA;
304 hd->hpib_data = C_UNL_P;
305 if (nhpibwait(hd, MIS_BO))
306 goto senderror;
307 hd->hpib_data = talker_par[hs->sc_ba];
308 hd->hpib_acr = AUX_STON;
309 if (nhpibwait(hd, MIS_BO))
310 goto senderror;
311 hd->hpib_data = listnr_par[slave];
312 if (nhpibwait(hd, MIS_BO))
313 goto senderror;
314 if (sec >= 0 || sec == -2) {
315 if (sec == -2) /* selected device clear KLUDGE */
316 hd->hpib_data = C_SDC_P;
317 else
318 hd->hpib_data = sec_par[sec];
319 if (nhpibwait(hd, MIS_BO))
320 goto senderror;
322 hd->hpib_acr = AUX_GTS;
323 if (cnt) {
324 while (--cnt > 0) {
325 hd->hpib_data = *addr++;
326 if (nhpibwait(hd, MIS_BO))
327 goto senderror;
329 hd->hpib_acr = AUX_EOI;
330 hd->hpib_data = *addr;
331 if (nhpibwait(hd, MIS_BO))
332 goto senderror;
333 hd->hpib_acr = AUX_TCA;
334 #if 0
336 * May be causing 345 disks to hang due to interference
337 * with PPOLL mechanism.
339 hd->hpib_data = C_UNL_P;
340 (void) nhpibwait(hd, MIS_BO);
341 #endif
343 return origcnt;
345 senderror:
346 nhpibifc(hd);
347 return origcnt - cnt - 1;
350 static int
351 nhpibrecv(struct hpibbus_softc *hs, int slave, int sec, void *ptr, int origcnt)
353 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
354 struct nhpibdevice *hd = sc->sc_regs;
355 int cnt = origcnt;
356 char *addr = ptr;
359 * Slave < 0 implies continuation of a previous receive
360 * that probably timed out.
362 if (slave >= 0) {
363 hd->hpib_acr = AUX_TCA;
364 hd->hpib_data = C_UNL_P;
365 if (nhpibwait(hd, MIS_BO))
366 goto recverror;
367 hd->hpib_data = listnr_par[hs->sc_ba];
368 hd->hpib_acr = AUX_SLON;
369 if (nhpibwait(hd, MIS_BO))
370 goto recverror;
371 hd->hpib_data = talker_par[slave];
372 if (nhpibwait(hd, MIS_BO))
373 goto recverror;
374 if (sec >= 0) {
375 hd->hpib_data = sec_par[sec];
376 if (nhpibwait(hd, MIS_BO))
377 goto recverror;
379 hd->hpib_acr = AUX_RHDF;
380 hd->hpib_acr = AUX_GTS;
382 if (cnt) {
383 while (--cnt >= 0) {
384 if (nhpibwait(hd, MIS_BI))
385 goto recvbyteserror;
386 *addr++ = hd->hpib_data;
388 hd->hpib_acr = AUX_TCA;
389 hd->hpib_data = (slave == 31) ? C_UNA_P : C_UNT_P;
390 (void) nhpibwait(hd, MIS_BO);
392 return origcnt;
394 recverror:
395 nhpibifc(hd);
396 recvbyteserror:
397 return origcnt - cnt - 1;
400 static void
401 nhpibgo(struct hpibbus_softc *hs, int slave, int sec, void *ptr, int count,
402 int rw, int timo)
404 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
405 struct nhpibdevice *hd = sc->sc_regs;
406 char *addr = ptr;
408 hs->sc_flags |= HPIBF_IO;
409 if (timo)
410 hs->sc_flags |= HPIBF_TIMO;
411 if (rw == B_READ)
412 hs->sc_flags |= HPIBF_READ;
413 #ifdef DEBUG
414 else if (hs->sc_flags & HPIBF_READ) {
415 printf("nhpibgo: HPIBF_READ still set\n");
416 hs->sc_flags &= ~HPIBF_READ;
418 #endif
419 hs->sc_count = count;
420 hs->sc_addr = addr;
421 if (hs->sc_flags & HPIBF_READ) {
422 hs->sc_curcnt = count;
423 dmago(hs->sc_dq->dq_chan, addr, count, DMAGO_BYTE|DMAGO_READ);
424 nhpibrecv(hs, slave, sec, 0, 0);
425 hd->hpib_mim = MIS_END;
426 } else {
427 hd->hpib_mim = 0;
428 if (count < hpibdmathresh) {
429 hs->sc_curcnt = count;
430 nhpibsend(hs, slave, sec, addr, count);
431 nhpibdone(hs);
432 return;
434 hs->sc_curcnt = --count;
435 dmago(hs->sc_dq->dq_chan, addr, count, DMAGO_BYTE);
436 nhpibsend(hs, slave, sec, 0, 0);
438 hd->hpib_ie = IDS_IE | IDS_DMA(hs->sc_dq->dq_chan);
442 * This timeout can only happen if a DMA read finishes DMAing with the read
443 * still pending (more data in read transaction than the driver was prepared
444 * to accept). At the moment, variable-record tape drives are the only things
445 * capabale of doing this. We repeat the necessary code from nhpibintr() -
446 * easier and quicker than calling nhpibintr() for this special case.
448 static void
449 nhpibreadtimo(void *arg)
451 struct hpibbus_softc *hs = arg;
452 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
453 int s = splbio();
455 if (hs->sc_flags & HPIBF_IO) {
456 struct nhpibdevice *hd = sc->sc_regs;
457 struct hpibqueue *hq;
459 hd->hpib_mim = 0;
460 hd->hpib_acr = AUX_TCA;
461 hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
462 dmafree(hs->sc_dq);
464 hq = TAILQ_FIRST(&hs->sc_queue);
465 (hq->hq_intr)(hq->hq_softc);
467 splx(s);
470 static void
471 nhpibdone(struct hpibbus_softc *hs)
473 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
474 struct nhpibdevice *hd = sc->sc_regs;
475 int cnt;
477 cnt = hs->sc_curcnt;
478 hs->sc_addr += cnt;
479 hs->sc_count -= cnt;
480 hs->sc_flags |= HPIBF_DONE;
481 hd->hpib_ie = IDS_IE;
482 if (hs->sc_flags & HPIBF_READ) {
483 if ((hs->sc_flags & HPIBF_TIMO) &&
484 (hd->hpib_ids & IDS_IR) == 0)
485 callout_reset(&sc->sc_read_ch, hz >> 2,
486 nhpibreadtimo, hs);
487 } else {
488 if (hs->sc_count == 1) {
489 (void) nhpibwait(hd, MIS_BO);
490 hd->hpib_acr = AUX_EOI;
491 hd->hpib_data = *hs->sc_addr;
492 hd->hpib_mim = MIS_BO;
494 #ifdef DEBUG
495 else if (hs->sc_count)
496 panic("nhpibdone");
497 #endif
501 static int
502 nhpibintr(void *arg)
504 struct nhpib_softc *sc = arg;
505 struct hpibbus_softc *hs = sc->sc_hpibbus;
506 struct nhpibdevice *hd = sc->sc_regs;
507 struct hpibqueue *hq;
508 int stat0;
509 int stat1;
511 #ifdef lint
512 if (stat1 = unit)
513 return 1;
514 #endif
515 if ((hd->hpib_ids & IDS_IR) == 0)
516 return 0;
517 stat0 = hd->hpib_mis;
518 stat1 = hd->hpib_lis;
520 hq = TAILQ_FIRST(&hs->sc_queue);
522 if (hs->sc_flags & HPIBF_IO) {
523 hd->hpib_mim = 0;
524 if ((hs->sc_flags & HPIBF_DONE) == 0) {
525 hs->sc_flags &= ~HPIBF_TIMO;
526 dmastop(hs->sc_dq->dq_chan);
527 } else if (hs->sc_flags & HPIBF_TIMO)
528 callout_stop(&sc->sc_read_ch);
529 hd->hpib_acr = AUX_TCA;
530 hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
532 dmafree(hs->sc_dq);
533 (hq->hq_intr)(hq->hq_softc);
534 } else if (hs->sc_flags & HPIBF_PPOLL) {
535 hd->hpib_mim = 0;
536 stat0 = nhpibppoll(hs);
537 if (stat0 & (0x80 >> hq->hq_slave)) {
538 hs->sc_flags &= ~HPIBF_PPOLL;
539 (hq->hq_intr)(hq->hq_softc);
541 #ifdef DEBUG
542 else
543 printf("%s: PPOLL intr bad status %x\n",
544 device_xname(hs->sc_dev), stat0);
545 #endif
547 return 1;
550 static int
551 nhpibppoll(struct hpibbus_softc *hs)
553 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
554 struct nhpibdevice *hd = sc->sc_regs;
555 int ppoll;
557 hd->hpib_acr = AUX_SPP;
558 DELAY(25);
559 ppoll = hd->hpib_cpt;
560 hd->hpib_acr = AUX_CPP;
561 return ppoll;
564 #ifdef DEBUG
565 int nhpibreporttimo = 0;
566 #endif
568 static int
569 nhpibwait(struct nhpibdevice *hd, int x)
571 int timo = hpibtimeout;
573 while ((hd->hpib_mis & x) == 0 && --timo)
574 DELAY(1);
575 if (timo == 0) {
576 #ifdef DEBUG
577 if (nhpibreporttimo)
578 printf("hpib0: %s timo\n", x==MIS_BO?"OUT":"IN");
579 #endif
580 return -1;
582 return 0;
585 static void
586 nhpibppwatch(void *arg)
588 struct hpibbus_softc *hs = arg;
589 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev));
591 if ((hs->sc_flags & HPIBF_PPOLL) == 0)
592 return;
593 again:
594 if (nhpibppoll(hs) & (0x80 >> TAILQ_FIRST(&hs->sc_queue)->hq_slave))
595 sc->sc_regs->hpib_mim = MIS_BO;
596 else if (cold)
597 /* timeouts not working yet */
598 goto again;
599 else
600 callout_reset(&sc->sc_ppwatch_ch, 1, nhpibppwatch, hs);