Home | History | Annotate | Line # | Download | only in podulebus
ptsc.c revision 1.10
      1 /*	$NetBSD: ptsc.c,v 1.10 2003/05/03 18:10:41 wiz 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 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <dev/scsipi/scsi_all.h>
     56 #include <dev/scsipi/scsipi_all.h>
     57 #include <dev/scsipi/scsiconf.h>
     58 #include <machine/io.h>
     59 #include <machine/intr.h>
     60 #include <machine/bootconfig.h>
     61 #include <acorn32/podulebus/podulebus.h>
     62 #include <acorn32/podulebus/sfasreg.h>
     63 #include <acorn32/podulebus/sfasvar.h>
     64 #include <acorn32/podulebus/ptscreg.h>
     65 #include <acorn32/podulebus/ptscvar.h>
     66 #include <dev/podulebus/podules.h>
     67 #include <dev/podulebus/powerromreg.h>
     68 
     69 int  ptscmatch  __P((struct device *, struct cfdata *, void *));
     70 void ptscattach __P((struct device *, struct device *, void *));
     71 
     72 CFATTACH_DECL(ptsc, sizeof(struct ptsc_softc),
     73     ptscmatch, ptscattach, NULL, NULL);
     74 
     75 int ptsc_intr(void *);
     76 int ptsc_setup_dma(void *, void *, int, int);
     77 int ptsc_build_dma_chain(void *, void *, void *, int);
     78 int ptsc_need_bump(void *, void *, int);
     79 void ptsc_led(void *, int);
     80 
     81 void ptsc_set_dma_adr(struct sfas_softc *, void *);
     82 void ptsc_set_dma_tc(struct sfas_softc *, unsigned int);
     83 void ptsc_set_dma_mode(struct sfas_softc *, int);
     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 = PAGE_SIZE;
    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 = sfas_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", dp->dv_xname);
    207 #else
    208 	printf(" polling");
    209 	sc->sc_softc.sc_adapter.adapt_flags = SCSIPI_ADAPT_POLL_ONLY;
    210 #endif
    211 
    212 	printf("\n");
    213 
    214 	/* attach all scsi units on us */
    215 	config_found(dp, &sc->sc_softc.sc_channel, scsiprint);
    216 }
    217 
    218 
    219 int
    220 ptsc_intr(arg)
    221 	void *arg;
    222 {
    223 	struct sfas_softc *dev = arg;
    224 	ptsc_regmap_p	      rp;
    225 	int		      quickints;
    226 
    227 	rp = (ptsc_regmap_p)dev->sc_fas;
    228 
    229 	if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
    230 		quickints = 16;
    231 		do {
    232 			dev->sc_status = *rp->FAS216.sfas_status;
    233 			dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
    234 
    235 			if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    236 				dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
    237 				dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
    238 			}
    239 
    240 			sfasintr(dev);
    241 
    242 		} while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
    243 			&& --quickints);
    244 	}
    245 
    246 	return(0);	/* Pass interrupt on down the chain */
    247 }
    248 
    249 /* Load transfer address into DMA register */
    250 void
    251 ptsc_set_dma_adr(sc, ptr)
    252 	struct sfas_softc *sc;
    253 	void		 *ptr;
    254 {
    255 #if 0
    256 	ptsc_regmap_p	rp;
    257 	unsigned int   *p;
    258 	unsigned int	d;
    259 #endif
    260 #if 0
    261 	printf("ptsc_set_dma_adr(sc = 0x%08x, ptr = 0x%08x)\n", (u_int)sc, (u_int)ptr);
    262 #endif
    263 	return;
    264 #if 0
    265 	rp = (ptsc_regmap_p)sc->sc_fas;
    266 
    267 	d = (unsigned int)ptr;
    268 	p = (unsigned int *)((d & 0xFFFFFF) + (int)rp->dmabase);
    269 
    270 	*rp->clear=0;
    271 	*p = d;
    272 #endif
    273 }
    274 
    275 /* Set DMA transfer counter */
    276 void
    277 ptsc_set_dma_tc(sc, len)
    278 	struct sfas_softc *sc;
    279 	unsigned int	  len;
    280 {
    281 	printf("ptsc_set_dma_tc(sc, len = 0x%08x)", len);
    282 
    283 	*sc->sc_fas->sfas_tc_low  = len; len >>= 8;
    284 	*sc->sc_fas->sfas_tc_mid  = len; len >>= 8;
    285 	*sc->sc_fas->sfas_tc_high = len;
    286 }
    287 
    288 /* Set DMA mode */
    289 void
    290 ptsc_set_dma_mode(sc, mode)
    291 	struct sfas_softc *sc;
    292 	int		  mode;
    293 {
    294 #if 0
    295 	struct csc_specific *spec;
    296 
    297 	spec = sc->sc_spec;
    298 
    299 	spec->portbits = (spec->portbits & ~FLSC_PB_DMA_BITS) | mode;
    300 	*((flsc_regmap_p)sc->sc_fas)->hardbits = spec->portbits;
    301 #endif
    302 }
    303 
    304 /* Initialize DMA for transfer */
    305 int
    306 ptsc_setup_dma(v, ptr, len, mode)
    307 	void	*v;
    308 	void	*ptr;
    309 	int	len;
    310 	int	mode;
    311 {
    312 	return(0);
    313 
    314 #if 0
    315 	struct sfas_softc *sc = v;
    316 	int	retval;
    317 
    318 	retval = 0;
    319 
    320 	printf("ptsc_setup_dma(sc, ptr = 0x%08x, len = 0x%08x, mode = 0x%08x)\n", (u_int)ptr, len, mode);
    321 
    322 	switch(mode) {
    323 	case SFAS_DMA_READ:
    324 	case SFAS_DMA_WRITE:
    325 		flsc_set_dma_adr(sc, ptr);
    326 		if (mode == SFAS_DMA_READ)
    327 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_READ);
    328 		else
    329 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_WRITE);
    330 
    331 		flsc_set_dma_tc(sc, len);
    332 		break;
    333 
    334 	case SFAS_DMA_CLEAR:
    335 	default:
    336 		flsc_set_dma_mode(sc, FLSC_PB_DISABLE_DMA);
    337 		flsc_set_dma_adr(sc, 0);
    338 
    339 		retval = (*sc->sc_fas->sfas_tc_high << 16) |
    340 			 (*sc->sc_fas->sfas_tc_mid  <<  8) |
    341 			  *sc->sc_fas->sfas_tc_low;
    342 
    343 		flsc_set_dma_tc(sc, 0);
    344 		break;
    345 	}
    346 
    347 	return(retval);
    348 #endif
    349 }
    350 
    351 /* Check if address and len is ok for DMA transfer */
    352 int
    353 ptsc_need_bump(v, ptr, len)
    354 	void	*v;
    355 	void	*ptr;
    356 	int	len;
    357 {
    358 	int	p;
    359 
    360 	p = (int)ptr & 0x03;
    361 
    362 	if (p) {
    363 		p = 4-p;
    364 
    365 		if (len < 256)
    366 			p = len;
    367 	}
    368 
    369 	return(p);
    370 }
    371 
    372 /* Interrupt driven routines */
    373 int
    374 ptsc_build_dma_chain(void *v1, void *v2, void *p, int l)
    375 {
    376 #if 0
    377 	vm_offset_t  pa, lastpa;
    378 	char	    *ptr;
    379 	int	     len, prelen, postlen, max_t, n;
    380 #endif
    381 #if 0
    382 	printf("ptsc_build_dma_chain()\n");
    383 #endif
    384 	return(0);
    385 
    386 #if 0
    387 	if (l == 0)
    388 		return(0);
    389 
    390 #define set_link(n, p, l, f)\
    391 do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
    392 
    393 	n = 0;
    394 
    395 	if (l < 512)
    396 		set_link(n, (vm_offset_t)p, l, SFAS_CHAIN_BUMP);
    397 	else if (p >= (void *)0xFF000000) {
    398 		while(l != 0) {
    399 			len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
    400 
    401 			set_link(n, (vm_offset_t)p, len, SFAS_CHAIN_BUMP);
    402 
    403 			p += len;
    404 			l -= len;
    405 		}
    406 	} else {
    407 		ptr = p;
    408 		len = l;
    409 
    410 		pa = kvtop(ptr);
    411 		prelen = ((int)ptr & 0x03);
    412 
    413 		if (prelen) {
    414 			prelen = 4-prelen;
    415 			set_link(n, (vm_offset_t)ptr, prelen, SFAS_CHAIN_BUMP);
    416 			ptr += prelen;
    417 			len -= prelen;
    418 		}
    419 
    420 		lastpa = 0;
    421 		while(len > 3) {
    422 			pa = kvtop(ptr);
    423 			max_t = PAGE_SIZE - (pa & PGOFSET);
    424 			if (max_t > len)
    425 			  max_t = len;
    426 
    427 			max_t &= ~3;
    428 
    429 			if (lastpa == pa)
    430 				sc->sc_chain[n-1].len += max_t;
    431 			else
    432 				set_link(n, pa, max_t, SFAS_CHAIN_DMA);
    433 
    434 			lastpa = pa+max_t;
    435 
    436 			ptr += max_t;
    437 			len -= max_t;
    438 		}
    439 
    440 		if (len)
    441 			set_link(n, (vm_offset_t)ptr, len, SFAS_CHAIN_BUMP);
    442 	}
    443 
    444 	return(n);
    445 #endif
    446 }
    447 
    448 /* Turn on/off led */
    449 void
    450 ptsc_led(void *v, int mode)
    451 {
    452 	struct sfas_softc	*sc = v;
    453 	ptsc_regmap_p		rp;
    454 
    455 	rp = (ptsc_regmap_p)sc->sc_fas;
    456 
    457 	if (mode) {
    458 		sc->sc_led_status++;
    459 	} else {
    460 		if (sc->sc_led_status)
    461 			sc->sc_led_status--;
    462 	}
    463 	*rp->led = (sc->sc_led_status?1:0);
    464 }
    465