Home | History | Annotate | Line # | Download | only in podulebus
ptsc.c revision 1.11
      1 /*	$NetBSD: ptsc.c,v 1.11 2003/07/14 22:48:26 lukem 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/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: ptsc.c,v 1.11 2003/07/14 22:48:26 lukem Exp $");
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/device.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 #include <dev/scsipi/scsi_all.h>
     59 #include <dev/scsipi/scsipi_all.h>
     60 #include <dev/scsipi/scsiconf.h>
     61 #include <machine/io.h>
     62 #include <machine/intr.h>
     63 #include <machine/bootconfig.h>
     64 #include <acorn32/podulebus/podulebus.h>
     65 #include <acorn32/podulebus/sfasreg.h>
     66 #include <acorn32/podulebus/sfasvar.h>
     67 #include <acorn32/podulebus/ptscreg.h>
     68 #include <acorn32/podulebus/ptscvar.h>
     69 #include <dev/podulebus/podules.h>
     70 #include <dev/podulebus/powerromreg.h>
     71 
     72 int  ptscmatch  __P((struct device *, struct cfdata *, void *));
     73 void ptscattach __P((struct device *, struct device *, void *));
     74 
     75 CFATTACH_DECL(ptsc, sizeof(struct ptsc_softc),
     76     ptscmatch, ptscattach, NULL, NULL);
     77 
     78 int ptsc_intr(void *);
     79 int ptsc_setup_dma(void *, void *, int, int);
     80 int ptsc_build_dma_chain(void *, void *, void *, int);
     81 int ptsc_need_bump(void *, void *, int);
     82 void ptsc_led(void *, int);
     83 
     84 void ptsc_set_dma_adr(struct sfas_softc *, void *);
     85 void ptsc_set_dma_tc(struct sfas_softc *, unsigned int);
     86 void ptsc_set_dma_mode(struct sfas_softc *, int);
     87 
     88 /*
     89  * if we are a Power-tec SCSI-2 card
     90  */
     91 int
     92 ptscmatch(pdp, cf, auxp)
     93 	struct device	*pdp;
     94 	struct cfdata	*cf;
     95 	void		*auxp;
     96 {
     97 	struct podule_attach_args *pa = (struct podule_attach_args *)auxp;
     98 
     99 	/* Look for the card */
    100 
    101 	/*
    102 	 * All Power-tec cards effectively have PowerROMS.  Note,
    103 	 * though, that here, if we fail to initialise the loader, we
    104 	 * assume this _is_ the right kind of card.
    105 	 */
    106         if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
    107             (podulebus_initloader(pa) != 0 ||
    108 	     podloader_callloader(pa, 0, 0) == PRID_POWERTEC))
    109                 return 1;
    110 
    111 	return 0;
    112 }
    113 
    114 void
    115 ptscattach(pdp, dp, auxp)
    116 	struct device	*pdp;
    117 	struct device	*dp;
    118 	void		*auxp;
    119 {
    120 	struct ptsc_softc *sc = (struct ptsc_softc *)dp;
    121 	struct podule_attach_args  *pa;
    122 	ptsc_regmap_p	   rp = &sc->sc_regmap;
    123 	vu_char		  *fas;
    124 
    125 	pa = (struct podule_attach_args *)auxp;
    126 
    127 	if (pa->pa_podule_number == -1)
    128 		panic("Podule has disappeared !");
    129 
    130 	sc->sc_specific.sc_podule_number = pa->pa_podule_number;
    131 	sc->sc_specific.sc_podule = pa->pa_podule;
    132 	sc->sc_specific.sc_iobase =
    133 	  (vu_char *)sc->sc_specific.sc_podule->fast_base;
    134 
    135 	rp->chipreset = &sc->sc_specific.sc_iobase[PTSC_CONTROL_CHIPRESET];
    136 	rp->inten = &sc->sc_specific.sc_iobase[PTSC_CONTROL_INTEN];
    137 	rp->status = &sc->sc_specific.sc_iobase[PTSC_STATUS];
    138 	rp->term = &sc->sc_specific.sc_iobase[PTSC_CONTROL_TERM];
    139 	rp->led = &sc->sc_specific.sc_iobase[PTSC_CONTROL_LED];
    140 	fas = &sc->sc_specific.sc_iobase[PTSC_FASOFFSET_BASE];
    141 
    142 	rp->FAS216.sfas_tc_low	= &fas[PTSC_FASOFFSET_TCL];
    143 	rp->FAS216.sfas_tc_mid	= &fas[PTSC_FASOFFSET_TCM];
    144 	rp->FAS216.sfas_fifo	= &fas[PTSC_FASOFFSET_FIFO];
    145 	rp->FAS216.sfas_command	= &fas[PTSC_FASOFFSET_COMMAND];
    146 	rp->FAS216.sfas_dest_id	= &fas[PTSC_FASOFFSET_DESTID];
    147 	rp->FAS216.sfas_timeout	= &fas[PTSC_FASOFFSET_TIMEOUT];
    148 	rp->FAS216.sfas_syncper	= &fas[PTSC_FASOFFSET_PERIOD];
    149 	rp->FAS216.sfas_syncoff	= &fas[PTSC_FASOFFSET_OFFSET];
    150 	rp->FAS216.sfas_config1	= &fas[PTSC_FASOFFSET_CONFIG1];
    151 	rp->FAS216.sfas_clkconv	= &fas[PTSC_FASOFFSET_CLOCKCONV];
    152 	rp->FAS216.sfas_test	= &fas[PTSC_FASOFFSET_TEST];
    153 	rp->FAS216.sfas_config2	= &fas[PTSC_FASOFFSET_CONFIG2];
    154 	rp->FAS216.sfas_config3	= &fas[PTSC_FASOFFSET_CONFIG3];
    155 	rp->FAS216.sfas_tc_high	= &fas[PTSC_FASOFFSET_TCH];
    156 	rp->FAS216.sfas_fifo_bot = &fas[PTSC_FASOFFSET_FIFOBOTTOM];
    157 
    158 	sc->sc_softc.sc_fas	= (sfas_regmap_p)rp;
    159 	sc->sc_softc.sc_spec	= &sc->sc_specific;
    160 
    161 	sc->sc_softc.sc_led	= ptsc_led;
    162 
    163 	sc->sc_softc.sc_setup_dma	= ptsc_setup_dma;
    164 	sc->sc_softc.sc_build_dma_chain = ptsc_build_dma_chain;
    165 	sc->sc_softc.sc_need_bump	= ptsc_need_bump;
    166 
    167 	sc->sc_softc.sc_clock_freq   = 40;   /* Power-Tec runs at 8MHz */
    168 	sc->sc_softc.sc_timeout      = 250;  /* Set default timeout to 250ms */
    169 	sc->sc_softc.sc_config_flags = SFAS_NO_DMA /*| SFAS_NF_DEBUG*/;
    170 	sc->sc_softc.sc_host_id      = 7;    /* Should check the jumpers */
    171 
    172 	sc->sc_softc.sc_bump_sz = PAGE_SIZE;
    173 	sc->sc_softc.sc_bump_pa = 0x0;
    174 
    175 	sfasinitialize((struct sfas_softc *)sc);
    176 
    177 	sc->sc_softc.sc_adapter.adapt_dev = &sc->sc_softc.sc_dev;
    178 	sc->sc_softc.sc_adapter.adapt_nchannels = 1;
    179 	sc->sc_softc.sc_adapter.adapt_openings = 7;
    180 	sc->sc_softc.sc_adapter.adapt_max_periph = 1;
    181 	sc->sc_softc.sc_adapter.adapt_ioctl = NULL;
    182 	sc->sc_softc.sc_adapter.adapt_minphys = sfas_minphys;
    183 	sc->sc_softc.sc_adapter.adapt_request = sfas_scsi_request;
    184 
    185 	sc->sc_softc.sc_channel.chan_adapter = &sc->sc_softc.sc_adapter;
    186 	sc->sc_softc.sc_channel.chan_bustype = &scsi_bustype;
    187 	sc->sc_softc.sc_channel.chan_channel = 0;
    188 	sc->sc_softc.sc_channel.chan_ntargets = 8;
    189 	sc->sc_softc.sc_channel.chan_nluns = 8;
    190 	sc->sc_softc.sc_channel.chan_id = sc->sc_softc.sc_host_id;
    191 
    192 	/* Provide an override for the host id */
    193 	(void)get_bootconf_option(boot_args, "ptsc.hostid",
    194 	    BOOTOPT_TYPE_INT, &sc->sc_softc.sc_channel.chan_id);
    195 
    196 	printf(": host=%d", sc->sc_softc.sc_channel.chan_id);
    197 
    198 	/* initialise the card */
    199 /*	*rp->term = 0;*/
    200 	*rp->inten = (PTSC_POLL?0:1);
    201 	*rp->led = 0;
    202 
    203 #if PTSC_POLL == 0
    204 	evcnt_attach_dynamic(&sc->sc_softc.sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    205 	    dp->dv_xname, "intr");
    206 	sc->sc_softc.sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
    207 	    ptsc_intr, &sc->sc_softc, &sc->sc_softc.sc_intrcnt);
    208 	if (sc->sc_softc.sc_ih == NULL)
    209 	    panic("%s: Cannot install IRQ handler", dp->dv_xname);
    210 #else
    211 	printf(" polling");
    212 	sc->sc_softc.sc_adapter.adapt_flags = SCSIPI_ADAPT_POLL_ONLY;
    213 #endif
    214 
    215 	printf("\n");
    216 
    217 	/* attach all scsi units on us */
    218 	config_found(dp, &sc->sc_softc.sc_channel, scsiprint);
    219 }
    220 
    221 
    222 int
    223 ptsc_intr(arg)
    224 	void *arg;
    225 {
    226 	struct sfas_softc *dev = arg;
    227 	ptsc_regmap_p	      rp;
    228 	int		      quickints;
    229 
    230 	rp = (ptsc_regmap_p)dev->sc_fas;
    231 
    232 	if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
    233 		quickints = 16;
    234 		do {
    235 			dev->sc_status = *rp->FAS216.sfas_status;
    236 			dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
    237 
    238 			if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    239 				dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
    240 				dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
    241 			}
    242 
    243 			sfasintr(dev);
    244 
    245 		} while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
    246 			&& --quickints);
    247 	}
    248 
    249 	return(0);	/* Pass interrupt on down the chain */
    250 }
    251 
    252 /* Load transfer address into DMA register */
    253 void
    254 ptsc_set_dma_adr(sc, ptr)
    255 	struct sfas_softc *sc;
    256 	void		 *ptr;
    257 {
    258 #if 0
    259 	ptsc_regmap_p	rp;
    260 	unsigned int   *p;
    261 	unsigned int	d;
    262 #endif
    263 #if 0
    264 	printf("ptsc_set_dma_adr(sc = 0x%08x, ptr = 0x%08x)\n", (u_int)sc, (u_int)ptr);
    265 #endif
    266 	return;
    267 #if 0
    268 	rp = (ptsc_regmap_p)sc->sc_fas;
    269 
    270 	d = (unsigned int)ptr;
    271 	p = (unsigned int *)((d & 0xFFFFFF) + (int)rp->dmabase);
    272 
    273 	*rp->clear=0;
    274 	*p = d;
    275 #endif
    276 }
    277 
    278 /* Set DMA transfer counter */
    279 void
    280 ptsc_set_dma_tc(sc, len)
    281 	struct sfas_softc *sc;
    282 	unsigned int	  len;
    283 {
    284 	printf("ptsc_set_dma_tc(sc, len = 0x%08x)", len);
    285 
    286 	*sc->sc_fas->sfas_tc_low  = len; len >>= 8;
    287 	*sc->sc_fas->sfas_tc_mid  = len; len >>= 8;
    288 	*sc->sc_fas->sfas_tc_high = len;
    289 }
    290 
    291 /* Set DMA mode */
    292 void
    293 ptsc_set_dma_mode(sc, mode)
    294 	struct sfas_softc *sc;
    295 	int		  mode;
    296 {
    297 #if 0
    298 	struct csc_specific *spec;
    299 
    300 	spec = sc->sc_spec;
    301 
    302 	spec->portbits = (spec->portbits & ~FLSC_PB_DMA_BITS) | mode;
    303 	*((flsc_regmap_p)sc->sc_fas)->hardbits = spec->portbits;
    304 #endif
    305 }
    306 
    307 /* Initialize DMA for transfer */
    308 int
    309 ptsc_setup_dma(v, ptr, len, mode)
    310 	void	*v;
    311 	void	*ptr;
    312 	int	len;
    313 	int	mode;
    314 {
    315 	return(0);
    316 
    317 #if 0
    318 	struct sfas_softc *sc = v;
    319 	int	retval;
    320 
    321 	retval = 0;
    322 
    323 	printf("ptsc_setup_dma(sc, ptr = 0x%08x, len = 0x%08x, mode = 0x%08x)\n", (u_int)ptr, len, mode);
    324 
    325 	switch(mode) {
    326 	case SFAS_DMA_READ:
    327 	case SFAS_DMA_WRITE:
    328 		flsc_set_dma_adr(sc, ptr);
    329 		if (mode == SFAS_DMA_READ)
    330 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_READ);
    331 		else
    332 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_WRITE);
    333 
    334 		flsc_set_dma_tc(sc, len);
    335 		break;
    336 
    337 	case SFAS_DMA_CLEAR:
    338 	default:
    339 		flsc_set_dma_mode(sc, FLSC_PB_DISABLE_DMA);
    340 		flsc_set_dma_adr(sc, 0);
    341 
    342 		retval = (*sc->sc_fas->sfas_tc_high << 16) |
    343 			 (*sc->sc_fas->sfas_tc_mid  <<  8) |
    344 			  *sc->sc_fas->sfas_tc_low;
    345 
    346 		flsc_set_dma_tc(sc, 0);
    347 		break;
    348 	}
    349 
    350 	return(retval);
    351 #endif
    352 }
    353 
    354 /* Check if address and len is ok for DMA transfer */
    355 int
    356 ptsc_need_bump(v, ptr, len)
    357 	void	*v;
    358 	void	*ptr;
    359 	int	len;
    360 {
    361 	int	p;
    362 
    363 	p = (int)ptr & 0x03;
    364 
    365 	if (p) {
    366 		p = 4-p;
    367 
    368 		if (len < 256)
    369 			p = len;
    370 	}
    371 
    372 	return(p);
    373 }
    374 
    375 /* Interrupt driven routines */
    376 int
    377 ptsc_build_dma_chain(void *v1, void *v2, void *p, 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 = PAGE_SIZE - (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(void *v, int mode)
    454 {
    455 	struct sfas_softc	*sc = v;
    456 	ptsc_regmap_p		rp;
    457 
    458 	rp = (ptsc_regmap_p)sc->sc_fas;
    459 
    460 	if (mode) {
    461 		sc->sc_led_status++;
    462 	} else {
    463 		if (sc->sc_led_status)
    464 			sc->sc_led_status--;
    465 	}
    466 	*rp->led = (sc->sc_led_status?1:0);
    467 }
    468