Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / vax / vsa / asc_vsbus.c
blobcae75cc6cdd3e90fef07e013a692a71b2fb3786c
1 /* $NetBSD: asc_vsbus.c,v 1.40 2008/04/28 20:23:39 martin Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 __KERNEL_RCSID(0, "$NetBSD: asc_vsbus.c,v 1.40 2008/04/28 20:23:39 martin Exp $");
36 #include "locators.h"
37 #include "opt_cputype.h"
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/errno.h>
44 #include <sys/ioctl.h>
45 #include <sys/device.h>
46 #include <sys/buf.h>
47 #include <sys/proc.h>
48 #include <sys/reboot.h>
49 #include <sys/queue.h>
51 #include <dev/scsipi/scsi_all.h>
52 #include <dev/scsipi/scsipi_all.h>
53 #include <dev/scsipi/scsiconf.h>
54 #include <dev/scsipi/scsi_message.h>
56 #include <machine/bus.h>
57 #include <machine/vmparam.h>
59 #include <dev/ic/ncr53c9xreg.h>
60 #include <dev/ic/ncr53c9xvar.h>
62 #include <machine/cpu.h>
63 #include <machine/sid.h>
64 #include <machine/scb.h>
65 #include <machine/vsbus.h>
66 #include <machine/clock.h> /* for SCSI ctlr ID# XXX */
68 struct asc_vsbus_softc {
69 struct ncr53c9x_softc sc_ncr53c9x; /* Must be first */
70 struct evcnt sc_intrcnt; /* count interrupts */
71 bus_space_tag_t sc_bst; /* bus space tag */
72 bus_space_handle_t sc_bsh; /* bus space handle */
73 bus_space_handle_t sc_dirh; /* scsi direction handle */
74 bus_space_handle_t sc_adrh; /* scsi address handle */
75 bus_space_handle_t sc_ncrh; /* ncr bus space handle */
76 bus_dma_tag_t sc_dmat; /* bus DMA tag */
77 bus_dmamap_t sc_dmamap;
78 uint8_t **sc_dmaaddr;
79 size_t *sc_dmalen;
80 size_t sc_dmasize;
81 unsigned int sc_flags;
82 #define ASC_FROMMEMORY 0x0001 /* Must be 1 */
83 #define ASC_DMAACTIVE 0x0002
84 #define ASC_MAPLOADED 0x0004
85 unsigned long sc_xfers;
88 #define ASC_REG_KA46_ADR 0x0000
89 #define ASC_REG_KA46_DIR 0x000C
90 #define ASC_REG_KA49_ADR 0x0000
91 #define ASC_REG_KA49_DIR 0x0004
92 #define ASC_REG_NCR 0x0080
93 #define ASC_REG_END 0x00B0
95 #define ASC_MAXXFERSIZE 65536
96 #define ASC_FREQUENCY 25000000
98 static int asc_vsbus_match(device_t, cfdata_t, void *);
99 static void asc_vsbus_attach(device_t, device_t, void *);
101 CFATTACH_DECL_NEW(asc_vsbus, sizeof(struct asc_vsbus_softc),
102 asc_vsbus_match, asc_vsbus_attach, NULL, NULL);
105 * Functions and the switch for the MI code
107 static uint8_t asc_vsbus_read_reg(struct ncr53c9x_softc *, int);
108 static void asc_vsbus_write_reg(struct ncr53c9x_softc *, int, uint8_t);
109 static int asc_vsbus_dma_isintr(struct ncr53c9x_softc *);
110 static void asc_vsbus_dma_reset(struct ncr53c9x_softc *);
111 static int asc_vsbus_dma_intr(struct ncr53c9x_softc *);
112 static int asc_vsbus_dma_setup(struct ncr53c9x_softc *, uint8_t **,
113 size_t *, int, size_t *);
114 static void asc_vsbus_dma_go(struct ncr53c9x_softc *);
115 static void asc_vsbus_dma_stop(struct ncr53c9x_softc *);
116 static int asc_vsbus_dma_isactive(struct ncr53c9x_softc *);
118 static const struct ncr53c9x_glue asc_vsbus_glue = {
119 .gl_read_reg = asc_vsbus_read_reg,
120 .gl_write_reg = asc_vsbus_write_reg,
121 .gl_dma_isintr = asc_vsbus_dma_isintr,
122 .gl_dma_reset = asc_vsbus_dma_reset,
123 .gl_dma_intr = asc_vsbus_dma_intr,
124 .gl_dma_setup = asc_vsbus_dma_setup,
125 .gl_dma_go = asc_vsbus_dma_go,
126 .gl_dma_stop = asc_vsbus_dma_stop,
127 .gl_dma_isactive = asc_vsbus_dma_isactive,
130 static uint8_t asc_attached; /* can't have more than one asc */
132 static int
133 asc_vsbus_match(device_t parent, cfdata_t cf, void *aux)
135 struct vsbus_attach_args * const va = aux;
136 volatile uint8_t *ncr_regs;
137 int dummy;
139 if (asc_attached)
140 return 0;
142 if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
143 if (cf->cf_loc[VSBUSCF_CSR] != 0x200c0080)
144 return 0;
145 } else if (vax_boardtype == VAX_BTYP_49 ||
146 vax_boardtype == VAX_BTYP_53) {
147 if (cf->cf_loc[VSBUSCF_CSR] != 0x26000080)
148 return 0;
149 } else {
150 return 0;
153 ncr_regs = (volatile uint8_t *) va->va_addr;
155 /* *** need to generate an interrupt here
156 * From trial and error, I've determined that an INT is generated
157 * only when the following sequence of events occurs:
158 * 1) The interrupt status register (0x05) must be read.
159 * 2) SCSI bus reset interrupt must be enabled
160 * 3) SCSI bus reset command must be sent
161 * 4) NOP command must be sent
164 dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
165 ncr_regs[NCR_CFG1 << 2] = 0x06; /* we're ID 6, turn on INT for SCSI reset */
166 ncr_regs[NCR_CMD << 2] = NCRCMD_RSTSCSI; /* send the reset */
167 ncr_regs[NCR_CMD << 2] = NCRCMD_NOP; /* send a NOP */
168 DELAY(10000);
170 dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
171 return (dummy & NCRINTR_SBR) != 0;
176 * Attach this instance, and then all the sub-devices
178 static void
179 asc_vsbus_attach(device_t parent, device_t self, void *aux)
181 struct vsbus_attach_args * const va = aux;
182 struct asc_vsbus_softc * const asc = device_private(self);
183 struct ncr53c9x_softc * const sc = &asc->sc_ncr53c9x;
184 int error;
186 asc_attached = 1;
188 * Set up glue for MI code early; we use some of it here.
190 sc->sc_dev = self;
191 sc->sc_glue = &asc_vsbus_glue;
193 asc->sc_bst = va->va_memt;
194 asc->sc_dmat = va->va_dmat;
196 error = bus_space_map(asc->sc_bst, va->va_paddr - ASC_REG_NCR,
197 ASC_REG_END, 0, &asc->sc_bsh);
198 if (error) {
199 aprint_error(": failed to map registers: error=%d\n", error);
200 return;
202 error = bus_space_subregion(asc->sc_bst, asc->sc_bsh, ASC_REG_NCR,
203 ASC_REG_END - ASC_REG_NCR, &asc->sc_ncrh);
204 if (error) {
205 aprint_error(": failed to map ncr registers: error=%d\n",
206 error);
207 return;
209 if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
210 error = bus_space_subregion(asc->sc_bst, asc->sc_bsh,
211 ASC_REG_KA46_ADR, sizeof(uint32_t), &asc->sc_adrh);
212 if (error) {
213 aprint_error(": failed to map adr register: error=%d\n",
214 error);
215 return;
217 error = bus_space_subregion(asc->sc_bst, asc->sc_bsh,
218 ASC_REG_KA46_DIR, sizeof(uint32_t), &asc->sc_dirh);
219 if (error) {
220 aprint_error(": failed to map dir register: error=%d\n",
221 error);
222 return;
224 } else {
225 /* This is a gross and disgusting kludge but it'll
226 * save a bunch of ugly code. Unlike the VS4000/60,
227 * the SCSI Address and direction registers are not
228 * near the SCSI NCR registers and are inside the
229 * block of general VAXstation registers. So we grab
230 * them from there and knowing the internals of the
231 * bus_space implementation, we cast to bus_space_handles.
233 struct vsbus_softc *vsc = device_private(parent);
234 asc->sc_adrh =
235 (bus_space_handle_t)(vsc->sc_vsregs + ASC_REG_KA49_ADR);
236 asc->sc_dirh =
237 (bus_space_handle_t)(vsc->sc_vsregs + ASC_REG_KA49_DIR);
238 #if 0
239 printf("\n%s: adrh=0x%08lx dirh=0x%08lx", device_xname(self),
240 asc->sc_adrh, asc->sc_dirh);
241 ncr53c9x_debug = NCR_SHOWDMA | NCR_SHOWINTS | NCR_SHOWCMDS |
242 NCR_SHOWPHASE | NCR_SHOWSTART | NCR_SHOWMSGS;
243 #endif
245 error = bus_dmamap_create(asc->sc_dmat, ASC_MAXXFERSIZE, 1,
246 ASC_MAXXFERSIZE, 0, BUS_DMA_NOWAIT, &asc->sc_dmamap);
248 #if defined(VAX46) || defined(VAX48) || defined(VAX49) || defined(VAXANY)
249 if(vax_boardtype != VAX_BTYP_53)
250 /* SCSI ID is store in the clock NVRAM at magic address 0xbc */
251 sc->sc_id = (clk_page[0xbc / 2] >> clk_tweak) & 7;
252 else
253 #endif
254 sc->sc_id = 6; /* XXX need to get this from VMB */
255 sc->sc_freq = ASC_FREQUENCY;
257 /* gimme MHz */
258 sc->sc_freq /= 1000000;
260 scb_vecalloc(va->va_cvec, (void (*)(void *))ncr53c9x_intr,
261 &asc->sc_ncr53c9x, SCB_ISTACK, &asc->sc_intrcnt);
262 evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
263 device_xname(self), "intr");
266 * XXX More of this should be in ncr53c9x_attach(), but
267 * XXX should we really poke around the chip that much in
268 * XXX the MI code? Think about this more...
272 * Set up static configuration info.
274 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
275 sc->sc_cfg2 = NCRCFG2_SCSI2;
276 sc->sc_cfg3 = 0;
277 sc->sc_rev = NCR_VARIANT_NCR53C94;
280 * XXX minsync and maxxfer _should_ be set up in MI code,
281 * XXX but it appears to have some dependency on what sort
282 * XXX of DMA we're hooked up to, etc.
286 * This is the value used to start sync negotiations
287 * Note that the NCR register "SYNCTP" is programmed
288 * in "clocks per byte", and has a minimum value of 4.
289 * The SCSI period used in negotiation is one-fourth
290 * of the time (in nanoseconds) needed to transfer one byte.
291 * Since the chip's clock is given in MHz, we have the following
292 * formula: 4 * period = (1000 / freq) * 4
294 sc->sc_minsync = (1000 / sc->sc_freq);
295 sc->sc_maxxfer = 64 * 1024;
297 aprint_normal("\n%s", device_xname(self)); /* Pretty print */
299 /* Do the common parts of attachment. */
300 sc->sc_adapter.adapt_minphys = minphys;
301 sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
302 ncr53c9x_attach(sc);
306 * Glue functions.
309 static uint8_t
310 asc_vsbus_read_reg(struct ncr53c9x_softc *sc, int reg)
312 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
314 return bus_space_read_1(asc->sc_bst, asc->sc_ncrh,
315 reg * sizeof(uint32_t));
318 static void
319 asc_vsbus_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
321 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
323 bus_space_write_1(asc->sc_bst, asc->sc_ncrh,
324 reg * sizeof(uint32_t), val);
327 static int
328 asc_vsbus_dma_isintr(struct ncr53c9x_softc *sc)
330 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
332 return bus_space_read_1(asc->sc_bst, asc->sc_ncrh,
333 NCR_STAT * sizeof(uint32_t)) & NCRSTAT_INT;
336 static void
337 asc_vsbus_dma_reset(struct ncr53c9x_softc *sc)
339 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
341 if (asc->sc_flags & ASC_MAPLOADED)
342 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
343 asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
346 static int
347 asc_vsbus_dma_intr(struct ncr53c9x_softc *sc)
349 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
350 u_int tcl, tcm;
351 int trans, resid;
353 if ((asc->sc_flags & ASC_DMAACTIVE) == 0)
354 panic("%s: DMA wasn't active", __func__);
356 asc->sc_flags &= ~ASC_DMAACTIVE;
358 if (asc->sc_dmasize == 0) {
359 /* A "Transfer Pad" operation completed */
360 tcl = NCR_READ_REG(sc, NCR_TCL);
361 tcm = NCR_READ_REG(sc, NCR_TCM);
362 NCR_DMA(("asc_vsbus_intr: discarded %d bytes (tcl=%d, tcm=%d)\n",
363 tcl | (tcm << 8), tcl, tcm));
364 return 0;
367 resid = 0;
368 if ((resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
369 NCR_DMA(("asc_vsbus_intr: empty FIFO of %d ", resid));
370 DELAY(1);
372 if (asc->sc_flags & ASC_MAPLOADED) {
373 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
374 0, asc->sc_dmasize,
375 asc->sc_flags & ASC_FROMMEMORY
376 ? BUS_DMASYNC_POSTWRITE
377 : BUS_DMASYNC_POSTREAD);
378 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
380 asc->sc_flags &= ~ASC_MAPLOADED;
382 resid += (tcl = NCR_READ_REG(sc, NCR_TCL));
383 resid += (tcm = NCR_READ_REG(sc, NCR_TCM)) << 8;
385 trans = asc->sc_dmasize - resid;
386 if (trans < 0) { /* transferred < 0 ? */
387 printf("asc_vsbus_intr: xfer (%d) > req (%lu)\n",
388 trans, (u_long) asc->sc_dmasize);
389 trans = asc->sc_dmasize;
391 NCR_DMA(("asc_vsbus_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
392 tcl, tcm, trans, resid));
394 *asc->sc_dmalen -= trans;
395 *asc->sc_dmaaddr += trans;
397 asc->sc_xfers++;
398 return 0;
401 static int
402 asc_vsbus_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
403 int datain, size_t *dmasize)
405 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
407 asc->sc_dmaaddr = addr;
408 asc->sc_dmalen = len;
409 if (datain) {
410 asc->sc_flags &= ~ASC_FROMMEMORY;
411 } else {
412 asc->sc_flags |= ASC_FROMMEMORY;
414 if ((vaddr_t)*asc->sc_dmaaddr < VM_MIN_KERNEL_ADDRESS)
415 panic("%s: DMA address (%p) outside of kernel",
416 __func__, *asc->sc_dmaaddr);
418 NCR_DMA(("%s: start %d@%p,%d\n", device_xname(&sc->sc_dev),
419 (int)*asc->sc_dmalen, *asc->sc_dmaaddr,
420 (asc->sc_flags & ASC_FROMMEMORY)));
421 *dmasize = asc->sc_dmasize = min(*dmasize, ASC_MAXXFERSIZE);
423 if (asc->sc_dmasize) {
424 if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap,
425 *asc->sc_dmaaddr, asc->sc_dmasize,
426 NULL /* kernel address */,
427 BUS_DMA_NOWAIT|VAX_BUS_DMA_SPILLPAGE))
428 panic("%s: cannot load DMA map",
429 device_xname(sc->sc_dev));
430 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
431 0, asc->sc_dmasize,
432 asc->sc_flags & ASC_FROMMEMORY
433 ? BUS_DMASYNC_PREWRITE
434 : BUS_DMASYNC_PREREAD);
435 bus_space_write_4(asc->sc_bst, asc->sc_adrh, 0,
436 asc->sc_dmamap->dm_segs[0].ds_addr);
437 bus_space_write_4(asc->sc_bst, asc->sc_dirh, 0,
438 asc->sc_flags & ASC_FROMMEMORY);
439 NCR_DMA(("%s: dma-load %lu@0x%08lx\n",
440 device_xname(sc->sc_dev),
441 asc->sc_dmamap->dm_segs[0].ds_len,
442 asc->sc_dmamap->dm_segs[0].ds_addr));
443 asc->sc_flags |= ASC_MAPLOADED;
446 return 0;
449 static void
450 asc_vsbus_dma_go(struct ncr53c9x_softc *sc)
452 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
454 asc->sc_flags |= ASC_DMAACTIVE;
457 static void
458 asc_vsbus_dma_stop(struct ncr53c9x_softc *sc)
460 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
462 if (asc->sc_flags & ASC_MAPLOADED) {
463 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
464 0, asc->sc_dmasize,
465 asc->sc_flags & ASC_FROMMEMORY
466 ? BUS_DMASYNC_POSTWRITE
467 : BUS_DMASYNC_POSTREAD);
468 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
471 asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
474 static int
475 asc_vsbus_dma_isactive(struct ncr53c9x_softc *sc)
477 struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
479 return (asc->sc_flags & ASC_DMAACTIVE) != 0;