flsc.c revision 1.14.10.2 1 1.14.10.2 thorpej /* $NetBSD: flsc.c,v 1.14.10.2 1997/10/14 08:26:35 thorpej Exp $ */
2 1.5 veego
3 1.1 chopps /*
4 1.14.10.2 thorpej * Copyright (c) 1997 Michael L. Hitch
5 1.1 chopps * Copyright (c) 1995 Daniel Widenfalk
6 1.1 chopps * Copyright (c) 1994 Christian E. Hopps
7 1.1 chopps * Copyright (c) 1982, 1990 The Regents of the University of California.
8 1.1 chopps * All rights reserved.
9 1.1 chopps *
10 1.1 chopps * Redistribution and use in source and binary forms, with or without
11 1.1 chopps * modification, are permitted provided that the following conditions
12 1.1 chopps * are met:
13 1.1 chopps * 1. Redistributions of source code must retain the above copyright
14 1.1 chopps * notice, this list of conditions and the following disclaimer.
15 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 chopps * notice, this list of conditions and the following disclaimer in the
17 1.1 chopps * documentation and/or other materials provided with the distribution.
18 1.1 chopps * 3. All advertising materials mentioning features or use of this software
19 1.1 chopps * must display the following acknowledgement:
20 1.14.10.2 thorpej * This product includes software developed by Daniel Widenfalk
21 1.14.10.2 thorpej * and Michael L. Hitch.
22 1.1 chopps * 4. Neither the name of the University nor the names of its contributors
23 1.1 chopps * may be used to endorse or promote products derived from this software
24 1.1 chopps * without specific prior written permission.
25 1.1 chopps *
26 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 chopps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 chopps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 chopps * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 chopps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 chopps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 chopps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 chopps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 chopps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 chopps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 chopps * SUCH DAMAGE.
37 1.1 chopps */
38 1.1 chopps
39 1.14.10.2 thorpej /*
40 1.14.10.2 thorpej * Initial amiga Fastlane driver by Daniel Widenfalk. Conversion to
41 1.14.10.2 thorpej * 53c9x MI driver by Michael L. Hitch (mhitch (at) montana.edu).
42 1.14.10.2 thorpej */
43 1.14.10.2 thorpej
44 1.14.10.2 thorpej #include <sys/types.h>
45 1.1 chopps #include <sys/param.h>
46 1.1 chopps #include <sys/systm.h>
47 1.1 chopps #include <sys/kernel.h>
48 1.14.10.2 thorpej #include <sys/errno.h>
49 1.14.10.2 thorpej #include <sys/ioctl.h>
50 1.1 chopps #include <sys/device.h>
51 1.14.10.2 thorpej #include <sys/buf.h>
52 1.14.10.2 thorpej #include <sys/proc.h>
53 1.14.10.2 thorpej #include <sys/user.h>
54 1.14.10.2 thorpej #include <sys/queue.h>
55 1.14.10.2 thorpej
56 1.14.10.1 thorpej #include <dev/scsipi/scsi_all.h>
57 1.14.10.1 thorpej #include <dev/scsipi/scsipi_all.h>
58 1.14.10.1 thorpej #include <dev/scsipi/scsiconf.h>
59 1.14.10.2 thorpej #include <dev/scsipi/scsi_message.h>
60 1.14.10.2 thorpej
61 1.14.10.2 thorpej #include <machine/cpu.h>
62 1.14.10.2 thorpej #include <machine/param.h>
63 1.14.10.2 thorpej
64 1.14.10.2 thorpej #include <dev/ic/ncr53c9xreg.h>
65 1.14.10.2 thorpej #include <dev/ic/ncr53c9xvar.h>
66 1.14.10.2 thorpej
67 1.1 chopps #include <amiga/amiga/isr.h>
68 1.1 chopps #include <amiga/dev/flscvar.h>
69 1.14.10.2 thorpej #include <amiga/dev/zbusvar.h>
70 1.1 chopps
71 1.14.10.2 thorpej void flscattach __P((struct device *, struct device *, void *));
72 1.14.10.2 thorpej int flscmatch __P((struct device *, struct cfdata *, void *));
73 1.14.10.2 thorpej
74 1.14.10.2 thorpej /* Linkup to the rest of the kernel */
75 1.14.10.2 thorpej struct cfattach flsc_ca = {
76 1.14.10.2 thorpej sizeof(struct flsc_softc), flscmatch, flscattach
77 1.14.10.2 thorpej };
78 1.1 chopps
79 1.14.10.2 thorpej struct cfdriver flsc_cd = {
80 1.14.10.2 thorpej NULL, "flsc", DV_DULL
81 1.1 chopps };
82 1.1 chopps
83 1.14.10.2 thorpej struct scsipi_adapter flsc_switch = {
84 1.14.10.2 thorpej ncr53c9x_scsi_cmd,
85 1.14.10.2 thorpej minphys, /* no max at this level; handled by DMA code */
86 1.14.10.2 thorpej NULL,
87 1.14.10.2 thorpej NULL,
88 1.1 chopps };
89 1.1 chopps
90 1.14.10.2 thorpej struct scsipi_device flsc_dev = {
91 1.14.10.2 thorpej NULL, /* Use default error handler */
92 1.14.10.2 thorpej NULL, /* have a queue, served by this */
93 1.14.10.2 thorpej NULL, /* have no async handler */
94 1.14.10.2 thorpej NULL, /* Use default 'done' routine */
95 1.3 thorpej };
96 1.1 chopps
97 1.14.10.2 thorpej /*
98 1.14.10.2 thorpej * Functions and the switch for the MI code.
99 1.14.10.2 thorpej */
100 1.14.10.2 thorpej u_char flsc_read_reg __P((struct ncr53c9x_softc *, int));
101 1.14.10.2 thorpej void flsc_write_reg __P((struct ncr53c9x_softc *, int, u_char));
102 1.14.10.2 thorpej int flsc_dma_isintr __P((struct ncr53c9x_softc *));
103 1.14.10.2 thorpej void flsc_dma_reset __P((struct ncr53c9x_softc *));
104 1.14.10.2 thorpej int flsc_dma_intr __P((struct ncr53c9x_softc *));
105 1.14.10.2 thorpej int flsc_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
106 1.14.10.2 thorpej size_t *, int, size_t *));
107 1.14.10.2 thorpej void flsc_dma_go __P((struct ncr53c9x_softc *));
108 1.14.10.2 thorpej void flsc_dma_stop __P((struct ncr53c9x_softc *));
109 1.14.10.2 thorpej int flsc_dma_isactive __P((struct ncr53c9x_softc *));
110 1.14.10.2 thorpej void flsc_clear_latched_intr __P((struct ncr53c9x_softc *));
111 1.14.10.2 thorpej
112 1.14.10.2 thorpej struct ncr53c9x_glue flsc_glue = {
113 1.14.10.2 thorpej flsc_read_reg,
114 1.14.10.2 thorpej flsc_write_reg,
115 1.14.10.2 thorpej flsc_dma_isintr,
116 1.14.10.2 thorpej flsc_dma_reset,
117 1.14.10.2 thorpej flsc_dma_intr,
118 1.14.10.2 thorpej flsc_dma_setup,
119 1.14.10.2 thorpej flsc_dma_go,
120 1.14.10.2 thorpej flsc_dma_stop,
121 1.14.10.2 thorpej flsc_dma_isactive,
122 1.14.10.2 thorpej flsc_clear_latched_intr,
123 1.3 thorpej };
124 1.1 chopps
125 1.14.10.2 thorpej /* Maximum DMA transfer length to reduce impact on high-speed serial input */
126 1.14.10.2 thorpej u_long flsc_max_dma = 1024;
127 1.14.10.2 thorpej extern int ser_open_speed;
128 1.14.10.2 thorpej
129 1.14.10.2 thorpej extern int ncr53c9x_debug;
130 1.14.10.2 thorpej extern u_long scsi_nosync;
131 1.14.10.2 thorpej extern int shift_nosync;
132 1.1 chopps
133 1.1 chopps /*
134 1.1 chopps * if we are an Advanced Systems & Software FastlaneZ3
135 1.1 chopps */
136 1.1 chopps int
137 1.14.10.2 thorpej flscmatch(parent, cf, aux)
138 1.14.10.2 thorpej struct device *parent;
139 1.14.10.2 thorpej struct cfdata *cf;
140 1.14.10.2 thorpej void *aux;
141 1.1 chopps {
142 1.1 chopps struct zbus_args *zap;
143 1.1 chopps
144 1.1 chopps if (!is_a4000() && !is_a3000())
145 1.1 chopps return(0);
146 1.1 chopps
147 1.14.10.2 thorpej zap = aux;
148 1.6 is if (zap->manid == 0x2140 && zap->prodid == 11
149 1.6 is && iszthreepa(zap->pa))
150 1.1 chopps return(1);
151 1.1 chopps
152 1.1 chopps return(0);
153 1.1 chopps }
154 1.1 chopps
155 1.14.10.2 thorpej /*
156 1.14.10.2 thorpej * Attach this instance, and then all the sub-devices
157 1.14.10.2 thorpej */
158 1.1 chopps void
159 1.14.10.2 thorpej flscattach(parent, self, aux)
160 1.14.10.2 thorpej struct device *parent, *self;
161 1.14.10.2 thorpej void *aux;
162 1.1 chopps {
163 1.14.10.2 thorpej struct flsc_softc *fsc = (void *)self;
164 1.14.10.2 thorpej struct ncr53c9x_softc *sc = &fsc->sc_ncr53c9x;
165 1.1 chopps struct zbus_args *zap;
166 1.1 chopps
167 1.14.10.2 thorpej /*
168 1.14.10.2 thorpej * Set up the glue for MI code early; we use some of it here.
169 1.14.10.2 thorpej */
170 1.14.10.2 thorpej sc->sc_glue = &flsc_glue;
171 1.14.10.2 thorpej
172 1.14.10.2 thorpej /*
173 1.14.10.2 thorpej * Save the regs
174 1.14.10.2 thorpej */
175 1.14.10.2 thorpej zap = aux;
176 1.14.10.2 thorpej fsc->sc_dmabase = (volatile u_char *)zap->va;
177 1.14.10.2 thorpej fsc->sc_reg = &((volatile u_char *)zap->va)[0x1000001];
178 1.14.10.2 thorpej
179 1.14.10.2 thorpej sc->sc_freq = 40; /* Clocked at 40Mhz */
180 1.14.10.2 thorpej
181 1.14.10.2 thorpej printf(": address %p", fsc->sc_reg);
182 1.14.10.2 thorpej
183 1.14.10.2 thorpej sc->sc_id = 7;
184 1.14.10.2 thorpej
185 1.14.10.2 thorpej /*
186 1.14.10.2 thorpej * It is necessary to try to load the 2nd config register here,
187 1.14.10.2 thorpej * to find out what rev the flsc chip is, else the flsc_reset
188 1.14.10.2 thorpej * will not set up the defaults correctly.
189 1.14.10.2 thorpej */
190 1.14.10.2 thorpej sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
191 1.14.10.2 thorpej sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
192 1.14.10.2 thorpej sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
193 1.14.10.2 thorpej sc->sc_rev = NCR_VARIANT_FAS216;
194 1.14.10.2 thorpej
195 1.14.10.2 thorpej /*
196 1.14.10.2 thorpej * This is the value used to start sync negotiations
197 1.14.10.2 thorpej * Note that the NCR register "SYNCTP" is programmed
198 1.14.10.2 thorpej * in "clocks per byte", and has a minimum value of 4.
199 1.14.10.2 thorpej * The SCSI period used in negotiation is one-fourth
200 1.14.10.2 thorpej * of the time (in nanoseconds) needed to transfer one byte.
201 1.14.10.2 thorpej * Since the chip's clock is given in MHz, we have the following
202 1.14.10.2 thorpej * formula: 4 * period = (1000 / freq) * 4
203 1.14.10.2 thorpej */
204 1.14.10.2 thorpej sc->sc_minsync = 1000 / sc->sc_freq;
205 1.14.10.2 thorpej
206 1.14.10.2 thorpej if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
207 1.14.10.2 thorpej sc->sc_minsync = 0;
208 1.14.10.2 thorpej
209 1.14.10.2 thorpej /* Really no limit, but since we want to fit into the TCR... */
210 1.14.10.2 thorpej sc->sc_maxxfer = 64 * 1024;
211 1.14.10.2 thorpej
212 1.14.10.2 thorpej fsc->sc_portbits = 0xa0 | FLSC_PB_EDI | FLSC_PB_ESI;
213 1.14.10.2 thorpej fsc->sc_hardbits = fsc->sc_reg[0x40];
214 1.14.10.2 thorpej
215 1.14.10.2 thorpej sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync) & 0xffff;
216 1.14.10.2 thorpej shift_nosync += 16;
217 1.14.10.2 thorpej ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
218 1.14.10.2 thorpej shift_nosync += 16;
219 1.14.10.2 thorpej
220 1.14.10.2 thorpej /*
221 1.14.10.2 thorpej * Configure interrupts.
222 1.14.10.2 thorpej */
223 1.14.10.2 thorpej fsc->sc_isr.isr_intr = (int (*)(void *))ncr53c9x_intr;
224 1.14.10.2 thorpej fsc->sc_isr.isr_arg = sc;
225 1.14.10.2 thorpej fsc->sc_isr.isr_ipl = 2;
226 1.14.10.2 thorpej add_isr(&fsc->sc_isr);
227 1.14.10.2 thorpej
228 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
229 1.14.10.2 thorpej
230 1.14.10.2 thorpej /*
231 1.14.10.2 thorpej * Now try to attach all the sub-devices
232 1.14.10.2 thorpej */
233 1.14.10.2 thorpej ncr53c9x_attach(sc, &flsc_switch, &flsc_dev);
234 1.14.10.2 thorpej }
235 1.1 chopps
236 1.14.10.2 thorpej /*
237 1.14.10.2 thorpej * Glue functions.
238 1.14.10.2 thorpej */
239 1.1 chopps
240 1.14.10.2 thorpej u_char
241 1.14.10.2 thorpej flsc_read_reg(sc, reg)
242 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
243 1.14.10.2 thorpej int reg;
244 1.14.10.2 thorpej {
245 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
246 1.1 chopps
247 1.14.10.2 thorpej return fsc->sc_reg[reg * 4];
248 1.1 chopps }
249 1.1 chopps
250 1.14.10.2 thorpej void
251 1.14.10.2 thorpej flsc_write_reg(sc, reg, val)
252 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
253 1.14.10.2 thorpej int reg;
254 1.14.10.2 thorpej u_char val;
255 1.1 chopps {
256 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
257 1.14.10.2 thorpej struct ncr53c9x_tinfo *ti;
258 1.14.10.2 thorpej u_char v = val;
259 1.14.10.2 thorpej
260 1.14.10.2 thorpej if (fsc->sc_piomode && reg == NCR_CMD &&
261 1.14.10.2 thorpej v == (NCRCMD_TRANS|NCRCMD_DMA)) {
262 1.14.10.2 thorpej v = NCRCMD_TRANS;
263 1.14.10.2 thorpej }
264 1.14.10.2 thorpej /*
265 1.14.10.2 thorpej * Can't do synchronous transfers in SCSI_POLL mode:
266 1.14.10.2 thorpej * If starting SCSI_POLL command, clear defer sync negotiation
267 1.14.10.2 thorpej * by clearing the T_NEGOTIATE flag. If starting SCSI_POLL and
268 1.14.10.2 thorpej * the device is currently running synchronous, force another
269 1.14.10.2 thorpej * T_NEGOTIATE with 0 offset.
270 1.14.10.2 thorpej */
271 1.14.10.2 thorpej if (reg == NCR_SELID) {
272 1.14.10.2 thorpej ti = &sc->sc_tinfo[
273 1.14.10.2 thorpej sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
274 1.14.10.2 thorpej if (sc->sc_nexus->xs->flags & SCSI_POLL) {
275 1.14.10.2 thorpej if (ti->flags & T_SYNCMODE) {
276 1.14.10.2 thorpej ti->flags ^= T_SYNCMODE | T_NEGOTIATE;
277 1.14.10.2 thorpej } else if (ti->flags & T_NEGOTIATE) {
278 1.14.10.2 thorpej ti->flags ^= T_NEGOTIATE | T_SYNCHOFF;
279 1.14.10.2 thorpej /* save T_NEGOTIATE in private flags? */
280 1.1 chopps }
281 1.14.10.2 thorpej } else {
282 1.14.10.2 thorpej /*
283 1.14.10.2 thorpej * If we haven't attempted sync negotiation yet,
284 1.14.10.2 thorpej * do it now.
285 1.14.10.2 thorpej */
286 1.14.10.2 thorpej if ((ti->flags & (T_SYNCMODE | T_SYNCHOFF)) ==
287 1.14.10.2 thorpej T_SYNCHOFF &&
288 1.14.10.2 thorpej sc->sc_minsync != 0) /* XXX */
289 1.14.10.2 thorpej ti->flags ^= T_NEGOTIATE | T_SYNCHOFF;
290 1.14.10.2 thorpej }
291 1.1 chopps }
292 1.14.10.2 thorpej if (reg == NCR_CMD && v == NCRCMD_SETATN &&
293 1.14.10.2 thorpej sc->sc_flags & NCR_SYNCHNEGO &&
294 1.14.10.2 thorpej sc->sc_nexus->xs->flags & SCSI_POLL) {
295 1.14.10.2 thorpej ti = &sc->sc_tinfo[
296 1.14.10.2 thorpej sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
297 1.14.10.2 thorpej ti->offset = 0;
298 1.14.10.2 thorpej }
299 1.14.10.2 thorpej fsc->sc_reg[reg * 4] = v;
300 1.1 chopps }
301 1.1 chopps
302 1.14.10.2 thorpej int
303 1.14.10.2 thorpej flsc_dma_isintr(sc)
304 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
305 1.1 chopps {
306 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
307 1.14.10.2 thorpej unsigned hardbits;
308 1.1 chopps
309 1.14.10.2 thorpej hardbits = fsc->sc_reg[0x40];
310 1.14.10.2 thorpej if (hardbits & FLSC_HB_IACT)
311 1.14.10.2 thorpej return (fsc->sc_csr = 0);
312 1.14.10.2 thorpej
313 1.14.10.2 thorpej if (sc->sc_state == NCR_CONNECTED || sc->sc_state == NCR_SELECTING)
314 1.14.10.2 thorpej fsc->sc_portbits |= FLSC_PB_LED;
315 1.14.10.2 thorpej else
316 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_LED;
317 1.14.10.2 thorpej
318 1.14.10.2 thorpej if ((hardbits & FLSC_HB_CREQ) && !(hardbits & FLSC_HB_MINT) &&
319 1.14.10.2 thorpej fsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) {
320 1.14.10.2 thorpej return 1;
321 1.14.10.2 thorpej }
322 1.14.10.2 thorpej /* Do I still need this? */
323 1.14.10.2 thorpej if (fsc->sc_piomode && fsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT &&
324 1.14.10.2 thorpej !(hardbits & FLSC_HB_MINT))
325 1.14.10.2 thorpej return 1;
326 1.14.10.2 thorpej
327 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits & ~FLSC_PB_INT_BITS;
328 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
329 1.14.10.2 thorpej return 0;
330 1.1 chopps }
331 1.1 chopps
332 1.1 chopps void
333 1.14.10.2 thorpej flsc_clear_latched_intr(sc)
334 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
335 1.1 chopps {
336 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
337 1.14.10.2 thorpej
338 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits & ~FLSC_PB_INT_BITS;
339 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
340 1.1 chopps }
341 1.1 chopps
342 1.1 chopps void
343 1.14.10.2 thorpej flsc_dma_reset(sc)
344 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
345 1.1 chopps {
346 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
347 1.14.10.2 thorpej struct ncr53c9x_tinfo *ti;
348 1.1 chopps
349 1.14.10.2 thorpej if (sc->sc_nexus)
350 1.14.10.2 thorpej ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
351 1.14.10.2 thorpej else
352 1.14.10.2 thorpej ti = &sc->sc_tinfo[1]; /* XXX */
353 1.14.10.2 thorpej if (fsc->sc_active) {
354 1.14.10.2 thorpej printf("dmaaddr %p dmasize %d stat %x flags %x off %d per %d ff %x",
355 1.14.10.2 thorpej *fsc->sc_dmaaddr, fsc->sc_dmasize, fsc->sc_reg[NCR_STAT * 4],
356 1.14.10.2 thorpej ti->flags, ti->offset, ti->period, fsc->sc_reg[NCR_FFLAG * 4]);
357 1.14.10.2 thorpej printf(" intr %x\n", fsc->sc_reg[NCR_INTR * 4]);
358 1.14.10.2 thorpej #ifdef DDB
359 1.14.10.2 thorpej Debugger();
360 1.14.10.2 thorpej #endif
361 1.14.10.2 thorpej }
362 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
363 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
364 1.14.10.2 thorpej fsc->sc_reg[0x80] = 0;
365 1.14.10.2 thorpej *((u_long *)fsc->sc_dmabase) = 0;
366 1.14.10.2 thorpej fsc->sc_active = 0;
367 1.14.10.2 thorpej fsc->sc_piomode = 0;
368 1.1 chopps }
369 1.1 chopps
370 1.1 chopps int
371 1.14.10.2 thorpej flsc_dma_intr(sc)
372 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
373 1.14.10.2 thorpej {
374 1.14.10.2 thorpej register struct flsc_softc *fsc = (struct flsc_softc *)sc;
375 1.14.10.2 thorpej register u_char *p;
376 1.14.10.2 thorpej volatile u_char *cmdreg, *intrreg, *statreg, *fiforeg;
377 1.14.10.2 thorpej register u_int flscphase, flscstat, flscintr;
378 1.14.10.2 thorpej register int cnt;
379 1.14.10.2 thorpej
380 1.14.10.2 thorpej NCR_DMA(("flsc_dma_intr: pio %d cnt %d int %x stat %x fifo %d ",
381 1.14.10.2 thorpej fsc->sc_piomode, fsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
382 1.14.10.2 thorpej fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
383 1.14.10.2 thorpej if (!(fsc->sc_reg[0x40] & FLSC_HB_CREQ))
384 1.14.10.2 thorpej printf("flsc_dma_intr: csr %x stat %x intr %x\n", fsc->sc_csr,
385 1.14.10.2 thorpej sc->sc_espstat, sc->sc_espintr);
386 1.14.10.2 thorpej if (fsc->sc_active == 0) {
387 1.14.10.2 thorpej printf("flsc_intr--inactive DMA\n");
388 1.14.10.2 thorpej return -1;
389 1.14.10.2 thorpej }
390 1.1 chopps
391 1.14.10.2 thorpej /* if DMA transfer, update sc_dmaaddr and sc_pdmalen, else PIO xfer */
392 1.14.10.2 thorpej if (fsc->sc_piomode == 0) {
393 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
394 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
395 1.14.10.2 thorpej fsc->sc_reg[0x80] = 0;
396 1.14.10.2 thorpej *((u_long *)fsc->sc_dmabase) = 0;
397 1.14.10.2 thorpej cnt = fsc->sc_reg[NCR_TCL * 4];
398 1.14.10.2 thorpej cnt += fsc->sc_reg[NCR_TCM * 4] << 8;
399 1.14.10.2 thorpej cnt += fsc->sc_reg[NCR_TCH * 4] << 16;
400 1.14.10.2 thorpej if (!fsc->sc_datain) {
401 1.14.10.2 thorpej cnt += fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
402 1.14.10.2 thorpej fsc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
403 1.14.10.2 thorpej }
404 1.14.10.2 thorpej cnt = fsc->sc_dmasize - cnt; /* number of bytes transferred */
405 1.14.10.2 thorpej NCR_DMA(("DMA xferred %d\n", cnt));
406 1.14.10.2 thorpej if (fsc->sc_xfr_align) {
407 1.14.10.2 thorpej int i;
408 1.14.10.2 thorpej for (i = 0; i < cnt; ++i)
409 1.14.10.2 thorpej (*fsc->sc_dmaaddr)[i] = fsc->sc_alignbuf[i];
410 1.14.10.2 thorpej fsc->sc_xfr_align = 0;
411 1.14.10.2 thorpej }
412 1.14.10.2 thorpej *fsc->sc_dmaaddr += cnt;
413 1.14.10.2 thorpej *fsc->sc_pdmalen -= cnt;
414 1.14.10.2 thorpej fsc->sc_active = 0;
415 1.14.10.2 thorpej return 0;
416 1.14.10.2 thorpej }
417 1.1 chopps
418 1.14.10.2 thorpej if ((sc->sc_espintr & NCRINTR_BS) == 0) {
419 1.14.10.2 thorpej fsc->sc_active = 0;
420 1.14.10.2 thorpej fsc->sc_piomode = 0;
421 1.14.10.2 thorpej NCR_DMA(("no NCRINTR_BS\n"));
422 1.14.10.2 thorpej return 0;
423 1.1 chopps }
424 1.1 chopps
425 1.14.10.2 thorpej cnt = fsc->sc_dmasize;
426 1.14.10.2 thorpej #if 0
427 1.14.10.2 thorpej if (cnt == 0) {
428 1.14.10.2 thorpej printf("data interrupt, but no count left.");
429 1.14.10.2 thorpej }
430 1.14.10.2 thorpej #endif
431 1.1 chopps
432 1.14.10.2 thorpej p = *fsc->sc_dmaaddr;
433 1.14.10.2 thorpej flscphase = sc->sc_phase;
434 1.14.10.2 thorpej flscstat = (u_int) sc->sc_espstat;
435 1.14.10.2 thorpej flscintr = (u_int) sc->sc_espintr;
436 1.14.10.2 thorpej cmdreg = fsc->sc_reg + NCR_CMD * 4;
437 1.14.10.2 thorpej fiforeg = fsc->sc_reg + NCR_FIFO * 4;
438 1.14.10.2 thorpej statreg = fsc->sc_reg + NCR_STAT * 4;
439 1.14.10.2 thorpej intrreg = fsc->sc_reg + NCR_INTR * 4;
440 1.14.10.2 thorpej NCR_DMA(("PIO %d datain %d phase %d stat %x intr %x\n",
441 1.14.10.2 thorpej cnt, fsc->sc_datain, flscphase, flscstat, flscintr));
442 1.14.10.2 thorpej do {
443 1.14.10.2 thorpej if (fsc->sc_datain) {
444 1.14.10.2 thorpej *p++ = *fiforeg;
445 1.14.10.2 thorpej cnt--;
446 1.14.10.2 thorpej if (flscphase == DATA_IN_PHASE) {
447 1.14.10.2 thorpej *cmdreg = NCRCMD_TRANS;
448 1.14.10.2 thorpej } else {
449 1.14.10.2 thorpej fsc->sc_active = 0;
450 1.14.10.2 thorpej }
451 1.14.10.2 thorpej } else {
452 1.14.10.2 thorpej NCR_DMA(("flsc_dma_intr: PIO out- phase %d cnt %d active %d\n", flscphase, cnt,
453 1.14.10.2 thorpej fsc->sc_active));
454 1.14.10.2 thorpej if ( (flscphase == DATA_OUT_PHASE)
455 1.14.10.2 thorpej || (flscphase == MESSAGE_OUT_PHASE)) {
456 1.14.10.2 thorpej int n;
457 1.14.10.2 thorpej n = 16 - (fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF);
458 1.14.10.2 thorpej if (n > cnt)
459 1.14.10.2 thorpej n = cnt;
460 1.14.10.2 thorpej cnt -= n;
461 1.14.10.2 thorpej while (n-- > 0)
462 1.14.10.2 thorpej *fiforeg = *p++;
463 1.14.10.2 thorpej *cmdreg = NCRCMD_TRANS;
464 1.14.10.2 thorpej } else {
465 1.14.10.2 thorpej fsc->sc_active = 0;
466 1.14.10.2 thorpej }
467 1.14.10.2 thorpej }
468 1.1 chopps
469 1.14.10.2 thorpej if (fsc->sc_active && cnt) {
470 1.14.10.2 thorpej while (!(*statreg & 0x80));
471 1.14.10.2 thorpej flscstat = *statreg;
472 1.14.10.2 thorpej flscintr = *intrreg;
473 1.14.10.2 thorpej flscphase = (flscintr & NCRINTR_DIS)
474 1.14.10.2 thorpej ? /* Disconnected */ BUSFREE_PHASE
475 1.14.10.2 thorpej : flscstat & PHASE_MASK;
476 1.14.10.2 thorpej }
477 1.14.10.2 thorpej } while (cnt && fsc->sc_active && (flscintr & NCRINTR_BS));
478 1.14.10.2 thorpej #if 1
479 1.14.10.2 thorpej if (fsc->sc_dmasize < 8 && cnt)
480 1.14.10.2 thorpej printf("flsc_dma_intr: short transfer: dmasize %d cnt %d\n",
481 1.14.10.2 thorpej fsc->sc_dmasize, cnt);
482 1.14.10.2 thorpej #endif
483 1.14.10.2 thorpej NCR_DMA(("flsc_dma_intr: PIO transfer [%d], %d->%d phase %d stat %x intr %x\n",
484 1.14.10.2 thorpej *fsc->sc_pdmalen, fsc->sc_dmasize, cnt, flscphase, flscstat, flscintr));
485 1.14.10.2 thorpej sc->sc_phase = flscphase;
486 1.14.10.2 thorpej sc->sc_espstat = (u_char) flscstat;
487 1.14.10.2 thorpej sc->sc_espintr = (u_char) flscintr;
488 1.14.10.2 thorpej *fsc->sc_dmaaddr = p;
489 1.14.10.2 thorpej *fsc->sc_pdmalen -= fsc->sc_dmasize - cnt;
490 1.14.10.2 thorpej fsc->sc_dmasize = cnt;
491 1.14.10.2 thorpej
492 1.14.10.2 thorpej if (*fsc->sc_pdmalen == 0) {
493 1.14.10.2 thorpej sc->sc_espstat |= NCRSTAT_TC;
494 1.14.10.2 thorpej fsc->sc_piomode = 0;
495 1.14.10.2 thorpej }
496 1.14.10.2 thorpej return 0;
497 1.1 chopps }
498 1.1 chopps
499 1.1 chopps int
500 1.14.10.2 thorpej flsc_dma_setup(sc, addr, len, datain, dmasize)
501 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
502 1.14.10.2 thorpej caddr_t *addr;
503 1.14.10.2 thorpej size_t *len;
504 1.14.10.2 thorpej int datain;
505 1.14.10.2 thorpej size_t *dmasize;
506 1.14.10.2 thorpej {
507 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
508 1.14.10.2 thorpej vm_offset_t pa;
509 1.14.10.2 thorpej u_char *ptr;
510 1.14.10.2 thorpej size_t xfer;
511 1.14.10.2 thorpej
512 1.14.10.2 thorpej fsc->sc_dmaaddr = addr;
513 1.14.10.2 thorpej fsc->sc_pdmalen = len;
514 1.14.10.2 thorpej fsc->sc_datain = datain;
515 1.14.10.2 thorpej fsc->sc_dmasize = *dmasize;
516 1.14.10.2 thorpej if (sc->sc_nexus->xs->flags & SCSI_POLL) {
517 1.14.10.2 thorpej /* polling mode, use PIO */
518 1.14.10.2 thorpej *dmasize = fsc->sc_dmasize;
519 1.14.10.2 thorpej NCR_DMA(("pfsc_dma_setup: PIO %p/%d [%d]\n", *addr,
520 1.14.10.2 thorpej fsc->sc_dmasize, *len));
521 1.14.10.2 thorpej fsc->sc_piomode = 1;
522 1.14.10.2 thorpej if (datain == 0) {
523 1.14.10.2 thorpej int n;
524 1.14.10.2 thorpej n = fsc->sc_dmasize;
525 1.14.10.2 thorpej if (n > 16)
526 1.14.10.2 thorpej n = 16;
527 1.14.10.2 thorpej while (n-- > 0) {
528 1.14.10.2 thorpej fsc->sc_reg[NCR_FIFO * 4] = **fsc->sc_dmaaddr;
529 1.14.10.2 thorpej (*fsc->sc_pdmalen)--;
530 1.14.10.2 thorpej (*fsc->sc_dmaaddr)++;
531 1.14.10.2 thorpej --fsc->sc_dmasize;
532 1.14.10.2 thorpej }
533 1.14.10.2 thorpej }
534 1.14.10.2 thorpej return 0;
535 1.14.10.2 thorpej }
536 1.14.10.2 thorpej /*
537 1.14.10.2 thorpej * DMA can be nasty for high-speed serial input, so limit the
538 1.14.10.2 thorpej * size of this DMA operation if the serial port is running at
539 1.14.10.2 thorpej * a high speed (higher than 19200 for now - should be adjusted
540 1.14.10.2 thorpej * based on cpu type and speed?).
541 1.14.10.2 thorpej * XXX - add serial speed check XXX
542 1.14.10.2 thorpej */
543 1.14.10.2 thorpej if (ser_open_speed > 19200 && flsc_max_dma != 0 &&
544 1.14.10.2 thorpej fsc->sc_dmasize > flsc_max_dma)
545 1.14.10.2 thorpej fsc->sc_dmasize = flsc_max_dma;
546 1.14.10.2 thorpej ptr = *addr; /* Kernel virtual address */
547 1.14.10.2 thorpej pa = kvtop(ptr); /* Physical address of DMA */
548 1.14.10.2 thorpej xfer = min(fsc->sc_dmasize, NBPG - (pa & (NBPG - 1)));
549 1.14.10.2 thorpej fsc->sc_xfr_align = 0;
550 1.14.10.2 thorpej fsc->sc_piomode = 0;
551 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
552 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
553 1.14.10.2 thorpej fsc->sc_reg[0x80] = 0;
554 1.14.10.2 thorpej *((u_long *)fsc->sc_dmabase) = 0;
555 1.14.10.2 thorpej
556 1.14.10.2 thorpej /*
557 1.14.10.2 thorpej * If output and length < 16, copy to fifo
558 1.14.10.2 thorpej */
559 1.14.10.2 thorpej if (datain == 0 && fsc->sc_dmasize < 16) {
560 1.14.10.2 thorpej int n;
561 1.14.10.2 thorpej for (n = 0; n < fsc->sc_dmasize; ++n)
562 1.14.10.2 thorpej fsc->sc_reg[NCR_FIFO * 4] = *ptr++;
563 1.14.10.2 thorpej NCR_DMA(("flsc_dma_setup: %d bytes written to fifo\n", n));
564 1.14.10.2 thorpej fsc->sc_piomode = 1;
565 1.14.10.2 thorpej fsc->sc_active = 1;
566 1.14.10.2 thorpej *fsc->sc_pdmalen -= fsc->sc_dmasize;
567 1.14.10.2 thorpej *fsc->sc_dmaaddr += fsc->sc_dmasize;
568 1.14.10.2 thorpej *dmasize = fsc->sc_dmasize;
569 1.14.10.2 thorpej fsc->sc_dmasize = 0;
570 1.14.10.2 thorpej return 0; /* All done */
571 1.14.10.2 thorpej }
572 1.14.10.2 thorpej /*
573 1.14.10.2 thorpej * If output and unaligned, copy unaligned data to fifo
574 1.14.10.2 thorpej */
575 1.14.10.2 thorpej else if (datain == 0 && (int)ptr & 3) {
576 1.14.10.2 thorpej int n = 4 - ((int)ptr & 3);
577 1.14.10.2 thorpej NCR_DMA(("flsc_dma_setup: align %d bytes written to fifo\n", n));
578 1.14.10.2 thorpej pa += n;
579 1.14.10.2 thorpej xfer -= n;
580 1.14.10.2 thorpej while (n--)
581 1.14.10.2 thorpej fsc->sc_reg[NCR_FIFO * 4] = *ptr++;
582 1.14.10.2 thorpej }
583 1.14.10.2 thorpej /*
584 1.14.10.2 thorpej * If unaligned address, read unaligned bytes into alignment buffer
585 1.14.10.2 thorpej */
586 1.14.10.2 thorpej else if ((int)ptr & 3 || xfer & 3) {
587 1.14.10.2 thorpej pa = kvtop((caddr_t)&fsc->sc_alignbuf);
588 1.14.10.2 thorpej xfer = fsc->sc_dmasize = min(xfer, sizeof (fsc->sc_alignbuf));
589 1.14.10.2 thorpej NCR_DMA(("flsc_dma_setup: align read by %d bytes\n", xfer));
590 1.14.10.2 thorpej fsc->sc_xfr_align = 1;
591 1.14.10.2 thorpej }
592 1.14.10.2 thorpej /*
593 1.14.10.2 thorpej * If length smaller than longword, read into alignment buffer
594 1.14.10.2 thorpej * XXX doesn't work for 1 or 2 bytes !!!!
595 1.14.10.2 thorpej */
596 1.14.10.2 thorpej else if (fsc->sc_dmasize < 4) {
597 1.14.10.2 thorpej NCR_DMA(("flsc_dma_setup: read remaining %d bytes\n",
598 1.14.10.2 thorpej fsc->sc_dmasize));
599 1.14.10.2 thorpej pa = kvtop((caddr_t)&fsc->sc_alignbuf);
600 1.14.10.2 thorpej fsc->sc_xfr_align = 1;
601 1.14.10.2 thorpej }
602 1.14.10.2 thorpej /*
603 1.14.10.2 thorpej * Finally, limit transfer length to multiple of 4 bytes.
604 1.14.10.2 thorpej */
605 1.14.10.2 thorpej else {
606 1.14.10.2 thorpej fsc->sc_dmasize &= -4;
607 1.14.10.2 thorpej xfer &= -4;
608 1.14.10.2 thorpej }
609 1.1 chopps
610 1.14.10.2 thorpej while (xfer < fsc->sc_dmasize) {
611 1.14.10.2 thorpej if ((pa + xfer) != kvtop(*addr + xfer))
612 1.14.10.2 thorpej break;
613 1.14.10.2 thorpej if ((fsc->sc_dmasize - xfer) < NBPG)
614 1.14.10.2 thorpej xfer = fsc->sc_dmasize;
615 1.14.10.2 thorpej else
616 1.14.10.2 thorpej xfer += NBPG;
617 1.14.10.2 thorpej }
618 1.1 chopps
619 1.14.10.2 thorpej fsc->sc_dmasize = xfer;
620 1.14.10.2 thorpej *dmasize = fsc->sc_dmasize;
621 1.14.10.2 thorpej fsc->sc_pa = pa;
622 1.7 is #if defined(M68040) || defined(M68060)
623 1.14.10.2 thorpej if (mmutype == MMU_68040) {
624 1.14.10.2 thorpej if (fsc->sc_xfr_align) {
625 1.14.10.2 thorpej int n;
626 1.14.10.2 thorpej for (n = 0; n < sizeof (fsc->sc_alignbuf); ++n)
627 1.14.10.2 thorpej fsc->sc_alignbuf[n] = n | 0x80;
628 1.14.10.2 thorpej dma_cachectl(fsc->sc_alignbuf,
629 1.14.10.2 thorpej sizeof(fsc->sc_alignbuf));
630 1.1 chopps }
631 1.14.10.2 thorpej else
632 1.14.10.2 thorpej dma_cachectl(*fsc->sc_dmaaddr, fsc->sc_dmasize);
633 1.1 chopps }
634 1.14.10.2 thorpej #endif
635 1.14.10.2 thorpej fsc->sc_reg[0x80] = 0;
636 1.14.10.2 thorpej *((u_long *)(fsc->sc_dmabase + (pa & 0x00fffffc))) = pa;
637 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
638 1.14.10.2 thorpej fsc->sc_portbits |= FLSC_PB_ENABLE_DMA |
639 1.14.10.2 thorpej (fsc->sc_datain ? FLSC_PB_DMA_READ : FLSC_PB_DMA_WRITE);
640 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
641 1.14.10.2 thorpej NCR_DMA(("flsc_dma_setup: DMA %p->%lx/%d [%d]\n",
642 1.14.10.2 thorpej ptr, pa, fsc->sc_dmasize, *len));
643 1.14.10.2 thorpej fsc->sc_active = 1;
644 1.14.10.2 thorpej return 0;
645 1.1 chopps }
646 1.1 chopps
647 1.1 chopps void
648 1.14.10.2 thorpej flsc_dma_go(sc)
649 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
650 1.14.10.2 thorpej {
651 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
652 1.14.10.2 thorpej
653 1.14.10.2 thorpej NCR_DMA(("flsc_dma_go: datain %d size %d\n", fsc->sc_datain,
654 1.14.10.2 thorpej fsc->sc_dmasize));
655 1.14.10.2 thorpej if (sc->sc_nexus->xs->flags & SCSI_POLL) {
656 1.14.10.2 thorpej fsc->sc_active = 1;
657 1.14.10.2 thorpej return;
658 1.14.10.2 thorpej } else if (fsc->sc_piomode == 0) {
659 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
660 1.14.10.2 thorpej fsc->sc_portbits |= FLSC_PB_ENABLE_DMA |
661 1.14.10.2 thorpej (fsc->sc_datain ? FLSC_PB_DMA_READ : FLSC_PB_DMA_WRITE);
662 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
663 1.1 chopps }
664 1.14.10.2 thorpej }
665 1.14.10.2 thorpej
666 1.14.10.2 thorpej void
667 1.14.10.2 thorpej flsc_dma_stop(sc)
668 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
669 1.14.10.2 thorpej {
670 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
671 1.14.10.2 thorpej
672 1.14.10.2 thorpej fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
673 1.14.10.2 thorpej fsc->sc_reg[0x40] = fsc->sc_portbits;
674 1.14.10.2 thorpej
675 1.14.10.2 thorpej fsc->sc_reg[0x80] = 0;
676 1.14.10.2 thorpej *((u_long *)fsc->sc_dmabase) = 0;
677 1.14.10.2 thorpej fsc->sc_piomode = 0;
678 1.14.10.2 thorpej }
679 1.14.10.2 thorpej
680 1.14.10.2 thorpej int
681 1.14.10.2 thorpej flsc_dma_isactive(sc)
682 1.14.10.2 thorpej struct ncr53c9x_softc *sc;
683 1.14.10.2 thorpej {
684 1.14.10.2 thorpej struct flsc_softc *fsc = (struct flsc_softc *)sc;
685 1.14.10.2 thorpej
686 1.14.10.2 thorpej return fsc->sc_active;
687 1.1 chopps }
688