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