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