xref: /netbsd-src/sys/dev/tc/asc_tcds.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /* $NetBSD: asc_tcds.c,v 1.17 2006/05/14 21:47:00 elad Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1994 Peter Galbavy.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by Peter Galbavy.
54  * 4. The name of the author may not be used to endorse or promote products
55  *    derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: asc_tcds.c,v 1.17 2006/05/14 21:47:00 elad Exp $");
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/device.h>
75 #include <sys/buf.h>
76 
77 #include <uvm/uvm_extern.h>
78 
79 #include <dev/scsipi/scsi_all.h>
80 #include <dev/scsipi/scsipi_all.h>
81 #include <dev/scsipi/scsiconf.h>
82 
83 #include <dev/ic/ncr53c9xreg.h>
84 #include <dev/ic/ncr53c9xvar.h>
85 
86 #include <machine/bus.h>
87 
88 #include <dev/tc/tcvar.h>
89 #include <dev/tc/tcdsreg.h>
90 #include <dev/tc/tcdsvar.h>
91 
92 struct asc_softc {
93 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
94 	bus_space_tag_t sc_bst;			/* bus space tag */
95 	bus_space_handle_t sc_scsi_bsh;		/* ASC register handle */
96 	bus_dma_tag_t sc_dmat;			/* bus DMA tag */
97 	bus_dmamap_t sc_dmamap;			/* bus dmamap */
98 	caddr_t *sc_dmaaddr;
99 	size_t *sc_dmalen;
100 	size_t sc_dmasize;
101 	unsigned sc_flags;
102 #define	ASC_ISPULLUP		0x01
103 #define	ASC_DMAACTIVE		0x02
104 #define	ASC_MAPLOADED		0x04
105 	struct tcds_slotconfig *sc_tcds;	/* DMA/slot info lives here */
106 };
107 
108 static int  asc_tcds_match (struct device *, struct cfdata *, void *);
109 static void asc_tcds_attach(struct device *, struct device *, void *);
110 
111 CFATTACH_DECL(asc_tcds, sizeof(struct asc_softc),
112     asc_tcds_match, asc_tcds_attach, NULL, NULL);
113 
114 /*
115  * Functions and the switch for the MI code.
116  */
117 static u_char	asc_read_reg(struct ncr53c9x_softc *, int);
118 static void	asc_write_reg(struct ncr53c9x_softc *, int, u_char);
119 static int	tcds_dma_isintr(struct ncr53c9x_softc *);
120 static void	tcds_dma_reset(struct ncr53c9x_softc *);
121 static int	tcds_dma_intr(struct ncr53c9x_softc *);
122 static int	tcds_dma_setup(struct ncr53c9x_softc *, caddr_t *,
123 	    size_t *, int, size_t *);
124 static void	tcds_dma_go(struct ncr53c9x_softc *);
125 static void	tcds_dma_stop(struct ncr53c9x_softc *);
126 static int	tcds_dma_isactive(struct ncr53c9x_softc *);
127 static void	tcds_clear_latched_intr(struct ncr53c9x_softc *);
128 
129 static struct ncr53c9x_glue asc_tcds_glue = {
130 	asc_read_reg,
131 	asc_write_reg,
132 	tcds_dma_isintr,
133 	tcds_dma_reset,
134 	tcds_dma_intr,
135 	tcds_dma_setup,
136 	tcds_dma_go,
137 	tcds_dma_stop,
138 	tcds_dma_isactive,
139 	tcds_clear_latched_intr,
140 };
141 
142 static int
143 asc_tcds_match(struct device *parent, struct cfdata *cf, void *aux)
144 {
145 
146 	/* We always exist. */
147 	return 1;
148 }
149 
150 #define DMAMAX(a)	(PAGE_SIZE - ((a) & (PAGE_SIZE - 1)))
151 
152 /*
153  * Attach this instance, and then all the sub-devices
154  */
155 static void
156 asc_tcds_attach(struct device *parent, struct device *self, void *aux)
157 {
158 	struct tcdsdev_attach_args *tcdsdev = aux;
159 	struct asc_softc *asc = device_private(self);
160 	struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
161 	int error;
162 
163 	/*
164 	 * Set up glue for MI code early; we use some of it here.
165 	 */
166 	sc->sc_glue = &asc_tcds_glue;
167 
168 	asc->sc_bst = tcdsdev->tcdsda_bst;
169 	asc->sc_scsi_bsh = tcdsdev->tcdsda_bsh;
170 	asc->sc_tcds = tcdsdev->tcdsda_sc;
171 
172 	/*
173 	 * The TCDS ASIC cannot DMA across 8k boundaries, and this
174 	 * driver is written such that each DMA segment gets a new
175 	 * call to tcds_dma_setup().  Thus, the DMA map only needs
176 	 * to support 8k transfers.
177 	 */
178 	asc->sc_dmat = tcdsdev->tcdsda_dmat;
179 	if ((error = bus_dmamap_create(asc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
180 	    PAGE_SIZE, BUS_DMA_NOWAIT, &asc->sc_dmamap)) < 0) {
181 		printf("failed to create DMA map, error = %d\n", error);
182 	}
183 
184 	sc->sc_id = tcdsdev->tcdsda_id;
185 	sc->sc_freq = tcdsdev->tcdsda_freq;
186 
187 	/* gimme MHz */
188 	sc->sc_freq /= 1000000;
189 
190 	tcds_intr_establish(parent, tcdsdev->tcdsda_chip, ncr53c9x_intr, sc);
191 
192 	/*
193 	 * XXX More of this should be in ncr53c9x_attach(), but
194 	 * XXX should we really poke around the chip that much in
195 	 * XXX the MI code?  Think about this more...
196 	 */
197 
198 	/*
199 	 * Set up static configuration info.
200 	 */
201 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
202 	sc->sc_cfg2 = NCRCFG2_SCSI2;
203 	sc->sc_cfg3 = NCRCFG3_CDB;
204 	if (sc->sc_freq > 25)
205 		sc->sc_cfg3 |= NCRF9XCFG3_FCLK;
206 	sc->sc_rev = tcdsdev->tcdsda_variant;
207 	if (tcdsdev->tcdsda_fast) {
208 		sc->sc_features |= NCR_F_FASTSCSI;
209 		sc->sc_cfg3_fscsi = NCRF9XCFG3_FSCSI;
210 	}
211 
212 	/*
213 	 * XXX minsync and maxxfer _should_ be set up in MI code,
214 	 * XXX but it appears to have some dependency on what sort
215 	 * XXX of DMA we're hooked up to, etc.
216 	 */
217 
218 	/*
219 	 * This is the value used to start sync negotiations
220 	 * Note that the NCR register "SYNCTP" is programmed
221 	 * in "clocks per byte", and has a minimum value of 4.
222 	 * The SCSI period used in negotiation is one-fourth
223 	 * of the time (in nanoseconds) needed to transfer one byte.
224 	 * Since the chip's clock is given in MHz, we have the following
225 	 * formula: 4 * period = (1000 / freq) * 4
226 	 */
227 	sc->sc_minsync = (1000 / sc->sc_freq) * tcdsdev->tcdsda_period / 4;
228 
229 	sc->sc_maxxfer = 64 * 1024;
230 
231 	/* Do the common parts of attachment. */
232 	sc->sc_adapter.adapt_minphys = minphys;
233 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
234 	ncr53c9x_attach(sc);
235 }
236 
237 static void
238 tcds_dma_reset(struct ncr53c9x_softc *sc)
239 {
240 	struct asc_softc *asc = (struct asc_softc *)sc;
241 
242 	/* TCDS SCSI disable/reset/enable. */
243 	tcds_scsi_reset(asc->sc_tcds);			/* XXX */
244 
245 	if (asc->sc_flags & ASC_MAPLOADED)
246 		bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
247 	asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
248 }
249 
250 /*
251  * start a DMA transfer or keep it going
252  */
253 int
254 tcds_dma_setup(struct ncr53c9x_softc *sc, caddr_t *addr, size_t *len,
255     int ispullup, size_t *dmasize)
256 {
257 	struct asc_softc *asc = (struct asc_softc *)sc;
258 	struct tcds_slotconfig *tcds = asc->sc_tcds;
259 	size_t size;
260 	u_int32_t dic;
261 
262 	NCR_DMA(("tcds_dma %d: start %d@%p,%s\n", tcds->sc_slot,
263 		(int)*asc->sc_dmalen, *asc->sc_dmaaddr,
264 		(ispullup) ? "IN" : "OUT"));
265 
266 	/*
267 	 * the rules say we cannot transfer more than the limit
268 	 * of this DMA chip (64k) and we cannot cross a 8k boundary.
269 	 */
270 	size = min(*dmasize, DMAMAX((size_t)*addr));
271 	asc->sc_dmaaddr = addr;
272 	asc->sc_dmalen = len;
273 	asc->sc_flags = (ispullup) ? ASC_ISPULLUP : 0;
274 	*dmasize = asc->sc_dmasize = size;
275 
276 	NCR_DMA(("dma_start: dmasize = %d\n", (int)size));
277 
278 	if (size == 0)
279 		return 0;
280 
281 	if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap, *addr, size,
282 	    NULL, BUS_DMA_NOWAIT | (ispullup ? BUS_DMA_READ : BUS_DMA_WRITE))) {
283 		/*
284 		 * XXX Should return an error, here, but the upper-layer
285 		 * XXX doesn't check the return value!
286 		 */
287 		panic("tcds_dma_setup: dmamap load failed");
288 	}
289 
290 	/* synchronize dmamap contents with memory image */
291 	bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap, 0, size,
292 		(ispullup) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
293 
294 	/* load address, set/clear unaligned transfer and read/write bits. */
295 	bus_space_write_4(tcds->sc_bst, tcds->sc_bsh, tcds->sc_sda,
296 	    asc->sc_dmamap->dm_segs[0].ds_addr >> 2);
297 	dic = bus_space_read_4(tcds->sc_bst, tcds->sc_bsh, tcds->sc_dic);
298 	dic &= ~TCDS_DIC_ADDRMASK;
299 	dic |= asc->sc_dmamap->dm_segs[0].ds_addr & TCDS_DIC_ADDRMASK;
300 	if (ispullup)
301 		dic |= TCDS_DIC_WRITE;
302 	else
303 		dic &= ~TCDS_DIC_WRITE;
304 	bus_space_write_4(tcds->sc_bst, tcds->sc_bsh, tcds->sc_dic, dic);
305 
306 	asc->sc_flags |= ASC_MAPLOADED;
307 	return 0;
308 }
309 
310 static void
311 tcds_dma_go(struct ncr53c9x_softc *sc)
312 {
313 	struct asc_softc *asc = (struct asc_softc *)sc;
314 
315 	/* mark unit as DMA-active */
316 	asc->sc_flags |= ASC_DMAACTIVE;
317 
318 	/* start DMA */
319 	tcds_dma_enable(asc->sc_tcds, 1);
320 }
321 
322 static void
323 tcds_dma_stop(struct ncr53c9x_softc *sc)
324 {
325 #if 0
326 	struct asc_softc *asc = (struct asc_softc *)sc;
327 #endif
328 
329 	/*
330 	 * XXX STOP DMA HERE!
331 	 */
332 }
333 
334 /*
335  * Pseudo (chained) interrupt from the asc driver to kick the
336  * current running DMA transfer. Called from ncr53c9x_intr()
337  * for now.
338  *
339  * return 1 if it was a DMA continue.
340  */
341 static int
342 tcds_dma_intr(struct ncr53c9x_softc *sc)
343 {
344 	struct asc_softc *asc = (struct asc_softc *)sc;
345 	struct tcds_slotconfig *tcds = asc->sc_tcds;
346 	int trans, resid;
347 	u_int32_t tcl, tcm;
348 	u_int32_t dud, dudmask, *addr;
349 	bus_addr_t pa;
350 
351 	NCR_DMA(("tcds_dma %d: intr", tcds->sc_slot));
352 
353 	if (tcds_scsi_iserr(tcds))
354 		return 0;
355 
356 	/* This is an "assertion" :) */
357 	if ((asc->sc_flags & ASC_DMAACTIVE) == 0)
358 		panic("tcds_dma_intr: DMA wasn't active");
359 
360 	/* DMA has stopped */
361 	tcds_dma_enable(tcds, 0);
362 	asc->sc_flags &= ~ASC_DMAACTIVE;
363 
364 	if (asc->sc_dmasize == 0) {
365 		/* A "Transfer Pad" operation completed */
366 		tcl = NCR_READ_REG(sc, NCR_TCL);
367 		tcm = NCR_READ_REG(sc, NCR_TCM);
368 		NCR_DMA(("dma_intr: discarded %d bytes (tcl=%d, tcm=%d)\n",
369 		    tcl | (tcm << 8), tcl, tcm));
370 		return 0;
371 	}
372 
373 	resid = 0;
374 	if ((asc->sc_flags & ASC_ISPULLUP) == 0 &&
375 	    (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
376 		NCR_DMA(("dma_intr: empty esp FIFO of %d ", resid));
377 		DELAY(1);
378 	}
379 
380 	resid += (tcl = NCR_READ_REG(sc, NCR_TCL));
381 	resid += (tcm = NCR_READ_REG(sc, NCR_TCM)) << 8;
382 
383 	trans = asc->sc_dmasize - resid;
384 	if (trans < 0) {			/* transferred < 0 ? */
385 		printf("tcds_dma %d: xfer (%d) > req (%d)\n",
386 		    tcds->sc_slot, trans, (int)asc->sc_dmasize);
387 		trans = asc->sc_dmasize;
388 	}
389 
390 	NCR_DMA(("dma_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
391 	    tcl, tcm, trans, resid));
392 
393 	*asc->sc_dmalen -= trans;
394 	*asc->sc_dmaaddr += trans;
395 
396 	bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
397 			0, asc->sc_dmamap->dm_mapsize,
398 			(sc->sc_flags & ASC_ISPULLUP)
399 				? BUS_DMASYNC_POSTREAD
400 				: BUS_DMASYNC_POSTWRITE);
401 
402 	/*
403 	 * Clean up unaligned DMAs into main memory.
404 	 */
405 	if (asc->sc_flags & ASC_ISPULLUP) {
406 		/* Handle unaligned starting address, length. */
407 		dud = bus_space_read_4(tcds->sc_bst,
408 		    tcds->sc_bsh, tcds->sc_dud0);
409 		if ((dud & TCDS_DUD0_VALIDBITS) != 0) {
410 			addr = (u_int32_t *)
411 			    ((paddr_t)*asc->sc_dmaaddr & ~0x3);
412 			dudmask = 0;
413 			if (dud & TCDS_DUD0_VALID00)
414 				panic("tcds_dma: dud0 byte 0 valid");
415 			if (dud & TCDS_DUD0_VALID01)
416 				dudmask |= TCDS_DUD_BYTE01;
417 			if (dud & TCDS_DUD0_VALID10)
418 				dudmask |= TCDS_DUD_BYTE10;
419 #ifdef DIAGNOSTIC
420 			if (dud & TCDS_DUD0_VALID11)
421 				dudmask |= TCDS_DUD_BYTE11;
422 #endif
423 			NCR_DMA(("dud0 at %p dudmask 0x%x\n",
424 			    addr, dudmask));
425 			*addr = (*addr & ~dudmask) | (dud & dudmask);
426 		}
427 		dud = bus_space_read_4(tcds->sc_bst,
428 		    tcds->sc_bsh, tcds->sc_dud1);
429 		if ((dud & TCDS_DUD1_VALIDBITS) != 0) {
430 			pa = bus_space_read_4(tcds->sc_bst, tcds->sc_bsh,
431 			    tcds->sc_sda) << 2;
432 			dudmask = 0;
433 			if (dud & TCDS_DUD1_VALID00)
434 				dudmask |= TCDS_DUD_BYTE00;
435 			if (dud & TCDS_DUD1_VALID01)
436 				dudmask |= TCDS_DUD_BYTE01;
437 			if (dud & TCDS_DUD1_VALID10)
438 				dudmask |= TCDS_DUD_BYTE10;
439 #ifdef DIAGNOSTIC
440 			if (dud & TCDS_DUD1_VALID11)
441 				panic("tcds_dma: dud1 byte 3 valid");
442 #endif
443 			NCR_DMA(("dud1 at 0x%lx dudmask 0x%x\n",
444 			    pa, dudmask));
445 			/* XXX Fix TC_PHYS_TO_UNCACHED() */
446 #if defined(__alpha__)
447 			addr = (u_int32_t *)ALPHA_PHYS_TO_K0SEG(pa);
448 #elif defined(__mips__)
449 			addr = (u_int32_t *)MIPS_PHYS_TO_KSEG1(pa);
450 #else
451 #error TURBOchannel only exists on DECs, folks...
452 #endif
453 			*addr = (*addr & ~dudmask) | (dud & dudmask);
454 		}
455 		/* XXX deal with saved residual byte? */
456 	}
457 
458 	bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
459 	asc->sc_flags &= ~ASC_MAPLOADED;
460 
461 	return 0;
462 }
463 
464 /*
465  * Glue functions.
466  */
467 static u_char
468 asc_read_reg(struct ncr53c9x_softc *sc, int reg)
469 {
470 	struct asc_softc *asc = (struct asc_softc *)sc;
471 	u_int32_t v;
472 
473 	v = bus_space_read_4(asc->sc_bst, asc->sc_scsi_bsh,
474 	    reg * sizeof(u_int32_t));
475 
476 	return v & 0xff;
477 }
478 
479 static void
480 asc_write_reg(struct ncr53c9x_softc *sc, int reg, u_char val)
481 {
482 	struct asc_softc *asc = (struct asc_softc *)sc;
483 
484 	bus_space_write_4(asc->sc_bst, asc->sc_scsi_bsh,
485 	    reg * sizeof(u_int32_t), val);
486 }
487 
488 static int
489 tcds_dma_isintr(struct ncr53c9x_softc *sc)
490 {
491 	struct asc_softc *asc = (struct asc_softc *)sc;
492 	int x;
493 
494 	x = tcds_scsi_isintr(asc->sc_tcds, 1);
495 
496 	/* XXX */
497 	return x;
498 }
499 
500 static int
501 tcds_dma_isactive(struct ncr53c9x_softc *sc)
502 {
503 	struct asc_softc *asc = (struct asc_softc *)sc;
504 
505 	return !!(asc->sc_flags & ASC_DMAACTIVE);
506 }
507 
508 static void
509 tcds_clear_latched_intr(struct ncr53c9x_softc *sc)
510 {
511 	struct asc_softc *asc = (struct asc_softc *)sc;
512 
513 	/* Clear the TCDS interrupt bit. */
514 	(void)tcds_scsi_isintr(asc->sc_tcds, 1);
515 }
516