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