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