Home | History | Annotate | Line # | Download | only in pci
bandit.c revision 1.18.2.2
      1  1.18.2.2  thorpej /*	$NetBSD: bandit.c,v 1.18.2.2 2002/05/16 01:01:39 thorpej Exp $	*/
      2  1.18.2.2  thorpej 
      3  1.18.2.2  thorpej /*-
      4  1.18.2.2  thorpej  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  1.18.2.2  thorpej  *
      6  1.18.2.2  thorpej  * Redistribution and use in source and binary forms, with or without
      7  1.18.2.2  thorpej  * modification, are permitted provided that the following conditions
      8  1.18.2.2  thorpej  * are met:
      9  1.18.2.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     10  1.18.2.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     11  1.18.2.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.18.2.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     13  1.18.2.2  thorpej  *    documentation and/or other materials provided with the distribution.
     14  1.18.2.2  thorpej  * 3. The name of the author may not be used to endorse or promote products
     15  1.18.2.2  thorpej  *    derived from this software without specific prior written permission.
     16  1.18.2.2  thorpej  *
     17  1.18.2.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.18.2.2  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.18.2.2  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.18.2.2  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.18.2.2  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.18.2.2  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.18.2.2  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.18.2.2  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.18.2.2  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.18.2.2  thorpej  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.18.2.2  thorpej  */
     28  1.18.2.2  thorpej 
     29  1.18.2.2  thorpej #include <sys/param.h>
     30  1.18.2.2  thorpej #include <sys/device.h>
     31  1.18.2.2  thorpej #include <sys/systm.h>
     32  1.18.2.2  thorpej 
     33  1.18.2.2  thorpej #include <dev/pci/pcivar.h>
     34  1.18.2.2  thorpej #include <dev/ofw/openfirm.h>
     35  1.18.2.2  thorpej #include <dev/ofw/ofw_pci.h>
     36  1.18.2.2  thorpej 
     37  1.18.2.2  thorpej #include <machine/autoconf.h>
     38  1.18.2.2  thorpej 
     39  1.18.2.2  thorpej struct bandit_softc {
     40  1.18.2.2  thorpej 	struct device sc_dev;
     41  1.18.2.2  thorpej 	struct pci_bridge sc_pc;
     42  1.18.2.2  thorpej };
     43  1.18.2.2  thorpej 
     44  1.18.2.2  thorpej void bandit_attach __P((struct device *, struct device *, void *));
     45  1.18.2.2  thorpej int bandit_match __P((struct device *, struct cfdata *, void *));
     46  1.18.2.2  thorpej int bandit_print __P((void *, const char *));
     47  1.18.2.2  thorpej 
     48  1.18.2.2  thorpej pcireg_t bandit_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
     49  1.18.2.2  thorpej void bandit_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
     50  1.18.2.2  thorpej 
     51  1.18.2.2  thorpej static void bandit_init __P((struct bandit_softc *));
     52  1.18.2.2  thorpej 
     53  1.18.2.2  thorpej struct cfattach bandit_ca = {
     54  1.18.2.2  thorpej 	sizeof(struct bandit_softc), bandit_match, bandit_attach
     55  1.18.2.2  thorpej };
     56  1.18.2.2  thorpej 
     57  1.18.2.2  thorpej int
     58  1.18.2.2  thorpej bandit_match(parent, cf, aux)
     59  1.18.2.2  thorpej 	struct device *parent;
     60  1.18.2.2  thorpej 	struct cfdata *cf;
     61  1.18.2.2  thorpej 	void *aux;
     62  1.18.2.2  thorpej {
     63  1.18.2.2  thorpej 	struct confargs *ca = aux;
     64  1.18.2.2  thorpej 
     65  1.18.2.2  thorpej 	if (strcmp(ca->ca_name, "bandit") == 0 ||
     66  1.18.2.2  thorpej 	    strcmp(ca->ca_name, "chaos") == 0)
     67  1.18.2.2  thorpej 		return 1;
     68  1.18.2.2  thorpej 
     69  1.18.2.2  thorpej 	return 0;
     70  1.18.2.2  thorpej }
     71  1.18.2.2  thorpej 
     72  1.18.2.2  thorpej void
     73  1.18.2.2  thorpej bandit_attach(parent, self, aux)
     74  1.18.2.2  thorpej 	struct device *parent, *self;
     75  1.18.2.2  thorpej 	void *aux;
     76  1.18.2.2  thorpej {
     77  1.18.2.2  thorpej 	struct bandit_softc *sc = (void *)self;
     78  1.18.2.2  thorpej 	pci_chipset_tag_t pc = &sc->sc_pc;
     79  1.18.2.2  thorpej 	struct confargs *ca = aux;
     80  1.18.2.2  thorpej 	struct pcibus_attach_args pba;
     81  1.18.2.2  thorpej 	int len, node = ca->ca_node;
     82  1.18.2.2  thorpej 	u_int32_t reg[2], busrange[2];
     83  1.18.2.2  thorpej 	struct ranges {
     84  1.18.2.2  thorpej 		u_int32_t pci_hi, pci_mid, pci_lo;
     85  1.18.2.2  thorpej 		u_int32_t host;
     86  1.18.2.2  thorpej 		u_int32_t size_hi, size_lo;
     87  1.18.2.2  thorpej 	} ranges[6], *rp = ranges;
     88  1.18.2.2  thorpej 
     89  1.18.2.2  thorpej 	printf("\n");
     90  1.18.2.2  thorpej 
     91  1.18.2.2  thorpej 	/* Bandit address */
     92  1.18.2.2  thorpej 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
     93  1.18.2.2  thorpej 		return;
     94  1.18.2.2  thorpej 
     95  1.18.2.2  thorpej 	/* PCI bus number */
     96  1.18.2.2  thorpej 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
     97  1.18.2.2  thorpej 		return;
     98  1.18.2.2  thorpej 
     99  1.18.2.2  thorpej 	pc->node = node;
    100  1.18.2.2  thorpej 	pc->addr = mapiodev(reg[0] + 0x800000, 4);
    101  1.18.2.2  thorpej 	pc->data = mapiodev(reg[0] + 0xc00000, 8);
    102  1.18.2.2  thorpej 	pc->bus = busrange[0];
    103  1.18.2.2  thorpej 	pc->conf_read = bandit_conf_read;
    104  1.18.2.2  thorpej 	pc->conf_write = bandit_conf_write;
    105  1.18.2.2  thorpej 	pc->memt = (bus_space_tag_t)0;
    106  1.18.2.2  thorpej 
    107  1.18.2.2  thorpej 	/* find i/o tag */
    108  1.18.2.2  thorpej 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
    109  1.18.2.2  thorpej 	if (len == -1)
    110  1.18.2.2  thorpej 		return;
    111  1.18.2.2  thorpej 	while (len >= sizeof(ranges[0])) {
    112  1.18.2.2  thorpej 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
    113  1.18.2.2  thorpej 		     OFW_PCI_PHYS_HI_SPACE_IO)
    114  1.18.2.2  thorpej 			pc->iot = (bus_space_tag_t)rp->host;
    115  1.18.2.2  thorpej 		len -= sizeof(ranges[0]);
    116  1.18.2.2  thorpej 		rp++;
    117  1.18.2.2  thorpej 	}
    118  1.18.2.2  thorpej 
    119  1.18.2.2  thorpej 	bandit_init(sc);
    120  1.18.2.2  thorpej 
    121  1.18.2.2  thorpej 	memset(&pba, 0, sizeof(pba));
    122  1.18.2.2  thorpej 	pba.pba_busname = "pci";
    123  1.18.2.2  thorpej 	pba.pba_memt = pc->memt;
    124  1.18.2.2  thorpej 	pba.pba_iot = pc->iot;
    125  1.18.2.2  thorpej 	pba.pba_dmat = &pci_bus_dma_tag;
    126  1.18.2.2  thorpej 	pba.pba_bus = pc->bus;
    127  1.18.2.2  thorpej 	pba.pba_bridgetag = NULL;
    128  1.18.2.2  thorpej 	pba.pba_pc = pc;
    129  1.18.2.2  thorpej 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    130  1.18.2.2  thorpej 
    131  1.18.2.2  thorpej 	config_found(self, &pba, bandit_print);
    132  1.18.2.2  thorpej }
    133  1.18.2.2  thorpej 
    134  1.18.2.2  thorpej int
    135  1.18.2.2  thorpej bandit_print(aux, pnp)
    136  1.18.2.2  thorpej 	void *aux;
    137  1.18.2.2  thorpej 	const char *pnp;
    138  1.18.2.2  thorpej {
    139  1.18.2.2  thorpej 	struct pcibus_attach_args *pa = aux;
    140  1.18.2.2  thorpej 
    141  1.18.2.2  thorpej 	if (pnp)
    142  1.18.2.2  thorpej 		printf("%s at %s", pa->pba_busname, pnp);
    143  1.18.2.2  thorpej 	printf(" bus %d", pa->pba_bus);
    144  1.18.2.2  thorpej 	return UNCONF;
    145  1.18.2.2  thorpej }
    146  1.18.2.2  thorpej 
    147  1.18.2.2  thorpej pcireg_t
    148  1.18.2.2  thorpej bandit_conf_read(pc, tag, reg)
    149  1.18.2.2  thorpej 	pci_chipset_tag_t pc;
    150  1.18.2.2  thorpej 	pcitag_t tag;
    151  1.18.2.2  thorpej 	int reg;
    152  1.18.2.2  thorpej {
    153  1.18.2.2  thorpej 	pcireg_t data;
    154  1.18.2.2  thorpej 	int bus, dev, func, s;
    155  1.18.2.2  thorpej 	u_int32_t x;
    156  1.18.2.2  thorpej 
    157  1.18.2.2  thorpej 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    158  1.18.2.2  thorpej 
    159  1.18.2.2  thorpej 	/*
    160  1.18.2.2  thorpej 	 * bandit's minimum device number of the first bus is 11.
    161  1.18.2.2  thorpej 	 * So we behave as if there is no device when dev < 11.
    162  1.18.2.2  thorpej 	 */
    163  1.18.2.2  thorpej 	if (func > 7)
    164  1.18.2.2  thorpej 		panic("pci_conf_read: func > 7");
    165  1.18.2.2  thorpej 
    166  1.18.2.2  thorpej 	if (bus == pc->bus) {
    167  1.18.2.2  thorpej 		if (dev < 11)
    168  1.18.2.2  thorpej 			return 0xffffffff;
    169  1.18.2.2  thorpej 		x = (1 << dev) | (func << 8) | reg;
    170  1.18.2.2  thorpej 	} else
    171  1.18.2.2  thorpej 		x = tag | reg | 1;
    172  1.18.2.2  thorpej 
    173  1.18.2.2  thorpej 	s = splhigh();
    174  1.18.2.2  thorpej 
    175  1.18.2.2  thorpej 	out32rb(pc->addr, x);
    176  1.18.2.2  thorpej 	DELAY(10);
    177  1.18.2.2  thorpej 	data = 0xffffffff;
    178  1.18.2.2  thorpej 	if (!badaddr(pc->data, 4))
    179  1.18.2.2  thorpej 		data = in32rb(pc->data);
    180  1.18.2.2  thorpej 	DELAY(10);
    181  1.18.2.2  thorpej 	out32rb(pc->addr, 0);
    182  1.18.2.2  thorpej 	DELAY(10);
    183  1.18.2.2  thorpej 
    184  1.18.2.2  thorpej 	splx(s);
    185  1.18.2.2  thorpej 
    186  1.18.2.2  thorpej 	return data;
    187  1.18.2.2  thorpej }
    188  1.18.2.2  thorpej 
    189  1.18.2.2  thorpej void
    190  1.18.2.2  thorpej bandit_conf_write(pc, tag, reg, data)
    191  1.18.2.2  thorpej 	pci_chipset_tag_t pc;
    192  1.18.2.2  thorpej 	pcitag_t tag;
    193  1.18.2.2  thorpej 	int reg;
    194  1.18.2.2  thorpej 	pcireg_t data;
    195  1.18.2.2  thorpej {
    196  1.18.2.2  thorpej 	int bus, dev, func, s;
    197  1.18.2.2  thorpej 	u_int32_t x;
    198  1.18.2.2  thorpej 
    199  1.18.2.2  thorpej 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    200  1.18.2.2  thorpej 
    201  1.18.2.2  thorpej 	if (func > 7)
    202  1.18.2.2  thorpej 		panic("pci_conf_write: func > 7");
    203  1.18.2.2  thorpej 
    204  1.18.2.2  thorpej 	if (bus == pc->bus) {
    205  1.18.2.2  thorpej 		if (dev < 11)
    206  1.18.2.2  thorpej 			panic("pci_conf_write: dev < 11");
    207  1.18.2.2  thorpej 		x = (1 << dev) | (func << 8) | reg;
    208  1.18.2.2  thorpej 	} else
    209  1.18.2.2  thorpej 		x = tag | reg | 1;
    210  1.18.2.2  thorpej 
    211  1.18.2.2  thorpej 	s = splhigh();
    212  1.18.2.2  thorpej 
    213  1.18.2.2  thorpej 	out32rb(pc->addr, x);
    214  1.18.2.2  thorpej 	DELAY(10);
    215  1.18.2.2  thorpej 	out32rb(pc->data, data);
    216  1.18.2.2  thorpej 	DELAY(10);
    217  1.18.2.2  thorpej 	out32rb(pc->addr, 0);
    218  1.18.2.2  thorpej 	DELAY(10);
    219  1.18.2.2  thorpej 
    220  1.18.2.2  thorpej 	splx(s);
    221  1.18.2.2  thorpej }
    222  1.18.2.2  thorpej 
    223  1.18.2.2  thorpej #define	PCI_BANDIT		11
    224  1.18.2.2  thorpej 
    225  1.18.2.2  thorpej #define	PCI_REG_MODE_SELECT	0x50
    226  1.18.2.2  thorpej 
    227  1.18.2.2  thorpej #define	PCI_MODE_IO_COHERENT	0x040	/* I/O coherent */
    228  1.18.2.2  thorpej 
    229  1.18.2.2  thorpej void
    230  1.18.2.2  thorpej bandit_init(sc)
    231  1.18.2.2  thorpej 	struct bandit_softc *sc;
    232  1.18.2.2  thorpej {
    233  1.18.2.2  thorpej 	pci_chipset_tag_t pc = &sc->sc_pc;
    234  1.18.2.2  thorpej 	pcitag_t tag;
    235  1.18.2.2  thorpej 	u_int mode;
    236  1.18.2.2  thorpej 
    237  1.18.2.2  thorpej 	tag = pci_make_tag(pc, pc->bus, PCI_BANDIT, 0);
    238  1.18.2.2  thorpej 	if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
    239  1.18.2.2  thorpej 		return;
    240  1.18.2.2  thorpej 
    241  1.18.2.2  thorpej 	mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
    242  1.18.2.2  thorpej 
    243  1.18.2.2  thorpej 	if ((mode & PCI_MODE_IO_COHERENT) == 0) {
    244  1.18.2.2  thorpej 		mode |= PCI_MODE_IO_COHERENT;
    245  1.18.2.2  thorpej 		pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
    246  1.18.2.2  thorpej 	}
    247  1.18.2.2  thorpej }
    248