ncr.c revision 1.48 1 1.48 matt /* $NetBSD: ncr.c,v 1.48 2010/12/14 23:31:16 matt Exp $ */
2 1.1 ragge
3 1.16 ragge /*-
4 1.16 ragge * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.1 ragge * All rights reserved.
6 1.1 ragge *
7 1.16 ragge * This code is derived from software contributed to The NetBSD Foundation
8 1.16 ragge * by Adam Glass, David Jones, Gordon W. Ross, and Jens A. Nilsson.
9 1.16 ragge *
10 1.1 ragge * Redistribution and use in source and binary forms, with or without
11 1.1 ragge * modification, are permitted provided that the following conditions
12 1.1 ragge * are met:
13 1.1 ragge * 1. Redistributions of source code must retain the above copyright
14 1.1 ragge * notice, this list of conditions and the following disclaimer.
15 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ragge * notice, this list of conditions and the following disclaimer in the
17 1.1 ragge * documentation and/or other materials provided with the distribution.
18 1.16 ragge *
19 1.16 ragge * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.16 ragge * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.16 ragge * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.16 ragge * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.16 ragge * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.16 ragge * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.16 ragge * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.16 ragge * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.16 ragge * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.16 ragge * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.16 ragge * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ragge */
31 1.1 ragge
32 1.1 ragge /*
33 1.16 ragge * This file contains the machine-dependent parts of the NCR-5380
34 1.16 ragge * controller. The machine-independent parts are in ncr5380sbc.c.
35 1.16 ragge *
36 1.16 ragge * Jens A. Nilsson.
37 1.16 ragge *
38 1.16 ragge * Credits:
39 1.16 ragge *
40 1.16 ragge * This code is based on arch/sun3/dev/si*
41 1.16 ragge * Written by David Jones, Gordon Ross, and Adam Glass.
42 1.1 ragge */
43 1.40 lukem
44 1.40 lukem #include <sys/cdefs.h>
45 1.48 matt __KERNEL_RCSID(0, "$NetBSD: ncr.c,v 1.48 2010/12/14 23:31:16 matt Exp $");
46 1.1 ragge
47 1.1 ragge #include <sys/param.h>
48 1.1 ragge #include <sys/systm.h>
49 1.48 matt #include <sys/buf.h>
50 1.48 matt #include <sys/bus.h>
51 1.48 matt #include <sys/cpu.h>
52 1.48 matt #include <sys/device.h>
53 1.16 ragge #include <sys/errno.h>
54 1.1 ragge #include <sys/kernel.h>
55 1.16 ragge #include <sys/malloc.h>
56 1.1 ragge #include <sys/proc.h>
57 1.1 ragge
58 1.9 bouyer #include <dev/scsipi/scsi_all.h>
59 1.9 bouyer #include <dev/scsipi/scsipi_all.h>
60 1.9 bouyer #include <dev/scsipi/scsipi_debug.h>
61 1.9 bouyer #include <dev/scsipi/scsiconf.h>
62 1.1 ragge
63 1.1 ragge #include <dev/ic/ncr5380reg.h>
64 1.1 ragge #include <dev/ic/ncr5380var.h>
65 1.1 ragge
66 1.16 ragge #include <machine/vsbus.h>
67 1.21 ragge #include <machine/sid.h>
68 1.24 matt #include <machine/scb.h>
69 1.27 ragge #include <machine/clock.h>
70 1.1 ragge
71 1.16 ragge #include "ioconf.h"
72 1.1 ragge
73 1.16 ragge #define MIN_DMA_LEN 128
74 1.1 ragge
75 1.1 ragge struct si_dma_handle {
76 1.16 ragge int dh_flags;
77 1.16 ragge #define SIDH_BUSY 1
78 1.16 ragge #define SIDH_OUT 2
79 1.42 christos void *dh_addr;
80 1.23 ragge int dh_len;
81 1.23 ragge struct proc *dh_proc;
82 1.1 ragge };
83 1.1 ragge
84 1.1 ragge struct si_softc {
85 1.23 ragge struct ncr5380_softc ncr_sc;
86 1.28 matt struct evcnt ncr_intrcnt;
87 1.42 christos void *ncr_addr;
88 1.23 ragge int ncr_off;
89 1.23 ragge int ncr_dmaaddr;
90 1.23 ragge int ncr_dmacount;
91 1.23 ragge int ncr_dmadir;
92 1.23 ragge struct si_dma_handle ncr_dma[SCI_OPENINGS];
93 1.32 ragge struct vsbus_dma sc_vd;
94 1.32 ragge int onlyscsi; /* This machine needs no queueing */
95 1.1 ragge };
96 1.1 ragge
97 1.32 ragge static int ncr_dmasize;
98 1.32 ragge
99 1.44 matt static int si_vsbus_match(device_t, cfdata_t, void *);
100 1.44 matt static void si_vsbus_attach(device_t, device_t, void *);
101 1.23 ragge static void si_minphys(struct buf *);
102 1.23 ragge
103 1.31 matt static void si_dma_alloc(struct ncr5380_softc *);
104 1.31 matt static void si_dma_free(struct ncr5380_softc *);
105 1.31 matt static void si_dma_setup(struct ncr5380_softc *);
106 1.31 matt static void si_dma_start(struct ncr5380_softc *);
107 1.31 matt static void si_dma_poll(struct ncr5380_softc *);
108 1.31 matt static void si_dma_eop(struct ncr5380_softc *);
109 1.31 matt static void si_dma_stop(struct ncr5380_softc *);
110 1.32 ragge static void si_dma_go(void *);
111 1.23 ragge
112 1.45 tsutsui CFATTACH_DECL_NEW(si_vsbus, sizeof(struct si_softc),
113 1.39 thorpej si_vsbus_match, si_vsbus_attach, NULL, NULL);
114 1.1 ragge
115 1.16 ragge static int
116 1.44 matt si_vsbus_match(device_t parent, cfdata_t cf, void *aux)
117 1.1 ragge {
118 1.44 matt struct vsbus_attach_args * const va = aux;
119 1.18 ragge volatile char *si_csr = (char *) va->va_addr;
120 1.1 ragge
121 1.31 matt if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
122 1.34 ragge || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53)
123 1.21 ragge return 0;
124 1.19 ragge /* This is the way Linux autoprobes the interrupt MK-990321 */
125 1.19 ragge si_csr[12] = 0;
126 1.19 ragge si_csr[16] = 0x80;
127 1.19 ragge si_csr[0] = 0x80;
128 1.19 ragge si_csr[4] = 5; /* 0xcf */
129 1.18 ragge DELAY(100000);
130 1.18 ragge return 1;
131 1.1 ragge }
132 1.1 ragge
133 1.16 ragge static void
134 1.44 matt si_vsbus_attach(device_t parent, device_t self, void *aux)
135 1.1 ragge {
136 1.44 matt struct vsbus_attach_args * const va = aux;
137 1.44 matt struct si_softc * const sc = device_private(self);
138 1.44 matt struct ncr5380_softc * const ncr_sc = &sc->ncr_sc;
139 1.27 ragge int tweak, target;
140 1.24 matt
141 1.45 tsutsui ncr_sc->sc_dev = self;
142 1.45 tsutsui
143 1.28 matt scb_vecalloc(va->va_cvec, (void (*)(void *)) ncr5380_intr, sc,
144 1.28 matt SCB_ISTACK, &sc->ncr_intrcnt);
145 1.30 matt evcnt_attach_dynamic(&sc->ncr_intrcnt, EVCNT_TYPE_INTR, NULL,
146 1.44 matt device_xname(self), "intr");
147 1.24 matt
148 1.1 ragge /*
149 1.23 ragge * DMA area mapin.
150 1.23 ragge * On VS3100, split the 128K block between the two devices.
151 1.23 ragge * On VS2000, don't care for now.
152 1.1 ragge */
153 1.23 ragge #define DMASIZE (64*1024)
154 1.32 ragge if (va->va_paddr & 0x100) { /* Secondary SCSI controller */
155 1.32 ragge sc->ncr_off = DMASIZE;
156 1.32 ragge sc->onlyscsi = 1;
157 1.23 ragge }
158 1.42 christos sc->ncr_addr = (void *)va->va_dmaaddr;
159 1.32 ragge ncr_dmasize = min(va->va_dmasize, MAXPHYS);
160 1.32 ragge
161 1.32 ragge /*
162 1.32 ragge * MD function pointers used by the MI code.
163 1.32 ragge */
164 1.32 ragge ncr_sc->sc_dma_alloc = si_dma_alloc;
165 1.32 ragge ncr_sc->sc_dma_free = si_dma_free;
166 1.32 ragge ncr_sc->sc_dma_setup = si_dma_setup;
167 1.32 ragge ncr_sc->sc_dma_start = si_dma_start;
168 1.32 ragge ncr_sc->sc_dma_poll = si_dma_poll;
169 1.32 ragge ncr_sc->sc_dma_eop = si_dma_eop;
170 1.32 ragge ncr_sc->sc_dma_stop = si_dma_stop;
171 1.32 ragge
172 1.32 ragge /* DMA control register offsets */
173 1.32 ragge sc->ncr_dmaaddr = 32; /* DMA address in buffer, longword */
174 1.32 ragge sc->ncr_dmacount = 64; /* DMA count register */
175 1.32 ragge sc->ncr_dmadir = 68; /* Direction of DMA transfer */
176 1.32 ragge
177 1.16 ragge ncr_sc->sc_pio_out = ncr5380_pio_out;
178 1.16 ragge ncr_sc->sc_pio_in = ncr5380_pio_in;
179 1.16 ragge
180 1.16 ragge ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
181 1.15 thorpej
182 1.15 thorpej /*
183 1.16 ragge * Initialize fields used by the MI code.
184 1.1 ragge */
185 1.22 ragge /* ncr_sc->sc_regt = Unused on VAX */
186 1.22 ragge ncr_sc->sc_regh = vax_map_physmem(va->va_paddr, 1);
187 1.22 ragge
188 1.22 ragge /* Register offsets */
189 1.22 ragge ncr_sc->sci_r0 = 0;
190 1.22 ragge ncr_sc->sci_r1 = 4;
191 1.22 ragge ncr_sc->sci_r2 = 8;
192 1.22 ragge ncr_sc->sci_r3 = 12;
193 1.22 ragge ncr_sc->sci_r4 = 16;
194 1.22 ragge ncr_sc->sci_r5 = 20;
195 1.22 ragge ncr_sc->sci_r6 = 24;
196 1.22 ragge ncr_sc->sci_r7 = 28;
197 1.26 tsutsui
198 1.26 tsutsui ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
199 1.1 ragge
200 1.20 ragge ncr_sc->sc_no_disconnect = 0xff;
201 1.25 mycroft
202 1.27 ragge /*
203 1.27 ragge * Get the SCSI chip target address out of NVRAM.
204 1.27 ragge * This do not apply to the VS2000.
205 1.27 ragge */
206 1.27 ragge tweak = clk_tweak + (va->va_paddr & 0x100 ? 3 : 0);
207 1.27 ragge if (vax_boardtype == VAX_BTYP_410)
208 1.27 ragge target = 7;
209 1.27 ragge else
210 1.27 ragge target = (clk_page[0xbc/2] >> tweak) & 7;
211 1.27 ragge
212 1.44 matt aprint_normal("\n");
213 1.44 matt aprint_normal_dev(self, "NCR5380, SCSI ID %d\n", target);
214 1.27 ragge
215 1.35 bouyer ncr_sc->sc_adapter.adapt_minphys = si_minphys;
216 1.35 bouyer ncr_sc->sc_channel.chan_id = target;
217 1.32 ragge
218 1.32 ragge /*
219 1.32 ragge * Init the vsbus DMA resource queue struct */
220 1.32 ragge sc->sc_vd.vd_go = si_dma_go;
221 1.32 ragge sc->sc_vd.vd_arg = sc;
222 1.32 ragge
223 1.1 ragge /*
224 1.16 ragge * Initialize si board itself.
225 1.1 ragge */
226 1.25 mycroft ncr5380_attach(ncr_sc);
227 1.1 ragge }
228 1.1 ragge
229 1.22 ragge /*
230 1.22 ragge * Adjust the max transfer size. The DMA buffer is only 16k on VS2000.
231 1.22 ragge */
232 1.16 ragge static void
233 1.1 ragge si_minphys(struct buf *bp)
234 1.1 ragge {
235 1.32 ragge if (bp->b_bcount > ncr_dmasize)
236 1.32 ragge bp->b_bcount = ncr_dmasize;
237 1.23 ragge }
238 1.23 ragge
239 1.23 ragge void
240 1.31 matt si_dma_alloc(struct ncr5380_softc *ncr_sc)
241 1.23 ragge {
242 1.23 ragge struct si_softc *sc = (struct si_softc *)ncr_sc;
243 1.23 ragge struct sci_req *sr = ncr_sc->sc_current;
244 1.23 ragge struct scsipi_xfer *xs = sr->sr_xs;
245 1.23 ragge struct si_dma_handle *dh;
246 1.23 ragge int xlen, i;
247 1.23 ragge
248 1.23 ragge #ifdef DIAGNOSTIC
249 1.23 ragge if (sr->sr_dma_hand != NULL)
250 1.23 ragge panic("si_dma_alloc: already have DMA handle");
251 1.23 ragge #endif
252 1.23 ragge
253 1.23 ragge /* Polled transfers shouldn't allocate a DMA handle. */
254 1.23 ragge if (sr->sr_flags & SR_IMMED)
255 1.23 ragge return;
256 1.23 ragge
257 1.23 ragge xlen = ncr_sc->sc_datalen;
258 1.23 ragge
259 1.23 ragge /* Make sure our caller checked sc_min_dma_len. */
260 1.23 ragge if (xlen < MIN_DMA_LEN)
261 1.36 provos panic("si_dma_alloc: len=0x%x", xlen);
262 1.23 ragge
263 1.23 ragge /*
264 1.23 ragge * Find free PDMA handle. Guaranteed to find one since we
265 1.23 ragge * have as many PDMA handles as the driver has processes.
266 1.23 ragge * (instances?)
267 1.23 ragge */
268 1.23 ragge for (i = 0; i < SCI_OPENINGS; i++) {
269 1.23 ragge if ((sc->ncr_dma[i].dh_flags & SIDH_BUSY) == 0)
270 1.23 ragge goto found;
271 1.23 ragge }
272 1.23 ragge panic("sbc: no free PDMA handles");
273 1.23 ragge found:
274 1.23 ragge dh = &sc->ncr_dma[i];
275 1.23 ragge dh->dh_flags = SIDH_BUSY;
276 1.23 ragge dh->dh_addr = ncr_sc->sc_dataptr;
277 1.23 ragge dh->dh_len = xlen;
278 1.23 ragge dh->dh_proc = xs->bp->b_proc;
279 1.23 ragge
280 1.23 ragge /* Remember dest buffer parameters */
281 1.23 ragge if (xs->xs_control & XS_CTL_DATA_OUT)
282 1.23 ragge dh->dh_flags |= SIDH_OUT;
283 1.23 ragge
284 1.23 ragge sr->sr_dma_hand = dh;
285 1.23 ragge }
286 1.23 ragge
287 1.23 ragge void
288 1.31 matt si_dma_free(struct ncr5380_softc *ncr_sc)
289 1.23 ragge {
290 1.23 ragge struct sci_req *sr = ncr_sc->sc_current;
291 1.23 ragge struct si_dma_handle *dh = sr->sr_dma_hand;
292 1.23 ragge
293 1.23 ragge if (dh->dh_flags & SIDH_BUSY)
294 1.23 ragge dh->dh_flags = 0;
295 1.23 ragge else
296 1.23 ragge printf("si_dma_free: free'ing unused buffer\n");
297 1.23 ragge
298 1.23 ragge sr->sr_dma_hand = NULL;
299 1.23 ragge }
300 1.23 ragge
301 1.23 ragge void
302 1.31 matt si_dma_setup(struct ncr5380_softc *ncr_sc)
303 1.23 ragge {
304 1.23 ragge /* Do nothing here */
305 1.23 ragge }
306 1.23 ragge
307 1.23 ragge void
308 1.31 matt si_dma_start(struct ncr5380_softc *ncr_sc)
309 1.23 ragge {
310 1.23 ragge struct si_softc *sc = (struct si_softc *)ncr_sc;
311 1.32 ragge
312 1.32 ragge /* Just put on queue; will call go() from below */
313 1.32 ragge if (sc->onlyscsi)
314 1.32 ragge si_dma_go(ncr_sc);
315 1.32 ragge else
316 1.32 ragge vsbus_dma_start(&sc->sc_vd);
317 1.32 ragge }
318 1.32 ragge
319 1.32 ragge /*
320 1.32 ragge * go() routine called when another transfer somewhere is finished.
321 1.32 ragge */
322 1.32 ragge void
323 1.32 ragge si_dma_go(void *arg)
324 1.32 ragge {
325 1.32 ragge struct ncr5380_softc *ncr_sc = arg;
326 1.32 ragge struct si_softc *sc = (struct si_softc *)ncr_sc;
327 1.23 ragge struct sci_req *sr = ncr_sc->sc_current;
328 1.23 ragge struct si_dma_handle *dh = sr->sr_dma_hand;
329 1.23 ragge
330 1.23 ragge /*
331 1.23 ragge * Set the VAX-DMA-specific registers, and copy the data if
332 1.23 ragge * it is directed "outbound".
333 1.23 ragge */
334 1.23 ragge if (dh->dh_flags & SIDH_OUT) {
335 1.32 ragge vsbus_copyfromproc(dh->dh_proc, dh->dh_addr,
336 1.43 christos (char *)sc->ncr_addr + sc->ncr_off, dh->dh_len);
337 1.23 ragge bus_space_write_1(ncr_sc->sc_regt, ncr_sc->sc_regh,
338 1.23 ragge sc->ncr_dmadir, 0);
339 1.23 ragge } else {
340 1.23 ragge bus_space_write_1(ncr_sc->sc_regt, ncr_sc->sc_regh,
341 1.23 ragge sc->ncr_dmadir, 1);
342 1.23 ragge }
343 1.23 ragge bus_space_write_4(ncr_sc->sc_regt, ncr_sc->sc_regh,
344 1.23 ragge sc->ncr_dmacount, -dh->dh_len);
345 1.23 ragge bus_space_write_4(ncr_sc->sc_regt, ncr_sc->sc_regh,
346 1.23 ragge sc->ncr_dmaaddr, sc->ncr_off);
347 1.23 ragge /*
348 1.23 ragge * Now from the 5380-internal DMA registers.
349 1.23 ragge */
350 1.23 ragge if (dh->dh_flags & SIDH_OUT) {
351 1.23 ragge NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
352 1.23 ragge NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
353 1.23 ragge NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode)
354 1.23 ragge | SCI_MODE_DMA | SCI_MODE_DMA_IE);
355 1.23 ragge NCR5380_WRITE(ncr_sc, sci_dma_send, 0);
356 1.23 ragge } else {
357 1.23 ragge NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
358 1.23 ragge NCR5380_WRITE(ncr_sc, sci_icmd, 0);
359 1.23 ragge NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode)
360 1.23 ragge | SCI_MODE_DMA | SCI_MODE_DMA_IE);
361 1.23 ragge NCR5380_WRITE(ncr_sc, sci_irecv, 0);
362 1.23 ragge }
363 1.23 ragge ncr_sc->sc_state |= NCR_DOINGDMA;
364 1.23 ragge }
365 1.23 ragge
366 1.23 ragge /*
367 1.23 ragge * When?
368 1.23 ragge */
369 1.23 ragge void
370 1.31 matt si_dma_poll(struct ncr5380_softc *ncr_sc)
371 1.23 ragge {
372 1.23 ragge printf("si_dma_poll\n");
373 1.23 ragge }
374 1.23 ragge
375 1.23 ragge /*
376 1.23 ragge * When?
377 1.23 ragge */
378 1.23 ragge void
379 1.31 matt si_dma_eop(struct ncr5380_softc *ncr_sc)
380 1.23 ragge {
381 1.23 ragge printf("si_dma_eop\n");
382 1.23 ragge }
383 1.23 ragge
384 1.23 ragge void
385 1.31 matt si_dma_stop(struct ncr5380_softc *ncr_sc)
386 1.23 ragge {
387 1.23 ragge struct si_softc *sc = (struct si_softc *)ncr_sc;
388 1.23 ragge struct sci_req *sr = ncr_sc->sc_current;
389 1.23 ragge struct si_dma_handle *dh = sr->sr_dma_hand;
390 1.23 ragge int count, i;
391 1.23 ragge
392 1.23 ragge if (ncr_sc->sc_state & NCR_DOINGDMA)
393 1.23 ragge ncr_sc->sc_state &= ~NCR_DOINGDMA;
394 1.23 ragge
395 1.23 ragge /*
396 1.23 ragge * Sometimes the FIFO buffer isn't drained when the
397 1.23 ragge * interrupt is posted. Just loop here and hope that
398 1.23 ragge * it will drain soon.
399 1.23 ragge */
400 1.23 ragge for (i = 0; i < 20000; i++) {
401 1.23 ragge count = bus_space_read_4(ncr_sc->sc_regt,
402 1.23 ragge ncr_sc->sc_regh, sc->ncr_dmacount);
403 1.23 ragge if (count == 0)
404 1.23 ragge break;
405 1.23 ragge DELAY(100);
406 1.23 ragge }
407 1.23 ragge if (count == 0) {
408 1.23 ragge if (((dh->dh_flags & SIDH_OUT) == 0)) {
409 1.33 ragge vsbus_copytoproc(dh->dh_proc,
410 1.43 christos (char *)sc->ncr_addr + sc->ncr_off,
411 1.32 ragge dh->dh_addr, dh->dh_len);
412 1.23 ragge }
413 1.23 ragge ncr_sc->sc_dataptr += dh->dh_len;
414 1.23 ragge ncr_sc->sc_datalen -= dh->dh_len;
415 1.23 ragge }
416 1.23 ragge
417 1.23 ragge NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode) &
418 1.23 ragge ~(SCI_MODE_DMA | SCI_MODE_DMA_IE));
419 1.23 ragge NCR5380_WRITE(ncr_sc, sci_icmd, 0);
420 1.32 ragge if (sc->onlyscsi == 0)
421 1.32 ragge vsbus_dma_intr(); /* Try to start more transfers */
422 1.1 ragge }
423