18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Ralink RT288x SoC PCI register definitions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2009 John Crispin <john@phrozen.org> 68c2ecf20Sopenharmony_ci * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Parts of this file are based on Ralink's 2.6.21 BSP 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/delay.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/pci.h> 148c2ecf20Sopenharmony_ci#include <linux/io.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 178c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 188c2ecf20Sopenharmony_ci#include <linux/of_pci.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <asm/mach-ralink/rt288x.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define RT2880_PCI_BASE 0x00440000 238c2ecf20Sopenharmony_ci#define RT288X_CPU_IRQ_PCI 4 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define RT2880_PCI_MEM_BASE 0x20000000 268c2ecf20Sopenharmony_ci#define RT2880_PCI_MEM_SIZE 0x10000000 278c2ecf20Sopenharmony_ci#define RT2880_PCI_IO_BASE 0x00460000 288c2ecf20Sopenharmony_ci#define RT2880_PCI_IO_SIZE 0x00010000 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_PCICFG_ADDR 0x00 318c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_PCIMSK_ADDR 0x0c 328c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_BAR0SETUP_ADDR 0x10 338c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_IMBASEBAR0_ADDR 0x18 348c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_CONFIG_ADDR 0x20 358c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_CONFIG_DATA 0x24 368c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_MEMBASE 0x28 378c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_IOBASE 0x2c 388c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_ID 0x30 398c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_CLASS 0x34 408c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_SUBID 0x38 418c2ecf20Sopenharmony_ci#define RT2880_PCI_REG_ARBCTL 0x80 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic void __iomem *rt2880_pci_base; 448c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(rt2880_pci_lock); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic u32 rt2880_pci_reg_read(u32 reg) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci return readl(rt2880_pci_base + reg); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic void rt2880_pci_reg_write(u32 val, u32 reg) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci writel(val, rt2880_pci_base + reg); 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic inline u32 rt2880_pci_get_cfgaddr(unsigned int bus, unsigned int slot, 578c2ecf20Sopenharmony_ci unsigned int func, unsigned int where) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci return ((bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 608c2ecf20Sopenharmony_ci 0x80000000); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn, 648c2ecf20Sopenharmony_ci int where, int size, u32 *val) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci unsigned long flags; 678c2ecf20Sopenharmony_ci u32 address; 688c2ecf20Sopenharmony_ci u32 data; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn), 718c2ecf20Sopenharmony_ci PCI_FUNC(devfn), where); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci spin_lock_irqsave(&rt2880_pci_lock, flags); 748c2ecf20Sopenharmony_ci rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 758c2ecf20Sopenharmony_ci data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA); 768c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rt2880_pci_lock, flags); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci switch (size) { 798c2ecf20Sopenharmony_ci case 1: 808c2ecf20Sopenharmony_ci *val = (data >> ((where & 3) << 3)) & 0xff; 818c2ecf20Sopenharmony_ci break; 828c2ecf20Sopenharmony_ci case 2: 838c2ecf20Sopenharmony_ci *val = (data >> ((where & 3) << 3)) & 0xffff; 848c2ecf20Sopenharmony_ci break; 858c2ecf20Sopenharmony_ci case 4: 868c2ecf20Sopenharmony_ci *val = data; 878c2ecf20Sopenharmony_ci break; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn, 948c2ecf20Sopenharmony_ci int where, int size, u32 val) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci unsigned long flags; 978c2ecf20Sopenharmony_ci u32 address; 988c2ecf20Sopenharmony_ci u32 data; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn), 1018c2ecf20Sopenharmony_ci PCI_FUNC(devfn), where); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci spin_lock_irqsave(&rt2880_pci_lock, flags); 1048c2ecf20Sopenharmony_ci rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 1058c2ecf20Sopenharmony_ci data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci switch (size) { 1088c2ecf20Sopenharmony_ci case 1: 1098c2ecf20Sopenharmony_ci data = (data & ~(0xff << ((where & 3) << 3))) | 1108c2ecf20Sopenharmony_ci (val << ((where & 3) << 3)); 1118c2ecf20Sopenharmony_ci break; 1128c2ecf20Sopenharmony_ci case 2: 1138c2ecf20Sopenharmony_ci data = (data & ~(0xffff << ((where & 3) << 3))) | 1148c2ecf20Sopenharmony_ci (val << ((where & 3) << 3)); 1158c2ecf20Sopenharmony_ci break; 1168c2ecf20Sopenharmony_ci case 4: 1178c2ecf20Sopenharmony_ci data = val; 1188c2ecf20Sopenharmony_ci break; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci rt2880_pci_reg_write(data, RT2880_PCI_REG_CONFIG_DATA); 1228c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rt2880_pci_lock, flags); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic struct pci_ops rt2880_pci_ops = { 1288c2ecf20Sopenharmony_ci .read = rt2880_pci_config_read, 1298c2ecf20Sopenharmony_ci .write = rt2880_pci_config_write, 1308c2ecf20Sopenharmony_ci}; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic struct resource rt2880_pci_mem_resource = { 1338c2ecf20Sopenharmony_ci .name = "PCI MEM space", 1348c2ecf20Sopenharmony_ci .start = RT2880_PCI_MEM_BASE, 1358c2ecf20Sopenharmony_ci .end = RT2880_PCI_MEM_BASE + RT2880_PCI_MEM_SIZE - 1, 1368c2ecf20Sopenharmony_ci .flags = IORESOURCE_MEM, 1378c2ecf20Sopenharmony_ci}; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic struct resource rt2880_pci_io_resource = { 1408c2ecf20Sopenharmony_ci .name = "PCI IO space", 1418c2ecf20Sopenharmony_ci .start = RT2880_PCI_IO_BASE, 1428c2ecf20Sopenharmony_ci .end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1, 1438c2ecf20Sopenharmony_ci .flags = IORESOURCE_IO, 1448c2ecf20Sopenharmony_ci}; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic struct pci_controller rt2880_pci_controller = { 1478c2ecf20Sopenharmony_ci .pci_ops = &rt2880_pci_ops, 1488c2ecf20Sopenharmony_ci .mem_resource = &rt2880_pci_mem_resource, 1498c2ecf20Sopenharmony_ci .io_resource = &rt2880_pci_io_resource, 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic inline u32 rt2880_pci_read_u32(unsigned long reg) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci unsigned long flags; 1558c2ecf20Sopenharmony_ci u32 address; 1568c2ecf20Sopenharmony_ci u32 ret; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci address = rt2880_pci_get_cfgaddr(0, 0, 0, reg); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci spin_lock_irqsave(&rt2880_pci_lock, flags); 1618c2ecf20Sopenharmony_ci rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 1628c2ecf20Sopenharmony_ci ret = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA); 1638c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rt2880_pci_lock, flags); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci return ret; 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic inline void rt2880_pci_write_u32(unsigned long reg, u32 val) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci unsigned long flags; 1718c2ecf20Sopenharmony_ci u32 address; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci address = rt2880_pci_get_cfgaddr(0, 0, 0, reg); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci spin_lock_irqsave(&rt2880_pci_lock, flags); 1768c2ecf20Sopenharmony_ci rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 1778c2ecf20Sopenharmony_ci rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA); 1788c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rt2880_pci_lock, flags); 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ciint pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci int irq = -1; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (dev->bus->number != 0) 1868c2ecf20Sopenharmony_ci return irq; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci switch (PCI_SLOT(dev->devfn)) { 1898c2ecf20Sopenharmony_ci case 0x00: 1908c2ecf20Sopenharmony_ci break; 1918c2ecf20Sopenharmony_ci case 0x11: 1928c2ecf20Sopenharmony_ci irq = RT288X_CPU_IRQ_PCI; 1938c2ecf20Sopenharmony_ci break; 1948c2ecf20Sopenharmony_ci default: 1958c2ecf20Sopenharmony_ci pr_err("%s:%s[%d] trying to alloc unknown pci irq\n", 1968c2ecf20Sopenharmony_ci __FILE__, __func__, __LINE__); 1978c2ecf20Sopenharmony_ci BUG(); 1988c2ecf20Sopenharmony_ci break; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci return irq; 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cistatic int rt288x_pci_probe(struct platform_device *pdev) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci void __iomem *io_map_base; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci rt2880_pci_base = ioremap(RT2880_PCI_BASE, PAGE_SIZE); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE); 2118c2ecf20Sopenharmony_ci rt2880_pci_controller.io_map_base = (unsigned long) io_map_base; 2128c2ecf20Sopenharmony_ci set_io_port_base((unsigned long) io_map_base); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci ioport_resource.start = RT2880_PCI_IO_BASE; 2158c2ecf20Sopenharmony_ci ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR); 2188c2ecf20Sopenharmony_ci udelay(1); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL); 2218c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR); 2228c2ecf20Sopenharmony_ci rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE); 2238c2ecf20Sopenharmony_ci rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE); 2248c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR); 2258c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID); 2268c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS); 2278c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID); 2288c2ecf20Sopenharmony_ci rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000); 2318c2ecf20Sopenharmony_ci (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci rt2880_pci_controller.of_node = pdev->dev.of_node; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci register_pci_controller(&rt2880_pci_controller); 2368c2ecf20Sopenharmony_ci return 0; 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ciint pcibios_plat_dev_init(struct pci_dev *dev) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci static bool slot0_init; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci /* 2448c2ecf20Sopenharmony_ci * Nobody seems to initialize slot 0, but this platform requires it, so 2458c2ecf20Sopenharmony_ci * do it once when some other slot is being enabled. The PCI subsystem 2468c2ecf20Sopenharmony_ci * should configure other slots properly, so no need to do anything 2478c2ecf20Sopenharmony_ci * special for those. 2488c2ecf20Sopenharmony_ci */ 2498c2ecf20Sopenharmony_ci if (!slot0_init && dev->bus->number == 0) { 2508c2ecf20Sopenharmony_ci u16 cmd; 2518c2ecf20Sopenharmony_ci u32 bar0; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci slot0_init = true; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci pci_bus_write_config_dword(dev->bus, 0, PCI_BASE_ADDRESS_0, 2568c2ecf20Sopenharmony_ci 0x08000000); 2578c2ecf20Sopenharmony_ci pci_bus_read_config_dword(dev->bus, 0, PCI_BASE_ADDRESS_0, 2588c2ecf20Sopenharmony_ci &bar0); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci pci_bus_read_config_word(dev->bus, 0, PCI_COMMAND, &cmd); 2618c2ecf20Sopenharmony_ci cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY; 2628c2ecf20Sopenharmony_ci pci_bus_write_config_word(dev->bus, 0, PCI_COMMAND, cmd); 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci return 0; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic const struct of_device_id rt288x_pci_match[] = { 2698c2ecf20Sopenharmony_ci { .compatible = "ralink,rt288x-pci" }, 2708c2ecf20Sopenharmony_ci {}, 2718c2ecf20Sopenharmony_ci}; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_cistatic struct platform_driver rt288x_pci_driver = { 2748c2ecf20Sopenharmony_ci .probe = rt288x_pci_probe, 2758c2ecf20Sopenharmony_ci .driver = { 2768c2ecf20Sopenharmony_ci .name = "rt288x-pci", 2778c2ecf20Sopenharmony_ci .of_match_table = rt288x_pci_match, 2788c2ecf20Sopenharmony_ci }, 2798c2ecf20Sopenharmony_ci}; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ciint __init pcibios_init(void) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci int ret = platform_driver_register(&rt288x_pci_driver); 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci if (ret) 2868c2ecf20Sopenharmony_ci pr_info("rt288x-pci: Error registering platform driver!"); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci return ret; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ciarch_initcall(pcibios_init); 292