Home | History | Annotate | Line # | Download | only in pci
i82365_pci.c revision 1.5
      1 /*	$NetBSD: i82365_pci.c,v 1.5 1998/11/30 19:47:17 jtk Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Marc Horowitz.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 
     37 #include <dev/ic/i82365reg.h>
     38 #include <dev/ic/i82365var.h>
     39 
     40 #include <dev/pci/pcivar.h>
     41 #include <dev/pci/pcireg.h>
     42 #include <dev/pci/pcidevs.h>
     43 
     44 /*
     45  * PCI constants.
     46  * XXX These should be in a common file!
     47  */
     48 #define	PCI_CBIO		0x10	/* Configuration Base IO Address */
     49 
     50 int	pcic_pci_match __P((struct device *, struct cfdata *, void *));
     51 void	pcic_pci_attach __P((struct device *, struct device *, void *));
     52 
     53 void	*pcic_pci_chip_intr_establish __P((pcmcia_chipset_handle_t,
     54 	    struct pcmcia_function *, int, int (*) (void *), void *));
     55 void	pcic_pci_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
     56 
     57 struct cfattach pcic_pci_ca = {
     58 	sizeof(struct pcic_softc), pcic_pci_match, pcic_pci_attach
     59 };
     60 
     61 static struct pcmcia_chip_functions pcic_pci_functions = {
     62 	pcic_chip_mem_alloc,
     63 	pcic_chip_mem_free,
     64 	pcic_chip_mem_map,
     65 	pcic_chip_mem_unmap,
     66 
     67 	pcic_chip_io_alloc,
     68 	pcic_chip_io_free,
     69 	pcic_chip_io_map,
     70 	pcic_chip_io_unmap,
     71 
     72 	pcic_pci_chip_intr_establish,
     73 	pcic_pci_chip_intr_disestablish,
     74 
     75 	pcic_chip_socket_enable,
     76 	pcic_chip_socket_disable,
     77 };
     78 
     79 int
     80 pcic_pci_match(parent, match, aux)
     81 	struct device *parent;
     82 	struct cfdata  *match;
     83 	void *aux;
     84 {
     85 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
     86 
     87 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
     88 		return (0);
     89 
     90 	switch (PCI_PRODUCT(pa->pa_id)) {
     91 	case PCI_PRODUCT_CIRRUS_CL_PD6729:
     92 		break;
     93 	default:
     94 		return (0);
     95 	}
     96 
     97 	return (1);
     98 }
     99 
    100 void
    101 pcic_pci_attach(parent, self, aux)
    102 	struct device *parent, *self;
    103 	void *aux;
    104 {
    105 	struct pcic_softc *sc = (void *) self;
    106 	struct pci_attach_args *pa = aux;
    107 	pci_chipset_tag_t pc = pa->pa_pc;
    108 	bus_space_tag_t memt = pa->pa_memt;
    109 	bus_space_handle_t memh;
    110 	pci_intr_handle_t ih;
    111 	char *model;
    112 	const char *intrstr = NULL;
    113 
    114 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    115 	    &sc->iot, &sc->ioh, NULL, NULL)) {
    116 		printf(": can't map i/o space\n");
    117 		return;
    118 	}
    119 
    120 	/*
    121 	 * XXX need some memory for mapping pcmcia cards into. Ideally, this
    122 	 * would be completely dynamic.  Practically this doesn't work,
    123 	 * because the extent mapper doesn't know about all the devices all
    124 	 * the time.  With ISA we could finesse the issue by specifying the
    125 	 * memory region in the config line.  We can't do that here, so we
    126 	 * cheat for now. Jason Thorpe, you are my Savior, come up with a fix
    127 	 * :-)
    128 	 */
    129 
    130 	/* Map mem space. */
    131 	if (bus_space_map(memt, 0xd0000, 0x4000, 0, &memh))
    132 		panic("pcic_isa_attach: can't map i/o space");
    133 
    134 	sc->membase = 0xd0000;
    135 	sc->subregionmask = (1 << (0x4000 / PCIC_MEM_PAGESIZE)) - 1;
    136 
    137 	/* same deal for io allocation */
    138 
    139 	sc->iobase = 0x400;
    140 	sc->iosize = 0xbff;
    141 
    142 	/* end XXX */
    143 
    144 	sc->intr_est = pc;
    145 	sc->pct = (pcmcia_chipset_tag_t) & pcic_pci_functions;
    146 
    147 	sc->memt = memt;
    148 	sc->memh = memh;
    149 
    150 	switch (PCI_PRODUCT(pa->pa_id)) {
    151 	case PCI_PRODUCT_CIRRUS_CL_PD6729:
    152 		model = "Cirrus Logic PD6729 PCMCIA controller";
    153 		break;
    154 	default:
    155 		model = "Model unknown";
    156 		break;
    157 	}
    158 
    159 	printf(": %s\n", model);
    160 
    161 	/* Enable the card. */
    162 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    163 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    164 	    PCI_COMMAND_MASTER_ENABLE);
    165 
    166 	pcic_attach(sc);
    167 
    168 	/* Map and establish the interrupt. */
    169 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    170 	    pa->pa_intrline, &ih)) {
    171 		printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
    172 		return;
    173 	}
    174 	intrstr = pci_intr_string(pc, ih);
    175 	sc->ih = pci_intr_establish(pc, ih, IPL_NET, pcic_intr, sc);
    176 	if (sc->ih == NULL) {
    177 		printf("%s: couldn't establish interrupt",
    178 		       sc->dev.dv_xname);
    179 		if (intrstr != NULL)
    180 			printf(" at %s", intrstr);
    181 		printf("\n");
    182 		return;
    183 	}
    184 	printf("%s: interrupting at %s\n", sc->dev.dv_xname, intrstr);
    185 
    186 	pcic_attach_sockets(sc);
    187 }
    188 
    189 /*
    190  * XXX This is almost totally wrong!  We need to map to PCI interupts,
    191  * XXX which themselves map to somthing else.
    192  */
    193 
    194 #include <dev/isa/isareg.h>
    195 #include <dev/isa/isavar.h>
    196 
    197 void *
    198 pcic_pci_chip_intr_establish(pch, pf, ipl, fct, arg)
    199 	pcmcia_chipset_handle_t pch;
    200 	struct pcmcia_function *pf;
    201 	int ipl;
    202 	int (*fct) (void *);
    203 	void *arg;
    204 {
    205 	struct pcic_handle *h = (struct pcic_handle *) pch;
    206 	pci_chipset_tag_t pc = h->sc->intr_est;
    207 	int irq;
    208 	pci_intr_handle_t piht;
    209 	void *ih;
    210 	int reg;
    211 
    212 	/*
    213 	 * XXX this will work for x86, but is guaranteed to lose elsewhere.
    214 	 * Hopefully Jason can bail me out of this one, too.
    215 	 */
    216 
    217 	isa_intr_alloc(NULL, 0xffff, IST_PULSE, &irq);
    218 
    219 	if (pci_intr_map(pc, pci_make_tag(NULL, 0, 0, 0),
    220 	    1, irq, &piht)) {
    221 		printf("%s: couldn't map interrupt\n", h->sc->dev.dv_xname);
    222 		return (NULL);
    223 	}
    224 	if ((ih = pci_intr_establish(pc, piht, ipl, fct,
    225 	    arg)) == NULL)
    226 		return (NULL);
    227 
    228 	reg = pcic_read(h, PCIC_INTR);
    229 	reg |= PCIC_INTR_ENABLE;
    230 	reg |= irq;
    231 	pcic_write(h, PCIC_INTR, reg);
    232 
    233 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    234 
    235 	return (ih);
    236 }
    237 
    238 void
    239 pcic_pci_chip_intr_disestablish(pch, ih)
    240 	pcmcia_chipset_handle_t pch;
    241 	void *ih;
    242 {
    243 	struct pcic_handle *h = (struct pcic_handle *) pch;
    244 	pci_chipset_tag_t pc = h->sc->intr_est;
    245 
    246 	pci_intr_disestablish(pc, ih);
    247 }
    248