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