wdsc.c revision 1.4 1 /* $NetBSD: wdsc.c,v 1.4 2001/11/18 08:16:16 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wayne Knowles
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wayne Knowles
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 * TODO:
41 * Support for 2nd SCSI controller
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <sys/buf.h>
49
50 #include <dev/scsipi/scsi_all.h>
51 #include <dev/scsipi/scsipi_all.h>
52 #include <dev/scsipi/scsiconf.h>
53
54 #include <machine/cpu.h>
55 #include <machine/bus.h>
56 #include <machine/autoconf.h>
57
58 #include <sgimips/hpc/hpcvar.h>
59 #include <sgimips/hpc/hpcreg.h>
60 #include <sgimips/hpc/hpcdma.h>
61
62 #include <sgimips/hpc/sbicreg.h>
63 #include <sgimips/hpc/sbicvar.h>
64
65 #include <opt_kgdb.h>
66 #include <sys/kgdb.h>
67
68 struct wdsc_softc {
69 struct sbic_softc sc_sbic; /* Must be first */
70 struct evcnt sc_intrcnt; /* Interrupt counter */
71 bus_dma_tag_t sc_dmat;
72 bus_dmamap_t sc_dmamap;
73 int sc_flags;
74 #define WDSC_DMA_ACTIVE 0x1
75 #define WDSC_DMA_MAPLOADED 0x2
76 struct hpc_dma_softc sc_hpcdma;
77 };
78
79
80 void wdsc_attach __P((struct device *, struct device *, void *));
81 int wdsc_match __P((struct device *, struct cfdata *, void *));
82
83 struct cfattach wdsc_ca = {
84 sizeof(struct wdsc_softc), wdsc_match, wdsc_attach
85 };
86
87 int wdsc_dmasetup __P((struct sbic_softc *, caddr_t *,size_t *,
88 int, size_t *));
89 int wdsc_dmago __P((struct sbic_softc *));
90 void wdsc_dmastop __P((struct sbic_softc *));
91 void wdsc_reset __P((struct sbic_softc *));
92 int wdsc_dmaintr __P((void *));
93 int wdsc_scsiintr __P((void *));
94
95 #define MAX_SCSI_XFER (512*1024)
96 #define MAX_SEG_SZ 8192
97 #define MAX_DMA_SZ MAX_SCSI_XFER
98 #define DMA_SEGS (MAX_DMA_SZ/MAX_SEG_SZ)
99
100 /*
101 * Match for SCSI devices on the onboard WD33C93 chip
102 */
103 int
104 wdsc_match(pdp, cf, auxp)
105 struct device *pdp;
106 struct cfdata *cf;
107 void *auxp;
108 {
109 struct hpc_attach_args *haa = auxp;
110
111 if (strcmp(haa->ha_name, cf->cf_driver->cd_name) == 0)
112 return (1);
113
114 return (0);
115 }
116
117 /*
118 * Attach the wdsc driver
119 */
120 void
121 wdsc_attach(pdp, dp, auxp)
122 struct device *pdp, *dp;
123 void *auxp;
124 {
125 struct sbic_softc *sc = (void *)dp;
126 struct wdsc_softc *wsc = (void *)dp;
127 struct hpc_attach_args *haa = auxp;
128 int err;
129
130 sc->sc_regt = haa->ha_st;
131 wsc->sc_dmat = haa->ha_dmat;
132
133 if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
134 haa->ha_devoff,
135 HPC_SCSI0_DEVREGS_SIZE,
136 &sc->sc_regh)) != 0) {
137 printf(": unable to map regs, err=%d\n", err);
138 return;
139 }
140
141 if (bus_dmamap_create(wsc->sc_dmat, MAX_DMA_SZ,
142 DMA_SEGS, MAX_SEG_SZ, MAX_SEG_SZ,
143 BUS_DMA_WAITOK,
144 &wsc->sc_dmamap) != 0) {
145 printf(": failed to create dmamap\n");
146 return;
147 }
148
149 sc->sc_dmasetup = wdsc_dmasetup;
150 sc->sc_dmago = wdsc_dmago;
151 sc->sc_dmastop = wdsc_dmastop;
152 sc->sc_reset = wdsc_reset;
153
154 sc->sc_adapter.adapt_request = sbic_scsi_request;
155 sc->sc_adapter.adapt_minphys = minphys;
156
157 sc->sc_id = 7; /* Host ID = 7 */
158 sc->sc_clkfreq = 200; /* 20MHz Clock */
159
160 evcnt_attach_dynamic(&wsc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
161 sc->sc_dev.dv_xname, "intr");
162
163 if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
164 wdsc_scsiintr, sc)) == NULL) {
165 printf(": unable to establish interrupt!\n");
166 return;
167 }
168
169 hpcdma_init(haa, &wsc->sc_hpcdma, DMA_SEGS);
170 sbic_attach(sc);
171 return;
172 }
173
174 /*
175 * Prime the hardware for a DMA transfer
176 *
177 * Requires splbio() interrupts to be disabled by the caller
178 */
179 int
180 wdsc_dmasetup(dev, addr, len, datain, dmasize)
181 struct sbic_softc *dev;
182 caddr_t *addr;
183 size_t *len;
184 int datain;
185 size_t *dmasize;
186 {
187 struct wdsc_softc *wsc = (void *)dev;
188 struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
189 int count, err;
190 void *vaddr;
191
192 KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
193
194 vaddr = *addr;
195 count = dsc->sc_dlen = *len;
196 if (count) {
197 KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED) == 0);
198
199 /* Build list of physical addresses for this transfer */
200 if ((err=bus_dmamap_load(wsc->sc_dmat, wsc->sc_dmamap,
201 vaddr, count,
202 NULL /* kernel address */,
203 BUS_DMA_NOWAIT)) != 0)
204 panic("%s: bus_dmamap_load err=%d",
205 dev->sc_dev.dv_xname, err);
206
207 hpcdma_sglist_create(dsc, wsc->sc_dmamap);
208 wsc->sc_flags |= WDSC_DMA_MAPLOADED;
209
210 if (datain) {
211 dsc->sc_dmacmd = HPC_DMACTL_ACTIVE;
212 dsc->sc_flags |= HPCDMA_READ;
213 } else {
214 dsc->sc_dmacmd = HPC_DMACTL_ACTIVE | HPC_DMACTL_DIR;
215 dsc->sc_flags &= ~HPCDMA_READ;
216 }
217 }
218 return(count);
219 }
220
221 /*
222 * Prime the hardware for the next DMA transfer
223 */
224 int
225 wdsc_dmago(dev)
226 struct sbic_softc *dev;
227 {
228 struct wdsc_softc *wsc = (void *)dev;
229 struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
230
231 if (dsc->sc_dlen == 0)
232 return(0);
233
234 KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
235 KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED));
236
237 wsc->sc_flags |= WDSC_DMA_ACTIVE;
238
239 bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
240 wsc->sc_dmamap->dm_mapsize,
241 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
242
243 hpcdma_cntl(dsc, dsc->sc_dmacmd); /* Thunderbirds are go! */
244
245 return(wsc->sc_dmamap->dm_mapsize);
246 }
247
248 /*
249 * Stop DMA and unload active DMA maps
250 */
251 void
252 wdsc_dmastop(dev)
253 struct sbic_softc *dev;
254 {
255 struct wdsc_softc *wsc = (void *)dev;
256 struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
257
258 if (wsc->sc_flags & WDSC_DMA_ACTIVE) {
259 if (dsc->sc_flags & HPCDMA_READ)
260 hpcdma_flush(dsc);
261 hpcdma_cntl(dsc, 0); /* Stop DMA */
262 bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
263 wsc->sc_dmamap->dm_mapsize,
264 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
265 }
266 if (wsc->sc_flags & WDSC_DMA_MAPLOADED)
267 bus_dmamap_unload(wsc->sc_dmat, wsc->sc_dmamap);
268 wsc->sc_flags &= ~(WDSC_DMA_ACTIVE | WDSC_DMA_MAPLOADED);
269 }
270
271 /*
272 * Reset the controller.
273 */
274 void
275 wdsc_reset(dev)
276 struct sbic_softc *dev;
277 {
278 struct wdsc_softc *wsc = (void *)dev;
279 struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
280
281 hpcdma_reset(dsc);
282 }
283
284 /*
285 * WD33c93 SCSI controller interrupt
286 */
287 int
288 wdsc_scsiintr(arg)
289 void *arg;
290 {
291 struct sbic_softc *dev = arg;
292 struct wdsc_softc *wsc = arg;
293 int found;
294
295 found = sbic_intr(dev);
296 if (found)
297 wsc->sc_intrcnt.ev_count++;
298 return(found);
299 }
300