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