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