1 /* $NetBSD: nhpib.c,v 1.40 2008/04/28 20:23:19 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 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. 18 * 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. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1990, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 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. 47 * 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. 59 * 60 * @(#)nhpib.c 8.2 (Berkeley) 1/12/94 61 */ 62 63 /* 64 * Internal/98624 HPIB driver 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: nhpib.c,v 1.40 2008/04/28 20:23:19 martin Exp $"); 69 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> 76 77 #include <machine/bus.h> 78 79 #include <hp300/dev/intiovar.h> 80 #include <hp300/dev/diovar.h> 81 #include <hp300/dev/diodevs.h> 82 #include <hp300/dev/dmavar.h> 83 84 #include <hp300/dev/nhpibreg.h> 85 #include <hp300/dev/hpibvar.h> 86 87 /* 88 * ODD parity table for listen and talk addresses and secondary commands. 89 * The TI9914A doesn't produce the parity bit. 90 */ 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, 96 }; 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, 102 }; 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 108 }; 109 110 static void nhpibifc(struct nhpibdevice *); 111 static void nhpibreadtimo(void *); 112 static int nhpibwait(struct nhpibdevice *, int); 113 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 *); 123 124 /* 125 * Our controller ops structure. 126 */ 127 static struct hpib_controller nhpib_controller = { 128 nhpibreset, 129 nhpibsend, 130 nhpibrecv, 131 nhpibppoll, 132 nhpibppwatch, 133 nhpibgo, 134 nhpibdone, 135 nhpibintr 136 }; 137 138 struct nhpib_softc { 139 device_t sc_dev; /* generic device glue */ 140 141 bus_space_tag_t sc_bst; 142 bus_space_handle_t sc_bsh; 143 144 struct nhpibdevice *sc_regs; /* device registers */ 145 struct hpibbus_softc *sc_hpibbus; /* XXX */ 146 147 int sc_myaddr; 148 int sc_type; 149 150 struct callout sc_read_ch; 151 struct callout sc_ppwatch_ch; 152 }; 153 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 *); 158 159 static void nhpib_common_attach(struct nhpib_softc *, const char *); 160 161 CFATTACH_DECL_NEW(nhpib_dio, sizeof(struct nhpib_softc), 162 nhpib_dio_match, nhpib_dio_attach, NULL, NULL); 163 164 CFATTACH_DECL_NEW(nhpib_intio, sizeof(struct nhpib_softc), 165 nhpib_intio_match, nhpib_intio_attach, NULL, NULL); 166 167 static int 168 nhpib_intio_match(device_t parent, cfdata_t cf, void *aux) 169 { 170 struct intio_attach_args *ia = aux; 171 172 if (strcmp("hpib", ia->ia_modname) == 0) 173 return 1; 174 175 return 0; 176 } 177 178 static int 179 nhpib_dio_match(device_t parent, cfdata_t cf, void *aux) 180 { 181 struct dio_attach_args *da = aux; 182 183 if (da->da_id == DIO_DEVICE_ID_NHPIB) 184 return 1; 185 186 return 0; 187 } 188 189 static void 190 nhpib_intio_attach(device_t parent, device_t self, void *aux) 191 { 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"; 196 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; 201 } 202 203 sc->sc_bst = bst; 204 sc->sc_myaddr = HPIBA_BA; 205 sc->sc_type = HPIBA; 206 207 nhpib_common_attach(sc, desc); 208 209 /* establish the interrupt handler */ 210 (void)intio_intr_establish(nhpibintr, sc, ia->ia_ipl, IPL_BIO); 211 } 212 213 static void 214 nhpib_dio_attach(device_t parent, device_t self, void *aux) 215 { 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; 220 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; 225 } 226 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; 232 233 nhpib_common_attach(sc, desc); 234 235 /* establish the interrupt handler */ 236 (void)dio_intr_establish(nhpibintr, sc, da->da_ipl, IPL_BIO); 237 } 238 239 static void 240 nhpib_common_attach(struct nhpib_softc *sc, const char *desc) 241 { 242 struct hpibdev_attach_args ha; 243 244 aprint_normal(": %s\n", desc); 245 246 sc->sc_regs = bus_space_vaddr(sc->sc_bst, sc->sc_bsh); 247 248 callout_init(&sc->sc_read_ch, 0); 249 callout_init(&sc->sc_ppwatch_ch, 0); 250 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); 256 } 257 258 static void 259 nhpibreset(struct hpibbus_softc *hs) 260 { 261 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev)); 262 struct nhpibdevice *hd = sc->sc_regs; 263 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); 281 } 282 283 static void 284 nhpibifc(struct nhpibdevice *hd) 285 { 286 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; 293 } 294 295 static int 296 nhpibsend(struct hpibbus_softc *hs, int slave, int sec, void *ptr, int origcnt) 297 { 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; 302 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; 321 } 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; 328 } 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 335 /* 336 * May be causing 345 disks to hang due to interference 337 * with PPOLL mechanism. 338 */ 339 hd->hpib_data = C_UNL_P; 340 (void) nhpibwait(hd, MIS_BO); 341 #endif 342 } 343 return origcnt; 344 345 senderror: 346 nhpibifc(hd); 347 return origcnt - cnt - 1; 348 } 349 350 static int 351 nhpibrecv(struct hpibbus_softc *hs, int slave, int sec, void *ptr, int origcnt) 352 { 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; 357 358 /* 359 * Slave < 0 implies continuation of a previous receive 360 * that probably timed out. 361 */ 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; 378 } 379 hd->hpib_acr = AUX_RHDF; 380 hd->hpib_acr = AUX_GTS; 381 } 382 if (cnt) { 383 while (--cnt >= 0) { 384 if (nhpibwait(hd, MIS_BI)) 385 goto recvbyteserror; 386 *addr++ = hd->hpib_data; 387 } 388 hd->hpib_acr = AUX_TCA; 389 hd->hpib_data = (slave == 31) ? C_UNA_P : C_UNT_P; 390 (void) nhpibwait(hd, MIS_BO); 391 } 392 return origcnt; 393 394 recverror: 395 nhpibifc(hd); 396 recvbyteserror: 397 return origcnt - cnt - 1; 398 } 399 400 static void 401 nhpibgo(struct hpibbus_softc *hs, int slave, int sec, void *ptr, int count, 402 int rw, int timo) 403 { 404 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev)); 405 struct nhpibdevice *hd = sc->sc_regs; 406 char *addr = ptr; 407 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; 417 } 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; 433 } 434 hs->sc_curcnt = --count; 435 dmago(hs->sc_dq->dq_chan, addr, count, DMAGO_BYTE); 436 nhpibsend(hs, slave, sec, 0, 0); 437 } 438 hd->hpib_ie = IDS_IE | IDS_DMA(hs->sc_dq->dq_chan); 439 } 440 441 /* 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. 447 */ 448 static void 449 nhpibreadtimo(void *arg) 450 { 451 struct hpibbus_softc *hs = arg; 452 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev)); 453 int s = splbio(); 454 455 if (hs->sc_flags & HPIBF_IO) { 456 struct nhpibdevice *hd = sc->sc_regs; 457 struct hpibqueue *hq; 458 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); 463 464 hq = TAILQ_FIRST(&hs->sc_queue); 465 (hq->hq_intr)(hq->hq_softc); 466 } 467 splx(s); 468 } 469 470 static void 471 nhpibdone(struct hpibbus_softc *hs) 472 { 473 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev)); 474 struct nhpibdevice *hd = sc->sc_regs; 475 int cnt; 476 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; 493 } 494 #ifdef DEBUG 495 else if (hs->sc_count) 496 panic("nhpibdone"); 497 #endif 498 } 499 } 500 501 static int 502 nhpibintr(void *arg) 503 { 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; 510 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; 519 520 hq = TAILQ_FIRST(&hs->sc_queue); 521 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); 531 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); 540 } 541 #ifdef DEBUG 542 else 543 printf("%s: PPOLL intr bad status %x\n", 544 device_xname(hs->sc_dev), stat0); 545 #endif 546 } 547 return 1; 548 } 549 550 static int 551 nhpibppoll(struct hpibbus_softc *hs) 552 { 553 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev)); 554 struct nhpibdevice *hd = sc->sc_regs; 555 int ppoll; 556 557 hd->hpib_acr = AUX_SPP; 558 DELAY(25); 559 ppoll = hd->hpib_cpt; 560 hd->hpib_acr = AUX_CPP; 561 return ppoll; 562 } 563 564 #ifdef DEBUG 565 int nhpibreporttimo = 0; 566 #endif 567 568 static int 569 nhpibwait(struct nhpibdevice *hd, int x) 570 { 571 int timo = hpibtimeout; 572 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; 581 } 582 return 0; 583 } 584 585 static void 586 nhpibppwatch(void *arg) 587 { 588 struct hpibbus_softc *hs = arg; 589 struct nhpib_softc *sc = device_private(device_parent(hs->sc_dev)); 590 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); 601 } 602