wdsc.c revision 1.3 1 1.3 thorpej /* $NetBSD: wdsc.c,v 1.3 2001/11/18 05:14:39 thorpej Exp $ */
2 1.1 wdk
3 1.1 wdk /*
4 1.1 wdk * Copyright (c) 2001 Wayne Knowles
5 1.1 wdk * All rights reserved.
6 1.1 wdk *
7 1.1 wdk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 wdk * by Wayne Knowles
9 1.1 wdk *
10 1.1 wdk * Redistribution and use in source and binary forms, with or without
11 1.1 wdk * modification, are permitted provided that the following conditions
12 1.1 wdk * are met:
13 1.1 wdk * 1. Redistributions of source code must retain the above copyright
14 1.1 wdk * notice, this list of conditions and the following disclaimer.
15 1.1 wdk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 wdk * notice, this list of conditions and the following disclaimer in the
17 1.1 wdk * documentation and/or other materials provided with the distribution.
18 1.1 wdk * 3. All advertising materials mentioning features or use of this software
19 1.1 wdk * must display the following acknowledgement:
20 1.1 wdk * This product includes software developed by the NetBSD
21 1.1 wdk * Foundation, Inc. and its contributors.
22 1.1 wdk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 wdk * contributors may be used to endorse or promote products derived
24 1.1 wdk * from this software without specific prior written permission.
25 1.1 wdk *
26 1.1 wdk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 wdk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 wdk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 wdk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 wdk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 wdk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 wdk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 wdk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 wdk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 wdk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 wdk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 wdk */
38 1.1 wdk
39 1.1 wdk /*
40 1.1 wdk * TODO:
41 1.1 wdk * Support for 2nd SCSI controller
42 1.1 wdk */
43 1.1 wdk
44 1.1 wdk #include <sys/param.h>
45 1.1 wdk #include <sys/systm.h>
46 1.1 wdk #include <sys/kernel.h>
47 1.1 wdk #include <sys/device.h>
48 1.1 wdk #include <sys/buf.h>
49 1.1 wdk
50 1.1 wdk #include <dev/scsipi/scsi_all.h>
51 1.1 wdk #include <dev/scsipi/scsipi_all.h>
52 1.1 wdk #include <dev/scsipi/scsiconf.h>
53 1.1 wdk
54 1.1 wdk #include <machine/cpu.h>
55 1.1 wdk #include <machine/bus.h>
56 1.1 wdk #include <machine/autoconf.h>
57 1.1 wdk
58 1.1 wdk #include <sgimips/hpc/hpcvar.h>
59 1.1 wdk #include <sgimips/hpc/hpcreg.h>
60 1.1 wdk #include <sgimips/hpc/hpcdma.h>
61 1.1 wdk
62 1.1 wdk #include <sgimips/hpc/sbicreg.h>
63 1.1 wdk #include <sgimips/hpc/sbicvar.h>
64 1.1 wdk
65 1.2 wdk #include <opt_kgdb.h>
66 1.2 wdk #include <sys/kgdb.h>
67 1.2 wdk
68 1.1 wdk struct wdsc_softc {
69 1.1 wdk struct sbic_softc sc_sbic; /* Must be first */
70 1.2 wdk struct evcnt sc_intrcnt; /* Interrupt counter */
71 1.2 wdk bus_dma_tag_t sc_dmat;
72 1.2 wdk bus_dmamap_t sc_dmamap;
73 1.2 wdk int sc_flags;
74 1.2 wdk #define WDSC_DMA_ACTIVE 0x1
75 1.2 wdk #define WDSC_DMA_MAPLOADED 0x2
76 1.1 wdk struct hpc_dma_softc sc_hpcdma;
77 1.1 wdk };
78 1.1 wdk
79 1.1 wdk
80 1.1 wdk void wdsc_attach __P((struct device *, struct device *, void *));
81 1.1 wdk int wdsc_match __P((struct device *, struct cfdata *, void *));
82 1.1 wdk
83 1.1 wdk struct cfattach wdsc_ca = {
84 1.1 wdk sizeof(struct wdsc_softc), wdsc_match, wdsc_attach
85 1.1 wdk };
86 1.1 wdk
87 1.1 wdk extern struct cfdriver wdsc_cd;
88 1.1 wdk
89 1.2 wdk int wdsc_dmasetup __P((struct sbic_softc *, caddr_t *,size_t *,
90 1.2 wdk int, size_t *));
91 1.1 wdk int wdsc_dmago __P((struct sbic_softc *));
92 1.1 wdk void wdsc_dmastop __P((struct sbic_softc *));
93 1.3 thorpej void wdsc_reset __P((struct sbic_softc *));
94 1.1 wdk int wdsc_dmaintr __P((void *));
95 1.1 wdk int wdsc_scsiintr __P((void *));
96 1.1 wdk
97 1.1 wdk #define MAX_SCSI_XFER (512*1024)
98 1.1 wdk #define MAX_SEG_SZ 8192
99 1.1 wdk #define MAX_DMA_SZ MAX_SCSI_XFER
100 1.1 wdk #define DMA_SEGS (MAX_DMA_SZ/MAX_SEG_SZ)
101 1.1 wdk
102 1.1 wdk /*
103 1.1 wdk * Match for SCSI devices on the onboard WD33C93 chip
104 1.1 wdk */
105 1.1 wdk int
106 1.1 wdk wdsc_match(pdp, cf, auxp)
107 1.2 wdk struct device *pdp;
108 1.2 wdk struct cfdata *cf;
109 1.2 wdk void *auxp;
110 1.1 wdk {
111 1.2 wdk struct hpc_attach_args *haa = auxp;
112 1.1 wdk
113 1.2 wdk if (strcmp(haa->ha_name, wdsc_cd.cd_name))
114 1.2 wdk return (0);
115 1.2 wdk return (1);
116 1.1 wdk }
117 1.1 wdk
118 1.1 wdk /*
119 1.1 wdk * Attach the wdsc driver
120 1.1 wdk */
121 1.1 wdk void
122 1.1 wdk wdsc_attach(pdp, dp, auxp)
123 1.2 wdk struct device *pdp, *dp;
124 1.2 wdk void *auxp;
125 1.1 wdk {
126 1.2 wdk struct sbic_softc *sc = (void *)dp;
127 1.2 wdk struct wdsc_softc *wsc = (void *)dp;
128 1.2 wdk struct hpc_attach_args *haa = auxp;
129 1.2 wdk int err;
130 1.1 wdk
131 1.2 wdk sc->sc_regt = haa->ha_iot;
132 1.2 wdk wsc->sc_dmat = haa->ha_dmat;
133 1.1 wdk
134 1.2 wdk if ((err = bus_space_subregion(haa->ha_iot, haa->ha_ioh,
135 1.1 wdk HPC_SCSI0_DEVREGS,
136 1.1 wdk HPC_SCSI0_DEVREGS_SIZE,
137 1.1 wdk &sc->sc_regh)) != 0) {
138 1.2 wdk printf(": unable to map regs, err=%d\n", err);
139 1.2 wdk return;
140 1.2 wdk }
141 1.1 wdk
142 1.2 wdk if (bus_dmamap_create(wsc->sc_dmat, MAX_DMA_SZ,
143 1.1 wdk DMA_SEGS, MAX_SEG_SZ, MAX_SEG_SZ,
144 1.1 wdk BUS_DMA_WAITOK,
145 1.2 wdk &wsc->sc_dmamap) != 0) {
146 1.1 wdk printf(": failed to create dmamap\n");
147 1.2 wdk return;
148 1.2 wdk }
149 1.1 wdk
150 1.2 wdk sc->sc_dmasetup = wdsc_dmasetup;
151 1.2 wdk sc->sc_dmago = wdsc_dmago;
152 1.2 wdk sc->sc_dmastop = wdsc_dmastop;
153 1.3 thorpej sc->sc_reset = wdsc_reset;
154 1.1 wdk
155 1.2 wdk sc->sc_adapter.adapt_request = sbic_scsi_request;
156 1.2 wdk sc->sc_adapter.adapt_minphys = minphys;
157 1.1 wdk
158 1.2 wdk sc->sc_id = 7; /* Host ID = 7 */
159 1.2 wdk sc->sc_clkfreq = 200; /* 20MHz Clock */
160 1.1 wdk
161 1.2 wdk evcnt_attach_dynamic(&wsc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
162 1.2 wdk sc->sc_dev.dv_xname, "intr");
163 1.1 wdk
164 1.2 wdk /* XXX: 1 = IRQ_LOCAL0 + 1 */
165 1.2 wdk if ((cpu_intr_establish(1, IPL_BIO, wdsc_scsiintr, sc)) == NULL) {
166 1.2 wdk printf(": unable to establish interrupt!\n");
167 1.2 wdk return;
168 1.2 wdk }
169 1.1 wdk
170 1.2 wdk hpcdma_init(haa, &wsc->sc_hpcdma, DMA_SEGS);
171 1.2 wdk sbic_attach(sc);
172 1.2 wdk return;
173 1.1 wdk }
174 1.1 wdk
175 1.1 wdk /*
176 1.1 wdk * Prime the hardware for a DMA transfer
177 1.2 wdk *
178 1.2 wdk * Requires splbio() interrupts to be disabled by the caller
179 1.1 wdk */
180 1.1 wdk int
181 1.2 wdk wdsc_dmasetup(dev, addr, len, datain, dmasize)
182 1.2 wdk struct sbic_softc *dev;
183 1.2 wdk caddr_t *addr;
184 1.2 wdk size_t *len;
185 1.2 wdk int datain;
186 1.2 wdk size_t *dmasize;
187 1.1 wdk {
188 1.2 wdk struct wdsc_softc *wsc = (void *)dev;
189 1.2 wdk struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
190 1.2 wdk int count, err;
191 1.2 wdk void *vaddr;
192 1.2 wdk
193 1.2 wdk KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
194 1.2 wdk
195 1.2 wdk vaddr = *addr;
196 1.2 wdk count = dsc->sc_dlen = *len;
197 1.2 wdk if (count) {
198 1.2 wdk KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED) == 0);
199 1.1 wdk
200 1.2 wdk /* Build list of physical addresses for this transfer */
201 1.2 wdk if ((err=bus_dmamap_load(wsc->sc_dmat, wsc->sc_dmamap,
202 1.1 wdk vaddr, count,
203 1.1 wdk NULL /* kernel address */,
204 1.1 wdk BUS_DMA_NOWAIT)) != 0)
205 1.2 wdk panic("%s: bus_dmamap_load err=%d",
206 1.2 wdk dev->sc_dev.dv_xname, err);
207 1.2 wdk
208 1.2 wdk hpcdma_sglist_create(dsc, wsc->sc_dmamap);
209 1.2 wdk wsc->sc_flags |= WDSC_DMA_MAPLOADED;
210 1.2 wdk
211 1.2 wdk if (datain) {
212 1.2 wdk dsc->sc_dmacmd = HPC_DMACTL_ACTIVE;
213 1.2 wdk dsc->sc_flags |= HPCDMA_READ;
214 1.2 wdk } else {
215 1.2 wdk dsc->sc_dmacmd = HPC_DMACTL_ACTIVE | HPC_DMACTL_DIR;
216 1.2 wdk dsc->sc_flags &= ~HPCDMA_READ;
217 1.2 wdk }
218 1.2 wdk }
219 1.2 wdk return(count);
220 1.1 wdk }
221 1.1 wdk
222 1.1 wdk /*
223 1.1 wdk * Prime the hardware for the next DMA transfer
224 1.1 wdk */
225 1.1 wdk int
226 1.1 wdk wdsc_dmago(dev)
227 1.2 wdk struct sbic_softc *dev;
228 1.1 wdk {
229 1.2 wdk struct wdsc_softc *wsc = (void *)dev;
230 1.2 wdk struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
231 1.1 wdk
232 1.2 wdk if (dsc->sc_dlen == 0)
233 1.2 wdk return(0);
234 1.1 wdk
235 1.2 wdk KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
236 1.2 wdk KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED));
237 1.1 wdk
238 1.2 wdk wsc->sc_flags |= WDSC_DMA_ACTIVE;
239 1.1 wdk
240 1.2 wdk bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
241 1.2 wdk wsc->sc_dmamap->dm_mapsize,
242 1.2 wdk BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
243 1.1 wdk
244 1.2 wdk hpcdma_cntl(dsc, dsc->sc_dmacmd); /* Thunderbirds are go! */
245 1.2 wdk
246 1.2 wdk return(wsc->sc_dmamap->dm_mapsize);
247 1.1 wdk }
248 1.1 wdk
249 1.1 wdk /*
250 1.2 wdk * Stop DMA and unload active DMA maps
251 1.1 wdk */
252 1.1 wdk void
253 1.1 wdk wdsc_dmastop(dev)
254 1.2 wdk struct sbic_softc *dev;
255 1.1 wdk {
256 1.2 wdk struct wdsc_softc *wsc = (void *)dev;
257 1.2 wdk struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
258 1.1 wdk
259 1.2 wdk if (wsc->sc_flags & WDSC_DMA_ACTIVE) {
260 1.2 wdk if (dsc->sc_flags & HPCDMA_READ)
261 1.2 wdk hpcdma_flush(dsc);
262 1.2 wdk hpcdma_cntl(dsc, 0); /* Stop DMA */
263 1.2 wdk bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
264 1.2 wdk wsc->sc_dmamap->dm_mapsize,
265 1.2 wdk BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
266 1.2 wdk }
267 1.2 wdk if (wsc->sc_flags & WDSC_DMA_MAPLOADED)
268 1.2 wdk bus_dmamap_unload(wsc->sc_dmat, wsc->sc_dmamap);
269 1.2 wdk wsc->sc_flags &= ~(WDSC_DMA_ACTIVE | WDSC_DMA_MAPLOADED);
270 1.3 thorpej }
271 1.3 thorpej
272 1.3 thorpej /*
273 1.3 thorpej * Reset the controller.
274 1.3 thorpej */
275 1.3 thorpej void
276 1.3 thorpej wdsc_reset(dev)
277 1.3 thorpej struct sbic_softc *dev;
278 1.3 thorpej {
279 1.3 thorpej struct wdsc_softc *wsc = (void *)dev;
280 1.3 thorpej struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
281 1.3 thorpej
282 1.3 thorpej hpcdma_reset(dsc);
283 1.1 wdk }
284 1.1 wdk
285 1.1 wdk /*
286 1.2 wdk * WD33c93 SCSI controller interrupt
287 1.1 wdk */
288 1.1 wdk int
289 1.1 wdk wdsc_scsiintr(arg)
290 1.2 wdk void *arg;
291 1.1 wdk {
292 1.2 wdk struct sbic_softc *dev = arg;
293 1.2 wdk struct wdsc_softc *wsc = arg;
294 1.2 wdk int found;
295 1.2 wdk
296 1.2 wdk found = sbic_intr(dev);
297 1.2 wdk if (found)
298 1.2 wdk wsc->sc_intrcnt.ev_count++;
299 1.2 wdk return(found);
300 1.1 wdk }
301