18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2005-2009 Cavium Networks
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/pci.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/time.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/swiotlb.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/time.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <asm/octeon/octeon.h>
208c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-npi-defs.h>
218c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-pci-defs.h>
228c2ecf20Sopenharmony_ci#include <asm/octeon/pci-octeon.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define USE_OCTEON_INTERNAL_ARBITER
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * Octeon's PCI controller uses did=3, subdid=2 for PCI IO
288c2ecf20Sopenharmony_ci * addresses. Use PCI endian swapping 1 so no address swapping is
298c2ecf20Sopenharmony_ci * necessary. The Linux io routines will endian swap the data.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci#define OCTEON_PCI_IOSPACE_BASE	    0x80011a0400000000ull
328c2ecf20Sopenharmony_ci#define OCTEON_PCI_IOSPACE_SIZE	    (1ull<<32)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Octeon't PCI controller uses did=3, subdid=3 for PCI memory. */
358c2ecf20Sopenharmony_ci#define OCTEON_PCI_MEMSPACE_OFFSET  (0x00011b0000000000ull)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciu64 octeon_bar1_pci_phys;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/**
408c2ecf20Sopenharmony_ci * This is the bit decoding used for the Octeon PCI controller addresses
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ciunion octeon_pci_address {
438c2ecf20Sopenharmony_ci	uint64_t u64;
448c2ecf20Sopenharmony_ci	struct {
458c2ecf20Sopenharmony_ci		uint64_t upper:2;
468c2ecf20Sopenharmony_ci		uint64_t reserved:13;
478c2ecf20Sopenharmony_ci		uint64_t io:1;
488c2ecf20Sopenharmony_ci		uint64_t did:5;
498c2ecf20Sopenharmony_ci		uint64_t subdid:3;
508c2ecf20Sopenharmony_ci		uint64_t reserved2:4;
518c2ecf20Sopenharmony_ci		uint64_t endian_swap:2;
528c2ecf20Sopenharmony_ci		uint64_t reserved3:10;
538c2ecf20Sopenharmony_ci		uint64_t bus:8;
548c2ecf20Sopenharmony_ci		uint64_t dev:5;
558c2ecf20Sopenharmony_ci		uint64_t func:3;
568c2ecf20Sopenharmony_ci		uint64_t reg:8;
578c2ecf20Sopenharmony_ci	} s;
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciint (*octeon_pcibios_map_irq)(const struct pci_dev *dev, u8 slot, u8 pin);
618c2ecf20Sopenharmony_cienum octeon_dma_bar_type octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_INVALID;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * Map a PCI device to the appropriate interrupt line
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * @dev:    The Linux PCI device structure for the device to map
678c2ecf20Sopenharmony_ci * @slot:   The slot number for this device on __BUS 0__. Linux
688c2ecf20Sopenharmony_ci *		 enumerates through all the bridges and figures out the
698c2ecf20Sopenharmony_ci *		 slot on Bus 0 where this device eventually hooks to.
708c2ecf20Sopenharmony_ci * @pin:    The PCI interrupt pin read from the device, then swizzled
718c2ecf20Sopenharmony_ci *		 as it goes through each bridge.
728c2ecf20Sopenharmony_ci * Returns Interrupt number for the device
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ciint pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	if (octeon_pcibios_map_irq)
778c2ecf20Sopenharmony_ci		return octeon_pcibios_map_irq(dev, slot, pin);
788c2ecf20Sopenharmony_ci	else
798c2ecf20Sopenharmony_ci		panic("octeon_pcibios_map_irq not set.");
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/*
848c2ecf20Sopenharmony_ci * Called to perform platform specific PCI setup
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_ciint pcibios_plat_dev_init(struct pci_dev *dev)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	uint16_t config;
898c2ecf20Sopenharmony_ci	uint32_t dconfig;
908c2ecf20Sopenharmony_ci	int pos;
918c2ecf20Sopenharmony_ci	/*
928c2ecf20Sopenharmony_ci	 * Force the Cache line setting to 64 bytes. The standard
938c2ecf20Sopenharmony_ci	 * Linux bus scan doesn't seem to set it. Octeon really has
948c2ecf20Sopenharmony_ci	 * 128 byte lines, but Intel bridges get really upset if you
958c2ecf20Sopenharmony_ci	 * try and set values above 64 bytes. Value is specified in
968c2ecf20Sopenharmony_ci	 * 32bit words.
978c2ecf20Sopenharmony_ci	 */
988c2ecf20Sopenharmony_ci	pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 64 / 4);
998c2ecf20Sopenharmony_ci	/* Set latency timers for all devices */
1008c2ecf20Sopenharmony_ci	pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* Enable reporting System errors and parity errors on all devices */
1038c2ecf20Sopenharmony_ci	/* Enable parity checking and error reporting */
1048c2ecf20Sopenharmony_ci	pci_read_config_word(dev, PCI_COMMAND, &config);
1058c2ecf20Sopenharmony_ci	config |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
1068c2ecf20Sopenharmony_ci	pci_write_config_word(dev, PCI_COMMAND, config);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (dev->subordinate) {
1098c2ecf20Sopenharmony_ci		/* Set latency timers on sub bridges */
1108c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 64);
1118c2ecf20Sopenharmony_ci		/* More bridge error detection */
1128c2ecf20Sopenharmony_ci		pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &config);
1138c2ecf20Sopenharmony_ci		config |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
1148c2ecf20Sopenharmony_ci		pci_write_config_word(dev, PCI_BRIDGE_CONTROL, config);
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* Enable the PCIe normal error reporting */
1188c2ecf20Sopenharmony_ci	config = PCI_EXP_DEVCTL_CERE; /* Correctable Error Reporting */
1198c2ecf20Sopenharmony_ci	config |= PCI_EXP_DEVCTL_NFERE; /* Non-Fatal Error Reporting */
1208c2ecf20Sopenharmony_ci	config |= PCI_EXP_DEVCTL_FERE;	/* Fatal Error Reporting */
1218c2ecf20Sopenharmony_ci	config |= PCI_EXP_DEVCTL_URRE;	/* Unsupported Request */
1228c2ecf20Sopenharmony_ci	pcie_capability_set_word(dev, PCI_EXP_DEVCTL, config);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* Find the Advanced Error Reporting capability */
1258c2ecf20Sopenharmony_ci	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1268c2ecf20Sopenharmony_ci	if (pos) {
1278c2ecf20Sopenharmony_ci		/* Clear Uncorrectable Error Status */
1288c2ecf20Sopenharmony_ci		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
1298c2ecf20Sopenharmony_ci				      &dconfig);
1308c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
1318c2ecf20Sopenharmony_ci				       dconfig);
1328c2ecf20Sopenharmony_ci		/* Enable reporting of all uncorrectable errors */
1338c2ecf20Sopenharmony_ci		/* Uncorrectable Error Mask - turned on bits disable errors */
1348c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, 0);
1358c2ecf20Sopenharmony_ci		/*
1368c2ecf20Sopenharmony_ci		 * Leave severity at HW default. This only controls if
1378c2ecf20Sopenharmony_ci		 * errors are reported as uncorrectable or
1388c2ecf20Sopenharmony_ci		 * correctable, not if the error is reported.
1398c2ecf20Sopenharmony_ci		 */
1408c2ecf20Sopenharmony_ci		/* PCI_ERR_UNCOR_SEVER - Uncorrectable Error Severity */
1418c2ecf20Sopenharmony_ci		/* Clear Correctable Error Status */
1428c2ecf20Sopenharmony_ci		pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &dconfig);
1438c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, dconfig);
1448c2ecf20Sopenharmony_ci		/* Enable reporting of all correctable errors */
1458c2ecf20Sopenharmony_ci		/* Correctable Error Mask - turned on bits disable errors */
1468c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, 0);
1478c2ecf20Sopenharmony_ci		/* Advanced Error Capabilities */
1488c2ecf20Sopenharmony_ci		pci_read_config_dword(dev, pos + PCI_ERR_CAP, &dconfig);
1498c2ecf20Sopenharmony_ci		/* ECRC Generation Enable */
1508c2ecf20Sopenharmony_ci		if (config & PCI_ERR_CAP_ECRC_GENC)
1518c2ecf20Sopenharmony_ci			config |= PCI_ERR_CAP_ECRC_GENE;
1528c2ecf20Sopenharmony_ci		/* ECRC Check Enable */
1538c2ecf20Sopenharmony_ci		if (config & PCI_ERR_CAP_ECRC_CHKC)
1548c2ecf20Sopenharmony_ci			config |= PCI_ERR_CAP_ECRC_CHKE;
1558c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_CAP, dconfig);
1568c2ecf20Sopenharmony_ci		/* PCI_ERR_HEADER_LOG - Header Log Register (16 bytes) */
1578c2ecf20Sopenharmony_ci		/* Report all errors to the root complex */
1588c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND,
1598c2ecf20Sopenharmony_ci				       PCI_ERR_ROOT_CMD_COR_EN |
1608c2ecf20Sopenharmony_ci				       PCI_ERR_ROOT_CMD_NONFATAL_EN |
1618c2ecf20Sopenharmony_ci				       PCI_ERR_ROOT_CMD_FATAL_EN);
1628c2ecf20Sopenharmony_ci		/* Clear the Root status register */
1638c2ecf20Sopenharmony_ci		pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &dconfig);
1648c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, dconfig);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	return 0;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/**
1718c2ecf20Sopenharmony_ci * Return the mapping of PCI device number to IRQ line. Each
1728c2ecf20Sopenharmony_ci * character in the return string represents the interrupt
1738c2ecf20Sopenharmony_ci * line for the device at that position. Device 1 maps to the
1748c2ecf20Sopenharmony_ci * first character, etc. The characters A-D are used for PCI
1758c2ecf20Sopenharmony_ci * interrupts.
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * Returns PCI interrupt mapping
1788c2ecf20Sopenharmony_ci */
1798c2ecf20Sopenharmony_ciconst char *octeon_get_pci_interrupts(void)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	/*
1828c2ecf20Sopenharmony_ci	 * Returning an empty string causes the interrupts to be
1838c2ecf20Sopenharmony_ci	 * routed based on the PCI specification. From the PCI spec:
1848c2ecf20Sopenharmony_ci	 *
1858c2ecf20Sopenharmony_ci	 * INTA# of Device Number 0 is connected to IRQW on the system
1868c2ecf20Sopenharmony_ci	 * board.  (Device Number has no significance regarding being
1878c2ecf20Sopenharmony_ci	 * located on the system board or in a connector.) INTA# of
1888c2ecf20Sopenharmony_ci	 * Device Number 1 is connected to IRQX on the system
1898c2ecf20Sopenharmony_ci	 * board. INTA# of Device Number 2 is connected to IRQY on the
1908c2ecf20Sopenharmony_ci	 * system board. INTA# of Device Number 3 is connected to IRQZ
1918c2ecf20Sopenharmony_ci	 * on the system board. The table below describes how each
1928c2ecf20Sopenharmony_ci	 * agent's INTx# lines are connected to the system board
1938c2ecf20Sopenharmony_ci	 * interrupt lines. The following equation can be used to
1948c2ecf20Sopenharmony_ci	 * determine to which INTx# signal on the system board a given
1958c2ecf20Sopenharmony_ci	 * device's INTx# line(s) is connected.
1968c2ecf20Sopenharmony_ci	 *
1978c2ecf20Sopenharmony_ci	 * MB = (D + I) MOD 4 MB = System board Interrupt (IRQW = 0,
1988c2ecf20Sopenharmony_ci	 * IRQX = 1, IRQY = 2, and IRQZ = 3) D = Device Number I =
1998c2ecf20Sopenharmony_ci	 * Interrupt Number (INTA# = 0, INTB# = 1, INTC# = 2, and
2008c2ecf20Sopenharmony_ci	 * INTD# = 3)
2018c2ecf20Sopenharmony_ci	 */
2028c2ecf20Sopenharmony_ci	if (of_machine_is_compatible("dlink,dsr-500n"))
2038c2ecf20Sopenharmony_ci		return "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
2048c2ecf20Sopenharmony_ci	switch (octeon_bootinfo->board_type) {
2058c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_NAO38:
2068c2ecf20Sopenharmony_ci		/* This is really the NAC38 */
2078c2ecf20Sopenharmony_ci		return "AAAAADABAAAAAAAAAAAAAAAAAAAAAAAA";
2088c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_EBH3100:
2098c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
2108c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
2118c2ecf20Sopenharmony_ci		return "AAABAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
2128c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_BBGW_REF:
2138c2ecf20Sopenharmony_ci		return "AABCD";
2148c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_CUST_DSR1000N:
2158c2ecf20Sopenharmony_ci		return "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
2168c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_THUNDER:
2178c2ecf20Sopenharmony_ci	case CVMX_BOARD_TYPE_EBH3000:
2188c2ecf20Sopenharmony_ci	default:
2198c2ecf20Sopenharmony_ci		return "";
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/**
2248c2ecf20Sopenharmony_ci * Map a PCI device to the appropriate interrupt line
2258c2ecf20Sopenharmony_ci *
2268c2ecf20Sopenharmony_ci * @dev:    The Linux PCI device structure for the device to map
2278c2ecf20Sopenharmony_ci * @slot:   The slot number for this device on __BUS 0__. Linux
2288c2ecf20Sopenharmony_ci *		 enumerates through all the bridges and figures out the
2298c2ecf20Sopenharmony_ci *		 slot on Bus 0 where this device eventually hooks to.
2308c2ecf20Sopenharmony_ci * @pin:    The PCI interrupt pin read from the device, then swizzled
2318c2ecf20Sopenharmony_ci *		 as it goes through each bridge.
2328c2ecf20Sopenharmony_ci * Returns Interrupt number for the device
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_ciint __init octeon_pci_pcibios_map_irq(const struct pci_dev *dev,
2358c2ecf20Sopenharmony_ci				      u8 slot, u8 pin)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	int irq_num;
2388c2ecf20Sopenharmony_ci	const char *interrupts;
2398c2ecf20Sopenharmony_ci	int dev_num;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Get the board specific interrupt mapping */
2428c2ecf20Sopenharmony_ci	interrupts = octeon_get_pci_interrupts();
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	dev_num = dev->devfn >> 3;
2458c2ecf20Sopenharmony_ci	if (dev_num < strlen(interrupts))
2468c2ecf20Sopenharmony_ci		irq_num = ((interrupts[dev_num] - 'A' + pin - 1) & 3) +
2478c2ecf20Sopenharmony_ci			OCTEON_IRQ_PCI_INT0;
2488c2ecf20Sopenharmony_ci	else
2498c2ecf20Sopenharmony_ci		irq_num = ((slot + pin - 3) & 3) + OCTEON_IRQ_PCI_INT0;
2508c2ecf20Sopenharmony_ci	return irq_num;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/*
2558c2ecf20Sopenharmony_ci * Read a value from configuration space
2568c2ecf20Sopenharmony_ci */
2578c2ecf20Sopenharmony_cistatic int octeon_read_config(struct pci_bus *bus, unsigned int devfn,
2588c2ecf20Sopenharmony_ci			      int reg, int size, u32 *val)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	union octeon_pci_address pci_addr;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	pci_addr.u64 = 0;
2638c2ecf20Sopenharmony_ci	pci_addr.s.upper = 2;
2648c2ecf20Sopenharmony_ci	pci_addr.s.io = 1;
2658c2ecf20Sopenharmony_ci	pci_addr.s.did = 3;
2668c2ecf20Sopenharmony_ci	pci_addr.s.subdid = 1;
2678c2ecf20Sopenharmony_ci	pci_addr.s.endian_swap = 1;
2688c2ecf20Sopenharmony_ci	pci_addr.s.bus = bus->number;
2698c2ecf20Sopenharmony_ci	pci_addr.s.dev = devfn >> 3;
2708c2ecf20Sopenharmony_ci	pci_addr.s.func = devfn & 0x7;
2718c2ecf20Sopenharmony_ci	pci_addr.s.reg = reg;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	switch (size) {
2748c2ecf20Sopenharmony_ci	case 4:
2758c2ecf20Sopenharmony_ci		*val = le32_to_cpu(cvmx_read64_uint32(pci_addr.u64));
2768c2ecf20Sopenharmony_ci		return PCIBIOS_SUCCESSFUL;
2778c2ecf20Sopenharmony_ci	case 2:
2788c2ecf20Sopenharmony_ci		*val = le16_to_cpu(cvmx_read64_uint16(pci_addr.u64));
2798c2ecf20Sopenharmony_ci		return PCIBIOS_SUCCESSFUL;
2808c2ecf20Sopenharmony_ci	case 1:
2818c2ecf20Sopenharmony_ci		*val = cvmx_read64_uint8(pci_addr.u64);
2828c2ecf20Sopenharmony_ci		return PCIBIOS_SUCCESSFUL;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci	return PCIBIOS_FUNC_NOT_SUPPORTED;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/*
2898c2ecf20Sopenharmony_ci * Write a value to PCI configuration space
2908c2ecf20Sopenharmony_ci */
2918c2ecf20Sopenharmony_cistatic int octeon_write_config(struct pci_bus *bus, unsigned int devfn,
2928c2ecf20Sopenharmony_ci			       int reg, int size, u32 val)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	union octeon_pci_address pci_addr;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	pci_addr.u64 = 0;
2978c2ecf20Sopenharmony_ci	pci_addr.s.upper = 2;
2988c2ecf20Sopenharmony_ci	pci_addr.s.io = 1;
2998c2ecf20Sopenharmony_ci	pci_addr.s.did = 3;
3008c2ecf20Sopenharmony_ci	pci_addr.s.subdid = 1;
3018c2ecf20Sopenharmony_ci	pci_addr.s.endian_swap = 1;
3028c2ecf20Sopenharmony_ci	pci_addr.s.bus = bus->number;
3038c2ecf20Sopenharmony_ci	pci_addr.s.dev = devfn >> 3;
3048c2ecf20Sopenharmony_ci	pci_addr.s.func = devfn & 0x7;
3058c2ecf20Sopenharmony_ci	pci_addr.s.reg = reg;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	switch (size) {
3088c2ecf20Sopenharmony_ci	case 4:
3098c2ecf20Sopenharmony_ci		cvmx_write64_uint32(pci_addr.u64, cpu_to_le32(val));
3108c2ecf20Sopenharmony_ci		return PCIBIOS_SUCCESSFUL;
3118c2ecf20Sopenharmony_ci	case 2:
3128c2ecf20Sopenharmony_ci		cvmx_write64_uint16(pci_addr.u64, cpu_to_le16(val));
3138c2ecf20Sopenharmony_ci		return PCIBIOS_SUCCESSFUL;
3148c2ecf20Sopenharmony_ci	case 1:
3158c2ecf20Sopenharmony_ci		cvmx_write64_uint8(pci_addr.u64, val);
3168c2ecf20Sopenharmony_ci		return PCIBIOS_SUCCESSFUL;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci	return PCIBIOS_FUNC_NOT_SUPPORTED;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic struct pci_ops octeon_pci_ops = {
3238c2ecf20Sopenharmony_ci	.read	= octeon_read_config,
3248c2ecf20Sopenharmony_ci	.write	= octeon_write_config,
3258c2ecf20Sopenharmony_ci};
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic struct resource octeon_pci_mem_resource = {
3288c2ecf20Sopenharmony_ci	.start = 0,
3298c2ecf20Sopenharmony_ci	.end = 0,
3308c2ecf20Sopenharmony_ci	.name = "Octeon PCI MEM",
3318c2ecf20Sopenharmony_ci	.flags = IORESOURCE_MEM,
3328c2ecf20Sopenharmony_ci};
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci/*
3358c2ecf20Sopenharmony_ci * PCI ports must be above 16KB so the ISA bus filtering in the PCI-X to PCI
3368c2ecf20Sopenharmony_ci * bridge
3378c2ecf20Sopenharmony_ci */
3388c2ecf20Sopenharmony_cistatic struct resource octeon_pci_io_resource = {
3398c2ecf20Sopenharmony_ci	.start = 0x4000,
3408c2ecf20Sopenharmony_ci	.end = OCTEON_PCI_IOSPACE_SIZE - 1,
3418c2ecf20Sopenharmony_ci	.name = "Octeon PCI IO",
3428c2ecf20Sopenharmony_ci	.flags = IORESOURCE_IO,
3438c2ecf20Sopenharmony_ci};
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic struct pci_controller octeon_pci_controller = {
3468c2ecf20Sopenharmony_ci	.pci_ops = &octeon_pci_ops,
3478c2ecf20Sopenharmony_ci	.mem_resource = &octeon_pci_mem_resource,
3488c2ecf20Sopenharmony_ci	.mem_offset = OCTEON_PCI_MEMSPACE_OFFSET,
3498c2ecf20Sopenharmony_ci	.io_resource = &octeon_pci_io_resource,
3508c2ecf20Sopenharmony_ci	.io_offset = 0,
3518c2ecf20Sopenharmony_ci	.io_map_base = OCTEON_PCI_IOSPACE_BASE,
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci/*
3568c2ecf20Sopenharmony_ci * Low level initialize the Octeon PCI controller
3578c2ecf20Sopenharmony_ci */
3588c2ecf20Sopenharmony_cistatic void octeon_pci_initialize(void)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	union cvmx_pci_cfg01 cfg01;
3618c2ecf20Sopenharmony_ci	union cvmx_npi_ctl_status ctl_status;
3628c2ecf20Sopenharmony_ci	union cvmx_pci_ctl_status_2 ctl_status_2;
3638c2ecf20Sopenharmony_ci	union cvmx_pci_cfg19 cfg19;
3648c2ecf20Sopenharmony_ci	union cvmx_pci_cfg16 cfg16;
3658c2ecf20Sopenharmony_ci	union cvmx_pci_cfg22 cfg22;
3668c2ecf20Sopenharmony_ci	union cvmx_pci_cfg56 cfg56;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/* Reset the PCI Bus */
3698c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x1);
3708c2ecf20Sopenharmony_ci	cvmx_read_csr(CVMX_CIU_SOFT_PRST);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	udelay(2000);		/* Hold PCI reset for 2 ms */
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	ctl_status.u64 = 0;	/* cvmx_read_csr(CVMX_NPI_CTL_STATUS); */
3758c2ecf20Sopenharmony_ci	ctl_status.s.max_word = 1;
3768c2ecf20Sopenharmony_ci	ctl_status.s.timer = 1;
3778c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_NPI_CTL_STATUS, ctl_status.u64);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* Deassert PCI reset and advertize PCX Host Mode Device Capability
3808c2ecf20Sopenharmony_ci	   (64b) */
3818c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x4);
3828c2ecf20Sopenharmony_ci	cvmx_read_csr(CVMX_CIU_SOFT_PRST);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	udelay(2000);		/* Wait 2 ms after deasserting PCI reset */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	ctl_status_2.u32 = 0;
3878c2ecf20Sopenharmony_ci	ctl_status_2.s.tsr_hwm = 1;	/* Initializes to 0.  Must be set
3888c2ecf20Sopenharmony_ci					   before any PCI reads. */
3898c2ecf20Sopenharmony_ci	ctl_status_2.s.bar2pres = 1;	/* Enable BAR2 */
3908c2ecf20Sopenharmony_ci	ctl_status_2.s.bar2_enb = 1;
3918c2ecf20Sopenharmony_ci	ctl_status_2.s.bar2_cax = 1;	/* Don't use L2 */
3928c2ecf20Sopenharmony_ci	ctl_status_2.s.bar2_esx = 1;
3938c2ecf20Sopenharmony_ci	ctl_status_2.s.pmo_amod = 1;	/* Round robin priority */
3948c2ecf20Sopenharmony_ci	if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG) {
3958c2ecf20Sopenharmony_ci		/* BAR1 hole */
3968c2ecf20Sopenharmony_ci		ctl_status_2.s.bb1_hole = OCTEON_PCI_BAR1_HOLE_BITS;
3978c2ecf20Sopenharmony_ci		ctl_status_2.s.bb1_siz = 1;  /* BAR1 is 2GB */
3988c2ecf20Sopenharmony_ci		ctl_status_2.s.bb_ca = 1;    /* Don't use L2 with big bars */
3998c2ecf20Sopenharmony_ci		ctl_status_2.s.bb_es = 1;    /* Big bar in byte swap mode */
4008c2ecf20Sopenharmony_ci		ctl_status_2.s.bb1 = 1;	     /* BAR1 is big */
4018c2ecf20Sopenharmony_ci		ctl_status_2.s.bb0 = 1;	     /* BAR0 is big */
4028c2ecf20Sopenharmony_ci	}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CTL_STATUS_2, ctl_status_2.u32);
4058c2ecf20Sopenharmony_ci	udelay(2000);		/* Wait 2 ms before doing PCI reads */
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	ctl_status_2.u32 = octeon_npi_read32(CVMX_NPI_PCI_CTL_STATUS_2);
4088c2ecf20Sopenharmony_ci	pr_notice("PCI Status: %s %s-bit\n",
4098c2ecf20Sopenharmony_ci		  ctl_status_2.s.ap_pcix ? "PCI-X" : "PCI",
4108c2ecf20Sopenharmony_ci		  ctl_status_2.s.ap_64ad ? "64" : "32");
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
4138c2ecf20Sopenharmony_ci		union cvmx_pci_cnt_reg cnt_reg_start;
4148c2ecf20Sopenharmony_ci		union cvmx_pci_cnt_reg cnt_reg_end;
4158c2ecf20Sopenharmony_ci		unsigned long cycles, pci_clock;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci		cnt_reg_start.u64 = cvmx_read_csr(CVMX_NPI_PCI_CNT_REG);
4188c2ecf20Sopenharmony_ci		cycles = read_c0_cvmcount();
4198c2ecf20Sopenharmony_ci		udelay(1000);
4208c2ecf20Sopenharmony_ci		cnt_reg_end.u64 = cvmx_read_csr(CVMX_NPI_PCI_CNT_REG);
4218c2ecf20Sopenharmony_ci		cycles = read_c0_cvmcount() - cycles;
4228c2ecf20Sopenharmony_ci		pci_clock = (cnt_reg_end.s.pcicnt - cnt_reg_start.s.pcicnt) /
4238c2ecf20Sopenharmony_ci			    (cycles / (mips_hpt_frequency / 1000000));
4248c2ecf20Sopenharmony_ci		pr_notice("PCI Clock: %lu MHz\n", pci_clock);
4258c2ecf20Sopenharmony_ci	}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/*
4288c2ecf20Sopenharmony_ci	 * TDOMC must be set to one in PCI mode. TDOMC should be set to 4
4298c2ecf20Sopenharmony_ci	 * in PCI-X mode to allow four outstanding splits. Otherwise,
4308c2ecf20Sopenharmony_ci	 * should not change from its reset value. Don't write PCI_CFG19
4318c2ecf20Sopenharmony_ci	 * in PCI mode (0x82000001 reset value), write it to 0x82000004
4328c2ecf20Sopenharmony_ci	 * after PCI-X mode is known. MRBCI,MDWE,MDRE -> must be zero.
4338c2ecf20Sopenharmony_ci	 * MRBCM -> must be one.
4348c2ecf20Sopenharmony_ci	 */
4358c2ecf20Sopenharmony_ci	if (ctl_status_2.s.ap_pcix) {
4368c2ecf20Sopenharmony_ci		cfg19.u32 = 0;
4378c2ecf20Sopenharmony_ci		/*
4388c2ecf20Sopenharmony_ci		 * Target Delayed/Split request outstanding maximum
4398c2ecf20Sopenharmony_ci		 * count. [1..31] and 0=32.  NOTE: If the user
4408c2ecf20Sopenharmony_ci		 * programs these bits beyond the Designed Maximum
4418c2ecf20Sopenharmony_ci		 * outstanding count, then the designed maximum table
4428c2ecf20Sopenharmony_ci		 * depth will be used instead.	No additional
4438c2ecf20Sopenharmony_ci		 * Deferred/Split transactions will be accepted if
4448c2ecf20Sopenharmony_ci		 * this outstanding maximum count is
4458c2ecf20Sopenharmony_ci		 * reached. Furthermore, no additional deferred/split
4468c2ecf20Sopenharmony_ci		 * transactions will be accepted if the I/O delay/ I/O
4478c2ecf20Sopenharmony_ci		 * Split Request outstanding maximum is reached.
4488c2ecf20Sopenharmony_ci		 */
4498c2ecf20Sopenharmony_ci		cfg19.s.tdomc = 4;
4508c2ecf20Sopenharmony_ci		/*
4518c2ecf20Sopenharmony_ci		 * Master Deferred Read Request Outstanding Max Count
4528c2ecf20Sopenharmony_ci		 * (PCI only).	CR4C[26:24] Max SAC cycles MAX DAC
4538c2ecf20Sopenharmony_ci		 * cycles 000 8 4 001 1 0 010 2 1 011 3 1 100 4 2 101
4548c2ecf20Sopenharmony_ci		 * 5 2 110 6 3 111 7 3 For example, if these bits are
4558c2ecf20Sopenharmony_ci		 * programmed to 100, the core can support 2 DAC
4568c2ecf20Sopenharmony_ci		 * cycles, 4 SAC cycles or a combination of 1 DAC and
4578c2ecf20Sopenharmony_ci		 * 2 SAC cycles. NOTE: For the PCI-X maximum
4588c2ecf20Sopenharmony_ci		 * outstanding split transactions, refer to
4598c2ecf20Sopenharmony_ci		 * CRE0[22:20].
4608c2ecf20Sopenharmony_ci		 */
4618c2ecf20Sopenharmony_ci		cfg19.s.mdrrmc = 2;
4628c2ecf20Sopenharmony_ci		/*
4638c2ecf20Sopenharmony_ci		 * Master Request (Memory Read) Byte Count/Byte Enable
4648c2ecf20Sopenharmony_ci		 * select. 0 = Byte Enables valid. In PCI mode, a
4658c2ecf20Sopenharmony_ci		 * burst transaction cannot be performed using Memory
4668c2ecf20Sopenharmony_ci		 * Read command=4?h6. 1 = DWORD Byte Count valid
4678c2ecf20Sopenharmony_ci		 * (default). In PCI Mode, the memory read byte
4688c2ecf20Sopenharmony_ci		 * enables are automatically generated by the
4698c2ecf20Sopenharmony_ci		 * core. Note: N3 Master Request transaction sizes are
4708c2ecf20Sopenharmony_ci		 * always determined through the
4718c2ecf20Sopenharmony_ci		 * am_attr[<35:32>|<7:0>] field.
4728c2ecf20Sopenharmony_ci		 */
4738c2ecf20Sopenharmony_ci		cfg19.s.mrbcm = 1;
4748c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG19, cfg19.u32);
4758c2ecf20Sopenharmony_ci	}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	cfg01.u32 = 0;
4798c2ecf20Sopenharmony_ci	cfg01.s.msae = 1;	/* Memory Space Access Enable */
4808c2ecf20Sopenharmony_ci	cfg01.s.me = 1;		/* Master Enable */
4818c2ecf20Sopenharmony_ci	cfg01.s.pee = 1;	/* PERR# Enable */
4828c2ecf20Sopenharmony_ci	cfg01.s.see = 1;	/* System Error Enable */
4838c2ecf20Sopenharmony_ci	cfg01.s.fbbe = 1;	/* Fast Back to Back Transaction Enable */
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CFG01, cfg01.u32);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci#ifdef USE_OCTEON_INTERNAL_ARBITER
4888c2ecf20Sopenharmony_ci	/*
4898c2ecf20Sopenharmony_ci	 * When OCTEON is a PCI host, most systems will use OCTEON's
4908c2ecf20Sopenharmony_ci	 * internal arbiter, so must enable it before any PCI/PCI-X
4918c2ecf20Sopenharmony_ci	 * traffic can occur.
4928c2ecf20Sopenharmony_ci	 */
4938c2ecf20Sopenharmony_ci	{
4948c2ecf20Sopenharmony_ci		union cvmx_npi_pci_int_arb_cfg pci_int_arb_cfg;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		pci_int_arb_cfg.u64 = 0;
4978c2ecf20Sopenharmony_ci		pci_int_arb_cfg.s.en = 1;	/* Internal arbiter enable */
4988c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_NPI_PCI_INT_ARB_CFG, pci_int_arb_cfg.u64);
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci#endif	/* USE_OCTEON_INTERNAL_ARBITER */
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	/*
5038c2ecf20Sopenharmony_ci	 * Preferably written to 1 to set MLTD. [RDSATI,TRTAE,
5048c2ecf20Sopenharmony_ci	 * TWTAE,TMAE,DPPMR -> must be zero. TILT -> must not be set to
5058c2ecf20Sopenharmony_ci	 * 1..7.
5068c2ecf20Sopenharmony_ci	 */
5078c2ecf20Sopenharmony_ci	cfg16.u32 = 0;
5088c2ecf20Sopenharmony_ci	cfg16.s.mltd = 1;	/* Master Latency Timer Disable */
5098c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CFG16, cfg16.u32);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/*
5128c2ecf20Sopenharmony_ci	 * Should be written to 0x4ff00. MTTV -> must be zero.
5138c2ecf20Sopenharmony_ci	 * FLUSH -> must be 1. MRV -> should be 0xFF.
5148c2ecf20Sopenharmony_ci	 */
5158c2ecf20Sopenharmony_ci	cfg22.u32 = 0;
5168c2ecf20Sopenharmony_ci	/* Master Retry Value [1..255] and 0=infinite */
5178c2ecf20Sopenharmony_ci	cfg22.s.mrv = 0xff;
5188c2ecf20Sopenharmony_ci	/*
5198c2ecf20Sopenharmony_ci	 * AM_DO_FLUSH_I control NOTE: This bit MUST BE ONE for proper
5208c2ecf20Sopenharmony_ci	 * N3K operation.
5218c2ecf20Sopenharmony_ci	 */
5228c2ecf20Sopenharmony_ci	cfg22.s.flush = 1;
5238c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CFG22, cfg22.u32);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	/*
5268c2ecf20Sopenharmony_ci	 * MOST Indicates the maximum number of outstanding splits (in -1
5278c2ecf20Sopenharmony_ci	 * notation) when OCTEON is in PCI-X mode.  PCI-X performance is
5288c2ecf20Sopenharmony_ci	 * affected by the MOST selection.  Should generally be written
5298c2ecf20Sopenharmony_ci	 * with one of 0x3be807, 0x2be807, 0x1be807, or 0x0be807,
5308c2ecf20Sopenharmony_ci	 * depending on the desired MOST of 3, 2, 1, or 0, respectively.
5318c2ecf20Sopenharmony_ci	 */
5328c2ecf20Sopenharmony_ci	cfg56.u32 = 0;
5338c2ecf20Sopenharmony_ci	cfg56.s.pxcid = 7;	/* RO - PCI-X Capability ID */
5348c2ecf20Sopenharmony_ci	cfg56.s.ncp = 0xe8;	/* RO - Next Capability Pointer */
5358c2ecf20Sopenharmony_ci	cfg56.s.dpere = 1;	/* Data Parity Error Recovery Enable */
5368c2ecf20Sopenharmony_ci	cfg56.s.roe = 1;	/* Relaxed Ordering Enable */
5378c2ecf20Sopenharmony_ci	cfg56.s.mmbc = 1;	/* Maximum Memory Byte Count
5388c2ecf20Sopenharmony_ci				   [0=512B,1=1024B,2=2048B,3=4096B] */
5398c2ecf20Sopenharmony_ci	cfg56.s.most = 3;	/* Maximum outstanding Split transactions [0=1
5408c2ecf20Sopenharmony_ci				   .. 7=32] */
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CFG56, cfg56.u32);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/*
5458c2ecf20Sopenharmony_ci	 * Affects PCI performance when OCTEON services reads to its
5468c2ecf20Sopenharmony_ci	 * BAR1/BAR2. Refer to Section 10.6.1.	The recommended values are
5478c2ecf20Sopenharmony_ci	 * 0x22, 0x33, and 0x33 for PCI_READ_CMD_6, PCI_READ_CMD_C, and
5488c2ecf20Sopenharmony_ci	 * PCI_READ_CMD_E, respectively. Unfortunately due to errata DDR-700,
5498c2ecf20Sopenharmony_ci	 * these values need to be changed so they won't possibly prefetch off
5508c2ecf20Sopenharmony_ci	 * of the end of memory if PCI is DMAing a buffer at the end of
5518c2ecf20Sopenharmony_ci	 * memory. Note that these values differ from their reset values.
5528c2ecf20Sopenharmony_ci	 */
5538c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_6, 0x21);
5548c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_C, 0x31);
5558c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_E, 0x31);
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci/*
5608c2ecf20Sopenharmony_ci * Initialize the Octeon PCI controller
5618c2ecf20Sopenharmony_ci */
5628c2ecf20Sopenharmony_cistatic int __init octeon_pci_setup(void)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	union cvmx_npi_mem_access_subidx mem_access;
5658c2ecf20Sopenharmony_ci	int index;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/* Only these chips have PCI */
5688c2ecf20Sopenharmony_ci	if (octeon_has_feature(OCTEON_FEATURE_PCIE))
5698c2ecf20Sopenharmony_ci		return 0;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	if (!octeon_is_pci_host()) {
5728c2ecf20Sopenharmony_ci		pr_notice("Not in host mode, PCI Controller not initialized\n");
5738c2ecf20Sopenharmony_ci		return 0;
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	/* Point pcibios_map_irq() to the PCI version of it */
5778c2ecf20Sopenharmony_ci	octeon_pcibios_map_irq = octeon_pci_pcibios_map_irq;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	/* Only use the big bars on chips that support it */
5808c2ecf20Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||
5818c2ecf20Sopenharmony_ci	    OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2) ||
5828c2ecf20Sopenharmony_ci	    OCTEON_IS_MODEL(OCTEON_CN38XX_PASS1))
5838c2ecf20Sopenharmony_ci		octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_SMALL;
5848c2ecf20Sopenharmony_ci	else
5858c2ecf20Sopenharmony_ci		octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_BIG;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/* PCI I/O and PCI MEM values */
5888c2ecf20Sopenharmony_ci	set_io_port_base(OCTEON_PCI_IOSPACE_BASE);
5898c2ecf20Sopenharmony_ci	ioport_resource.start = 0;
5908c2ecf20Sopenharmony_ci	ioport_resource.end = OCTEON_PCI_IOSPACE_SIZE - 1;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	pr_notice("%s Octeon big bar support\n",
5938c2ecf20Sopenharmony_ci		  (octeon_dma_bar_type ==
5948c2ecf20Sopenharmony_ci		  OCTEON_DMA_BAR_TYPE_BIG) ? "Enabling" : "Disabling");
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	octeon_pci_initialize();
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	mem_access.u64 = 0;
5998c2ecf20Sopenharmony_ci	mem_access.s.esr = 1;	/* Endian-Swap on read. */
6008c2ecf20Sopenharmony_ci	mem_access.s.esw = 1;	/* Endian-Swap on write. */
6018c2ecf20Sopenharmony_ci	mem_access.s.nsr = 0;	/* No-Snoop on read. */
6028c2ecf20Sopenharmony_ci	mem_access.s.nsw = 0;	/* No-Snoop on write. */
6038c2ecf20Sopenharmony_ci	mem_access.s.ror = 0;	/* Relax Read on read. */
6048c2ecf20Sopenharmony_ci	mem_access.s.row = 0;	/* Relax Order on write. */
6058c2ecf20Sopenharmony_ci	mem_access.s.ba = 0;	/* PCI Address bits [63:36]. */
6068c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_NPI_MEM_ACCESS_SUBID3, mem_access.u64);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	/*
6098c2ecf20Sopenharmony_ci	 * Remap the Octeon BAR 2 above all 32 bit devices
6108c2ecf20Sopenharmony_ci	 * (0x8000000000ul).  This is done here so it is remapped
6118c2ecf20Sopenharmony_ci	 * before the readl()'s below. We don't want BAR2 overlapping
6128c2ecf20Sopenharmony_ci	 * with BAR0/BAR1 during these reads.
6138c2ecf20Sopenharmony_ci	 */
6148c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CFG08,
6158c2ecf20Sopenharmony_ci			   (u32)(OCTEON_BAR2_PCI_ADDRESS & 0xffffffffull));
6168c2ecf20Sopenharmony_ci	octeon_npi_write32(CVMX_NPI_PCI_CFG09,
6178c2ecf20Sopenharmony_ci			   (u32)(OCTEON_BAR2_PCI_ADDRESS >> 32));
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG) {
6208c2ecf20Sopenharmony_ci		/* Remap the Octeon BAR 0 to 0-2GB */
6218c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG04, 0);
6228c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG05, 0);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci		/*
6258c2ecf20Sopenharmony_ci		 * Remap the Octeon BAR 1 to map 2GB-4GB (minus the
6268c2ecf20Sopenharmony_ci		 * BAR 1 hole).
6278c2ecf20Sopenharmony_ci		 */
6288c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG06, 2ul << 30);
6298c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG07, 0);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci		/* BAR1 movable mappings set for identity mapping */
6328c2ecf20Sopenharmony_ci		octeon_bar1_pci_phys = 0x80000000ull;
6338c2ecf20Sopenharmony_ci		for (index = 0; index < 32; index++) {
6348c2ecf20Sopenharmony_ci			union cvmx_pci_bar1_indexx bar1_index;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci			bar1_index.u32 = 0;
6378c2ecf20Sopenharmony_ci			/* Address bits[35:22] sent to L2C */
6388c2ecf20Sopenharmony_ci			bar1_index.s.addr_idx =
6398c2ecf20Sopenharmony_ci				(octeon_bar1_pci_phys >> 22) + index;
6408c2ecf20Sopenharmony_ci			/* Don't put PCI accesses in L2. */
6418c2ecf20Sopenharmony_ci			bar1_index.s.ca = 1;
6428c2ecf20Sopenharmony_ci			/* Endian Swap Mode */
6438c2ecf20Sopenharmony_ci			bar1_index.s.end_swp = 1;
6448c2ecf20Sopenharmony_ci			/* Set '1' when the selected address range is valid. */
6458c2ecf20Sopenharmony_ci			bar1_index.s.addr_v = 1;
6468c2ecf20Sopenharmony_ci			octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
6478c2ecf20Sopenharmony_ci					   bar1_index.u32);
6488c2ecf20Sopenharmony_ci		}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci		/* Devices go after BAR1 */
6518c2ecf20Sopenharmony_ci		octeon_pci_mem_resource.start =
6528c2ecf20Sopenharmony_ci			OCTEON_PCI_MEMSPACE_OFFSET + (4ul << 30) -
6538c2ecf20Sopenharmony_ci			(OCTEON_PCI_BAR1_HOLE_SIZE << 20);
6548c2ecf20Sopenharmony_ci		octeon_pci_mem_resource.end =
6558c2ecf20Sopenharmony_ci			octeon_pci_mem_resource.start + (1ul << 30);
6568c2ecf20Sopenharmony_ci	} else {
6578c2ecf20Sopenharmony_ci		/* Remap the Octeon BAR 0 to map 128MB-(128MB+4KB) */
6588c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG04, 128ul << 20);
6598c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG05, 0);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci		/* Remap the Octeon BAR 1 to map 0-128MB */
6628c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG06, 0);
6638c2ecf20Sopenharmony_ci		octeon_npi_write32(CVMX_NPI_PCI_CFG07, 0);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci		/* BAR1 movable regions contiguous to cover the swiotlb */
6668c2ecf20Sopenharmony_ci		octeon_bar1_pci_phys =
6678c2ecf20Sopenharmony_ci			virt_to_phys(octeon_swiotlb) & ~((1ull << 22) - 1);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci		for (index = 0; index < 32; index++) {
6708c2ecf20Sopenharmony_ci			union cvmx_pci_bar1_indexx bar1_index;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci			bar1_index.u32 = 0;
6738c2ecf20Sopenharmony_ci			/* Address bits[35:22] sent to L2C */
6748c2ecf20Sopenharmony_ci			bar1_index.s.addr_idx =
6758c2ecf20Sopenharmony_ci				(octeon_bar1_pci_phys >> 22) + index;
6768c2ecf20Sopenharmony_ci			/* Don't put PCI accesses in L2. */
6778c2ecf20Sopenharmony_ci			bar1_index.s.ca = 1;
6788c2ecf20Sopenharmony_ci			/* Endian Swap Mode */
6798c2ecf20Sopenharmony_ci			bar1_index.s.end_swp = 1;
6808c2ecf20Sopenharmony_ci			/* Set '1' when the selected address range is valid. */
6818c2ecf20Sopenharmony_ci			bar1_index.s.addr_v = 1;
6828c2ecf20Sopenharmony_ci			octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
6838c2ecf20Sopenharmony_ci					   bar1_index.u32);
6848c2ecf20Sopenharmony_ci		}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci		/* Devices go after BAR0 */
6878c2ecf20Sopenharmony_ci		octeon_pci_mem_resource.start =
6888c2ecf20Sopenharmony_ci			OCTEON_PCI_MEMSPACE_OFFSET + (128ul << 20) +
6898c2ecf20Sopenharmony_ci			(4ul << 10);
6908c2ecf20Sopenharmony_ci		octeon_pci_mem_resource.end =
6918c2ecf20Sopenharmony_ci			octeon_pci_mem_resource.start + (1ul << 30);
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	register_pci_controller(&octeon_pci_controller);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/*
6978c2ecf20Sopenharmony_ci	 * Clear any errors that might be pending from before the bus
6988c2ecf20Sopenharmony_ci	 * was setup properly.
6998c2ecf20Sopenharmony_ci	 */
7008c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_NPI_PCI_INT_SUM2, -1);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	if (IS_ERR(platform_device_register_simple("octeon_pci_edac",
7038c2ecf20Sopenharmony_ci						   -1, NULL, 0)))
7048c2ecf20Sopenharmony_ci		pr_err("Registration of co_pci_edac failed!\n");
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	octeon_pci_dma_init();
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	return 0;
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ciarch_initcall(octeon_pci_setup);
712