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