Home | History | Annotate | Line # | Download | only in pci
u3.c revision 1.1.40.1
      1  1.1.40.1     matt /* $NetBSD: u3.c,v 1.1.40.1 2007/11/06 23:18:54 matt Exp $ */
      2  1.1.40.1     matt 
      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.1  sanjayl 
     50       1.1  sanjayl struct ibmcpc_softc
     51       1.1  sanjayl {
     52  1.1.40.1     matt 	struct device sc_dev;
     53  1.1.40.1     matt 	struct genppc_pci_chipset sc_pc[8];
     54  1.1.40.1     matt 	struct powerpc_bus_space sc_iot;
     55  1.1.40.1     matt 	struct powerpc_bus_space sc_memt;
     56       1.1  sanjayl };
     57       1.1  sanjayl 
     58  1.1.40.1     matt static void ibmcpc_attach(struct device *, struct device *, void *);
     59  1.1.40.1     matt static int ibmcpc_match(struct device *, struct cfdata *, void *);
     60       1.1  sanjayl 
     61  1.1.40.1     matt static pcireg_t ibmcpc_conf_read(void *, pcitag_t, int);
     62  1.1.40.1     matt static void ibmcpc_conf_write(void *, pcitag_t, int, pcireg_t);
     63       1.1  sanjayl 
     64       1.1  sanjayl CFATTACH_DECL(ibmcpc, sizeof(struct ibmcpc_softc),
     65       1.1  sanjayl               ibmcpc_match, ibmcpc_attach, NULL, NULL);
     66       1.1  sanjayl 
     67       1.1  sanjayl #define PCI_DEVFN(slot,func)    ((((slot) & 0x1f) << 3) | ((func) & 0x07))
     68       1.1  sanjayl 
     69  1.1.40.1     matt static int
     70  1.1.40.1     matt ibmcpc_match(struct device *parent, struct cfdata *cf, void *aux)
     71       1.1  sanjayl {
     72  1.1.40.1     matt 	struct confargs *ca = aux;
     73  1.1.40.1     matt 	char compat[32];
     74       1.1  sanjayl 
     75  1.1.40.1     matt 	if (strcmp(ca->ca_name, "ht") != 0)
     76  1.1.40.1     matt 		return 0;
     77       1.1  sanjayl 
     78  1.1.40.1     matt 	memset(compat, 0, sizeof(compat));
     79  1.1.40.1     matt 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     80       1.1  sanjayl 
     81  1.1.40.1     matt 	if (strcmp(compat, "u3-ht") != 0)
     82  1.1.40.1     matt 		return 0;
     83       1.1  sanjayl 
     84  1.1.40.1     matt 	return 1;
     85       1.1  sanjayl }
     86       1.1  sanjayl 
     87  1.1.40.1     matt static void
     88  1.1.40.1     matt ibmcpc_attach(struct device *parent, struct device *self, void *aux)
     89       1.1  sanjayl {
     90  1.1.40.1     matt 	struct ibmcpc_softc *sc = (void *) self;
     91  1.1.40.1     matt 	pci_chipset_tag_t pc = sc->sc_pc;
     92  1.1.40.1     matt 	struct confargs *ca = aux;
     93  1.1.40.1     matt 	struct pcibus_attach_args pba;
     94  1.1.40.1     matt 	int node = ca->ca_node, child;
     95  1.1.40.1     matt 	uint32_t reg[6], busrange[2], i;
     96  1.1.40.1     matt 	void *pc_data;
     97  1.1.40.1     matt 	char name[32];
     98  1.1.40.1     matt 
     99  1.1.40.1     matt 	aprint_normal("\n");
    100  1.1.40.1     matt 
    101  1.1.40.1     matt 	/* u3 address */
    102  1.1.40.1     matt 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) {
    103  1.1.40.1     matt 		return;
    104  1.1.40.1     matt 	}
    105  1.1.40.1     matt 	aprint_normal("Mapping in config space @ pa 0x%08x, size: 0x%08x\n",
    106  1.1.40.1     matt 	    reg[1], reg[2]);
    107  1.1.40.1     matt 	pc_data = mapiodev(reg[1], reg[2]);
    108  1.1.40.1     matt 
    109  1.1.40.1     matt 	for (child = OF_child(OF_finddevice("/ht")), i = 1; child;
    110  1.1.40.1     matt 	    child = OF_peer(child), i++) {
    111  1.1.40.1     matt 
    112  1.1.40.1     matt 		memset(name, 0, sizeof(name));
    113  1.1.40.1     matt 
    114  1.1.40.1     matt 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
    115  1.1.40.1     matt 			continue;
    116  1.1.40.1     matt 
    117  1.1.40.1     matt 		if (strcmp(name, "pci") != 0)
    118  1.1.40.1     matt 			continue;
    119  1.1.40.1     matt 
    120  1.1.40.1     matt 		if (OF_getprop(child, "bus-range", busrange, 8) < 8)
    121  1.1.40.1     matt 			continue;
    122  1.1.40.1     matt 
    123  1.1.40.1     matt 		sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN |
    124  1.1.40.1     matt 		    _BUS_SPACE_IO_TYPE;
    125  1.1.40.1     matt 		sc->sc_iot.pbs_base = 0x00000000;
    126  1.1.40.1     matt 		if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, child,
    127  1.1.40.1     matt 		    &sc->sc_iot, "ibmcpc io") != 0)
    128  1.1.40.1     matt 			panic("Can't init ibmcpc io tag");
    129  1.1.40.1     matt 
    130  1.1.40.1     matt 		sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN |
    131  1.1.40.1     matt 		    _BUS_SPACE_MEM_TYPE;
    132  1.1.40.1     matt 		sc->sc_memt.pbs_base = 0x00000000;
    133  1.1.40.1     matt 		if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, child,
    134  1.1.40.1     matt 		    &sc->sc_memt, "ibmcpc mem") != 0)
    135  1.1.40.1     matt 			panic("Can't init ibmcpc mem tag");
    136  1.1.40.1     matt 
    137  1.1.40.1     matt 		macppc_pci_get_chipset_tag(pc);
    138  1.1.40.1     matt 		pc->pc_node = child;
    139  1.1.40.1     matt 		pc->pc_bus = busrange[0];
    140  1.1.40.1     matt 		pc->pc_addr = 0x0;
    141  1.1.40.1     matt 		pc->pc_data = pc_data;
    142  1.1.40.1     matt 		pc->pc_conf_read = ibmcpc_conf_read;
    143  1.1.40.1     matt 		pc->pc_conf_write = ibmcpc_conf_write;
    144  1.1.40.1     matt 		pc->pc_memt = &sc->sc_memt;
    145  1.1.40.1     matt 		pc->pc_iot = &sc->sc_iot;
    146  1.1.40.1     matt 
    147  1.1.40.1     matt 		memset(&pba, 0, sizeof(pba));
    148  1.1.40.1     matt 		pba.pba_memt = pc->pc_memt;
    149  1.1.40.1     matt 		pba.pba_iot = pc->pc_iot;
    150  1.1.40.1     matt 		pba.pba_dmat = &pci_bus_dma_tag;
    151  1.1.40.1     matt 		pba.pba_dmat64 = NULL;
    152  1.1.40.1     matt 		pba.pba_bridgetag = NULL;
    153  1.1.40.1     matt 		pba.pba_pc = pc;
    154  1.1.40.1     matt 		pba.pba_bus = pc->pc_bus;
    155  1.1.40.1     matt 		pba.pba_flags = PCI_FLAGS_MEM_ENABLED | PCI_FLAGS_IO_ENABLED;
    156  1.1.40.1     matt 		config_found_ia(self, "pcibus", &pba, pcibusprint);
    157       1.1  sanjayl 
    158  1.1.40.1     matt 		pc++;
    159  1.1.40.1     matt 	}
    160       1.1  sanjayl }
    161       1.1  sanjayl 
    162  1.1.40.1     matt static pcireg_t
    163  1.1.40.1     matt ibmcpc_conf_read(void *cookie, pcitag_t tag, int reg)
    164       1.1  sanjayl {
    165  1.1.40.1     matt 	pci_chipset_tag_t pc = cookie;
    166  1.1.40.1     matt 	u_int32_t daddr = (u_int32_t) pc->pc_data;
    167  1.1.40.1     matt 	pcireg_t data;
    168  1.1.40.1     matt 	u_int32_t bus, dev, func, x, devfn;
    169  1.1.40.1     matt 
    170  1.1.40.1     matt 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    171       1.1  sanjayl 
    172  1.1.40.1     matt 	devfn = PCI_DEVFN(dev, func);
    173       1.1  sanjayl 
    174  1.1.40.1     matt 	if (bus == 0) {
    175       1.1  sanjayl 
    176  1.1.40.1     matt 		if (devfn == 0x0) {
    177       1.1  sanjayl 
    178  1.1.40.1     matt 			data = 0xffffffff;
    179  1.1.40.1     matt 			goto done;
    180  1.1.40.1     matt 		}
    181       1.1  sanjayl 
    182  1.1.40.1     matt 		x = daddr + ((devfn << 8) | reg);
    183  1.1.40.1     matt 	} else
    184  1.1.40.1     matt 		x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL;
    185       1.1  sanjayl 
    186  1.1.40.1     matt 	data = in32rb(x);
    187       1.1  sanjayl 
    188  1.1.40.1     matt done:
    189  1.1.40.1     matt 	return data;
    190       1.1  sanjayl }
    191       1.1  sanjayl 
    192  1.1.40.1     matt static void
    193  1.1.40.1     matt ibmcpc_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
    194       1.1  sanjayl {
    195  1.1.40.1     matt 	pci_chipset_tag_t pc = cookie;
    196  1.1.40.1     matt 	int32_t *daddr = pc->pc_data;
    197  1.1.40.1     matt 	u_int32_t bus, dev, func;
    198  1.1.40.1     matt 	u_int32_t x, devfn;
    199  1.1.40.1     matt 
    200  1.1.40.1     matt 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    201  1.1.40.1     matt 
    202  1.1.40.1     matt 	devfn = PCI_DEVFN(dev, func);
    203  1.1.40.1     matt 
    204  1.1.40.1     matt 	if (bus == 0) {
    205  1.1.40.1     matt 
    206  1.1.40.1     matt 		if (devfn == 0x0) {
    207  1.1.40.1     matt 			goto done;
    208  1.1.40.1     matt 		}
    209  1.1.40.1     matt 		x = (u_int32_t) daddr + ((devfn << 8) | reg);
    210  1.1.40.1     matt 	} else
    211  1.1.40.1     matt 		x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) +
    212  1.1.40.1     matt 		    0x01000000UL;
    213       1.1  sanjayl 
    214  1.1.40.1     matt 	out32rb(x, data);
    215       1.1  sanjayl 
    216  1.1.40.1     matt done:
    217  1.1.40.1     matt 	return;
    218       1.1  sanjayl }
    219