Home | History | Annotate | Line # | Download | only in pci
i82365_pci.c revision 1.6
      1  1.6  msaitoh /*	$NetBSD: i82365_pci.c,v 1.6 1998/12/17 17:45:08 msaitoh 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/types.h>
     33  1.2  thorpej #include <sys/param.h>
     34  1.2  thorpej #include <sys/systm.h>
     35  1.2  thorpej #include <sys/device.h>
     36  1.2  thorpej 
     37  1.2  thorpej #include <dev/ic/i82365reg.h>
     38  1.2  thorpej #include <dev/ic/i82365var.h>
     39  1.2  thorpej 
     40  1.2  thorpej #include <dev/pci/pcivar.h>
     41  1.2  thorpej #include <dev/pci/pcireg.h>
     42  1.2  thorpej #include <dev/pci/pcidevs.h>
     43  1.2  thorpej 
     44  1.2  thorpej /*
     45  1.2  thorpej  * PCI constants.
     46  1.2  thorpej  * XXX These should be in a common file!
     47  1.2  thorpej  */
     48  1.2  thorpej #define	PCI_CBIO		0x10	/* Configuration Base IO Address */
     49  1.2  thorpej 
     50  1.2  thorpej int	pcic_pci_match __P((struct device *, struct cfdata *, void *));
     51  1.2  thorpej void	pcic_pci_attach __P((struct device *, struct device *, void *));
     52  1.2  thorpej 
     53  1.2  thorpej void	*pcic_pci_chip_intr_establish __P((pcmcia_chipset_handle_t,
     54  1.2  thorpej 	    struct pcmcia_function *, int, int (*) (void *), void *));
     55  1.2  thorpej void	pcic_pci_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
     56  1.2  thorpej 
     57  1.2  thorpej struct cfattach pcic_pci_ca = {
     58  1.2  thorpej 	sizeof(struct pcic_softc), pcic_pci_match, pcic_pci_attach
     59  1.2  thorpej };
     60  1.2  thorpej 
     61  1.2  thorpej static struct pcmcia_chip_functions pcic_pci_functions = {
     62  1.2  thorpej 	pcic_chip_mem_alloc,
     63  1.2  thorpej 	pcic_chip_mem_free,
     64  1.2  thorpej 	pcic_chip_mem_map,
     65  1.2  thorpej 	pcic_chip_mem_unmap,
     66  1.2  thorpej 
     67  1.2  thorpej 	pcic_chip_io_alloc,
     68  1.2  thorpej 	pcic_chip_io_free,
     69  1.2  thorpej 	pcic_chip_io_map,
     70  1.2  thorpej 	pcic_chip_io_unmap,
     71  1.2  thorpej 
     72  1.2  thorpej 	pcic_pci_chip_intr_establish,
     73  1.2  thorpej 	pcic_pci_chip_intr_disestablish,
     74  1.2  thorpej 
     75  1.2  thorpej 	pcic_chip_socket_enable,
     76  1.2  thorpej 	pcic_chip_socket_disable,
     77  1.2  thorpej };
     78  1.2  thorpej 
     79  1.2  thorpej int
     80  1.2  thorpej pcic_pci_match(parent, match, aux)
     81  1.2  thorpej 	struct device *parent;
     82  1.2  thorpej 	struct cfdata  *match;
     83  1.2  thorpej 	void *aux;
     84  1.2  thorpej {
     85  1.2  thorpej 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
     86  1.2  thorpej 
     87  1.2  thorpej 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
     88  1.2  thorpej 		return (0);
     89  1.2  thorpej 
     90  1.2  thorpej 	switch (PCI_PRODUCT(pa->pa_id)) {
     91  1.5      jtk 	case PCI_PRODUCT_CIRRUS_CL_PD6729:
     92  1.2  thorpej 		break;
     93  1.2  thorpej 	default:
     94  1.2  thorpej 		return (0);
     95  1.2  thorpej 	}
     96  1.2  thorpej 
     97  1.2  thorpej 	return (1);
     98  1.2  thorpej }
     99  1.2  thorpej 
    100  1.2  thorpej void
    101  1.2  thorpej pcic_pci_attach(parent, self, aux)
    102  1.2  thorpej 	struct device *parent, *self;
    103  1.2  thorpej 	void *aux;
    104  1.2  thorpej {
    105  1.2  thorpej 	struct pcic_softc *sc = (void *) self;
    106  1.2  thorpej 	struct pci_attach_args *pa = aux;
    107  1.2  thorpej 	pci_chipset_tag_t pc = pa->pa_pc;
    108  1.2  thorpej 	bus_space_tag_t memt = pa->pa_memt;
    109  1.2  thorpej 	bus_space_handle_t memh;
    110  1.2  thorpej 	pci_intr_handle_t ih;
    111  1.2  thorpej 	char *model;
    112  1.2  thorpej 	const char *intrstr = NULL;
    113  1.2  thorpej 
    114  1.2  thorpej 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    115  1.2  thorpej 	    &sc->iot, &sc->ioh, NULL, NULL)) {
    116  1.2  thorpej 		printf(": can't map i/o space\n");
    117  1.2  thorpej 		return;
    118  1.2  thorpej 	}
    119  1.2  thorpej 
    120  1.2  thorpej 	/*
    121  1.2  thorpej 	 * XXX need some memory for mapping pcmcia cards into. Ideally, this
    122  1.2  thorpej 	 * would be completely dynamic.  Practically this doesn't work,
    123  1.2  thorpej 	 * because the extent mapper doesn't know about all the devices all
    124  1.2  thorpej 	 * the time.  With ISA we could finesse the issue by specifying the
    125  1.2  thorpej 	 * memory region in the config line.  We can't do that here, so we
    126  1.2  thorpej 	 * cheat for now. Jason Thorpe, you are my Savior, come up with a fix
    127  1.2  thorpej 	 * :-)
    128  1.2  thorpej 	 */
    129  1.2  thorpej 
    130  1.2  thorpej 	/* Map mem space. */
    131  1.2  thorpej 	if (bus_space_map(memt, 0xd0000, 0x4000, 0, &memh))
    132  1.6  msaitoh 		panic("pcic_pci_attach: can't map mem space");
    133  1.2  thorpej 
    134  1.2  thorpej 	sc->membase = 0xd0000;
    135  1.2  thorpej 	sc->subregionmask = (1 << (0x4000 / PCIC_MEM_PAGESIZE)) - 1;
    136  1.2  thorpej 
    137  1.2  thorpej 	/* same deal for io allocation */
    138  1.2  thorpej 
    139  1.2  thorpej 	sc->iobase = 0x400;
    140  1.2  thorpej 	sc->iosize = 0xbff;
    141  1.2  thorpej 
    142  1.2  thorpej 	/* end XXX */
    143  1.2  thorpej 
    144  1.2  thorpej 	sc->intr_est = pc;
    145  1.2  thorpej 	sc->pct = (pcmcia_chipset_tag_t) & pcic_pci_functions;
    146  1.2  thorpej 
    147  1.2  thorpej 	sc->memt = memt;
    148  1.2  thorpej 	sc->memh = memh;
    149  1.2  thorpej 
    150  1.2  thorpej 	switch (PCI_PRODUCT(pa->pa_id)) {
    151  1.5      jtk 	case PCI_PRODUCT_CIRRUS_CL_PD6729:
    152  1.5      jtk 		model = "Cirrus Logic PD6729 PCMCIA controller";
    153  1.2  thorpej 		break;
    154  1.2  thorpej 	default:
    155  1.2  thorpej 		model = "Model unknown";
    156  1.2  thorpej 		break;
    157  1.2  thorpej 	}
    158  1.2  thorpej 
    159  1.2  thorpej 	printf(": %s\n", model);
    160  1.2  thorpej 
    161  1.2  thorpej 	/* Enable the card. */
    162  1.2  thorpej 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    163  1.2  thorpej 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    164  1.2  thorpej 	    PCI_COMMAND_MASTER_ENABLE);
    165  1.2  thorpej 
    166  1.2  thorpej 	pcic_attach(sc);
    167  1.2  thorpej 
    168  1.2  thorpej 	/* Map and establish the interrupt. */
    169  1.2  thorpej 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    170  1.2  thorpej 	    pa->pa_intrline, &ih)) {
    171  1.2  thorpej 		printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
    172  1.2  thorpej 		return;
    173  1.2  thorpej 	}
    174  1.2  thorpej 	intrstr = pci_intr_string(pc, ih);
    175  1.2  thorpej 	sc->ih = pci_intr_establish(pc, ih, IPL_NET, pcic_intr, sc);
    176  1.2  thorpej 	if (sc->ih == NULL) {
    177  1.2  thorpej 		printf("%s: couldn't establish interrupt",
    178  1.2  thorpej 		       sc->dev.dv_xname);
    179  1.2  thorpej 		if (intrstr != NULL)
    180  1.2  thorpej 			printf(" at %s", intrstr);
    181  1.2  thorpej 		printf("\n");
    182  1.2  thorpej 		return;
    183  1.2  thorpej 	}
    184  1.2  thorpej 	printf("%s: interrupting at %s\n", sc->dev.dv_xname, intrstr);
    185  1.2  thorpej 
    186  1.2  thorpej 	pcic_attach_sockets(sc);
    187  1.2  thorpej }
    188  1.2  thorpej 
    189  1.2  thorpej /*
    190  1.2  thorpej  * XXX This is almost totally wrong!  We need to map to PCI interupts,
    191  1.2  thorpej  * XXX which themselves map to somthing else.
    192  1.2  thorpej  */
    193  1.2  thorpej 
    194  1.2  thorpej #include <dev/isa/isareg.h>
    195  1.2  thorpej #include <dev/isa/isavar.h>
    196  1.2  thorpej 
    197  1.2  thorpej void *
    198  1.2  thorpej pcic_pci_chip_intr_establish(pch, pf, ipl, fct, arg)
    199  1.2  thorpej 	pcmcia_chipset_handle_t pch;
    200  1.2  thorpej 	struct pcmcia_function *pf;
    201  1.2  thorpej 	int ipl;
    202  1.2  thorpej 	int (*fct) (void *);
    203  1.2  thorpej 	void *arg;
    204  1.2  thorpej {
    205  1.2  thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    206  1.3     matt 	pci_chipset_tag_t pc = h->sc->intr_est;
    207  1.2  thorpej 	int irq;
    208  1.2  thorpej 	pci_intr_handle_t piht;
    209  1.2  thorpej 	void *ih;
    210  1.2  thorpej 	int reg;
    211  1.2  thorpej 
    212  1.2  thorpej 	/*
    213  1.2  thorpej 	 * XXX this will work for x86, but is guaranteed to lose elsewhere.
    214  1.2  thorpej 	 * Hopefully Jason can bail me out of this one, too.
    215  1.2  thorpej 	 */
    216  1.2  thorpej 
    217  1.2  thorpej 	isa_intr_alloc(NULL, 0xffff, IST_PULSE, &irq);
    218  1.2  thorpej 
    219  1.3     matt 	if (pci_intr_map(pc, pci_make_tag(NULL, 0, 0, 0),
    220  1.2  thorpej 	    1, irq, &piht)) {
    221  1.2  thorpej 		printf("%s: couldn't map interrupt\n", h->sc->dev.dv_xname);
    222  1.2  thorpej 		return (NULL);
    223  1.2  thorpej 	}
    224  1.3     matt 	if ((ih = pci_intr_establish(pc, piht, ipl, fct,
    225  1.2  thorpej 	    arg)) == NULL)
    226  1.2  thorpej 		return (NULL);
    227  1.2  thorpej 
    228  1.2  thorpej 	reg = pcic_read(h, PCIC_INTR);
    229  1.2  thorpej 	reg |= PCIC_INTR_ENABLE;
    230  1.2  thorpej 	reg |= irq;
    231  1.2  thorpej 	pcic_write(h, PCIC_INTR, reg);
    232  1.2  thorpej 
    233  1.2  thorpej 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    234  1.2  thorpej 
    235  1.2  thorpej 	return (ih);
    236  1.2  thorpej }
    237  1.2  thorpej 
    238  1.2  thorpej void
    239  1.2  thorpej pcic_pci_chip_intr_disestablish(pch, ih)
    240  1.2  thorpej 	pcmcia_chipset_handle_t pch;
    241  1.2  thorpej 	void *ih;
    242  1.2  thorpej {
    243  1.2  thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    244  1.3     matt 	pci_chipset_tag_t pc = h->sc->intr_est;
    245  1.2  thorpej 
    246  1.3     matt 	pci_intr_disestablish(pc, ih);
    247  1.2  thorpej }
    248