18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Support for Faraday Technology FTPC100 PCI Controller 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on the out-of-tree OpenWRT patch for Cortina Gemini: 88c2ecf20Sopenharmony_ci * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com> 98c2ecf20Sopenharmony_ci * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt> 108c2ecf20Sopenharmony_ci * Based on SL2312 PCI controller code 118c2ecf20Sopenharmony_ci * Storlink (C) 2003 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/init.h> 158c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 168c2ecf20Sopenharmony_ci#include <linux/io.h> 178c2ecf20Sopenharmony_ci#include <linux/kernel.h> 188c2ecf20Sopenharmony_ci#include <linux/of_address.h> 198c2ecf20Sopenharmony_ci#include <linux/of_device.h> 208c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 218c2ecf20Sopenharmony_ci#include <linux/of_pci.h> 228c2ecf20Sopenharmony_ci#include <linux/pci.h> 238c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/irqdomain.h> 268c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h> 278c2ecf20Sopenharmony_ci#include <linux/bitops.h> 288c2ecf20Sopenharmony_ci#include <linux/irq.h> 298c2ecf20Sopenharmony_ci#include <linux/clk.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include "../pci.h" 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* 348c2ecf20Sopenharmony_ci * Special configuration registers directly in the first few words 358c2ecf20Sopenharmony_ci * in I/O space. 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ci#define FTPCI_IOSIZE 0x00 388c2ecf20Sopenharmony_ci#define FTPCI_PROT 0x04 /* AHB protection */ 398c2ecf20Sopenharmony_ci#define FTPCI_CTRL 0x08 /* PCI control signal */ 408c2ecf20Sopenharmony_ci#define FTPCI_SOFTRST 0x10 /* Soft reset counter and response error enable */ 418c2ecf20Sopenharmony_ci#define FTPCI_CONFIG 0x28 /* PCI configuration command register */ 428c2ecf20Sopenharmony_ci#define FTPCI_DATA 0x2C 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#define FARADAY_PCI_STATUS_CMD 0x04 /* Status and command */ 458c2ecf20Sopenharmony_ci#define FARADAY_PCI_PMC 0x40 /* Power management control */ 468c2ecf20Sopenharmony_ci#define FARADAY_PCI_PMCSR 0x44 /* Power management status */ 478c2ecf20Sopenharmony_ci#define FARADAY_PCI_CTRL1 0x48 /* Control register 1 */ 488c2ecf20Sopenharmony_ci#define FARADAY_PCI_CTRL2 0x4C /* Control register 2 */ 498c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEM1_BASE_SIZE 0x50 /* Memory base and size #1 */ 508c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEM2_BASE_SIZE 0x54 /* Memory base and size #2 */ 518c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEM3_BASE_SIZE 0x58 /* Memory base and size #3 */ 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define PCI_STATUS_66MHZ_CAPABLE BIT(21) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* Bits 31..28 gives INTD..INTA status */ 568c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTSTS_SHIFT 28 578c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_CMDERR BIT(27) 588c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_PARERR BIT(26) 598c2ecf20Sopenharmony_ci/* Bits 25..22 masks INTD..INTA */ 608c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_SHIFT 22 618c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_MABRT_RX BIT(21) 628c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_TABRT_RX BIT(20) 638c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_TABRT_TX BIT(19) 648c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_RETRY4 BIT(18) 658c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_SERR_RX BIT(17) 668c2ecf20Sopenharmony_ci#define PCI_CTRL2_INTMASK_PERR_RX BIT(16) 678c2ecf20Sopenharmony_ci/* Bit 15 reserved */ 688c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ6 BIT(14) 698c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ5 BIT(13) 708c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ4 BIT(12) 718c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ3 BIT(11) 728c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ2 BIT(10) 738c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ1 BIT(9) 748c2ecf20Sopenharmony_ci#define PCI_CTRL2_MSTPRI_REQ0 BIT(8) 758c2ecf20Sopenharmony_ci/* Bits 7..4 reserved */ 768c2ecf20Sopenharmony_ci/* Bits 3..0 TRDYW */ 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* 798c2ecf20Sopenharmony_ci * Memory configs: 808c2ecf20Sopenharmony_ci * Bit 31..20 defines the PCI side memory base 818c2ecf20Sopenharmony_ci * Bit 19..16 (4 bits) defines the size per below 828c2ecf20Sopenharmony_ci */ 838c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMBASE_MASK 0xfff00000 848c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_1MB 0x0 858c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_2MB 0x1 868c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_4MB 0x2 878c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_8MB 0x3 888c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_16MB 0x4 898c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_32MB 0x5 908c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_64MB 0x6 918c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_128MB 0x7 928c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_256MB 0x8 938c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_512MB 0x9 948c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_1GB 0xa 958c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_2GB 0xb 968c2ecf20Sopenharmony_ci#define FARADAY_PCI_MEMSIZE_SHIFT 16 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci/* 998c2ecf20Sopenharmony_ci * The DMA base is set to 0x0 for all memory segments, it reflects the 1008c2ecf20Sopenharmony_ci * fact that the memory of the host system starts at 0x0. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci#define FARADAY_PCI_DMA_MEM1_BASE 0x00000000 1038c2ecf20Sopenharmony_ci#define FARADAY_PCI_DMA_MEM2_BASE 0x00000000 1048c2ecf20Sopenharmony_ci#define FARADAY_PCI_DMA_MEM3_BASE 0x00000000 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/* Defines for PCI configuration command register */ 1078c2ecf20Sopenharmony_ci#define PCI_CONF_ENABLE BIT(31) 1088c2ecf20Sopenharmony_ci#define PCI_CONF_WHERE(r) ((r) & 0xFC) 1098c2ecf20Sopenharmony_ci#define PCI_CONF_BUS(b) (((b) & 0xFF) << 16) 1108c2ecf20Sopenharmony_ci#define PCI_CONF_DEVICE(d) (((d) & 0x1F) << 11) 1118c2ecf20Sopenharmony_ci#define PCI_CONF_FUNCTION(f) (((f) & 0x07) << 8) 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/** 1148c2ecf20Sopenharmony_ci * struct faraday_pci_variant - encodes IP block differences 1158c2ecf20Sopenharmony_ci * @cascaded_irq: this host has cascaded IRQs from an interrupt controller 1168c2ecf20Sopenharmony_ci * embedded in the host bridge. 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_cistruct faraday_pci_variant { 1198c2ecf20Sopenharmony_ci bool cascaded_irq; 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistruct faraday_pci { 1238c2ecf20Sopenharmony_ci struct device *dev; 1248c2ecf20Sopenharmony_ci void __iomem *base; 1258c2ecf20Sopenharmony_ci struct irq_domain *irqdomain; 1268c2ecf20Sopenharmony_ci struct pci_bus *bus; 1278c2ecf20Sopenharmony_ci struct clk *bus_clk; 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int faraday_res_to_memcfg(resource_size_t mem_base, 1318c2ecf20Sopenharmony_ci resource_size_t mem_size, u32 *val) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci u32 outval; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci switch (mem_size) { 1368c2ecf20Sopenharmony_ci case SZ_1M: 1378c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_1MB; 1388c2ecf20Sopenharmony_ci break; 1398c2ecf20Sopenharmony_ci case SZ_2M: 1408c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_2MB; 1418c2ecf20Sopenharmony_ci break; 1428c2ecf20Sopenharmony_ci case SZ_4M: 1438c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_4MB; 1448c2ecf20Sopenharmony_ci break; 1458c2ecf20Sopenharmony_ci case SZ_8M: 1468c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_8MB; 1478c2ecf20Sopenharmony_ci break; 1488c2ecf20Sopenharmony_ci case SZ_16M: 1498c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_16MB; 1508c2ecf20Sopenharmony_ci break; 1518c2ecf20Sopenharmony_ci case SZ_32M: 1528c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_32MB; 1538c2ecf20Sopenharmony_ci break; 1548c2ecf20Sopenharmony_ci case SZ_64M: 1558c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_64MB; 1568c2ecf20Sopenharmony_ci break; 1578c2ecf20Sopenharmony_ci case SZ_128M: 1588c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_128MB; 1598c2ecf20Sopenharmony_ci break; 1608c2ecf20Sopenharmony_ci case SZ_256M: 1618c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_256MB; 1628c2ecf20Sopenharmony_ci break; 1638c2ecf20Sopenharmony_ci case SZ_512M: 1648c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_512MB; 1658c2ecf20Sopenharmony_ci break; 1668c2ecf20Sopenharmony_ci case SZ_1G: 1678c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_1GB; 1688c2ecf20Sopenharmony_ci break; 1698c2ecf20Sopenharmony_ci case SZ_2G: 1708c2ecf20Sopenharmony_ci outval = FARADAY_PCI_MEMSIZE_2GB; 1718c2ecf20Sopenharmony_ci break; 1728c2ecf20Sopenharmony_ci default: 1738c2ecf20Sopenharmony_ci return -EINVAL; 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci outval <<= FARADAY_PCI_MEMSIZE_SHIFT; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci /* This is probably not good */ 1788c2ecf20Sopenharmony_ci if (mem_base & ~(FARADAY_PCI_MEMBASE_MASK)) 1798c2ecf20Sopenharmony_ci pr_warn("truncated PCI memory base\n"); 1808c2ecf20Sopenharmony_ci /* Translate to bridge side address space */ 1818c2ecf20Sopenharmony_ci outval |= (mem_base & FARADAY_PCI_MEMBASE_MASK); 1828c2ecf20Sopenharmony_ci pr_debug("Translated pci base @%pap, size %pap to config %08x\n", 1838c2ecf20Sopenharmony_ci &mem_base, &mem_size, outval); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci *val = outval; 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic int faraday_raw_pci_read_config(struct faraday_pci *p, int bus_number, 1908c2ecf20Sopenharmony_ci unsigned int fn, int config, int size, 1918c2ecf20Sopenharmony_ci u32 *value) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci writel(PCI_CONF_BUS(bus_number) | 1948c2ecf20Sopenharmony_ci PCI_CONF_DEVICE(PCI_SLOT(fn)) | 1958c2ecf20Sopenharmony_ci PCI_CONF_FUNCTION(PCI_FUNC(fn)) | 1968c2ecf20Sopenharmony_ci PCI_CONF_WHERE(config) | 1978c2ecf20Sopenharmony_ci PCI_CONF_ENABLE, 1988c2ecf20Sopenharmony_ci p->base + FTPCI_CONFIG); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci *value = readl(p->base + FTPCI_DATA); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci if (size == 1) 2038c2ecf20Sopenharmony_ci *value = (*value >> (8 * (config & 3))) & 0xFF; 2048c2ecf20Sopenharmony_ci else if (size == 2) 2058c2ecf20Sopenharmony_ci *value = (*value >> (8 * (config & 3))) & 0xFFFF; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic int faraday_pci_read_config(struct pci_bus *bus, unsigned int fn, 2118c2ecf20Sopenharmony_ci int config, int size, u32 *value) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci struct faraday_pci *p = bus->sysdata; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci dev_dbg(&bus->dev, 2168c2ecf20Sopenharmony_ci "[read] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n", 2178c2ecf20Sopenharmony_ci PCI_SLOT(fn), PCI_FUNC(fn), config, size, *value); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci return faraday_raw_pci_read_config(p, bus->number, fn, config, size, value); 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic int faraday_raw_pci_write_config(struct faraday_pci *p, int bus_number, 2238c2ecf20Sopenharmony_ci unsigned int fn, int config, int size, 2248c2ecf20Sopenharmony_ci u32 value) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci int ret = PCIBIOS_SUCCESSFUL; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci writel(PCI_CONF_BUS(bus_number) | 2298c2ecf20Sopenharmony_ci PCI_CONF_DEVICE(PCI_SLOT(fn)) | 2308c2ecf20Sopenharmony_ci PCI_CONF_FUNCTION(PCI_FUNC(fn)) | 2318c2ecf20Sopenharmony_ci PCI_CONF_WHERE(config) | 2328c2ecf20Sopenharmony_ci PCI_CONF_ENABLE, 2338c2ecf20Sopenharmony_ci p->base + FTPCI_CONFIG); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci switch (size) { 2368c2ecf20Sopenharmony_ci case 4: 2378c2ecf20Sopenharmony_ci writel(value, p->base + FTPCI_DATA); 2388c2ecf20Sopenharmony_ci break; 2398c2ecf20Sopenharmony_ci case 2: 2408c2ecf20Sopenharmony_ci writew(value, p->base + FTPCI_DATA + (config & 3)); 2418c2ecf20Sopenharmony_ci break; 2428c2ecf20Sopenharmony_ci case 1: 2438c2ecf20Sopenharmony_ci writeb(value, p->base + FTPCI_DATA + (config & 3)); 2448c2ecf20Sopenharmony_ci break; 2458c2ecf20Sopenharmony_ci default: 2468c2ecf20Sopenharmony_ci ret = PCIBIOS_BAD_REGISTER_NUMBER; 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci return ret; 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic int faraday_pci_write_config(struct pci_bus *bus, unsigned int fn, 2538c2ecf20Sopenharmony_ci int config, int size, u32 value) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci struct faraday_pci *p = bus->sysdata; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci dev_dbg(&bus->dev, 2588c2ecf20Sopenharmony_ci "[write] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n", 2598c2ecf20Sopenharmony_ci PCI_SLOT(fn), PCI_FUNC(fn), config, size, value); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci return faraday_raw_pci_write_config(p, bus->number, fn, config, size, 2628c2ecf20Sopenharmony_ci value); 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_cistatic struct pci_ops faraday_pci_ops = { 2668c2ecf20Sopenharmony_ci .read = faraday_pci_read_config, 2678c2ecf20Sopenharmony_ci .write = faraday_pci_write_config, 2688c2ecf20Sopenharmony_ci}; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cistatic void faraday_pci_ack_irq(struct irq_data *d) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct faraday_pci *p = irq_data_get_irq_chip_data(d); 2738c2ecf20Sopenharmony_ci unsigned int reg; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, ®); 2768c2ecf20Sopenharmony_ci reg &= ~(0xF << PCI_CTRL2_INTSTS_SHIFT); 2778c2ecf20Sopenharmony_ci reg |= BIT(irqd_to_hwirq(d) + PCI_CTRL2_INTSTS_SHIFT); 2788c2ecf20Sopenharmony_ci faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, reg); 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_cistatic void faraday_pci_mask_irq(struct irq_data *d) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci struct faraday_pci *p = irq_data_get_irq_chip_data(d); 2848c2ecf20Sopenharmony_ci unsigned int reg; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, ®); 2878c2ecf20Sopenharmony_ci reg &= ~((0xF << PCI_CTRL2_INTSTS_SHIFT) 2888c2ecf20Sopenharmony_ci | BIT(irqd_to_hwirq(d) + PCI_CTRL2_INTMASK_SHIFT)); 2898c2ecf20Sopenharmony_ci faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, reg); 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic void faraday_pci_unmask_irq(struct irq_data *d) 2938c2ecf20Sopenharmony_ci{ 2948c2ecf20Sopenharmony_ci struct faraday_pci *p = irq_data_get_irq_chip_data(d); 2958c2ecf20Sopenharmony_ci unsigned int reg; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, ®); 2988c2ecf20Sopenharmony_ci reg &= ~(0xF << PCI_CTRL2_INTSTS_SHIFT); 2998c2ecf20Sopenharmony_ci reg |= BIT(irqd_to_hwirq(d) + PCI_CTRL2_INTMASK_SHIFT); 3008c2ecf20Sopenharmony_ci faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, reg); 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_cistatic void faraday_pci_irq_handler(struct irq_desc *desc) 3048c2ecf20Sopenharmony_ci{ 3058c2ecf20Sopenharmony_ci struct faraday_pci *p = irq_desc_get_handler_data(desc); 3068c2ecf20Sopenharmony_ci struct irq_chip *irqchip = irq_desc_get_chip(desc); 3078c2ecf20Sopenharmony_ci unsigned int irq_stat, reg, i; 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, ®); 3108c2ecf20Sopenharmony_ci irq_stat = reg >> PCI_CTRL2_INTSTS_SHIFT; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci chained_irq_enter(irqchip, desc); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) { 3158c2ecf20Sopenharmony_ci if ((irq_stat & BIT(i)) == 0) 3168c2ecf20Sopenharmony_ci continue; 3178c2ecf20Sopenharmony_ci generic_handle_irq(irq_find_mapping(p->irqdomain, i)); 3188c2ecf20Sopenharmony_ci } 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci chained_irq_exit(irqchip, desc); 3218c2ecf20Sopenharmony_ci} 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_cistatic struct irq_chip faraday_pci_irq_chip = { 3248c2ecf20Sopenharmony_ci .name = "PCI", 3258c2ecf20Sopenharmony_ci .irq_ack = faraday_pci_ack_irq, 3268c2ecf20Sopenharmony_ci .irq_mask = faraday_pci_mask_irq, 3278c2ecf20Sopenharmony_ci .irq_unmask = faraday_pci_unmask_irq, 3288c2ecf20Sopenharmony_ci}; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cistatic int faraday_pci_irq_map(struct irq_domain *domain, unsigned int irq, 3318c2ecf20Sopenharmony_ci irq_hw_number_t hwirq) 3328c2ecf20Sopenharmony_ci{ 3338c2ecf20Sopenharmony_ci irq_set_chip_and_handler(irq, &faraday_pci_irq_chip, handle_level_irq); 3348c2ecf20Sopenharmony_ci irq_set_chip_data(irq, domain->host_data); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci return 0; 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cistatic const struct irq_domain_ops faraday_pci_irqdomain_ops = { 3408c2ecf20Sopenharmony_ci .map = faraday_pci_irq_map, 3418c2ecf20Sopenharmony_ci}; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_cistatic int faraday_pci_setup_cascaded_irq(struct faraday_pci *p) 3448c2ecf20Sopenharmony_ci{ 3458c2ecf20Sopenharmony_ci struct device_node *intc = of_get_next_child(p->dev->of_node, NULL); 3468c2ecf20Sopenharmony_ci int irq; 3478c2ecf20Sopenharmony_ci int i; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci if (!intc) { 3508c2ecf20Sopenharmony_ci dev_err(p->dev, "missing child interrupt-controller node\n"); 3518c2ecf20Sopenharmony_ci return -EINVAL; 3528c2ecf20Sopenharmony_ci } 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci /* All PCI IRQs cascade off this one */ 3558c2ecf20Sopenharmony_ci irq = of_irq_get(intc, 0); 3568c2ecf20Sopenharmony_ci if (irq <= 0) { 3578c2ecf20Sopenharmony_ci dev_err(p->dev, "failed to get parent IRQ\n"); 3588c2ecf20Sopenharmony_ci of_node_put(intc); 3598c2ecf20Sopenharmony_ci return irq ?: -EINVAL; 3608c2ecf20Sopenharmony_ci } 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci p->irqdomain = irq_domain_add_linear(intc, PCI_NUM_INTX, 3638c2ecf20Sopenharmony_ci &faraday_pci_irqdomain_ops, p); 3648c2ecf20Sopenharmony_ci of_node_put(intc); 3658c2ecf20Sopenharmony_ci if (!p->irqdomain) { 3668c2ecf20Sopenharmony_ci dev_err(p->dev, "failed to create Gemini PCI IRQ domain\n"); 3678c2ecf20Sopenharmony_ci return -EINVAL; 3688c2ecf20Sopenharmony_ci } 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci irq_set_chained_handler_and_data(irq, faraday_pci_irq_handler, p); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) 3738c2ecf20Sopenharmony_ci irq_create_mapping(p->irqdomain, i); 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci return 0; 3768c2ecf20Sopenharmony_ci} 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_cistatic int faraday_pci_parse_map_dma_ranges(struct faraday_pci *p) 3798c2ecf20Sopenharmony_ci{ 3808c2ecf20Sopenharmony_ci struct device *dev = p->dev; 3818c2ecf20Sopenharmony_ci struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p); 3828c2ecf20Sopenharmony_ci struct resource_entry *entry; 3838c2ecf20Sopenharmony_ci u32 confreg[3] = { 3848c2ecf20Sopenharmony_ci FARADAY_PCI_MEM1_BASE_SIZE, 3858c2ecf20Sopenharmony_ci FARADAY_PCI_MEM2_BASE_SIZE, 3868c2ecf20Sopenharmony_ci FARADAY_PCI_MEM3_BASE_SIZE, 3878c2ecf20Sopenharmony_ci }; 3888c2ecf20Sopenharmony_ci int i = 0; 3898c2ecf20Sopenharmony_ci u32 val; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci resource_list_for_each_entry(entry, &bridge->dma_ranges) { 3928c2ecf20Sopenharmony_ci u64 pci_addr = entry->res->start - entry->offset; 3938c2ecf20Sopenharmony_ci u64 end = entry->res->end - entry->offset; 3948c2ecf20Sopenharmony_ci int ret; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci ret = faraday_res_to_memcfg(pci_addr, 3978c2ecf20Sopenharmony_ci resource_size(entry->res), &val); 3988c2ecf20Sopenharmony_ci if (ret) { 3998c2ecf20Sopenharmony_ci dev_err(dev, 4008c2ecf20Sopenharmony_ci "DMA range %d: illegal MEM resource size\n", i); 4018c2ecf20Sopenharmony_ci return -EINVAL; 4028c2ecf20Sopenharmony_ci } 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci dev_info(dev, "DMA MEM%d BASE: 0x%016llx -> 0x%016llx config %08x\n", 4058c2ecf20Sopenharmony_ci i + 1, pci_addr, end, val); 4068c2ecf20Sopenharmony_ci if (i <= 2) { 4078c2ecf20Sopenharmony_ci faraday_raw_pci_write_config(p, 0, 0, confreg[i], 4088c2ecf20Sopenharmony_ci 4, val); 4098c2ecf20Sopenharmony_ci } else { 4108c2ecf20Sopenharmony_ci dev_err(dev, "ignore extraneous dma-range %d\n", i); 4118c2ecf20Sopenharmony_ci break; 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci i++; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci return 0; 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_cistatic int faraday_pci_probe(struct platform_device *pdev) 4218c2ecf20Sopenharmony_ci{ 4228c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 4238c2ecf20Sopenharmony_ci const struct faraday_pci_variant *variant = 4248c2ecf20Sopenharmony_ci of_device_get_match_data(dev); 4258c2ecf20Sopenharmony_ci struct resource_entry *win; 4268c2ecf20Sopenharmony_ci struct faraday_pci *p; 4278c2ecf20Sopenharmony_ci struct resource *io; 4288c2ecf20Sopenharmony_ci struct pci_host_bridge *host; 4298c2ecf20Sopenharmony_ci struct clk *clk; 4308c2ecf20Sopenharmony_ci unsigned char max_bus_speed = PCI_SPEED_33MHz; 4318c2ecf20Sopenharmony_ci unsigned char cur_bus_speed = PCI_SPEED_33MHz; 4328c2ecf20Sopenharmony_ci int ret; 4338c2ecf20Sopenharmony_ci u32 val; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci host = devm_pci_alloc_host_bridge(dev, sizeof(*p)); 4368c2ecf20Sopenharmony_ci if (!host) 4378c2ecf20Sopenharmony_ci return -ENOMEM; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci host->ops = &faraday_pci_ops; 4408c2ecf20Sopenharmony_ci p = pci_host_bridge_priv(host); 4418c2ecf20Sopenharmony_ci host->sysdata = p; 4428c2ecf20Sopenharmony_ci p->dev = dev; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci /* Retrieve and enable optional clocks */ 4458c2ecf20Sopenharmony_ci clk = devm_clk_get_enabled(dev, "PCLK"); 4468c2ecf20Sopenharmony_ci if (IS_ERR(clk)) 4478c2ecf20Sopenharmony_ci return PTR_ERR(clk); 4488c2ecf20Sopenharmony_ci p->bus_clk = devm_clk_get_enabled(dev, "PCICLK"); 4498c2ecf20Sopenharmony_ci if (IS_ERR(p->bus_clk)) 4508c2ecf20Sopenharmony_ci return PTR_ERR(p->bus_clk); 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci p->base = devm_platform_ioremap_resource(pdev, 0); 4538c2ecf20Sopenharmony_ci if (IS_ERR(p->base)) 4548c2ecf20Sopenharmony_ci return PTR_ERR(p->base); 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci win = resource_list_first_type(&host->windows, IORESOURCE_IO); 4578c2ecf20Sopenharmony_ci if (win) { 4588c2ecf20Sopenharmony_ci io = win->res; 4598c2ecf20Sopenharmony_ci if (!faraday_res_to_memcfg(io->start - win->offset, 4608c2ecf20Sopenharmony_ci resource_size(io), &val)) { 4618c2ecf20Sopenharmony_ci /* setup I/O space size */ 4628c2ecf20Sopenharmony_ci writel(val, p->base + FTPCI_IOSIZE); 4638c2ecf20Sopenharmony_ci } else { 4648c2ecf20Sopenharmony_ci dev_err(dev, "illegal IO mem size\n"); 4658c2ecf20Sopenharmony_ci return -EINVAL; 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci } 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci /* Setup hostbridge */ 4708c2ecf20Sopenharmony_ci val = readl(p->base + FTPCI_CTRL); 4718c2ecf20Sopenharmony_ci val |= PCI_COMMAND_IO; 4728c2ecf20Sopenharmony_ci val |= PCI_COMMAND_MEMORY; 4738c2ecf20Sopenharmony_ci val |= PCI_COMMAND_MASTER; 4748c2ecf20Sopenharmony_ci writel(val, p->base + FTPCI_CTRL); 4758c2ecf20Sopenharmony_ci /* Mask and clear all interrupts */ 4768c2ecf20Sopenharmony_ci faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2 + 2, 2, 0xF000); 4778c2ecf20Sopenharmony_ci if (variant->cascaded_irq) { 4788c2ecf20Sopenharmony_ci ret = faraday_pci_setup_cascaded_irq(p); 4798c2ecf20Sopenharmony_ci if (ret) { 4808c2ecf20Sopenharmony_ci dev_err(dev, "failed to setup cascaded IRQ\n"); 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci } 4838c2ecf20Sopenharmony_ci } 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci /* Check bus clock if we can gear up to 66 MHz */ 4868c2ecf20Sopenharmony_ci if (!IS_ERR(p->bus_clk)) { 4878c2ecf20Sopenharmony_ci unsigned long rate; 4888c2ecf20Sopenharmony_ci u32 val; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci faraday_raw_pci_read_config(p, 0, 0, 4918c2ecf20Sopenharmony_ci FARADAY_PCI_STATUS_CMD, 4, &val); 4928c2ecf20Sopenharmony_ci rate = clk_get_rate(p->bus_clk); 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci if ((rate == 33000000) && (val & PCI_STATUS_66MHZ_CAPABLE)) { 4958c2ecf20Sopenharmony_ci dev_info(dev, "33MHz bus is 66MHz capable\n"); 4968c2ecf20Sopenharmony_ci max_bus_speed = PCI_SPEED_66MHz; 4978c2ecf20Sopenharmony_ci ret = clk_set_rate(p->bus_clk, 66000000); 4988c2ecf20Sopenharmony_ci if (ret) 4998c2ecf20Sopenharmony_ci dev_err(dev, "failed to set bus clock\n"); 5008c2ecf20Sopenharmony_ci } else { 5018c2ecf20Sopenharmony_ci dev_info(dev, "33MHz only bus\n"); 5028c2ecf20Sopenharmony_ci max_bus_speed = PCI_SPEED_33MHz; 5038c2ecf20Sopenharmony_ci } 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci /* Bumping the clock may fail so read back the rate */ 5068c2ecf20Sopenharmony_ci rate = clk_get_rate(p->bus_clk); 5078c2ecf20Sopenharmony_ci if (rate == 33000000) 5088c2ecf20Sopenharmony_ci cur_bus_speed = PCI_SPEED_33MHz; 5098c2ecf20Sopenharmony_ci if (rate == 66000000) 5108c2ecf20Sopenharmony_ci cur_bus_speed = PCI_SPEED_66MHz; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci ret = faraday_pci_parse_map_dma_ranges(p); 5148c2ecf20Sopenharmony_ci if (ret) 5158c2ecf20Sopenharmony_ci return ret; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci ret = pci_scan_root_bus_bridge(host); 5188c2ecf20Sopenharmony_ci if (ret) { 5198c2ecf20Sopenharmony_ci dev_err(dev, "failed to scan host: %d\n", ret); 5208c2ecf20Sopenharmony_ci return ret; 5218c2ecf20Sopenharmony_ci } 5228c2ecf20Sopenharmony_ci p->bus = host->bus; 5238c2ecf20Sopenharmony_ci p->bus->max_bus_speed = max_bus_speed; 5248c2ecf20Sopenharmony_ci p->bus->cur_bus_speed = cur_bus_speed; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci pci_bus_assign_resources(p->bus); 5278c2ecf20Sopenharmony_ci pci_bus_add_devices(p->bus); 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci return 0; 5308c2ecf20Sopenharmony_ci} 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci/* 5338c2ecf20Sopenharmony_ci * We encode bridge variants here, we have at least two so it doesn't 5348c2ecf20Sopenharmony_ci * hurt to have infrastructure to encompass future variants as well. 5358c2ecf20Sopenharmony_ci */ 5368c2ecf20Sopenharmony_cistatic const struct faraday_pci_variant faraday_regular = { 5378c2ecf20Sopenharmony_ci .cascaded_irq = true, 5388c2ecf20Sopenharmony_ci}; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic const struct faraday_pci_variant faraday_dual = { 5418c2ecf20Sopenharmony_ci .cascaded_irq = false, 5428c2ecf20Sopenharmony_ci}; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_cistatic const struct of_device_id faraday_pci_of_match[] = { 5458c2ecf20Sopenharmony_ci { 5468c2ecf20Sopenharmony_ci .compatible = "faraday,ftpci100", 5478c2ecf20Sopenharmony_ci .data = &faraday_regular, 5488c2ecf20Sopenharmony_ci }, 5498c2ecf20Sopenharmony_ci { 5508c2ecf20Sopenharmony_ci .compatible = "faraday,ftpci100-dual", 5518c2ecf20Sopenharmony_ci .data = &faraday_dual, 5528c2ecf20Sopenharmony_ci }, 5538c2ecf20Sopenharmony_ci {}, 5548c2ecf20Sopenharmony_ci}; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_cistatic struct platform_driver faraday_pci_driver = { 5578c2ecf20Sopenharmony_ci .driver = { 5588c2ecf20Sopenharmony_ci .name = "ftpci100", 5598c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(faraday_pci_of_match), 5608c2ecf20Sopenharmony_ci .suppress_bind_attrs = true, 5618c2ecf20Sopenharmony_ci }, 5628c2ecf20Sopenharmony_ci .probe = faraday_pci_probe, 5638c2ecf20Sopenharmony_ci}; 5648c2ecf20Sopenharmony_cibuiltin_platform_driver(faraday_pci_driver); 565