1 /* $NetBSD: hpib.c,v 1.23 2002/10/02 05:15:52 thorpej 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1982, 1990, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)hpib.c 8.2 (Berkeley) 1/12/94 72 */ 73 74 /* 75 * HP-IB bus driver 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: hpib.c,v 1.23 2002/10/02 05:15:52 thorpej Exp $"); 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/buf.h> 84 #include <sys/malloc.h> 85 #include <sys/device.h> 86 87 #include <hp300/dev/dmavar.h> 88 89 #include <hp300/dev/hpibvar.h> 90 91 #include <machine/cpu.h> 92 #include <machine/hp300spu.h> 93 94 int hpibbusmatch __P((struct device *, struct cfdata *, void *)); 95 void hpibbusattach __P((struct device *, struct device *, void *)); 96 97 CFATTACH_DECL(hpibbus, sizeof(struct hpibbus_softc), 98 hpibbusmatch, hpibbusattach, NULL, NULL); 99 100 extern struct cfdriver hpibbus_cd; 101 102 void hpibbus_attach_children __P((struct hpibbus_softc *)); 103 int hpibbussearch __P((struct device *, struct cfdata *, void *)); 104 int hpibbusprint __P((void *, const char *)); 105 106 int hpibbus_alloc __P((struct hpibbus_softc *, int, int)); 107 void hpibbus_free __P((struct hpibbus_softc *, int, int)); 108 109 void hpibstart __P((void *)); 110 void hpibdone __P((void *)); 111 112 int hpibtimeout = 100000; /* # of status tests before we give up */ 113 int hpibidtimeout = 10000; /* # of status tests for hpibid() calls */ 114 int hpibdmathresh = 3; /* byte count beyond which to attempt dma */ 115 116 /* 117 * HP-IB is essentially an IEEE 488 bus, with an HP command 118 * set (CS/80 on `newer' devices, Amigo on before-you-were-born 119 * devices) thrown on top. Devices that respond to CS/80 (and 120 * probably Amigo, too) are tagged with a 16-bit ID. 121 * 122 * HP-IB has a 2-level addressing scheme; slave, the analog 123 * of a SCSI ID, and punit, the analog of a SCSI LUN. Unforunately, 124 * IDs are on a per-slave basis; punits are often used for disk 125 * drives that have an accompanying tape drive on the second punit. 126 * 127 * In addition, not all HP-IB devices speak CS/80 or Amigo. 128 * Examples of such devices are HP-IB plotters, which simply 129 * take raw plotter commands over 488. These devices do not 130 * have ID tags, and often the host cannot even tell if such 131 * a device is attached to the system! 132 * 133 * These two nasty bits mean that we have to treat HP-IB as 134 * an indirect bus. However, since we are given some ID 135 * information, it is unreasonable to disallow cloning of 136 * CS/80 devices. 137 * 138 * To deal with all of this, we use the semi-twisted scheme 139 * in hpibbus_attach_children(). For each HP-IB slave, we loop 140 * through all of the possibly-configured children, allowing 141 * them to modify the punit parameter (but NOT the slave!). 142 * 143 * This is evil, but what can you do? 144 */ 145 146 int 147 hpibbusmatch(parent, match, aux) 148 struct device *parent; 149 struct cfdata *match; 150 void *aux; 151 { 152 153 return (1); 154 } 155 156 void 157 hpibbusattach(parent, self, aux) 158 struct device *parent, *self; 159 void *aux; 160 { 161 struct hpibbus_softc *sc = (struct hpibbus_softc *)self; 162 struct hpibdev_attach_args *ha = aux; 163 164 printf("\n"); 165 166 /* Get the operations vector for the controller. */ 167 sc->sc_ops = ha->ha_ops; 168 sc->sc_type = ha->ha_type; /* XXX */ 169 sc->sc_ba = ha->ha_ba; 170 *(ha->ha_softcpp) = sc; /* XXX */ 171 172 hpibreset(self->dv_unit); /* XXX souldn't be here */ 173 174 /* 175 * Initialize the DMA queue entry. 176 */ 177 MALLOC(sc->sc_dq, struct dmaqueue *, sizeof(struct dmaqueue), 178 M_DEVBUF, M_NOWAIT); 179 if (sc->sc_dq == NULL) { 180 printf("%s: can't allocate DMA queue entry\n", self->dv_xname); 181 return; 182 } 183 sc->sc_dq->dq_softc = sc; 184 sc->sc_dq->dq_start = hpibstart; 185 sc->sc_dq->dq_done = hpibdone; 186 187 /* Initialize the slave request queue. */ 188 TAILQ_INIT(&sc->sc_queue); 189 190 /* Attach any devices on the bus. */ 191 hpibbus_attach_children(sc); 192 } 193 194 void 195 hpibbus_attach_children(sc) 196 struct hpibbus_softc *sc; 197 { 198 struct hpibbus_attach_args ha; 199 int slave; 200 201 for (slave = 0; slave < 8; slave++) { 202 /* 203 * Get the ID tag for the device, if any. 204 * Plotters won't identify themselves, and 205 * get the same value as non-existent devices. 206 */ 207 ha.ha_id = hpibid(sc->sc_dev.dv_unit, slave); 208 209 ha.ha_slave = slave; /* not to be modified by children */ 210 ha.ha_punit = 0; /* children modify this */ 211 212 /* 213 * Search though all configured children for this bus. 214 */ 215 (void)config_search(hpibbussearch, &sc->sc_dev, &ha); 216 } 217 } 218 219 int 220 hpibbussearch(parent, cf, aux) 221 struct device *parent; 222 struct cfdata *cf; 223 void *aux; 224 { 225 struct hpibbus_softc *sc = (struct hpibbus_softc *)parent; 226 struct hpibbus_attach_args *ha = aux; 227 228 /* Make sure this is in a consistent state. */ 229 ha->ha_punit = 0; 230 231 if (config_match(parent, cf, ha) > 0) { 232 /* 233 * The device probe has succeeded, and filled in 234 * the punit information. Make sure the configuration 235 * allows for this slave/punit combination. 236 */ 237 if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT && 238 cf->hpibbuscf_slave != ha->ha_slave) 239 goto out; 240 if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT && 241 cf->hpibbuscf_punit != ha->ha_punit) 242 goto out; 243 244 /* 245 * Allocate the device's address from the bus's 246 * resource map. 247 */ 248 if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit)) 249 goto out; 250 251 /* 252 * This device is allowed; attach it. 253 */ 254 config_attach(parent, cf, ha, hpibbusprint); 255 } 256 out: 257 return (0); 258 } 259 260 int 261 hpibbusprint(aux, pnp) 262 void *aux; 263 const char *pnp; 264 { 265 struct hpibbus_attach_args *ha = aux; 266 267 printf(" slave %d punit %d", ha->ha_slave, ha->ha_punit); 268 return (UNCONF); 269 } 270 271 int 272 hpibdevprint(aux, pnp) 273 void *aux; 274 const char *pnp; 275 { 276 277 /* only hpibbus's can attach to hpibdev's -- easy. */ 278 if (pnp != NULL) 279 printf("hpibbus at %s", pnp); 280 return (UNCONF); 281 } 282 283 void 284 hpibreset(unit) 285 int unit; 286 { 287 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 288 289 (*sc->sc_ops->hpib_reset)(sc); 290 } 291 292 int 293 hpibreq(pdev, hq) 294 struct device *pdev; 295 struct hpibqueue *hq; 296 { 297 struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev; 298 int s; 299 300 s = splhigh(); /* XXXthorpej */ 301 TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list); 302 splx(s); 303 304 if (sc->sc_queue.tqh_first == hq) 305 return (1); 306 307 return (0); 308 } 309 310 void 311 hpibfree(pdev, hq) 312 struct device *pdev; 313 struct hpibqueue *hq; 314 { 315 struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev; 316 int s; 317 318 s = splhigh(); /* XXXthorpej */ 319 TAILQ_REMOVE(&sc->sc_queue, hq, hq_list); 320 splx(s); 321 322 if ((hq = sc->sc_queue.tqh_first) != NULL) 323 (*hq->hq_start)(hq->hq_softc); 324 } 325 326 int 327 hpibid(unit, slave) 328 int unit, slave; 329 { 330 short id; 331 int ohpibtimeout; 332 333 /* 334 * XXX shorten timeout value so autoconfig doesn't 335 * take forever on slow CPUs. 336 */ 337 ohpibtimeout = hpibtimeout; 338 hpibtimeout = hpibidtimeout * (cpuspeed / 8); 339 if (hpibrecv(unit, 31, slave, &id, 2) != 2) 340 id = 0; 341 hpibtimeout = ohpibtimeout; 342 return(id); 343 } 344 345 int 346 hpibsend(unit, slave, sec, addr, cnt) 347 int unit, slave, sec, cnt; 348 void *addr; 349 { 350 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 351 352 return ((*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt)); 353 } 354 355 int 356 hpibrecv(unit, slave, sec, addr, cnt) 357 int unit, slave, sec, cnt; 358 void *addr; 359 { 360 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 361 362 return ((*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt)); 363 } 364 365 int 366 hpibpptest(unit, slave) 367 int unit; 368 int slave; 369 { 370 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 371 372 return ((*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave)); 373 } 374 375 void 376 hpibppclear(unit) 377 int unit; 378 { 379 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 380 381 sc->sc_flags &= ~HPIBF_PPOLL; 382 } 383 384 void 385 hpibawait(unit) 386 int unit; 387 { 388 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 389 390 sc->sc_flags |= HPIBF_PPOLL; 391 (*sc->sc_ops->hpib_ppwatch)(sc); 392 } 393 394 int 395 hpibswait(unit, slave) 396 int unit; 397 int slave; 398 { 399 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 400 int timo = hpibtimeout; 401 int mask, (*ppoll) __P((struct hpibbus_softc *)); 402 403 ppoll = sc->sc_ops->hpib_ppoll; 404 mask = 0x80 >> slave; 405 while (((*ppoll)(sc) & mask) == 0) { 406 if (--timo == 0) { 407 printf("%s: swait timeout\n", sc->sc_dev.dv_xname); 408 return(-1); 409 } 410 } 411 return(0); 412 } 413 414 int 415 hpibustart(unit) 416 int unit; 417 { 418 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 419 420 if (sc->sc_type == HPIBA) 421 sc->sc_dq->dq_chan = DMA0; 422 else 423 sc->sc_dq->dq_chan = DMA0 | DMA1; 424 if (dmareq(sc->sc_dq)) 425 return(1); 426 return(0); 427 } 428 429 void 430 hpibstart(arg) 431 void *arg; 432 { 433 struct hpibbus_softc *sc = arg; 434 struct hpibqueue *hq; 435 436 hq = sc->sc_queue.tqh_first; 437 (*hq->hq_go)(hq->hq_softc); 438 } 439 440 void 441 hpibgo(unit, slave, sec, vbuf, count, rw, timo) 442 int unit, slave, sec; 443 void *vbuf; 444 int count, rw, timo; 445 { 446 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 447 448 (*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo); 449 } 450 451 void 452 hpibdone(arg) 453 void *arg; 454 { 455 struct hpibbus_softc *sc = arg; 456 457 (*sc->sc_ops->hpib_done)(sc); 458 } 459 460 int 461 hpibintr(arg) 462 void *arg; 463 { 464 struct hpibbus_softc *sc = arg; 465 466 return ((sc->sc_ops->hpib_intr)(arg)); 467 } 468 469 int 470 hpibbus_alloc(sc, slave, punit) 471 struct hpibbus_softc *sc; 472 int slave, punit; 473 { 474 475 if (slave >= HPIB_NSLAVES || 476 punit >= HPIB_NPUNITS) 477 panic("hpibbus_alloc: device address out of range"); 478 479 if (sc->sc_rmap[slave][punit] == 0) { 480 sc->sc_rmap[slave][punit] = 1; 481 return (0); 482 } 483 return (1); 484 } 485 486 void 487 hpibbus_free(sc, slave, punit) 488 struct hpibbus_softc *sc; 489 int slave, punit; 490 { 491 492 if (slave >= HPIB_NSLAVES || 493 punit >= HPIB_NPUNITS) 494 panic("hpibbus_free: device address out of range"); 495 496 #ifdef DIAGNOSTIC 497 if (sc->sc_rmap[slave][punit] == 0) 498 panic("hpibbus_free: not allocated"); 499 #endif 500 501 sc->sc_rmap[slave][punit] = 0; 502 } 503