18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * arch/arm/mach-mv78xx0/pcie.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * PCIe functions for Marvell MV78xx0 SoCs
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public
78c2ecf20Sopenharmony_ci * License version 2.  This program is licensed "as is" without any
88c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/pci.h>
138c2ecf20Sopenharmony_ci#include <linux/mbus.h>
148c2ecf20Sopenharmony_ci#include <video/vga.h>
158c2ecf20Sopenharmony_ci#include <asm/irq.h>
168c2ecf20Sopenharmony_ci#include <asm/mach/pci.h>
178c2ecf20Sopenharmony_ci#include <plat/pcie.h>
188c2ecf20Sopenharmony_ci#include "mv78xx0.h"
198c2ecf20Sopenharmony_ci#include "common.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define MV78XX0_MBUS_PCIE_MEM_TARGET(port, lane) ((port) ? 8 : 4)
228c2ecf20Sopenharmony_ci#define MV78XX0_MBUS_PCIE_MEM_ATTR(port, lane)   (0xf8 & ~(0x10 << (lane)))
238c2ecf20Sopenharmony_ci#define MV78XX0_MBUS_PCIE_IO_TARGET(port, lane)  ((port) ? 8 : 4)
248c2ecf20Sopenharmony_ci#define MV78XX0_MBUS_PCIE_IO_ATTR(port, lane)    (0xf0 & ~(0x10 << (lane)))
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct pcie_port {
278c2ecf20Sopenharmony_ci	u8			maj;
288c2ecf20Sopenharmony_ci	u8			min;
298c2ecf20Sopenharmony_ci	u8			root_bus_nr;
308c2ecf20Sopenharmony_ci	void __iomem		*base;
318c2ecf20Sopenharmony_ci	spinlock_t		conf_lock;
328c2ecf20Sopenharmony_ci	char			mem_space_name[20];
338c2ecf20Sopenharmony_ci	struct resource		res;
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic struct pcie_port pcie_port[8];
378c2ecf20Sopenharmony_cistatic int num_pcie_ports;
388c2ecf20Sopenharmony_cistatic struct resource pcie_io_space;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_civoid __init mv78xx0_pcie_id(u32 *dev, u32 *rev)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	*dev = orion_pcie_dev_id(PCIE00_VIRT_BASE);
438c2ecf20Sopenharmony_ci	*rev = orion_pcie_rev(PCIE00_VIRT_BASE);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciu32 pcie_port_size[8] = {
478c2ecf20Sopenharmony_ci	0,
488c2ecf20Sopenharmony_ci	0x30000000,
498c2ecf20Sopenharmony_ci	0x10000000,
508c2ecf20Sopenharmony_ci	0x10000000,
518c2ecf20Sopenharmony_ci	0x08000000,
528c2ecf20Sopenharmony_ci	0x08000000,
538c2ecf20Sopenharmony_ci	0x08000000,
548c2ecf20Sopenharmony_ci	0x04000000,
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void __init mv78xx0_pcie_preinit(void)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	int i;
608c2ecf20Sopenharmony_ci	u32 size_each;
618c2ecf20Sopenharmony_ci	u32 start;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	pcie_io_space.name = "PCIe I/O Space";
648c2ecf20Sopenharmony_ci	pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0);
658c2ecf20Sopenharmony_ci	pcie_io_space.end =
668c2ecf20Sopenharmony_ci		MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1;
678c2ecf20Sopenharmony_ci	pcie_io_space.flags = IORESOURCE_MEM;
688c2ecf20Sopenharmony_ci	if (request_resource(&iomem_resource, &pcie_io_space))
698c2ecf20Sopenharmony_ci		panic("can't allocate PCIe I/O space");
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (num_pcie_ports > 7)
728c2ecf20Sopenharmony_ci		panic("invalid number of PCIe ports");
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	size_each = pcie_port_size[num_pcie_ports];
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	start = MV78XX0_PCIE_MEM_PHYS_BASE;
778c2ecf20Sopenharmony_ci	for (i = 0; i < num_pcie_ports; i++) {
788c2ecf20Sopenharmony_ci		struct pcie_port *pp = pcie_port + i;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
818c2ecf20Sopenharmony_ci			"PCIe %d.%d MEM", pp->maj, pp->min);
828c2ecf20Sopenharmony_ci		pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
838c2ecf20Sopenharmony_ci		pp->res.name = pp->mem_space_name;
848c2ecf20Sopenharmony_ci		pp->res.flags = IORESOURCE_MEM;
858c2ecf20Sopenharmony_ci		pp->res.start = start;
868c2ecf20Sopenharmony_ci		pp->res.end = start + size_each - 1;
878c2ecf20Sopenharmony_ci		start += size_each;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci		if (request_resource(&iomem_resource, &pp->res))
908c2ecf20Sopenharmony_ci			panic("can't allocate PCIe MEM sub-space");
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		mvebu_mbus_add_window_by_id(MV78XX0_MBUS_PCIE_MEM_TARGET(pp->maj, pp->min),
938c2ecf20Sopenharmony_ci					    MV78XX0_MBUS_PCIE_MEM_ATTR(pp->maj, pp->min),
948c2ecf20Sopenharmony_ci					    pp->res.start, resource_size(&pp->res));
958c2ecf20Sopenharmony_ci		mvebu_mbus_add_window_remap_by_id(MV78XX0_MBUS_PCIE_IO_TARGET(pp->maj, pp->min),
968c2ecf20Sopenharmony_ci						  MV78XX0_MBUS_PCIE_IO_ATTR(pp->maj, pp->min),
978c2ecf20Sopenharmony_ci						  i * SZ_64K, SZ_64K, 0);
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct pcie_port *pp;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (nr >= num_pcie_ports)
1068c2ecf20Sopenharmony_ci		return 0;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	pp = &pcie_port[nr];
1098c2ecf20Sopenharmony_ci	sys->private_data = pp;
1108c2ecf20Sopenharmony_ci	pp->root_bus_nr = sys->busnr;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/*
1138c2ecf20Sopenharmony_ci	 * Generic PCIe unit setup.
1148c2ecf20Sopenharmony_ci	 */
1158c2ecf20Sopenharmony_ci	orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
1168c2ecf20Sopenharmony_ci	orion_pcie_setup(pp->base);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	pci_ioremap_io(nr * SZ_64K, MV78XX0_PCIE_IO_PHYS_BASE(nr));
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	return 1;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 * Don't go out when trying to access nonexisting devices
1298c2ecf20Sopenharmony_ci	 * on the local bus.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	if (bus == pp->root_bus_nr && dev > 1)
1328c2ecf20Sopenharmony_ci		return 0;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return 1;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
1388c2ecf20Sopenharmony_ci			int size, u32 *val)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct pci_sys_data *sys = bus->sysdata;
1418c2ecf20Sopenharmony_ci	struct pcie_port *pp = sys->private_data;
1428c2ecf20Sopenharmony_ci	unsigned long flags;
1438c2ecf20Sopenharmony_ci	int ret;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
1468c2ecf20Sopenharmony_ci		*val = 0xffffffff;
1478c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	spin_lock_irqsave(&pp->conf_lock, flags);
1518c2ecf20Sopenharmony_ci	ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
1528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&pp->conf_lock, flags);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	return ret;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
1588c2ecf20Sopenharmony_ci			int where, int size, u32 val)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	struct pci_sys_data *sys = bus->sysdata;
1618c2ecf20Sopenharmony_ci	struct pcie_port *pp = sys->private_data;
1628c2ecf20Sopenharmony_ci	unsigned long flags;
1638c2ecf20Sopenharmony_ci	int ret;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
1668c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	spin_lock_irqsave(&pp->conf_lock, flags);
1698c2ecf20Sopenharmony_ci	ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
1708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&pp->conf_lock, flags);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return ret;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct pci_ops pcie_ops = {
1768c2ecf20Sopenharmony_ci	.read = pcie_rd_conf,
1778c2ecf20Sopenharmony_ci	.write = pcie_wr_conf,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic void rc_pci_fixup(struct pci_dev *dev)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	/*
1838c2ecf20Sopenharmony_ci	 * Prevent enumeration of root complex.
1848c2ecf20Sopenharmony_ci	 */
1858c2ecf20Sopenharmony_ci	if (dev->bus->parent == NULL && dev->devfn == 0) {
1868c2ecf20Sopenharmony_ci		int i;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1898c2ecf20Sopenharmony_ci			dev->resource[i].start = 0;
1908c2ecf20Sopenharmony_ci			dev->resource[i].end   = 0;
1918c2ecf20Sopenharmony_ci			dev->resource[i].flags = 0;
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic int __init mv78xx0_pcie_scan_bus(int nr, struct pci_host_bridge *bridge)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct pci_sys_data *sys = pci_host_bridge_priv(bridge);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (nr >= num_pcie_ports) {
2028c2ecf20Sopenharmony_ci		BUG();
2038c2ecf20Sopenharmony_ci		return -EINVAL;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	list_splice_init(&sys->resources, &bridge->windows);
2078c2ecf20Sopenharmony_ci	bridge->dev.parent = NULL;
2088c2ecf20Sopenharmony_ci	bridge->sysdata = sys;
2098c2ecf20Sopenharmony_ci	bridge->busnr = sys->busnr;
2108c2ecf20Sopenharmony_ci	bridge->ops = &pcie_ops;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return pci_scan_root_bus_bridge(bridge);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
2168c2ecf20Sopenharmony_ci	u8 pin)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct pci_sys_data *sys = dev->bus->sysdata;
2198c2ecf20Sopenharmony_ci	struct pcie_port *pp = sys->private_data;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic struct hw_pci mv78xx0_pci __initdata = {
2258c2ecf20Sopenharmony_ci	.nr_controllers	= 8,
2268c2ecf20Sopenharmony_ci	.preinit	= mv78xx0_pcie_preinit,
2278c2ecf20Sopenharmony_ci	.setup		= mv78xx0_pcie_setup,
2288c2ecf20Sopenharmony_ci	.scan		= mv78xx0_pcie_scan_bus,
2298c2ecf20Sopenharmony_ci	.map_irq	= mv78xx0_pcie_map_irq,
2308c2ecf20Sopenharmony_ci};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic void __init add_pcie_port(int maj, int min, void __iomem *base)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	if (orion_pcie_link_up(base)) {
2378c2ecf20Sopenharmony_ci		struct pcie_port *pp = &pcie_port[num_pcie_ports++];
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		printk("link up\n");
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		pp->maj = maj;
2428c2ecf20Sopenharmony_ci		pp->min = min;
2438c2ecf20Sopenharmony_ci		pp->root_bus_nr = -1;
2448c2ecf20Sopenharmony_ci		pp->base = base;
2458c2ecf20Sopenharmony_ci		spin_lock_init(&pp->conf_lock);
2468c2ecf20Sopenharmony_ci		memset(&pp->res, 0, sizeof(pp->res));
2478c2ecf20Sopenharmony_ci	} else {
2488c2ecf20Sopenharmony_ci		printk("link down, ignoring\n");
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_civoid __init mv78xx0_pcie_init(int init_port0, int init_port1)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (init_port0) {
2578c2ecf20Sopenharmony_ci		add_pcie_port(0, 0, PCIE00_VIRT_BASE);
2588c2ecf20Sopenharmony_ci		if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) {
2598c2ecf20Sopenharmony_ci			add_pcie_port(0, 1, PCIE01_VIRT_BASE);
2608c2ecf20Sopenharmony_ci			add_pcie_port(0, 2, PCIE02_VIRT_BASE);
2618c2ecf20Sopenharmony_ci			add_pcie_port(0, 3, PCIE03_VIRT_BASE);
2628c2ecf20Sopenharmony_ci		}
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (init_port1) {
2668c2ecf20Sopenharmony_ci		add_pcie_port(1, 0, PCIE10_VIRT_BASE);
2678c2ecf20Sopenharmony_ci		if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
2688c2ecf20Sopenharmony_ci			add_pcie_port(1, 1, PCIE11_VIRT_BASE);
2698c2ecf20Sopenharmony_ci			add_pcie_port(1, 2, PCIE12_VIRT_BASE);
2708c2ecf20Sopenharmony_ci			add_pcie_port(1, 3, PCIE13_VIRT_BASE);
2718c2ecf20Sopenharmony_ci		}
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	pci_common_init(&mv78xx0_pci);
2758c2ecf20Sopenharmony_ci}
276