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