cbsc.c revision 1.7 1 /* $NetBSD: cbsc.c,v 1.7 1998/11/14 21:48:22 mhitch 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)
148 return(0); /* It's not Phase5 */
149 if (zap->prodid != 12 && zap->prodid != 11)
150 return(0); /* Not CyberStorm MKI SCSI */
151 if (zap->prodid == 11 && iszthreepa(zap->pa))
152 return(0); /* Fastlane Z3! */
153 regs = &((volatile u_char *)zap->va)[0xf400];
154 if (badaddr((caddr_t)regs))
155 return(0);
156 regs[NCR_CFG1 * 4] = 0;
157 regs[NCR_CFG1 * 4] = NCRCFG1_PARENB | 7;
158 delay(5);
159 if (regs[NCR_CFG1 * 4] != (NCRCFG1_PARENB | 7))
160 return(0);
161 return(1);
162 }
163
164 /*
165 * Attach this instance, and then all the sub-devices
166 */
167 void
168 cbscattach(parent, self, aux)
169 struct device *parent, *self;
170 void *aux;
171 {
172 struct cbsc_softc *csc = (void *)self;
173 struct ncr53c9x_softc *sc = &csc->sc_ncr53c9x;
174 struct zbus_args *zap;
175 extern u_long scsi_nosync;
176 extern int shift_nosync;
177 extern int ncr53c9x_debug;
178
179 /*
180 * Set up the glue for MI code early; we use some of it here.
181 */
182 sc->sc_glue = &cbsc_glue;
183
184 /*
185 * Save the regs
186 */
187 zap = aux;
188 csc->sc_reg = &((volatile u_char *)zap->va)[0xf400];
189 csc->sc_dmabase = &csc->sc_reg[0x400];
190
191 sc->sc_freq = 40; /* Clocked at 40Mhz */
192
193 printf(": address %p", csc->sc_reg);
194
195 sc->sc_id = 7;
196
197 /*
198 * It is necessary to try to load the 2nd config register here,
199 * to find out what rev the FAS chip is, else the ncr53c9x_reset
200 * will not set up the defaults correctly.
201 */
202 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
203 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
204 sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
205 sc->sc_rev = NCR_VARIANT_FAS216;
206
207 /*
208 * This is the value used to start sync negotiations
209 * Note that the NCR register "SYNCTP" is programmed
210 * in "clocks per byte", and has a minimum value of 4.
211 * The SCSI period used in negotiation is one-fourth
212 * of the time (in nanoseconds) needed to transfer one byte.
213 * Since the chip's clock is given in MHz, we have the following
214 * formula: 4 * period = (1000 / freq) * 4
215 */
216 sc->sc_minsync = 1000 / sc->sc_freq;
217
218 /*
219 * get flags from -I argument and set cf_flags.
220 * NOTE: low 8 bits are to disable disconnect, and the next
221 * 8 bits are to disable sync.
222 */
223 sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync)
224 & 0xffff;
225 shift_nosync += 16;
226
227 /* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
228 ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
229 shift_nosync += 16;
230
231 #if 1
232 if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
233 sc->sc_minsync = 0;
234 #endif
235
236 /* Really no limit, but since we want to fit into the TCR... */
237 sc->sc_maxxfer = 64 * 1024;
238
239 /*
240 * Configure interrupts.
241 */
242 csc->sc_isr.isr_intr = (int (*)(void *))ncr53c9x_intr;
243 csc->sc_isr.isr_arg = sc;
244 csc->sc_isr.isr_ipl = 2;
245 add_isr(&csc->sc_isr);
246
247 /*
248 * Now try to attach all the sub-devices
249 */
250 ncr53c9x_attach(sc, &cbsc_switch, &cbsc_dev);
251 }
252
253 /*
254 * Glue functions.
255 */
256
257 u_char
258 cbsc_read_reg(sc, reg)
259 struct ncr53c9x_softc *sc;
260 int reg;
261 {
262 struct cbsc_softc *csc = (struct cbsc_softc *)sc;
263
264 return csc->sc_reg[reg * 4];
265 }
266
267 void
268 cbsc_write_reg(sc, reg, val)
269 struct ncr53c9x_softc *sc;
270 int reg;
271 u_char val;
272 {
273 struct cbsc_softc *csc = (struct cbsc_softc *)sc;
274 u_char v = val;
275
276 csc->sc_reg[reg * 4] = v;
277 #ifdef DEBUG
278 if (cbsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL*/ &&
279 reg == NCR_CMD/* && csc->sc_active*/) {
280 cbsc_trace[(cbsc_trace_ptr - 1) & 127].yy = v;
281 /* printf(" cmd %x", v);*/
282 }
283 #endif
284 }
285
286 int
287 cbsc_dma_isintr(sc)
288 struct ncr53c9x_softc *sc;
289 {
290 struct cbsc_softc *csc = (struct cbsc_softc *)sc;
291
292 if ((csc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) == 0)
293 return 0;
294
295 if (sc->sc_state == NCR_CONNECTED)
296 csc->sc_portbits |= CBSC_PB_LED;
297 else
298 csc->sc_portbits &= ~CBSC_PB_LED;
299 csc->sc_reg[0x802] = csc->sc_portbits;
300
301 if ((csc->sc_reg[0x802] & CBSC_HB_CREQ) == 0)
302 return 0;
303 #ifdef DEBUG
304 if (/*sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL &&*/ cbsc_trace_enable) {
305 cbsc_trace[cbsc_trace_ptr].status = csc->sc_reg[NCR_STAT * 4];
306 cbsc_trace[cbsc_trace_ptr].xx = csc->sc_reg[NCR_CMD * 4];
307 cbsc_trace[cbsc_trace_ptr].yy = csc->sc_active;
308 cbsc_trace_ptr = (cbsc_trace_ptr + 1) & 127;
309 }
310 #endif
311 return 1;
312 }
313
314 void
315 cbsc_dma_reset(sc)
316 struct ncr53c9x_softc *sc;
317 {
318 struct cbsc_softc *csc = (struct cbsc_softc *)sc;
319
320 csc->sc_active = 0;
321 }
322
323 int
324 cbsc_dma_intr(sc)
325 struct ncr53c9x_softc *sc;
326 {
327 register struct cbsc_softc *csc = (struct cbsc_softc *)sc;
328 register int cnt;
329
330 NCR_DMA(("cbsc_dma_intr: cnt %d int %x stat %x fifo %d ",
331 csc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
332 csc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
333 if (csc->sc_active == 0) {
334 printf("cbsc_intr--inactive DMA\n");
335 return -1;
336 }
337
338 /* update sc_dmaaddr and sc_pdmalen */
339 cnt = csc->sc_reg[NCR_TCL * 4];
340 cnt += csc->sc_reg[NCR_TCM * 4] << 8;
341 cnt += csc->sc_reg[NCR_TCH * 4] << 16;
342 if (!csc->sc_datain) {
343 cnt += csc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
344 csc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
345 }
346 cnt = csc->sc_dmasize - cnt; /* number of bytes transferred */
347 NCR_DMA(("DMA xferred %d\n", cnt));
348 if (csc->sc_xfr_align) {
349 bcopy(csc->sc_alignbuf, *csc->sc_dmaaddr, cnt);
350 csc->sc_xfr_align = 0;
351 }
352 *csc->sc_dmaaddr += cnt;
353 *csc->sc_pdmalen -= cnt;
354 csc->sc_active = 0;
355 return 0;
356 }
357
358 int
359 cbsc_dma_setup(sc, addr, len, datain, dmasize)
360 struct ncr53c9x_softc *sc;
361 caddr_t *addr;
362 size_t *len;
363 int datain;
364 size_t *dmasize;
365 {
366 struct cbsc_softc *csc = (struct cbsc_softc *)sc;
367 vm_offset_t pa;
368 u_char *ptr;
369 size_t xfer;
370
371 csc->sc_dmaaddr = addr;
372 csc->sc_pdmalen = len;
373 csc->sc_datain = datain;
374 csc->sc_dmasize = *dmasize;
375 /*
376 * DMA can be nasty for high-speed serial input, so limit the
377 * size of this DMA operation if the serial port is running at
378 * a high speed (higher than 19200 for now - should be adjusted
379 * based on cpu type and speed?).
380 * XXX - add serial speed check XXX
381 */
382 if (ser_open_speed > 19200 && cbsc_max_dma != 0 &&
383 csc->sc_dmasize > cbsc_max_dma)
384 csc->sc_dmasize = cbsc_max_dma;
385 ptr = *addr; /* Kernel virtual address */
386 pa = kvtop(ptr); /* Physical address of DMA */
387 xfer = min(csc->sc_dmasize, NBPG - (pa & (NBPG - 1)));
388 csc->sc_xfr_align = 0;
389 /*
390 * If output and unaligned, stuff odd byte into FIFO
391 */
392 if (datain == 0 && (int)ptr & 1) {
393 NCR_DMA(("cbsc_dma_setup: align byte written to fifo\n"));
394 pa++;
395 xfer--; /* XXXX CHECK THIS !!!! XXXX */
396 csc->sc_reg[NCR_FIFO * 4] = *ptr++;
397 }
398 /*
399 * If unaligned address, read unaligned bytes into alignment buffer
400 */
401 else if ((int)ptr & 1) {
402 pa = kvtop((caddr_t)&csc->sc_alignbuf);
403 xfer = csc->sc_dmasize = min(xfer, sizeof (csc->sc_alignbuf));
404 NCR_DMA(("cbsc_dma_setup: align read by %d bytes\n", xfer));
405 csc->sc_xfr_align = 1;
406 }
407 ++cbsc_cnt_dma; /* number of DMA operations */
408
409 while (xfer < csc->sc_dmasize) {
410 if ((pa + xfer) != kvtop(*addr + xfer))
411 break;
412 if ((csc->sc_dmasize - xfer) < NBPG)
413 xfer = csc->sc_dmasize;
414 else
415 xfer += NBPG;
416 ++cbsc_cnt_dma3;
417 }
418 if (xfer != *len)
419 ++cbsc_cnt_dma2;
420
421 csc->sc_dmasize = xfer;
422 *dmasize = csc->sc_dmasize;
423 csc->sc_pa = pa;
424 #if defined(M68040) || defined(M68060)
425 if (mmutype == MMU_68040) {
426 if (csc->sc_xfr_align) {
427 dma_cachectl(csc->sc_alignbuf,
428 sizeof(csc->sc_alignbuf));
429 }
430 else
431 dma_cachectl(*csc->sc_dmaaddr, csc->sc_dmasize);
432 }
433 #endif
434
435 if (csc->sc_datain)
436 pa &= ~1;
437 else
438 pa |= 1;
439 csc->sc_dmabase[0] = (u_int8_t)(pa >> 24);
440 csc->sc_dmabase[2] = (u_int8_t)(pa >> 16);
441 csc->sc_dmabase[4] = (u_int8_t)(pa >> 8);
442 csc->sc_dmabase[6] = (u_int8_t)(pa);
443 if (csc->sc_datain)
444 csc->sc_portbits &= ~CBSC_PB_WRITE;
445 else
446 csc->sc_portbits |= CBSC_PB_WRITE;
447 csc->sc_reg[0x802] = csc->sc_portbits;
448 csc->sc_active = 1;
449 return 0;
450 }
451
452 void
453 cbsc_dma_go(sc)
454 struct ncr53c9x_softc *sc;
455 {
456 }
457
458 void
459 cbsc_dma_stop(sc)
460 struct ncr53c9x_softc *sc;
461 {
462 }
463
464 int
465 cbsc_dma_isactive(sc)
466 struct ncr53c9x_softc *sc;
467 {
468 struct cbsc_softc *csc = (struct cbsc_softc *)sc;
469
470 return csc->sc_active;
471 }
472
473 #ifdef DEBUG
474 void
475 cbsc_dump()
476 {
477 int i;
478
479 i = cbsc_trace_ptr;
480 printf("cbsc_trace dump: ptr %x\n", cbsc_trace_ptr);
481 do {
482 if (cbsc_trace[i].hardbits == 0) {
483 i = (i + 1) & 127;
484 continue;
485 }
486 printf("%02x%02x%02x%02x(", cbsc_trace[i].hardbits,
487 cbsc_trace[i].status, cbsc_trace[i].xx, cbsc_trace[i].yy);
488 if (cbsc_trace[i].status & NCRSTAT_INT)
489 printf("NCRINT/");
490 if (cbsc_trace[i].status & NCRSTAT_TC)
491 printf("NCRTC/");
492 switch(cbsc_trace[i].status & NCRSTAT_PHASE) {
493 case 0:
494 printf("dataout"); break;
495 case 1:
496 printf("datain"); break;
497 case 2:
498 printf("cmdout"); break;
499 case 3:
500 printf("status"); break;
501 case 6:
502 printf("msgout"); break;
503 case 7:
504 printf("msgin"); break;
505 default:
506 printf("phase%d?", cbsc_trace[i].status & NCRSTAT_PHASE);
507 }
508 printf(") ");
509 i = (i + 1) & 127;
510 } while (i != cbsc_trace_ptr);
511 printf("\n");
512 }
513 #endif
514