ptsc.c revision 1.16.6.1 1 /* $NetBSD: ptsc.c,v 1.16.6.1 2011/06/06 09:04:40 jruoho 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.16.6.1 2011/06/06 09:04:40 jruoho 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(device_t, cfdata_t, void *);
102 void ptscattach(device_t, device_t, 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(device_t parent, cfdata_t cf, void *aux)
122 {
123 struct podule_attach_args *pa = aux;
124
125 /* Look for the card */
126
127 /*
128 * All Power-tec cards effectively have PowerROMS. Note,
129 * though, that here, if we fail to initialise the loader, we
130 * assume this _is_ the right kind of card.
131 */
132 if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
133 (podulebus_initloader(pa) != 0 ||
134 podloader_callloader(pa, 0, 0) == PRID_POWERTEC))
135 return 1;
136
137 return 0;
138 }
139
140 void
141 ptscattach(device_t parent, device_t self, void *aux)
142 {
143 struct ptsc_softc *sc = device_private(self);
144 struct podule_attach_args *pa = aux;
145 ptsc_regmap_p rp = &sc->sc_regmap;
146 vu_char *fas;
147
148 if (pa->pa_podule_number == -1)
149 panic("Podule has disappeared !");
150
151 sc->sc_specific.sc_podule_number = pa->pa_podule_number;
152 sc->sc_specific.sc_podule = pa->pa_podule;
153 sc->sc_specific.sc_iobase =
154 (vu_char *)sc->sc_specific.sc_podule->fast_base;
155
156 rp->chipreset = &sc->sc_specific.sc_iobase[PTSC_CONTROL_CHIPRESET];
157 rp->inten = &sc->sc_specific.sc_iobase[PTSC_CONTROL_INTEN];
158 rp->status = &sc->sc_specific.sc_iobase[PTSC_STATUS];
159 rp->term = &sc->sc_specific.sc_iobase[PTSC_CONTROL_TERM];
160 rp->led = &sc->sc_specific.sc_iobase[PTSC_CONTROL_LED];
161 fas = &sc->sc_specific.sc_iobase[PTSC_FASOFFSET_BASE];
162
163 rp->FAS216.sfas_tc_low = &fas[PTSC_FASOFFSET_TCL];
164 rp->FAS216.sfas_tc_mid = &fas[PTSC_FASOFFSET_TCM];
165 rp->FAS216.sfas_fifo = &fas[PTSC_FASOFFSET_FIFO];
166 rp->FAS216.sfas_command = &fas[PTSC_FASOFFSET_COMMAND];
167 rp->FAS216.sfas_dest_id = &fas[PTSC_FASOFFSET_DESTID];
168 rp->FAS216.sfas_timeout = &fas[PTSC_FASOFFSET_TIMEOUT];
169 rp->FAS216.sfas_syncper = &fas[PTSC_FASOFFSET_PERIOD];
170 rp->FAS216.sfas_syncoff = &fas[PTSC_FASOFFSET_OFFSET];
171 rp->FAS216.sfas_config1 = &fas[PTSC_FASOFFSET_CONFIG1];
172 rp->FAS216.sfas_clkconv = &fas[PTSC_FASOFFSET_CLOCKCONV];
173 rp->FAS216.sfas_test = &fas[PTSC_FASOFFSET_TEST];
174 rp->FAS216.sfas_config2 = &fas[PTSC_FASOFFSET_CONFIG2];
175 rp->FAS216.sfas_config3 = &fas[PTSC_FASOFFSET_CONFIG3];
176 rp->FAS216.sfas_tc_high = &fas[PTSC_FASOFFSET_TCH];
177 rp->FAS216.sfas_fifo_bot = &fas[PTSC_FASOFFSET_FIFOBOTTOM];
178
179 sc->sc_softc.sc_fas = (sfas_regmap_p)rp;
180 sc->sc_softc.sc_spec = &sc->sc_specific;
181
182 sc->sc_softc.sc_led = ptsc_led;
183
184 sc->sc_softc.sc_setup_dma = ptsc_setup_dma;
185 sc->sc_softc.sc_build_dma_chain = ptsc_build_dma_chain;
186 sc->sc_softc.sc_need_bump = ptsc_need_bump;
187
188 sc->sc_softc.sc_clock_freq = 40; /* Power-Tec runs at 8MHz */
189 sc->sc_softc.sc_timeout = 250; /* Set default timeout to 250ms */
190 sc->sc_softc.sc_config_flags = SFAS_NO_DMA /*| SFAS_NF_DEBUG*/;
191 sc->sc_softc.sc_host_id = 7; /* Should check the jumpers */
192
193 sc->sc_softc.sc_bump_sz = PAGE_SIZE;
194 sc->sc_softc.sc_bump_pa = 0x0;
195
196 sfasinitialize((struct sfas_softc *)sc);
197
198 sc->sc_softc.sc_adapter.adapt_dev = &sc->sc_softc.sc_dev;
199 sc->sc_softc.sc_adapter.adapt_nchannels = 1;
200 sc->sc_softc.sc_adapter.adapt_openings = 7;
201 sc->sc_softc.sc_adapter.adapt_max_periph = 1;
202 sc->sc_softc.sc_adapter.adapt_ioctl = NULL;
203 sc->sc_softc.sc_adapter.adapt_minphys = sfas_minphys;
204 sc->sc_softc.sc_adapter.adapt_request = sfas_scsi_request;
205
206 sc->sc_softc.sc_channel.chan_adapter = &sc->sc_softc.sc_adapter;
207 sc->sc_softc.sc_channel.chan_bustype = &scsi_bustype;
208 sc->sc_softc.sc_channel.chan_channel = 0;
209 sc->sc_softc.sc_channel.chan_ntargets = 8;
210 sc->sc_softc.sc_channel.chan_nluns = 8;
211 sc->sc_softc.sc_channel.chan_id = sc->sc_softc.sc_host_id;
212
213 /* Provide an override for the host id */
214 (void)get_bootconf_option(boot_args, "ptsc.hostid",
215 BOOTOPT_TYPE_INT, &sc->sc_softc.sc_channel.chan_id);
216
217 printf(": host=%d", sc->sc_softc.sc_channel.chan_id);
218
219 /* initialise the card */
220 /* *rp->term = 0;*/
221 *rp->inten = (PTSC_POLL?0:1);
222 *rp->led = 0;
223
224 #if PTSC_POLL == 0
225 evcnt_attach_dynamic(&sc->sc_softc.sc_intrcnt, EVCNT_TYPE_INTR, NULL,
226 device_xname(self), "intr");
227 sc->sc_softc.sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
228 ptsc_intr, &sc->sc_softc, &sc->sc_softc.sc_intrcnt);
229 if (sc->sc_softc.sc_ih == NULL)
230 panic("%s: Cannot install IRQ handler", device_xname(self));
231 #else
232 printf(" polling");
233 sc->sc_softc.sc_adapter.adapt_flags = SCSIPI_ADAPT_POLL_ONLY;
234 #endif
235
236 printf("\n");
237
238 /* attach all scsi units on us */
239 config_found(self, &sc->sc_softc.sc_channel, scsiprint);
240 }
241
242
243 int
244 ptsc_intr(void *arg)
245 {
246 struct sfas_softc *dev = arg;
247 ptsc_regmap_p rp;
248 int quickints;
249
250 rp = (ptsc_regmap_p)dev->sc_fas;
251
252 if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
253 quickints = 16;
254 do {
255 dev->sc_status = *rp->FAS216.sfas_status;
256 dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
257
258 if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
259 dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
260 dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
261 }
262
263 sfasintr(dev);
264
265 } while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
266 && --quickints);
267 }
268
269 return(0); /* Pass interrupt on down the chain */
270 }
271
272 /* Load transfer address into DMA register */
273 void
274 ptsc_set_dma_adr(struct sfas_softc *sc, void *ptr)
275 {
276 #if 0
277 ptsc_regmap_p rp;
278 unsigned int *p;
279 unsigned int d;
280 #endif
281 #if 0
282 printf("ptsc_set_dma_adr(sc = 0x%08x, ptr = 0x%08x)\n", (u_int)sc, (u_int)ptr);
283 #endif
284 return;
285 #if 0
286 rp = (ptsc_regmap_p)sc->sc_fas;
287
288 d = (unsigned int)ptr;
289 p = (unsigned int *)((d & 0xFFFFFF) + (int)rp->dmabase);
290
291 *rp->clear=0;
292 *p = d;
293 #endif
294 }
295
296 /* Set DMA transfer counter */
297 void
298 ptsc_set_dma_tc(struct sfas_softc *sc, unsigned int len)
299 {
300 printf("ptsc_set_dma_tc(sc, len = 0x%08x)", len);
301
302 *sc->sc_fas->sfas_tc_low = len; len >>= 8;
303 *sc->sc_fas->sfas_tc_mid = len; len >>= 8;
304 *sc->sc_fas->sfas_tc_high = len;
305 }
306
307 /* Set DMA mode */
308 void
309 ptsc_set_dma_mode(struct sfas_softc *sc, int mode)
310 {
311 #if 0
312 struct csc_specific *spec;
313
314 spec = sc->sc_spec;
315
316 spec->portbits = (spec->portbits & ~FLSC_PB_DMA_BITS) | mode;
317 *((flsc_regmap_p)sc->sc_fas)->hardbits = spec->portbits;
318 #endif
319 }
320
321 /* Initialize DMA for transfer */
322 int
323 ptsc_setup_dma(void *v, void *ptr, int len, int mode)
324 {
325 return(0);
326
327 #if 0
328 struct sfas_softc *sc = v;
329 int retval;
330
331 retval = 0;
332
333 printf("ptsc_setup_dma(sc, ptr = 0x%08x, len = 0x%08x, mode = 0x%08x)\n", (u_int)ptr, len, mode);
334
335 switch(mode) {
336 case SFAS_DMA_READ:
337 case SFAS_DMA_WRITE:
338 flsc_set_dma_adr(sc, ptr);
339 if (mode == SFAS_DMA_READ)
340 flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_READ);
341 else
342 flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_WRITE);
343
344 flsc_set_dma_tc(sc, len);
345 break;
346
347 case SFAS_DMA_CLEAR:
348 default:
349 flsc_set_dma_mode(sc, FLSC_PB_DISABLE_DMA);
350 flsc_set_dma_adr(sc, 0);
351
352 retval = (*sc->sc_fas->sfas_tc_high << 16) |
353 (*sc->sc_fas->sfas_tc_mid << 8) |
354 *sc->sc_fas->sfas_tc_low;
355
356 flsc_set_dma_tc(sc, 0);
357 break;
358 }
359
360 return(retval);
361 #endif
362 }
363
364 /* Check if address and len is ok for DMA transfer */
365 int
366 ptsc_need_bump(void *v, void *ptr, int len)
367 {
368 int p;
369
370 p = (int)ptr & 0x03;
371
372 if (p) {
373 p = 4-p;
374
375 if (len < 256)
376 p = len;
377 }
378
379 return(p);
380 }
381
382 /* Interrupt driven routines */
383 int
384 ptsc_build_dma_chain(void *v1, void *v2, void *p, int l)
385 {
386 #if 0
387 vm_offset_t pa, lastpa;
388 char *ptr;
389 int len, prelen, postlen, max_t, n;
390 #endif
391 #if 0
392 printf("ptsc_build_dma_chain()\n");
393 #endif
394 return(0);
395
396 #if 0
397 if (l == 0)
398 return(0);
399
400 #define set_link(n, p, l, f)\
401 do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
402
403 n = 0;
404
405 if (l < 512)
406 set_link(n, (vm_offset_t)p, l, SFAS_CHAIN_BUMP);
407 else if (p >= (void *)0xFF000000) {
408 while(l != 0) {
409 len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
410
411 set_link(n, (vm_offset_t)p, len, SFAS_CHAIN_BUMP);
412
413 p += len;
414 l -= len;
415 }
416 } else {
417 ptr = p;
418 len = l;
419
420 pa = kvtop(ptr);
421 prelen = ((int)ptr & 0x03);
422
423 if (prelen) {
424 prelen = 4-prelen;
425 set_link(n, (vm_offset_t)ptr, prelen, SFAS_CHAIN_BUMP);
426 ptr += prelen;
427 len -= prelen;
428 }
429
430 lastpa = 0;
431 while(len > 3) {
432 pa = kvtop(ptr);
433 max_t = PAGE_SIZE - (pa & PGOFSET);
434 if (max_t > len)
435 max_t = len;
436
437 max_t &= ~3;
438
439 if (lastpa == pa)
440 sc->sc_chain[n-1].len += max_t;
441 else
442 set_link(n, pa, max_t, SFAS_CHAIN_DMA);
443
444 lastpa = pa+max_t;
445
446 ptr += max_t;
447 len -= max_t;
448 }
449
450 if (len)
451 set_link(n, (vm_offset_t)ptr, len, SFAS_CHAIN_BUMP);
452 }
453
454 return(n);
455 #endif
456 }
457
458 /* Turn on/off led */
459 void
460 ptsc_led(void *v, int mode)
461 {
462 struct sfas_softc *sc = v;
463 ptsc_regmap_p rp;
464
465 rp = (ptsc_regmap_p)sc->sc_fas;
466
467 if (mode) {
468 sc->sc_led_status++;
469 } else {
470 if (sc->sc_led_status)
471 sc->sc_led_status--;
472 }
473 *rp->led = (sc->sc_led_status?1:0);
474 }
475