Home | History | Annotate | Line # | Download | only in pci
bandit.c revision 1.30.30.2
      1  1.30.30.2     skrll /*	$NetBSD: bandit.c,v 1.30.30.2 2016/07/09 20:24:53 skrll 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.23     lukem 
     29       1.23     lukem #include <sys/cdefs.h>
     30  1.30.30.2     skrll __KERNEL_RCSID(0, "$NetBSD: bandit.c,v 1.30.30.2 2016/07/09 20:24:53 skrll Exp $");
     31        1.1    tsubai 
     32        1.1    tsubai #include <sys/param.h>
     33        1.1    tsubai #include <sys/device.h>
     34        1.5    tsubai #include <sys/systm.h>
     35        1.1    tsubai 
     36        1.1    tsubai #include <dev/pci/pcivar.h>
     37        1.1    tsubai #include <dev/ofw/openfirm.h>
     38        1.9   thorpej #include <dev/ofw/ofw_pci.h>
     39        1.1    tsubai 
     40       1.13    tsubai #include <machine/autoconf.h>
     41       1.26   garbled #include <machine/pci_machdep.h>
     42       1.26   garbled #include <machine/pio.h>
     43        1.1    tsubai 
     44       1.13    tsubai struct bandit_softc {
     45       1.30  macallan 	device_t sc_dev;
     46       1.26   garbled 	struct genppc_pci_chipset sc_pc;
     47       1.26   garbled 	struct powerpc_bus_space sc_iot;
     48       1.26   garbled 	struct powerpc_bus_space sc_memt;
     49  1.30.30.2     skrll 	boolean_t sc_is_chaos;
     50       1.13    tsubai };
     51       1.13    tsubai 
     52       1.28      matt static void bandit_attach(device_t, device_t, void *);
     53       1.28      matt static int bandit_match(device_t, cfdata_t, void *);
     54       1.13    tsubai 
     55       1.26   garbled static pcireg_t bandit_conf_read(void *, pcitag_t, int);
     56       1.26   garbled static void bandit_conf_write(void *, pcitag_t, int, pcireg_t);
     57  1.30.30.2     skrll static void chaos_conf_write(void *, pcitag_t, int, pcireg_t);
     58       1.13    tsubai 
     59       1.26   garbled static void bandit_init(struct bandit_softc *);
     60       1.13    tsubai 
     61       1.30  macallan CFATTACH_DECL_NEW(bandit, sizeof(struct bandit_softc),
     62       1.20   thorpej     bandit_match, bandit_attach, NULL, NULL);
     63       1.13    tsubai 
     64       1.26   garbled static int
     65       1.28      matt bandit_match(device_t parent, cfdata_t cf, void *aux)
     66       1.13    tsubai {
     67       1.13    tsubai 	struct confargs *ca = aux;
     68        1.1    tsubai 
     69       1.14    tsubai 	if (strcmp(ca->ca_name, "bandit") == 0 ||
     70       1.14    tsubai 	    strcmp(ca->ca_name, "chaos") == 0)
     71       1.13    tsubai 		return 1;
     72        1.1    tsubai 
     73       1.13    tsubai 	return 0;
     74       1.13    tsubai }
     75        1.1    tsubai 
     76       1.26   garbled static void
     77       1.28      matt bandit_attach(device_t parent, device_t self, void *aux)
     78        1.1    tsubai {
     79       1.28      matt 	struct bandit_softc *sc = device_private(self);
     80       1.13    tsubai 	pci_chipset_tag_t pc = &sc->sc_pc;
     81       1.13    tsubai 	struct confargs *ca = aux;
     82       1.13    tsubai 	struct pcibus_attach_args pba;
     83       1.13    tsubai 	int len, node = ca->ca_node;
     84       1.26   garbled 	uint32_t reg[2], busrange[2];
     85       1.26   garbled 	struct range {
     86       1.26   garbled 		uint32_t pci_hi, pci_mid, pci_lo;
     87       1.26   garbled 		uint32_t host;
     88       1.26   garbled 		uint32_t size_hi, size_lo;
     89       1.13    tsubai 	} ranges[6], *rp = ranges;
     90        1.9   thorpej 
     91       1.26   garbled 	aprint_normal("\n");
     92       1.30  macallan 	sc->sc_dev = self;
     93       1.13    tsubai 
     94  1.30.30.2     skrll 	sc->sc_is_chaos = (strcmp(ca->ca_name, "chaos") == 0);
     95  1.30.30.2     skrll 
     96       1.13    tsubai 	/* Bandit address */
     97       1.13    tsubai 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
     98       1.13    tsubai 		return;
     99        1.1    tsubai 
    100       1.13    tsubai 	/* PCI bus number */
    101       1.13    tsubai 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
    102       1.13    tsubai 		return;
    103        1.1    tsubai 
    104       1.13    tsubai 	/* find i/o tag */
    105       1.13    tsubai 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
    106       1.13    tsubai 	if (len == -1)
    107        1.1    tsubai 		return;
    108       1.13    tsubai 	while (len >= sizeof(ranges[0])) {
    109       1.13    tsubai 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
    110       1.26   garbled 		     OFW_PCI_PHYS_HI_SPACE_IO) {
    111       1.26   garbled 			sc->sc_iot.pbs_base = rp->host;
    112       1.26   garbled 			sc->sc_iot.pbs_limit = rp->host + rp->size_lo;
    113       1.26   garbled 			break;
    114       1.26   garbled 		}
    115       1.13    tsubai 		len -= sizeof(ranges[0]);
    116       1.13    tsubai 		rp++;
    117       1.13    tsubai 	}
    118       1.26   garbled 	memset(&sc->sc_iot, 0, sizeof(struct powerpc_bus_space));
    119       1.26   garbled 	memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space));
    120       1.26   garbled 	sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
    121       1.26   garbled 	if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
    122       1.26   garbled 	    "bandit io-space") != 0)
    123       1.26   garbled 		panic("Can't init bandit io tag");
    124       1.26   garbled 
    125       1.26   garbled 	sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
    126       1.26   garbled 	if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
    127       1.26   garbled 	    "bandit mem-space") != 0)
    128       1.26   garbled 		panic("Can't init bandit mem tag");
    129       1.26   garbled 
    130       1.26   garbled 	macppc_pci_get_chipset_tag(pc);
    131       1.26   garbled 	pc->pc_node = node;
    132       1.26   garbled 	pc->pc_iot = &sc->sc_iot;
    133       1.26   garbled 	pc->pc_memt = &sc->sc_memt;
    134       1.29      matt 	pc->pc_addr = mapiodev(reg[0] + 0x800000, 4, false);
    135       1.29      matt 	pc->pc_data = mapiodev(reg[0] + 0xc00000, 8, false);
    136       1.26   garbled 	pc->pc_bus = busrange[0];
    137       1.26   garbled 	pc->pc_conf_read = bandit_conf_read;
    138  1.30.30.2     skrll 	if (sc->sc_is_chaos) {
    139  1.30.30.2     skrll 		pc->pc_conf_write = chaos_conf_write;
    140  1.30.30.2     skrll 	} else
    141  1.30.30.2     skrll 		pc->pc_conf_write = bandit_conf_write;
    142        1.1    tsubai 
    143       1.13    tsubai 	bandit_init(sc);
    144        1.1    tsubai 
    145       1.16       wiz 	memset(&pba, 0, sizeof(pba));
    146       1.26   garbled 	pba.pba_memt = pc->pc_memt;
    147       1.26   garbled 	pba.pba_iot = pc->pc_iot;
    148       1.13    tsubai 	pba.pba_dmat = &pci_bus_dma_tag;
    149       1.22      fvdl 	pba.pba_dmat64 = NULL;
    150       1.26   garbled 	pba.pba_bus = pc->pc_bus;
    151       1.18   thorpej 	pba.pba_bridgetag = NULL;
    152       1.13    tsubai 	pba.pba_pc = pc;
    153       1.27    dyoung 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
    154       1.13    tsubai 
    155       1.24  drochner 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    156       1.13    tsubai }
    157        1.1    tsubai 
    158       1.26   garbled static pcireg_t
    159       1.26   garbled bandit_conf_read(void *cookie, pcitag_t tag, int reg)
    160        1.1    tsubai {
    161       1.26   garbled 	pci_chipset_tag_t pc = cookie;
    162       1.13    tsubai 	pcireg_t data;
    163       1.13    tsubai 	int bus, dev, func, s;
    164       1.26   garbled 	uint32_t x;
    165        1.1    tsubai 
    166  1.30.30.1     skrll 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    167  1.30.30.1     skrll 		return (pcireg_t) -1;
    168  1.30.30.1     skrll 
    169       1.13    tsubai 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    170       1.13    tsubai 
    171       1.13    tsubai 	/*
    172       1.13    tsubai 	 * bandit's minimum device number of the first bus is 11.
    173       1.13    tsubai 	 * So we behave as if there is no device when dev < 11.
    174       1.13    tsubai 	 */
    175       1.13    tsubai 	if (func > 7)
    176       1.13    tsubai 		panic("pci_conf_read: func > 7");
    177        1.2    tsubai 
    178       1.26   garbled 	if (bus == pc->pc_bus) {
    179       1.17   nathanw 		if (dev < 11)
    180       1.17   nathanw 			return 0xffffffff;
    181       1.13    tsubai 		x = (1 << dev) | (func << 8) | reg;
    182       1.13    tsubai 	} else
    183       1.13    tsubai 		x = tag | reg | 1;
    184       1.13    tsubai 
    185       1.13    tsubai 	s = splhigh();
    186       1.13    tsubai 
    187       1.26   garbled 	out32rb(pc->pc_addr, x);
    188       1.13    tsubai 	DELAY(10);
    189       1.13    tsubai 	data = 0xffffffff;
    190       1.26   garbled 	if (!badaddr(pc->pc_data, 4))
    191       1.26   garbled 		data = in32rb(pc->pc_data);
    192       1.13    tsubai 	DELAY(10);
    193       1.26   garbled 	out32rb(pc->pc_addr, 0);
    194       1.13    tsubai 	DELAY(10);
    195        1.4    tsubai 
    196       1.13    tsubai 	splx(s);
    197        1.2    tsubai 
    198       1.13    tsubai 	return data;
    199        1.1    tsubai }
    200        1.1    tsubai 
    201       1.26   garbled static void
    202       1.26   garbled bandit_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
    203        1.1    tsubai {
    204       1.26   garbled 	pci_chipset_tag_t pc = cookie;
    205       1.13    tsubai 	int bus, dev, func, s;
    206       1.13    tsubai 	u_int32_t x;
    207       1.13    tsubai 
    208  1.30.30.1     skrll 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    209  1.30.30.1     skrll 		return;
    210  1.30.30.1     skrll 
    211       1.13    tsubai 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    212       1.13    tsubai 
    213       1.13    tsubai 	if (func > 7)
    214       1.13    tsubai 		panic("pci_conf_write: func > 7");
    215       1.13    tsubai 
    216       1.26   garbled 	if (bus == pc->pc_bus) {
    217       1.13    tsubai 		if (dev < 11)
    218       1.13    tsubai 			panic("pci_conf_write: dev < 11");
    219       1.13    tsubai 		x = (1 << dev) | (func << 8) | reg;
    220       1.13    tsubai 	} else
    221       1.13    tsubai 		x = tag | reg | 1;
    222       1.13    tsubai 
    223       1.13    tsubai 	s = splhigh();
    224       1.13    tsubai 
    225       1.26   garbled 	out32rb(pc->pc_addr, x);
    226       1.13    tsubai 	DELAY(10);
    227       1.26   garbled 	out32rb(pc->pc_data, data);
    228       1.13    tsubai 	DELAY(10);
    229       1.26   garbled 	out32rb(pc->pc_addr, 0);
    230       1.13    tsubai 	DELAY(10);
    231        1.1    tsubai 
    232       1.13    tsubai 	splx(s);
    233       1.13    tsubai }
    234        1.1    tsubai 
    235  1.30.30.2     skrll /*
    236  1.30.30.2     skrll  * XXX
    237  1.30.30.2     skrll  * /chaos really hates writes to config space, so we just don't do them
    238  1.30.30.2     skrll  */
    239  1.30.30.2     skrll static void
    240  1.30.30.2     skrll chaos_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
    241  1.30.30.2     skrll {
    242  1.30.30.2     skrll }
    243  1.30.30.2     skrll 
    244       1.13    tsubai #define	PCI_BANDIT		11
    245        1.3    tsubai 
    246       1.13    tsubai #define	PCI_REG_MODE_SELECT	0x50
    247        1.3    tsubai 
    248       1.13    tsubai #define	PCI_MODE_IO_COHERENT	0x040	/* I/O coherent */
    249        1.3    tsubai 
    250       1.26   garbled static void
    251       1.26   garbled bandit_init(struct bandit_softc *sc)
    252       1.13    tsubai {
    253       1.13    tsubai 	pci_chipset_tag_t pc = &sc->sc_pc;
    254       1.13    tsubai 	pcitag_t tag;
    255       1.13    tsubai 	u_int mode;
    256        1.1    tsubai 
    257       1.26   garbled 	tag = pci_make_tag(pc, pc->pc_bus, PCI_BANDIT, 0);
    258       1.13    tsubai 	if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
    259        1.7    tsubai 		return;
    260        1.1    tsubai 
    261       1.13    tsubai 	mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
    262       1.13    tsubai 
    263       1.13    tsubai 	if ((mode & PCI_MODE_IO_COHERENT) == 0) {
    264       1.13    tsubai 		mode |= PCI_MODE_IO_COHERENT;
    265       1.13    tsubai 		pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
    266        1.8    tsubai 	}
    267        1.1    tsubai }
    268