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