1 /* $NetBSD: hpib.c,v 1.35 2007/12/05 12:03:08 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.35 2007/12/05 12:03:08 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 #include "ioconf.h" 91 92 static int hpibbusmatch(struct device *, struct cfdata *, void *); 93 static void hpibbusattach(struct device *, struct device *, void *); 94 95 CFATTACH_DECL(hpibbus, sizeof(struct hpibbus_softc), 96 hpibbusmatch, hpibbusattach, NULL, NULL); 97 98 static void hpibbus_attach_children(struct hpibbus_softc *); 99 static int hpibbussearch(struct device *, struct cfdata *, 100 const int *, void *); 101 static int hpibbusprint(void *, const char *); 102 103 static int hpibbus_alloc(struct hpibbus_softc *, int, int); 104 #if 0 105 static void hpibbus_free(struct hpibbus_softc *, int, int); 106 #endif 107 108 static void hpibstart(void *); 109 static void hpibdone(void *); 110 111 int hpibtimeout = 100000; /* # of status tests before we give up */ 112 int hpibidtimeout = 10000; /* # of status tests for hpibid() calls */ 113 int hpibdmathresh = 3; /* byte count beyond which to attempt dma */ 114 115 /* 116 * HP-IB is essentially an IEEE 488 bus, with an HP command 117 * set (CS/80 on `newer' devices, Amigo on before-you-were-born 118 * devices) thrown on top. Devices that respond to CS/80 (and 119 * probably Amigo, too) are tagged with a 16-bit ID. 120 * 121 * HP-IB has a 2-level addressing scheme; slave, the analog 122 * of a SCSI ID, and punit, the analog of a SCSI LUN. Unforunately, 123 * IDs are on a per-slave basis; punits are often used for disk 124 * drives that have an accompanying tape drive on the second punit. 125 * 126 * In addition, not all HP-IB devices speak CS/80 or Amigo. 127 * Examples of such devices are HP-IB plotters, which simply 128 * take raw plotter commands over 488. These devices do not 129 * have ID tags, and often the host cannot even tell if such 130 * a device is attached to the system! 131 * 132 * These two nasty bits mean that we have to treat HP-IB as 133 * an indirect bus. However, since we are given some ID 134 * information, it is unreasonable to disallow cloning of 135 * CS/80 devices. 136 * 137 * To deal with all of this, we use the semi-twisted scheme 138 * in hpibbus_attach_children(). For each HP-IB slave, we loop 139 * through all of the possibly-configured children, allowing 140 * them to modify the punit parameter (but NOT the slave!). 141 * 142 * This is evil, but what can you do? 143 */ 144 145 static int 146 hpibbusmatch(struct device *parent, struct cfdata *match, void *aux) 147 { 148 149 return 1; 150 } 151 152 static void 153 hpibbusattach(struct device *parent, struct device *self, void *aux) 154 { 155 struct hpibbus_softc *sc = (struct hpibbus_softc *)self; 156 struct hpibdev_attach_args *ha = aux; 157 158 printf("\n"); 159 160 /* Get the operations vector for the controller. */ 161 sc->sc_ops = ha->ha_ops; 162 sc->sc_type = ha->ha_type; /* XXX */ 163 sc->sc_ba = ha->ha_ba; 164 *(ha->ha_softcpp) = sc; /* XXX */ 165 166 hpibreset(device_unit(self)); /* XXX souldn't be here */ 167 168 /* 169 * Initialize the DMA queue entry. 170 */ 171 sc->sc_dq = malloc(sizeof(struct dmaqueue), M_DEVBUF, M_NOWAIT); 172 if (sc->sc_dq == NULL) { 173 printf("%s: can't allocate DMA queue entry\n", self->dv_xname); 174 return; 175 } 176 sc->sc_dq->dq_softc = sc; 177 sc->sc_dq->dq_start = hpibstart; 178 sc->sc_dq->dq_done = hpibdone; 179 180 /* Initialize the slave request queue. */ 181 TAILQ_INIT(&sc->sc_queue); 182 183 /* Attach any devices on the bus. */ 184 hpibbus_attach_children(sc); 185 } 186 187 static void 188 hpibbus_attach_children(struct hpibbus_softc *sc) 189 { 190 struct hpibbus_attach_args ha; 191 int slave; 192 193 for (slave = 0; slave < 8; slave++) { 194 /* 195 * Get the ID tag for the device, if any. 196 * Plotters won't identify themselves, and 197 * get the same value as non-existent devices. 198 */ 199 ha.ha_id = hpibid(device_unit(&sc->sc_dev), slave); 200 201 ha.ha_slave = slave; /* not to be modified by children */ 202 ha.ha_punit = 0; /* children modify this */ 203 204 /* 205 * Search though all configured children for this bus. 206 */ 207 config_search_ia(hpibbussearch, &sc->sc_dev, "hpibbus", &ha); 208 } 209 } 210 211 static int 212 hpibbussearch(struct device *parent, struct cfdata *cf, 213 const int *ldesc, void *aux) 214 { 215 struct hpibbus_softc *sc = (struct hpibbus_softc *)parent; 216 struct hpibbus_attach_args *ha = aux; 217 218 /* Make sure this is in a consistent state. */ 219 ha->ha_punit = 0; 220 221 if (config_match(parent, cf, ha) > 0) { 222 /* 223 * The device probe has succeeded, and filled in 224 * the punit information. Make sure the configuration 225 * allows for this slave/punit combination. 226 */ 227 if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT && 228 cf->hpibbuscf_slave != ha->ha_slave) 229 goto out; 230 if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT && 231 cf->hpibbuscf_punit != ha->ha_punit) 232 goto out; 233 234 /* 235 * Allocate the device's address from the bus's 236 * resource map. 237 */ 238 if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit)) 239 goto out; 240 241 /* 242 * This device is allowed; attach it. 243 */ 244 config_attach(parent, cf, ha, hpibbusprint); 245 } 246 out: 247 return 0; 248 } 249 250 static int 251 hpibbusprint(void *aux, const char *pnp) 252 { 253 struct hpibbus_attach_args *ha = aux; 254 255 aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit); 256 return UNCONF; 257 } 258 259 int 260 hpibdevprint(void *aux, const char *pnp) 261 { 262 263 /* only hpibbus's can attach to hpibdev's -- easy. */ 264 if (pnp != NULL) 265 aprint_normal("hpibbus at %s", pnp); 266 return UNCONF; 267 } 268 269 void 270 hpibreset(int unit) 271 { 272 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 273 274 (*sc->sc_ops->hpib_reset)(sc); 275 } 276 277 int 278 hpibreq(struct device *pdev, struct hpibqueue *hq) 279 { 280 struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev; 281 int s; 282 283 s = splhigh(); /* XXXthorpej */ 284 TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list); 285 splx(s); 286 287 if (TAILQ_FIRST(&sc->sc_queue) == hq) 288 return 1; 289 290 return 0; 291 } 292 293 void 294 hpibfree(struct device *pdev, struct hpibqueue *hq) 295 { 296 struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev; 297 int s; 298 299 s = splhigh(); /* XXXthorpej */ 300 TAILQ_REMOVE(&sc->sc_queue, hq, hq_list); 301 splx(s); 302 303 if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL) 304 (*hq->hq_start)(hq->hq_softc); 305 } 306 307 int 308 hpibid(int unit, int slave) 309 { 310 short id; 311 int ohpibtimeout; 312 313 /* 314 * XXX shorten timeout value so autoconfig doesn't 315 * take forever on slow CPUs. 316 */ 317 ohpibtimeout = hpibtimeout; 318 hpibtimeout = hpibidtimeout * (cpuspeed / 8); 319 if (hpibrecv(unit, 31, slave, &id, 2) != 2) 320 id = 0; 321 hpibtimeout = ohpibtimeout; 322 return id; 323 } 324 325 int 326 hpibsend(int unit, int slave, int sec, void *addr, int cnt) 327 { 328 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 329 330 return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt); 331 } 332 333 int 334 hpibrecv(int unit, int slave, int sec, void *addr, int cnt) 335 { 336 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 337 338 return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt); 339 } 340 341 int 342 hpibpptest(int unit, int slave) 343 { 344 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 345 346 return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave); 347 } 348 349 void 350 hpibppclear(int unit) 351 { 352 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 353 354 sc->sc_flags &= ~HPIBF_PPOLL; 355 } 356 357 void 358 hpibawait(int unit) 359 { 360 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 361 362 sc->sc_flags |= HPIBF_PPOLL; 363 (*sc->sc_ops->hpib_ppwatch)(sc); 364 } 365 366 int 367 hpibswait(int unit, int slave) 368 { 369 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 370 int timo = hpibtimeout; 371 int mask, (*ppoll)(struct hpibbus_softc *); 372 373 ppoll = sc->sc_ops->hpib_ppoll; 374 mask = 0x80 >> slave; 375 while (((*ppoll)(sc) & mask) == 0) { 376 if (--timo == 0) { 377 printf("%s: swait timeout\n", sc->sc_dev.dv_xname); 378 return -1; 379 } 380 } 381 return 0; 382 } 383 384 int 385 hpibustart(int unit) 386 { 387 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 388 389 if (sc->sc_type == HPIBA) 390 sc->sc_dq->dq_chan = DMA0; 391 else 392 sc->sc_dq->dq_chan = DMA0 | DMA1; 393 if (dmareq(sc->sc_dq)) 394 return 1; 395 return 0; 396 } 397 398 static void 399 hpibstart(void *arg) 400 { 401 struct hpibbus_softc *sc = arg; 402 struct hpibqueue *hq; 403 404 hq = TAILQ_FIRST(&sc->sc_queue); 405 (*hq->hq_go)(hq->hq_softc); 406 } 407 408 void 409 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo) 410 { 411 struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit]; 412 413 (*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo); 414 } 415 416 static void 417 hpibdone(void *arg) 418 { 419 struct hpibbus_softc *sc = arg; 420 421 (*sc->sc_ops->hpib_done)(sc); 422 } 423 424 int 425 hpibintr(void *arg) 426 { 427 struct hpibbus_softc *sc = arg; 428 429 return (sc->sc_ops->hpib_intr)(arg); 430 } 431 432 static int 433 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit) 434 { 435 436 if (slave >= HPIB_NSLAVES || 437 punit >= HPIB_NPUNITS) 438 panic("hpibbus_alloc: device address out of range"); 439 440 if (sc->sc_rmap[slave][punit] == 0) { 441 sc->sc_rmap[slave][punit] = 1; 442 return 0; 443 } 444 return 1; 445 } 446 447 #if 0 448 static void 449 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit) 450 { 451 452 if (slave >= HPIB_NSLAVES || 453 punit >= HPIB_NPUNITS) 454 panic("hpibbus_free: device address out of range"); 455 456 #ifdef DIAGNOSTIC 457 if (sc->sc_rmap[slave][punit] == 0) 458 panic("hpibbus_free: not allocated"); 459 #endif 460 461 sc->sc_rmap[slave][punit] = 0; 462 } 463 #endif 464