Home | History | Annotate | Line # | Download | only in pci
grackle.c revision 1.8
      1 /*	$NetBSD: grackle.c,v 1.8 2003/07/15 02:43:33 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  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. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: grackle.c,v 1.8 2003/07/15 02:43:33 lukem Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/systm.h>
     35 
     36 #include <dev/pci/pcivar.h>
     37 #include <dev/ofw/openfirm.h>
     38 #include <dev/ofw/ofw_pci.h>
     39 
     40 #include <machine/autoconf.h>
     41 
     42 struct grackle_softc {
     43 	struct device sc_dev;
     44 	struct pci_bridge sc_pc;
     45 };
     46 
     47 void grackle_attach __P((struct device *, struct device *, void *));
     48 int grackle_match __P((struct device *, struct cfdata *, void *));
     49 int grackle_print __P((void *, const char *));
     50 
     51 pcireg_t grackle_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
     52 void grackle_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
     53 
     54 CFATTACH_DECL(grackle, sizeof(struct grackle_softc),
     55     grackle_match, grackle_attach, NULL, NULL);
     56 
     57 int
     58 grackle_match(parent, cf, aux)
     59 	struct device *parent;
     60 	struct cfdata *cf;
     61 	void *aux;
     62 {
     63 	struct confargs *ca = aux;
     64 	char compat[32];
     65 
     66 	if (strcmp(ca->ca_name, "pci") != 0)
     67 		return 0;
     68 
     69 	memset(compat, 0, sizeof(compat));
     70 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     71 	if (strcmp(compat, "grackle") != 0)
     72 		return 0;
     73 
     74 	return 1;
     75 }
     76 
     77 #define GRACKLE_ADDR 0xfec00000
     78 #define GRACKLE_DATA 0xfee00000
     79 
     80 void
     81 grackle_attach(parent, self, aux)
     82 	struct device *parent, *self;
     83 	void *aux;
     84 {
     85 	struct grackle_softc *sc = (void *)self;
     86 	pci_chipset_tag_t pc = &sc->sc_pc;
     87 	struct confargs *ca = aux;
     88 	struct pcibus_attach_args pba;
     89 	int len, node = ca->ca_node;
     90 	u_int32_t busrange[2];
     91 	struct ranges {
     92 		u_int32_t pci_hi, pci_mid, pci_lo;
     93 		u_int32_t host;
     94 		u_int32_t size_hi, size_lo;
     95 	} ranges[6], *rp = ranges;
     96 
     97 	printf("\n");
     98 
     99 	/* PCI bus number */
    100 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
    101 		return;
    102 
    103 	pc->node = node;
    104 	pc->addr = mapiodev(GRACKLE_ADDR, 4);
    105 	pc->data = mapiodev(GRACKLE_DATA, 4);
    106 	pc->bus = busrange[0];
    107 	pc->conf_read = grackle_conf_read;
    108 	pc->conf_write = grackle_conf_write;
    109 	pc->memt = (bus_space_tag_t)0;
    110 
    111 	/* find i/o tag */
    112 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
    113 	if (len == -1)
    114 		return;
    115 	while (len >= sizeof(ranges[0])) {
    116 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
    117 		     OFW_PCI_PHYS_HI_SPACE_IO)
    118 			pc->iot = (bus_space_tag_t)rp->host;
    119 		len -= sizeof(ranges[0]);
    120 		rp++;
    121 	}
    122 
    123 	memset(&pba, 0, sizeof(pba));
    124 	pba.pba_busname = "pci";
    125 	pba.pba_memt = pc->memt;
    126 	pba.pba_iot = pc->iot;
    127 	pba.pba_dmat = &pci_bus_dma_tag;
    128 	pba.pba_dmat64 = NULL;
    129 	pba.pba_bus = pc->bus;
    130 	pba.pba_bridgetag = NULL;
    131 	pba.pba_pc = pc;
    132 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    133 
    134 	config_found(self, &pba, grackle_print);
    135 }
    136 
    137 int
    138 grackle_print(aux, pnp)
    139 	void *aux;
    140 	const char *pnp;
    141 {
    142 	struct pcibus_attach_args *pa = aux;
    143 
    144 	if (pnp)
    145 		aprint_normal("%s at %s", pa->pba_busname, pnp);
    146 	aprint_normal(" bus %d", pa->pba_bus);
    147 	return UNCONF;
    148 }
    149 
    150 pcireg_t
    151 grackle_conf_read(pc, tag, reg)
    152 	pci_chipset_tag_t pc;
    153 	pcitag_t tag;
    154 	int reg;
    155 {
    156 	pcireg_t data;
    157 	int s;
    158 
    159 	s = splhigh();
    160 
    161 	out32rb(pc->addr, tag | reg);
    162 	data = 0xffffffff;
    163 	if (!badaddr(pc->data, 4))
    164 		data = in32rb(pc->data);
    165 	out32rb(pc->addr, 0);
    166 
    167 	splx(s);
    168 
    169 	return data;
    170 }
    171 
    172 void
    173 grackle_conf_write(pc, tag, reg, data)
    174 	pci_chipset_tag_t pc;
    175 	pcitag_t tag;
    176 	int reg;
    177 	pcireg_t data;
    178 {
    179 	int s;
    180 
    181 	s = splhigh();
    182 
    183 	out32rb(pc->addr, tag | reg);
    184 	out32rb(pc->data, data);
    185 	out32rb(pc->addr, 0);
    186 
    187 	splx(s);
    188 }
    189