xref: /netbsd-src/sys/dev/eisa/cac_eisa.c (revision 5ce3f1d17010026525408cd8be9d30054cf6d8c2)
1 /*	$NetBSD: cac_eisa.c,v 1.26 2021/01/27 04:35:15 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
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) 2000 Jonathan Lemon
34  * Copyright (c) 1999 by Matthew N. Dodd <winter@jurai.net>
35  * All Rights Reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions, and the following disclaimer,
42  *    without modification, immediately at the beginning of the file.
43  * 2. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
50  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  */
58 
59 /*
60  * EISA front-end for cac(4) driver.
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: cac_eisa.c,v 1.26 2021/01/27 04:35:15 thorpej Exp $");
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/device.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/intr.h>
72 
73 #include <dev/eisa/eisavar.h>
74 #include <dev/eisa/eisadevs.h>
75 
76 #include <dev/ic/cacreg.h>
77 #include <dev/ic/cacvar.h>
78 
79 #include "ioconf.h"
80 
81 #define CAC_EISA_SLOT_OFFSET		0x0c88
82 #define CAC_EISA_IOSIZE			0x0017
83 #define CAC_EISA_IOCONF			0x38
84 
85 static void	cac_eisa_attach(device_t, device_t, void *);
86 static int	cac_eisa_match(device_t, cfdata_t, void *);
87 
88 static struct	cac_ccb *cac_eisa_l0_completed(struct cac_softc *);
89 static int	cac_eisa_l0_fifo_full(struct cac_softc *);
90 static void	cac_eisa_l0_intr_enable(struct cac_softc *, int);
91 static int	cac_eisa_l0_intr_pending(struct cac_softc *);
92 static void	cac_eisa_l0_submit(struct cac_softc *, struct cac_ccb *);
93 
94 CFATTACH_DECL3_NEW(cac_eisa, sizeof(struct cac_softc),
95     cac_eisa_match, cac_eisa_attach, NULL, NULL, cac_rescan, NULL, 0);
96 
97 static const struct cac_linkage cac_eisa_l0 = {
98 	cac_eisa_l0_completed,
99 	cac_eisa_l0_fifo_full,
100 	cac_eisa_l0_intr_enable,
101 	cac_eisa_l0_intr_pending,
102 	cac_eisa_l0_submit
103 };
104 
105 struct cac_eisa_type {
106 	const char	*ct_typestr;
107 	const struct	cac_linkage *ct_linkage;
108 };
109 
110 static const struct cac_eisa_type cpq4001 = {
111 	.ct_typestr = "IDA",
112 	.ct_linkage = &cac_eisa_l0
113 };
114 
115 static const struct cac_eisa_type cpq4002 = {
116 	.ct_typestr = "IDA-2",
117 	.ct_linkage = &cac_eisa_l0
118 };
119 
120 static const struct cac_eisa_type cpq4010 = {
121 	.ct_typestr = "IEAS",
122 	.ct_linkage = &cac_eisa_l0
123 };
124 
125 static const struct cac_eisa_type cpq4020 = {
126 	.ct_typestr = "SMART",
127 	.ct_linkage = &cac_eisa_l0
128 };
129 
130 static const struct cac_eisa_type cpq4030 = {
131 	.ct_typestr = "SMART-2/E",
132 	.ct_linkage = &cac_l0
133 };
134 
135 static const struct device_compatible_entry compat_data[] = {
136 	{ .compat = "CPQ4001",	.data = &cpq4001 },
137 	{ .compat = "CPQ4002",	.data = &cpq4002 },
138 	{ .compat = "CPQ4010",	.data = &cpq4010 },
139 	{ .compat = "CPQ4020",	.data = &cpq4020 },
140 	{ .compat = "CPQ4030",	.data = &cpq4030 },
141 	DEVICE_COMPAT_EOL
142 };
143 
144 static int
cac_eisa_match(device_t parent,cfdata_t match,void * aux)145 cac_eisa_match(device_t parent, cfdata_t match, void *aux)
146 {
147 	struct eisa_attach_args *ea = aux;
148 
149 	return (eisa_compatible_match(ea, compat_data));
150 }
151 
152 static void
cac_eisa_attach(device_t parent,device_t self,void * aux)153 cac_eisa_attach(device_t parent, device_t self, void *aux)
154 {
155 	struct eisa_attach_args *ea = aux;
156 	const struct device_compatible_entry *dce;
157 	const struct cac_eisa_type *ct;
158 	bus_space_handle_t ioh;
159 	eisa_chipset_tag_t ec;
160 	eisa_intr_handle_t ih;
161 	struct cac_softc *sc;
162 	bus_space_tag_t iot;
163 	const char *intrstr;
164 	int irq;
165 	char intrbuf[EISA_INTRSTR_LEN];
166 
167 	sc = device_private(self);
168 	iot = ea->ea_iot;
169 	ec = ea->ea_ec;
170 
171 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
172 	    CAC_EISA_SLOT_OFFSET, CAC_EISA_IOSIZE, 0, &ioh)) {
173 		aprint_error(": can't map i/o space\n");
174 		return;
175 	}
176 
177 	dce = eisa_compatible_lookup(ea, compat_data);
178 	KASSERT(dce != NULL);
179 	ct = dce->data;
180 
181 	sc->sc_dev = self;
182 	sc->sc_iot = iot;
183 	sc->sc_ioh = ioh;
184 	sc->sc_dmat = ea->ea_dmat;
185 
186 	/*
187 	 * Map and establish the interrupt.
188 	 */
189 	switch (bus_space_read_1(iot, ioh, CAC_EISA_IOCONF) & 0xf0) {
190 	case 0x20:
191 		irq = 10;
192 		break;
193 	case 0x10:
194 		irq = 11;
195 		break;
196 	case 0x40:
197 		irq = 14;
198 		break;
199 	case 0x80:
200 		irq = 15;
201 		break;
202 	default:
203 		aprint_error(": controller on invalid IRQ\n");
204 		return;
205 	}
206 
207 	if (eisa_intr_map(ec, irq, &ih)) {
208 		aprint_error(": can't map interrupt (%d)\n", irq);
209 		return;
210 	}
211 
212 	intrstr = eisa_intr_string(ec, ih, intrbuf, sizeof(intrbuf));
213 	if ((sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
214 	    cac_intr, sc)) == NULL) {
215 		aprint_error(": can't establish interrupt");
216 		if (intrstr != NULL)
217 			aprint_normal(" at %s", intrstr);
218 		aprint_normal("\n");
219 		return;
220 	}
221 
222 	/*
223 	 * Print board type and attach to the bus-independent code.
224 	 */
225 	aprint_normal(": Compaq %s\n", ct->ct_typestr);
226 	memcpy(&sc->sc_cl, ct->ct_linkage, sizeof(sc->sc_cl));
227 	cac_init(sc, intrstr, 0);
228 }
229 
230 /*
231  * Linkage specific to EISA boards.
232  */
233 
234 static int
cac_eisa_l0_fifo_full(struct cac_softc * sc)235 cac_eisa_l0_fifo_full(struct cac_softc *sc)
236 {
237 
238 	return ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
239 	    CAC_EISA_CHANNEL_CLEAR) == 0);
240 }
241 
242 static void
cac_eisa_l0_submit(struct cac_softc * sc,struct cac_ccb * ccb)243 cac_eisa_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
244 {
245 	u_int16_t size;
246 
247 	/*
248 	 * On these boards, `ccb_hdr.size' is actually for control flags.
249 	 * Set it to zero and pass the value by means of an I/O port.
250 	 */
251 	size = le16toh(ccb->ccb_hdr.size) << 2;
252 	ccb->ccb_hdr.size = 0;
253 
254 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
255 	    (char *)ccb - (char *)sc->sc_ccbs,
256 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
257 
258 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
259 	cac_outl(sc, CAC_EISAREG_LIST_ADDR, ccb->ccb_paddr);
260 	cac_outw(sc, CAC_EISAREG_LIST_LEN, size);
261 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_BUSY);
262 }
263 
264 static struct cac_ccb *
cac_eisa_l0_completed(struct cac_softc * sc)265 cac_eisa_l0_completed(struct cac_softc *sc)
266 {
267 	struct cac_ccb *ccb;
268 	u_int32_t off;
269 	u_int8_t status;
270 
271 	if ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
272 	    CAC_EISA_CHANNEL_BUSY) == 0)
273 		return (NULL);
274 
275 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_BUSY);
276 	off = cac_inl(sc, CAC_EISAREG_COMPLETE_ADDR);
277 	status = cac_inb(sc, CAC_EISAREG_LIST_STATUS);
278 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
279 
280 	if (off == 0)
281 		return (NULL);
282 
283 	off = (off & ~3) - sc->sc_ccbs_paddr;
284 	ccb = (struct cac_ccb *)((char *)sc->sc_ccbs + off);
285 
286 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
287 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
288 
289 	ccb->ccb_req.error = status;
290 
291 	if ((off & 3) != 0 && ccb->ccb_req.error == 0)
292 		ccb->ccb_req.error = CAC_RET_CMD_REJECTED;
293 
294 	return (ccb);
295 }
296 
297 static int
cac_eisa_l0_intr_pending(struct cac_softc * sc)298 cac_eisa_l0_intr_pending(struct cac_softc *sc)
299 {
300 
301 	return (cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
302 	    CAC_EISA_CHANNEL_BUSY);
303 }
304 
305 static void
cac_eisa_l0_intr_enable(struct cac_softc * sc,int state)306 cac_eisa_l0_intr_enable(struct cac_softc *sc, int state)
307 {
308 
309 	if (state) {
310 		cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL,
311 		    ~CAC_EISA_CHANNEL_CLEAR);
312 		cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL,
313 		    CAC_EISA_CHANNEL_BUSY);
314 		cac_outb(sc, CAC_EISAREG_INTR_MASK, CAC_INTR_ENABLE);
315 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_ENABLE);
316 	} else
317 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_DISABLE);
318 }
319 
320 MODULE(MODULE_CLASS_DRIVER, cac_eisa, "cac");	/* No eisa module yet! */
321 
322 #ifdef _MODULE
323 /*
324  * XXX Don't allow ioconf.c to redefine the "struct cfdriver cac_cd"
325  * XXX it will be defined in the common-code module
326  */
327 #undef  CFDRIVER_DECL
328 #define CFDRIVER_DECL(name, class, attr)
329 #include "ioconf.c"
330 #endif
331 
332 static int
cac_eisa_modcmd(modcmd_t cmd,void * opaque)333 cac_eisa_modcmd(modcmd_t cmd, void *opaque)
334 {
335 	int error = 0;
336 
337 #ifdef _MODULE
338 	switch (cmd) {
339 	case MODULE_CMD_INIT:
340 		/*
341 		 * We skip over the first entry in cfdriver[] array
342 		 * since the cfdriver is attached by the common
343 		 * (non-attachment-specific) code.
344 		 */
345 		error = config_init_component(&cfdriver_ioconf_cac_eisa[1],
346 		    cfattach_ioconf_cac_eisa, cfdata_ioconf_cac_eisa);
347 		break;
348 	case MODULE_CMD_FINI:
349 		error = config_fini_component(&cfdriver_ioconf_cac_eisa[1],
350 		    cfattach_ioconf_cac_eisa, cfdata_ioconf_cac_eisa);
351 		break;
352 	default:
353 		error = ENOTTY;
354 		break;
355 	}
356 #endif
357 
358 	return error;
359 }
360