Home | History | Annotate | Line # | Download | only in pci
bandit.c revision 1.22
      1 /*	$NetBSD: bandit.c,v 1.22 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 bandit_softc {
     40 	struct device sc_dev;
     41 	struct pci_bridge sc_pc;
     42 };
     43 
     44 void bandit_attach __P((struct device *, struct device *, void *));
     45 int bandit_match __P((struct device *, struct cfdata *, void *));
     46 int bandit_print __P((void *, const char *));
     47 
     48 pcireg_t bandit_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
     49 void bandit_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
     50 
     51 static void bandit_init __P((struct bandit_softc *));
     52 
     53 CFATTACH_DECL(bandit, sizeof(struct bandit_softc),
     54     bandit_match, bandit_attach, NULL, NULL);
     55 
     56 int
     57 bandit_match(parent, cf, aux)
     58 	struct device *parent;
     59 	struct cfdata *cf;
     60 	void *aux;
     61 {
     62 	struct confargs *ca = aux;
     63 
     64 	if (strcmp(ca->ca_name, "bandit") == 0 ||
     65 	    strcmp(ca->ca_name, "chaos") == 0)
     66 		return 1;
     67 
     68 	return 0;
     69 }
     70 
     71 void
     72 bandit_attach(parent, self, aux)
     73 	struct device *parent, *self;
     74 	void *aux;
     75 {
     76 	struct bandit_softc *sc = (void *)self;
     77 	pci_chipset_tag_t pc = &sc->sc_pc;
     78 	struct confargs *ca = aux;
     79 	struct pcibus_attach_args pba;
     80 	int len, node = ca->ca_node;
     81 	u_int32_t reg[2], busrange[2];
     82 	struct ranges {
     83 		u_int32_t pci_hi, pci_mid, pci_lo;
     84 		u_int32_t host;
     85 		u_int32_t size_hi, size_lo;
     86 	} ranges[6], *rp = ranges;
     87 
     88 	printf("\n");
     89 
     90 	/* Bandit address */
     91 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
     92 		return;
     93 
     94 	/* PCI bus number */
     95 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
     96 		return;
     97 
     98 	pc->node = node;
     99 	pc->addr = mapiodev(reg[0] + 0x800000, 4);
    100 	pc->data = mapiodev(reg[0] + 0xc00000, 8);
    101 	pc->bus = busrange[0];
    102 	pc->conf_read = bandit_conf_read;
    103 	pc->conf_write = bandit_conf_write;
    104 	pc->memt = (bus_space_tag_t)0;
    105 
    106 	/* find i/o tag */
    107 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
    108 	if (len == -1)
    109 		return;
    110 	while (len >= sizeof(ranges[0])) {
    111 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
    112 		     OFW_PCI_PHYS_HI_SPACE_IO)
    113 			pc->iot = (bus_space_tag_t)rp->host;
    114 		len -= sizeof(ranges[0]);
    115 		rp++;
    116 	}
    117 
    118 	bandit_init(sc);
    119 
    120 	memset(&pba, 0, sizeof(pba));
    121 	pba.pba_busname = "pci";
    122 	pba.pba_memt = pc->memt;
    123 	pba.pba_iot = pc->iot;
    124 	pba.pba_dmat = &pci_bus_dma_tag;
    125 	pba.pba_dmat64 = NULL;
    126 	pba.pba_bus = pc->bus;
    127 	pba.pba_bridgetag = NULL;
    128 	pba.pba_pc = pc;
    129 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    130 
    131 	config_found(self, &pba, bandit_print);
    132 }
    133 
    134 int
    135 bandit_print(aux, pnp)
    136 	void *aux;
    137 	const char *pnp;
    138 {
    139 	struct pcibus_attach_args *pa = aux;
    140 
    141 	if (pnp)
    142 		aprint_normal("%s at %s", pa->pba_busname, pnp);
    143 	aprint_normal(" bus %d", pa->pba_bus);
    144 	return UNCONF;
    145 }
    146 
    147 pcireg_t
    148 bandit_conf_read(pc, tag, reg)
    149 	pci_chipset_tag_t pc;
    150 	pcitag_t tag;
    151 	int reg;
    152 {
    153 	pcireg_t data;
    154 	int bus, dev, func, s;
    155 	u_int32_t x;
    156 
    157 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    158 
    159 	/*
    160 	 * bandit's minimum device number of the first bus is 11.
    161 	 * So we behave as if there is no device when dev < 11.
    162 	 */
    163 	if (func > 7)
    164 		panic("pci_conf_read: func > 7");
    165 
    166 	if (bus == pc->bus) {
    167 		if (dev < 11)
    168 			return 0xffffffff;
    169 		x = (1 << dev) | (func << 8) | reg;
    170 	} else
    171 		x = tag | reg | 1;
    172 
    173 	s = splhigh();
    174 
    175 	out32rb(pc->addr, x);
    176 	DELAY(10);
    177 	data = 0xffffffff;
    178 	if (!badaddr(pc->data, 4))
    179 		data = in32rb(pc->data);
    180 	DELAY(10);
    181 	out32rb(pc->addr, 0);
    182 	DELAY(10);
    183 
    184 	splx(s);
    185 
    186 	return data;
    187 }
    188 
    189 void
    190 bandit_conf_write(pc, tag, reg, data)
    191 	pci_chipset_tag_t pc;
    192 	pcitag_t tag;
    193 	int reg;
    194 	pcireg_t data;
    195 {
    196 	int bus, dev, func, s;
    197 	u_int32_t x;
    198 
    199 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    200 
    201 	if (func > 7)
    202 		panic("pci_conf_write: func > 7");
    203 
    204 	if (bus == pc->bus) {
    205 		if (dev < 11)
    206 			panic("pci_conf_write: dev < 11");
    207 		x = (1 << dev) | (func << 8) | reg;
    208 	} else
    209 		x = tag | reg | 1;
    210 
    211 	s = splhigh();
    212 
    213 	out32rb(pc->addr, x);
    214 	DELAY(10);
    215 	out32rb(pc->data, data);
    216 	DELAY(10);
    217 	out32rb(pc->addr, 0);
    218 	DELAY(10);
    219 
    220 	splx(s);
    221 }
    222 
    223 #define	PCI_BANDIT		11
    224 
    225 #define	PCI_REG_MODE_SELECT	0x50
    226 
    227 #define	PCI_MODE_IO_COHERENT	0x040	/* I/O coherent */
    228 
    229 void
    230 bandit_init(sc)
    231 	struct bandit_softc *sc;
    232 {
    233 	pci_chipset_tag_t pc = &sc->sc_pc;
    234 	pcitag_t tag;
    235 	u_int mode;
    236 
    237 	tag = pci_make_tag(pc, pc->bus, PCI_BANDIT, 0);
    238 	if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
    239 		return;
    240 
    241 	mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
    242 
    243 	if ((mode & PCI_MODE_IO_COHERENT) == 0) {
    244 		mode |= PCI_MODE_IO_COHERENT;
    245 		pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
    246 	}
    247 }
    248