1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2187c26ddSJohn Crispin /* 3187c26ddSJohn Crispin * Ralink RT288x SoC PCI register definitions 4187c26ddSJohn Crispin * 597b92108SJohn Crispin * Copyright (C) 2009 John Crispin <john@phrozen.org> 6187c26ddSJohn Crispin * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org> 7187c26ddSJohn Crispin * 8187c26ddSJohn Crispin * Parts of this file are based on Ralink's 2.6.21 BSP 9187c26ddSJohn Crispin */ 10187c26ddSJohn Crispin 11c861519fSRalf Baechle #include <linux/delay.h> 12187c26ddSJohn Crispin #include <linux/types.h> 13187c26ddSJohn Crispin #include <linux/pci.h> 14187c26ddSJohn Crispin #include <linux/io.h> 15187c26ddSJohn Crispin #include <linux/init.h> 16187c26ddSJohn Crispin #include <linux/of_platform.h> 17187c26ddSJohn Crispin #include <linux/of_irq.h> 18187c26ddSJohn Crispin #include <linux/of_pci.h> 19187c26ddSJohn Crispin 20187c26ddSJohn Crispin #include <asm/mach-ralink/rt288x.h> 21187c26ddSJohn Crispin 22187c26ddSJohn Crispin #define RT2880_PCI_BASE 0x00440000 23187c26ddSJohn Crispin #define RT288X_CPU_IRQ_PCI 4 24187c26ddSJohn Crispin 25187c26ddSJohn Crispin #define RT2880_PCI_MEM_BASE 0x20000000 26187c26ddSJohn Crispin #define RT2880_PCI_MEM_SIZE 0x10000000 27187c26ddSJohn Crispin #define RT2880_PCI_IO_BASE 0x00460000 28187c26ddSJohn Crispin #define RT2880_PCI_IO_SIZE 0x00010000 29187c26ddSJohn Crispin 30187c26ddSJohn Crispin #define RT2880_PCI_REG_PCICFG_ADDR 0x00 31187c26ddSJohn Crispin #define RT2880_PCI_REG_PCIMSK_ADDR 0x0c 32187c26ddSJohn Crispin #define RT2880_PCI_REG_BAR0SETUP_ADDR 0x10 33187c26ddSJohn Crispin #define RT2880_PCI_REG_IMBASEBAR0_ADDR 0x18 34187c26ddSJohn Crispin #define RT2880_PCI_REG_CONFIG_ADDR 0x20 35187c26ddSJohn Crispin #define RT2880_PCI_REG_CONFIG_DATA 0x24 36187c26ddSJohn Crispin #define RT2880_PCI_REG_MEMBASE 0x28 37187c26ddSJohn Crispin #define RT2880_PCI_REG_IOBASE 0x2c 38187c26ddSJohn Crispin #define RT2880_PCI_REG_ID 0x30 39187c26ddSJohn Crispin #define RT2880_PCI_REG_CLASS 0x34 40187c26ddSJohn Crispin #define RT2880_PCI_REG_SUBID 0x38 41187c26ddSJohn Crispin #define RT2880_PCI_REG_ARBCTL 0x80 42187c26ddSJohn Crispin 43187c26ddSJohn Crispin static void __iomem *rt2880_pci_base; 44187c26ddSJohn Crispin static DEFINE_SPINLOCK(rt2880_pci_lock); 45187c26ddSJohn Crispin 46187c26ddSJohn Crispin static u32 rt2880_pci_reg_read(u32 reg) 47187c26ddSJohn Crispin { 48187c26ddSJohn Crispin return readl(rt2880_pci_base + reg); 49187c26ddSJohn Crispin } 50187c26ddSJohn Crispin 51187c26ddSJohn Crispin static void rt2880_pci_reg_write(u32 val, u32 reg) 52187c26ddSJohn Crispin { 53187c26ddSJohn Crispin writel(val, rt2880_pci_base + reg); 54187c26ddSJohn Crispin } 55187c26ddSJohn Crispin 56187c26ddSJohn Crispin static inline u32 rt2880_pci_get_cfgaddr(unsigned int bus, unsigned int slot, 57187c26ddSJohn Crispin unsigned int func, unsigned int where) 58187c26ddSJohn Crispin { 59187c26ddSJohn Crispin return ((bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 60187c26ddSJohn Crispin 0x80000000); 61187c26ddSJohn Crispin } 62187c26ddSJohn Crispin 63187c26ddSJohn Crispin static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn, 64187c26ddSJohn Crispin int where, int size, u32 *val) 65187c26ddSJohn Crispin { 66187c26ddSJohn Crispin unsigned long flags; 67187c26ddSJohn Crispin u32 address; 68187c26ddSJohn Crispin u32 data; 69187c26ddSJohn Crispin 70187c26ddSJohn Crispin address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn), 71187c26ddSJohn Crispin PCI_FUNC(devfn), where); 72187c26ddSJohn Crispin 73187c26ddSJohn Crispin spin_lock_irqsave(&rt2880_pci_lock, flags); 74187c26ddSJohn Crispin rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 75187c26ddSJohn Crispin data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA); 76187c26ddSJohn Crispin spin_unlock_irqrestore(&rt2880_pci_lock, flags); 77187c26ddSJohn Crispin 78187c26ddSJohn Crispin switch (size) { 79187c26ddSJohn Crispin case 1: 80187c26ddSJohn Crispin *val = (data >> ((where & 3) << 3)) & 0xff; 81187c26ddSJohn Crispin break; 82187c26ddSJohn Crispin case 2: 83187c26ddSJohn Crispin *val = (data >> ((where & 3) << 3)) & 0xffff; 84187c26ddSJohn Crispin break; 85187c26ddSJohn Crispin case 4: 86187c26ddSJohn Crispin *val = data; 87187c26ddSJohn Crispin break; 88187c26ddSJohn Crispin } 89187c26ddSJohn Crispin 90187c26ddSJohn Crispin return PCIBIOS_SUCCESSFUL; 91187c26ddSJohn Crispin } 92187c26ddSJohn Crispin 93187c26ddSJohn Crispin static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn, 94187c26ddSJohn Crispin int where, int size, u32 val) 95187c26ddSJohn Crispin { 96187c26ddSJohn Crispin unsigned long flags; 97187c26ddSJohn Crispin u32 address; 98187c26ddSJohn Crispin u32 data; 99187c26ddSJohn Crispin 100187c26ddSJohn Crispin address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn), 101187c26ddSJohn Crispin PCI_FUNC(devfn), where); 102187c26ddSJohn Crispin 103187c26ddSJohn Crispin spin_lock_irqsave(&rt2880_pci_lock, flags); 104187c26ddSJohn Crispin rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 105187c26ddSJohn Crispin data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA); 106187c26ddSJohn Crispin 107187c26ddSJohn Crispin switch (size) { 108187c26ddSJohn Crispin case 1: 109187c26ddSJohn Crispin data = (data & ~(0xff << ((where & 3) << 3))) | 110187c26ddSJohn Crispin (val << ((where & 3) << 3)); 111187c26ddSJohn Crispin break; 112187c26ddSJohn Crispin case 2: 113187c26ddSJohn Crispin data = (data & ~(0xffff << ((where & 3) << 3))) | 114187c26ddSJohn Crispin (val << ((where & 3) << 3)); 115187c26ddSJohn Crispin break; 116187c26ddSJohn Crispin case 4: 117187c26ddSJohn Crispin data = val; 118187c26ddSJohn Crispin break; 119187c26ddSJohn Crispin } 120187c26ddSJohn Crispin 121187c26ddSJohn Crispin rt2880_pci_reg_write(data, RT2880_PCI_REG_CONFIG_DATA); 122187c26ddSJohn Crispin spin_unlock_irqrestore(&rt2880_pci_lock, flags); 123187c26ddSJohn Crispin 124187c26ddSJohn Crispin return PCIBIOS_SUCCESSFUL; 125187c26ddSJohn Crispin } 126187c26ddSJohn Crispin 127187c26ddSJohn Crispin static struct pci_ops rt2880_pci_ops = { 128187c26ddSJohn Crispin .read = rt2880_pci_config_read, 129187c26ddSJohn Crispin .write = rt2880_pci_config_write, 130187c26ddSJohn Crispin }; 131187c26ddSJohn Crispin 132187c26ddSJohn Crispin static struct resource rt2880_pci_mem_resource = { 133187c26ddSJohn Crispin .name = "PCI MEM space", 134187c26ddSJohn Crispin .start = RT2880_PCI_MEM_BASE, 135187c26ddSJohn Crispin .end = RT2880_PCI_MEM_BASE + RT2880_PCI_MEM_SIZE - 1, 136187c26ddSJohn Crispin .flags = IORESOURCE_MEM, 137187c26ddSJohn Crispin }; 138187c26ddSJohn Crispin 139187c26ddSJohn Crispin static struct resource rt2880_pci_io_resource = { 140187c26ddSJohn Crispin .name = "PCI IO space", 141187c26ddSJohn Crispin .start = RT2880_PCI_IO_BASE, 142187c26ddSJohn Crispin .end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1, 143187c26ddSJohn Crispin .flags = IORESOURCE_IO, 144187c26ddSJohn Crispin }; 145187c26ddSJohn Crispin 146187c26ddSJohn Crispin static struct pci_controller rt2880_pci_controller = { 147187c26ddSJohn Crispin .pci_ops = &rt2880_pci_ops, 148187c26ddSJohn Crispin .mem_resource = &rt2880_pci_mem_resource, 149187c26ddSJohn Crispin .io_resource = &rt2880_pci_io_resource, 150187c26ddSJohn Crispin }; 151187c26ddSJohn Crispin 152187c26ddSJohn Crispin static inline u32 rt2880_pci_read_u32(unsigned long reg) 153187c26ddSJohn Crispin { 154187c26ddSJohn Crispin unsigned long flags; 155187c26ddSJohn Crispin u32 address; 156187c26ddSJohn Crispin u32 ret; 157187c26ddSJohn Crispin 158187c26ddSJohn Crispin address = rt2880_pci_get_cfgaddr(0, 0, 0, reg); 159187c26ddSJohn Crispin 160187c26ddSJohn Crispin spin_lock_irqsave(&rt2880_pci_lock, flags); 161187c26ddSJohn Crispin rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 162187c26ddSJohn Crispin ret = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA); 163187c26ddSJohn Crispin spin_unlock_irqrestore(&rt2880_pci_lock, flags); 164187c26ddSJohn Crispin 165187c26ddSJohn Crispin return ret; 166187c26ddSJohn Crispin } 167187c26ddSJohn Crispin 168187c26ddSJohn Crispin static inline void rt2880_pci_write_u32(unsigned long reg, u32 val) 169187c26ddSJohn Crispin { 170187c26ddSJohn Crispin unsigned long flags; 171187c26ddSJohn Crispin u32 address; 172187c26ddSJohn Crispin 173187c26ddSJohn Crispin address = rt2880_pci_get_cfgaddr(0, 0, 0, reg); 174187c26ddSJohn Crispin 175187c26ddSJohn Crispin spin_lock_irqsave(&rt2880_pci_lock, flags); 176187c26ddSJohn Crispin rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR); 177187c26ddSJohn Crispin rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA); 178187c26ddSJohn Crispin spin_unlock_irqrestore(&rt2880_pci_lock, flags); 179187c26ddSJohn Crispin } 180187c26ddSJohn Crispin 1818eba3651SManuel Lauss int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 182187c26ddSJohn Crispin { 183187c26ddSJohn Crispin int irq = -1; 184187c26ddSJohn Crispin 185187c26ddSJohn Crispin if (dev->bus->number != 0) 186187c26ddSJohn Crispin return irq; 187187c26ddSJohn Crispin 188187c26ddSJohn Crispin switch (PCI_SLOT(dev->devfn)) { 189187c26ddSJohn Crispin case 0x00: 190187c26ddSJohn Crispin break; 191187c26ddSJohn Crispin case 0x11: 192187c26ddSJohn Crispin irq = RT288X_CPU_IRQ_PCI; 193187c26ddSJohn Crispin break; 194187c26ddSJohn Crispin default: 195187c26ddSJohn Crispin pr_err("%s:%s[%d] trying to alloc unknown pci irq\n", 196187c26ddSJohn Crispin __FILE__, __func__, __LINE__); 197187c26ddSJohn Crispin BUG(); 198187c26ddSJohn Crispin break; 199187c26ddSJohn Crispin } 200187c26ddSJohn Crispin 201187c26ddSJohn Crispin return irq; 202187c26ddSJohn Crispin } 203187c26ddSJohn Crispin 204187c26ddSJohn Crispin static int rt288x_pci_probe(struct platform_device *pdev) 205187c26ddSJohn Crispin { 206187c26ddSJohn Crispin void __iomem *io_map_base; 207187c26ddSJohn Crispin 2084bdc0d67SChristoph Hellwig rt2880_pci_base = ioremap(RT2880_PCI_BASE, PAGE_SIZE); 209187c26ddSJohn Crispin 210187c26ddSJohn Crispin io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE); 211187c26ddSJohn Crispin rt2880_pci_controller.io_map_base = (unsigned long) io_map_base; 212187c26ddSJohn Crispin set_io_port_base((unsigned long) io_map_base); 213187c26ddSJohn Crispin 214187c26ddSJohn Crispin ioport_resource.start = RT2880_PCI_IO_BASE; 215187c26ddSJohn Crispin ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1; 216187c26ddSJohn Crispin 217187c26ddSJohn Crispin rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR); 218c861519fSRalf Baechle udelay(1); 219187c26ddSJohn Crispin 220187c26ddSJohn Crispin rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL); 221187c26ddSJohn Crispin rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR); 222187c26ddSJohn Crispin rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE); 223187c26ddSJohn Crispin rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE); 224187c26ddSJohn Crispin rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR); 225187c26ddSJohn Crispin rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID); 226187c26ddSJohn Crispin rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS); 227187c26ddSJohn Crispin rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID); 228187c26ddSJohn Crispin rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR); 229187c26ddSJohn Crispin 230187c26ddSJohn Crispin rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000); 231187c26ddSJohn Crispin (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0); 232187c26ddSJohn Crispin 2330eb1cfffSTobias Wolf rt2880_pci_controller.of_node = pdev->dev.of_node; 2340eb1cfffSTobias Wolf 235187c26ddSJohn Crispin register_pci_controller(&rt2880_pci_controller); 236187c26ddSJohn Crispin return 0; 237187c26ddSJohn Crispin } 238187c26ddSJohn Crispin 239187c26ddSJohn Crispin int pcibios_plat_dev_init(struct pci_dev *dev) 240187c26ddSJohn Crispin { 241*8e98b697SIlya Lipnitskiy static bool slot0_init; 242*8e98b697SIlya Lipnitskiy 243*8e98b697SIlya Lipnitskiy /* 244*8e98b697SIlya Lipnitskiy * Nobody seems to initialize slot 0, but this platform requires it, so 245*8e98b697SIlya Lipnitskiy * do it once when some other slot is being enabled. The PCI subsystem 246*8e98b697SIlya Lipnitskiy * should configure other slots properly, so no need to do anything 247*8e98b697SIlya Lipnitskiy * special for those. 248*8e98b697SIlya Lipnitskiy */ 249*8e98b697SIlya Lipnitskiy if (!slot0_init && dev->bus->number == 0) { 250*8e98b697SIlya Lipnitskiy u16 cmd; 251*8e98b697SIlya Lipnitskiy u32 bar0; 252*8e98b697SIlya Lipnitskiy 253*8e98b697SIlya Lipnitskiy slot0_init = true; 254*8e98b697SIlya Lipnitskiy 255*8e98b697SIlya Lipnitskiy pci_bus_write_config_dword(dev->bus, 0, PCI_BASE_ADDRESS_0, 256*8e98b697SIlya Lipnitskiy 0x08000000); 257*8e98b697SIlya Lipnitskiy pci_bus_read_config_dword(dev->bus, 0, PCI_BASE_ADDRESS_0, 258*8e98b697SIlya Lipnitskiy &bar0); 259*8e98b697SIlya Lipnitskiy 260*8e98b697SIlya Lipnitskiy pci_bus_read_config_word(dev->bus, 0, PCI_COMMAND, &cmd); 261*8e98b697SIlya Lipnitskiy cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY; 262*8e98b697SIlya Lipnitskiy pci_bus_write_config_word(dev->bus, 0, PCI_COMMAND, cmd); 263*8e98b697SIlya Lipnitskiy } 264*8e98b697SIlya Lipnitskiy 265187c26ddSJohn Crispin return 0; 266187c26ddSJohn Crispin } 267187c26ddSJohn Crispin 268187c26ddSJohn Crispin static const struct of_device_id rt288x_pci_match[] = { 269187c26ddSJohn Crispin { .compatible = "ralink,rt288x-pci" }, 270187c26ddSJohn Crispin {}, 271187c26ddSJohn Crispin }; 272187c26ddSJohn Crispin 273187c26ddSJohn Crispin static struct platform_driver rt288x_pci_driver = { 274187c26ddSJohn Crispin .probe = rt288x_pci_probe, 275187c26ddSJohn Crispin .driver = { 276187c26ddSJohn Crispin .name = "rt288x-pci", 277187c26ddSJohn Crispin .of_match_table = rt288x_pci_match, 278187c26ddSJohn Crispin }, 279187c26ddSJohn Crispin }; 280187c26ddSJohn Crispin 281187c26ddSJohn Crispin int __init pcibios_init(void) 282187c26ddSJohn Crispin { 283187c26ddSJohn Crispin int ret = platform_driver_register(&rt288x_pci_driver); 284187c26ddSJohn Crispin 285187c26ddSJohn Crispin if (ret) 286187c26ddSJohn Crispin pr_info("rt288x-pci: Error registering platform driver!"); 287187c26ddSJohn Crispin 288187c26ddSJohn Crispin return ret; 289187c26ddSJohn Crispin } 290187c26ddSJohn Crispin 291187c26ddSJohn Crispin arch_initcall(pcibios_init); 292