Home | History | Annotate | Line # | Download | only in pci
uninorth.c revision 1.8
      1 /*	$NetBSD: uninorth.c,v 1.8 2003/06/15 23:09:02 fvdl 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/param.h>
     30 #include <sys/device.h>
     31 #include <sys/systm.h>
     32 
     33 #include <dev/pci/pcivar.h>
     34 #include <dev/ofw/openfirm.h>
     35 #include <dev/ofw/ofw_pci.h>
     36 
     37 #include <machine/autoconf.h>
     38 
     39 struct uninorth_softc {
     40 	struct device sc_dev;
     41 	struct pci_bridge sc_pc;
     42 };
     43 
     44 void uninorth_attach __P((struct device *, struct device *, void *));
     45 int uninorth_match __P((struct device *, struct cfdata *, void *));
     46 int uninorth_print __P((void *, const char *));
     47 
     48 pcireg_t uninorth_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
     49 void uninorth_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
     50 
     51 CFATTACH_DECL(uninorth, sizeof(struct uninorth_softc),
     52     uninorth_match, uninorth_attach, NULL, NULL);
     53 
     54 int
     55 uninorth_match(parent, cf, aux)
     56 	struct device *parent;
     57 	struct cfdata *cf;
     58 	void *aux;
     59 {
     60 	struct confargs *ca = aux;
     61 	char compat[32];
     62 
     63 	if (strcmp(ca->ca_name, "pci") != 0)
     64 		return 0;
     65 
     66 	memset(compat, 0, sizeof(compat));
     67 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     68 	if (strcmp(compat, "uni-north") != 0)
     69 		return 0;
     70 
     71 	return 1;
     72 }
     73 
     74 void
     75 uninorth_attach(parent, self, aux)
     76 	struct device *parent, *self;
     77 	void *aux;
     78 {
     79 	struct uninorth_softc *sc = (void *)self;
     80 	pci_chipset_tag_t pc = &sc->sc_pc;
     81 	struct confargs *ca = aux;
     82 	struct pcibus_attach_args pba;
     83 	int len, child, node = ca->ca_node;
     84 	u_int32_t reg[2], busrange[2];
     85 	struct ranges {
     86 		u_int32_t pci_hi, pci_mid, pci_lo;
     87 		u_int32_t host;
     88 		u_int32_t size_hi, size_lo;
     89 	} ranges[6], *rp = ranges;
     90 
     91 	printf("\n");
     92 
     93 	/* UniNorth address */
     94 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
     95 		return;
     96 
     97 	/* PCI bus number */
     98 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
     99 		return;
    100 
    101 	pc->node = node;
    102 	pc->addr = mapiodev(reg[0] + 0x800000, 4);
    103 	pc->data = mapiodev(reg[0] + 0xc00000, 8);
    104 	pc->bus = busrange[0];
    105 	pc->conf_read = uninorth_conf_read;
    106 	pc->conf_write = uninorth_conf_write;
    107 	pc->memt = (bus_space_tag_t)0;
    108 
    109 	/* find i/o tag */
    110 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
    111 	if (len == -1)
    112 		return;
    113 	while (len >= sizeof(ranges[0])) {
    114 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
    115 		     OFW_PCI_PHYS_HI_SPACE_IO)
    116 			pc->iot = (bus_space_tag_t)rp->host;
    117 		len -= sizeof(ranges[0]);
    118 		rp++;
    119 	}
    120 
    121 	/* XXX enable gmac ethernet */
    122 	for (child = OF_child(node); child; child = OF_peer(child)) {
    123 		volatile int *gmac_gbclock_en = (void *)0xf8000020;
    124 		char compat[32];
    125 
    126 		memset(compat, 0, sizeof(compat));
    127 		OF_getprop(child, "compatible", compat, sizeof(compat));
    128 		if (strcmp(compat, "gmac") == 0)
    129 			*gmac_gbclock_en |= 0x02;
    130 	}
    131 
    132 	memset(&pba, 0, sizeof(pba));
    133 	pba.pba_busname = "pci";
    134 	pba.pba_memt = pc->memt;
    135 	pba.pba_iot = pc->iot;
    136 	pba.pba_dmat = &pci_bus_dma_tag;
    137 	pba.pba_dmat64 = NULL;
    138 	pba.pba_bus = pc->bus;
    139 	pba.pba_bridgetag = NULL;
    140 	pba.pba_pc = pc;
    141 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    142 
    143 	config_found(self, &pba, uninorth_print);
    144 }
    145 
    146 int
    147 uninorth_print(aux, pnp)
    148 	void *aux;
    149 	const char *pnp;
    150 {
    151 	struct pcibus_attach_args *pa = aux;
    152 
    153 	if (pnp)
    154 		aprint_normal("%s at %s", pa->pba_busname, pnp);
    155 	aprint_normal(" bus %d", pa->pba_bus);
    156 	return UNCONF;
    157 }
    158 
    159 pcireg_t
    160 uninorth_conf_read(pc, tag, reg)
    161 	pci_chipset_tag_t pc;
    162 	pcitag_t tag;
    163 	int reg;
    164 {
    165 	int32_t *daddr = pc->data;
    166 	pcireg_t data;
    167 	int bus, dev, func, s;
    168 	u_int32_t x;
    169 
    170 	/* UniNorth seems to have a 64bit data port */
    171 	if (reg & 0x04)
    172 		daddr++;
    173 
    174 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    175 
    176 	/*
    177 	 * bandit's minimum device number of the first bus is 11.
    178 	 * So we behave as if there is no device when dev < 11.
    179 	 */
    180 	if (func > 7)
    181 		panic("pci_conf_read: func > 7");
    182 
    183 	if (bus == pc->bus) {
    184 		if (dev < 11)
    185 			return 0xffffffff;
    186 		x = (1 << dev) | (func << 8) | reg;
    187 	} else
    188 		x = tag | reg | 1;
    189 
    190 	s = splhigh();
    191 
    192 	out32rb(pc->addr, x);
    193 	in32rb(pc->addr);
    194 	data = 0xffffffff;
    195 	if (!badaddr(daddr, 4))
    196 		data = in32rb(daddr);
    197 	out32rb(pc->addr, 0);
    198 	in32rb(pc->addr);
    199 	splx(s);
    200 
    201 	return data;
    202 }
    203 
    204 void
    205 uninorth_conf_write(pc, tag, reg, data)
    206 	pci_chipset_tag_t pc;
    207 	pcitag_t tag;
    208 	int reg;
    209 	pcireg_t data;
    210 {
    211 	int32_t *daddr = pc->data;
    212 	int bus, dev, func, s;
    213 	u_int32_t x;
    214 
    215 	/* UniNorth seems to have a 64bit data port */
    216 	if (reg & 0x04)
    217 		daddr++;
    218 
    219 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    220 
    221 	if (func > 7)
    222 		panic("pci_conf_write: func > 7");
    223 
    224 	if (bus == pc->bus) {
    225 		if (dev < 11)
    226 			panic("pci_conf_write: dev < 11");
    227 		x = (1 << dev) | (func << 8) | reg;
    228 	} else
    229 		x = tag | reg | 1;
    230 
    231 	s = splhigh();
    232 
    233 	out32rb(pc->addr, x);
    234 	in32rb(pc->addr);
    235 	out32rb(daddr, data);
    236 	out32rb(pc->addr, 0);
    237 	in32rb(pc->addr);
    238 
    239 	splx(s);
    240 }
    241