ptsc.c revision 1.5 1 /* $NetBSD: ptsc.c,v 1.5 2002/09/27 20:29:17 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1995 Scott Stevens
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.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 the University of
21 * California, Berkeley and its contributors.
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.
25 *
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
36 * SUCH DAMAGE.
37 *
38 * @(#)ptsc.c
39 */
40
41 /*
42 * Power-tec SCSI-2 driver uses SFAS216 generic driver
43 *
44 * Thanks to Alsystems for loaning a development card and providing
45 * programming information.
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <dev/scsipi/scsi_all.h>
53 #include <dev/scsipi/scsipi_all.h>
54 #include <dev/scsipi/scsiconf.h>
55 #include <uvm/uvm_extern.h>
56 #include <machine/pmap.h>
57 #include <machine/io.h>
58 #include <machine/intr.h>
59 #include <machine/bootconfig.h>
60 #include <acorn32/podulebus/podulebus.h>
61 #include <acorn32/podulebus/sfasreg.h>
62 #include <acorn32/podulebus/sfasvar.h>
63 #include <acorn32/podulebus/ptscreg.h>
64 #include <acorn32/podulebus/ptscvar.h>
65 #include <dev/podulebus/podules.h>
66 #include <dev/podulebus/powerromreg.h>
67
68 void ptscattach __P((struct device *, struct device *, void *));
69 int ptscmatch __P((struct device *, struct cfdata *, void *));
70
71 const struct cfattach ptsc_ca = {
72 sizeof(struct ptsc_softc), ptscmatch, ptscattach
73 };
74
75 int ptsc_intr __P((void *arg));
76 int ptsc_setup_dma __P((struct sfas_softc *sc, void *ptr, int len,
77 int mode));
78 int ptsc_build_dma_chain __P((struct sfas_softc *sc,
79 struct sfas_dma_chain *chain, void *p, int l));
80 int ptsc_need_bump __P((struct sfas_softc *sc, void *ptr, int len));
81 void ptsc_led __P((struct sfas_softc *sc, int mode));
82
83 /*
84 * if we are a Power-tec SCSI-2 card
85 */
86 int
87 ptscmatch(pdp, cf, auxp)
88 struct device *pdp;
89 struct cfdata *cf;
90 void *auxp;
91 {
92 struct podule_attach_args *pa = (struct podule_attach_args *)auxp;
93
94 /* Look for the card */
95
96 /*
97 * All Power-tec cards effectively have PowerROMS. Note,
98 * though, that here, if we fail to initialise the loader, we
99 * assume this _is_ the right kind of card.
100 */
101 if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
102 (podulebus_initloader(pa) != 0 ||
103 podloader_callloader(pa, 0, 0) == PRID_POWERTEC))
104 return 1;
105
106 return 0;
107 }
108
109 void
110 ptscattach(pdp, dp, auxp)
111 struct device *pdp;
112 struct device *dp;
113 void *auxp;
114 {
115 struct ptsc_softc *sc = (struct ptsc_softc *)dp;
116 struct podule_attach_args *pa;
117 ptsc_regmap_p rp = &sc->sc_regmap;
118 vu_char *fas;
119
120 pa = (struct podule_attach_args *)auxp;
121
122 if (pa->pa_podule_number == -1)
123 panic("Podule has disappeared !");
124
125 sc->sc_specific.sc_podule_number = pa->pa_podule_number;
126 sc->sc_specific.sc_podule = pa->pa_podule;
127 sc->sc_specific.sc_iobase =
128 (vu_char *)sc->sc_specific.sc_podule->fast_base;
129
130 rp->chipreset = &sc->sc_specific.sc_iobase[PTSC_CONTROL_CHIPRESET];
131 rp->inten = &sc->sc_specific.sc_iobase[PTSC_CONTROL_INTEN];
132 rp->status = &sc->sc_specific.sc_iobase[PTSC_STATUS];
133 rp->term = &sc->sc_specific.sc_iobase[PTSC_CONTROL_TERM];
134 rp->led = &sc->sc_specific.sc_iobase[PTSC_CONTROL_LED];
135 fas = &sc->sc_specific.sc_iobase[PTSC_FASOFFSET_BASE];
136
137 rp->FAS216.sfas_tc_low = &fas[PTSC_FASOFFSET_TCL];
138 rp->FAS216.sfas_tc_mid = &fas[PTSC_FASOFFSET_TCM];
139 rp->FAS216.sfas_fifo = &fas[PTSC_FASOFFSET_FIFO];
140 rp->FAS216.sfas_command = &fas[PTSC_FASOFFSET_COMMAND];
141 rp->FAS216.sfas_dest_id = &fas[PTSC_FASOFFSET_DESTID];
142 rp->FAS216.sfas_timeout = &fas[PTSC_FASOFFSET_TIMEOUT];
143 rp->FAS216.sfas_syncper = &fas[PTSC_FASOFFSET_PERIOD];
144 rp->FAS216.sfas_syncoff = &fas[PTSC_FASOFFSET_OFFSET];
145 rp->FAS216.sfas_config1 = &fas[PTSC_FASOFFSET_CONFIG1];
146 rp->FAS216.sfas_clkconv = &fas[PTSC_FASOFFSET_CLOCKCONV];
147 rp->FAS216.sfas_test = &fas[PTSC_FASOFFSET_TEST];
148 rp->FAS216.sfas_config2 = &fas[PTSC_FASOFFSET_CONFIG2];
149 rp->FAS216.sfas_config3 = &fas[PTSC_FASOFFSET_CONFIG3];
150 rp->FAS216.sfas_tc_high = &fas[PTSC_FASOFFSET_TCH];
151 rp->FAS216.sfas_fifo_bot = &fas[PTSC_FASOFFSET_FIFOBOTTOM];
152
153 sc->sc_softc.sc_fas = (sfas_regmap_p)rp;
154 sc->sc_softc.sc_spec = &sc->sc_specific;
155
156 sc->sc_softc.sc_led = ptsc_led;
157
158 sc->sc_softc.sc_setup_dma = ptsc_setup_dma;
159 sc->sc_softc.sc_build_dma_chain = ptsc_build_dma_chain;
160 sc->sc_softc.sc_need_bump = ptsc_need_bump;
161
162 sc->sc_softc.sc_clock_freq = 40; /* Power-Tec runs at 8MHz */
163 sc->sc_softc.sc_timeout = 250; /* Set default timeout to 250ms */
164 sc->sc_softc.sc_config_flags = SFAS_NO_DMA /*| SFAS_NF_DEBUG*/;
165 sc->sc_softc.sc_host_id = 7; /* Should check the jumpers */
166
167 sc->sc_softc.sc_bump_sz = NBPG;
168 sc->sc_softc.sc_bump_pa = 0x0;
169
170 sfasinitialize((struct sfas_softc *)sc);
171
172 sc->sc_softc.sc_adapter.adapt_dev = &sc->sc_softc.sc_dev;
173 sc->sc_softc.sc_adapter.adapt_nchannels = 1;
174 sc->sc_softc.sc_adapter.adapt_openings = 7;
175 sc->sc_softc.sc_adapter.adapt_max_periph = 1;
176 sc->sc_softc.sc_adapter.adapt_ioctl = NULL;
177 sc->sc_softc.sc_adapter.adapt_minphys = sfas_minphys;
178 sc->sc_softc.sc_adapter.adapt_request = sfas_scsi_request;
179
180 sc->sc_softc.sc_channel.chan_adapter = &sc->sc_softc.sc_adapter;
181 sc->sc_softc.sc_channel.chan_bustype = &scsi_bustype;
182 sc->sc_softc.sc_channel.chan_channel = 0;
183 sc->sc_softc.sc_channel.chan_ntargets = 8;
184 sc->sc_softc.sc_channel.chan_nluns = 8;
185 sc->sc_softc.sc_channel.chan_id = sc->sc_softc.sc_host_id;
186
187 /* Provide an override for the host id */
188 (void)get_bootconf_option(boot_args, "ptsc.hostid",
189 BOOTOPT_TYPE_INT, &sc->sc_softc.sc_channel.chan_id);
190
191 printf(": host=%d", sc->sc_softc.sc_channel.chan_id);
192
193 /* initialise the card */
194 /* *rp->term = 0;*/
195 *rp->inten = (PTSC_POLL?0:1);
196 *rp->led = 0;
197
198 #if PTSC_POLL == 0
199 evcnt_attach_dynamic(&sc->sc_softc.sc_intrcnt, EVCNT_TYPE_INTR, NULL,
200 dp->dv_xname, "intr");
201 sc->sc_softc.sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
202 ptsc_intr, &sc->sc_softc, &sc->sc_softc.sc_intrcnt);
203 if (sc->sc_softc.sc_ih == NULL)
204 panic("%s: Cannot install IRQ handler", dp->dv_xname);
205 #else
206 printf(" polling");
207 sc->sc_softc.sc_adapter.adapt_flags = SCSIPI_ADAPT_POLL_ONLY;
208 #endif
209
210 printf("\n");
211
212 /* attach all scsi units on us */
213 config_found(dp, &sc->sc_softc.sc_channel, scsiprint);
214 }
215
216
217 int
218 ptsc_intr(arg)
219 void *arg;
220 {
221 struct sfas_softc *dev = arg;
222 ptsc_regmap_p rp;
223 int quickints;
224
225 rp = (ptsc_regmap_p)dev->sc_fas;
226
227 if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
228 quickints = 16;
229 do {
230 dev->sc_status = *rp->FAS216.sfas_status;
231 dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
232
233 if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
234 dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
235 dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
236 }
237
238 sfasintr(dev);
239
240 } while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
241 && --quickints);
242 }
243
244 return(0); /* Pass interrupt on down the chain */
245 }
246
247 /* Load transfer address into dma register */
248 void
249 ptsc_set_dma_adr(sc, ptr)
250 struct sfas_softc *sc;
251 void *ptr;
252 {
253 #if 0
254 ptsc_regmap_p rp;
255 unsigned int *p;
256 unsigned int d;
257 #endif
258 #if 0
259 printf("ptsc_set_dma_adr(sc = 0x%08x, ptr = 0x%08x)\n", (u_int)sc, (u_int)ptr);
260 #endif
261 return;
262 #if 0
263 rp = (ptsc_regmap_p)sc->sc_fas;
264
265 d = (unsigned int)ptr;
266 p = (unsigned int *)((d & 0xFFFFFF) + (int)rp->dmabase);
267
268 *rp->clear=0;
269 *p = d;
270 #endif
271 }
272
273 /* Set DMA transfer counter */
274 void
275 ptsc_set_dma_tc(sc, len)
276 struct sfas_softc *sc;
277 unsigned int len;
278 {
279 printf("ptsc_set_dma_tc(sc, len = 0x%08x)", len);
280
281 *sc->sc_fas->sfas_tc_low = len; len >>= 8;
282 *sc->sc_fas->sfas_tc_mid = len; len >>= 8;
283 *sc->sc_fas->sfas_tc_high = len;
284 }
285
286 /* Set DMA mode */
287 void
288 ptsc_set_dma_mode(sc, mode)
289 struct sfas_softc *sc;
290 int mode;
291 {
292 #if 0
293 struct csc_specific *spec;
294
295 spec = sc->sc_spec;
296
297 spec->portbits = (spec->portbits & ~FLSC_PB_DMA_BITS) | mode;
298 *((flsc_regmap_p)sc->sc_fas)->hardbits = spec->portbits;
299 #endif
300 }
301
302 /* Initialize DMA for transfer */
303 int
304 ptsc_setup_dma(sc, ptr, len, mode)
305 struct sfas_softc *sc;
306 void *ptr;
307 int len;
308 int mode;
309 {
310 int retval;
311
312 retval = 0;
313
314 #if 0
315 printf("ptsc_setup_dma(sc, ptr = 0x%08x, len = 0x%08x, mode = 0x%08x)\n", (u_int)ptr, len, mode);
316 #endif
317 return(0);
318
319 #if 0
320 switch(mode) {
321 case SFAS_DMA_READ:
322 case SFAS_DMA_WRITE:
323 flsc_set_dma_adr(sc, ptr);
324 if (mode == SFAS_DMA_READ)
325 flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_READ);
326 else
327 flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_WRITE);
328
329 flsc_set_dma_tc(sc, len);
330 break;
331
332 case SFAS_DMA_CLEAR:
333 default:
334 flsc_set_dma_mode(sc, FLSC_PB_DISABLE_DMA);
335 flsc_set_dma_adr(sc, 0);
336
337 retval = (*sc->sc_fas->sfas_tc_high << 16) |
338 (*sc->sc_fas->sfas_tc_mid << 8) |
339 *sc->sc_fas->sfas_tc_low;
340
341 flsc_set_dma_tc(sc, 0);
342 break;
343 }
344
345 return(retval);
346 #endif
347 }
348
349 /* Check if address and len is ok for DMA transfer */
350 int
351 ptsc_need_bump(sc, ptr, len)
352 struct sfas_softc *sc;
353 void *ptr;
354 int len;
355 {
356 int p;
357
358 p = (int)ptr & 0x03;
359
360 if (p) {
361 p = 4-p;
362
363 if (len < 256)
364 p = len;
365 }
366
367 return(p);
368 }
369
370 /* Interrupt driven routines */
371 int
372 ptsc_build_dma_chain(sc, chain, p, l)
373 struct sfas_softc *sc;
374 struct sfas_dma_chain *chain;
375 void *p;
376 int l;
377 {
378 #if 0
379 vm_offset_t pa, lastpa;
380 char *ptr;
381 int len, prelen, postlen, max_t, n;
382 #endif
383 #if 0
384 printf("ptsc_build_dma_chain()\n");
385 #endif
386 return(0);
387
388 #if 0
389 if (l == 0)
390 return(0);
391
392 #define set_link(n, p, l, f)\
393 do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
394
395 n = 0;
396
397 if (l < 512)
398 set_link(n, (vm_offset_t)p, l, SFAS_CHAIN_BUMP);
399 else if (p >= (void *)0xFF000000) {
400 while(l != 0) {
401 len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
402
403 set_link(n, (vm_offset_t)p, len, SFAS_CHAIN_BUMP);
404
405 p += len;
406 l -= len;
407 }
408 } else {
409 ptr = p;
410 len = l;
411
412 pa = kvtop(ptr);
413 prelen = ((int)ptr & 0x03);
414
415 if (prelen) {
416 prelen = 4-prelen;
417 set_link(n, (vm_offset_t)ptr, prelen, SFAS_CHAIN_BUMP);
418 ptr += prelen;
419 len -= prelen;
420 }
421
422 lastpa = 0;
423 while(len > 3) {
424 pa = kvtop(ptr);
425 max_t = NBPG - (pa & PGOFSET);
426 if (max_t > len)
427 max_t = len;
428
429 max_t &= ~3;
430
431 if (lastpa == pa)
432 sc->sc_chain[n-1].len += max_t;
433 else
434 set_link(n, pa, max_t, SFAS_CHAIN_DMA);
435
436 lastpa = pa+max_t;
437
438 ptr += max_t;
439 len -= max_t;
440 }
441
442 if (len)
443 set_link(n, (vm_offset_t)ptr, len, SFAS_CHAIN_BUMP);
444 }
445
446 return(n);
447 #endif
448 }
449
450 /* Turn on/off led */
451 void
452 ptsc_led(sc, mode)
453 struct sfas_softc *sc;
454 int mode;
455 {
456 ptsc_regmap_p rp;
457
458 rp = (ptsc_regmap_p)sc->sc_fas;
459
460 if (mode) {
461 sc->sc_led_status++;
462 } else {
463 if (sc->sc_led_status)
464 sc->sc_led_status--;
465 }
466 *rp->led = (sc->sc_led_status?1:0);
467 }
468