bzsc.c revision 1.8 1 /* $NetBSD: bzsc.c,v 1.8 1996/06/10 16:11:20 is Exp $ */
2
3 /*
4 * Copyright (c) 1995 Daniel Widenfalk
5 * Copyright (c) 1994 Christian E. Hopps
6 * Copyright (c) 1982, 1990 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)dma.c
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <scsi/scsi_all.h>
45 #include <scsi/scsiconf.h>
46 #include <vm/vm.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_page.h>
49 #include <machine/pmap.h>
50 #include <amiga/amiga/custom.h>
51 #include <amiga/amiga/cc.h>
52 #include <amiga/amiga/device.h>
53 #include <amiga/amiga/isr.h>
54 #include <amiga/dev/sfasreg.h>
55 #include <amiga/dev/sfasvar.h>
56 #include <amiga/dev/zbusvar.h>
57 #include <amiga/dev/bzscreg.h>
58 #include <amiga/dev/bzscvar.h>
59
60 int bzscprint __P((void *auxp, char *));
61 void bzscattach __P((struct device *, struct device *, void *));
62 int bzscmatch __P((struct device *, void *, void *));
63
64 struct scsi_adapter bzsc_scsiswitch = {
65 sfas_scsicmd,
66 sfas_minphys,
67 0, /* no lun support */
68 0, /* no lun support */
69 };
70
71 struct scsi_device bzsc_scsidev = {
72 NULL, /* use default error handler */
73 NULL, /* do not have a start functio */
74 NULL, /* have no async handler */
75 NULL, /* Use default done routine */
76 };
77
78 struct cfattach bzsc_ca = {
79 sizeof(struct bzsc_softc), bzscmatch, bzscattach
80 };
81
82 struct cfdriver bzsc_cd = {
83 NULL, "bzsc", DV_DULL, NULL, 0
84 };
85
86 int bzsc_intr __P((void *));
87 void bzsc_set_dma_adr __P((struct sfas_softc *sc, vm_offset_t ptr, int mode));
88 void bzsc_set_dma_tc __P((struct sfas_softc *sc, unsigned int len));
89 int bzsc_setup_dma __P((struct sfas_softc *sc, vm_offset_t ptr, int len,
90 int mode));
91 int bzsc_build_dma_chain __P((struct sfas_softc *sc,
92 struct sfas_dma_chain *chain, void *p, int l));
93 int bzsc_need_bump __P((struct sfas_softc *sc, vm_offset_t ptr, int len));
94 void bzsc_led_dummy __P((struct sfas_softc *sc, int mode));
95
96 /*
97 * if we are an Advanced Systems & Software FastlaneZ3
98 */
99 int
100 bzscmatch(pdp, match, auxp)
101 struct device *pdp;
102 void *match, *auxp;
103 {
104 struct zbus_args *zap;
105
106 if (!is_a1200())
107 return(0);
108
109 zap = auxp;
110 if (zap->manid == 0x2140 && zap->prodid == 11)
111 return(1);
112
113 return(0);
114 }
115
116 void
117 bzscattach(pdp, dp, auxp)
118 struct device *pdp;
119 struct device *dp;
120 void *auxp;
121 {
122 struct bzsc_softc *sc;
123 struct zbus_args *zap;
124 bzsc_regmap_p rp;
125 vu_char *fas;
126
127 zap = auxp;
128 fas = (vu_char *)(((char *)zap->va)+0x10000);
129
130 sc = (struct bzsc_softc *)dp;
131 rp = &sc->sc_regmap;
132
133 rp->FAS216.sfas_tc_low = &fas[0x00];
134 rp->FAS216.sfas_tc_mid = &fas[0x02];
135 rp->FAS216.sfas_fifo = &fas[0x04];
136 rp->FAS216.sfas_command = &fas[0x06];
137 rp->FAS216.sfas_dest_id = &fas[0x08];
138 rp->FAS216.sfas_timeout = &fas[0x0A];
139 rp->FAS216.sfas_syncper = &fas[0x0C];
140 rp->FAS216.sfas_syncoff = &fas[0x0E];
141 rp->FAS216.sfas_config1 = &fas[0x10];
142 rp->FAS216.sfas_clkconv = &fas[0x12];
143 rp->FAS216.sfas_test = &fas[0x14];
144 rp->FAS216.sfas_config2 = &fas[0x16];
145 rp->FAS216.sfas_config3 = &fas[0x18];
146 rp->FAS216.sfas_tc_high = &fas[0x1C];
147 rp->FAS216.sfas_fifo_bot = &fas[0x1E];
148 rp->cclkaddr = &fas[0x21];
149 rp->epowaddr = &fas[0x31];
150
151 sc->sc_softc.sc_fas = (sfas_regmap_p)rp;
152 sc->sc_softc.sc_spec = 0;
153
154 sc->sc_softc.sc_led = bzsc_led_dummy;
155
156 sc->sc_softc.sc_setup_dma = bzsc_setup_dma;
157 sc->sc_softc.sc_build_dma_chain = bzsc_build_dma_chain;
158 sc->sc_softc.sc_need_bump = bzsc_need_bump;
159
160 sc->sc_softc.sc_clock_freq = 40; /* BlizzardII 1230 runs at 40MHz? */
161 sc->sc_softc.sc_timeout = 250; /* Set default timeout to 250ms */
162 sc->sc_softc.sc_config_flags = 0;
163 sc->sc_softc.sc_host_id = 7;
164
165 sc->sc_softc.sc_bump_sz = NBPG;
166 sc->sc_softc.sc_bump_pa = 0x0;
167
168 sfasinitialize((struct sfas_softc *)sc);
169
170 sc->sc_softc.sc_link.adapter_softc = sc;
171 sc->sc_softc.sc_link.adapter_target = sc->sc_softc.sc_host_id;
172 sc->sc_softc.sc_link.adapter = &bzsc_scsiswitch;
173 sc->sc_softc.sc_link.device = &bzsc_scsidev;
174 sc->sc_softc.sc_link.openings = 1;
175
176 printf("\n");
177
178 sc->sc_softc.sc_isr.isr_intr = bzsc_intr;
179 sc->sc_softc.sc_isr.isr_arg = &sc->sc_softc;
180 sc->sc_softc.sc_isr.isr_ipl = 2;
181 add_isr(&sc->sc_softc.sc_isr);
182
183 /* attach all scsi units on us */
184 config_found(dp, &sc->sc_softc.sc_link, bzscprint);
185 }
186
187 /* print diag if pnp is NULL else just extra */
188 int
189 bzscprint(auxp, pnp)
190 void *auxp;
191 char *pnp;
192 {
193 if (pnp == NULL)
194 return(UNCONF);
195
196 return(QUIET);
197 }
198
199 int
200 bzsc_intr(arg)
201 void *arg;
202 {
203 struct sfas_softc *dev = arg;
204 bzsc_regmap_p rp;
205 int quickints;
206
207 rp = (bzsc_regmap_p)dev->sc_fas;
208
209 if (!(*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING))
210 return(0);
211
212 quickints = 16;
213 do {
214 dev->sc_status = *rp->FAS216.sfas_status;
215 dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
216
217 if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
218 dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
219 dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
220 }
221 sfasintr(dev);
222 } while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) &&
223 --quickints);
224
225 return(1);
226 }
227
228 /* --------- */
229 void
230 bzsc_set_dma_adr(sc, ptr, mode)
231 struct sfas_softc *sc;
232 vm_offset_t ptr;
233 int mode;
234 {
235 bzsc_regmap_p rp;
236 unsigned long p;
237
238 rp = (bzsc_regmap_p)sc->sc_fas;
239
240 p = ((unsigned long)ptr)>>1;
241
242 if (mode == SFAS_DMA_WRITE)
243 p |= BZSC_DMA_WRITE;
244 else
245 p |= BZSC_DMA_READ;
246
247 *rp->epowaddr = (u_char)(p>>24) & 0xFF;
248 *rp->cclkaddr = (u_char)(p>>16) & 0xFF;
249 *rp->cclkaddr = (u_char)(p>> 8) & 0xFF;
250 *rp->cclkaddr = (u_char)(p ) & 0xFF;
251 }
252
253 /* Set DMA transfer counter */
254 void
255 bzsc_set_dma_tc(sc, len)
256 struct sfas_softc *sc;
257 unsigned int len;
258 {
259 *sc->sc_fas->sfas_tc_low = len; len >>= 8;
260 *sc->sc_fas->sfas_tc_mid = len; len >>= 8;
261 *sc->sc_fas->sfas_tc_high = len;
262 }
263
264 /* Initialize DMA for transfer */
265 int
266 bzsc_setup_dma(sc, ptr, len, mode)
267 struct sfas_softc *sc;
268 vm_offset_t ptr;
269 int len;
270 int mode;
271 {
272 int retval;
273
274 retval = 0;
275
276 switch(mode) {
277 case SFAS_DMA_READ:
278 case SFAS_DMA_WRITE:
279 bzsc_set_dma_adr(sc, ptr, mode);
280 bzsc_set_dma_tc(sc, len);
281 break;
282 case SFAS_DMA_CLEAR:
283 default:
284 retval = (*sc->sc_fas->sfas_tc_high << 16) |
285 (*sc->sc_fas->sfas_tc_mid << 8) |
286 *sc->sc_fas->sfas_tc_low;
287
288 bzsc_set_dma_tc(sc, 0);
289 break;
290 }
291
292 return(retval);
293 }
294
295 /* Check if address and len is ok for DMA transfer */
296 int
297 bzsc_need_bump(sc, ptr, len)
298 struct sfas_softc *sc;
299 vm_offset_t ptr;
300 int len;
301 {
302 int p;
303
304 p = (int)ptr & 0x03;
305
306 if (p) {
307 p = 4-p;
308
309 if (len < 256)
310 p = len;
311 }
312
313 return(p);
314 }
315
316 /* Interrupt driven routines */
317 int
318 bzsc_build_dma_chain(sc, chain, p, l)
319 struct sfas_softc *sc;
320 struct sfas_dma_chain *chain;
321 void *p;
322 int l;
323 {
324 int n;
325
326 if (!l)
327 return(0);
328
329 #define set_link(n, p, l, f)\
330 do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
331
332 n = 0;
333
334 if (l < 512)
335 set_link(n, (vm_offset_t)p, l, SFAS_CHAIN_BUMP);
336 else if (
337 #if defined(M68040) || defined(M68060)
338 ((mmutype == MMU_68040) && ((vm_offset_t)p >= 0xFFFC0000)) &&
339 #endif
340 ((vm_offset_t)p >= 0xFF000000)) {
341 int len;
342
343 while(l) {
344 len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
345
346 set_link(n, (vm_offset_t)p, len, SFAS_CHAIN_BUMP);
347
348 p += len;
349 l -= len;
350 }
351 } else {
352 char *ptr;
353 vm_offset_t pa, lastpa;
354 int len, prelen, max_t;
355
356 ptr = p;
357 len = l;
358
359 pa = kvtop(ptr);
360 prelen = ((int)ptr & 0x03);
361
362 if (prelen) {
363 prelen = 4-prelen;
364 set_link(n, (vm_offset_t)ptr, prelen, SFAS_CHAIN_BUMP);
365 ptr += prelen;
366 len -= prelen;
367 }
368
369 lastpa = 0;
370 while(len > 3) {
371 pa = kvtop(ptr);
372 max_t = NBPG - (pa & PGOFSET);
373 if (max_t > len)
374 max_t = len;
375
376 max_t &= ~3;
377
378 if (lastpa == pa)
379 sc->sc_chain[n-1].len += max_t;
380 else
381 set_link(n, pa, max_t, SFAS_CHAIN_DMA);
382
383 lastpa = pa+max_t;
384
385 ptr += max_t;
386 len -= max_t;
387 }
388
389 if (len)
390 set_link(n, (vm_offset_t)ptr, len, SFAS_CHAIN_BUMP);
391 }
392
393 return(n);
394 }
395
396 /* Turn on led */
397 void bzsc_led_dummy(sc, mode)
398 struct sfas_softc *sc;
399 int mode;
400 {
401 }
402