1 /* $NetBSD: bzsc.c,v 1.44 2008/04/13 04:55:52 tsutsui Exp $ */
4 * Copyright (c) 1997 Michael L. Hitch
5 * Copyright (c) 1995 Daniel Widenfalk
6 * Copyright (c) 1994 Christian E. Hopps
7 * Copyright (c) 1982, 1990 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Daniel Widenfalk
21 * and Michael L. Hitch.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: bzsc.c,v 1.44 2008/04/13 04:55:52 tsutsui Exp $");
43 * Initial amiga Blizzard 1230-II driver by Daniel Widenfalk. Conversion to
44 * 53c9x MI driver by Michael L. Hitch (mhitch@montana.edu).
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/ioctl.h>
53 #include <sys/device.h>
56 #include <sys/queue.h>
58 #include <uvm/uvm_extern.h>
60 #include <dev/scsipi/scsi_all.h>
61 #include <dev/scsipi/scsipi_all.h>
62 #include <dev/scsipi/scsiconf.h>
63 #include <dev/scsipi/scsi_message.h>
65 #include <machine/cpu.h>
66 #include <machine/param.h>
68 #include <dev/ic/ncr53c9xreg.h>
69 #include <dev/ic/ncr53c9xvar.h>
71 #include <amiga/amiga/isr.h>
72 #include <amiga/dev/bzscvar.h>
73 #include <amiga/dev/zbusvar.h>
76 #define badaddr(a) badaddr_read(a, 2, NULL)
79 int bzscmatch(device_t
, cfdata_t
, void *);
80 void bzscattach(device_t
, device_t
, void *);
82 /* Linkup to the rest of the kernel */
83 CFATTACH_DECL_NEW(bzsc
, sizeof(struct bzsc_softc
),
84 bzscmatch
, bzscattach
, NULL
, NULL
);
87 * Functions and the switch for the MI code.
89 uint8_t bzsc_read_reg(struct ncr53c9x_softc
*, int);
90 void bzsc_write_reg(struct ncr53c9x_softc
*, int, uint8_t);
91 int bzsc_dma_isintr(struct ncr53c9x_softc
*);
92 void bzsc_dma_reset(struct ncr53c9x_softc
*);
93 int bzsc_dma_intr(struct ncr53c9x_softc
*);
94 int bzsc_dma_setup(struct ncr53c9x_softc
*, uint8_t **,
95 size_t *, int, size_t *);
96 void bzsc_dma_go(struct ncr53c9x_softc
*);
97 void bzsc_dma_stop(struct ncr53c9x_softc
*);
98 int bzsc_dma_isactive(struct ncr53c9x_softc
*);
100 struct ncr53c9x_glue bzsc_glue
= {
113 /* Maximum DMA transfer length to reduce impact on high-speed serial input */
114 u_long bzsc_max_dma
= 1024;
115 extern int ser_open_speed
;
117 u_long bzsc_cnt_pio
= 0; /* number of PIO transfers */
118 u_long bzsc_cnt_dma
= 0; /* number of DMA transfers */
119 u_long bzsc_cnt_dma2
= 0; /* number of DMA transfers broken up */
120 u_long bzsc_cnt_dma3
= 0; /* number of pages combined */
129 int bzsc_trace_ptr
= 0;
130 int bzsc_trace_enable
= 1;
131 void bzsc_dump(void);
135 * if we are a Phase5 Blizzard 1230 II
138 bzscmatch(device_t parent
, cfdata_t cf
, void *aux
)
140 struct zbus_args
*zap
;
141 volatile uint8_t *regs
;
144 if (zap
->manid
!= 0x2140 || zap
->prodid
!= 11)
145 return 0; /* It's not Blizzard 1230 */
147 return 0; /* And not A1200 */
148 regs
= &((volatile uint8_t *)zap
->va
)[0x10000];
149 if (badaddr((void *)__UNVOLATILE(regs
)))
151 regs
[NCR_CFG1
* 2] = 0;
152 regs
[NCR_CFG1
* 2] = NCRCFG1_PARENB
| 7;
154 if (regs
[NCR_CFG1
* 2] != (NCRCFG1_PARENB
| 7))
160 * Attach this instance, and then all the sub-devices
163 bzscattach(device_t parent
, device_t self
, void *aux
)
165 struct bzsc_softc
*bsc
= device_private(self
);
166 struct ncr53c9x_softc
*sc
= &bsc
->sc_ncr53c9x
;
167 struct zbus_args
*zap
;
168 extern u_long scsi_nosync
;
169 extern int shift_nosync
;
170 extern int ncr53c9x_debug
;
173 * Set up the glue for MI code early; we use some of it here.
176 sc
->sc_glue
= &bzsc_glue
;
182 bsc
->sc_reg
= &((volatile uint8_t *)zap
->va
)[0x10000];
183 bsc
->sc_dmabase
= &bsc
->sc_reg
[0x21];
185 sc
->sc_freq
= 40; /* Clocked at 40 MHz */
187 aprint_normal(": address %p", bsc
->sc_reg
);
192 * It is necessary to try to load the 2nd config register here,
193 * to find out what rev the FAS chip is, else the ncr53c9x_reset
194 * will not set up the defaults correctly.
196 sc
->sc_cfg1
= sc
->sc_id
| NCRCFG1_PARENB
;
197 sc
->sc_cfg2
= NCRCFG2_SCSI2
| NCRCFG2_FE
;
198 sc
->sc_cfg3
= 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI
| NCRESPCFG3_CDB
;
199 sc
->sc_rev
= NCR_VARIANT_FAS216
;
202 * This is the value used to start sync negotiations
203 * Note that the NCR register "SYNCTP" is programmed
204 * in "clocks per byte", and has a minimum value of 4.
205 * The SCSI period used in negotiation is one-fourth
206 * of the time (in nanoseconds) needed to transfer one byte.
207 * Since the chip's clock is given in MHz, we have the following
208 * formula: 4 * period = (1000 / freq) * 4
210 sc
->sc_minsync
= 1000 / sc
->sc_freq
;
213 * get flags from -I argument and set cf_flags.
214 * NOTE: low 8 bits are to disable disconnect, and the next
215 * 8 bits are to disable sync.
217 device_cfdata(self
)->cf_flags
|= (scsi_nosync
>> shift_nosync
)
221 /* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
222 ncr53c9x_debug
|= (scsi_nosync
>> shift_nosync
) & 0xffff;
226 if (((scsi_nosync
>> shift_nosync
) & 0xff00) == 0xff00)
230 /* Really no limit, but since we want to fit into the TCR... */
231 sc
->sc_maxxfer
= 64 * 1024;
234 * Configure interrupts.
236 bsc
->sc_isr
.isr_intr
= ncr53c9x_intr
;
237 bsc
->sc_isr
.isr_arg
= sc
;
238 bsc
->sc_isr
.isr_ipl
= 2;
239 add_isr(&bsc
->sc_isr
);
242 * Now try to attach all the sub-devices
244 sc
->sc_adapter
.adapt_request
= ncr53c9x_scsipi_request
;
245 sc
->sc_adapter
.adapt_minphys
= minphys
;
254 bzsc_read_reg(struct ncr53c9x_softc
*sc
, int reg
)
256 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
258 return bsc
->sc_reg
[reg
* 2];
262 bzsc_write_reg(struct ncr53c9x_softc
*sc
, int reg
, uint8_t val
)
264 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
267 bsc
->sc_reg
[reg
* 2] = v
;
269 if (bzsc_trace_enable
/* && sc->sc_nexus && sc->sc_nexus->xs->xs_control & XS_CTL_POLL*/ &&
270 reg
== NCR_CMD
/* && bsc->sc_active*/) {
271 bzsc_trace
[(bzsc_trace_ptr
- 1) & 127].yy
= v
;
272 /* printf(" cmd %x", v);*/
278 bzsc_dma_isintr(struct ncr53c9x_softc
*sc
)
280 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
282 if ((bsc
->sc_reg
[NCR_STAT
* 2] & NCRSTAT_INT
) == 0)
286 if (/*sc->sc_nexus && sc->sc_nexus->xs->xs_control & XS_CTL_POLL &&*/ bzsc_trace_enable
) {
287 bzsc_trace
[bzsc_trace_ptr
].status
= bsc
->sc_reg
[NCR_STAT
* 2];
288 bzsc_trace
[bzsc_trace_ptr
].xx
= bsc
->sc_reg
[NCR_CMD
* 2];
289 bzsc_trace
[bzsc_trace_ptr
].yy
= bsc
->sc_active
;
290 bzsc_trace_ptr
= (bzsc_trace_ptr
+ 1) & 127;
297 bzsc_dma_reset(struct ncr53c9x_softc
*sc
)
299 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
305 bzsc_dma_intr(struct ncr53c9x_softc
*sc
)
307 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
310 NCR_DMA(("bzsc_dma_intr: cnt %d int %x stat %x fifo %d ",
311 bsc
->sc_dmasize
, sc
->sc_espintr
, sc
->sc_espstat
,
312 bsc
->sc_reg
[NCR_FFLAG
* 2] & NCRFIFO_FF
));
313 if (bsc
->sc_active
== 0) {
314 printf("bzsc_intr--inactive DMA\n");
318 /* update sc_dmaaddr and sc_pdmalen */
319 cnt
= bsc
->sc_reg
[NCR_TCL
* 2];
320 cnt
+= bsc
->sc_reg
[NCR_TCM
* 2] << 8;
321 cnt
+= bsc
->sc_reg
[NCR_TCH
* 2] << 16;
322 if (!bsc
->sc_datain
) {
323 cnt
+= bsc
->sc_reg
[NCR_FFLAG
* 2] & NCRFIFO_FF
;
324 bsc
->sc_reg
[NCR_CMD
* 2] = NCRCMD_FLUSH
;
326 cnt
= bsc
->sc_dmasize
- cnt
; /* number of bytes transferred */
327 NCR_DMA(("DMA xferred %d\n", cnt
));
328 if (bsc
->sc_xfr_align
) {
329 memcpy(*bsc
->sc_dmaaddr
, bsc
->sc_alignbuf
, cnt
);
330 bsc
->sc_xfr_align
= 0;
332 *bsc
->sc_dmaaddr
+= cnt
;
333 *bsc
->sc_pdmalen
-= cnt
;
339 bzsc_dma_setup(struct ncr53c9x_softc
*sc
, uint8_t **addr
, size_t *len
,
340 int datain
, size_t *dmasize
)
342 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
347 bsc
->sc_dmaaddr
= addr
;
348 bsc
->sc_pdmalen
= len
;
349 bsc
->sc_datain
= datain
;
350 bsc
->sc_dmasize
= *dmasize
;
352 * DMA can be nasty for high-speed serial input, so limit the
353 * size of this DMA operation if the serial port is running at
354 * a high speed (higher than 19200 for now - should be adjusted
355 * based on CPU type and speed?).
356 * XXX - add serial speed check XXX
358 if (ser_open_speed
> 19200 && bzsc_max_dma
!= 0 &&
359 bsc
->sc_dmasize
> bzsc_max_dma
)
360 bsc
->sc_dmasize
= bzsc_max_dma
;
361 ptr
= *addr
; /* Kernel virtual address */
362 pa
= kvtop(ptr
); /* Physical address of DMA */
363 xfer
= min(bsc
->sc_dmasize
, PAGE_SIZE
- (pa
& (PAGE_SIZE
- 1)));
364 bsc
->sc_xfr_align
= 0;
366 * If output and unaligned, stuff odd byte into FIFO
368 if (datain
== 0 && (int)ptr
& 1) {
369 NCR_DMA(("bzsc_dma_setup: align byte written to fifo\n"));
371 xfer
--; /* XXXX CHECK THIS !!!! XXXX */
372 bsc
->sc_reg
[NCR_FIFO
* 2] = *ptr
++;
375 * If unaligned address, read unaligned bytes into alignment buffer
377 else if ((int)ptr
& 1) {
378 pa
= kvtop((void *)&bsc
->sc_alignbuf
);
379 xfer
= bsc
->sc_dmasize
= min(xfer
, sizeof(bsc
->sc_alignbuf
));
380 NCR_DMA(("bzsc_dma_setup: align read by %d bytes\n", xfer
));
381 bsc
->sc_xfr_align
= 1;
383 ++bzsc_cnt_dma
; /* number of DMA operations */
385 while (xfer
< bsc
->sc_dmasize
) {
386 if ((pa
+ xfer
) != kvtop(*addr
+ xfer
))
388 if ((bsc
->sc_dmasize
- xfer
) < PAGE_SIZE
)
389 xfer
= bsc
->sc_dmasize
;
397 bsc
->sc_dmasize
= xfer
;
398 *dmasize
= bsc
->sc_dmasize
;
400 #if defined(M68040) || defined(M68060)
401 if (mmutype
== MMU_68040
) {
402 if (bsc
->sc_xfr_align
) {
403 dma_cachectl(bsc
->sc_alignbuf
,
404 sizeof(bsc
->sc_alignbuf
));
407 dma_cachectl(*bsc
->sc_dmaaddr
, bsc
->sc_dmasize
);
414 bsc
->sc_dmabase
[0x10] = (uint8_t)(pa
>> 24);
415 bsc
->sc_dmabase
[0] = (uint8_t)(pa
>> 16);
416 bsc
->sc_dmabase
[0] = (uint8_t)(pa
>> 8);
417 bsc
->sc_dmabase
[0] = (uint8_t)(pa
);
423 bzsc_dma_go(struct ncr53c9x_softc
*sc
)
428 bzsc_dma_stop(struct ncr53c9x_softc
*sc
)
433 bzsc_dma_isactive(struct ncr53c9x_softc
*sc
)
435 struct bzsc_softc
*bsc
= (struct bzsc_softc
*)sc
;
437 return bsc
->sc_active
;
447 printf("bzsc_trace dump: ptr %x\n", bzsc_trace_ptr
);
449 if (bzsc_trace
[i
].hardbits
== 0) {
453 printf("%02x%02x%02x%02x(", bzsc_trace
[i
].hardbits
,
454 bzsc_trace
[i
].status
, bzsc_trace
[i
].xx
, bzsc_trace
[i
].yy
);
455 if (bzsc_trace
[i
].status
& NCRSTAT_INT
)
457 if (bzsc_trace
[i
].status
& NCRSTAT_TC
)
459 switch(bzsc_trace
[i
].status
& NCRSTAT_PHASE
) {
461 printf("dataout"); break;
463 printf("datain"); break;
465 printf("cmdout"); break;
467 printf("status"); break;
469 printf("msgout"); break;
471 printf("msgin"); break;
473 printf("phase%d?", bzsc_trace
[i
].status
& NCRSTAT_PHASE
);
477 } while (i
!= bzsc_trace_ptr
);