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