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