xref: /netbsd/sys/dev/vme/sc_vme.c (revision bf9ec67e)
1 /*	$NetBSD: sc_vme.c,v 1.4 2001/11/15 09:48:19 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996,2000,2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, David Jones, Gordon W. Ross, Jason R. Thorpe,
9  * Paul Kranenburg, and Matt Fredette.
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 /*
41  * This file contains VME bus-dependent of the `sc' SCSI adapter.
42  * This hardware is frequently found on Sun 2, Sun 3, and Sun 4 machines.
43  *
44  * The SCSI machinery on this adapter is implemented by an Sun custom
45  * chipset, which is handled by the chipset driver in /sys/dev/ic/sunscpal.c
46  */
47 
48 /*
49  * This driver originated as an MD implementation for the `si' VME driver.
50  * The notes pertaining to that history are included below.
51  *
52  * David Jones wrote the initial version of this module for NetBSD/sun3,
53  * which included support for the VME adapter only. (no reselection).
54  *
55  * Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
56  * both the VME and OBIO code to support disconnect/reselect.
57  * (Required figuring out the hardware "features" noted above.)
58  *
59  * The autoconfiguration boilerplate came from Adam Glass.
60  *
61  * Jason R. Thorpe ported the autoconfiguration and VME portions to
62  * NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
63  * a wacky OBIO variant of the VME SCSI-3.  Many thanks to Chuck Cranor
64  * for lots of helpful tips and suggestions.  Thanks also to Paul Kranenburg
65  * and Chris Torek for bits of insight needed along the way.  Thanks to
66  * David Gilbert and Andrew Gillham who risked filesystem life-and-limb
67  * for the sake of testing.  Andrew Gillham helped work out the bugs
68  * the 4/100 DMA code.
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: sc_vme.c,v 1.4 2001/11/15 09:48:19 lukem Exp $");
73 
74 #include "opt_ddb.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/errno.h>
81 #include <sys/device.h>
82 #include <sys/buf.h>
83 
84 #include <machine/bus.h>
85 #include <machine/intr.h>
86 
87 #include <dev/vme/vmereg.h>
88 #include <dev/vme/vmevar.h>
89 
90 #include <dev/scsipi/scsi_all.h>
91 #include <dev/scsipi/scsipi_all.h>
92 #include <dev/scsipi/scsipi_debug.h>
93 #include <dev/scsipi/scsiconf.h>
94 
95 #if !defined(DDB) && !defined(Debugger)
96 #define	Debugger()
97 #endif
98 
99 #ifndef DEBUG
100 #define DEBUG XXX
101 #endif
102 
103 #include <dev/ic/sunscpalvar.h>
104 
105 #include <dev/vme/screg.h>
106 
107 /*
108  * Transfers smaller than this are done using PIO
109  * (on assumption they're not worth DMA overhead)
110  */
111 #define	MIN_DMA_LEN 128
112 
113 int sunsc_vme_options = 0;
114 
115 static int	sc_vme_match __P((struct device *, struct cfdata *, void *));
116 static void	sc_vme_attach __P((struct device *, struct device *, void *));
117 static int	sc_vme_intr __P((void *));
118 
119 /* Auto-configuration glue. */
120 struct cfattach sc_vme_ca = {
121 	sizeof(struct sunscpal_softc), sc_vme_match, sc_vme_attach
122 };
123 
124 static int
125 sc_vme_match(parent, cf, aux)
126 	struct device	*parent;
127 	struct cfdata *cf;
128 	void *aux;
129 {
130 	struct vme_attach_args	*va = aux;
131 	vme_chipset_tag_t	ct = va->va_vct;
132         vme_am_t		mod;
133         vme_addr_t		vme_addr;
134 
135 	/* Make sure there is something there... */
136 	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
137 	vme_addr = va->r[0].offset;
138 
139 	if (vme_probe(ct, vme_addr, 1, mod, VME_D8, NULL, 0) != 0)
140 		return (0);
141 
142 	/*
143 	 * If this is a VME SCSI board, we have to determine whether
144 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
145 	 * be determined using the fact that the "sc" board occupies
146 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
147 	 */
148 	return (vme_probe(ct, vme_addr + 0x801, 1, mod, VME_D8, NULL, 0) == 0);
149 }
150 
151 static void
152 sc_vme_attach(parent, self, aux)
153 	struct device	*parent, *self;
154 	void		*aux;
155 {
156 	struct sunscpal_softc	*sc = (struct sunscpal_softc *) self;
157 	struct vme_attach_args	*va = aux;
158 	vme_chipset_tag_t	ct = va->va_vct;
159 	bus_space_tag_t		bt;
160 	bus_space_handle_t	bh;
161 	vme_mapresc_t resc;
162 	vme_intr_handle_t	ih;
163 	vme_am_t		mod;
164 	int i;
165 
166 	sc->sunscpal_dmat = va->va_bdt;
167 
168 	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
169 
170 	if (vme_space_map(ct, va->r[0].offset, SCREG_BANK_SZ,
171 			  mod, VME_D8, 0, &bt, &bh, &resc) != 0)
172 		panic("%s: vme_space_map", sc->sc_dev.dv_xname);
173 
174 	sc->sunscpal_regt = bt;
175 	sc->sunscpal_regh = bh;
176 
177 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
178 	vme_intr_establish(ct, ih, IPL_BIO, sc_vme_intr, sc);
179 
180 	printf("\n");
181 
182 	/*
183 	 * Initialize fields used by the MI code
184 	 */
185 
186 	/* PAL register bank offsets */
187 	sc->sunscpal_data = SCREG_DATA;
188 	sc->sunscpal_cmd_stat = SCREG_CMD_STAT;
189 	sc->sunscpal_icr = SCREG_ICR;
190 	sc->sunscpal_dma_addr_h = SCREG_DMA_ADDR_H;
191 	sc->sunscpal_dma_addr_l = SCREG_DMA_ADDR_L;
192 	sc->sunscpal_dma_count = SCREG_DMA_COUNT;
193 	sc->sunscpal_intvec = SCREG_INTVEC;
194 
195 	/* Miscellaneous. */
196 	sc->sc_min_dma_len = MIN_DMA_LEN;
197 	sc->sc_rev = SUNSCPAL_VARIANT_501_1045;
198 
199 	/*
200 	 * Allocate DMA handles.
201 	 */
202 	i = SUNSCPAL_OPENINGS * sizeof(struct sunscpal_dma_handle);
203 	sc->sc_dma_handles = (struct sunscpal_dma_handle *)malloc(i, M_DEVBUF, M_NOWAIT);
204 	if (sc->sc_dma_handles == NULL)
205 		panic("sc: dma handle malloc failed\n");
206 
207 	for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
208 		sc->sc_dma_handles[i].dh_flags = 0;
209 
210 		/* Allocate a DMA handle */
211 		if (vme_dmamap_create(
212 				ct,		/* VME chip tag */
213 				SUNSCPAL_MAX_DMA_LEN,	/* size */
214 				VME_AM_A24,	/* address modifier */
215 				VME_D16,	/* data size */
216 				0,		/* swap */
217 				1,		/* nsegments */
218 				SUNSCPAL_MAX_DMA_LEN,	/* maxsegsz */
219 				0,		/* boundary */
220 				BUS_DMA_NOWAIT,
221 				&sc->sc_dma_handles[i].dh_dmamap) != 0) {
222 
223 			printf("%s: DMA buffer map create error\n",
224 				sc->sc_dev.dv_xname);
225 			return;
226 		}
227 	}
228 
229 	/*
230 	 * Set up interrupts on the board.
231 	 */
232 	SUNSCPAL_WRITE_1(sc, sunscpal_intvec, va->ivector & 0xFF);
233 
234 	/* Do the common attach stuff. */
235 	printf("%s", sc->sc_dev.dv_xname);
236 	sunscpal_attach(sc, (sc->sc_dev.dv_cfdata->cf_flags ? sc->sc_dev.dv_cfdata->cf_flags : sunsc_vme_options));
237 }
238 
239 static int
240 sc_vme_intr(void *arg)
241 {
242 	struct sunscpal_softc *sc = arg;
243 	int claimed;
244 
245 	claimed = sunscpal_intr(sc);
246 #ifdef  DEBUG
247 	if (!claimed) {
248         	printf("sc_vme_intr: spurious from SBC\n");
249 	}
250 #endif
251 	/* Yes, we DID cause this interrupt. */
252 	claimed = 1;
253 
254 	return (claimed);
255 }
256 
257