1 /* $NetBSD: nhpib.c,v 1.39 2008/03/29 06:47:08 tsutsui Exp $ */
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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
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>
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,
121 static void nhpibdone(struct hpibbus_softc
*);
122 static int nhpibintr(void *);
125 * Our controller ops structure.
127 static struct hpib_controller nhpib_controller
= {
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 */
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
);
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)
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
)
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";
198 if (bus_space_map(bst
, ia
->ia_iobase
, INTIO_DEVSIZE
, 0, &sc
->sc_bsh
)) {
199 aprint_error(": can't map registers\n");
204 sc
->sc_myaddr
= HPIBA_BA
;
207 nhpib_common_attach(sc
, desc
);
209 /* establish the interrupt handler */
210 (void)intio_intr_establish(nhpibintr
, sc
, ia
->ia_ipl
, IPL_BIO
);
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
;
222 if (bus_space_map(bst
, da
->da_addr
, da
->da_size
, 0, &sc
->sc_bsh
)) {
223 aprint_error(": can't map registers\n");
229 /* read address off switches */
230 sc
->sc_myaddr
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, 5);
233 nhpib_common_attach(sc
, desc
);
235 /* establish the interrupt handler */
236 (void)dio_intr_establish(nhpibintr
, sc
, da
->da_ipl
, IPL_BIO
);
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
);
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
;
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
;
278 hd
->hpib_ie
= IDS_IE
;
279 hd
->hpib_data
= C_DCL_P
;
284 nhpibifc(struct nhpibdevice
*hd
)
287 hd
->hpib_acr
= AUX_TCA
;
288 hd
->hpib_acr
= AUX_CSRE
;
289 hd
->hpib_acr
= AUX_SSIC
;
291 hd
->hpib_acr
= AUX_CSIC
;
292 hd
->hpib_acr
= AUX_SSRE
;
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
;
303 hd
->hpib_acr
= AUX_TCA
;
304 hd
->hpib_data
= C_UNL_P
;
305 if (nhpibwait(hd
, MIS_BO
))
307 hd
->hpib_data
= talker_par
[hs
->sc_ba
];
308 hd
->hpib_acr
= AUX_STON
;
309 if (nhpibwait(hd
, MIS_BO
))
311 hd
->hpib_data
= listnr_par
[slave
];
312 if (nhpibwait(hd
, MIS_BO
))
314 if (sec
>= 0 || sec
== -2) {
315 if (sec
== -2) /* selected device clear KLUDGE */
316 hd
->hpib_data
= C_SDC_P
;
318 hd
->hpib_data
= sec_par
[sec
];
319 if (nhpibwait(hd
, MIS_BO
))
322 hd
->hpib_acr
= AUX_GTS
;
325 hd
->hpib_data
= *addr
++;
326 if (nhpibwait(hd
, MIS_BO
))
329 hd
->hpib_acr
= AUX_EOI
;
330 hd
->hpib_data
= *addr
;
331 if (nhpibwait(hd
, MIS_BO
))
333 hd
->hpib_acr
= AUX_TCA
;
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
);
347 return origcnt
- cnt
- 1;
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
;
359 * Slave < 0 implies continuation of a previous receive
360 * that probably timed out.
363 hd
->hpib_acr
= AUX_TCA
;
364 hd
->hpib_data
= C_UNL_P
;
365 if (nhpibwait(hd
, MIS_BO
))
367 hd
->hpib_data
= listnr_par
[hs
->sc_ba
];
368 hd
->hpib_acr
= AUX_SLON
;
369 if (nhpibwait(hd
, MIS_BO
))
371 hd
->hpib_data
= talker_par
[slave
];
372 if (nhpibwait(hd
, MIS_BO
))
375 hd
->hpib_data
= sec_par
[sec
];
376 if (nhpibwait(hd
, MIS_BO
))
379 hd
->hpib_acr
= AUX_RHDF
;
380 hd
->hpib_acr
= AUX_GTS
;
384 if (nhpibwait(hd
, MIS_BI
))
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
);
397 return origcnt
- cnt
- 1;
401 nhpibgo(struct hpibbus_softc
*hs
, int slave
, int sec
, void *ptr
, int count
,
404 struct nhpib_softc
*sc
= device_private(device_parent(hs
->sc_dev
));
405 struct nhpibdevice
*hd
= sc
->sc_regs
;
408 hs
->sc_flags
|= HPIBF_IO
;
410 hs
->sc_flags
|= HPIBF_TIMO
;
412 hs
->sc_flags
|= HPIBF_READ
;
414 else if (hs
->sc_flags
& HPIBF_READ
) {
415 printf("nhpibgo: HPIBF_READ still set\n");
416 hs
->sc_flags
&= ~HPIBF_READ
;
419 hs
->sc_count
= count
;
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
;
428 if (count
< hpibdmathresh
) {
429 hs
->sc_curcnt
= count
;
430 nhpibsend(hs
, slave
, sec
, addr
, count
);
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.
449 nhpibreadtimo(void *arg
)
451 struct hpibbus_softc
*hs
= arg
;
452 struct nhpib_softc
*sc
= device_private(device_parent(hs
->sc_dev
));
455 if (hs
->sc_flags
& HPIBF_IO
) {
456 struct nhpibdevice
*hd
= sc
->sc_regs
;
457 struct hpibqueue
*hq
;
460 hd
->hpib_acr
= AUX_TCA
;
461 hs
->sc_flags
&= ~(HPIBF_DONE
|HPIBF_IO
|HPIBF_READ
|HPIBF_TIMO
);
464 hq
= TAILQ_FIRST(&hs
->sc_queue
);
465 (hq
->hq_intr
)(hq
->hq_softc
);
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
;
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,
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
;
495 else if (hs
->sc_count
)
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
;
515 if ((hd
->hpib_ids
& IDS_IR
) == 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
) {
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
);
533 (hq
->hq_intr
)(hq
->hq_softc
);
534 } else if (hs
->sc_flags
& HPIBF_PPOLL
) {
536 stat0
= nhpibppoll(hs
);
537 if (stat0
& (0x80 >> hq
->hq_slave
)) {
538 hs
->sc_flags
&= ~HPIBF_PPOLL
;
539 (hq
->hq_intr
)(hq
->hq_softc
);
543 printf("%s: PPOLL intr bad status %x\n",
544 device_xname(hs
->sc_dev
), stat0
);
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
;
557 hd
->hpib_acr
= AUX_SPP
;
559 ppoll
= hd
->hpib_cpt
;
560 hd
->hpib_acr
= AUX_CPP
;
565 int nhpibreporttimo
= 0;
569 nhpibwait(struct nhpibdevice
*hd
, int x
)
571 int timo
= hpibtimeout
;
573 while ((hd
->hpib_mis
& x
) == 0 && --timo
)
578 printf("hpib0: %s timo\n", x
==MIS_BO
?"OUT":"IN");
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)
594 if (nhpibppoll(hs
) & (0x80 >> TAILQ_FIRST(&hs
->sc_queue
)->hq_slave
))
595 sc
->sc_regs
->hpib_mim
= MIS_BO
;
597 /* timeouts not working yet */
600 callout_reset(&sc
->sc_ppwatch_ch
, 1, nhpibppwatch
, hs
);