bzsc.c revision 1.19 1 /* $NetBSD: bzsc.c,v 1.19 1997/10/12 19:32:12 mhitch 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 cfdriver bzsc_cd = {
80 NULL, "bzsc", DV_DULL
81 };
82
83 struct scsipi_adapter bzsc_switch = {
84 ncr53c9x_scsi_cmd,
85 minphys, /* no max at this level; handled by DMA code */
86 NULL,
87 NULL,
88 };
89
90 struct scsipi_device bzsc_dev = {
91 NULL, /* Use default error handler */
92 NULL, /* have a queue, served by this */
93 NULL, /* have no async handler */
94 NULL, /* Use default 'done' routine */
95 };
96
97 /*
98 * Functions and the switch for the MI code.
99 */
100 u_char bzsc_read_reg __P((struct ncr53c9x_softc *, int));
101 void bzsc_write_reg __P((struct ncr53c9x_softc *, int, u_char));
102 int bzsc_dma_isintr __P((struct ncr53c9x_softc *));
103 void bzsc_dma_reset __P((struct ncr53c9x_softc *));
104 int bzsc_dma_intr __P((struct ncr53c9x_softc *));
105 int bzsc_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
106 size_t *, int, size_t *));
107 void bzsc_dma_go __P((struct ncr53c9x_softc *));
108 void bzsc_dma_stop __P((struct ncr53c9x_softc *));
109 int bzsc_dma_isactive __P((struct ncr53c9x_softc *));
110
111 struct ncr53c9x_glue bzsc_glue = {
112 bzsc_read_reg,
113 bzsc_write_reg,
114 bzsc_dma_isintr,
115 bzsc_dma_reset,
116 bzsc_dma_intr,
117 bzsc_dma_setup,
118 bzsc_dma_go,
119 bzsc_dma_stop,
120 bzsc_dma_isactive,
121 0,
122 };
123
124 /* Maximum DMA transfer length to reduce impact on high-speed serial input */
125 u_long bzsc_max_dma = 1024;
126 extern int ser_open_speed;
127
128 u_long bzsc_cnt_pio = 0; /* number of PIO transfers */
129 u_long bzsc_cnt_dma = 0; /* number of DMA transfers */
130 u_long bzsc_cnt_dma2 = 0; /* number of DMA transfers broken up */
131 u_long bzsc_cnt_dma3 = 0; /* number of pages combined */
132
133 #ifdef DEBUG
134 struct {
135 u_char hardbits;
136 u_char status;
137 u_char xx;
138 u_char yy;
139 } bzsc_trace[128];
140 int bzsc_trace_ptr = 0;
141 int bzsc_trace_enable = 1;
142 void bzsc_dump __P((void));
143 #endif
144
145 /*
146 * if we are a Phase5 Blizzard 1230 II
147 */
148 int
149 bzscmatch(parent, cf, aux)
150 struct device *parent;
151 struct cfdata *cf;
152 void *aux;
153 {
154 struct zbus_args *zap;
155 volatile u_char *regs;
156
157 zap = aux;
158 if (zap->manid != 0x2140 || zap->prodid != 11)
159 return(0); /* It's not Blizzard 1230 */
160 if (!is_a1200())
161 return(0); /* And not A1200 */
162 regs = &((volatile u_char *)zap->va)[0x10000];
163 if (badaddr((caddr_t)regs))
164 return(0);
165 regs[NCR_CFG1 * 2] = 0;
166 regs[NCR_CFG1 * 2] = NCRCFG1_PARENB | 7;
167 delay(5);
168 if (regs[NCR_CFG1 * 2] != (NCRCFG1_PARENB | 7))
169 return(0);
170 return(1);
171 }
172
173 /*
174 * Attach this instance, and then all the sub-devices
175 */
176 void
177 bzscattach(parent, self, aux)
178 struct device *parent, *self;
179 void *aux;
180 {
181 struct bzsc_softc *bsc = (void *)self;
182 struct ncr53c9x_softc *sc = &bsc->sc_ncr53c9x;
183 struct zbus_args *zap;
184 extern u_long scsi_nosync;
185 extern int shift_nosync;
186 extern int ncr53c9x_debug;
187
188 /*
189 * Set up the glue for MI code early; we use some of it here.
190 */
191 sc->sc_glue = &bzsc_glue;
192
193 /*
194 * Save the regs
195 */
196 zap = aux;
197 bsc->sc_reg = &((volatile u_char *)zap->va)[0x10000];
198 bsc->sc_dmabase = &bsc->sc_reg[0x21];
199
200 sc->sc_freq = 40; /* Clocked at 40Mhz */
201
202 printf(": address %p", bsc->sc_reg);
203
204 sc->sc_id = 7;
205
206 /*
207 * It is necessary to try to load the 2nd config register here,
208 * to find out what rev the FAS chip is, else the ncr53c9x_reset
209 * will not set up the defaults correctly.
210 */
211 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
212 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
213 sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
214 sc->sc_rev = NCR_VARIANT_FAS216;
215
216 /*
217 * This is the value used to start sync negotiations
218 * Note that the NCR register "SYNCTP" is programmed
219 * in "clocks per byte", and has a minimum value of 4.
220 * The SCSI period used in negotiation is one-fourth
221 * of the time (in nanoseconds) needed to transfer one byte.
222 * Since the chip's clock is given in MHz, we have the following
223 * formula: 4 * period = (1000 / freq) * 4
224 */
225 sc->sc_minsync = 1000 / sc->sc_freq;
226
227 /*
228 * get flags from -I argument and set cf_flags.
229 * NOTE: low 8 bits are to disable disconnect, and the next
230 * 8 bits are to disable sync.
231 */
232 sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync)
233 & 0xffff;
234 shift_nosync += 16;
235
236 /* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
237 ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
238 shift_nosync += 16;
239
240 #if 1
241 if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
242 sc->sc_minsync = 0;
243 #endif
244
245 /* Really no limit, but since we want to fit into the TCR... */
246 sc->sc_maxxfer = 64 * 1024;
247
248 /*
249 * Configure interrupts.
250 */
251 bsc->sc_isr.isr_intr = (int (*)(void *))ncr53c9x_intr;
252 bsc->sc_isr.isr_arg = sc;
253 bsc->sc_isr.isr_ipl = 2;
254 add_isr(&bsc->sc_isr);
255
256 /*
257 * Now try to attach all the sub-devices
258 */
259 ncr53c9x_attach(sc, &bzsc_switch, &bzsc_dev);
260 }
261
262 /*
263 * Glue functions.
264 */
265
266 u_char
267 bzsc_read_reg(sc, reg)
268 struct ncr53c9x_softc *sc;
269 int reg;
270 {
271 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
272
273 return bsc->sc_reg[reg * 2];
274 }
275
276 void
277 bzsc_write_reg(sc, reg, val)
278 struct ncr53c9x_softc *sc;
279 int reg;
280 u_char val;
281 {
282 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
283 u_char v = val;
284
285 bsc->sc_reg[reg * 2] = v;
286 #ifdef DEBUG
287 if (bzsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL*/ &&
288 reg == NCR_CMD/* && bsc->sc_active*/) {
289 bzsc_trace[(bzsc_trace_ptr - 1) & 127].yy = v;
290 /* printf(" cmd %x", v);*/
291 }
292 #endif
293 }
294
295 int
296 bzsc_dma_isintr(sc)
297 struct ncr53c9x_softc *sc;
298 {
299 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
300
301 if ((bsc->sc_reg[NCR_STAT * 2] & NCRSTAT_INT) == 0)
302 return 0;
303
304 #ifdef DEBUG
305 if (/*sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL &&*/ bzsc_trace_enable) {
306 bzsc_trace[bzsc_trace_ptr].status = bsc->sc_reg[NCR_STAT * 2];
307 bzsc_trace[bzsc_trace_ptr].xx = bsc->sc_reg[NCR_CMD * 2];
308 bzsc_trace[bzsc_trace_ptr].yy = bsc->sc_active;
309 bzsc_trace_ptr = (bzsc_trace_ptr + 1) & 127;
310 }
311 #endif
312 return 1;
313 }
314
315 void
316 bzsc_dma_reset(sc)
317 struct ncr53c9x_softc *sc;
318 {
319 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
320
321 bsc->sc_active = 0;
322 }
323
324 int
325 bzsc_dma_intr(sc)
326 struct ncr53c9x_softc *sc;
327 {
328 register struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
329 register int cnt;
330
331 NCR_DMA(("bzsc_dma_intr: cnt %d int %x stat %x fifo %d ",
332 bsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
333 bsc->sc_reg[NCR_FFLAG * 2] & NCRFIFO_FF));
334 if (bsc->sc_active == 0) {
335 printf("bzsc_intr--inactive DMA\n");
336 return -1;
337 }
338
339 /* update sc_dmaaddr and sc_pdmalen */
340 cnt = bsc->sc_reg[NCR_TCL * 2];
341 cnt += bsc->sc_reg[NCR_TCM * 2] << 8;
342 cnt += bsc->sc_reg[NCR_TCH * 2] << 16;
343 if (!bsc->sc_datain) {
344 cnt += bsc->sc_reg[NCR_FFLAG * 2] & NCRFIFO_FF;
345 bsc->sc_reg[NCR_CMD * 2] = NCRCMD_FLUSH;
346 }
347 cnt = bsc->sc_dmasize - cnt; /* number of bytes transferred */
348 NCR_DMA(("DMA xferred %d\n", cnt));
349 if (bsc->sc_xfr_align) {
350 bcopy(bsc->sc_alignbuf, *bsc->sc_dmaaddr, cnt);
351 bsc->sc_xfr_align = 0;
352 }
353 *bsc->sc_dmaaddr += cnt;
354 *bsc->sc_pdmalen -= cnt;
355 bsc->sc_active = 0;
356 return 0;
357 }
358
359 int
360 bzsc_dma_setup(sc, addr, len, datain, dmasize)
361 struct ncr53c9x_softc *sc;
362 caddr_t *addr;
363 size_t *len;
364 int datain;
365 size_t *dmasize;
366 {
367 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
368 vm_offset_t pa;
369 u_char *ptr;
370 size_t xfer;
371
372 bsc->sc_dmaaddr = addr;
373 bsc->sc_pdmalen = len;
374 bsc->sc_datain = datain;
375 bsc->sc_dmasize = *dmasize;
376 /*
377 * DMA can be nasty for high-speed serial input, so limit the
378 * size of this DMA operation if the serial port is running at
379 * a high speed (higher than 19200 for now - should be adjusted
380 * based on cpu type and speed?).
381 * XXX - add serial speed check XXX
382 */
383 if (ser_open_speed > 19200 && bzsc_max_dma != 0 &&
384 bsc->sc_dmasize > bzsc_max_dma)
385 bsc->sc_dmasize = bzsc_max_dma;
386 ptr = *addr; /* Kernel virtual address */
387 pa = kvtop(ptr); /* Physical address of DMA */
388 xfer = min(bsc->sc_dmasize, NBPG - (pa & (NBPG - 1)));
389 bsc->sc_xfr_align = 0;
390 /*
391 * If output and unaligned, stuff odd byte into FIFO
392 */
393 if (datain == 0 && (int)ptr & 1) {
394 NCR_DMA(("bzsc_dma_setup: align byte written to fifo\n"));
395 pa++;
396 xfer--; /* XXXX CHECK THIS !!!! XXXX */
397 bsc->sc_reg[NCR_FIFO * 2] = *ptr++;
398 }
399 /*
400 * If unaligned address, read unaligned bytes into alignment buffer
401 */
402 else if ((int)ptr & 1) {
403 pa = kvtop((caddr_t)&bsc->sc_alignbuf);
404 xfer = bsc->sc_dmasize = min(xfer, sizeof (bsc->sc_alignbuf));
405 NCR_DMA(("bzsc_dma_setup: align read by %d bytes\n", xfer));
406 bsc->sc_xfr_align = 1;
407 }
408 ++bzsc_cnt_dma; /* number of DMA operations */
409
410 while (xfer < bsc->sc_dmasize) {
411 if ((pa + xfer) != kvtop(*addr + xfer))
412 break;
413 if ((bsc->sc_dmasize - xfer) < NBPG)
414 xfer = bsc->sc_dmasize;
415 else
416 xfer += NBPG;
417 ++bzsc_cnt_dma3;
418 }
419 if (xfer != *len)
420 ++bzsc_cnt_dma2;
421
422 bsc->sc_dmasize = xfer;
423 *dmasize = bsc->sc_dmasize;
424 bsc->sc_pa = pa;
425 #if defined(M68040) || defined(M68060)
426 if (mmutype == MMU_68040) {
427 if (bsc->sc_xfr_align) {
428 dma_cachectl(bsc->sc_alignbuf,
429 sizeof(bsc->sc_alignbuf));
430 }
431 else
432 dma_cachectl(*bsc->sc_dmaaddr, bsc->sc_dmasize);
433 }
434 #endif
435
436 pa >>= 1;
437 if (!bsc->sc_datain)
438 pa |= 0x80000000;
439 bsc->sc_dmabase[0x10] = (u_int8_t)(pa >> 24);
440 bsc->sc_dmabase[0] = (u_int8_t)(pa >> 16);
441 bsc->sc_dmabase[0] = (u_int8_t)(pa >> 8);
442 bsc->sc_dmabase[0] = (u_int8_t)(pa);
443 bsc->sc_active = 1;
444 return 0;
445 }
446
447 void
448 bzsc_dma_go(sc)
449 struct ncr53c9x_softc *sc;
450 {
451 }
452
453 void
454 bzsc_dma_stop(sc)
455 struct ncr53c9x_softc *sc;
456 {
457 }
458
459 int
460 bzsc_dma_isactive(sc)
461 struct ncr53c9x_softc *sc;
462 {
463 struct bzsc_softc *bsc = (struct bzsc_softc *)sc;
464
465 return bsc->sc_active;
466 }
467
468 #ifdef DEBUG
469 void
470 bzsc_dump()
471 {
472 int i;
473
474 i = bzsc_trace_ptr;
475 printf("bzsc_trace dump: ptr %x\n", bzsc_trace_ptr);
476 do {
477 if (bzsc_trace[i].hardbits == 0) {
478 i = (i + 1) & 127;
479 continue;
480 }
481 printf("%02x%02x%02x%02x(", bzsc_trace[i].hardbits,
482 bzsc_trace[i].status, bzsc_trace[i].xx, bzsc_trace[i].yy);
483 if (bzsc_trace[i].status & NCRSTAT_INT)
484 printf("NCRINT/");
485 if (bzsc_trace[i].status & NCRSTAT_TC)
486 printf("NCRTC/");
487 switch(bzsc_trace[i].status & NCRSTAT_PHASE) {
488 case 0:
489 printf("dataout"); break;
490 case 1:
491 printf("datain"); break;
492 case 2:
493 printf("cmdout"); break;
494 case 3:
495 printf("status"); break;
496 case 6:
497 printf("msgout"); break;
498 case 7:
499 printf("msgin"); break;
500 default:
501 printf("phase%d?", bzsc_trace[i].status & NCRSTAT_PHASE);
502 }
503 printf(") ");
504 i = (i + 1) & 127;
505 } while (i != bzsc_trace_ptr);
506 printf("\n");
507 }
508 #endif
509