xref: /netbsd-src/sys/arch/sparc/dev/esp_obio.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: esp_obio.c,v 1.11 2002/03/11 16:27:02 pk Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/errno.h>
45 #include <sys/device.h>
46 #include <sys/buf.h>
47 
48 #include <dev/scsipi/scsi_all.h>
49 #include <dev/scsipi/scsipi_all.h>
50 #include <dev/scsipi/scsiconf.h>
51 #include <dev/scsipi/scsi_message.h>
52 
53 #include <machine/bus.h>
54 #include <machine/autoconf.h>
55 #include <machine/intr.h>
56 
57 #include <dev/ic/lsi64854reg.h>
58 #include <dev/ic/lsi64854var.h>
59 
60 #include <dev/ic/ncr53c9xreg.h>
61 #include <dev/ic/ncr53c9xvar.h>
62 
63 #include <dev/sbus/sbusvar.h>
64 
65 struct esp_softc {
66 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
67 	bus_space_tag_t		sc_bustag;
68 	bus_dma_tag_t		sc_dmatag;
69 	bus_space_handle_t	sc_reg;		/* the registers */
70 	struct lsi64854_softc	*sc_dma;	/* pointer to my dma */
71 };
72 
73 
74 void	espattach_obio	__P((struct device *, struct device *, void *));
75 int	espmatch_obio	__P((struct device *, struct cfdata *, void *));
76 
77 /* Linkup to the rest of the kernel */
78 struct cfattach esp_obio_ca = {
79 	sizeof(struct esp_softc), espmatch_obio, espattach_obio
80 };
81 
82 /*
83  * Functions and the switch for the MI code.
84  */
85 static u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
86 static void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
87 static int	esp_dma_isintr __P((struct ncr53c9x_softc *));
88 static void	esp_dma_reset __P((struct ncr53c9x_softc *));
89 static int	esp_dma_intr __P((struct ncr53c9x_softc *));
90 static int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
91 				    size_t *, int, size_t *));
92 static void	esp_dma_go __P((struct ncr53c9x_softc *));
93 static void	esp_dma_stop __P((struct ncr53c9x_softc *));
94 static int	esp_dma_isactive __P((struct ncr53c9x_softc *));
95 
96 static struct ncr53c9x_glue esp_obio_glue = {
97 	esp_read_reg,
98 	esp_write_reg,
99 	esp_dma_isintr,
100 	esp_dma_reset,
101 	esp_dma_intr,
102 	esp_dma_setup,
103 	esp_dma_go,
104 	esp_dma_stop,
105 	esp_dma_isactive,
106 	NULL,			/* gl_clear_latched_intr */
107 };
108 
109 int
110 espmatch_obio(parent, cf, aux)
111 	struct device *parent;
112 	struct cfdata *cf;
113 	void *aux;
114 {
115 	union obio_attach_args *uoba = aux;
116 	struct obio4_attach_args *oba;
117 
118 	if (uoba->uoba_isobio4 == 0)
119 		return (0);
120 
121 	oba = &uoba->uoba_oba4;
122 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
123 				1,	/* probe size */
124 				0,	/* offset */
125 				0,	/* flags */
126 				NULL, NULL));
127 }
128 
129 void
130 espattach_obio(parent, self, aux)
131 	struct device *parent, *self;
132 	void *aux;
133 {
134 	union obio_attach_args *uoba = aux;
135 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
136 	struct esp_softc *esc = (void *)self;
137 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
138 
139 	esc->sc_bustag = oba->oba_bustag;
140 	esc->sc_dmatag = oba->oba_dmatag;
141 
142 	sc->sc_id = 7;
143 	sc->sc_freq = 24000000;
144 
145 	/*
146 	 * Find the DMA by poking around the dma device structures
147 	 */
148 	esc->sc_dma = (struct lsi64854_softc *)
149 			getdevunit("dma", sc->sc_dev.dv_unit);
150 
151 	/*
152 	 * and a back pointer to us, for DMA
153 	 */
154 	if (esc->sc_dma)
155 		esc->sc_dma->sc_client = sc;
156 	else {
157 		printf("\n");
158 		panic("espattach: no dma found");
159 	}
160 
161 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
162 			  16,	/* size (of ncr53c9xreg) */
163 			  BUS_SPACE_MAP_LINEAR,
164 			  &esc->sc_reg) != 0) {
165 		printf("%s @ obio: cannot map registers\n", self->dv_xname);
166 		return;
167 	}
168 
169 	/*
170 	 * Set up glue for MI code early; we use some of it here.
171 	 */
172 	sc->sc_glue = &esp_obio_glue;
173 
174 	/* gimme Mhz */
175 	sc->sc_freq /= 1000000;
176 
177 	/*
178 	 * XXX More of this should be in ncr53c9x_attach(), but
179 	 * XXX should we really poke around the chip that much in
180 	 * XXX the MI code?  Think about this more...
181 	 */
182 
183 	/*
184 	 * It is necessary to try to load the 2nd config register here,
185 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
186 	 * will not set up the defaults correctly.
187 	 */
188 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
189 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
190 	sc->sc_cfg3 = NCRCFG3_CDB;
191 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
192 
193 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
194 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
195 		sc->sc_rev = NCR_VARIANT_ESP100;
196 	} else {
197 		sc->sc_cfg2 = NCRCFG2_SCSI2;
198 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
199 		sc->sc_cfg3 = 0;
200 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
201 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
202 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
203 		if (NCR_READ_REG(sc, NCR_CFG3) !=
204 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
205 			sc->sc_rev = NCR_VARIANT_ESP100A;
206 		} else {
207 			/* NCRCFG2_FE enables > 64K transfers */
208 			sc->sc_cfg2 |= NCRCFG2_FE;
209 			sc->sc_cfg3 = 0;
210 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
211 			sc->sc_rev = NCR_VARIANT_ESP200;
212 		}
213 	}
214 
215 	/*
216 	 * XXX minsync and maxxfer _should_ be set up in MI code,
217 	 * XXX but it appears to have some dependency on what sort
218 	 * XXX of DMA we're hooked up to, etc.
219 	 */
220 
221 	/*
222 	 * This is the value used to start sync negotiations
223 	 * Note that the NCR register "SYNCTP" is programmed
224 	 * in "clocks per byte", and has a minimum value of 4.
225 	 * The SCSI period used in negotiation is one-fourth
226 	 * of the time (in nanoseconds) needed to transfer one byte.
227 	 * Since the chip's clock is given in MHz, we have the following
228 	 * formula: 4 * period = (1000 / freq) * 4
229 	 */
230 	sc->sc_minsync = 1000 / sc->sc_freq;
231 
232 	/*
233 	 * Alas, we must now modify the value a bit, because it's
234 	 * only valid when can switch on FASTCLK and FASTSCSI bits
235 	 * in config register 3...
236 	 */
237 	switch (sc->sc_rev) {
238 	case NCR_VARIANT_ESP100:
239 		sc->sc_maxxfer = 64 * 1024;
240 		sc->sc_minsync = 0;	/* No synch on old chip? */
241 		break;
242 
243 	case NCR_VARIANT_ESP100A:
244 		sc->sc_maxxfer = 64 * 1024;
245 		/* Min clocks/byte is 5 */
246 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
247 		break;
248 
249 	case NCR_VARIANT_ESP200:
250 		sc->sc_maxxfer = 16 * 1024 * 1024;
251 		/* XXX - do actually set FAST* bits */
252 		break;
253 	}
254 
255 	/* Establish interrupt channel */
256 	bus_intr_establish(esc->sc_bustag, oba->oba_pri, IPL_BIO, 0,
257 			   ncr53c9x_intr, sc);
258 
259 	/* register interrupt stats */
260 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
261 	    sc->sc_dev.dv_xname, "intr");
262 
263 	/* Do the common parts of attachment. */
264 	sc->sc_adapter.adapt_minphys = minphys;
265 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
266 	ncr53c9x_attach(sc);
267 	sc->sc_features |= NCR_F_DMASELECT;
268 }
269 
270 /*
271  * Glue functions.
272  */
273 
274 u_char
275 esp_read_reg(sc, reg)
276 	struct ncr53c9x_softc *sc;
277 	int reg;
278 {
279 	struct esp_softc *esc = (struct esp_softc *)sc;
280 
281 	return (bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg * 4));
282 }
283 
284 void
285 esp_write_reg(sc, reg, v)
286 	struct ncr53c9x_softc *sc;
287 	int reg;
288 	u_char v;
289 {
290 	struct esp_softc *esc = (struct esp_softc *)sc;
291 
292 	bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg * 4, v);
293 }
294 
295 int
296 esp_dma_isintr(sc)
297 	struct ncr53c9x_softc *sc;
298 {
299 	struct esp_softc *esc = (struct esp_softc *)sc;
300 
301 	return (DMA_ISINTR(esc->sc_dma));
302 }
303 
304 void
305 esp_dma_reset(sc)
306 	struct ncr53c9x_softc *sc;
307 {
308 	struct esp_softc *esc = (struct esp_softc *)sc;
309 
310 	DMA_RESET(esc->sc_dma);
311 }
312 
313 int
314 esp_dma_intr(sc)
315 	struct ncr53c9x_softc *sc;
316 {
317 	struct esp_softc *esc = (struct esp_softc *)sc;
318 
319 	return (DMA_INTR(esc->sc_dma));
320 }
321 
322 int
323 esp_dma_setup(sc, addr, len, datain, dmasize)
324 	struct ncr53c9x_softc *sc;
325 	caddr_t *addr;
326 	size_t *len;
327 	int datain;
328 	size_t *dmasize;
329 {
330 	struct esp_softc *esc = (struct esp_softc *)sc;
331 
332 	return (DMA_SETUP(esc->sc_dma, addr, len, datain, dmasize));
333 }
334 
335 void
336 esp_dma_go(sc)
337 	struct ncr53c9x_softc *sc;
338 {
339 	struct esp_softc *esc = (struct esp_softc *)sc;
340 
341 	DMA_GO(esc->sc_dma);
342 }
343 
344 void
345 esp_dma_stop(sc)
346 	struct ncr53c9x_softc *sc;
347 {
348 	struct esp_softc *esc = (struct esp_softc *)sc;
349 	u_int32_t csr;
350 
351 	csr = L64854_GCSR(esc->sc_dma);
352 	csr &= ~D_EN_DMA;
353 	L64854_SCSR(esc->sc_dma, csr);
354 }
355 
356 int
357 esp_dma_isactive(sc)
358 	struct ncr53c9x_softc *sc;
359 {
360 	struct esp_softc *esc = (struct esp_softc *)sc;
361 
362 	return (DMA_ISACTIVE(esc->sc_dma));
363 }
364