xref: /netbsd-src/sys/arch/sun2/dev/sc_mbmem.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: sc_mbmem.c,v 1.8 2003/07/15 03:36:12 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 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, and Matthew Fredette.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * This file contains only the machine-dependent parts of the
41  * Sun-2 SCSI driver.  (Autoconfig stuff and DMA functions.)
42  * The machine-independent parts are in sunscpal.c
43  *
44  * Supported hardware includes:
45  * Sun SCSI-2 on Multibus (Sun2/120,Sun2/170)
46  * Sun SCSI-2 on VME (Sun2/50,Sun3/160,Sun3/260,others?)
47  *
48  * Credits, history:
49  *
50  * Matthew Fredette took revision 1.15 of si_vme.c, and then heavily
51  * modified it to support the Sun sc.  The remaining credits
52  * are from si_vme.c:
53  *
54  * David Jones wrote the initial version of this module, which
55  * included support for the VME adapter only. (no reselection).
56  *
57  * Gordon Ross added support for the OBIO adapter, and re-worked
58  * both the VME and OBIO code to support disconnect/reselect.
59  * (Required figuring out the hardware "features" noted above.)
60  *
61  * The autoconfiguration boilerplate came from Adam Glass.
62  */
63 
64 /*****************************************************************
65  * Multibus functions for sc
66  ****************************************************************/
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: sc_mbmem.c,v 1.8 2003/07/15 03:36:12 lukem Exp $");
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/errno.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/device.h>
77 #include <sys/buf.h>
78 #include <sys/proc.h>
79 #include <sys/user.h>
80 
81 #include <dev/scsipi/scsi_all.h>
82 #include <dev/scsipi/scsipi_all.h>
83 #include <dev/scsipi/scsipi_debug.h>
84 #include <dev/scsipi/scsiconf.h>
85 
86 #include <machine/autoconf.h>
87 #include <machine/bus.h>
88 
89 /* #define DEBUG XXX */
90 
91 #include <dev/ic/sunscpalreg.h>
92 #include <dev/ic/sunscpalvar.h>
93 
94 /* Yes, we use a VME header file. */
95 #include <dev/vme/screg.h>
96 
97 /*
98  * Transfers smaller than this are done using PIO
99  * (on assumption they're not worth DMA overhead)
100  */
101 #define	MIN_DMA_LEN 128
102 
103 /*
104  * New-style autoconfig attachment
105  */
106 
107 static int	sunsc_mbmem_match __P((struct device *, struct cfdata *, void *));
108 static void	sunsc_mbmem_attach __P((struct device *, struct device *, void *));
109 static int	sunsc_mbmem_intr __P((void *));
110 
111 CFATTACH_DECL(sc_mbmem, sizeof(struct sunscpal_softc),
112     sunsc_mbmem_match, sunsc_mbmem_attach, NULL, NULL);
113 
114 /*
115  * Options for parity, DMA, and interrupts.
116  */
117 int sunsc_mbmem_options = 0x00;
118 
119 static int
120 sunsc_mbmem_match(parent, cf, aux)
121 	struct device *parent;
122 	struct cfdata *cf;
123 	void *aux;
124 {
125 	struct mbmem_attach_args *mbma = aux;
126 	bus_space_handle_t bh;
127 	int matched;
128 
129 	/* No default Multibus address. */
130 	if (mbma->mbma_paddr == -1)
131 		return (0);
132 
133 	/* Make sure there is something there... */
134 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, SCREG_BANK_SZ,
135 			  0, &bh))
136 		return (0);
137 	matched = (bus_space_peek_2(mbma->mbma_bustag, bh, SCREG_ICR, NULL) == 0);
138 	bus_space_unmap(mbma->mbma_bustag, bh, SCREG_BANK_SZ);
139 	if (!matched)
140 		return (0);
141 
142 	/* Default interrupt priority. */
143 	if (mbma->mbma_pri == -1)
144 		mbma->mbma_pri = 2;
145 
146 	return (1);
147 }
148 
149 static void
150 sunsc_mbmem_attach(parent, self, args)
151 	struct device	*parent, *self;
152 	void		*args;
153 {
154 	struct sunscpal_softc *sc = (void *) self;
155 	struct cfdata *cf = self->dv_cfdata;
156 	struct mbmem_attach_args *mbma = args;
157 	int i;
158 
159 	/* Map in the device. */
160 	sc->sunscpal_regt = mbma->mbma_bustag;
161 	sc->sunscpal_dmat = mbma->mbma_dmatag;
162 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, SCREG_BANK_SZ,
163 			  0, &sc->sunscpal_regh))
164 		panic("sunsc_mbmem_attach: can't map");
165 
166 	/* Device register offsets. */
167 	sc->sunscpal_data = SCREG_DATA;
168 	sc->sunscpal_cmd_stat = SCREG_CMD_STAT;
169 	sc->sunscpal_icr = SCREG_ICR;
170 	sc->sunscpal_dma_addr_h = SCREG_DMA_ADDR_H;
171 	sc->sunscpal_dma_addr_l = SCREG_DMA_ADDR_L;
172 	sc->sunscpal_dma_count = SCREG_DMA_COUNT;
173 	sc->sunscpal_intvec = SCREG_INTVEC;
174 
175 	/* Allocate DMA handles. */
176 	i = SUNSCPAL_OPENINGS * sizeof(struct sunscpal_dma_handle);
177 	sc->sc_dma_handles = (sunscpal_dma_handle_t)
178 		malloc(i, M_DEVBUF, M_WAITOK);
179 	if (sc->sc_dma_handles == NULL)
180 		panic("sc: DMA handles malloc failed");
181 	for (i = 0; i < SUNSCPAL_OPENINGS; i++)
182 		if (bus_dmamap_create(sc->sunscpal_dmat, SUNSCPAL_MAX_DMA_LEN,
183 				      1, SUNSCPAL_MAX_DMA_LEN,
184 				      0, BUS_DMA_WAITOK, &sc->sc_dma_handles[i].
185 dh_dmamap) != 0)
186 			panic("sc: DMA map create failed");
187 
188 	/* Miscellaneous. */
189 	sc->sc_min_dma_len = MIN_DMA_LEN;
190 	sc->sc_rev = SUNSCPAL_VARIANT_501_1006;
191 
192 	/* Attach interrupt handler. */
193 	bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_BIO, 0,
194 			   sunsc_mbmem_intr, sc);
195 
196 	/* Do the common attach stuff. */
197 	sunscpal_attach(sc, (cf->cf_flags ? cf->cf_flags : sunsc_mbmem_options));
198 }
199 
200 static int
201 sunsc_mbmem_intr(void *arg)
202 {
203 	struct sunscpal_softc *sc = arg;
204 	int claimed;
205 
206 	claimed = sunscpal_intr(sc);
207 #ifdef	DEBUG
208 	if (!claimed) {
209 		printf("sunsc_intr: spurious from SBC\n");
210 	}
211 #endif
212 	/* Yes, we DID cause this interrupt. */
213 	claimed = 1;
214 
215 	return (claimed);
216 }
217