xref: /openbsd-src/sys/dev/eisa/cac_eisa.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: cac_eisa.c,v 1.4 2009/06/02 11:38:21 deraadt Exp $	*/
2 /*	$NetBSD: cac_eisa.c,v 1.1 2000/09/01 12:15:20 ad Exp $	*/
3 
4 /*-
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 2000 Jonathan Lemon
35  * Copyright (c) 1999 by Matthew N. Dodd <winter@jurai.net>
36  * All Rights Reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions, and the following disclaimer,
43  *    without modification, immediately at the beginning of the file.
44  * 2. The name of the author may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
51  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 /*
61  * EISA front-end for cac(4) driver.
62  */
63 
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/device.h>
68 
69 #include <machine/bus.h>
70 #include <machine/intr.h>
71 
72 #include <dev/eisa/eisavar.h>
73 #include <dev/eisa/eisadevs.h>
74 
75 #include <scsi/scsi_all.h>
76 #include <scsi/scsi_disk.h>
77 #include <scsi/scsiconf.h>
78 
79 #include <dev/ic/cacreg.h>
80 #include <dev/ic/cacvar.h>
81 
82 #define CAC_EISA_SLOT_OFFSET		0x0c88
83 #define CAC_EISA_IOSIZE			0x0017
84 #define CAC_EISA_IOCONF			0x38
85 
86 int	cac_eisa_match(struct device *, void *, void *);
87 void	cac_eisa_attach(struct device *, struct device *, void *);
88 
89 struct	cac_ccb *cac_eisa_l0_completed(struct cac_softc *);
90 int	cac_eisa_l0_fifo_full(struct cac_softc *);
91 void	cac_eisa_l0_intr_enable(struct cac_softc *, int);
92 int	cac_eisa_l0_intr_pending(struct cac_softc *);
93 void	cac_eisa_l0_submit(struct cac_softc *, struct cac_ccb *);
94 
95 struct cfattach cac_eisa_ca = {
96 	sizeof(struct cac_softc), cac_eisa_match, cac_eisa_attach
97 };
98 
99 static const
100 struct cac_linkage cac_eisa_l0 = {
101 	cac_eisa_l0_completed,
102 	cac_eisa_l0_fifo_full,
103 	cac_eisa_l0_intr_enable,
104 	cac_eisa_l0_intr_pending,
105 	cac_eisa_l0_submit
106 };
107 
108 static const
109 struct cac_eisa_type {
110 	const char	*ct_prodstr;
111 	const char	*ct_typestr;
112 	const struct	cac_linkage *ct_linkage;
113 } cac_eisa_type[] = {
114 	{ "CPQ4001",	"IDA",		&cac_eisa_l0 },
115 	{ "CPQ4002",	"IDA-2",	&cac_eisa_l0 },
116 	{ "CPQ4010",	"IEAS",		&cac_eisa_l0 },
117 	{ "CPQ4020",	"SMART",	&cac_eisa_l0 },
118 	{ "CPQ4030",	"SMART-2/E",	&cac_l0 },
119 };
120 
121 int
122 cac_eisa_match(parent, match, aux)
123 	struct device *parent;
124 	void *match, *aux;
125 {
126 	struct eisa_attach_args *ea;
127 	int i;
128 
129 	ea = aux;
130 
131 	for (i = 0; i < sizeof(cac_eisa_type) / sizeof(cac_eisa_type[0]); i++)
132 		if (strcmp(ea->ea_idstring, cac_eisa_type[i].ct_prodstr) == 0)
133 			return (1);
134 
135 	return (0);
136 }
137 
138 void
139 cac_eisa_attach(parent, self, aux)
140 	struct device *parent;
141 	struct device *self;
142 	void *aux;
143 {
144 	struct eisa_attach_args *ea;
145 	bus_space_handle_t ioh;
146 	eisa_chipset_tag_t ec;
147 	eisa_intr_handle_t ih;
148 	struct cac_softc *sc;
149 	bus_space_tag_t iot;
150 	const char *intrstr;
151 	int irq, i;
152 
153 	ea = aux;
154 	sc = (struct cac_softc *)self;
155 	iot = ea->ea_iot;
156 	ec = ea->ea_ec;
157 
158 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
159 	    CAC_EISA_SLOT_OFFSET, CAC_EISA_IOSIZE, 0, &ioh)) {
160 		printf(": can't map i/o space\n");
161 		return;
162 	}
163 
164 	/*
165 	 * Print board type and attach to the bus-independent code.
166 	 */
167 	for (i = 0; i < nitems(cac_eisa_type); i++)
168 		if (strcmp(ea->ea_idstring, cac_eisa_type[i].ct_prodstr) == 0)
169 			break;
170 
171 	if (i == nitems(cac_eisa_type)) {
172 		printf(": failed to attach %s\n", ea->ea_idstring);
173 		return;
174 	}
175 
176 	sc->sc_iot = iot;
177 	sc->sc_ioh = ioh;
178 	sc->sc_dmat = ea->ea_dmat;
179 
180 	/*
181 	 * Map and establish the interrupt.
182 	 */
183 	switch (bus_space_read_1(iot, ioh, CAC_EISA_IOCONF) & 0xf0) {
184 	case 0x20:
185 		irq = 10;
186 		break;
187 	case 0x10:
188 		irq = 11;
189 		break;
190 	case 0x40:
191 		irq = 14;
192 		break;
193 	case 0x80:
194 		irq = 15;
195 		break;
196 	default:
197 		printf(": controller on invalid IRQ\n");
198 		return;
199 	}
200 
201 	if (eisa_intr_map(ec, irq, &ih)) {
202 		printf(": can't map interrupt (%d)\n", irq);
203 		return;
204 	}
205 
206 	intrstr = eisa_intr_string(ec, ih);
207 	if ((sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
208 	    cac_intr, sc, sc->sc_dv.dv_xname)) == NULL) {
209 		printf(": can't establish interrupt");
210 		if (intrstr != NULL)
211 			printf(" at %s", intrstr);
212 		printf("\n");
213 		return;
214 	}
215 
216 	printf(" %s: Compaq %s\n", intrstr, cac_eisa_type[i].ct_typestr);
217 	sc->sc_cl = cac_eisa_type[i].ct_linkage;
218 	cac_init(sc, 0);
219 }
220 
221 /*
222  * Linkage specific to EISA boards.
223  */
224 
225 int
226 cac_eisa_l0_fifo_full(struct cac_softc *sc)
227 {
228 
229 	return ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
230 	    CAC_EISA_CHANNEL_CLEAR) == 0);
231 }
232 
233 void
234 cac_eisa_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
235 {
236 	u_int16_t size;
237 
238 	/*
239 	 * On these boards, `ccb_hdr.size' is actually for control flags.
240 	 * Set it to zero and pass the value by means of an I/O port.
241 	 */
242 	size = letoh16(ccb->ccb_hdr.size) << 2;
243 	ccb->ccb_hdr.size = 0;
244 
245 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
246 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
247 
248 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
249 	cac_outl(sc, CAC_EISAREG_LIST_ADDR, ccb->ccb_paddr);
250 	cac_outw(sc, CAC_EISAREG_LIST_LEN, size);
251 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_BUSY);
252 }
253 
254 struct cac_ccb *
255 cac_eisa_l0_completed(struct cac_softc *sc)
256 {
257 	struct cac_ccb *ccb;
258 	u_int32_t off;
259 	u_int8_t status;
260 
261 	if ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
262 	    CAC_EISA_CHANNEL_BUSY) == 0)
263 		return (NULL);
264 
265 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_BUSY);
266 	off = cac_inl(sc, CAC_EISAREG_COMPLETE_ADDR);
267 	status = cac_inb(sc, CAC_EISAREG_LIST_STATUS);
268 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
269 
270 	if (off == 0)
271 		return (NULL);
272 
273 	off = (off & ~3) - sc->sc_ccbs_paddr;
274 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
275 
276 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
277 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
278 
279 	ccb->ccb_req.error = status;
280 	return (ccb);
281 }
282 
283 int
284 cac_eisa_l0_intr_pending(struct cac_softc *sc)
285 {
286 
287 	return (cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
288 	    CAC_EISA_CHANNEL_BUSY);
289 }
290 
291 void
292 cac_eisa_l0_intr_enable(struct cac_softc *sc, int state)
293 {
294 
295 	if (state) {
296 		cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL,
297 		    ~CAC_EISA_CHANNEL_CLEAR);
298 		cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL,
299 		    CAC_EISA_CHANNEL_BUSY);
300 		cac_outb(sc, CAC_EISAREG_INTR_MASK, CAC_INTR_ENABLE);
301 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_ENABLE);
302 	} else
303 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_DISABLE);
304 }
305