1 /*        $NetBSD: hpib.c,v 1.45 2023/01/15 06:19:45 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  *
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  *        @(#)hpib.c          8.2 (Berkeley) 1/12/94
61  */
62 
63 /*
64  * HP-IB bus driver
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: hpib.c,v 1.45 2023/01/15 06:19:45 tsutsui Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/buf.h>
73 #include <sys/kmem.h>
74 #include <sys/device.h>
75 
76 #include <hp300/dev/dmavar.h>
77 
78 #include <hp300/dev/hpibvar.h>
79 
80 #include <machine/cpu.h>
81 #include <machine/hp300spu.h>
82 
83 #include "ioconf.h"
84 
85 static int          hpibbusmatch(device_t, cfdata_t, void *);
86 static void         hpibbusattach(device_t, device_t, void *);
87 
88 CFATTACH_DECL_NEW(hpibbus, sizeof(struct hpibbus_softc),
89     hpibbusmatch, hpibbusattach, NULL, NULL);
90 
91 static void         hpibbus_attach_children(struct hpibbus_softc *);
92 static int          hpibbussubmatch(device_t, cfdata_t, const int *ldesc, void *);
93 static int          hpibbusprint(void *, const char *);
94 
95 static void         hpibstart(void *);
96 static void         hpibdone(void *);
97 
98 int       hpibtimeout = 100000;         /* # of status tests before we give up */
99 int       hpibidtimeout = 10000;        /* # of status tests for hpibid() calls */
100 int       hpibdmathresh = 3;  /* byte count beyond which to attempt dma */
101 
102 /*
103  * HP-IB is essentially an IEEE 488 bus, with an HP command
104  * set (CS/80 on `newer' devices, Amigo on before-you-were-born
105  * devices) thrown on top.  Devices that respond to CS/80 (and
106  * probably Amigo, too) are tagged with a 16-bit ID.
107  *
108  * HP-IB has a 2-level addressing scheme; slave, the analog
109  * of a SCSI ID, and punit, the analog of a SCSI LUN.  Unforunately,
110  * IDs are on a per-slave basis; punits are often used for disk
111  * drives that have an accompanying tape drive on the second punit.
112  *
113  * In addition, not all HP-IB devices speak CS/80 or Amigo.
114  * Examples of such devices are HP-IB plotters, which simply
115  * take raw plotter commands over 488.  These devices do not
116  * have ID tags, and often the host cannot even tell if such
117  * a device is attached to the system!
118  *
119  * * We nevertheless probe the whole (slave, punit) tuple space, since
120  * drivers for devices with a unique ID know exactly where to attach;
121  * and we disallow ``star'' locators for other drivers.
122  */
123 
124 static int
hpibbusmatch(device_t parent,cfdata_t cf,void * aux)125 hpibbusmatch(device_t parent, cfdata_t cf, void *aux)
126 {
127 
128           return 1;
129 }
130 
131 static void
hpibbusattach(device_t parent,device_t self,void * aux)132 hpibbusattach(device_t parent, device_t self, void *aux)
133 {
134           struct hpibbus_softc *sc = device_private(self);
135           struct hpibdev_attach_args *ha = aux;
136 
137           sc->sc_dev = self;
138           aprint_normal("\n");
139 
140           /* Get the operations vector for the controller. */
141           sc->sc_ops = ha->ha_ops;
142           sc->sc_type = ha->ha_type;              /* XXX */
143           sc->sc_ba = ha->ha_ba;
144           *(ha->ha_softcpp) = sc;                           /* XXX */
145 
146           hpibreset(device_unit(self));           /* XXX souldn't be here */
147 
148           /*
149            * Initialize the DMA queue entry.
150            */
151           sc->sc_dq = kmem_alloc(sizeof(struct dmaqueue), KM_SLEEP);
152           sc->sc_dq->dq_softc = sc;
153           sc->sc_dq->dq_start = hpibstart;
154           sc->sc_dq->dq_done = hpibdone;
155 
156           /* Initialize the slave request queue. */
157           TAILQ_INIT(&sc->sc_queue);
158 
159           /* Attach any devices on the bus. */
160           hpibbus_attach_children(sc);
161 }
162 
163 static void
hpibbus_attach_children(struct hpibbus_softc * sc)164 hpibbus_attach_children(struct hpibbus_softc *sc)
165 {
166           struct hpibbus_attach_args ha;
167           int id, slave, punit;
168           int i;
169 
170           for (slave = 0; slave < HPIB_NSLAVES; slave++) {
171                     /*
172                      * Get the ID tag for the device, if any.
173                      * Plotters won't identify themselves, and
174                      * get the same value as non-existent devices.
175                      * However, aging HP-IB drives are slow to respond; try up
176                      * to three times to get a valid ID.
177                      */
178                     for (i = 0; i < 3; i++) {
179                               id = hpibid(device_unit(sc->sc_dev), slave);
180                               if ((id & 0x200) != 0)
181                                         break;
182                               delay(10000);
183                     }
184 
185                     for (punit = 0; punit < HPIB_NPUNITS; punit++) {
186                               /*
187                                * Search through all configured children for this bus.
188                                */
189                               ha.ha_id = id;
190                               ha.ha_slave = slave;
191                               ha.ha_punit = punit;
192                               config_found(sc->sc_dev, &ha, hpibbusprint,
193                                   CFARGS(.submatch = hpibbussubmatch));
194                     }
195           }
196 }
197 
198 static int
hpibbussubmatch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)199 hpibbussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
200 {
201           struct hpibbus_attach_args *ha = aux;
202 
203           if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
204               cf->hpibbuscf_slave != ha->ha_slave)
205                     return 0;
206           if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
207               cf->hpibbuscf_punit != ha->ha_punit)
208                     return 0;
209 
210           return config_match(parent, cf, aux);
211 }
212 
213 static int
hpibbusprint(void * aux,const char * pnp)214 hpibbusprint(void *aux, const char *pnp)
215 {
216           struct hpibbus_attach_args *ha = aux;
217 
218           if (pnp != NULL) {
219                     if (ha->ha_id == 0 || ha->ha_punit != 0 /* XXX */)
220                               return QUIET;
221                     printf("HP-IB device (id %04X) at %s", ha->ha_id, pnp);
222           }
223           aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
224           return UNCONF;
225 }
226 
227 int
hpibdevprint(void * aux,const char * pnp)228 hpibdevprint(void *aux, const char *pnp)
229 {
230 
231           /* only hpibbus's can attach to hpibdev's -- easy. */
232           if (pnp != NULL)
233                     aprint_normal("hpibbus at %s", pnp);
234           return UNCONF;
235 }
236 
237 void
hpibreset(int unit)238 hpibreset(int unit)
239 {
240           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
241 
242           (*sc->sc_ops->hpib_reset)(sc);
243 }
244 
245 int
hpibreq(device_t pdev,struct hpibqueue * hq)246 hpibreq(device_t pdev, struct hpibqueue *hq)
247 {
248           struct hpibbus_softc *sc = device_private(pdev);
249           int s;
250 
251           s = splhigh();      /* XXXthorpej */
252           TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
253           splx(s);
254 
255           if (TAILQ_FIRST(&sc->sc_queue) == hq)
256                     return 1;
257 
258           return 0;
259 }
260 
261 void
hpibfree(device_t pdev,struct hpibqueue * hq)262 hpibfree(device_t pdev, struct hpibqueue *hq)
263 {
264           struct hpibbus_softc *sc = device_private(pdev);
265           int s;
266 
267           s = splhigh();      /* XXXthorpej */
268           TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
269           splx(s);
270 
271           if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
272                     (*hq->hq_start)(hq->hq_softc);
273 }
274 
275 int
hpibid(int unit,int slave)276 hpibid(int unit, int slave)
277 {
278           short id;
279           int ohpibtimeout;
280 
281           /*
282            * XXX shorten timeout value so autoconfig doesn't
283            * take forever on slow CPUs.
284            */
285           ohpibtimeout = hpibtimeout;
286           hpibtimeout = hpibidtimeout * (cpuspeed / 8);
287           if (hpibrecv(unit, 31, slave, &id, 2) != 2)
288                     id = 0;
289           hpibtimeout = ohpibtimeout;
290           return id;
291 }
292 
293 int
hpibsend(int unit,int slave,int sec,void * addr,int cnt)294 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
295 {
296           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
297 
298           return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
299 }
300 
301 int
hpibrecv(int unit,int slave,int sec,void * addr,int cnt)302 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
303 {
304           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
305 
306           return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
307 }
308 
309 int
hpibpptest(int unit,int slave)310 hpibpptest(int unit, int slave)
311 {
312           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
313 
314           return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
315 }
316 
317 void
hpibppclear(int unit)318 hpibppclear(int unit)
319 {
320           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
321 
322           sc->sc_flags &= ~HPIBF_PPOLL;
323 }
324 
325 void
hpibawait(int unit)326 hpibawait(int unit)
327 {
328           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
329 
330           sc->sc_flags |= HPIBF_PPOLL;
331           (*sc->sc_ops->hpib_ppwatch)(sc);
332 }
333 
334 int
hpibswait(int unit,int slave)335 hpibswait(int unit, int slave)
336 {
337           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
338           int timo = hpibtimeout;
339           int mask, (*ppoll)(struct hpibbus_softc *);
340 
341           ppoll = sc->sc_ops->hpib_ppoll;
342           mask = 0x80 >> slave;
343           while (((*ppoll)(sc) & mask) == 0) {
344                     if (--timo == 0) {
345                               printf("%s: swait timeout\n", device_xname(sc->sc_dev));
346                               return -1;
347                     }
348           }
349           return 0;
350 }
351 
352 int
hpibustart(int unit)353 hpibustart(int unit)
354 {
355           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
356 
357           if (sc->sc_type == HPIBA)
358                     sc->sc_dq->dq_chan = DMA0;
359           else
360                     sc->sc_dq->dq_chan = DMA0 | DMA1;
361           if (dmareq(sc->sc_dq))
362                     return 1;
363           return 0;
364 }
365 
366 static void
hpibstart(void * arg)367 hpibstart(void *arg)
368 {
369           struct hpibbus_softc *sc = arg;
370           struct hpibqueue *hq;
371 
372           hq = TAILQ_FIRST(&sc->sc_queue);
373           (*hq->hq_go)(hq->hq_softc);
374 }
375 
376 void
hpibgo(int unit,int slave,int sec,void * vbuf,int count,int rw,int timo)377 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
378 {
379           struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
380 
381           (*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
382 }
383 
384 static void
hpibdone(void * arg)385 hpibdone(void *arg)
386 {
387           struct hpibbus_softc *sc = arg;
388 
389           (*sc->sc_ops->hpib_done)(sc);
390 }
391 
392 int
hpibintr(void * arg)393 hpibintr(void *arg)
394 {
395           struct hpibbus_softc *sc = arg;
396 
397           return (sc->sc_ops->hpib_intr)(arg);
398 }
399