Home | History | Annotate | Line # | Download | only in podulebus
csc.c revision 1.7
      1 /*	$NetBSD: csc.c,v 1.7 2002/10/02 02:23:51 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Scott Stevens.
      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 NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Cumana SCSI-2 driver uses the SFAS216 generic driver
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <dev/scsipi/scsi_all.h>
     48 #include <dev/scsipi/scsipi_all.h>
     49 #include <dev/scsipi/scsiconf.h>
     50 #include <uvm/uvm_extern.h>
     51 #include <machine/pmap.h>
     52 #include <machine/io.h>
     53 #include <machine/intr.h>
     54 #include <machine/bootconfig.h>
     55 #include <acorn32/podulebus/podulebus.h>
     56 #include <acorn32/podulebus/sfasreg.h>
     57 #include <acorn32/podulebus/sfasvar.h>
     58 #include <acorn32/podulebus/cscreg.h>
     59 #include <acorn32/podulebus/cscvar.h>
     60 #include <dev/podulebus/podules.h>
     61 #include <dev/podulebus/powerromreg.h>
     62 
     63 void cscattach   __P((struct device *, struct device *, void *));
     64 int  cscmatch    __P((struct device *, struct cfdata *, void *));
     65 
     66 CFATTACH_DECL(csc, sizeof(struct csc_softc),
     67 	cscmatch, cscattach, NULL, NULL);
     68 
     69 int csc_intr		__P((void *arg));
     70 int csc_setup_dma	__P((struct sfas_softc *sc, void *ptr, int len,
     71 			      int mode));
     72 int csc_build_dma_chain	__P((struct sfas_softc *sc,
     73 			      struct sfas_dma_chain *chain, void *p, int l));
     74 int csc_need_bump	__P((struct sfas_softc *sc, void *ptr, int len));
     75 void csc_led		__P((struct sfas_softc *sc, int mode));
     76 
     77 /*
     78  * if we are a Cumana SCSI-2 card
     79  */
     80 int
     81 cscmatch(pdp, cf, auxp)
     82 	struct device	*pdp;
     83 	struct cfdata	*cf;
     84 	void		*auxp;
     85 {
     86 	struct podule_attach_args *pa = (struct podule_attach_args *)auxp;
     87 
     88 	/* Look for the card */
     89 	if (pa->pa_product == PODULE_CUMANA_SCSI2)
     90 		return 1;
     91 
     92 	/* PowerROM */
     93         if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
     94             podulebus_initloader(pa) == 0 &&
     95             podloader_callloader(pa, 0, 0) == PRID_CUMANA_SCSI2)
     96                 return 1;
     97 
     98 	return 0;
     99 }
    100 
    101 void
    102 cscattach(pdp, dp, auxp)
    103 	struct device	*pdp;
    104 	struct device	*dp;
    105 	void		*auxp;
    106 {
    107 	struct csc_softc *sc = (struct csc_softc *)dp;
    108 	struct podule_attach_args  *pa;
    109 	csc_regmap_p	   rp = &sc->sc_regmap;
    110 	vu_char		  *fas;
    111 	int loop;
    112 
    113 	pa = (struct podule_attach_args *)auxp;
    114 
    115 	if (pa->pa_podule_number == -1)
    116 		panic("Podule has disappeared !");
    117 
    118 	sc->sc_specific.sc_podule_number = pa->pa_podule_number;
    119 	sc->sc_specific.sc_podule = pa->pa_podule;
    120 	sc->sc_specific.sc_iobase =
    121 	    (vu_char *)sc->sc_specific.sc_podule->mod_base;
    122 
    123 	rp->status0 = &sc->sc_specific.sc_iobase[CSC_STATUS0];
    124 	rp->alatch = &sc->sc_specific.sc_iobase[CSC_ALATCH];
    125 	rp->dack = (vu_short *)&sc->sc_specific.sc_iobase[CSC_DACK];
    126 	fas = &sc->sc_specific.sc_iobase[CSC_FAS_OFFSET_BASE];
    127 
    128 	rp->FAS216.sfas_tc_low	= &fas[CSC_FAS_OFFSET_TCL];
    129 	rp->FAS216.sfas_tc_mid	= &fas[CSC_FAS_OFFSET_TCM];
    130 	rp->FAS216.sfas_fifo	= &fas[CSC_FAS_OFFSET_FIFO];
    131 	rp->FAS216.sfas_command	= &fas[CSC_FAS_OFFSET_COMMAND];
    132 	rp->FAS216.sfas_dest_id	= &fas[CSC_FAS_OFFSET_DESTID];
    133 	rp->FAS216.sfas_timeout	= &fas[CSC_FAS_OFFSET_TIMEOUT];
    134 	rp->FAS216.sfas_syncper	= &fas[CSC_FAS_OFFSET_PERIOD];
    135 	rp->FAS216.sfas_syncoff	= &fas[CSC_FAS_OFFSET_OFFSET];
    136 	rp->FAS216.sfas_config1	= &fas[CSC_FAS_OFFSET_CONFIG1];
    137 	rp->FAS216.sfas_clkconv	= &fas[CSC_FAS_OFFSET_CLKCONV];
    138 	rp->FAS216.sfas_test	= &fas[CSC_FAS_OFFSET_TEST];
    139 	rp->FAS216.sfas_config2	= &fas[CSC_FAS_OFFSET_CONFIG2];
    140 	rp->FAS216.sfas_config3	= &fas[CSC_FAS_OFFSET_CONFIG3];
    141 	rp->FAS216.sfas_tc_high	= &fas[CSC_FAS_OFFSET_TCH];
    142 	rp->FAS216.sfas_fifo_bot = &fas[CSC_FAS_OFFSET_FIFOBOT];
    143 
    144 	sc->sc_softc.sc_fas	= (sfas_regmap_p)rp;
    145 	sc->sc_softc.sc_spec	= &sc->sc_specific;
    146 
    147 	sc->sc_softc.sc_led	= csc_led;
    148 
    149 	sc->sc_softc.sc_setup_dma	= csc_setup_dma;
    150 	sc->sc_softc.sc_build_dma_chain = csc_build_dma_chain;
    151 	sc->sc_softc.sc_need_bump	= csc_need_bump;
    152 
    153 	sc->sc_softc.sc_clock_freq   = 8;   /* Cumana runs at 8MHz */
    154 	sc->sc_softc.sc_timeout      = 250;  /* Set default timeout to 250ms */
    155 	sc->sc_softc.sc_config_flags = SFAS_NO_DMA /*| SFAS_NF_DEBUG*/;
    156 	sc->sc_softc.sc_host_id      = 7;    /* Should check the jumpers */
    157 
    158 	sc->sc_softc.sc_bump_sz = NBPG;
    159 	sc->sc_softc.sc_bump_pa = 0x0;
    160 
    161 	sfasinitialize((struct sfas_softc *)sc);
    162 
    163 	sc->sc_softc.sc_adapter.adapt_dev = &sc->sc_softc.sc_dev;
    164 	sc->sc_softc.sc_adapter.adapt_nchannels = 1;
    165 	sc->sc_softc.sc_adapter.adapt_openings = 7;
    166 	sc->sc_softc.sc_adapter.adapt_max_periph = 1;
    167 	sc->sc_softc.sc_adapter.adapt_ioctl = NULL;
    168 	sc->sc_softc.sc_adapter.adapt_minphys = sfas_minphys;
    169 	sc->sc_softc.sc_adapter.adapt_request = sfas_scsi_request;
    170 
    171 	sc->sc_softc.sc_channel.chan_adapter = &sc->sc_softc.sc_adapter;
    172 	sc->sc_softc.sc_channel.chan_bustype = &scsi_bustype;
    173 	sc->sc_softc.sc_channel.chan_channel = 0;
    174 	sc->sc_softc.sc_channel.chan_ntargets = 8;
    175 	sc->sc_softc.sc_channel.chan_nluns = 8;
    176 	sc->sc_softc.sc_channel.chan_id = sc->sc_softc.sc_host_id;
    177 
    178 	/* Provide an override for the host id */
    179 	(void)get_bootconf_option(boot_args, "csc.hostid",
    180 	    BOOTOPT_TYPE_INT, &sc->sc_softc.sc_channel.chan_id);
    181 
    182 	printf(": host=%d", sc->sc_softc.sc_channel.chan_id);
    183 
    184 	/* initialise the alatch */
    185 	sc->sc_specific.sc_alatch_defs = (CSC_POLL?0:CSC_ALATCH_DEFS_INTEN);
    186 	for (loop = 0; loop < 8; loop ++) {
    187 	    if(loop != 3)
    188 		*rp->alatch = (loop << 1) |
    189 		    ((sc->sc_specific.sc_alatch_defs & (1 << loop))?1:0);
    190 	}
    191 
    192 #if CSC_POLL == 0
    193 	evcnt_attach_dynamic(&sc->sc_softc.sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    194 	    dp->dv_xname, "intr");
    195 	sc->sc_softc.sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
    196 	    csc_intr, &sc->sc_softc, &sc->sc_softc.sc_intrcnt);
    197 	if (sc->sc_softc.sc_ih == NULL)
    198 	    panic("%s: Cannot install IRQ handler", dp->dv_xname);
    199 #else
    200 	printf(" polling");
    201 	sc->sc_softc.sc_adapter.adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
    202 #endif
    203 	printf("\n");
    204 
    205 	/* attach all scsi units on us */
    206 	config_found(dp, &sc->sc_softc.sc_channel, scsiprint);
    207 }
    208 
    209 
    210 int
    211 csc_intr(arg)
    212 	void *arg;
    213 {
    214 	struct sfas_softc *dev = arg;
    215 	csc_regmap_p	      rp;
    216 	int		      quickints;
    217 
    218 	rp = (csc_regmap_p)dev->sc_fas;
    219 
    220 	if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
    221 		quickints = 16;
    222 		do {
    223 			dev->sc_status = *rp->FAS216.sfas_status;
    224 			dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
    225 
    226 			if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    227 				dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
    228 				dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
    229 			}
    230 			sfasintr(dev);
    231 
    232 		} while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
    233 			&& --quickints);
    234 	}
    235 
    236 	return(0);	/* Pass interrupt on down the chain */
    237 }
    238 
    239 /* Load transfer address into dma register */
    240 void
    241 csc_set_dma_adr(sc, ptr)
    242 	struct sfas_softc *sc;
    243 	void		 *ptr;
    244 {
    245 	return;
    246 }
    247 
    248 /* Set DMA transfer counter */
    249 void
    250 csc_set_dma_tc(sc, len)
    251 	struct sfas_softc *sc;
    252 	unsigned int	  len;
    253 {
    254 	*sc->sc_fas->sfas_tc_low  = len; len >>= 8;
    255 	*sc->sc_fas->sfas_tc_mid  = len; len >>= 8;
    256 	*sc->sc_fas->sfas_tc_high = len;
    257 }
    258 
    259 /* Set DMA mode */
    260 void
    261 csc_set_dma_mode(sc, mode)
    262 	struct sfas_softc *sc;
    263 	int		  mode;
    264 {
    265 }
    266 
    267 /* Initialize DMA for transfer */
    268 int
    269 csc_setup_dma(sc, ptr, len, mode)
    270 	struct sfas_softc *sc;
    271 	void		 *ptr;
    272 	int		  len;
    273 	int		  mode;
    274 {
    275 	return(0);
    276 }
    277 
    278 /* Check if address and len is ok for DMA transfer */
    279 int
    280 csc_need_bump(sc, ptr, len)
    281 	struct sfas_softc *sc;
    282 	void		 *ptr;
    283 	int		  len;
    284 {
    285 	int	p;
    286 
    287 	p = (int)ptr & 0x03;
    288 
    289 	if (p) {
    290 		p = 4-p;
    291 
    292 		if (len < 256)
    293 			p = len;
    294 	}
    295 
    296 	return(p);
    297 }
    298 
    299 /* Interrupt driven routines */
    300 int
    301 csc_build_dma_chain(sc, chain, p, l)
    302 	struct sfas_softc	*sc;
    303 	struct sfas_dma_chain	*chain;
    304 	void			*p;
    305 	int			 l;
    306 {
    307 	return(0);
    308 }
    309 
    310 /* Turn on/off led */
    311 void
    312 csc_led(sc, mode)
    313 	struct sfas_softc *sc;
    314 	int		  mode;
    315 {
    316 	csc_regmap_p		rp;
    317 
    318 	rp = (csc_regmap_p)sc->sc_fas;
    319 
    320 	if (mode) {
    321 		sc->sc_led_status++;
    322 	} else {
    323 		if (sc->sc_led_status)
    324 			sc->sc_led_status--;
    325 	}
    326 }
    327