Home | History | Annotate | Line # | Download | only in xscale
i80321_pci.c revision 1.17
      1  1.17      chs /*	$NetBSD: i80321_pci.c,v 1.17 2020/06/14 01:40:03 chs Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (c) 2001, 2002 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 i80321 I/O Processor chip.
     40   1.1  thorpej  */
     41   1.4    lukem 
     42   1.4    lukem #include <sys/cdefs.h>
     43  1.17      chs __KERNEL_RCSID(0, "$NetBSD: i80321_pci.c,v 1.17 2020/06/14 01:40:03 chs Exp $");
     44  1.15     matt 
     45  1.15     matt #include "opt_pci.h"
     46  1.15     matt #include "opt_i80321.h"
     47  1.15     matt #include "pci.h"
     48   1.1  thorpej 
     49   1.1  thorpej #include <sys/param.h>
     50   1.1  thorpej #include <sys/systm.h>
     51   1.1  thorpej #include <sys/device.h>
     52   1.1  thorpej #include <sys/extent.h>
     53   1.1  thorpej #include <sys/malloc.h>
     54  1.15     matt #include <sys/bus.h>
     55   1.1  thorpej 
     56   1.1  thorpej #include <uvm/uvm_extern.h>
     57   1.1  thorpej 
     58  1.15     matt #include <dev/pci/pcivar.h>
     59  1.15     matt #include <dev/pci/pciconf.h>
     60  1.15     matt #include <dev/pci/ppbreg.h>
     61  1.15     matt 
     62  1.15     matt #include <arm/locore.h>
     63   1.1  thorpej 
     64   1.1  thorpej #include <arm/xscale/i80321reg.h>
     65   1.1  thorpej #include <arm/xscale/i80321var.h>
     66   1.1  thorpej 
     67  1.14  msaitoh void		i80321_pci_attach_hook(device_t, device_t,
     68   1.1  thorpej 		    struct pcibus_attach_args *);
     69   1.1  thorpej int		i80321_pci_bus_maxdevs(void *, int);
     70   1.1  thorpej pcitag_t	i80321_pci_make_tag(void *, int, int, int);
     71   1.1  thorpej void		i80321_pci_decompose_tag(void *, pcitag_t, int *, int *,
     72   1.1  thorpej 		    int *);
     73   1.1  thorpej pcireg_t	i80321_pci_conf_read(void *, pcitag_t, int);
     74   1.1  thorpej void		i80321_pci_conf_write(void *, pcitag_t, int, pcireg_t);
     75  1.13     matt void		i80321_pci_conf_interrupt(void *, int, int, int, int, int *);
     76   1.1  thorpej 
     77   1.1  thorpej #define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
     78   1.1  thorpej #define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))
     79   1.1  thorpej 
     80   1.1  thorpej void
     81   1.1  thorpej i80321_pci_init(pci_chipset_tag_t pc, void *cookie)
     82   1.1  thorpej {
     83   1.1  thorpej #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     84   1.1  thorpej 	struct i80321_softc *sc = cookie;
     85   1.1  thorpej 	struct extent *ioext, *memext;
     86   1.1  thorpej 	uint32_t busno;
     87   1.1  thorpej #endif
     88   1.1  thorpej 
     89   1.1  thorpej 	pc->pc_conf_v = cookie;
     90   1.1  thorpej 	pc->pc_attach_hook = i80321_pci_attach_hook;
     91   1.1  thorpej 	pc->pc_bus_maxdevs = i80321_pci_bus_maxdevs;
     92   1.1  thorpej 	pc->pc_make_tag = i80321_pci_make_tag;
     93   1.1  thorpej 	pc->pc_decompose_tag = i80321_pci_decompose_tag;
     94   1.1  thorpej 	pc->pc_conf_read = i80321_pci_conf_read;
     95   1.1  thorpej 	pc->pc_conf_write = i80321_pci_conf_write;
     96  1.13     matt 	pc->pc_conf_interrupt = i80321_pci_conf_interrupt;
     97   1.1  thorpej 
     98   1.1  thorpej #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     99   1.1  thorpej 	/*
    100   1.1  thorpej 	 * Configure the PCI bus.
    101   1.1  thorpej 	 *
    102   1.1  thorpej 	 * XXX We need to revisit this.  We only configure the Secondary
    103   1.1  thorpej 	 * bus (and its children).  The bus configure code needs changes
    104   1.1  thorpej 	 * to support how the busses are arranged on this chip.  We also
    105   1.1  thorpej 	 * need to only configure devices in the private device space on
    106   1.1  thorpej 	 * the Secondary bus.
    107   1.1  thorpej 	 */
    108   1.1  thorpej 
    109   1.1  thorpej 	busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
    110   1.1  thorpej 	busno = PCIXSR_BUSNO(busno);
    111   1.1  thorpej 	if (busno == 0xff)
    112   1.1  thorpej 		busno = 0;
    113   1.1  thorpej 
    114   1.7   briggs 	ioext  = extent_create("pciio",
    115   1.7   briggs 	    sc->sc_ioout_xlate + sc->sc_ioout_xlate_offset,
    116   1.1  thorpej 	    sc->sc_ioout_xlate + VERDE_OUT_XLATE_IO_WIN_SIZE - 1,
    117  1.17      chs 	    NULL, 0, EX_WAITOK);
    118   1.8    gavan 
    119   1.8    gavan #ifdef I80321_USE_DIRECT_WIN
    120   1.8    gavan 	memext = extent_create("pcimem", VERDE_OUT_DIRECT_WIN_BASE + VERDE_OUT_DIRECT_WIN_SKIP,
    121   1.8    gavan 	    VERDE_OUT_DIRECT_WIN_BASE + VERDE_OUT_DIRECT_WIN_SIZE- 1,
    122  1.17      chs 	    NULL, 0, EX_WAITOK);
    123   1.8    gavan #else
    124   1.1  thorpej 	memext = extent_create("pcimem", sc->sc_owin[0].owin_xlate_lo,
    125   1.1  thorpej 	    sc->sc_owin[0].owin_xlate_lo + VERDE_OUT_XLATE_MEM_WIN_SIZE - 1,
    126  1.17      chs 	    NULL, 0, EX_WAITOK);
    127   1.8    gavan #endif
    128   1.1  thorpej 
    129  1.12     matt 	aprint_normal_dev(sc->sc_dev, "configuring PCI bus\n");
    130   1.1  thorpej 	pci_configure_bus(pc, ioext, memext, NULL, busno, arm_dcache_align);
    131   1.1  thorpej 
    132   1.1  thorpej 	extent_destroy(ioext);
    133   1.1  thorpej 	extent_destroy(memext);
    134   1.1  thorpej #endif
    135   1.1  thorpej }
    136   1.1  thorpej 
    137   1.1  thorpej void
    138  1.13     matt i80321_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
    139   1.1  thorpej {
    140   1.1  thorpej }
    141   1.1  thorpej 
    142   1.1  thorpej void
    143  1.14  msaitoh i80321_pci_attach_hook(device_t parent, device_t self,
    144   1.1  thorpej     struct pcibus_attach_args *pba)
    145   1.1  thorpej {
    146   1.1  thorpej 
    147   1.1  thorpej 	/* Nothing to do. */
    148   1.1  thorpej }
    149   1.1  thorpej 
    150   1.1  thorpej int
    151   1.1  thorpej i80321_pci_bus_maxdevs(void *v, int busno)
    152   1.1  thorpej {
    153   1.1  thorpej 
    154   1.1  thorpej 	return (32);
    155   1.1  thorpej }
    156   1.1  thorpej 
    157   1.1  thorpej pcitag_t
    158   1.1  thorpej i80321_pci_make_tag(void *v, int b, int d, int f)
    159   1.1  thorpej {
    160   1.1  thorpej 
    161   1.1  thorpej 	return ((b << 16) | (d << 11) | (f << 8));
    162   1.1  thorpej }
    163   1.1  thorpej 
    164   1.1  thorpej void
    165   1.1  thorpej i80321_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    166   1.1  thorpej {
    167   1.1  thorpej 
    168   1.1  thorpej 	if (bp != NULL)
    169   1.1  thorpej 		*bp = (tag >> 16) & 0xff;
    170   1.1  thorpej 	if (dp != NULL)
    171   1.1  thorpej 		*dp = (tag >> 11) & 0x1f;
    172   1.1  thorpej 	if (fp != NULL)
    173   1.1  thorpej 		*fp = (tag >> 8) & 0x7;
    174   1.1  thorpej }
    175   1.1  thorpej 
    176   1.1  thorpej struct pciconf_state {
    177   1.1  thorpej 	uint32_t ps_addr_val;
    178   1.1  thorpej 
    179   1.1  thorpej 	int ps_b, ps_d, ps_f;
    180   1.1  thorpej };
    181   1.1  thorpej 
    182   1.1  thorpej static int
    183   1.1  thorpej i80321_pci_conf_setup(struct i80321_softc *sc, pcitag_t tag, int offset,
    184   1.1  thorpej     struct pciconf_state *ps)
    185   1.1  thorpej {
    186   1.1  thorpej 	uint32_t busno;
    187   1.1  thorpej 
    188  1.16  msaitoh 	if ((unsigned int)offset >= PCI_CONF_SIZE)
    189  1.16  msaitoh 		return (1);
    190  1.16  msaitoh 
    191   1.1  thorpej 	i80321_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
    192   1.1  thorpej 
    193   1.1  thorpej 	busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
    194   1.1  thorpej 	busno = PCIXSR_BUSNO(busno);
    195   1.1  thorpej 	if (busno == 0xff)
    196   1.1  thorpej 		busno = 0;
    197   1.1  thorpej 
    198   1.1  thorpej 	/*
    199   1.1  thorpej 	 * If the bus # is the same as our own, then use Type 0 cycles,
    200   1.1  thorpej 	 * else use Type 1.
    201   1.1  thorpej 	 *
    202   1.1  thorpej 	 * XXX We should filter out all non-private devices here!
    203   1.1  thorpej 	 * XXX How does private space interact with PCI-PCI bridges?
    204   1.1  thorpej 	 */
    205   1.1  thorpej 	if (ps->ps_b == busno) {
    206   1.1  thorpej 		if (ps->ps_d > (31 - 16))
    207   1.1  thorpej 			return (1);
    208   1.3  thorpej 		/*
    209   1.3  thorpej 		 * NOTE: PCI-X requires that that devices updated their
    210   1.3  thorpej 		 * PCIXSR on every config write with the device number
    211   1.3  thorpej 		 * specified in AD[15:11].  If we don't set this field,
    212   1.3  thorpej 		 * each device could end of thinking it is at device 0,
    213   1.3  thorpej 		 * which can cause a number of problems.  Doing this
    214   1.3  thorpej 		 * unconditionally should be OK when only PCI devices
    215   1.3  thorpej 		 * are present.
    216   1.3  thorpej 		 */
    217   1.3  thorpej 		ps->ps_addr_val = (1U << (ps->ps_d + 16)) |
    218   1.3  thorpej 		    (ps->ps_d << 11) | (ps->ps_f << 8) | offset;
    219   1.1  thorpej 	} else {
    220   1.1  thorpej 		/* The tag is already in the correct format. */
    221   1.1  thorpej 		ps->ps_addr_val = tag | offset | 1;
    222   1.1  thorpej 	}
    223   1.1  thorpej 
    224   1.1  thorpej 	return (0);
    225   1.1  thorpej }
    226   1.1  thorpej 
    227   1.1  thorpej pcireg_t
    228   1.1  thorpej i80321_pci_conf_read(void *v, pcitag_t tag, int offset)
    229   1.1  thorpej {
    230   1.1  thorpej 	struct i80321_softc *sc = v;
    231   1.1  thorpej 	struct pciconf_state ps;
    232   1.1  thorpej 	vaddr_t va;
    233   1.1  thorpej 	uint32_t isr;
    234   1.1  thorpej 	pcireg_t rv;
    235   1.1  thorpej 	u_int s;
    236   1.1  thorpej 
    237   1.1  thorpej 	if (i80321_pci_conf_setup(sc, tag, offset, &ps))
    238   1.1  thorpej 		return ((pcireg_t) -1);
    239   1.1  thorpej 
    240   1.1  thorpej 	PCI_CONF_LOCK(s);
    241   1.1  thorpej 
    242   1.1  thorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_OCCAR,
    243   1.1  thorpej 	    ps.ps_addr_val);
    244   1.1  thorpej 
    245   1.1  thorpej 	va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
    246   1.1  thorpej 	if (badaddr_read((void *) (va + ATU_OCCDR), sizeof(rv), &rv)) {
    247   1.1  thorpej 		isr = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_ATUISR);
    248   1.1  thorpej 		bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_ATUISR,
    249   1.1  thorpej 		    isr & (ATUISR_P_SERR_DET|ATUISR_PMA|ATUISR_PTAM|
    250   1.1  thorpej 			   ATUISR_PTAT|ATUISR_PMPE));
    251   1.1  thorpej #if 0
    252   1.1  thorpej 		printf("conf_read: %d/%d/%d bad address\n",
    253   1.1  thorpej 		    ps.ps_b, ps.ps_d, ps.ps_f);
    254   1.1  thorpej #endif
    255   1.1  thorpej 		rv = (pcireg_t) -1;
    256   1.1  thorpej 	}
    257   1.1  thorpej 
    258   1.1  thorpej 	PCI_CONF_UNLOCK(s);
    259   1.1  thorpej 
    260   1.1  thorpej 	return (rv);
    261   1.1  thorpej }
    262   1.1  thorpej 
    263   1.1  thorpej void
    264   1.1  thorpej i80321_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    265   1.1  thorpej {
    266   1.1  thorpej 	struct i80321_softc *sc = v;
    267   1.1  thorpej 	struct pciconf_state ps;
    268   1.1  thorpej 	u_int s;
    269   1.1  thorpej 
    270   1.1  thorpej 	if (i80321_pci_conf_setup(sc, tag, offset, &ps))
    271   1.1  thorpej 		return;
    272   1.1  thorpej 
    273   1.1  thorpej 	PCI_CONF_LOCK(s);
    274   1.1  thorpej 
    275   1.1  thorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_OCCAR,
    276   1.1  thorpej 	    ps.ps_addr_val);
    277   1.1  thorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_OCCDR, val);
    278   1.1  thorpej 
    279   1.1  thorpej 	PCI_CONF_UNLOCK(s);
    280   1.1  thorpej }
    281