Home | History | Annotate | Line # | Download | only in pcmcia
aic_pcmcia.c revision 1.8.4.1.2.1
      1  1.8.4.1.2.1   thorpej /*	$NetBSD: aic_pcmcia.c,v 1.8.4.1.2.1 1999/06/21 01:18:45 thorpej Exp $	*/
      2          1.2   thorpej 
      3          1.2   thorpej /*
      4          1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5          1.2   thorpej  *
      6          1.2   thorpej  * Redistribution and use in source and binary forms, with or without
      7          1.2   thorpej  * modification, are permitted provided that the following conditions
      8          1.2   thorpej  * are met:
      9          1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     10          1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     11          1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     12          1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     13          1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     14          1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     15          1.2   thorpej  *    must display the following acknowledgement:
     16          1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     17          1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     18          1.2   thorpej  *    derived from this software without specific prior written permission.
     19          1.2   thorpej  *
     20          1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21          1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22          1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23          1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24          1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25          1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26          1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27          1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28          1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29          1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30          1.2   thorpej  */
     31          1.2   thorpej 
     32          1.2   thorpej #include <sys/param.h>
     33          1.2   thorpej #include <sys/systm.h>
     34          1.2   thorpej #include <sys/select.h>
     35          1.2   thorpej #include <sys/device.h>
     36          1.2   thorpej 
     37          1.2   thorpej #include <machine/cpu.h>
     38          1.2   thorpej #include <machine/bus.h>
     39          1.2   thorpej #include <machine/intr.h>
     40          1.2   thorpej 
     41          1.2   thorpej #include <dev/scsipi/scsipi_all.h>
     42          1.2   thorpej #include <dev/scsipi/scsipiconf.h>
     43          1.2   thorpej #include <dev/scsipi/scsi_all.h>
     44          1.2   thorpej 
     45          1.2   thorpej #include <dev/ic/aic6360var.h>
     46          1.2   thorpej 
     47          1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     48          1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     49          1.6  christos #include <dev/pcmcia/pcmciadevs.h>
     50          1.2   thorpej 
     51          1.2   thorpej int	aic_pcmcia_match __P((struct device *, struct cfdata *, void *));
     52          1.2   thorpej void	aic_pcmcia_attach __P((struct device *, struct device *, void *));
     53          1.2   thorpej 
     54          1.2   thorpej struct aic_pcmcia_softc {
     55          1.2   thorpej 	struct aic_softc sc_aic;		/* real "aic" softc */
     56          1.2   thorpej 
     57          1.2   thorpej 	/* PCMCIA-specific goo. */
     58          1.2   thorpej 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     59          1.2   thorpej 	int sc_io_window;			/* our i/o window */
     60          1.2   thorpej 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     61          1.2   thorpej 	void *sc_ih;				/* interrupt handler */
     62          1.2   thorpej };
     63          1.2   thorpej 
     64          1.2   thorpej struct cfattach aic_pcmcia_ca = {
     65          1.2   thorpej 	sizeof(struct aic_pcmcia_softc), aic_pcmcia_match, aic_pcmcia_attach
     66          1.2   thorpej };
     67          1.2   thorpej 
     68          1.8   thorpej int	aic_pcmcia_enable __P((void *, int));
     69          1.8   thorpej 
     70          1.7   thorpej struct aic_pcmcia_product {
     71          1.7   thorpej 	u_int32_t	app_vendor;		/* PCMCIA vendor ID */
     72          1.7   thorpej 	u_int32_t	app_product;		/* PCMCIA product ID */
     73          1.7   thorpej 	int		app_expfunc;		/* expected function number */
     74          1.7   thorpej 	const char	*app_name;		/* device name */
     75          1.7   thorpej } aic_pcmcia_products[] = {
     76          1.7   thorpej 	{ PCMCIA_VENDOR_ADAPTEC,	PCMCIA_PRODUCT_ADAPTEC_APA1460_1,
     77          1.7   thorpej 	  0,				PCMCIA_STR_ADAPTEC_APA1460_1 },
     78          1.7   thorpej 	{ PCMCIA_VENDOR_ADAPTEC,	PCMCIA_PRODUCT_ADAPTEC_APA1460_2,
     79          1.7   thorpej 	  0,				PCMCIA_STR_ADAPTEC_APA1460_2 },
     80  1.8.4.1.2.1   thorpej 	{ PCMCIA_VENDOR_NEWMEDIA,	PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER,
     81  1.8.4.1.2.1   thorpej 	  0,				PCMCIA_STR_NEWMEDIA_BUSTOASTER },
     82          1.7   thorpej 
     83          1.7   thorpej 	{ 0,				0,
     84          1.7   thorpej 	  0,				NULL },
     85          1.7   thorpej };
     86          1.7   thorpej 
     87          1.7   thorpej struct aic_pcmcia_product *aic_pcmcia_lookup __P((struct pcmcia_attach_args *));
     88          1.7   thorpej 
     89          1.7   thorpej struct aic_pcmcia_product *
     90          1.7   thorpej aic_pcmcia_lookup(pa)
     91          1.7   thorpej 	struct pcmcia_attach_args *pa;
     92          1.7   thorpej {
     93          1.7   thorpej 	struct aic_pcmcia_product *app;
     94          1.7   thorpej 
     95          1.7   thorpej 	for (app = aic_pcmcia_products; app->app_name != NULL; app++) {
     96          1.7   thorpej 		if (pa->manufacturer == app->app_vendor &&
     97          1.7   thorpej 		    pa->product == app->app_product &&
     98          1.7   thorpej 		    pa->pf->number == app->app_expfunc)
     99          1.7   thorpej 			return (app);
    100          1.7   thorpej 	}
    101          1.7   thorpej 	return (NULL);
    102          1.7   thorpej }
    103          1.7   thorpej 
    104          1.2   thorpej int
    105          1.2   thorpej aic_pcmcia_match(parent, match, aux)
    106          1.2   thorpej 	struct device *parent;
    107          1.3  drochner 	struct cfdata *match;
    108          1.2   thorpej 	void *aux;
    109          1.2   thorpej {
    110          1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    111          1.2   thorpej 
    112          1.7   thorpej 	if (aic_pcmcia_lookup(pa) != NULL)
    113          1.7   thorpej 		return (1);
    114          1.2   thorpej 	return (0);
    115          1.2   thorpej }
    116          1.2   thorpej 
    117          1.2   thorpej void
    118          1.2   thorpej aic_pcmcia_attach(parent, self, aux)
    119          1.2   thorpej 	struct device *parent, *self;
    120          1.2   thorpej 	void *aux;
    121          1.2   thorpej {
    122          1.2   thorpej 	struct aic_pcmcia_softc *psc = (void *)self;
    123          1.2   thorpej 	struct aic_softc *sc = &psc->sc_aic;
    124          1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    125          1.2   thorpej 	struct pcmcia_config_entry *cfe;
    126          1.2   thorpej 	struct pcmcia_function *pf = pa->pf;
    127          1.7   thorpej 	struct aic_pcmcia_product *app;
    128          1.2   thorpej 
    129          1.2   thorpej 	psc->sc_pf = pf;
    130          1.2   thorpej 
    131          1.4     enami 	for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
    132          1.4     enami 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    133          1.2   thorpej 		if (cfe->num_memspace != 0 ||
    134          1.2   thorpej 		    cfe->num_iospace != 1)
    135  1.8.4.1.2.1   thorpej 			continue;
    136  1.8.4.1.2.1   thorpej 
    137  1.8.4.1.2.1   thorpej 		/* The bustoaster has a default config as first
    138  1.8.4.1.2.1   thorpej 		 * entry, we don't want to use that. */
    139  1.8.4.1.2.1   thorpej 
    140  1.8.4.1.2.1   thorpej 		if (pa->manufacturer == PCMCIA_VENDOR_NEWMEDIA &&
    141  1.8.4.1.2.1   thorpej 		    pa->product == PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER &&
    142  1.8.4.1.2.1   thorpej 		    cfe->iospace[0].start == 0)
    143          1.2   thorpej 			continue;
    144          1.2   thorpej 
    145          1.2   thorpej 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    146          1.2   thorpej 		    cfe->iospace[0].length, 0, &psc->sc_pcioh) == 0)
    147          1.2   thorpej 			break;
    148          1.2   thorpej 	}
    149          1.2   thorpej 
    150          1.2   thorpej 	if (cfe == 0) {
    151          1.2   thorpej 		printf(": can't alloc i/o space\n");
    152          1.2   thorpej 		return;
    153          1.2   thorpej 	}
    154          1.2   thorpej 
    155          1.2   thorpej 	sc->sc_iot = psc->sc_pcioh.iot;
    156          1.2   thorpej 	sc->sc_ioh = psc->sc_pcioh.ioh;
    157          1.2   thorpej 
    158          1.2   thorpej 	/* Enable the card. */
    159          1.2   thorpej 	pcmcia_function_init(pf, cfe);
    160          1.2   thorpej 	if (pcmcia_function_enable(pf)) {
    161          1.2   thorpej 		printf(": function enable failed\n");
    162          1.2   thorpej 		return;
    163          1.2   thorpej 	}
    164          1.2   thorpej 
    165          1.2   thorpej 	/* Map in the io space */
    166          1.2   thorpej 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    167          1.2   thorpej 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    168          1.2   thorpej 		printf(": can't map i/o space\n");
    169          1.2   thorpej 		return;
    170          1.2   thorpej 	}
    171          1.2   thorpej 
    172          1.7   thorpej 	if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
    173          1.7   thorpej 		printf(": unable to detect chip!\n");
    174          1.7   thorpej 		return;
    175          1.7   thorpej 	}
    176          1.2   thorpej 
    177          1.7   thorpej 	app = aic_pcmcia_lookup(pa);
    178          1.7   thorpej 	if (app == NULL) {
    179          1.7   thorpej 		printf("\n");
    180          1.7   thorpej 		panic("aic_pcmcia_attach: impossible");
    181          1.6  christos 	}
    182          1.6  christos 
    183      1.8.4.1     perry #if 0	/* XXX power management broken somehow. */
    184          1.8   thorpej 	/* We can enable and disable the controller. */
    185          1.8   thorpej 	sc->sc_adapter.scsipi_enable = aic_pcmcia_enable;
    186          1.8   thorpej 
    187          1.8   thorpej 	/*
    188          1.8   thorpej 	 * Disable the pcmcia function now; we will be enbled again
    189          1.8   thorpej 	 * as the SCSI code adds references to probe for children.
    190          1.8   thorpej 	 */
    191          1.8   thorpej 	pcmcia_function_disable(pf);
    192          1.8   thorpej 
    193          1.7   thorpej 	printf(": %s\n", app->app_name);
    194      1.8.4.1     perry #else
    195      1.8.4.1     perry 	printf(": %s\n", app->app_name);
    196      1.8.4.1     perry 
    197      1.8.4.1     perry 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
    198      1.8.4.1     perry 	    aicintr, &psc->sc_aic);
    199      1.8.4.1     perry 	if (psc->sc_ih == NULL) {
    200      1.8.4.1     perry 		printf("%s: couldn't establish interrupt handler\n",
    201      1.8.4.1     perry 		    psc->sc_aic.sc_dev.dv_xname);
    202      1.8.4.1     perry 		return;
    203      1.8.4.1     perry 	}
    204      1.8.4.1     perry #endif
    205          1.2   thorpej 
    206          1.2   thorpej 	aicattach(sc);
    207          1.8   thorpej }
    208          1.8   thorpej 
    209          1.8   thorpej int
    210          1.8   thorpej aic_pcmcia_enable(arg, onoff)
    211          1.8   thorpej 	void *arg;
    212          1.8   thorpej 	int onoff;
    213          1.8   thorpej {
    214          1.8   thorpej 	struct aic_pcmcia_softc *psc = arg;
    215          1.2   thorpej 
    216          1.8   thorpej 	if (onoff) {
    217          1.8   thorpej 		/* Establish the interrupt handler. */
    218          1.8   thorpej 		psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
    219          1.8   thorpej 		    aicintr, &psc->sc_aic);
    220          1.8   thorpej 		if (psc->sc_ih == NULL) {
    221          1.8   thorpej 			printf("%s: couldn't establish interrupt handler\n",
    222          1.8   thorpej 			    psc->sc_aic.sc_dev.dv_xname);
    223          1.8   thorpej 			return (EIO);
    224          1.8   thorpej 		}
    225          1.8   thorpej 
    226          1.8   thorpej 		if (pcmcia_function_enable(psc->sc_pf)) {
    227          1.8   thorpej 			printf("%s: couldn't enable PCMCIA function\n",
    228          1.8   thorpej 			    psc->sc_aic.sc_dev.dv_xname);
    229          1.8   thorpej 			pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    230          1.8   thorpej 			return (EIO);
    231          1.8   thorpej 		}
    232          1.8   thorpej 	} else {
    233          1.8   thorpej 		pcmcia_function_disable(psc->sc_pf);
    234          1.8   thorpej 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    235          1.8   thorpej 	}
    236          1.8   thorpej 
    237          1.8   thorpej 	return (0);
    238          1.2   thorpej }
    239