asc_vsbus.c revision 1.3 1 /* $NetBSD: asc_vsbus.c,v 1.3 2000/03/04 18:20:53 matt 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.3 2000/03/04 18:20:53 matt Exp $");
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/ioctl.h>
49 #include <sys/device.h>
50 #include <sys/buf.h>
51 #include <sys/proc.h>
52 #include <sys/user.h>
53 #include <sys/reboot.h>
54 #include <sys/queue.h>
55
56 #include <dev/scsipi/scsi_all.h>
57 #include <dev/scsipi/scsipi_all.h>
58 #include <dev/scsipi/scsiconf.h>
59 #include <dev/scsipi/scsi_message.h>
60
61 #include <machine/bus.h>
62
63 #include <dev/ic/ncr53c9xreg.h>
64 #include <dev/ic/ncr53c9xvar.h>
65
66 #include <machine/cpu.h>
67 #include <machine/sid.h>
68 #include <machine/rpb.h>
69 #include <machine/scb.h>
70 #include <machine/vsbus.h>
71
72 struct asc_vsbus_softc {
73 struct ncr53c9x_softc sc_ncr53c9x; /* Must be first */
74 bus_space_tag_t sc_bst; /* bus space tag */
75 bus_space_handle_t sc_bsh; /* bus space handle */
76 bus_dma_tag_t sc_dmat; /* bus dma tag */
77 bus_dmamap_t sc_dmamap;
78 caddr_t *sc_dmaaddr;
79 size_t *sc_dmalen;
80 size_t sc_dmasize;
81 unsigned int sc_flags;
82 #define ASC_DMAACTIVE 0x0001
83 #define ASC_ISWRITE 0x0002
84 #define ASC_INTR 0x0004
85 };
86
87 #define ASC_REG_ADR 0x0000
88 #define ASC_REG_DIR 0x000C
89 #define ASC_REG_NCR 0x0080
90 #define ASC_REG_END 0x00B0
91
92 #define ASC_DIR_TO_MEMORY 0x0000
93 #define ASC_DIR_FROM_MEMORY 0x0001
94
95 #define ASC_MAXXFERSIZE 65536
96 #define ASC_FREQUENCY 20000000
97
98 static int asc_vsbus_match __P((struct device *, struct cfdata *, void *));
99 static void asc_vsbus_attach __P((struct device *, struct device *, void *));
100 static void ascintr __P((void *));
101
102 struct cfattach asc_vsbus_ca = {
103 sizeof(struct asc_vsbus_softc), asc_vsbus_match, asc_vsbus_attach
104 };
105
106 static struct scsipi_device asc_vsbus_dev = {
107 NULL, /* Use the default error handler */
108 NULL, /* have a queue, served by this */
109 NULL, /* have no async handler */
110 NULL, /* use the default done handler */
111 };
112
113 /*
114 * Functions and the switch for the MI code
115 */
116 static u_char asc_vsbus_read_reg __P((struct ncr53c9x_softc *, int));
117 static void asc_vsbus_write_reg __P((struct ncr53c9x_softc *, int, u_char));
118 static int asc_vsbus_dma_isintr __P((struct ncr53c9x_softc *));
119 static void asc_vsbus_dma_reset __P((struct ncr53c9x_softc *));
120 static int asc_vsbus_dma_intr __P((struct ncr53c9x_softc *));
121 static int asc_vsbus_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
122 size_t *, int, size_t *));
123 static void asc_vsbus_dma_go __P((struct ncr53c9x_softc *));
124 static void asc_vsbus_dma_stop __P((struct ncr53c9x_softc *));
125 static int asc_vsbus_dma_isactive __P((struct ncr53c9x_softc *));
126 static void asc_vsbus_clear_latched_intr __P((struct ncr53c9x_softc *));
127
128 static struct ncr53c9x_glue asc_vsbus_glue = {
129 asc_vsbus_read_reg,
130 asc_vsbus_write_reg,
131 asc_vsbus_dma_isintr,
132 asc_vsbus_dma_reset,
133 asc_vsbus_dma_intr,
134 asc_vsbus_dma_setup,
135 asc_vsbus_dma_go,
136 asc_vsbus_dma_stop,
137 asc_vsbus_dma_isactive,
138 asc_vsbus_clear_latched_intr,
139 };
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 int dummy;
146 volatile u_int8_t *ncr_regs;
147
148 if (vax_boardtype != VAX_BTYP_46
149 && vax_boardtype != VAX_BTYP_48
150 && vax_boardtype != VAX_BTYP_49)
151 return 0;
152
153 ncr_regs = (volatile u_int8_t *) va->va_addr;
154
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
162 */
163
164 dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
165 ncr_regs[NCR_CFG1 << 2] = 0x07; /* we're ID 7, 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);
169
170 dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
171 return (dummy & NCRINTR_SBR) != 0;
172 }
173
174
175 /*
176 * Attach this instance, and then all the sub-devices
177 */
178 static void
179 asc_vsbus_attach(struct device *parent, struct device *self, void *aux)
180 {
181 struct vsbus_attach_args *va = aux;
182 struct asc_vsbus_softc *asc = (void *)self;
183 struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
184 int error;
185
186 /*
187 * Set up glue for MI code early; we use some of it here.
188 */
189 sc->sc_glue = &asc_vsbus_glue;
190
191 asc->sc_bst = va->va_iot;
192 asc->sc_dmat = va->va_dmat;
193
194 error = bus_space_map(asc->sc_bst, va->va_paddr - ASC_REG_NCR,
195 ASC_REG_END, 0, &asc->sc_bsh);
196 if (error) {
197 printf(": failed to map registers: error=%d\n", error);
198 return;
199 }
200
201 sc->sc_id = 7; /* XXX need to get this from VMB */
202 sc->sc_freq = ASC_FREQUENCY;
203
204 /* gimme Mhz */
205 sc->sc_freq /= 1000000;
206
207 scb_vecalloc(va->va_cvec, ascintr, asc, SCB_ISTACK);
208
209 /*
210 * XXX More of this should be in ncr53c9x_attach(), but
211 * XXX should we really poke around the chip that much in
212 * XXX the MI code? Think about this more...
213 */
214
215 /*
216 * Set up static configuration info.
217 */
218 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
219 sc->sc_cfg2 = NCRCFG2_SCSI2;
220 sc->sc_cfg3 = NCRCFG3_CDB;
221 sc->sc_rev = NCR_VARIANT_NCR53C94;
222
223 /*
224 * XXX minsync and maxxfer _should_ be set up in MI code,
225 * XXX but it appears to have some dependency on what sort
226 * XXX of DMA we're hooked up to, etc.
227 */
228
229 /*
230 * This is the value used to start sync negotiations
231 * Note that the NCR register "SYNCTP" is programmed
232 * in "clocks per byte", and has a minimum value of 4.
233 * The SCSI period used in negotiation is one-fourth
234 * of the time (in nanoseconds) needed to transfer one byte.
235 * Since the chip's clock is given in MHz, we have the following
236 * formula: 4 * period = (1000 / freq) * 4
237 */
238 sc->sc_minsync = (1000 / sc->sc_freq);
239 sc->sc_maxxfer = 64 * 1024;
240
241 printf("\n%s", self->dv_xname); /* Pretty print */
242
243 /* Do the common parts of attachment. */
244 sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
245 sc->sc_adapter.scsipi_minphys = minphys;
246 ncr53c9x_attach(sc, &asc_vsbus_dev);
247
248 /*
249 * Register this device as boot device if we booted from it.
250 * This will fail if there are more than one le in a machine,
251 * fortunately there may be only one.
252 */
253 if (B_TYPE(bootdev) == BDEV_SD)
254 booted_from = self;
255 }
256
257 static void
258 ascintr(void *arg)
259 {
260 struct asc_vsbus_softc *asc = arg;
261 asc->sc_flags |= ASC_INTR;
262 ncr53c9x_intr(&asc->sc_ncr53c9x);
263 asc->sc_flags &= ~ASC_INTR;
264 }
265
266 /*
267 * Glue functions.
268 */
269
270 static u_char
271 asc_vsbus_read_reg(struct ncr53c9x_softc *sc, int reg)
272 {
273 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
274 u_char v;
275
276 v = bus_space_read_1(asc->sc_bst, asc->sc_bsh,
277 ASC_REG_NCR + reg * sizeof(u_int32_t));
278
279 return (v);
280 }
281
282 static void
283 asc_vsbus_write_reg(sc, reg, val)
284 struct ncr53c9x_softc *sc;
285 int reg;
286 u_char val;
287 {
288 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
289
290 bus_space_write_1(asc->sc_bst, asc->sc_bsh,
291 ASC_REG_NCR + reg * sizeof(u_int32_t), val);
292 }
293
294 static int
295 asc_vsbus_dma_isintr(sc)
296 struct ncr53c9x_softc *sc;
297 {
298 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
299 int isintr = (asc->sc_flags & ASC_INTR);
300 asc->sc_flags &= ~ASC_INTR;
301 return isintr; /* this is the only reason */
302 }
303
304 static void
305 asc_vsbus_dma_reset(sc)
306 struct ncr53c9x_softc *sc;
307 {
308 #if 0
309 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
310 #endif
311
312 /* nothing to do */
313 }
314
315 static int
316 asc_vsbus_dma_intr(sc)
317 struct ncr53c9x_softc *sc;
318 {
319 #if 0
320 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
321 #endif
322
323 return 0;
324 }
325
326 static int
327 asc_vsbus_dma_setup(struct ncr53c9x_softc *sc, caddr_t *addr, size_t *len,
328 int datain, size_t *dmasize)
329 {
330 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
331
332 asc->sc_dmaaddr = addr;
333 asc->sc_dmalen = len;
334 if (datain) {
335 asc->sc_flags |= ASC_ISWRITE;
336 } else {
337 asc->sc_flags &= ~ASC_ISWRITE;
338 }
339
340 NCR_DMA(("%s: start %d@%p,%d\n", sc->sc_dev.dv_xname,
341 (int)*asc->sc_dmalen, *asc->sc_dmaaddr, (asc->sc_flags & ASC_ISWRITE)));
342 *dmasize = asc->sc_dmasize = min(*dmasize, ASC_MAXXFERSIZE);
343
344 if (asc->sc_dmasize) {
345 if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap,
346 *asc->sc_dmaaddr, asc->sc_dmasize,
347 NULL /* kernel address */,
348 BUS_DMA_NOWAIT))
349 panic("%s: cannot load dma map", sc->sc_dev.dv_xname);
350 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
351 0, asc->sc_dmasize, datain
352 ? BUS_DMASYNC_PREREAD
353 : BUS_DMASYNC_PREWRITE);
354 bus_space_write_4(asc->sc_bst, asc->sc_bsh, ASC_REG_ADR,
355 asc->sc_dmamap->dm_segs[0].ds_addr);
356 bus_space_write_4(asc->sc_bst, asc->sc_bsh, ASC_REG_DIR,
357 datain ? 0 : 1);
358 }
359
360
361 return 0;
362 }
363
364 static void
365 asc_vsbus_dma_go(struct ncr53c9x_softc *sc)
366 {
367 #if 0
368 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
369 #endif
370
371 /* Nothing to do */
372 }
373
374 static void
375 asc_vsbus_dma_stop(struct ncr53c9x_softc *sc)
376 {
377 #if 0
378 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
379 #endif
380
381 /*
382 * XXX STOP DMA HERE!
383 */
384 }
385
386 static int
387 asc_vsbus_dma_isactive(struct ncr53c9x_softc *sc)
388 {
389 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
390
391 return (asc->sc_flags & ASC_DMAACTIVE) != 0;
392 }
393
394 static void
395 asc_vsbus_clear_latched_intr(struct ncr53c9x_softc *sc)
396 {
397 #if 0
398 struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
399 #endif
400 }
401