bzsc.c revision 1.24 1 /* $NetBSD: bzsc.c,v 1.24 1998/11/19 21:44:34 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1997 Michael L. Hitch
5 * Copyright (c) 1995 Daniel Widenfalk
6 * Copyright (c) 1994 Christian E. Hopps
7 * Copyright (c) 1982, 1990 The Regents of the University of California.
8 * All rights reserved.
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 Daniel Widenfalk
21 * and Michael L. Hitch.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 /*
40 * Initial amiga Blizzard 1230-II driver by Daniel Widenfalk. Conversion to
41 * 53c9x MI driver by Michael L. Hitch (mhitch (at) montana.edu).
42 */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/device.h>
51 #include <sys/buf.h>
52 #include <sys/proc.h>
53 #include <sys/user.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/cpu.h>
62 #include <machine/param.h>
63
64 #include <dev/ic/ncr53c9xreg.h>
65 #include <dev/ic/ncr53c9xvar.h>
66
67 #include <amiga/amiga/isr.h>
68 #include <amiga/dev/bzscvar.h>
69 #include <amiga/dev/zbusvar.h>
70
71 void bzscattach __P((struct device *, struct device *, void *));
72 int bzscmatch __P((struct device *, struct cfdata *, void *));
73
74 /* Linkup to the rest of the kernel */
75 struct cfattach bzsc_ca = {
76 sizeof(struct bzsc_softc), bzscmatch, bzscattach
77 };
78
79 struct scsipi_device bzsc_dev = {
80 NULL, /* Use default error handler */
81 NULL, /* have a queue, served by this */
82 NULL, /* have no async handler */
83 NULL, /* Use default 'done' routine */
84 };
85
86 /*
87 * Functions and the switch for the MI code.
88 */
89 u_char bzsc_read_reg __P((struct ncr53c9x_softc *, int));
90 void bzsc_write_reg __P((struct ncr53c9x_softc *, int, u_char));
91 int bzsc_dma_isintr __P((struct ncr53c9x_softc *));
92 void bzsc_dma_reset __P((struct ncr53c9x_softc *));
93 int bzsc_dma_intr __P((struct ncr53c9x_softc *));
94 int bzsc_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
95 size_t *, int, size_t *));
96 void bzsc_dma_go __P((struct ncr53c9x_softc *));
97 void bzsc_dma_stop __P((struct ncr53c9x_softc *));
98 int bzsc_dma_isactive __P((struct ncr53c9x_softc *));
99
100 struct ncr53c9x_glue bzsc_glue = {
101 bzsc_read_reg,
102 bzsc_write_reg,
103 bzsc_dma_isintr,
104 bzsc_dma_reset,
105 bzsc_dma_intr,
106 bzsc_dma_setup,
107 bzsc_dma_go,
108 bzsc_dma_stop,
109 bzsc_dma_isactive,
110 0,
111 };
112
113 /* Maximum DMA transfer length to reduce impact on high-speed serial input */
114 u_long bzsc_max_dma = 1024;
115 extern int ser_open_speed;
116
117 u_long bzsc_cnt_pio = 0; /* number of PIO transfers */
118 u_long bzsc_cnt_dma = 0; /* number of DMA transfers */
119 u_long bzsc_cnt_dma2 = 0; /* number of DMA transfers broken up */
120 u_long bzsc_cnt_dma3 = 0; /* number of pages combined */
121
122 #ifdef DEBUG
123 struct {
124 u_char hardbits;
125 u_char status;
126 u_char xx;
127 u_char yy;
128 } bzsc_trace[128];
129 int bzsc_trace_ptr = 0;
130 int bzsc_trace_enable = 1;
131 void bzsc_dump __P((void));
132 #endif
133
134 /*
135 * if we are a Phase5 Blizzard 1230 II
136 */
137 int
138 bzscmatch(parent, cf, aux)
139 struct device *parent;
140 struct cfdata *cf;
141 void *aux;
142 {
143 struct zbus_args *zap;
144 volatile u_char *regs;
145
146 zap = aux;
147 if (zap->manid != 0x2140 || zap->prodid != 11)
148 return(0); /* It's not Blizzard 1230 */
149 if (!is_a1200())
150 return(0); /* And not A1200 */
151 regs = &((volatile u_char *)zap->va)[0x10000];
152 if (badaddr((caddr_t)regs))
153 return(0);
154 regs[NCR_CFG1 * 2] = 0;
155 regs[NCR_CFG1 * 2] = NCRCFG1_PARENB | 7;
156 delay(5);
157 if (regs[NCR_CFG1 * 2] != (NCRCFG1_PARENB | 7))
158 return(0);
159 return(1);
160 }
161
162 /*
163 * Attach this instance, and then all the sub-devices
164 */
165 void
166 bzscattach(parent, self, aux)
167 struct device *parent, *self;
168 void *aux;
169 {
170 struct bzsc_softc *bsc = (void *)self;
171 struct ncr53c9x_softc *sc = &bsc->sc_ncr53c9x;
172 struct zbus_args *zap;
173 extern u_long scsi_nosync;
174 extern int shift_nosync;
175 extern int ncr53c9x_debug;
176
177 /*
178 * Set up the glue for MI code early; we use some of it here.
179 */
180 sc->sc_glue = &bzsc_glue;
181
182 /*
183 * Save the regs
184 */
185 zap = aux;
186 bsc->sc_reg = &((volatile u_char *)zap->va)[0x10000];
187 bsc->sc_dmabase = &bsc->sc_reg[0x21];
188
189 sc->sc_freq = 40; /* Clocked at 40Mhz */
190
191 printf(": address %p", bsc->sc_reg);
192
193 sc->sc_id = 7;
194
195 /*
196 * It is necessary to try to load the 2nd config register here,
197 * to find out what rev the FAS chip is, else the ncr53c9x_reset
198 * will not set up the defaults correctly.
199 */
200 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
201 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
202 sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
203 sc->sc_rev = NCR_VARIANT_FAS216;
204
205 /*
206 * This is the value used to start sync negotiations
207 * Note that the NCR register "SYNCTP" is programmed
208 * in "clocks per byte", and has a minimum value of 4.
209 * The SCSI period used in negotiation is one-fourth
210 * of the time (in nanoseconds) needed to transfer one byte.
211 * Since the chip's clock is given in MHz, we have the following
212 * formula: 4 * period = (1000 / freq) * 4
213 */
214 sc->sc_minsync = 1000 / sc->sc_freq;
215
216 /*
217 * get flags from -I argument and set cf_flags.
218 * NOTE: low 8 bits are to disable disconnect, and the next
219 * 8 bits are to disable sync.
220 */
221 sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync)
222 & 0xffff;
223 shift_nosync += 16;
224
225 /* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
226 ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
227 shift_nosync += 16;
228
229 #if 1
230 if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
231 sc->sc_minsync = 0;
232 #endif
233
234 /* Really no limit, but since we want to fit into the TCR... */
235 sc->sc_maxxfer = 64 * 1024;
236
237 /*
238 * Configure interrupts.
239 */
240 bsc->sc_isr.isr_intr = (int (*)(void *))ncr53c9x_intr;
241 bsc->sc_isr.isr_arg = sc;
242 bsc->sc_isr.isr_ipl = 2;
243 add_isr(&bsc->sc_isr);
244
245 /*
246 * Now try to attach all the sub-devices
247 */
248 sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
249 sc->sc_adapter.scsipi_minphys = minphys;
250 ncr53c9x_attach(sc, &bzsc_dev);
251 }
252
253 /*
254 * Glue functions.
255 */
256
257 u_char
258 bzsc_read_reg(sc, reg)
259 struct ncr53c9x_softc *sc;
260 int reg;
261 {
262 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
263
264 return bsc->sc_reg[reg * 2];
265 }
266
267 void
268 bzsc_write_reg(sc, reg, val)
269 struct ncr53c9x_softc *sc;
270 int reg;
271 u_char val;
272 {
273 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
274 u_char v = val;
275
276 bsc->sc_reg[reg * 2] = v;
277 #ifdef DEBUG
278 if (bzsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL*/ &&
279 reg == NCR_CMD/* && bsc->sc_active*/) {
280 bzsc_trace[(bzsc_trace_ptr - 1) & 127].yy = v;
281 /* printf(" cmd %x", v);*/
282 }
283 #endif
284 }
285
286 int
287 bzsc_dma_isintr(sc)
288 struct ncr53c9x_softc *sc;
289 {
290 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
291
292 if ((bsc->sc_reg[NCR_STAT * 2] & NCRSTAT_INT) == 0)
293 return 0;
294
295 #ifdef DEBUG
296 if (/*sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL &&*/ bzsc_trace_enable) {
297 bzsc_trace[bzsc_trace_ptr].status = bsc->sc_reg[NCR_STAT * 2];
298 bzsc_trace[bzsc_trace_ptr].xx = bsc->sc_reg[NCR_CMD * 2];
299 bzsc_trace[bzsc_trace_ptr].yy = bsc->sc_active;
300 bzsc_trace_ptr = (bzsc_trace_ptr + 1) & 127;
301 }
302 #endif
303 return 1;
304 }
305
306 void
307 bzsc_dma_reset(sc)
308 struct ncr53c9x_softc *sc;
309 {
310 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
311
312 bsc->sc_active = 0;
313 }
314
315 int
316 bzsc_dma_intr(sc)
317 struct ncr53c9x_softc *sc;
318 {
319 register struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
320 register int cnt;
321
322 NCR_DMA(("bzsc_dma_intr: cnt %d int %x stat %x fifo %d ",
323 bsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
324 bsc->sc_reg[NCR_FFLAG * 2] & NCRFIFO_FF));
325 if (bsc->sc_active == 0) {
326 printf("bzsc_intr--inactive DMA\n");
327 return -1;
328 }
329
330 /* update sc_dmaaddr and sc_pdmalen */
331 cnt = bsc->sc_reg[NCR_TCL * 2];
332 cnt += bsc->sc_reg[NCR_TCM * 2] << 8;
333 cnt += bsc->sc_reg[NCR_TCH * 2] << 16;
334 if (!bsc->sc_datain) {
335 cnt += bsc->sc_reg[NCR_FFLAG * 2] & NCRFIFO_FF;
336 bsc->sc_reg[NCR_CMD * 2] = NCRCMD_FLUSH;
337 }
338 cnt = bsc->sc_dmasize - cnt; /* number of bytes transferred */
339 NCR_DMA(("DMA xferred %d\n", cnt));
340 if (bsc->sc_xfr_align) {
341 bcopy(bsc->sc_alignbuf, *bsc->sc_dmaaddr, cnt);
342 bsc->sc_xfr_align = 0;
343 }
344 *bsc->sc_dmaaddr += cnt;
345 *bsc->sc_pdmalen -= cnt;
346 bsc->sc_active = 0;
347 return 0;
348 }
349
350 int
351 bzsc_dma_setup(sc, addr, len, datain, dmasize)
352 struct ncr53c9x_softc *sc;
353 caddr_t *addr;
354 size_t *len;
355 int datain;
356 size_t *dmasize;
357 {
358 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
359 vm_offset_t pa;
360 u_char *ptr;
361 size_t xfer;
362
363 bsc->sc_dmaaddr = addr;
364 bsc->sc_pdmalen = len;
365 bsc->sc_datain = datain;
366 bsc->sc_dmasize = *dmasize;
367 /*
368 * DMA can be nasty for high-speed serial input, so limit the
369 * size of this DMA operation if the serial port is running at
370 * a high speed (higher than 19200 for now - should be adjusted
371 * based on cpu type and speed?).
372 * XXX - add serial speed check XXX
373 */
374 if (ser_open_speed > 19200 && bzsc_max_dma != 0 &&
375 bsc->sc_dmasize > bzsc_max_dma)
376 bsc->sc_dmasize = bzsc_max_dma;
377 ptr = *addr; /* Kernel virtual address */
378 pa = kvtop(ptr); /* Physical address of DMA */
379 xfer = min(bsc->sc_dmasize, NBPG - (pa & (NBPG - 1)));
380 bsc->sc_xfr_align = 0;
381 /*
382 * If output and unaligned, stuff odd byte into FIFO
383 */
384 if (datain == 0 && (int)ptr & 1) {
385 NCR_DMA(("bzsc_dma_setup: align byte written to fifo\n"));
386 pa++;
387 xfer--; /* XXXX CHECK THIS !!!! XXXX */
388 bsc->sc_reg[NCR_FIFO * 2] = *ptr++;
389 }
390 /*
391 * If unaligned address, read unaligned bytes into alignment buffer
392 */
393 else if ((int)ptr & 1) {
394 pa = kvtop((caddr_t)&bsc->sc_alignbuf);
395 xfer = bsc->sc_dmasize = min(xfer, sizeof (bsc->sc_alignbuf));
396 NCR_DMA(("bzsc_dma_setup: align read by %d bytes\n", xfer));
397 bsc->sc_xfr_align = 1;
398 }
399 ++bzsc_cnt_dma; /* number of DMA operations */
400
401 while (xfer < bsc->sc_dmasize) {
402 if ((pa + xfer) != kvtop(*addr + xfer))
403 break;
404 if ((bsc->sc_dmasize - xfer) < NBPG)
405 xfer = bsc->sc_dmasize;
406 else
407 xfer += NBPG;
408 ++bzsc_cnt_dma3;
409 }
410 if (xfer != *len)
411 ++bzsc_cnt_dma2;
412
413 bsc->sc_dmasize = xfer;
414 *dmasize = bsc->sc_dmasize;
415 bsc->sc_pa = pa;
416 #if defined(M68040) || defined(M68060)
417 if (mmutype == MMU_68040) {
418 if (bsc->sc_xfr_align) {
419 dma_cachectl(bsc->sc_alignbuf,
420 sizeof(bsc->sc_alignbuf));
421 }
422 else
423 dma_cachectl(*bsc->sc_dmaaddr, bsc->sc_dmasize);
424 }
425 #endif
426
427 pa >>= 1;
428 if (!bsc->sc_datain)
429 pa |= 0x80000000;
430 bsc->sc_dmabase[0x10] = (u_int8_t)(pa >> 24);
431 bsc->sc_dmabase[0] = (u_int8_t)(pa >> 16);
432 bsc->sc_dmabase[0] = (u_int8_t)(pa >> 8);
433 bsc->sc_dmabase[0] = (u_int8_t)(pa);
434 bsc->sc_active = 1;
435 return 0;
436 }
437
438 void
439 bzsc_dma_go(sc)
440 struct ncr53c9x_softc *sc;
441 {
442 }
443
444 void
445 bzsc_dma_stop(sc)
446 struct ncr53c9x_softc *sc;
447 {
448 }
449
450 int
451 bzsc_dma_isactive(sc)
452 struct ncr53c9x_softc *sc;
453 {
454 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
455
456 return bsc->sc_active;
457 }
458
459 #ifdef DEBUG
460 void
461 bzsc_dump()
462 {
463 int i;
464
465 i = bzsc_trace_ptr;
466 printf("bzsc_trace dump: ptr %x\n", bzsc_trace_ptr);
467 do {
468 if (bzsc_trace[i].hardbits == 0) {
469 i = (i + 1) & 127;
470 continue;
471 }
472 printf("%02x%02x%02x%02x(", bzsc_trace[i].hardbits,
473 bzsc_trace[i].status, bzsc_trace[i].xx, bzsc_trace[i].yy);
474 if (bzsc_trace[i].status & NCRSTAT_INT)
475 printf("NCRINT/");
476 if (bzsc_trace[i].status & NCRSTAT_TC)
477 printf("NCRTC/");
478 switch(bzsc_trace[i].status & NCRSTAT_PHASE) {
479 case 0:
480 printf("dataout"); break;
481 case 1:
482 printf("datain"); break;
483 case 2:
484 printf("cmdout"); break;
485 case 3:
486 printf("status"); break;
487 case 6:
488 printf("msgout"); break;
489 case 7:
490 printf("msgin"); break;
491 default:
492 printf("phase%d?", bzsc_trace[i].status & NCRSTAT_PHASE);
493 }
494 printf(") ");
495 i = (i + 1) & 127;
496 } while (i != bzsc_trace_ptr);
497 printf("\n");
498 }
499 #endif
500