1*d2912cb1SThomas 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 u16 cmd; 184187c26ddSJohn Crispin int irq = -1; 185187c26ddSJohn Crispin 186187c26ddSJohn Crispin if (dev->bus->number != 0) 187187c26ddSJohn Crispin return irq; 188187c26ddSJohn Crispin 189187c26ddSJohn Crispin switch (PCI_SLOT(dev->devfn)) { 190187c26ddSJohn Crispin case 0x00: 191187c26ddSJohn Crispin rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000); 192187c26ddSJohn Crispin (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0); 193187c26ddSJohn Crispin break; 194187c26ddSJohn Crispin case 0x11: 195187c26ddSJohn Crispin irq = RT288X_CPU_IRQ_PCI; 196187c26ddSJohn Crispin break; 197187c26ddSJohn Crispin default: 198187c26ddSJohn Crispin pr_err("%s:%s[%d] trying to alloc unknown pci irq\n", 199187c26ddSJohn Crispin __FILE__, __func__, __LINE__); 200187c26ddSJohn Crispin BUG(); 201187c26ddSJohn Crispin break; 202187c26ddSJohn Crispin } 203187c26ddSJohn Crispin 204187c26ddSJohn Crispin pci_write_config_byte((struct pci_dev *) dev, 205187c26ddSJohn Crispin PCI_CACHE_LINE_SIZE, 0x14); 206187c26ddSJohn Crispin pci_write_config_byte((struct pci_dev *) dev, PCI_LATENCY_TIMER, 0xFF); 207187c26ddSJohn Crispin pci_read_config_word((struct pci_dev *) dev, PCI_COMMAND, &cmd); 208187c26ddSJohn Crispin cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | 209187c26ddSJohn Crispin PCI_COMMAND_INVALIDATE | PCI_COMMAND_FAST_BACK | 210187c26ddSJohn Crispin PCI_COMMAND_SERR | PCI_COMMAND_WAIT | PCI_COMMAND_PARITY; 211187c26ddSJohn Crispin pci_write_config_word((struct pci_dev *) dev, PCI_COMMAND, cmd); 212187c26ddSJohn Crispin pci_write_config_byte((struct pci_dev *) dev, PCI_INTERRUPT_LINE, 213187c26ddSJohn Crispin dev->irq); 214187c26ddSJohn Crispin return irq; 215187c26ddSJohn Crispin } 216187c26ddSJohn Crispin 217187c26ddSJohn Crispin static int rt288x_pci_probe(struct platform_device *pdev) 218187c26ddSJohn Crispin { 219187c26ddSJohn Crispin void __iomem *io_map_base; 220187c26ddSJohn Crispin 221187c26ddSJohn Crispin rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE); 222187c26ddSJohn Crispin 223187c26ddSJohn Crispin io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE); 224187c26ddSJohn Crispin rt2880_pci_controller.io_map_base = (unsigned long) io_map_base; 225187c26ddSJohn Crispin set_io_port_base((unsigned long) io_map_base); 226187c26ddSJohn Crispin 227187c26ddSJohn Crispin ioport_resource.start = RT2880_PCI_IO_BASE; 228187c26ddSJohn Crispin ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1; 229187c26ddSJohn Crispin 230187c26ddSJohn Crispin rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR); 231c861519fSRalf Baechle udelay(1); 232187c26ddSJohn Crispin 233187c26ddSJohn Crispin rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL); 234187c26ddSJohn Crispin rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR); 235187c26ddSJohn Crispin rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE); 236187c26ddSJohn Crispin rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE); 237187c26ddSJohn Crispin rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR); 238187c26ddSJohn Crispin rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID); 239187c26ddSJohn Crispin rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS); 240187c26ddSJohn Crispin rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID); 241187c26ddSJohn Crispin rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR); 242187c26ddSJohn Crispin 243187c26ddSJohn Crispin rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000); 244187c26ddSJohn Crispin (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0); 245187c26ddSJohn Crispin 2460eb1cfffSTobias Wolf rt2880_pci_controller.of_node = pdev->dev.of_node; 2470eb1cfffSTobias Wolf 248187c26ddSJohn Crispin register_pci_controller(&rt2880_pci_controller); 249187c26ddSJohn Crispin return 0; 250187c26ddSJohn Crispin } 251187c26ddSJohn Crispin 252187c26ddSJohn Crispin int pcibios_plat_dev_init(struct pci_dev *dev) 253187c26ddSJohn Crispin { 254187c26ddSJohn Crispin return 0; 255187c26ddSJohn Crispin } 256187c26ddSJohn Crispin 257187c26ddSJohn Crispin static const struct of_device_id rt288x_pci_match[] = { 258187c26ddSJohn Crispin { .compatible = "ralink,rt288x-pci" }, 259187c26ddSJohn Crispin {}, 260187c26ddSJohn Crispin }; 261187c26ddSJohn Crispin 262187c26ddSJohn Crispin static struct platform_driver rt288x_pci_driver = { 263187c26ddSJohn Crispin .probe = rt288x_pci_probe, 264187c26ddSJohn Crispin .driver = { 265187c26ddSJohn Crispin .name = "rt288x-pci", 266187c26ddSJohn Crispin .of_match_table = rt288x_pci_match, 267187c26ddSJohn Crispin }, 268187c26ddSJohn Crispin }; 269187c26ddSJohn Crispin 270187c26ddSJohn Crispin int __init pcibios_init(void) 271187c26ddSJohn Crispin { 272187c26ddSJohn Crispin int ret = platform_driver_register(&rt288x_pci_driver); 273187c26ddSJohn Crispin 274187c26ddSJohn Crispin if (ret) 275187c26ddSJohn Crispin pr_info("rt288x-pci: Error registering platform driver!"); 276187c26ddSJohn Crispin 277187c26ddSJohn Crispin return ret; 278187c26ddSJohn Crispin } 279187c26ddSJohn Crispin 280187c26ddSJohn Crispin arch_initcall(pcibios_init); 281