18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2001,2002,2005 Broadcom Corporation
48c2ecf20Sopenharmony_ci * Copyright (C) 2004 by Ralf Baechle (ralf@linux-mips.org)
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * BCM1x80/1x55-specific PCI support
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This module provides the glue between Linux's PCI subsystem
118c2ecf20Sopenharmony_ci * and the hardware.  We basically provide glue for accessing
128c2ecf20Sopenharmony_ci * configuration space, and set up the translation for I/O
138c2ecf20Sopenharmony_ci * space accesses.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * To access configuration space, we use ioremap.  In the 32-bit
168c2ecf20Sopenharmony_ci * kernel, this consumes either 4 or 8 page table pages, and 16MB of
178c2ecf20Sopenharmony_ci * kernel mapped memory.  Hopefully neither of these should be a huge
188c2ecf20Sopenharmony_ci * problem.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * XXX: AT THIS TIME, ONLY the NATIVE PCI-X INTERFACE IS SUPPORTED.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci#include <linux/types.h>
238c2ecf20Sopenharmony_ci#include <linux/pci.h>
248c2ecf20Sopenharmony_ci#include <linux/kernel.h>
258c2ecf20Sopenharmony_ci#include <linux/init.h>
268c2ecf20Sopenharmony_ci#include <linux/mm.h>
278c2ecf20Sopenharmony_ci#include <linux/console.h>
288c2ecf20Sopenharmony_ci#include <linux/tty.h>
298c2ecf20Sopenharmony_ci#include <linux/vt.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <asm/sibyte/bcm1480_regs.h>
328c2ecf20Sopenharmony_ci#include <asm/sibyte/bcm1480_scd.h>
338c2ecf20Sopenharmony_ci#include <asm/sibyte/board.h>
348c2ecf20Sopenharmony_ci#include <asm/io.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Macros for calculating offsets into config space given a device
388c2ecf20Sopenharmony_ci * structure or dev/fun/reg
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci#define CFGOFFSET(bus, devfn, where) (((bus)<<16)+((devfn)<<8)+(where))
418c2ecf20Sopenharmony_ci#define CFGADDR(bus, devfn, where)   CFGOFFSET((bus)->number, (devfn), where)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void *cfg_space;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define PCI_BUS_ENABLED 1
468c2ecf20Sopenharmony_ci#define PCI_DEVICE_MODE 2
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic int bcm1480_bus_status;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define PCI_BRIDGE_DEVICE  0
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * Read/write 32-bit values in config space.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_cistatic inline u32 READCFG32(u32 addr)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	return *(u32 *)(cfg_space + (addr&~3));
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic inline void WRITECFG32(u32 addr, u32 data)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	*(u32 *)(cfg_space + (addr & ~3)) = data;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciint pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	if (pin == 0)
688c2ecf20Sopenharmony_ci		return -1;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return K_BCM1480_INT_PCI_INTA - 1 + pin;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* Do platform specific device initialization at pci_enable_device() time */
748c2ecf20Sopenharmony_ciint pcibios_plat_dev_init(struct pci_dev *dev)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Some checks before doing config cycles:
818c2ecf20Sopenharmony_ci * In PCI Device Mode, hide everything on bus 0 except the LDT host
828c2ecf20Sopenharmony_ci * bridge.  Otherwise, access is controlled by bridge MasterEn bits.
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_cistatic int bcm1480_pci_can_access(struct pci_bus *bus, int devfn)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	u32 devno;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (!(bcm1480_bus_status & (PCI_BUS_ENABLED | PCI_DEVICE_MODE)))
898c2ecf20Sopenharmony_ci		return 0;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (bus->number == 0) {
928c2ecf20Sopenharmony_ci		devno = PCI_SLOT(devfn);
938c2ecf20Sopenharmony_ci		if (bcm1480_bus_status & PCI_DEVICE_MODE)
948c2ecf20Sopenharmony_ci			return 0;
958c2ecf20Sopenharmony_ci		else
968c2ecf20Sopenharmony_ci			return 1;
978c2ecf20Sopenharmony_ci	} else
988c2ecf20Sopenharmony_ci		return 1;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * Read/write access functions for various sizes of values
1038c2ecf20Sopenharmony_ci * in config space.  Return all 1's for disallowed accesses
1048c2ecf20Sopenharmony_ci * for a kludgy but adequate simulation of master aborts.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int bcm1480_pcibios_read(struct pci_bus *bus, unsigned int devfn,
1088c2ecf20Sopenharmony_ci				int where, int size, u32 * val)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	u32 data = 0;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if ((size == 2) && (where & 1))
1138c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1148c2ecf20Sopenharmony_ci	else if ((size == 4) && (where & 3))
1158c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (bcm1480_pci_can_access(bus, devfn))
1188c2ecf20Sopenharmony_ci		data = READCFG32(CFGADDR(bus, devfn, where));
1198c2ecf20Sopenharmony_ci	else
1208c2ecf20Sopenharmony_ci		data = 0xFFFFFFFF;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (size == 1)
1238c2ecf20Sopenharmony_ci		*val = (data >> ((where & 3) << 3)) & 0xff;
1248c2ecf20Sopenharmony_ci	else if (size == 2)
1258c2ecf20Sopenharmony_ci		*val = (data >> ((where & 3) << 3)) & 0xffff;
1268c2ecf20Sopenharmony_ci	else
1278c2ecf20Sopenharmony_ci		*val = data;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic int bcm1480_pcibios_write(struct pci_bus *bus, unsigned int devfn,
1338c2ecf20Sopenharmony_ci				int where, int size, u32 val)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	u32 cfgaddr = CFGADDR(bus, devfn, where);
1368c2ecf20Sopenharmony_ci	u32 data = 0;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if ((size == 2) && (where & 1))
1398c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1408c2ecf20Sopenharmony_ci	else if ((size == 4) && (where & 3))
1418c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (!bcm1480_pci_can_access(bus, devfn))
1448c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	data = READCFG32(cfgaddr);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (size == 1)
1498c2ecf20Sopenharmony_ci		data = (data & ~(0xff << ((where & 3) << 3))) |
1508c2ecf20Sopenharmony_ci		    (val << ((where & 3) << 3));
1518c2ecf20Sopenharmony_ci	else if (size == 2)
1528c2ecf20Sopenharmony_ci		data = (data & ~(0xffff << ((where & 3) << 3))) |
1538c2ecf20Sopenharmony_ci		    (val << ((where & 3) << 3));
1548c2ecf20Sopenharmony_ci	else
1558c2ecf20Sopenharmony_ci		data = val;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	WRITECFG32(cfgaddr, data);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistruct pci_ops bcm1480_pci_ops = {
1638c2ecf20Sopenharmony_ci	.read	= bcm1480_pcibios_read,
1648c2ecf20Sopenharmony_ci	.write	= bcm1480_pcibios_write,
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic struct resource bcm1480_mem_resource = {
1688c2ecf20Sopenharmony_ci	.name	= "BCM1480 PCI MEM",
1698c2ecf20Sopenharmony_ci	.start	= A_BCM1480_PHYS_PCI_MEM_MATCH_BYTES,
1708c2ecf20Sopenharmony_ci	.end	= A_BCM1480_PHYS_PCI_MEM_MATCH_BYTES + 0xfffffffUL,
1718c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_MEM,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic struct resource bcm1480_io_resource = {
1758c2ecf20Sopenharmony_ci	.name	= "BCM1480 PCI I/O",
1768c2ecf20Sopenharmony_ci	.start	= A_BCM1480_PHYS_PCI_IO_MATCH_BYTES,
1778c2ecf20Sopenharmony_ci	.end	= A_BCM1480_PHYS_PCI_IO_MATCH_BYTES + 0x1ffffffUL,
1788c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_IO,
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistruct pci_controller bcm1480_controller = {
1828c2ecf20Sopenharmony_ci	.pci_ops	= &bcm1480_pci_ops,
1838c2ecf20Sopenharmony_ci	.mem_resource	= &bcm1480_mem_resource,
1848c2ecf20Sopenharmony_ci	.io_resource	= &bcm1480_io_resource,
1858c2ecf20Sopenharmony_ci	.io_offset	= A_BCM1480_PHYS_PCI_IO_MATCH_BYTES,
1868c2ecf20Sopenharmony_ci};
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int __init bcm1480_pcibios_init(void)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	uint32_t cmdreg;
1928c2ecf20Sopenharmony_ci	uint64_t reg;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* CFE will assign PCI resources */
1958c2ecf20Sopenharmony_ci	pci_set_flags(PCI_PROBE_ONLY);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Avoid ISA compat ranges.  */
1988c2ecf20Sopenharmony_ci	PCIBIOS_MIN_IO = 0x00008000UL;
1998c2ecf20Sopenharmony_ci	PCIBIOS_MIN_MEM = 0x01000000UL;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	/* Set I/O resource limits. - unlimited for now to accommodate HT */
2028c2ecf20Sopenharmony_ci	ioport_resource.end = 0xffffffffUL;
2038c2ecf20Sopenharmony_ci	iomem_resource.end = 0xffffffffUL;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	cfg_space = ioremap(A_BCM1480_PHYS_PCI_CFG_MATCH_BITS, 16*1024*1024);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/*
2088c2ecf20Sopenharmony_ci	 * See if the PCI bus has been configured by the firmware.
2098c2ecf20Sopenharmony_ci	 */
2108c2ecf20Sopenharmony_ci	reg = __raw_readq(IOADDR(A_SCD_SYSTEM_CFG));
2118c2ecf20Sopenharmony_ci	if (!(reg & M_BCM1480_SYS_PCI_HOST)) {
2128c2ecf20Sopenharmony_ci		bcm1480_bus_status |= PCI_DEVICE_MODE;
2138c2ecf20Sopenharmony_ci	} else {
2148c2ecf20Sopenharmony_ci		cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0),
2158c2ecf20Sopenharmony_ci					     PCI_COMMAND));
2168c2ecf20Sopenharmony_ci		if (!(cmdreg & PCI_COMMAND_MASTER)) {
2178c2ecf20Sopenharmony_ci			printk
2188c2ecf20Sopenharmony_ci			    ("PCI: Skipping PCI probe.	Bus is not initialized.\n");
2198c2ecf20Sopenharmony_ci			iounmap(cfg_space);
2208c2ecf20Sopenharmony_ci			return 1; /* XXX */
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci		bcm1480_bus_status |= PCI_BUS_ENABLED;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* turn on ExpMemEn */
2268c2ecf20Sopenharmony_ci	cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40));
2278c2ecf20Sopenharmony_ci	WRITECFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40),
2288c2ecf20Sopenharmony_ci			cmdreg | 0x10);
2298c2ecf20Sopenharmony_ci	cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40));
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/*
2328c2ecf20Sopenharmony_ci	 * Establish mappings in KSEG2 (kernel virtual) to PCI I/O
2338c2ecf20Sopenharmony_ci	 * space.  Use "match bytes" policy to make everything look
2348c2ecf20Sopenharmony_ci	 * little-endian.  So, you need to also set
2358c2ecf20Sopenharmony_ci	 * CONFIG_SWAP_IO_SPACE, but this is the combination that
2368c2ecf20Sopenharmony_ci	 * works correctly with most of Linux's drivers.
2378c2ecf20Sopenharmony_ci	 * XXX ehs: Should this happen in PCI Device mode?
2388c2ecf20Sopenharmony_ci	 */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	bcm1480_controller.io_map_base = (unsigned long)
2418c2ecf20Sopenharmony_ci		ioremap(A_BCM1480_PHYS_PCI_IO_MATCH_BYTES, 65536);
2428c2ecf20Sopenharmony_ci	bcm1480_controller.io_map_base -= bcm1480_controller.io_offset;
2438c2ecf20Sopenharmony_ci	set_io_port_base(bcm1480_controller.io_map_base);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	register_pci_controller(&bcm1480_controller);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#ifdef CONFIG_VGA_CONSOLE
2488c2ecf20Sopenharmony_ci	console_lock();
2498c2ecf20Sopenharmony_ci	do_take_over_console(&vga_con, 0, MAX_NR_CONSOLES-1, 1);
2508c2ecf20Sopenharmony_ci	console_unlock();
2518c2ecf20Sopenharmony_ci#endif
2528c2ecf20Sopenharmony_ci	return 0;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ciarch_initcall(bcm1480_pcibios_init);
256