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