xref: /netbsd-src/sys/arch/news68k/dev/si.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: si.c,v 1.21 2008/04/04 16:00:57 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, David Jones, Gordon W. Ross, and Jens A. Nilsson.
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  * This file contains the machine-dependent parts of the Sony CXD1180
41  * controller. The machine-independent parts are in ncr5380sbc.c.
42  * Written by Izumi Tsutsui.
43  *
44  * This code is based on arch/vax/vsa/ncr.c and sun3/dev/si.c
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: si.c,v 1.21 2008/04/04 16:00:57 tsutsui Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/buf.h>
54 
55 #include <machine/cpu.h>
56 #include <m68k/cacheops.h>
57 
58 #include <dev/scsipi/scsipi_all.h>
59 #include <dev/scsipi/scsiconf.h>
60 
61 #include <dev/ic/ncr5380reg.h>
62 #include <dev/ic/ncr5380var.h>
63 
64 #include <news68k/dev/hbvar.h>
65 #include <news68k/dev/dmac_0266.h>
66 
67 #include "ioconf.h"
68 
69 #define MIN_DMA_LEN 128
70 #define DMAC_BASE	0xe0e80000 /* XXX */
71 #define SI_REGSIZE	8
72 
73 struct si_softc {
74 	struct	ncr5380_softc	ncr_sc;
75 	int	sc_options;
76 	volatile struct dma_regs *sc_regs;
77 	int	sc_xlen;
78 };
79 
80 static int  si_match(device_t, cfdata_t, void *);
81 static void si_attach(device_t, device_t, void *);
82 int  si_intr(int);
83 
84 static void si_dma_alloc(struct ncr5380_softc *);
85 static void si_dma_free(struct ncr5380_softc *);
86 static void si_dma_start(struct ncr5380_softc *);
87 static void si_dma_poll(struct ncr5380_softc *);
88 static void si_dma_eop(struct ncr5380_softc *);
89 static void si_dma_stop(struct ncr5380_softc *);
90 
91 CFATTACH_DECL_NEW(si, sizeof(struct si_softc),
92     si_match, si_attach, NULL, NULL);
93 
94 /*
95  * Options for disconnect/reselect, DMA, and interrupts.
96  * By default, allow disconnect/reselect on targets 4-6.
97  * Those are normally tapes that really need it enabled.
98  * The options are taken from the config file.
99  */
100 #define SI_NO_DISCONNECT	0x000ff
101 #define SI_NO_PARITY_CHK	0x0ff00
102 #define SI_FORCE_POLLING	0x10000
103 #define SI_DISABLE_DMA		0x20000
104 
105 int si_options = 0x00;
106 
107 
108 static int
109 si_match(device_t parent, cfdata_t cf, void *aux)
110 {
111 	struct hb_attach_args *ha = aux;
112 	int addr;
113 
114 	if (strcmp(ha->ha_name, "si"))
115 		return 0;
116 
117 	addr = IIOV(ha->ha_address);
118 
119 	if (badaddr((void *)addr, 1))
120 		return 0;
121 
122 	ha->ha_size = SI_REGSIZE;
123 
124 	return 1;
125 }
126 
127 /*
128  * Card attach function
129  */
130 
131 static void
132 si_attach(device_t parent, device_t self, void *aux)
133 {
134 	struct si_softc *sc = device_private(self);
135 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
136 	struct cfdata *cf = device_cfdata(self);
137 	struct hb_attach_args *ha = aux;
138 
139 	ncr_sc->sc_dev = self;
140 	ncr_sc->sc_regt = ha->ha_bust;
141 	if (bus_space_map(ncr_sc->sc_regt, (bus_addr_t)ha->ha_address,
142 	    ha->ha_size, 0, &ncr_sc->sc_regh) != 0) {
143 		aprint_error(": can't map device space\n");
144 		return;
145 	}
146 
147 	/* Get options from config flags if specified. */
148 	if (cf->cf_flags)
149 		sc->sc_options = cf->cf_flags;
150 	else
151 		sc->sc_options = si_options;
152 
153 	if (sc->sc_options != 0)
154 		aprint_normal(": options=0x%x", sc->sc_options);
155 	aprint_normal("\n");
156 
157 	ncr_sc->sc_no_disconnect = (sc->sc_options & SI_NO_DISCONNECT);
158 	ncr_sc->sc_parity_disable = (sc->sc_options & SI_NO_PARITY_CHK) >> 8;
159 	if (sc->sc_options & SI_FORCE_POLLING)
160 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
161 
162 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
163 	ncr_sc->sc_dma_alloc   = si_dma_alloc;
164 	ncr_sc->sc_dma_free    = si_dma_free;
165 	ncr_sc->sc_dma_poll    = si_dma_poll;
166 	ncr_sc->sc_dma_start   = si_dma_start;
167 	ncr_sc->sc_dma_eop     = si_dma_eop;
168 	ncr_sc->sc_dma_stop    = si_dma_stop;
169 
170 	if (sc->sc_options & SI_DISABLE_DMA)
171 		/* Override this function pointer. */
172 		ncr_sc->sc_dma_alloc = NULL;
173 
174 	ncr_sc->sci_r0 = 0;
175 	ncr_sc->sci_r1 = 1;
176 	ncr_sc->sci_r2 = 2;
177 	ncr_sc->sci_r3 = 3;
178 	ncr_sc->sci_r4 = 4;
179 	ncr_sc->sci_r5 = 5;
180 	ncr_sc->sci_r6 = 6;
181 	ncr_sc->sci_r7 = 7;
182 
183 	ncr_sc->sc_rev = NCR_VARIANT_CXD1180;
184 
185 	ncr_sc->sc_pio_in  = ncr5380_pio_in;
186 	ncr_sc->sc_pio_out = ncr5380_pio_out;
187 
188 	ncr_sc->sc_adapter.adapt_minphys = minphys;
189 	ncr_sc->sc_channel.chan_id = 7;
190 
191 	/* soft reset DMAC */
192 	sc->sc_regs = (void *)IIOV(DMAC_BASE);
193 	sc->sc_regs->ctl = DC_CTL_RST;
194 
195 	ncr5380_attach(ncr_sc);
196 }
197 
198 int
199 si_intr(int unit)
200 {
201 	struct si_softc *sc;
202 
203 	if (unit >= si_cd.cd_ndevs)
204 		return 0;
205 
206 	sc = device_lookup_private(&si_cd, unit);	/* XXX */
207 	(void)ncr5380_intr(&sc->ncr_sc);
208 
209 	return 0;
210 }
211 
212 /*
213  *  DMA routines for news1700 machines
214  */
215 static void
216 si_dma_alloc(struct ncr5380_softc *ncr_sc)
217 {
218 	struct sci_req *sr = ncr_sc->sc_current;
219 
220 #ifdef DIAGNOSTIC
221 	if (sr->sr_dma_hand != NULL)
222 		panic("%s: DMA already in use", __func__);
223 #endif
224 
225 	/*
226 	 * On news68k, SCSI has its own DMAC so no need allocate it.
227 	 * Just mark that DMA is available.
228 	 */
229 	sr->sr_dma_hand = (void *)-1;
230 }
231 
232 static void
233 si_dma_free(struct ncr5380_softc *ncr_sc)
234 {
235 	struct sci_req *sr = ncr_sc->sc_current;
236 
237 #ifdef DIAGNOSTIC
238 	if (sr->sr_dma_hand == NULL)
239 		panic("%s: DMA not in use", __func__);
240 #endif
241 
242 	sr->sr_dma_hand = NULL;
243 }
244 
245 
246 static void
247 si_dma_start(struct ncr5380_softc *ncr_sc)
248 {
249 	struct si_softc *sc = (struct si_softc *)ncr_sc;
250 	volatile struct dma_regs *dmac = sc->sc_regs;
251 	struct sci_req *sr = ncr_sc->sc_current;
252 	u_int addr, offset, rest;
253 	long len;
254 	int i;
255 
256 	/* reset DMAC */
257 	dmac->ctl = DC_CTL_RST;
258 	dmac->ctl = 0;
259 
260 	addr = (u_int)ncr_sc->sc_dataptr;
261 	offset = addr & DMAC_SEG_OFFSET;
262 	len = sc->sc_xlen = ncr_sc->sc_datalen;
263 
264 	/* set DMA transfer length */
265 	dmac->tcnt = (uint32_t)len;
266 
267 	/* set offset of first segment */
268 	dmac->offset = offset;
269 
270 	/* set first DMA segment address */
271 	dmac->tag = 0;
272 	dmac->mapent = kvtop((void *)addr) >> DMAC_SEG_SHIFT;
273 	rest = DMAC_SEG_SIZE - offset;
274 	addr += rest;
275 	len -= rest;
276 
277 	/* set all the rest segments */
278 	for (i = 1; len > 0; i++) {
279 		dmac->tag = i;
280 		dmac->mapent = kvtop((void *)addr) >> DMAC_SEG_SHIFT;
281 		len -= DMAC_SEG_SIZE;
282 		addr += DMAC_SEG_SIZE;
283 	}
284 	/* terminate TAG */
285 	dmac->tag = 0;
286 
287 	if (sr->sr_xs->xs_control & XS_CTL_DATA_OUT) {
288 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
289 		NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
290 		NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode)
291 		    | SCI_MODE_DMA | SCI_MODE_DMA_IE);
292 
293 		/* set Dir */
294 		dmac->ctl = 0;
295 
296 		/* start DMA */
297 		NCR5380_WRITE(ncr_sc, sci_dma_send, 0);
298 		dmac->ctl = DC_CTL_ENB;
299 	} else {
300 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
301 		NCR5380_WRITE(ncr_sc, sci_icmd, 0);
302 		NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode)
303 		    | SCI_MODE_DMA | SCI_MODE_DMA_IE);
304 
305 		/* set Dir */
306 		dmac->ctl = DC_CTL_MOD;
307 
308 		/* start DMA */
309 		NCR5380_WRITE(ncr_sc, sci_irecv, 0);
310 		dmac->ctl = DC_CTL_MOD | DC_CTL_ENB;
311 	}
312 	ncr_sc->sc_state |= NCR_DOINGDMA;
313 }
314 
315 /*
316  * When?
317  */
318 static void
319 si_dma_poll(struct ncr5380_softc *ncr_sc)
320 {
321 
322 	printf("si_dma_poll\n");
323 }
324 
325 /*
326  * news68k (probably) does not use the EOP signal.
327  */
328 static void
329 si_dma_eop(struct ncr5380_softc *ncr_sc)
330 {
331 
332 	printf("si_dma_eop\n");
333 }
334 
335 static void
336 si_dma_stop(struct ncr5380_softc *ncr_sc)
337 {
338 	struct si_softc *sc = (struct si_softc *)ncr_sc;
339 	volatile struct dma_regs *dmac = sc->sc_regs;
340 	struct sci_req *sr = ncr_sc->sc_current;
341 	int resid, ntrans;
342 
343 	/* check DMAC interrupt status */
344 	if ((dmac->stat & DC_ST_INT) == 0) {
345 #ifdef DEBUG
346 		printf("%s: no DMA interrupt\n", __func__);
347 #endif
348 		return; /* XXX */
349 	}
350 
351 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
352 #ifdef DEBUG
353 		printf("%s: dma not running\n", __func__);
354 #endif
355 		return;
356 	}
357 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
358 
359 	/* stop DMAC */
360 	resid = dmac->tcnt;
361 	dmac->ctl &= ~DC_CTL_ENB;
362 
363 	/* OK, have either phase mis-match or end of DMA. */
364 	/* Set an impossible phase to prevent data movement? */
365 	NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_INVALID);
366 
367 	/* Note that timeout may have set the error flag. */
368 	if (ncr_sc->sc_state & NCR_ABORTING)
369 		goto out;
370 
371 #ifdef DEBUG
372 	if (resid)
373 		printf("%s: datalen = 0x%x, resid = 0x%x\n",
374 		    __func__, sc->sc_xlen, resid);
375 #endif
376 
377 	ntrans = sc->sc_xlen - resid;
378 
379 	ncr_sc->sc_dataptr += ntrans;
380 	ncr_sc->sc_datalen -= ntrans;
381 
382 	if (sr->sr_xs->xs_control & XS_CTL_DATA_IN) {
383 		/* flush data cache */
384 		PCIA();
385 	}
386 
387  out:
388 	/* reset DMAC */
389 	dmac->ctl = DC_CTL_RST;
390 	dmac->ctl = 0;
391 
392 	NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode) &
393 	    ~(SCI_MODE_DMA | SCI_MODE_DMA_IE));
394 	NCR5380_WRITE(ncr_sc, sci_icmd, 0);
395 }
396