asc_vsbus.c revision 1.37 1 /* $NetBSD: asc_vsbus.c,v 1.37 2008/03/07 16:02:43 christos 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 Charles M. Hannum.
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> /* RCS ID & Copyright macro defns */
40
41 __KERNEL_RCSID(0, "$NetBSD: asc_vsbus.c,v 1.37 2008/03/07 16:02:43 christos Exp $");
42
43 #include "locators.h"
44 #include "opt_cputype.h"
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/errno.h>
51 #include <sys/ioctl.h>
52 #include <sys/device.h>
53 #include <sys/buf.h>
54 #include <sys/proc.h>
55 #include <sys/user.h>
56 #include <sys/reboot.h>
57 #include <sys/queue.h>
58
59 #include <dev/scsipi/scsi_all.h>
60 #include <dev/scsipi/scsipi_all.h>
61 #include <dev/scsipi/scsiconf.h>
62 #include <dev/scsipi/scsi_message.h>
63
64 #include <machine/bus.h>
65 #include <machine/vmparam.h>
66
67 #include <dev/ic/ncr53c9xreg.h>
68 #include <dev/ic/ncr53c9xvar.h>
69
70 #include <machine/cpu.h>
71 #include <machine/sid.h>
72 #include <machine/scb.h>
73 #include <machine/vsbus.h>
74 #include <machine/clock.h> /* for SCSI ctlr ID# XXX */
75
76 struct asc_vsbus_softc {
77 struct ncr53c9x_softc sc_ncr53c9x; /* Must be first */
78 struct evcnt sc_intrcnt; /* count interrupts */
79 bus_space_tag_t sc_bst; /* bus space tag */
80 bus_space_handle_t sc_bsh; /* bus space handle */
81 bus_space_handle_t sc_dirh; /* scsi direction handle */
82 bus_space_handle_t sc_adrh; /* scsi address handle */
83 bus_space_handle_t sc_ncrh; /* ncr bus space handle */
84 bus_dma_tag_t sc_dmat; /* bus DMA tag */
85 bus_dmamap_t sc_dmamap;
86 void **sc_dmaaddr;
87 size_t *sc_dmalen;
88 size_t sc_dmasize;
89 unsigned int sc_flags;
90 #define ASC_FROMMEMORY 0x0001 /* Must be 1 */
91 #define ASC_DMAACTIVE 0x0002
92 #define ASC_MAPLOADED 0x0004
93 unsigned long sc_xfers;
94 };
95
96 #define ASC_REG_KA46_ADR 0x0000
97 #define ASC_REG_KA46_DIR 0x000C
98 #define ASC_REG_KA49_ADR 0x0000
99 #define ASC_REG_KA49_DIR 0x0004
100 #define ASC_REG_NCR 0x0080
101 #define ASC_REG_END 0x00B0
102
103 #define ASC_MAXXFERSIZE 65536
104 #define ASC_FREQUENCY 25000000
105
106 static int asc_vsbus_match(struct device *, struct cfdata *, void *);
107 static void asc_vsbus_attach(struct device *, struct device *, void *);
108
109 CFATTACH_DECL(asc_vsbus, sizeof(struct asc_vsbus_softc),
110 asc_vsbus_match, asc_vsbus_attach, NULL, NULL);
111
112 /*
113 * Functions and the switch for the MI code
114 */
115 static u_char asc_vsbus_read_reg(struct ncr53c9x_softc *, int);
116 static void asc_vsbus_write_reg(struct ncr53c9x_softc *, int, u_char);
117 static int asc_vsbus_dma_isintr(struct ncr53c9x_softc *);
118 static void asc_vsbus_dma_reset(struct ncr53c9x_softc *);
119 static int asc_vsbus_dma_intr(struct ncr53c9x_softc *);
120 static int asc_vsbus_dma_setup(struct ncr53c9x_softc *, void **,
121 size_t *, int, size_t *);
122 static void asc_vsbus_dma_go(struct ncr53c9x_softc *);
123 static void asc_vsbus_dma_stop(struct ncr53c9x_softc *);
124 static int asc_vsbus_dma_isactive(struct ncr53c9x_softc *);
125
126 static struct ncr53c9x_glue asc_vsbus_glue = {
127 asc_vsbus_read_reg,
128 asc_vsbus_write_reg,
129 asc_vsbus_dma_isintr,
130 asc_vsbus_dma_reset,
131 asc_vsbus_dma_intr,
132 asc_vsbus_dma_setup,
133 asc_vsbus_dma_go,
134 asc_vsbus_dma_stop,
135 asc_vsbus_dma_isactive,
136 NULL,
137 };
138
139 static u_int8_t asc_attached; /* can't have more than one asc */
140
141 static int
142 asc_vsbus_match( struct device *parent, struct cfdata *cf, void *aux)
143 {
144 struct vsbus_attach_args *va = aux;
145 volatile u_int8_t *ncr_regs;
146 int dummy;
147
148 if (asc_attached)
149 return 0;
150
151 if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
152 if (cf->cf_loc[VSBUSCF_CSR] != 0x200c0080)
153 return 0;
154 } else if (vax_boardtype == VAX_BTYP_49 ||
155 vax_boardtype == VAX_BTYP_53) {
156 if (cf->cf_loc[VSBUSCF_CSR] != 0x26000080)
157 return 0;
158 } else {
159 return 0;
160 }
161
162 ncr_regs = (volatile u_int8_t *) va->va_addr;
163
164 /* *** need to generate an interrupt here
165 * From trial and error, I've determined that an INT is generated
166 * only when the following sequence of events occurs:
167 * 1) The interrupt status register (0x05) must be read.
168 * 2) SCSI bus reset interrupt must be enabled
169 * 3) SCSI bus reset command must be sent
170 * 4) NOP command must be sent
171 */
172
173 dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
174 ncr_regs[NCR_CFG1 << 2] = 0x06; /* we're ID 6, turn on INT for SCSI reset */
175 ncr_regs[NCR_CMD << 2] = NCRCMD_RSTSCSI; /* send the reset */
176 ncr_regs[NCR_CMD << 2] = NCRCMD_NOP; /* send a NOP */
177 DELAY(10000);
178
179 dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
180 return (dummy & NCRINTR_SBR) != 0;
181 }
182
183
184 /*
185 * Attach this instance, and then all the sub-devices
186 */
187 static void
188 asc_vsbus_attach(struct device *parent, struct device *self, void *aux)
189 {
190 struct vsbus_attach_args *va = aux;
191 struct asc_vsbus_softc *asc = (void *)self;
192 struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
193 int error;
194
195 asc_attached = 1;
196 /*
197 * Set up glue for MI code early; we use some of it here.
198 */
199 sc->sc_glue = &asc_vsbus_glue;
200
201 asc->sc_bst = va->va_memt;
202 asc->sc_dmat = va->va_dmat;
203
204 error = bus_space_map(asc->sc_bst, va->va_paddr - ASC_REG_NCR,
205 ASC_REG_END, 0, &asc->sc_bsh);
206 if (error) {
207 printf(": failed to map registers: error=%d\n", error);
208 return;
209 }
210 error = bus_space_subregion(asc->sc_bst, asc->sc_bsh, ASC_REG_NCR,
211 ASC_REG_END - ASC_REG_NCR, &asc->sc_ncrh);
212 if (error) {
213 printf(": failed to map ncr registers: error=%d\n", error);
214 return;
215 }
216 if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
217 error = bus_space_subregion(asc->sc_bst, asc->sc_bsh,
218 ASC_REG_KA46_ADR, sizeof(u_int32_t), &asc->sc_adrh);
219 if (error) {
220 printf(": failed to map adr register: error=%d\n",
221 error);
222 return;
223 }
224 error = bus_space_subregion(asc->sc_bst, asc->sc_bsh,
225 ASC_REG_KA46_DIR, sizeof(u_int32_t), &asc->sc_dirh);
226 if (error) {
227 printf(": failed to map dir register: error=%d\n",
228 error);
229 return;
230 }
231 } else {
232 /* This is a gross and disgusting kludge but it'll
233 * save a bunch of ugly code. Unlike the VS4000/60,
234 * the SCSI Address and direction registers are not
235 * near the SCSI NCR registers and are inside the
236 * block of general VAXstation registers. So we grab
237 * them from there and knowing the internals of the
238 * bus_space implementation, we cast to bus_space_handles.
239 */
240 struct vsbus_softc *vsc = (struct vsbus_softc *) parent;
241 asc->sc_adrh = (bus_space_handle_t) (vsc->sc_vsregs + ASC_REG_KA49_ADR);
242 asc->sc_dirh = (bus_space_handle_t) (vsc->sc_vsregs + ASC_REG_KA49_DIR);
243 #if 0
244 printf("\n%s: adrh=0x%08lx dirh=0x%08lx", self->dv_xname,
245 asc->sc_adrh, asc->sc_dirh);
246 ncr53c9x_debug = NCR_SHOWDMA|NCR_SHOWINTS|NCR_SHOWCMDS|NCR_SHOWPHASE|NCR_SHOWSTART|NCR_SHOWMSGS;
247 #endif
248 }
249 error = bus_dmamap_create(asc->sc_dmat, ASC_MAXXFERSIZE, 1,
250 ASC_MAXXFERSIZE, 0, BUS_DMA_NOWAIT, &asc->sc_dmamap);
251
252 #if defined(VAX46) || defined(VAX48) || defined(VAX49) || defined(VAXANY)
253 if(vax_boardtype != VAX_BTYP_53)
254 /* SCSI ID is store in the clock NVRAM at magic address 0xbc */
255 sc->sc_id = (clk_page[0xbc/2] >> clk_tweak) & 7;
256 else
257 #endif
258 sc->sc_id = 6; /* XXX need to get this from VMB */
259 sc->sc_freq = ASC_FREQUENCY;
260
261 /* gimme MHz */
262 sc->sc_freq /= 1000000;
263
264 scb_vecalloc(va->va_cvec, (void (*)(void *)) ncr53c9x_intr,
265 &asc->sc_ncr53c9x, SCB_ISTACK, &asc->sc_intrcnt);
266 evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
267 self->dv_xname, "intr");
268
269 /*
270 * XXX More of this should be in ncr53c9x_attach(), but
271 * XXX should we really poke around the chip that much in
272 * XXX the MI code? Think about this more...
273 */
274
275 /*
276 * Set up static configuration info.
277 */
278 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
279 sc->sc_cfg2 = NCRCFG2_SCSI2;
280 sc->sc_cfg3 = 0;
281 sc->sc_rev = NCR_VARIANT_NCR53C94;
282
283 /*
284 * XXX minsync and maxxfer _should_ be set up in MI code,
285 * XXX but it appears to have some dependency on what sort
286 * XXX of DMA we're hooked up to, etc.
287 */
288
289 /*
290 * This is the value used to start sync negotiations
291 * Note that the NCR register "SYNCTP" is programmed
292 * in "clocks per byte", and has a minimum value of 4.
293 * The SCSI period used in negotiation is one-fourth
294 * of the time (in nanoseconds) needed to transfer one byte.
295 * Since the chip's clock is given in MHz, we have the following
296 * formula: 4 * period = (1000 / freq) * 4
297 */
298 sc->sc_minsync = (1000 / sc->sc_freq);
299 sc->sc_maxxfer = 64 * 1024;
300
301 printf("\n%s", self->dv_xname); /* Pretty print */
302
303 /* Do the common parts of attachment. */
304 sc->sc_adapter.adapt_minphys = minphys;
305 sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
306 ncr53c9x_attach(sc);
307 }
308
309 /*
310 * Glue functions.
311 */
312
313 static u_char
314 asc_vsbus_read_reg(struct ncr53c9x_softc *sc, int reg)
315 {
316 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
317
318 return bus_space_read_1(asc->sc_bst, asc->sc_ncrh,
319 reg * sizeof(u_int32_t));
320 }
321
322 static void
323 asc_vsbus_write_reg(sc, reg, val)
324 struct ncr53c9x_softc *sc;
325 int reg;
326 u_char val;
327 {
328 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
329
330 bus_space_write_1(asc->sc_bst, asc->sc_ncrh,
331 reg * sizeof(u_int32_t), val);
332 }
333
334 static int
335 asc_vsbus_dma_isintr(sc)
336 struct ncr53c9x_softc *sc;
337 {
338 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
339 return bus_space_read_1(asc->sc_bst, asc->sc_ncrh,
340 NCR_STAT * sizeof(u_int32_t)) & NCRSTAT_INT;
341 }
342
343 static void
344 asc_vsbus_dma_reset(sc)
345 struct ncr53c9x_softc *sc;
346 {
347 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
348
349 if (asc->sc_flags & ASC_MAPLOADED)
350 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
351 asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
352 }
353
354 static int
355 asc_vsbus_dma_intr(sc)
356 struct ncr53c9x_softc *sc;
357 {
358 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
359 u_int tcl, tcm;
360 int trans, resid;
361
362 if ((asc->sc_flags & ASC_DMAACTIVE) == 0)
363 panic("asc_vsbus_dma_intr: DMA wasn't active");
364
365 asc->sc_flags &= ~ASC_DMAACTIVE;
366
367 if (asc->sc_dmasize == 0) {
368 /* A "Transfer Pad" operation completed */
369 tcl = NCR_READ_REG(sc, NCR_TCL);
370 tcm = NCR_READ_REG(sc, NCR_TCM);
371 NCR_DMA(("asc_vsbus_intr: discarded %d bytes (tcl=%d, tcm=%d)\n",
372 tcl | (tcm << 8), tcl, tcm));
373 return 0;
374 }
375
376 resid = 0;
377 if ((resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
378 NCR_DMA(("asc_vsbus_intr: empty FIFO of %d ", resid));
379 DELAY(1);
380 }
381 if (asc->sc_flags & ASC_MAPLOADED) {
382 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
383 0, asc->sc_dmasize,
384 asc->sc_flags & ASC_FROMMEMORY
385 ? BUS_DMASYNC_POSTWRITE
386 : BUS_DMASYNC_POSTREAD);
387 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
388 }
389 asc->sc_flags &= ~ASC_MAPLOADED;
390
391 resid += (tcl = NCR_READ_REG(sc, NCR_TCL));
392 resid += (tcm = NCR_READ_REG(sc, NCR_TCM)) << 8;
393
394 trans = asc->sc_dmasize - resid;
395 if (trans < 0) { /* transferred < 0 ? */
396 printf("asc_vsbus_intr: xfer (%d) > req (%lu)\n",
397 trans, (u_long) asc->sc_dmasize);
398 trans = asc->sc_dmasize;
399 }
400 NCR_DMA(("asc_vsbus_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
401 tcl, tcm, trans, resid));
402
403 *asc->sc_dmalen -= trans;
404 *asc->sc_dmaaddr = (char *)*asc->sc_dmaaddr + trans;
405
406 asc->sc_xfers++;
407 return 0;
408 }
409
410 static int
411 asc_vsbus_dma_setup(struct ncr53c9x_softc *sc, void **addr, size_t *len,
412 int datain, size_t *dmasize)
413 {
414 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
415
416 asc->sc_dmaaddr = addr;
417 asc->sc_dmalen = len;
418 if (datain) {
419 asc->sc_flags &= ~ASC_FROMMEMORY;
420 } else {
421 asc->sc_flags |= ASC_FROMMEMORY;
422 }
423 if ((vaddr_t) *asc->sc_dmaaddr < VM_MIN_KERNEL_ADDRESS)
424 panic("asc_vsbus_dma_setup: DMA address (%p) outside of kernel",
425 *asc->sc_dmaaddr);
426
427 NCR_DMA(("%s: start %d@%p,%d\n", sc->sc_dev.dv_xname,
428 (int)*asc->sc_dmalen, *asc->sc_dmaaddr, (asc->sc_flags & ASC_FROMMEMORY)));
429 *dmasize = asc->sc_dmasize = min(*dmasize, ASC_MAXXFERSIZE);
430
431 if (asc->sc_dmasize) {
432 if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap,
433 *asc->sc_dmaaddr, asc->sc_dmasize,
434 NULL /* kernel address */,
435 BUS_DMA_NOWAIT|VAX_BUS_DMA_SPILLPAGE))
436 panic("%s: cannot load DMA map", sc->sc_dev.dv_xname);
437 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
438 0, asc->sc_dmasize,
439 asc->sc_flags & ASC_FROMMEMORY
440 ? BUS_DMASYNC_PREWRITE
441 : BUS_DMASYNC_PREREAD);
442 bus_space_write_4(asc->sc_bst, asc->sc_adrh, 0,
443 asc->sc_dmamap->dm_segs[0].ds_addr);
444 bus_space_write_4(asc->sc_bst, asc->sc_dirh, 0,
445 asc->sc_flags & ASC_FROMMEMORY);
446 NCR_DMA(("%s: dma-load %lu@0x%08lx\n", sc->sc_dev.dv_xname,
447 asc->sc_dmamap->dm_segs[0].ds_len,
448 asc->sc_dmamap->dm_segs[0].ds_addr));
449 asc->sc_flags |= ASC_MAPLOADED;
450 }
451
452 return 0;
453 }
454
455 static void
456 asc_vsbus_dma_go(struct ncr53c9x_softc *sc)
457 {
458 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
459
460 asc->sc_flags |= ASC_DMAACTIVE;
461 }
462
463 static void
464 asc_vsbus_dma_stop(struct ncr53c9x_softc *sc)
465 {
466 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
467
468 if (asc->sc_flags & ASC_MAPLOADED) {
469 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
470 0, asc->sc_dmasize,
471 asc->sc_flags & ASC_FROMMEMORY
472 ? BUS_DMASYNC_POSTWRITE
473 : BUS_DMASYNC_POSTREAD);
474 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
475 }
476
477 asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
478 }
479
480 static int
481 asc_vsbus_dma_isactive(struct ncr53c9x_softc *sc)
482 {
483 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
484
485 return (asc->sc_flags & ASC_DMAACTIVE) != 0;
486 }
487