bztzsc.c revision 1.10 1 /* $NetBSD: bztzsc.c,v 1.10 1998/10/10 00:28:35 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1997 Michael L. Hitch
5 * Copyright (c) 1996 Ignatios Souvatzis
6 * Copyright (c) 1982, 1990 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product contains software written by Ignatios Souvatzis and
20 * Michael L. Hitch for the NetBSD project.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 */
38
39 /*
40 * Initial amiga Blizzard 2060 driver by Ingatios Souvatzis. 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/bztzscvar.h>
69 #include <amiga/dev/zbusvar.h>
70
71 void bztzscattach __P((struct device *, struct device *, void *));
72 int bztzscmatch __P((struct device *, struct cfdata *, void *));
73
74 /* Linkup to the rest of the kernel */
75 struct cfattach bztzsc_ca = {
76 sizeof(struct bztzsc_softc), bztzscmatch, bztzscattach
77 };
78
79 struct scsipi_adapter bztzsc_switch = {
80 ncr53c9x_scsi_cmd,
81 minphys, /* no max at this level; handled by DMA code */
82 NULL, /* scsipi_ioctl */
83 };
84
85 struct scsipi_device bztzsc_dev = {
86 NULL, /* Use default error handler */
87 NULL, /* have a queue, served by this */
88 NULL, /* have no async handler */
89 NULL, /* Use default 'done' routine */
90 };
91
92 /*
93 * Functions and the switch for the MI code.
94 */
95 u_char bztzsc_read_reg __P((struct ncr53c9x_softc *, int));
96 void bztzsc_write_reg __P((struct ncr53c9x_softc *, int, u_char));
97 int bztzsc_dma_isintr __P((struct ncr53c9x_softc *));
98 void bztzsc_dma_reset __P((struct ncr53c9x_softc *));
99 int bztzsc_dma_intr __P((struct ncr53c9x_softc *));
100 int bztzsc_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
101 size_t *, int, size_t *));
102 void bztzsc_dma_go __P((struct ncr53c9x_softc *));
103 void bztzsc_dma_stop __P((struct ncr53c9x_softc *));
104 int bztzsc_dma_isactive __P((struct ncr53c9x_softc *));
105
106 struct ncr53c9x_glue bztzsc_glue = {
107 bztzsc_read_reg,
108 bztzsc_write_reg,
109 bztzsc_dma_isintr,
110 bztzsc_dma_reset,
111 bztzsc_dma_intr,
112 bztzsc_dma_setup,
113 bztzsc_dma_go,
114 bztzsc_dma_stop,
115 bztzsc_dma_isactive,
116 0,
117 };
118
119 /* Maximum DMA transfer length to reduce impact on high-speed serial input */
120 u_long bztzsc_max_dma = 1024;
121 extern int ser_open_speed;
122
123 u_long bztzsc_cnt_pio = 0; /* number of PIO transfers */
124 u_long bztzsc_cnt_dma = 0; /* number of DMA transfers */
125 u_long bztzsc_cnt_dma2 = 0; /* number of DMA transfers broken up */
126 u_long bztzsc_cnt_dma3 = 0; /* number of pages combined */
127
128 #ifdef DEBUG
129 struct {
130 u_char hardbits;
131 u_char status;
132 u_char xx;
133 u_char yy;
134 } bztzsc_trace[128];
135 int bztzsc_trace_ptr = 0;
136 int bztzsc_trace_enable = 1;
137 void bztzsc_dump __P((void));
138 #endif
139
140 /*
141 * if we are a Phase5 Blizzard 2060 SCSI
142 */
143 int
144 bztzscmatch(parent, cf, aux)
145 struct device *parent;
146 struct cfdata *cf;
147 void *aux;
148 {
149 struct zbus_args *zap;
150 volatile u_char *regs;
151
152 zap = aux;
153 if (zap->manid != 0x2140 || zap->prodid != 24)
154 return(0);
155 regs = &((volatile u_char *)zap->va)[0x1ff00];
156 if (badaddr((caddr_t)regs))
157 return(0);
158 regs[NCR_CFG1 * 4] = 0;
159 regs[NCR_CFG1 * 4] = NCRCFG1_PARENB | 7;
160 delay(5);
161 if (regs[NCR_CFG1 * 4] != (NCRCFG1_PARENB | 7))
162 return(0);
163 return(1);
164 }
165
166 /*
167 * Attach this instance, and then all the sub-devices
168 */
169 void
170 bztzscattach(parent, self, aux)
171 struct device *parent, *self;
172 void *aux;
173 {
174 struct bztzsc_softc *bsc = (void *)self;
175 struct ncr53c9x_softc *sc = &bsc->sc_ncr53c9x;
176 struct zbus_args *zap;
177 extern u_long scsi_nosync;
178 extern int shift_nosync;
179 extern int ncr53c9x_debug;
180
181 /*
182 * Set up the glue for MI code early; we use some of it here.
183 */
184 sc->sc_glue = &bztzsc_glue;
185
186 /*
187 * Save the regs
188 */
189 zap = aux;
190 bsc->sc_reg = &((volatile u_char *)zap->va)[0x1ff00];
191 bsc->sc_dmabase = &bsc->sc_reg[0xf0];
192
193 sc->sc_freq = 40; /* Clocked at 40Mhz */
194
195 printf(": address %p", bsc->sc_reg);
196
197 sc->sc_id = 7;
198
199 /*
200 * It is necessary to try to load the 2nd config register here,
201 * to find out what rev the FAS chip is, else the ncr53c9x_reset
202 * will not set up the defaults correctly.
203 */
204 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
205 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
206 sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
207 sc->sc_rev = NCR_VARIANT_FAS216;
208
209 /*
210 * This is the value used to start sync negotiations
211 * Note that the NCR register "SYNCTP" is programmed
212 * in "clocks per byte", and has a minimum value of 4.
213 * The SCSI period used in negotiation is one-fourth
214 * of the time (in nanoseconds) needed to transfer one byte.
215 * Since the chip's clock is given in MHz, we have the following
216 * formula: 4 * period = (1000 / freq) * 4
217 */
218 sc->sc_minsync = 1000 / sc->sc_freq;
219
220 /*
221 * get flags from -I argument and set cf_flags.
222 * NOTE: low 8 bits are to disable disconnect, and the next
223 * 8 bits are to disable sync.
224 */
225 sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync)
226 & 0xffff;
227 shift_nosync += 16;
228
229 /* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
230 ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
231 shift_nosync += 16;
232
233 #if 1
234 if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
235 sc->sc_minsync = 0;
236 #endif
237
238 /* Really no limit, but since we want to fit into the TCR... */
239 sc->sc_maxxfer = 64 * 1024;
240
241 bsc->sc_reg[0xe0] = BZTZSC_PB_LED; /* Turn LED off */
242
243 /*
244 * Configure interrupts.
245 */
246 bsc->sc_isr.isr_intr = (int (*)(void *))ncr53c9x_intr;
247 bsc->sc_isr.isr_arg = sc;
248 bsc->sc_isr.isr_ipl = 2;
249 add_isr(&bsc->sc_isr);
250
251 /*
252 * Now try to attach all the sub-devices
253 */
254 ncr53c9x_attach(sc, &bztzsc_switch, &bztzsc_dev);
255 }
256
257 /*
258 * Glue functions.
259 */
260
261 u_char
262 bztzsc_read_reg(sc, reg)
263 struct ncr53c9x_softc *sc;
264 int reg;
265 {
266 struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
267
268 return bsc->sc_reg[reg * 4];
269 }
270
271 void
272 bztzsc_write_reg(sc, reg, val)
273 struct ncr53c9x_softc *sc;
274 int reg;
275 u_char val;
276 {
277 struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
278 u_char v = val;
279
280 bsc->sc_reg[reg * 4] = v;
281 #ifdef DEBUG
282 if (bztzsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL*/ &&
283 reg == NCR_CMD/* && bsc->sc_active*/) {
284 bztzsc_trace[(bztzsc_trace_ptr - 1) & 127].yy = v;
285 /* printf(" cmd %x", v);*/
286 }
287 #endif
288 }
289
290 int
291 bztzsc_dma_isintr(sc)
292 struct ncr53c9x_softc *sc;
293 {
294 struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
295
296 if ((bsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) == 0)
297 return 0;
298
299 if (sc->sc_state == NCR_CONNECTED)
300 bsc->sc_reg[0xe0] = 0; /* Turn LED on */
301 else
302 bsc->sc_reg[0xe0] = BZTZSC_PB_LED; /* Turn LED off */
303
304 #ifdef DEBUG
305 if (/*sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL &&*/ bztzsc_trace_enable) {
306 bztzsc_trace[bztzsc_trace_ptr].status = bsc->sc_reg[NCR_STAT * 4];
307 bztzsc_trace[bztzsc_trace_ptr].xx = bsc->sc_reg[NCR_CMD * 4];
308 bztzsc_trace[bztzsc_trace_ptr].yy = bsc->sc_active;
309 bztzsc_trace_ptr = (bztzsc_trace_ptr + 1) & 127;
310 }
311 #endif
312 return 1;
313 }
314
315 void
316 bztzsc_dma_reset(sc)
317 struct ncr53c9x_softc *sc;
318 {
319 struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
320
321 bsc->sc_active = 0;
322 }
323
324 int
325 bztzsc_dma_intr(sc)
326 struct ncr53c9x_softc *sc;
327 {
328 register struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
329 register int cnt;
330
331 NCR_DMA(("bztzsc_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 * 4] & NCRFIFO_FF));
334 if (bsc->sc_active == 0) {
335 printf("bztzsc_intr--inactive DMA\n");
336 return -1;
337 }
338
339 /* update sc_dmaaddr and sc_pdmalen */
340 cnt = bsc->sc_reg[NCR_TCL * 4];
341 cnt += bsc->sc_reg[NCR_TCM * 4] << 8;
342 cnt += bsc->sc_reg[NCR_TCH * 4] << 16;
343 if (!bsc->sc_datain) {
344 cnt += bsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
345 bsc->sc_reg[NCR_CMD * 4] = 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 bztzsc_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 bztzsc_softc *bsc = (struct bztzsc_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 && bztzsc_max_dma != 0 &&
384 bsc->sc_dmasize > bztzsc_max_dma)
385 bsc->sc_dmasize = bztzsc_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(("bztzsc_dma_setup: align byte written to fifo\n"));
395 pa++;
396 xfer--; /* XXXX CHECK THIS !!!! XXXX */
397 bsc->sc_reg[NCR_FIFO * 4] = *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(("bztzsc_dma_setup: align read by %d bytes\n", xfer));
406 bsc->sc_xfr_align = 1;
407 }
408 ++bztzsc_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 ++bztzsc_cnt_dma3;
418 }
419 if (xfer != *len)
420 ++bztzsc_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[12] = (u_int8_t)(pa);
440 bsc->sc_dmabase[8] = (u_int8_t)(pa >> 8);
441 bsc->sc_dmabase[4] = (u_int8_t)(pa >> 16);
442 bsc->sc_dmabase[0] = (u_int8_t)(pa >> 24);
443 bsc->sc_active = 1;
444 return 0;
445 }
446
447 void
448 bztzsc_dma_go(sc)
449 struct ncr53c9x_softc *sc;
450 {
451 }
452
453 void
454 bztzsc_dma_stop(sc)
455 struct ncr53c9x_softc *sc;
456 {
457 }
458
459 int
460 bztzsc_dma_isactive(sc)
461 struct ncr53c9x_softc *sc;
462 {
463 struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
464
465 return bsc->sc_active;
466 }
467
468 #ifdef DEBUG
469 void
470 bztzsc_dump()
471 {
472 int i;
473
474 i = bztzsc_trace_ptr;
475 printf("bztzsc_trace dump: ptr %x\n", bztzsc_trace_ptr);
476 do {
477 if (bztzsc_trace[i].hardbits == 0) {
478 i = (i + 1) & 127;
479 continue;
480 }
481 printf("%02x%02x%02x%02x(", bztzsc_trace[i].hardbits,
482 bztzsc_trace[i].status, bztzsc_trace[i].xx, bztzsc_trace[i].yy);
483 if (bztzsc_trace[i].status & NCRSTAT_INT)
484 printf("NCRINT/");
485 if (bztzsc_trace[i].status & NCRSTAT_TC)
486 printf("NCRTC/");
487 switch(bztzsc_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?", bztzsc_trace[i].status & NCRSTAT_PHASE);
502 }
503 printf(") ");
504 i = (i + 1) & 127;
505 } while (i != bztzsc_trace_ptr);
506 printf("\n");
507 }
508 #endif
509