xref: /openbsd-src/sys/dev/eisa/cac_eisa.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: cac_eisa.c,v 1.3 2008/06/26 05:42:14 ray 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 	sc->sc_iot = iot;
165 	sc->sc_ioh = ioh;
166 	sc->sc_dmat = ea->ea_dmat;
167 
168 	/*
169 	 * Map and establish the interrupt.
170 	 */
171 	switch (bus_space_read_1(iot, ioh, CAC_EISA_IOCONF) & 0xf0) {
172 	case 0x20:
173 		irq = 10;
174 		break;
175 	case 0x10:
176 		irq = 11;
177 		break;
178 	case 0x40:
179 		irq = 14;
180 		break;
181 	case 0x80:
182 		irq = 15;
183 		break;
184 	default:
185 		printf(": controller on invalid IRQ\n");
186 		return;
187 	}
188 
189 	if (eisa_intr_map(ec, irq, &ih)) {
190 		printf(": can't map interrupt (%d)\n", irq);
191 		return;
192 	}
193 
194 	intrstr = eisa_intr_string(ec, ih);
195 	if ((sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
196 	    cac_intr, sc, sc->sc_dv.dv_xname)) == NULL) {
197 		printf(": can't establish interrupt");
198 		if (intrstr != NULL)
199 			printf(" at %s", intrstr);
200 		printf("\n");
201 		return;
202 	}
203 
204 	/*
205 	 * Print board type and attach to the bus-independent code.
206 	 */
207 	for (i = 0; i < sizeof(cac_eisa_type) / sizeof(cac_eisa_type[0]); i++)
208 		if (strcmp(ea->ea_idstring, cac_eisa_type[i].ct_prodstr) == 0)
209 			break;
210 
211 	printf(" %s: Compaq %s\n", intrstr, cac_eisa_type[i].ct_typestr);
212 	sc->sc_cl = cac_eisa_type[i].ct_linkage;
213 	cac_init(sc, 0);
214 }
215 
216 /*
217  * Linkage specific to EISA boards.
218  */
219 
220 int
221 cac_eisa_l0_fifo_full(struct cac_softc *sc)
222 {
223 
224 	return ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
225 	    CAC_EISA_CHANNEL_CLEAR) == 0);
226 }
227 
228 void
229 cac_eisa_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
230 {
231 	u_int16_t size;
232 
233 	/*
234 	 * On these boards, `ccb_hdr.size' is actually for control flags.
235 	 * Set it to zero and pass the value by means of an I/O port.
236 	 */
237 	size = letoh16(ccb->ccb_hdr.size) << 2;
238 	ccb->ccb_hdr.size = 0;
239 
240 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
241 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
242 
243 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
244 	cac_outl(sc, CAC_EISAREG_LIST_ADDR, ccb->ccb_paddr);
245 	cac_outw(sc, CAC_EISAREG_LIST_LEN, size);
246 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_BUSY);
247 }
248 
249 struct cac_ccb *
250 cac_eisa_l0_completed(struct cac_softc *sc)
251 {
252 	struct cac_ccb *ccb;
253 	u_int32_t off;
254 	u_int8_t status;
255 
256 	if ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
257 	    CAC_EISA_CHANNEL_BUSY) == 0)
258 		return (NULL);
259 
260 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_BUSY);
261 	off = cac_inl(sc, CAC_EISAREG_COMPLETE_ADDR);
262 	status = cac_inb(sc, CAC_EISAREG_LIST_STATUS);
263 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
264 
265 	if (off == 0)
266 		return (NULL);
267 
268 	off = (off & ~3) - sc->sc_ccbs_paddr;
269 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
270 
271 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
272 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
273 
274 	ccb->ccb_req.error = status;
275 	return (ccb);
276 }
277 
278 int
279 cac_eisa_l0_intr_pending(struct cac_softc *sc)
280 {
281 
282 	return (cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
283 	    CAC_EISA_CHANNEL_BUSY);
284 }
285 
286 void
287 cac_eisa_l0_intr_enable(struct cac_softc *sc, int state)
288 {
289 
290 	if (state) {
291 		cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL,
292 		    ~CAC_EISA_CHANNEL_CLEAR);
293 		cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL,
294 		    CAC_EISA_CHANNEL_BUSY);
295 		cac_outb(sc, CAC_EISAREG_INTR_MASK, CAC_INTR_ENABLE);
296 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_ENABLE);
297 	} else
298 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_DISABLE);
299 }
300