Home | History | Annotate | Line # | Download | only in xscale
i80312_pci.c revision 1.19
      1  1.19  thorpej /*	$NetBSD: i80312_pci.c,v 1.19 2020/07/07 03:38:46 thorpej Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (c) 2001 Wasabi Systems, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7   1.1  thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8   1.1  thorpej  *
      9   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10   1.1  thorpej  * modification, are permitted provided that the following conditions
     11   1.1  thorpej  * are met:
     12   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18   1.1  thorpej  *    must display the following acknowledgement:
     19   1.1  thorpej  *	This product includes software developed for the NetBSD Project by
     20   1.1  thorpej  *	Wasabi Systems, Inc.
     21   1.1  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1  thorpej  *    or promote products derived from this software without specific prior
     23   1.1  thorpej  *    written permission.
     24   1.1  thorpej  *
     25   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1  thorpej  */
     37   1.1  thorpej 
     38   1.1  thorpej /*
     39   1.1  thorpej  * PCI configuration support for i80312 Companion I/O chip.
     40   1.1  thorpej  */
     41   1.8    lukem 
     42   1.8    lukem #include <sys/cdefs.h>
     43  1.19  thorpej __KERNEL_RCSID(0, "$NetBSD: i80312_pci.c,v 1.19 2020/07/07 03:38:46 thorpej Exp $");
     44  1.14     matt 
     45  1.14     matt #include "opt_pci.h"
     46  1.14     matt #include "pci.h"
     47   1.1  thorpej 
     48   1.1  thorpej #include <sys/param.h>
     49   1.1  thorpej #include <sys/systm.h>
     50   1.1  thorpej #include <sys/device.h>
     51   1.3  thorpej #include <sys/malloc.h>
     52  1.14     matt #include <sys/bus.h>
     53   1.1  thorpej 
     54   1.1  thorpej #include <uvm/uvm_extern.h>
     55   1.1  thorpej 
     56  1.14     matt #include <dev/pci/pcivar.h>
     57  1.14     matt #include <dev/pci/pciconf.h>
     58  1.14     matt #include <dev/pci/ppbreg.h>
     59  1.14     matt 
     60  1.14     matt #include <arm/locore.h>
     61   1.1  thorpej 
     62   1.1  thorpej #include <arm/xscale/i80312reg.h>
     63   1.1  thorpej #include <arm/xscale/i80312var.h>
     64   1.1  thorpej 
     65  1.13  msaitoh void		i80312_pci_attach_hook(device_t, device_t,
     66   1.1  thorpej 		    struct pcibus_attach_args *);
     67   1.1  thorpej int		i80312_pci_bus_maxdevs(void *, int);
     68   1.1  thorpej pcitag_t	i80312_pci_make_tag(void *, int, int, int);
     69   1.1  thorpej void		i80312_pci_decompose_tag(void *, pcitag_t, int *, int *,
     70   1.1  thorpej 		    int *);
     71   1.1  thorpej pcireg_t	i80312_pci_conf_read(void *, pcitag_t, int);
     72   1.1  thorpej void		i80312_pci_conf_write(void *, pcitag_t, int, pcireg_t);
     73  1.12     matt void		i80312_pci_conf_interrupt(void *, int, int, int, int, int *);
     74   1.1  thorpej 
     75   1.1  thorpej #define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
     76   1.1  thorpej #define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))
     77   1.1  thorpej 
     78   1.1  thorpej void
     79   1.1  thorpej i80312_pci_init(pci_chipset_tag_t pc, void *cookie)
     80   1.1  thorpej {
     81   1.3  thorpej #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     82   1.3  thorpej 	struct i80312_softc *sc = cookie;
     83  1.19  thorpej 	struct pciconf_resources *pcires;
     84   1.3  thorpej 	pcireg_t binfo;
     85  1.15   martin 	int sbus;
     86   1.3  thorpej #endif
     87   1.1  thorpej 
     88   1.1  thorpej 	pc->pc_conf_v = cookie;
     89   1.1  thorpej 	pc->pc_attach_hook = i80312_pci_attach_hook;
     90   1.1  thorpej 	pc->pc_bus_maxdevs = i80312_pci_bus_maxdevs;
     91   1.1  thorpej 	pc->pc_make_tag = i80312_pci_make_tag;
     92   1.1  thorpej 	pc->pc_decompose_tag = i80312_pci_decompose_tag;
     93   1.1  thorpej 	pc->pc_conf_read = i80312_pci_conf_read;
     94   1.1  thorpej 	pc->pc_conf_write = i80312_pci_conf_write;
     95  1.12     matt 	pc->pc_conf_interrupt = i80312_pci_conf_interrupt;
     96   1.3  thorpej 
     97   1.3  thorpej #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     98   1.3  thorpej 	/*
     99   1.3  thorpej 	 * Configure the PCI bus.
    100   1.3  thorpej 	 *
    101   1.6  thorpej 	 * XXX We need to revisit this.  We only configure the Secondary
    102   1.6  thorpej 	 * bus (and its children).  The bus configure code needs changes
    103   1.6  thorpej 	 * to support how the busses are arranged on this chip.  We also
    104   1.6  thorpej 	 * need to only configure devices in the private device space on
    105   1.6  thorpej 	 * the Secondary bus.
    106   1.3  thorpej 	 */
    107   1.3  thorpej 
    108  1.17  msaitoh 	binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PCI_BRIDGE_BUS_REG);
    109  1.17  msaitoh 	/* pbus = PCI_BRIDGE_BUS_NUM_PRIMARY(binfo); */
    110  1.17  msaitoh 	sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(binfo);
    111   1.3  thorpej 
    112  1.19  thorpej 	pcires = pciconf_resource_init();
    113  1.19  thorpej 
    114  1.19  thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    115  1.19  thorpej 	    sc->sc_sioout_base, sc->sc_sioout_size);
    116  1.19  thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    117  1.19  thorpej 	    sc->sc_smemout_base, sc->sc_smemout_size);
    118   1.3  thorpej 
    119  1.13  msaitoh 	aprint_normal_dev(sc->sc_dev, "configuring Secondary PCI bus\n");
    120  1.19  thorpej 	pci_configure_bus(pc, pcires, sbus, arm_dcache_align);
    121   1.3  thorpej 
    122  1.19  thorpej 	pciconf_resource_fini(pcires);
    123   1.3  thorpej #endif
    124   1.3  thorpej }
    125   1.3  thorpej 
    126   1.3  thorpej void
    127  1.12     matt i80312_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
    128   1.3  thorpej {
    129   1.1  thorpej }
    130   1.1  thorpej 
    131   1.1  thorpej void
    132  1.13  msaitoh i80312_pci_attach_hook(device_t parent, device_t self,
    133   1.1  thorpej     struct pcibus_attach_args *pba)
    134   1.1  thorpej {
    135   1.1  thorpej 
    136   1.1  thorpej 	/* Nothing to do. */
    137   1.1  thorpej }
    138   1.1  thorpej 
    139   1.1  thorpej int
    140   1.1  thorpej i80312_pci_bus_maxdevs(void *v, int busno)
    141   1.1  thorpej {
    142   1.1  thorpej 
    143   1.1  thorpej 	return (32);
    144   1.1  thorpej }
    145   1.1  thorpej 
    146   1.1  thorpej pcitag_t
    147   1.1  thorpej i80312_pci_make_tag(void *v, int b, int d, int f)
    148   1.1  thorpej {
    149   1.1  thorpej 
    150   1.1  thorpej 	return ((b << 16) | (d << 11) | (f << 8));
    151   1.1  thorpej }
    152   1.1  thorpej 
    153   1.1  thorpej void
    154   1.1  thorpej i80312_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    155   1.1  thorpej {
    156   1.1  thorpej 
    157   1.1  thorpej 	if (bp != NULL)
    158   1.1  thorpej 		*bp = (tag >> 16) & 0xff;
    159   1.1  thorpej 	if (dp != NULL)
    160   1.1  thorpej 		*dp = (tag >> 11) & 0x1f;
    161   1.1  thorpej 	if (fp != NULL)
    162   1.1  thorpej 		*fp = (tag >> 8) & 0x7;
    163   1.1  thorpej }
    164   1.1  thorpej 
    165   1.1  thorpej struct pciconf_state {
    166   1.1  thorpej 	bus_addr_t ps_addr_reg;
    167   1.1  thorpej 	bus_addr_t ps_data_reg;
    168   1.2  thorpej 	bus_addr_t ps_csr_reg;
    169   1.1  thorpej 	uint32_t ps_addr_val;
    170   1.1  thorpej 
    171   1.1  thorpej 	int ps_b, ps_d, ps_f;
    172   1.1  thorpej };
    173   1.1  thorpej 
    174   1.1  thorpej static int
    175   1.1  thorpej i80312_pci_conf_setup(struct i80312_softc *sc, pcitag_t tag, int offset,
    176   1.1  thorpej     struct pciconf_state *ps)
    177   1.1  thorpej {
    178   1.1  thorpej 	pcireg_t binfo;
    179   1.1  thorpej 	int pbus, sbus;
    180   1.1  thorpej 
    181  1.16  msaitoh 	if ((unsigned int)offset >= PCI_CONF_SIZE)
    182  1.16  msaitoh 		return (1);
    183  1.16  msaitoh 
    184   1.1  thorpej 	i80312_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
    185   1.1  thorpej 
    186  1.17  msaitoh 	binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PCI_BRIDGE_BUS_REG);
    187  1.17  msaitoh 	pbus = PCI_BRIDGE_BUS_NUM_PRIMARY(binfo);
    188  1.17  msaitoh 	sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(binfo);
    189   1.1  thorpej 
    190   1.1  thorpej 	/*
    191   1.1  thorpej 	 * If the bus # is the Primary bus #, use the Primary
    192   1.1  thorpej 	 * Address/Data registers, otherwise use the Secondary
    193   1.1  thorpej 	 * Address/Data registers.
    194   1.1  thorpej 	 */
    195   1.1  thorpej 	if (ps->ps_b == pbus) {
    196   1.1  thorpej 		ps->ps_addr_reg = I80312_ATU_POCCA;
    197   1.1  thorpej 		ps->ps_data_reg = I80312_ATU_POCCD;
    198   1.2  thorpej 		ps->ps_csr_reg = PCI_COMMAND_STATUS_REG;
    199   1.1  thorpej 	} else {
    200   1.1  thorpej 		ps->ps_addr_reg = I80312_ATU_SOCCA;
    201   1.1  thorpej 		ps->ps_data_reg = I80312_ATU_SOCCD;
    202   1.2  thorpej 		ps->ps_csr_reg = I80312_ATU_SACS;
    203   1.1  thorpej 	}
    204   1.1  thorpej 
    205   1.1  thorpej 	/*
    206   1.1  thorpej 	 * If the bus # is the Primary or Secondary bus #, then use
    207   1.1  thorpej 	 * Type 0 cycles, else use Type 1.
    208   1.1  thorpej 	 *
    209   1.1  thorpej 	 * XXX We should filter out all non-private devices here!
    210   1.1  thorpej 	 * XXX How does private space interact with PCI-PCI bridges?
    211   1.1  thorpej 	 */
    212   1.1  thorpej 	if (ps->ps_b == pbus || ps->ps_b == sbus) {
    213   1.1  thorpej 		if (ps->ps_d > (31 - 11))
    214   1.1  thorpej 			return (1);
    215   1.1  thorpej 		ps->ps_addr_val = (1U << (ps->ps_d + 11)) | (ps->ps_f << 8) |
    216   1.1  thorpej 		    offset;
    217   1.1  thorpej 	} else {
    218   1.1  thorpej 		/* The tag is already in the correct format. */
    219   1.1  thorpej 		ps->ps_addr_val = tag | offset | 1;
    220   1.1  thorpej 	}
    221   1.1  thorpej 
    222   1.1  thorpej 	return (0);
    223   1.1  thorpej }
    224   1.1  thorpej 
    225   1.1  thorpej pcireg_t
    226   1.1  thorpej i80312_pci_conf_read(void *v, pcitag_t tag, int offset)
    227   1.1  thorpej {
    228   1.1  thorpej 	struct i80312_softc *sc = v;
    229   1.1  thorpej 	struct pciconf_state ps;
    230   1.1  thorpej 	vaddr_t va;
    231   1.1  thorpej 	pcireg_t rv;
    232   1.1  thorpej 	u_int s;
    233   1.1  thorpej 
    234   1.1  thorpej 	if (i80312_pci_conf_setup(sc, tag, offset, &ps))
    235   1.1  thorpej 		return ((pcireg_t) -1);
    236   1.1  thorpej 
    237   1.1  thorpej 	PCI_CONF_LOCK(s);
    238   1.1  thorpej 
    239   1.1  thorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
    240   1.1  thorpej 	    ps.ps_addr_val);
    241   1.1  thorpej 
    242   1.1  thorpej 	va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
    243   1.1  thorpej 	if (badaddr_read((void *) (va + ps.ps_data_reg), sizeof(rv), &rv)) {
    244   1.2  thorpej 		/*
    245   1.2  thorpej 		 * Clear the Master Abort by reading the PCI
    246   1.2  thorpej 		 * Status Register.
    247   1.2  thorpej 		 */
    248   1.2  thorpej 		(void) bus_space_read_4(sc->sc_st, sc->sc_atu_sh,
    249   1.2  thorpej 		    ps.ps_csr_reg);
    250   1.2  thorpej #if 0
    251   1.1  thorpej 		printf("conf_read: %d/%d/%d bad address\n",
    252   1.1  thorpej 		    ps.ps_b, ps.ps_d, ps.ps_f);
    253   1.2  thorpej #endif
    254   1.1  thorpej 		rv = (pcireg_t) -1;
    255   1.1  thorpej 	}
    256   1.1  thorpej 
    257   1.1  thorpej 	PCI_CONF_UNLOCK(s);
    258   1.1  thorpej 
    259   1.1  thorpej 	return (rv);
    260   1.1  thorpej }
    261   1.1  thorpej 
    262   1.1  thorpej void
    263   1.1  thorpej i80312_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    264   1.1  thorpej {
    265   1.1  thorpej 	struct i80312_softc *sc = v;
    266   1.1  thorpej 	struct pciconf_state ps;
    267   1.1  thorpej 	u_int s;
    268   1.1  thorpej 
    269   1.1  thorpej 	if (i80312_pci_conf_setup(sc, tag, offset, &ps))
    270   1.1  thorpej 		return;
    271   1.1  thorpej 
    272   1.1  thorpej 	PCI_CONF_LOCK(s);
    273   1.1  thorpej 
    274   1.1  thorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
    275   1.1  thorpej 	    ps.ps_addr_val);
    276   1.1  thorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_data_reg, val);
    277   1.1  thorpej 
    278   1.1  thorpej 	PCI_CONF_UNLOCK(s);
    279   1.1  thorpej }
    280