Home | History | Annotate | Line # | Download | only in acpi
acpi_pci_layerscape_gen4.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: acpi_pci_layerscape_gen4.c,v 1.1 2020/02/01 13:26:43 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmcneill  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  1.1  jmcneill  *
     10  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11  1.1  jmcneill  * modification, are permitted provided that the following conditions
     12  1.1  jmcneill  * are met:
     13  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18  1.1  jmcneill  *
     19  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill /*
     33  1.1  jmcneill  * NXP Layerscape PCIe Gen4 controller (not ECAM compliant)
     34  1.1  jmcneill  */
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/cdefs.h>
     37  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_pci_layerscape_gen4.c,v 1.1 2020/02/01 13:26:43 jmcneill Exp $");
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <sys/param.h>
     40  1.1  jmcneill #include <sys/bus.h>
     41  1.1  jmcneill #include <sys/device.h>
     42  1.1  jmcneill #include <sys/intr.h>
     43  1.1  jmcneill #include <sys/systm.h>
     44  1.1  jmcneill #include <sys/kernel.h>
     45  1.1  jmcneill #include <sys/extent.h>
     46  1.1  jmcneill #include <sys/kmem.h>
     47  1.1  jmcneill #include <sys/mutex.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <machine/cpu.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill #include <dev/pci/pcireg.h>
     52  1.1  jmcneill #include <dev/pci/pcivar.h>
     53  1.1  jmcneill #include <dev/pci/pciconf.h>
     54  1.1  jmcneill 
     55  1.1  jmcneill #include <dev/acpi/acpivar.h>
     56  1.1  jmcneill #include <dev/acpi/acpi_pci.h>
     57  1.1  jmcneill #include <dev/acpi/acpi_mcfg.h>
     58  1.1  jmcneill 
     59  1.1  jmcneill #include <arm/acpi/acpi_pci_machdep.h>
     60  1.1  jmcneill 
     61  1.1  jmcneill #define	PAB_CTRL			0x808
     62  1.1  jmcneill #define	 PAB_CTRL_PAGE_SEL		__BITS(18,13)
     63  1.1  jmcneill #define	PAB_AXI_AMAP_PEX_WIN_L(x)	(0xba8 + 0x10 * (x))
     64  1.1  jmcneill #define	PAB_AXI_AMAP_PEX_WIN_H(x)	(0xbac + 0x10 * (x))
     65  1.1  jmcneill #define	INDIRECT_ADDR_BOUNDARY		0xc00
     66  1.1  jmcneill 
     67  1.1  jmcneill #define	LUT_BASE			0x80000
     68  1.1  jmcneill #define	LUT_GCR				0x28
     69  1.1  jmcneill #define	LUT_GCR_RRE			__BIT(0)
     70  1.1  jmcneill 
     71  1.1  jmcneill #define	REG_TO_PAGE_INDEX(reg)	(((reg) >> 10) & 0x3ff)
     72  1.1  jmcneill #define	REG_TO_PAGE_ADDR(reg)	(((reg) & 0x3ff) | INDIRECT_ADDR_BOUNDARY)
     73  1.1  jmcneill 
     74  1.1  jmcneill #define	PAB_TARGET_BUS(b)		((b) << 24)
     75  1.1  jmcneill #define	PAB_TARGET_DEV(d)		((d) << 19)
     76  1.1  jmcneill #define	PAB_TARGET_FUNC(f)		((f) << 16)
     77  1.1  jmcneill 
     78  1.1  jmcneill struct acpi_pci_layerscape_gen4 {
     79  1.1  jmcneill 	bus_space_tag_t bst;
     80  1.1  jmcneill 	bus_space_handle_t bsh;
     81  1.1  jmcneill 	uint8_t rev;
     82  1.1  jmcneill 	kmutex_t lock;
     83  1.1  jmcneill };
     84  1.1  jmcneill 
     85  1.1  jmcneill static void
     86  1.1  jmcneill acpi_pci_layerscape_gen4_ccsr_setpage(struct acpi_pci_layerscape_gen4 *pcie, u_int page_index)
     87  1.1  jmcneill {
     88  1.1  jmcneill 	uint32_t val;
     89  1.1  jmcneill 
     90  1.1  jmcneill 	val = bus_space_read_4(pcie->bst, pcie->bsh, PAB_CTRL);
     91  1.1  jmcneill 	val &= ~PAB_CTRL_PAGE_SEL;
     92  1.1  jmcneill 	val |= __SHIFTIN(page_index, PAB_CTRL_PAGE_SEL);
     93  1.1  jmcneill 	bus_space_write_4(pcie->bst, pcie->bsh, PAB_CTRL, val);
     94  1.1  jmcneill }
     95  1.1  jmcneill 
     96  1.1  jmcneill static uint32_t
     97  1.1  jmcneill acpi_pci_layerscape_gen4_ccsr_read4(struct acpi_pci_layerscape_gen4 *pcie, bus_size_t reg)
     98  1.1  jmcneill {
     99  1.1  jmcneill 	const bool indirect = reg >= INDIRECT_ADDR_BOUNDARY;
    100  1.1  jmcneill 	const u_int page_index = indirect ? REG_TO_PAGE_INDEX(reg) : 0;
    101  1.1  jmcneill 	const bus_size_t page_addr = indirect ? REG_TO_PAGE_ADDR(reg) : reg;
    102  1.1  jmcneill 
    103  1.1  jmcneill 	acpi_pci_layerscape_gen4_ccsr_setpage(pcie, page_index);
    104  1.1  jmcneill 	return bus_space_read_4(pcie->bst, pcie->bsh, page_addr);
    105  1.1  jmcneill }
    106  1.1  jmcneill 
    107  1.1  jmcneill static void
    108  1.1  jmcneill acpi_pci_layerscape_gen4_ccsr_write4(struct acpi_pci_layerscape_gen4 *pcie,
    109  1.1  jmcneill     bus_size_t reg, pcireg_t data)
    110  1.1  jmcneill {
    111  1.1  jmcneill 	const bool indirect = reg >= INDIRECT_ADDR_BOUNDARY;
    112  1.1  jmcneill 	const u_int page_index = indirect ? REG_TO_PAGE_INDEX(reg) : 0;
    113  1.1  jmcneill 	const bus_size_t page_addr = indirect ? REG_TO_PAGE_ADDR(reg) : reg;
    114  1.1  jmcneill 
    115  1.1  jmcneill 	acpi_pci_layerscape_gen4_ccsr_setpage(pcie, page_index);
    116  1.1  jmcneill 	bus_space_write_4(pcie->bst, pcie->bsh, page_addr, data);
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static void
    120  1.1  jmcneill acpi_pci_layerscape_gen4_select_target(struct acpi_pci_layerscape_gen4 *pcie,
    121  1.1  jmcneill     pci_chipset_tag_t pc, pcitag_t tag)
    122  1.1  jmcneill {
    123  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
    124  1.1  jmcneill 	int b, d, f;
    125  1.1  jmcneill 
    126  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    127  1.1  jmcneill 
    128  1.1  jmcneill 	const uint32_t target = PAB_TARGET_BUS(b - ap->ap_bus) |
    129  1.1  jmcneill 	    PAB_TARGET_DEV(d) | PAB_TARGET_FUNC(f);
    130  1.1  jmcneill 
    131  1.1  jmcneill 	acpi_pci_layerscape_gen4_ccsr_write4(pcie, PAB_AXI_AMAP_PEX_WIN_L(0), target);
    132  1.1  jmcneill 	acpi_pci_layerscape_gen4_ccsr_write4(pcie, PAB_AXI_AMAP_PEX_WIN_H(0), 0);
    133  1.1  jmcneill }
    134  1.1  jmcneill 
    135  1.1  jmcneill static bool
    136  1.1  jmcneill acpi_pci_layerscape_gen4_is_tag_okay(pci_chipset_tag_t pc, pcitag_t tag)
    137  1.1  jmcneill {
    138  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
    139  1.1  jmcneill 	int b, d, f;
    140  1.1  jmcneill 
    141  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    142  1.1  jmcneill 
    143  1.1  jmcneill 	if (b <= ap->ap_bus + 1 && d > 0)
    144  1.1  jmcneill 		return false;
    145  1.1  jmcneill 
    146  1.1  jmcneill 	return true;
    147  1.1  jmcneill }
    148  1.1  jmcneill 
    149  1.1  jmcneill static int
    150  1.1  jmcneill acpi_pci_layerscape_gen4_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
    151  1.1  jmcneill {
    152  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
    153  1.1  jmcneill 	struct acpi_pci_layerscape_gen4 *pcie = ap->ap_conf_priv;
    154  1.1  jmcneill 	int b, d, f;
    155  1.1  jmcneill 	int error;
    156  1.1  jmcneill 
    157  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    158  1.1  jmcneill 
    159  1.1  jmcneill 	if (!acpi_pci_layerscape_gen4_is_tag_okay(pc, tag)) {
    160  1.1  jmcneill 		*data = -1;
    161  1.1  jmcneill 		return EINVAL;
    162  1.1  jmcneill 	}
    163  1.1  jmcneill 
    164  1.1  jmcneill 	mutex_enter(&pcie->lock);
    165  1.1  jmcneill 
    166  1.1  jmcneill 	if (pcie->rev == 0x10 && reg == PCI_ID_REG)
    167  1.1  jmcneill 		bus_space_write_4(pcie->bst, pcie->bsh, LUT_BASE + LUT_GCR, 0);
    168  1.1  jmcneill 
    169  1.1  jmcneill 	if (b == ap->ap_bus) {
    170  1.1  jmcneill 		*data = acpi_pci_layerscape_gen4_ccsr_read4(pcie, reg);
    171  1.1  jmcneill 		error = 0;
    172  1.1  jmcneill 	} else {
    173  1.1  jmcneill 		acpi_pci_layerscape_gen4_select_target(pcie, pc, tag);
    174  1.1  jmcneill 		error = acpimcfg_conf_read(pc, pci_make_tag(pc, 0, 0, 0), reg, data);
    175  1.1  jmcneill 	}
    176  1.1  jmcneill 
    177  1.1  jmcneill 	if (pcie->rev == 0x10 && reg == PCI_ID_REG)
    178  1.1  jmcneill 		bus_space_write_4(pcie->bst, pcie->bsh, LUT_BASE + LUT_GCR, LUT_GCR_RRE);
    179  1.1  jmcneill 
    180  1.1  jmcneill 	mutex_exit(&pcie->lock);
    181  1.1  jmcneill 
    182  1.1  jmcneill 	return error;
    183  1.1  jmcneill }
    184  1.1  jmcneill 
    185  1.1  jmcneill static int
    186  1.1  jmcneill acpi_pci_layerscape_gen4_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    187  1.1  jmcneill {
    188  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
    189  1.1  jmcneill 	struct acpi_pci_layerscape_gen4 *pcie = ap->ap_conf_priv;
    190  1.1  jmcneill 	int b, d, f;
    191  1.1  jmcneill 	int error;
    192  1.1  jmcneill 
    193  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    194  1.1  jmcneill 
    195  1.1  jmcneill 	if (!acpi_pci_layerscape_gen4_is_tag_okay(pc, tag))
    196  1.1  jmcneill 		return EINVAL;
    197  1.1  jmcneill 
    198  1.1  jmcneill 	mutex_enter(&pcie->lock);
    199  1.1  jmcneill 
    200  1.1  jmcneill 	if (b == ap->ap_bus) {
    201  1.1  jmcneill 		acpi_pci_layerscape_gen4_ccsr_write4(pcie, reg, data);
    202  1.1  jmcneill 		error = 0;
    203  1.1  jmcneill 	} else {
    204  1.1  jmcneill 		acpi_pci_layerscape_gen4_select_target(pcie, pc, tag);
    205  1.1  jmcneill 		error = acpimcfg_conf_write(pc, pci_make_tag(pc, 0, 0, 0), reg, data);
    206  1.1  jmcneill 	}
    207  1.1  jmcneill 
    208  1.1  jmcneill 	mutex_exit(&pcie->lock);
    209  1.1  jmcneill 
    210  1.1  jmcneill 	return error;
    211  1.1  jmcneill }
    212  1.1  jmcneill 
    213  1.1  jmcneill static ACPI_STATUS
    214  1.1  jmcneill acpi_pci_layerscape_gen4_map(ACPI_HANDLE handle, UINT32 level, void *ctx, void **retval)
    215  1.1  jmcneill {
    216  1.1  jmcneill 	struct acpi_pci_context *ap = ctx;
    217  1.1  jmcneill 	struct acpi_resources res;
    218  1.1  jmcneill 	struct acpi_mem *mem;
    219  1.1  jmcneill 	struct acpi_pci_layerscape_gen4 *pcie;
    220  1.1  jmcneill 	bus_space_handle_t bsh;
    221  1.1  jmcneill 	ACPI_HANDLE parent;
    222  1.1  jmcneill 	ACPI_INTEGER seg;
    223  1.1  jmcneill 	ACPI_STATUS rv;
    224  1.1  jmcneill 	int error;
    225  1.1  jmcneill 
    226  1.1  jmcneill 	rv = AcpiGetParent(handle, &parent);
    227  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    228  1.1  jmcneill 		return rv;
    229  1.1  jmcneill 	rv = acpi_eval_integer(parent, "_SEG", &seg);
    230  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    231  1.1  jmcneill 		seg = 0;
    232  1.1  jmcneill 	if (ap->ap_seg != seg)
    233  1.1  jmcneill 		return AE_OK;
    234  1.1  jmcneill 
    235  1.1  jmcneill 	rv = acpi_resource_parse(ap->ap_dev, handle, "_CRS", &res, &acpi_resource_parse_ops_quiet);
    236  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    237  1.1  jmcneill 		return rv;
    238  1.1  jmcneill 
    239  1.1  jmcneill 	mem = acpi_res_mem(&res, 0);
    240  1.1  jmcneill 	if (mem == NULL) {
    241  1.1  jmcneill 		acpi_resource_cleanup(&res);
    242  1.1  jmcneill 		return AE_NOT_FOUND;
    243  1.1  jmcneill 	}
    244  1.1  jmcneill 
    245  1.1  jmcneill 	error = bus_space_map(ap->ap_bst, mem->ar_base, mem->ar_length,
    246  1.1  jmcneill 	    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &bsh);
    247  1.1  jmcneill 	if (error != 0)
    248  1.1  jmcneill 		return AE_NO_MEMORY;
    249  1.1  jmcneill 
    250  1.1  jmcneill 	pcie = kmem_alloc(sizeof(*pcie), KM_SLEEP);
    251  1.1  jmcneill 	pcie->bst = ap->ap_bst;
    252  1.1  jmcneill 	pcie->bsh = bsh;
    253  1.1  jmcneill 	mutex_init(&pcie->lock, MUTEX_DEFAULT, IPL_HIGH);
    254  1.1  jmcneill 
    255  1.1  jmcneill 	const pcireg_t cr = bus_space_read_4(pcie->bst, pcie->bsh, PCI_CLASS_REG);
    256  1.1  jmcneill 	pcie->rev = PCI_REVISION(cr);
    257  1.1  jmcneill 
    258  1.1  jmcneill 	ap->ap_conf_read = acpi_pci_layerscape_gen4_conf_read;
    259  1.1  jmcneill 	ap->ap_conf_write = acpi_pci_layerscape_gen4_conf_write;
    260  1.1  jmcneill 	ap->ap_conf_priv = pcie;
    261  1.1  jmcneill 
    262  1.1  jmcneill 	aprint_verbose_dev(ap->ap_dev,
    263  1.1  jmcneill 	    "PCIe segment %lu: Layerscape Gen4 rev. %#x found at %#lx-%#lx\n",
    264  1.1  jmcneill 	    seg, pcie->rev, mem->ar_base, mem->ar_base + mem->ar_length - 1);
    265  1.1  jmcneill 
    266  1.1  jmcneill 	return AE_CTRL_TERMINATE;
    267  1.1  jmcneill }
    268  1.1  jmcneill 
    269  1.1  jmcneill void
    270  1.1  jmcneill acpi_pci_layerscape_gen4_init(struct acpi_pci_context *ap)
    271  1.1  jmcneill {
    272  1.1  jmcneill 	ACPI_STATUS rv;
    273  1.1  jmcneill 
    274  1.1  jmcneill 	rv = AcpiGetDevices(__UNCONST("NXP0016"), acpi_pci_layerscape_gen4_map, ap, NULL);
    275  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    276  1.1  jmcneill 		return;
    277  1.1  jmcneill }
    278