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