Home | History | Annotate | Line # | Download | only in pci
u3.c revision 1.1.28.2
      1 /*
      2  * Copyright 2006 Kyma Systems LLC.
      3  * All rights reserved.
      4  *
      5  * Written by Sanjay Lal <sanjayl (at) kymasys.com>
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Kyma Systems LLC.
     19  * 4. The name of Kyma Systems LLC may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY KYMA SYSTEMS LLC ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL KYMA SYSTEMS LLC
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 
     38 #include <sys/param.h>
     39 #include <sys/device.h>
     40 #include <sys/systm.h>
     41 
     42 #include <dev/pci/pcivar.h>
     43 #include <dev/ofw/openfirm.h>
     44 #include <dev/ofw/ofw_pci.h>
     45 
     46 #include <machine/autoconf.h>
     47 
     48 struct ibmcpc_softc
     49 {
     50 	struct device sc_dev;
     51 	struct genppc_pci_chipset sc_pc[8];
     52 };
     53 
     54 static void ibmcpc_attach(struct device *, struct device *, void *);
     55 static int ibmcpc_match(struct device *, struct cfdata *, void *);
     56 
     57 static pcireg_t ibmcpc_conf_read(void *, pcitag_t, int);
     58 static void ibmcpc_conf_write(void *, pcitag_t, int, pcireg_t);
     59 
     60 CFATTACH_DECL(ibmcpc, sizeof(struct ibmcpc_softc),
     61               ibmcpc_match, ibmcpc_attach, NULL, NULL);
     62 
     63 #define PCI_DEVFN(slot,func)    ((((slot) & 0x1f) << 3) | ((func) & 0x07))
     64 
     65 static int
     66 ibmcpc_match(struct device *parent, struct cfdata *cf, void *aux)
     67 {
     68 	struct confargs *ca = aux;
     69 	char compat[32];
     70 
     71 	if (strcmp(ca->ca_name, "ht") != 0)
     72 		return 0;
     73 
     74 	memset(compat, 0, sizeof(compat));
     75 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     76 
     77 	if (strcmp(compat, "u3-ht") != 0)
     78 		return 0;
     79 
     80 	return 1;
     81 }
     82 
     83 static void
     84 ibmcpc_attach(struct device *parent, struct device *self, void *aux)
     85 {
     86 	struct ibmcpc_softc *sc = (void *) self;
     87 	pci_chipset_tag_t pc = sc->sc_pc;
     88 	struct confargs *ca = aux;
     89 	struct pcibus_attach_args pba;
     90 	int node = ca->ca_node, child;
     91 	u_int32_t reg[6], i;
     92 	void *pc_data;
     93 	char name[32];
     94 	u_int32_t busrange[2];
     95 
     96 	printf("\n");
     97 
     98 	/* u3 address */
     99 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) {
    100 		return;
    101 	}
    102 	printf("Mapping in config space @ pa 0x%08x, size: 0x%08x\n", reg[1],
    103 	    reg[2]);
    104 	pc_data = mapiodev(reg[1], reg[2]);
    105 
    106 	for (child = OF_child(OF_finddevice("/ht")), i = 1; child;
    107 	    child = OF_peer(child), i++) {
    108 
    109 		memset(name, 0, sizeof(name));
    110 
    111 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
    112 			continue;
    113 
    114 		if (strcmp(name, "pci") != 0)
    115 			continue;
    116 
    117 
    118 		if (OF_getprop(child, "bus-range", busrange, 8) < 8)
    119 			continue;
    120 
    121 		macppc_pci_get_chipset_tag(pc);
    122 		pc->pc_node = child;
    123 		pc->pc_bus = busrange[0];
    124 		pc->pc_addr = 0x0;
    125 		pc->pc_data = pc_data;
    126 		pc->pc_conf_read = ibmcpc_conf_read;
    127 		pc->pc_conf_write = ibmcpc_conf_write;
    128 		pc->pc_memt = (bus_space_tag_t) 0;
    129 		pc->pc_iot = (bus_space_tag_t) 0;
    130 
    131 		memset(&pba, 0, sizeof(pba));
    132 		pba.pba_memt = pc->pc_memt;
    133 		pba.pba_iot = pc->pc_iot;
    134 		pba.pba_dmat = &pci_bus_dma_tag;
    135 		pba.pba_dmat64 = NULL;
    136 		pba.pba_bridgetag = NULL;
    137 		pba.pba_pc = pc;
    138 		pba.pba_bus = pc->pc_bus;
    139 		pba.pba_flags = PCI_FLAGS_MEM_ENABLED | PCI_FLAGS_IO_ENABLED;
    140 		config_found_ia(self, "pcibus", &pba, pcibusprint);
    141 
    142 		pc++;
    143 	}
    144 }
    145 
    146 static pcireg_t
    147 ibmcpc_conf_read(void *cookie, pcitag_t tag, int reg)
    148 {
    149 	pci_chipset_tag_t pc = cookie;
    150 	u_int32_t daddr = (u_int32_t) pc->pc_data;
    151 	pcireg_t data;
    152 	u_int32_t bus, dev, func, x, devfn;
    153 
    154 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    155 
    156 	devfn = PCI_DEVFN(dev, func);
    157 
    158 	if (bus == 0) {
    159 
    160 		if (devfn == 0x0) {
    161 
    162 			data = 0xffffffff;
    163 			goto done;
    164 		}
    165 
    166 		x = daddr + ((devfn << 8) | reg);
    167 	} else
    168 		x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL;
    169 
    170 	data = in32rb(x);
    171 
    172 done:
    173 	return data;
    174 }
    175 
    176 static void
    177 ibmcpc_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
    178 {
    179 	pci_chipset_tag_t pc = cookie;
    180 	int32_t *daddr = pc->pc_data;
    181 	u_int32_t bus, dev, func;
    182 	u_int32_t x, devfn;
    183 
    184 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    185 
    186 	devfn = PCI_DEVFN(dev, func);
    187 
    188 	if (bus == 0) {
    189 
    190 		if (devfn == 0x0) {
    191 			goto done;
    192 		}
    193 		x = (u_int32_t) daddr + ((devfn << 8) | reg);
    194 	} else
    195 		x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) +
    196 		    0x01000000UL;
    197 
    198 	out32rb(x, data);
    199 
    200 done:
    201 	return;
    202 }
    203