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 * BCM1480/1455-specific HT support (looking like PCI)
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 */
218c2ecf20Sopenharmony_ci#include <linux/types.h>
228c2ecf20Sopenharmony_ci#include <linux/pci.h>
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci#include <linux/init.h>
258c2ecf20Sopenharmony_ci#include <linux/mm.h>
268c2ecf20Sopenharmony_ci#include <linux/console.h>
278c2ecf20Sopenharmony_ci#include <linux/tty.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <asm/sibyte/bcm1480_regs.h>
308c2ecf20Sopenharmony_ci#include <asm/sibyte/bcm1480_scd.h>
318c2ecf20Sopenharmony_ci#include <asm/sibyte/board.h>
328c2ecf20Sopenharmony_ci#include <asm/io.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Macros for calculating offsets into config space given a device
368c2ecf20Sopenharmony_ci * structure or dev/fun/reg
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci#define CFGOFFSET(bus, devfn, where) (((bus)<<16)+((devfn)<<8)+(where))
398c2ecf20Sopenharmony_ci#define CFGADDR(bus, devfn, where)   CFGOFFSET((bus)->number, (devfn), where)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic void *ht_cfg_space;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define PCI_BUS_ENABLED 1
448c2ecf20Sopenharmony_ci#define PCI_DEVICE_MODE 2
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int bcm1480ht_bus_status;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define PCI_BRIDGE_DEVICE  0
498c2ecf20Sopenharmony_ci#define HT_BRIDGE_DEVICE   1
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * HT's level-sensitive interrupts require EOI, which is generated
538c2ecf20Sopenharmony_ci * through a 4MB memory-mapped region
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ciunsigned long ht_eoi_space;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci * Read/write 32-bit values in config space.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistatic inline u32 READCFG32(u32 addr)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return *(u32 *)(ht_cfg_space + (addr&~3));
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic inline void WRITECFG32(u32 addr, u32 data)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	*(u32 *)(ht_cfg_space + (addr & ~3)) = data;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Some checks before doing config cycles:
728c2ecf20Sopenharmony_ci * In PCI Device Mode, hide everything on bus 0 except the LDT host
738c2ecf20Sopenharmony_ci * bridge.  Otherwise, access is controlled by bridge MasterEn bits.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic int bcm1480ht_can_access(struct pci_bus *bus, int devfn)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	u32 devno;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (!(bcm1480ht_bus_status & (PCI_BUS_ENABLED | PCI_DEVICE_MODE)))
808c2ecf20Sopenharmony_ci		return 0;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (bus->number == 0) {
838c2ecf20Sopenharmony_ci		devno = PCI_SLOT(devfn);
848c2ecf20Sopenharmony_ci		if (bcm1480ht_bus_status & PCI_DEVICE_MODE)
858c2ecf20Sopenharmony_ci			return 0;
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci	return 1;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/*
918c2ecf20Sopenharmony_ci * Read/write access functions for various sizes of values
928c2ecf20Sopenharmony_ci * in config space.  Return all 1's for disallowed accesses
938c2ecf20Sopenharmony_ci * for a kludgy but adequate simulation of master aborts.
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic int bcm1480ht_pcibios_read(struct pci_bus *bus, unsigned int devfn,
978c2ecf20Sopenharmony_ci				  int where, int size, u32 * val)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	u32 data = 0;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if ((size == 2) && (where & 1))
1028c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1038c2ecf20Sopenharmony_ci	else if ((size == 4) && (where & 3))
1048c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (bcm1480ht_can_access(bus, devfn))
1078c2ecf20Sopenharmony_ci		data = READCFG32(CFGADDR(bus, devfn, where));
1088c2ecf20Sopenharmony_ci	else
1098c2ecf20Sopenharmony_ci		data = 0xFFFFFFFF;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (size == 1)
1128c2ecf20Sopenharmony_ci		*val = (data >> ((where & 3) << 3)) & 0xff;
1138c2ecf20Sopenharmony_ci	else if (size == 2)
1148c2ecf20Sopenharmony_ci		*val = (data >> ((where & 3) << 3)) & 0xffff;
1158c2ecf20Sopenharmony_ci	else
1168c2ecf20Sopenharmony_ci		*val = data;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic int bcm1480ht_pcibios_write(struct pci_bus *bus, unsigned int devfn,
1228c2ecf20Sopenharmony_ci				   int where, int size, u32 val)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	u32 cfgaddr = CFGADDR(bus, devfn, where);
1258c2ecf20Sopenharmony_ci	u32 data = 0;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	if ((size == 2) && (where & 1))
1288c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1298c2ecf20Sopenharmony_ci	else if ((size == 4) && (where & 3))
1308c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (!bcm1480ht_can_access(bus, devfn))
1338c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	data = READCFG32(cfgaddr);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (size == 1)
1388c2ecf20Sopenharmony_ci		data = (data & ~(0xff << ((where & 3) << 3))) |
1398c2ecf20Sopenharmony_ci		    (val << ((where & 3) << 3));
1408c2ecf20Sopenharmony_ci	else if (size == 2)
1418c2ecf20Sopenharmony_ci		data = (data & ~(0xffff << ((where & 3) << 3))) |
1428c2ecf20Sopenharmony_ci		    (val << ((where & 3) << 3));
1438c2ecf20Sopenharmony_ci	else
1448c2ecf20Sopenharmony_ci		data = val;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	WRITECFG32(cfgaddr, data);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int bcm1480ht_pcibios_get_busno(void)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	return 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistruct pci_ops bcm1480ht_pci_ops = {
1578c2ecf20Sopenharmony_ci	.read	= bcm1480ht_pcibios_read,
1588c2ecf20Sopenharmony_ci	.write	= bcm1480ht_pcibios_write,
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic struct resource bcm1480ht_mem_resource = {
1628c2ecf20Sopenharmony_ci	.name	= "BCM1480 HT MEM",
1638c2ecf20Sopenharmony_ci	.start	= A_BCM1480_PHYS_HT_MEM_MATCH_BYTES,
1648c2ecf20Sopenharmony_ci	.end	= A_BCM1480_PHYS_HT_MEM_MATCH_BYTES + 0x1fffffffUL,
1658c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_MEM,
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic struct resource bcm1480ht_io_resource = {
1698c2ecf20Sopenharmony_ci	.name	= "BCM1480 HT I/O",
1708c2ecf20Sopenharmony_ci	.start	= A_BCM1480_PHYS_HT_IO_MATCH_BYTES,
1718c2ecf20Sopenharmony_ci	.end	= A_BCM1480_PHYS_HT_IO_MATCH_BYTES + 0x01ffffffUL,
1728c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_IO,
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistruct pci_controller bcm1480ht_controller = {
1768c2ecf20Sopenharmony_ci	.pci_ops	= &bcm1480ht_pci_ops,
1778c2ecf20Sopenharmony_ci	.mem_resource	= &bcm1480ht_mem_resource,
1788c2ecf20Sopenharmony_ci	.io_resource	= &bcm1480ht_io_resource,
1798c2ecf20Sopenharmony_ci	.index		= 1,
1808c2ecf20Sopenharmony_ci	.get_busno	= bcm1480ht_pcibios_get_busno,
1818c2ecf20Sopenharmony_ci	.io_offset	= A_BCM1480_PHYS_HT_IO_MATCH_BYTES,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int __init bcm1480ht_pcibios_init(void)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	ht_cfg_space = ioremap(A_BCM1480_PHYS_HT_CFG_MATCH_BITS, 16*1024*1024);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* CFE doesn't always init all HT paths, so we always scan */
1898c2ecf20Sopenharmony_ci	bcm1480ht_bus_status |= PCI_BUS_ENABLED;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	ht_eoi_space = (unsigned long)
1928c2ecf20Sopenharmony_ci		ioremap(A_BCM1480_PHYS_HT_SPECIAL_MATCH_BYTES,
1938c2ecf20Sopenharmony_ci			4 * 1024 * 1024);
1948c2ecf20Sopenharmony_ci	bcm1480ht_controller.io_map_base = (unsigned long)
1958c2ecf20Sopenharmony_ci		ioremap(A_BCM1480_PHYS_HT_IO_MATCH_BYTES, 65536);
1968c2ecf20Sopenharmony_ci	bcm1480ht_controller.io_map_base -= bcm1480ht_controller.io_offset;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	register_pci_controller(&bcm1480ht_controller);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciarch_initcall(bcm1480ht_pcibios_init);
204