xref: /netbsd-src/sys/arch/acorn32/podulebus/ptsc.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: ptsc.c,v 1.21 2021/08/07 16:18:40 thorpej 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.21 2021/08/07 16:18:40 thorpej 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_NEW(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
ptscmatch(device_t parent,cfdata_t cf,void * aux)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
ptscattach(device_t parent,device_t self,void * aux)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_dev	= self;
180 	sc->sc_softc.sc_fas	= (sfas_regmap_p)rp;
181 	sc->sc_softc.sc_spec	= &sc->sc_specific;
182 
183 	sc->sc_softc.sc_led	= ptsc_led;
184 
185 	sc->sc_softc.sc_setup_dma	= ptsc_setup_dma;
186 	sc->sc_softc.sc_build_dma_chain = ptsc_build_dma_chain;
187 	sc->sc_softc.sc_need_bump	= ptsc_need_bump;
188 
189 	sc->sc_softc.sc_clock_freq   = 40;   /* Power-Tec runs at 8MHz */
190 	sc->sc_softc.sc_timeout      = 250;  /* Set default timeout to 250ms */
191 	sc->sc_softc.sc_config_flags = SFAS_NO_DMA /*| SFAS_NF_DEBUG*/;
192 	sc->sc_softc.sc_host_id      = 7;    /* Should check the jumpers */
193 
194 	sc->sc_softc.sc_bump_sz = PAGE_SIZE;
195 	sc->sc_softc.sc_bump_pa = 0x0;
196 
197 	sfasinitialize((struct sfas_softc *)sc);
198 
199 	sc->sc_softc.sc_adapter.adapt_dev = sc->sc_softc.sc_dev;
200 	sc->sc_softc.sc_adapter.adapt_nchannels = 1;
201 	sc->sc_softc.sc_adapter.adapt_openings = 7;
202 	sc->sc_softc.sc_adapter.adapt_max_periph = 1;
203 	sc->sc_softc.sc_adapter.adapt_ioctl = NULL;
204 	sc->sc_softc.sc_adapter.adapt_minphys = sfas_minphys;
205 	sc->sc_softc.sc_adapter.adapt_request = sfas_scsi_request;
206 
207 	sc->sc_softc.sc_channel.chan_adapter = &sc->sc_softc.sc_adapter;
208 	sc->sc_softc.sc_channel.chan_bustype = &scsi_bustype;
209 	sc->sc_softc.sc_channel.chan_channel = 0;
210 	sc->sc_softc.sc_channel.chan_ntargets = 8;
211 	sc->sc_softc.sc_channel.chan_nluns = 8;
212 	sc->sc_softc.sc_channel.chan_id = sc->sc_softc.sc_host_id;
213 
214 	/* Provide an override for the host id */
215 	(void)get_bootconf_option(boot_args, "ptsc.hostid",
216 	    BOOTOPT_TYPE_INT, &sc->sc_softc.sc_channel.chan_id);
217 
218 	printf(": host=%d", sc->sc_softc.sc_channel.chan_id);
219 
220 	/* initialise the card */
221 /*	*rp->term = 0;*/
222 	*rp->inten = (PTSC_POLL?0:1);
223 	*rp->led = 0;
224 
225 #if PTSC_POLL == 0
226 	evcnt_attach_dynamic(&sc->sc_softc.sc_intrcnt, EVCNT_TYPE_INTR, NULL,
227 	    device_xname(self), "intr");
228 	sc->sc_softc.sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
229 	    ptsc_intr, &sc->sc_softc, &sc->sc_softc.sc_intrcnt);
230 	if (sc->sc_softc.sc_ih == NULL)
231 	    panic("%s: Cannot install IRQ handler", device_xname(self));
232 #else
233 	printf(" polling");
234 	sc->sc_softc.sc_adapter.adapt_flags = SCSIPI_ADAPT_POLL_ONLY;
235 #endif
236 
237 	printf("\n");
238 
239 	/* attach all scsi units on us */
240 	config_found(self, &sc->sc_softc.sc_channel, scsiprint, CFARGS_NONE);
241 }
242 
243 
244 int
ptsc_intr(void * arg)245 ptsc_intr(void *arg)
246 {
247 	struct sfas_softc *dev = arg;
248 	ptsc_regmap_p	      rp;
249 	int		      quickints;
250 
251 	rp = (ptsc_regmap_p)dev->sc_fas;
252 
253 	if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
254 		quickints = 16;
255 		do {
256 			dev->sc_status = *rp->FAS216.sfas_status;
257 			dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
258 
259 			if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
260 				dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
261 				dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
262 			}
263 
264 			sfasintr(dev);
265 
266 		} while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
267 			&& --quickints);
268 	}
269 
270 	return(0);	/* Pass interrupt on down the chain */
271 }
272 
273 /* Load transfer address into DMA register */
274 void
ptsc_set_dma_adr(struct sfas_softc * sc,void * ptr)275 ptsc_set_dma_adr(struct sfas_softc *sc, void *ptr)
276 {
277 #if 0
278 	ptsc_regmap_p	rp;
279 	unsigned int   *p;
280 	unsigned int	d;
281 #endif
282 #if 0
283 	printf("ptsc_set_dma_adr(sc = 0x%08x, ptr = 0x%08x)\n", (u_int)sc, (u_int)ptr);
284 #endif
285 	return;
286 #if 0
287 	rp = (ptsc_regmap_p)sc->sc_fas;
288 
289 	d = (unsigned int)ptr;
290 	p = (unsigned int *)((d & 0xFFFFFF) + (int)rp->dmabase);
291 
292 	*rp->clear=0;
293 	*p = d;
294 #endif
295 }
296 
297 /* Set DMA transfer counter */
298 void
ptsc_set_dma_tc(struct sfas_softc * sc,unsigned int len)299 ptsc_set_dma_tc(struct sfas_softc *sc, unsigned int len)
300 {
301 	printf("ptsc_set_dma_tc(sc, len = 0x%08x)", len);
302 
303 	*sc->sc_fas->sfas_tc_low  = len; len >>= 8;
304 	*sc->sc_fas->sfas_tc_mid  = len; len >>= 8;
305 	*sc->sc_fas->sfas_tc_high = len;
306 }
307 
308 /* Set DMA mode */
309 void
ptsc_set_dma_mode(struct sfas_softc * sc,int mode)310 ptsc_set_dma_mode(struct sfas_softc *sc, int mode)
311 {
312 #if 0
313 	struct csc_specific *spec;
314 
315 	spec = sc->sc_spec;
316 
317 	spec->portbits = (spec->portbits & ~FLSC_PB_DMA_BITS) | mode;
318 	*((flsc_regmap_p)sc->sc_fas)->hardbits = spec->portbits;
319 #endif
320 }
321 
322 /* Initialize DMA for transfer */
323 int
ptsc_setup_dma(void * v,void * ptr,int len,int mode)324 ptsc_setup_dma(void *v, void *ptr, int len, int mode)
325 {
326 	return(0);
327 
328 #if 0
329 	struct sfas_softc *sc = v;
330 	int	retval;
331 
332 	retval = 0;
333 
334 	printf("ptsc_setup_dma(sc, ptr = 0x%08x, len = 0x%08x, mode = 0x%08x)\n", (u_int)ptr, len, mode);
335 
336 	switch(mode) {
337 	case SFAS_DMA_READ:
338 	case SFAS_DMA_WRITE:
339 		flsc_set_dma_adr(sc, ptr);
340 		if (mode == SFAS_DMA_READ)
341 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_READ);
342 		else
343 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_WRITE);
344 
345 		flsc_set_dma_tc(sc, len);
346 		break;
347 
348 	case SFAS_DMA_CLEAR:
349 	default:
350 		flsc_set_dma_mode(sc, FLSC_PB_DISABLE_DMA);
351 		flsc_set_dma_adr(sc, 0);
352 
353 		retval = (*sc->sc_fas->sfas_tc_high << 16) |
354 			 (*sc->sc_fas->sfas_tc_mid  <<  8) |
355 			  *sc->sc_fas->sfas_tc_low;
356 
357 		flsc_set_dma_tc(sc, 0);
358 		break;
359 	}
360 
361 	return(retval);
362 #endif
363 }
364 
365 /* Check if address and len is ok for DMA transfer */
366 int
ptsc_need_bump(void * v,void * ptr,int len)367 ptsc_need_bump(void *v, void *ptr, int len)
368 {
369 	int	p;
370 
371 	p = (int)ptr & 0x03;
372 
373 	if (p) {
374 		p = 4-p;
375 
376 		if (len < 256)
377 			p = len;
378 	}
379 
380 	return(p);
381 }
382 
383 /* Interrupt driven routines */
384 int
ptsc_build_dma_chain(void * v1,void * v2,void * p,int l)385 ptsc_build_dma_chain(void *v1, void *v2, void *p, int l)
386 {
387 #if 0
388 	vaddr_t  pa, lastpa;
389 	char	    *ptr;
390 	int	     len, prelen, postlen, max_t, n;
391 #endif
392 #if 0
393 	printf("ptsc_build_dma_chain()\n");
394 #endif
395 	return(0);
396 
397 #if 0
398 	if (l == 0)
399 		return(0);
400 
401 #define set_link(n, p, l, f)\
402 do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
403 
404 	n = 0;
405 
406 	if (l < 512)
407 		set_link(n, (vaddr_t)p, l, SFAS_CHAIN_BUMP);
408 	else if (p >= (void *)0xFF000000) {
409 		while(l != 0) {
410 			len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
411 
412 			set_link(n, (vaddr_t)p, len, SFAS_CHAIN_BUMP);
413 
414 			p += len;
415 			l -= len;
416 		}
417 	} else {
418 		ptr = p;
419 		len = l;
420 
421 		pa = kvtop(ptr);
422 		prelen = ((int)ptr & 0x03);
423 
424 		if (prelen) {
425 			prelen = 4-prelen;
426 			set_link(n, (vaddr_t)ptr, prelen, SFAS_CHAIN_BUMP);
427 			ptr += prelen;
428 			len -= prelen;
429 		}
430 
431 		lastpa = 0;
432 		while(len > 3) {
433 			pa = kvtop(ptr);
434 			max_t = PAGE_SIZE - (pa & PGOFSET);
435 			if (max_t > len)
436 			  max_t = len;
437 
438 			max_t &= ~3;
439 
440 			if (lastpa == pa)
441 				sc->sc_chain[n-1].len += max_t;
442 			else
443 				set_link(n, pa, max_t, SFAS_CHAIN_DMA);
444 
445 			lastpa = pa+max_t;
446 
447 			ptr += max_t;
448 			len -= max_t;
449 		}
450 
451 		if (len)
452 			set_link(n, (vaddr_t)ptr, len, SFAS_CHAIN_BUMP);
453 	}
454 
455 	return(n);
456 #endif
457 }
458 
459 /* Turn on/off led */
460 void
ptsc_led(void * v,int mode)461 ptsc_led(void *v, int mode)
462 {
463 	struct sfas_softc	*sc = v;
464 	ptsc_regmap_p		rp;
465 
466 	rp = (ptsc_regmap_p)sc->sc_fas;
467 
468 	if (mode) {
469 		sc->sc_led_status++;
470 	} else {
471 		if (sc->sc_led_status)
472 			sc->sc_led_status--;
473 	}
474 	*rp->led = (sc->sc_led_status?1:0);
475 }
476