Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 2 | /* |
| 3 | * PCIe host controller driver for Texas Instruments Keystone SoCs |
| 4 | * |
| 5 | * Copyright (C) 2013-2014 Texas Instruments., Ltd. |
Alexander A. Klimov | 7ecd4a8 | 2020-06-27 12:30:50 +0200 | [diff] [blame] | 6 | * https://www.ti.com |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 7 | * |
| 8 | * Author: Murali Karicheri <m-karicheri2@ti.com> |
| 9 | * Implementation based on pci-exynos.c and pcie-designware.c |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 12 | #include <linux/clk.h> |
| 13 | #include <linux/delay.h> |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 14 | #include <linux/gpio/consumer.h> |
Paul Gortmaker | 1481bf2 | 2016-07-02 19:13:26 -0400 | [diff] [blame] | 15 | #include <linux/init.h> |
Kishon Vijay Abraham I | c0b8558 | 2018-10-17 13:11:13 +0530 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irqchip/chained_irq.h> |
| 18 | #include <linux/irqdomain.h> |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 19 | #include <linux/mfd/syscon.h> |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 20 | #include <linux/msi.h> |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 21 | #include <linux/of.h> |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 22 | #include <linux/of_device.h> |
Kishon Vijay Abraham I | c0b8558 | 2018-10-17 13:11:13 +0530 | [diff] [blame] | 23 | #include <linux/of_irq.h> |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 24 | #include <linux/of_pci.h> |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 25 | #include <linux/phy/phy.h> |
Kishon Vijay Abraham I | c0b8558 | 2018-10-17 13:11:13 +0530 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 27 | #include <linux/regmap.h> |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 28 | #include <linux/resource.h> |
| 29 | #include <linux/signal.h> |
| 30 | |
Kishon Vijay Abraham I | fbb2de8 | 2019-03-25 15:09:38 +0530 | [diff] [blame] | 31 | #include "../../pci.h" |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 32 | #include "pcie-designware.h" |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 33 | |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 34 | #define PCIE_VENDORID_MASK 0xffff |
| 35 | #define PCIE_DEVICEID_SHIFT 16 |
| 36 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 37 | /* Application registers */ |
| 38 | #define CMD_STATUS 0x004 |
Kishon Vijay Abraham I | 261de72 | 2018-10-17 13:11:14 +0530 | [diff] [blame] | 39 | #define LTSSM_EN_VAL BIT(0) |
| 40 | #define OB_XLAT_EN_VAL BIT(1) |
| 41 | #define DBI_CS2 BIT(5) |
Kishon Vijay Abraham I | 44c747a | 2018-10-17 13:11:06 +0530 | [diff] [blame] | 42 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 43 | #define CFG_SETUP 0x008 |
Kishon Vijay Abraham I | 44c747a | 2018-10-17 13:11:06 +0530 | [diff] [blame] | 44 | #define CFG_BUS(x) (((x) & 0xff) << 16) |
| 45 | #define CFG_DEVICE(x) (((x) & 0x1f) << 8) |
| 46 | #define CFG_FUNC(x) ((x) & 0x7) |
| 47 | #define CFG_TYPE1 BIT(24) |
| 48 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 49 | #define OB_SIZE 0x030 |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 50 | #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n))) |
| 51 | #define OB_OFFSET_HI(n) (0x204 + (8 * (n))) |
Kishon Vijay Abraham I | e75043a | 2018-10-17 13:11:08 +0530 | [diff] [blame] | 52 | #define OB_ENABLEN BIT(0) |
| 53 | #define OB_WIN_SIZE 8 /* 8MB */ |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 54 | |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 55 | #define PCIE_LEGACY_IRQ_ENABLE_SET(n) (0x188 + (0x10 * ((n) - 1))) |
| 56 | #define PCIE_LEGACY_IRQ_ENABLE_CLR(n) (0x18c + (0x10 * ((n) - 1))) |
| 57 | #define PCIE_EP_IRQ_SET 0x64 |
| 58 | #define PCIE_EP_IRQ_CLR 0x68 |
| 59 | #define INT_ENABLE BIT(0) |
| 60 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 61 | /* IRQ register defines */ |
| 62 | #define IRQ_EOI 0x050 |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 63 | |
| 64 | #define MSI_IRQ 0x054 |
Kishon Vijay Abraham I | 1beb551 | 2019-03-21 15:29:20 +0530 | [diff] [blame] | 65 | #define MSI_IRQ_STATUS(n) (0x104 + ((n) << 4)) |
| 66 | #define MSI_IRQ_ENABLE_SET(n) (0x108 + ((n) << 4)) |
| 67 | #define MSI_IRQ_ENABLE_CLR(n) (0x10c + ((n) << 4)) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 68 | #define MSI_IRQ_OFFSET 4 |
| 69 | |
Kishon Vijay Abraham I | 1beb551 | 2019-03-21 15:29:20 +0530 | [diff] [blame] | 70 | #define IRQ_STATUS(n) (0x184 + ((n) << 4)) |
| 71 | #define IRQ_ENABLE_SET(n) (0x188 + ((n) << 4)) |
| 72 | #define INTx_EN BIT(0) |
| 73 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 74 | #define ERR_IRQ_STATUS 0x1c4 |
| 75 | #define ERR_IRQ_ENABLE_SET 0x1c8 |
Kishon Vijay Abraham I | 261de72 | 2018-10-17 13:11:14 +0530 | [diff] [blame] | 76 | #define ERR_AER BIT(5) /* ECRC error */ |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 77 | #define AM6_ERR_AER BIT(4) /* AM6 ECRC error */ |
Kishon Vijay Abraham I | 261de72 | 2018-10-17 13:11:14 +0530 | [diff] [blame] | 78 | #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */ |
| 79 | #define ERR_CORR BIT(3) /* Correctable error */ |
| 80 | #define ERR_NONFATAL BIT(2) /* Non-fatal error */ |
| 81 | #define ERR_FATAL BIT(1) /* Fatal error */ |
| 82 | #define ERR_SYS BIT(0) /* System error */ |
| 83 | #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \ |
| 84 | ERR_NONFATAL | ERR_FATAL | ERR_SYS) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 85 | |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 86 | /* PCIE controller device IDs */ |
Kishon Vijay Abraham I | 261de72 | 2018-10-17 13:11:14 +0530 | [diff] [blame] | 87 | #define PCIE_RC_K2HK 0xb008 |
| 88 | #define PCIE_RC_K2E 0xb009 |
| 89 | #define PCIE_RC_K2L 0xb00a |
| 90 | #define PCIE_RC_K2G 0xb00b |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 91 | |
Kishon Vijay Abraham I | 156c6fe | 2019-03-25 15:09:29 +0530 | [diff] [blame] | 92 | #define KS_PCIE_DEV_TYPE_MASK (0x3 << 1) |
| 93 | #define KS_PCIE_DEV_TYPE(mode) ((mode) << 1) |
| 94 | |
| 95 | #define EP 0x0 |
| 96 | #define LEG_EP 0x1 |
| 97 | #define RC 0x2 |
| 98 | |
| 99 | #define KS_PCIE_SYSCLOCKOUTEN BIT(0) |
| 100 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 101 | #define AM654_PCIE_DEV_TYPE_MASK 0x3 |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 102 | #define AM654_WIN_SIZE SZ_64K |
| 103 | |
| 104 | #define APP_ADDR_SPACE_0 (16 * SZ_1K) |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 105 | |
Kishon Vijay Abraham I | 261de72 | 2018-10-17 13:11:14 +0530 | [diff] [blame] | 106 | #define to_keystone_pcie(x) dev_get_drvdata((x)->dev) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 107 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 108 | struct ks_pcie_of_data { |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 109 | enum dw_pcie_device_mode mode; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 110 | const struct dw_pcie_host_ops *host_ops; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 111 | const struct dw_pcie_ep_ops *ep_ops; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 112 | unsigned int version; |
| 113 | }; |
| 114 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 115 | struct keystone_pcie { |
| 116 | struct dw_pcie *pci; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 117 | /* PCI Device ID */ |
| 118 | u32 device_id; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 119 | int legacy_host_irqs[PCI_NUM_INTX]; |
| 120 | struct device_node *legacy_intc_np; |
| 121 | |
Kishon Vijay Abraham I | f6f2900 | 2019-03-21 15:29:22 +0530 | [diff] [blame] | 122 | int msi_host_irq; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 123 | int num_lanes; |
Rob Herring | fcde397 | 2020-11-05 15:11:57 -0600 | [diff] [blame] | 124 | u32 num_viewport; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 125 | struct phy **phy; |
| 126 | struct device_link **link; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 127 | struct device_node *msi_intc_np; |
| 128 | struct irq_domain *legacy_irq_domain; |
| 129 | struct device_node *np; |
| 130 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 131 | /* Application register space */ |
| 132 | void __iomem *va_app_base; /* DT 1st resource */ |
| 133 | struct resource app; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 134 | bool is_am6; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 135 | }; |
| 136 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 137 | static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 138 | { |
| 139 | return readl(ks_pcie->va_app_base + offset); |
| 140 | } |
| 141 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 142 | static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset, |
| 143 | u32 val) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 144 | { |
| 145 | writel(val, ks_pcie->va_app_base + offset); |
| 146 | } |
| 147 | |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 148 | static void ks_pcie_msi_irq_ack(struct irq_data *data) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 149 | { |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 150 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 151 | struct keystone_pcie *ks_pcie; |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 152 | u32 irq = data->hwirq; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 153 | struct dw_pcie *pci; |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 154 | u32 reg_offset; |
| 155 | u32 bit_pos; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 156 | |
| 157 | pci = to_dw_pcie_from_pp(pp); |
| 158 | ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | 66c10ec | 2019-03-21 15:29:23 +0530 | [diff] [blame] | 159 | |
| 160 | reg_offset = irq % 8; |
| 161 | bit_pos = irq >> 3; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 162 | |
Kishon Vijay Abraham I | 1beb551 | 2019-03-21 15:29:20 +0530 | [diff] [blame] | 163 | ks_pcie_app_writel(ks_pcie, MSI_IRQ_STATUS(reg_offset), |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 164 | BIT(bit_pos)); |
| 165 | ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 166 | } |
| 167 | |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 168 | static void ks_pcie_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 169 | { |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 170 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 171 | struct keystone_pcie *ks_pcie; |
| 172 | struct dw_pcie *pci; |
| 173 | u64 msi_target; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 174 | |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 175 | pci = to_dw_pcie_from_pp(pp); |
| 176 | ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | 66c10ec | 2019-03-21 15:29:23 +0530 | [diff] [blame] | 177 | |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 178 | msi_target = ks_pcie->app.start + MSI_IRQ; |
| 179 | msg->address_lo = lower_32_bits(msi_target); |
| 180 | msg->address_hi = upper_32_bits(msi_target); |
| 181 | msg->data = data->hwirq; |
| 182 | |
| 183 | dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n", |
| 184 | (int)data->hwirq, msg->address_hi, msg->address_lo); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 185 | } |
| 186 | |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 187 | static int ks_pcie_msi_set_affinity(struct irq_data *irq_data, |
| 188 | const struct cpumask *mask, bool force) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 189 | { |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | static void ks_pcie_msi_mask(struct irq_data *data) |
| 194 | { |
| 195 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 196 | struct keystone_pcie *ks_pcie; |
| 197 | u32 irq = data->hwirq; |
| 198 | struct dw_pcie *pci; |
| 199 | unsigned long flags; |
| 200 | u32 reg_offset; |
| 201 | u32 bit_pos; |
| 202 | |
| 203 | raw_spin_lock_irqsave(&pp->lock, flags); |
| 204 | |
| 205 | pci = to_dw_pcie_from_pp(pp); |
| 206 | ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 207 | |
Kishon Vijay Abraham I | 66c10ec | 2019-03-21 15:29:23 +0530 | [diff] [blame] | 208 | reg_offset = irq % 8; |
| 209 | bit_pos = irq >> 3; |
| 210 | |
Kishon Vijay Abraham I | 1beb551 | 2019-03-21 15:29:20 +0530 | [diff] [blame] | 211 | ks_pcie_app_writel(ks_pcie, MSI_IRQ_ENABLE_CLR(reg_offset), |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 212 | BIT(bit_pos)); |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 213 | |
| 214 | raw_spin_unlock_irqrestore(&pp->lock, flags); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 215 | } |
| 216 | |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 217 | static void ks_pcie_msi_unmask(struct irq_data *data) |
| 218 | { |
| 219 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 220 | struct keystone_pcie *ks_pcie; |
| 221 | u32 irq = data->hwirq; |
| 222 | struct dw_pcie *pci; |
| 223 | unsigned long flags; |
| 224 | u32 reg_offset; |
| 225 | u32 bit_pos; |
| 226 | |
| 227 | raw_spin_lock_irqsave(&pp->lock, flags); |
| 228 | |
| 229 | pci = to_dw_pcie_from_pp(pp); |
| 230 | ks_pcie = to_keystone_pcie(pci); |
| 231 | |
| 232 | reg_offset = irq % 8; |
| 233 | bit_pos = irq >> 3; |
| 234 | |
| 235 | ks_pcie_app_writel(ks_pcie, MSI_IRQ_ENABLE_SET(reg_offset), |
| 236 | BIT(bit_pos)); |
| 237 | |
| 238 | raw_spin_unlock_irqrestore(&pp->lock, flags); |
| 239 | } |
| 240 | |
| 241 | static struct irq_chip ks_pcie_msi_irq_chip = { |
| 242 | .name = "KEYSTONE-PCI-MSI", |
| 243 | .irq_ack = ks_pcie_msi_irq_ack, |
| 244 | .irq_compose_msi_msg = ks_pcie_compose_msi_msg, |
| 245 | .irq_set_affinity = ks_pcie_msi_set_affinity, |
| 246 | .irq_mask = ks_pcie_msi_mask, |
| 247 | .irq_unmask = ks_pcie_msi_unmask, |
| 248 | }; |
| 249 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 250 | static int ks_pcie_msi_host_init(struct pcie_port *pp) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 251 | { |
Kishon Vijay Abraham I | 117c3b6 | 2019-03-21 15:29:25 +0530 | [diff] [blame] | 252 | pp->msi_irq_chip = &ks_pcie_msi_irq_chip; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 253 | return dw_pcie_allocate_domains(pp); |
| 254 | } |
| 255 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 256 | static void ks_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie, |
| 257 | int offset) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 258 | { |
| 259 | struct dw_pcie *pci = ks_pcie->pci; |
| 260 | struct device *dev = pci->dev; |
| 261 | u32 pending; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 262 | |
Kishon Vijay Abraham I | 1beb551 | 2019-03-21 15:29:20 +0530 | [diff] [blame] | 263 | pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS(offset)); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 264 | |
| 265 | if (BIT(0) & pending) { |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 266 | dev_dbg(dev, ": irq: irq_offset %d", offset); |
| 267 | generic_handle_domain_irq(ks_pcie->legacy_irq_domain, offset); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* EOI the INTx interrupt */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 271 | ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 272 | } |
| 273 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 274 | static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 275 | { |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 276 | ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 277 | } |
| 278 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 279 | static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 280 | { |
Kishon Vijay Abraham I | daaaa66 | 2018-10-17 13:11:12 +0530 | [diff] [blame] | 281 | u32 reg; |
| 282 | struct device *dev = ks_pcie->pci->dev; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 283 | |
Kishon Vijay Abraham I | daaaa66 | 2018-10-17 13:11:12 +0530 | [diff] [blame] | 284 | reg = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS); |
| 285 | if (!reg) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 286 | return IRQ_NONE; |
| 287 | |
Kishon Vijay Abraham I | daaaa66 | 2018-10-17 13:11:12 +0530 | [diff] [blame] | 288 | if (reg & ERR_SYS) |
| 289 | dev_err(dev, "System Error\n"); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 290 | |
Kishon Vijay Abraham I | daaaa66 | 2018-10-17 13:11:12 +0530 | [diff] [blame] | 291 | if (reg & ERR_FATAL) |
| 292 | dev_err(dev, "Fatal Error\n"); |
| 293 | |
| 294 | if (reg & ERR_NONFATAL) |
| 295 | dev_dbg(dev, "Non Fatal Error\n"); |
| 296 | |
| 297 | if (reg & ERR_CORR) |
| 298 | dev_dbg(dev, "Correctable Error\n"); |
| 299 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 300 | if (!ks_pcie->is_am6 && (reg & ERR_AXI)) |
Kishon Vijay Abraham I | daaaa66 | 2018-10-17 13:11:12 +0530 | [diff] [blame] | 301 | dev_err(dev, "AXI tag lookup fatal Error\n"); |
| 302 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 303 | if (reg & ERR_AER || (ks_pcie->is_am6 && (reg & AM6_ERR_AER))) |
Kishon Vijay Abraham I | daaaa66 | 2018-10-17 13:11:12 +0530 | [diff] [blame] | 304 | dev_err(dev, "ECRC Error\n"); |
| 305 | |
| 306 | ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, reg); |
| 307 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 308 | return IRQ_HANDLED; |
| 309 | } |
| 310 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 311 | static void ks_pcie_ack_legacy_irq(struct irq_data *d) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 312 | { |
| 313 | } |
| 314 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 315 | static void ks_pcie_mask_legacy_irq(struct irq_data *d) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 316 | { |
| 317 | } |
| 318 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 319 | static void ks_pcie_unmask_legacy_irq(struct irq_data *d) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 320 | { |
| 321 | } |
| 322 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 323 | static struct irq_chip ks_pcie_legacy_irq_chip = { |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 324 | .name = "Keystone-PCI-Legacy-IRQ", |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 325 | .irq_ack = ks_pcie_ack_legacy_irq, |
| 326 | .irq_mask = ks_pcie_mask_legacy_irq, |
| 327 | .irq_unmask = ks_pcie_unmask_legacy_irq, |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 328 | }; |
| 329 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 330 | static int ks_pcie_init_legacy_irq_map(struct irq_domain *d, |
| 331 | unsigned int irq, |
| 332 | irq_hw_number_t hw_irq) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 333 | { |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 334 | irq_set_chip_and_handler(irq, &ks_pcie_legacy_irq_chip, |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 335 | handle_level_irq); |
| 336 | irq_set_chip_data(irq, d->host_data); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 341 | static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops = { |
| 342 | .map = ks_pcie_init_legacy_irq_map, |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 343 | .xlate = irq_domain_xlate_onetwocell, |
| 344 | }; |
| 345 | |
| 346 | /** |
Krzysztof Wilczyński | 43395d9 | 2021-03-11 00:17:17 +0000 | [diff] [blame] | 347 | * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask registers |
| 348 | * @ks_pcie: A pointer to the keystone_pcie structure which holds the KeyStone |
| 349 | * PCIe host controller driver information. |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 350 | * |
| 351 | * Since modification of dbi_cs2 involves different clock domain, read the |
| 352 | * status back to ensure the transition is complete. |
| 353 | */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 354 | static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 355 | { |
| 356 | u32 val; |
| 357 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 358 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
Kishon Vijay Abraham I | f9127db | 2018-10-17 13:11:09 +0530 | [diff] [blame] | 359 | val |= DBI_CS2; |
| 360 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 361 | |
| 362 | do { |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 363 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
Kishon Vijay Abraham I | f9127db | 2018-10-17 13:11:09 +0530 | [diff] [blame] | 364 | } while (!(val & DBI_CS2)); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 368 | * ks_pcie_clear_dbi_mode() - Disable DBI mode |
Krzysztof Wilczyński | 43395d9 | 2021-03-11 00:17:17 +0000 | [diff] [blame] | 369 | * @ks_pcie: A pointer to the keystone_pcie structure which holds the KeyStone |
| 370 | * PCIe host controller driver information. |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 371 | * |
| 372 | * Since modification of dbi_cs2 involves different clock domain, read the |
| 373 | * status back to ensure the transition is complete. |
| 374 | */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 375 | static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 376 | { |
| 377 | u32 val; |
| 378 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 379 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
Kishon Vijay Abraham I | f9127db | 2018-10-17 13:11:09 +0530 | [diff] [blame] | 380 | val &= ~DBI_CS2; |
| 381 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 382 | |
| 383 | do { |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 384 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
Kishon Vijay Abraham I | f9127db | 2018-10-17 13:11:09 +0530 | [diff] [blame] | 385 | } while (val & DBI_CS2); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 386 | } |
| 387 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 388 | static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 389 | { |
Kishon Vijay Abraham I | e75043a | 2018-10-17 13:11:08 +0530 | [diff] [blame] | 390 | u32 val; |
Rob Herring | fcde397 | 2020-11-05 15:11:57 -0600 | [diff] [blame] | 391 | u32 num_viewport = ks_pcie->num_viewport; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 392 | struct dw_pcie *pci = ks_pcie->pci; |
| 393 | struct pcie_port *pp = &pci->pp; |
Rob Herring | 0f71c60 | 2020-08-20 21:53:58 -0600 | [diff] [blame] | 394 | u64 start, end; |
| 395 | struct resource *mem; |
Kishon Vijay Abraham I | e75043a | 2018-10-17 13:11:08 +0530 | [diff] [blame] | 396 | int i; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 397 | |
Rob Herring | 0f71c60 | 2020-08-20 21:53:58 -0600 | [diff] [blame] | 398 | mem = resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM)->res; |
| 399 | start = mem->start; |
| 400 | end = mem->end; |
| 401 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 402 | /* Disable BARs for inbound access */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 403 | ks_pcie_set_dbi_mode(ks_pcie); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 404 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0); |
| 405 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0); |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 406 | ks_pcie_clear_dbi_mode(ks_pcie); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 407 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 408 | if (ks_pcie->is_am6) |
| 409 | return; |
| 410 | |
Kishon Vijay Abraham I | e75043a | 2018-10-17 13:11:08 +0530 | [diff] [blame] | 411 | val = ilog2(OB_WIN_SIZE); |
| 412 | ks_pcie_app_writel(ks_pcie, OB_SIZE, val); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 413 | |
| 414 | /* Using Direct 1:1 mapping of RC <-> PCI memory space */ |
Kishon Vijay Abraham I | e75043a | 2018-10-17 13:11:08 +0530 | [diff] [blame] | 415 | for (i = 0; i < num_viewport && (start < end); i++) { |
| 416 | ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i), |
| 417 | lower_32_bits(start) | OB_ENABLEN); |
| 418 | ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i), |
| 419 | upper_32_bits(start)); |
Yurii Monakov | 2d0c3fb | 2019-10-04 18:48:11 +0300 | [diff] [blame] | 420 | start += OB_WIN_SIZE * SZ_1M; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 421 | } |
| 422 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 423 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
Kishon Vijay Abraham I | e75043a | 2018-10-17 13:11:08 +0530 | [diff] [blame] | 424 | val |= OB_XLAT_EN_VAL; |
| 425 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 426 | } |
| 427 | |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 428 | static void __iomem *ks_pcie_other_map_bus(struct pci_bus *bus, |
| 429 | unsigned int devfn, int where) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 430 | { |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 431 | struct pcie_port *pp = bus->sysdata; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 432 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 433 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | 44c747a | 2018-10-17 13:11:06 +0530 | [diff] [blame] | 434 | u32 reg; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 435 | |
Kishon Vijay Abraham I | 44c747a | 2018-10-17 13:11:06 +0530 | [diff] [blame] | 436 | reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) | |
| 437 | CFG_FUNC(PCI_FUNC(devfn)); |
Rob Herring | 5525493 | 2020-07-21 20:25:00 -0600 | [diff] [blame] | 438 | if (!pci_is_root_bus(bus->parent)) |
Kishon Vijay Abraham I | 44c747a | 2018-10-17 13:11:06 +0530 | [diff] [blame] | 439 | reg |= CFG_TYPE1; |
| 440 | ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 441 | |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 442 | return pp->va_cfg0_base + where; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 443 | } |
| 444 | |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 445 | static struct pci_ops ks_child_pcie_ops = { |
| 446 | .map_bus = ks_pcie_other_map_bus, |
| 447 | .read = pci_generic_config_read, |
| 448 | .write = pci_generic_config_write, |
| 449 | }; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 450 | |
| 451 | /** |
Rob Herring | 6ab15b5 | 2020-08-20 21:53:55 -0600 | [diff] [blame] | 452 | * ks_pcie_v3_65_add_bus() - keystone add_bus post initialization |
Krzysztof Wilczyński | 43395d9 | 2021-03-11 00:17:17 +0000 | [diff] [blame] | 453 | * @bus: A pointer to the PCI bus structure. |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 454 | * |
| 455 | * This sets BAR0 to enable inbound access for MSI_IRQ register |
| 456 | */ |
Rob Herring | 6ab15b5 | 2020-08-20 21:53:55 -0600 | [diff] [blame] | 457 | static int ks_pcie_v3_65_add_bus(struct pci_bus *bus) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 458 | { |
Rob Herring | 6ab15b5 | 2020-08-20 21:53:55 -0600 | [diff] [blame] | 459 | struct pcie_port *pp = bus->sysdata; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 460 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 461 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 462 | |
Rob Herring | 6ab15b5 | 2020-08-20 21:53:55 -0600 | [diff] [blame] | 463 | if (!pci_is_root_bus(bus)) |
| 464 | return 0; |
| 465 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 466 | /* Configure and set up BAR0 */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 467 | ks_pcie_set_dbi_mode(ks_pcie); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 468 | |
| 469 | /* Enable BAR0 */ |
| 470 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1); |
| 471 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1); |
| 472 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 473 | ks_pcie_clear_dbi_mode(ks_pcie); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * For BAR0, just setting bus address for inbound writes (MSI) should |
| 477 | * be sufficient. Use physical address to avoid any conflicts. |
| 478 | */ |
| 479 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start); |
Rob Herring | 6ab15b5 | 2020-08-20 21:53:55 -0600 | [diff] [blame] | 480 | |
| 481 | return 0; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 482 | } |
| 483 | |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 484 | static struct pci_ops ks_pcie_ops = { |
| 485 | .map_bus = dw_pcie_own_conf_map_bus, |
| 486 | .read = pci_generic_config_read, |
| 487 | .write = pci_generic_config_write, |
Rob Herring | 6ab15b5 | 2020-08-20 21:53:55 -0600 | [diff] [blame] | 488 | .add_bus = ks_pcie_v3_65_add_bus, |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 489 | }; |
| 490 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 491 | /** |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 492 | * ks_pcie_link_up() - Check if link up |
Krzysztof Wilczyński | 43395d9 | 2021-03-11 00:17:17 +0000 | [diff] [blame] | 493 | * @pci: A pointer to the dw_pcie structure which holds the DesignWare PCIe host |
| 494 | * controller driver information. |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 495 | */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 496 | static int ks_pcie_link_up(struct dw_pcie *pci) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 497 | { |
| 498 | u32 val; |
| 499 | |
Kishon Vijay Abraham I | 23fe5bd | 2018-10-17 13:11:10 +0530 | [diff] [blame] | 500 | val = dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0); |
| 501 | val &= PORT_LOGIC_LTSSM_STATE_MASK; |
| 502 | return (val == PORT_LOGIC_LTSSM_STATE_L0); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 503 | } |
| 504 | |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 505 | static void ks_pcie_stop_link(struct dw_pcie *pci) |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 506 | { |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 507 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 508 | u32 val; |
| 509 | |
| 510 | /* Disable Link training */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 511 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 512 | val &= ~LTSSM_EN_VAL; |
Yurii Monakov | 6df1987 | 2019-12-17 14:38:36 +0300 | [diff] [blame] | 513 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static int ks_pcie_start_link(struct dw_pcie *pci) |
| 517 | { |
| 518 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 519 | u32 val; |
| 520 | |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 521 | /* Initiate Link Training */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 522 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 523 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val); |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 524 | |
| 525 | return 0; |
Kishon Vijay Abraham I | b492aca | 2018-10-17 13:10:59 +0530 | [diff] [blame] | 526 | } |
| 527 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 528 | static void ks_pcie_quirk(struct pci_dev *dev) |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 529 | { |
| 530 | struct pci_bus *bus = dev->bus; |
Kishon Vijay Abraham I | 00a2c40 | 2018-10-17 13:10:55 +0530 | [diff] [blame] | 531 | struct pci_dev *bridge; |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 532 | static const struct pci_device_id rc_pci_devids[] = { |
| 533 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK), |
| 534 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 535 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E), |
| 536 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 537 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L), |
| 538 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
Kishon Vijay Abraham I | 148e340 | 2018-10-17 13:10:54 +0530 | [diff] [blame] | 539 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G), |
| 540 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 541 | { 0, }, |
| 542 | }; |
| 543 | |
| 544 | if (pci_is_root_bus(bus)) |
Kishon Vijay Abraham I | 00a2c40 | 2018-10-17 13:10:55 +0530 | [diff] [blame] | 545 | bridge = dev; |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 546 | |
| 547 | /* look for the host bridge */ |
| 548 | while (!pci_is_root_bus(bus)) { |
| 549 | bridge = bus->self; |
| 550 | bus = bus->parent; |
| 551 | } |
| 552 | |
Kishon Vijay Abraham I | 00a2c40 | 2018-10-17 13:10:55 +0530 | [diff] [blame] | 553 | if (!bridge) |
| 554 | return; |
| 555 | |
| 556 | /* |
| 557 | * Keystone PCI controller has a h/w limitation of |
| 558 | * 256 bytes maximum read request size. It can't handle |
| 559 | * anything higher than this. So force this limit on |
| 560 | * all downstream devices. |
| 561 | */ |
| 562 | if (pci_match_id(rc_pci_devids, bridge)) { |
| 563 | if (pcie_get_readrq(dev) > 256) { |
| 564 | dev_info(&dev->dev, "limiting MRRS to 256\n"); |
| 565 | pcie_set_readrq(dev, 256); |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | } |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 569 | DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, ks_pcie_quirk); |
Murali Karicheri | c15982d | 2014-09-08 13:03:34 -0400 | [diff] [blame] | 570 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 571 | static void ks_pcie_msi_irq_handler(struct irq_desc *desc) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 572 | { |
Kishon Vijay Abraham I | f6f2900 | 2019-03-21 15:29:22 +0530 | [diff] [blame] | 573 | unsigned int irq = desc->irq_data.hwirq; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 574 | struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc); |
Kishon Vijay Abraham I | f6f2900 | 2019-03-21 15:29:22 +0530 | [diff] [blame] | 575 | u32 offset = irq - ks_pcie->msi_host_irq; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 576 | struct dw_pcie *pci = ks_pcie->pci; |
Kishon Vijay Abraham I | 66c10ec | 2019-03-21 15:29:23 +0530 | [diff] [blame] | 577 | struct pcie_port *pp = &pci->pp; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 578 | struct device *dev = pci->dev; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 579 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 580 | u32 vector, reg, pos; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 581 | |
Bjorn Helgaas | 21fa0c5 | 2016-10-11 22:48:42 -0500 | [diff] [blame] | 582 | dev_dbg(dev, "%s, irq %d\n", __func__, irq); |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * The chained irq handler installation would have replaced normal |
| 586 | * interrupt driver handler so we need to take care of mask/unmask and |
| 587 | * ack operation. |
| 588 | */ |
| 589 | chained_irq_enter(chip, desc); |
Kishon Vijay Abraham I | 66c10ec | 2019-03-21 15:29:23 +0530 | [diff] [blame] | 590 | |
| 591 | reg = ks_pcie_app_readl(ks_pcie, MSI_IRQ_STATUS(offset)); |
| 592 | /* |
| 593 | * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit |
| 594 | * shows 1, 9, 17, 25 and so forth |
| 595 | */ |
| 596 | for (pos = 0; pos < 4; pos++) { |
| 597 | if (!(reg & BIT(pos))) |
| 598 | continue; |
| 599 | |
| 600 | vector = offset + (pos << 3); |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 601 | dev_dbg(dev, "irq: bit %d, vector %d\n", pos, vector); |
| 602 | generic_handle_domain_irq(pp->irq_domain, vector); |
Kishon Vijay Abraham I | 66c10ec | 2019-03-21 15:29:23 +0530 | [diff] [blame] | 603 | } |
| 604 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 605 | chained_irq_exit(chip, desc); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * ks_pcie_legacy_irq_handler() - Handle legacy interrupt |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 610 | * @desc: Pointer to irq descriptor |
| 611 | * |
| 612 | * Traverse through pending legacy interrupts and invoke handler for each. Also |
| 613 | * takes care of interrupt controller level mask/ack operation. |
| 614 | */ |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 615 | static void ks_pcie_legacy_irq_handler(struct irq_desc *desc) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 616 | { |
Thomas Gleixner | 97a8596 | 2015-07-16 23:24:10 +0200 | [diff] [blame] | 617 | unsigned int irq = irq_desc_get_irq(desc); |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 618 | struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 619 | struct dw_pcie *pci = ks_pcie->pci; |
| 620 | struct device *dev = pci->dev; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 621 | u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0]; |
| 622 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 623 | |
Bjorn Helgaas | 21fa0c5 | 2016-10-11 22:48:42 -0500 | [diff] [blame] | 624 | dev_dbg(dev, ": Handling legacy irq %d\n", irq); |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 625 | |
| 626 | /* |
| 627 | * The chained irq handler installation would have replaced normal |
| 628 | * interrupt driver handler so we need to take care of mask/unmask and |
| 629 | * ack operation. |
| 630 | */ |
| 631 | chained_irq_enter(chip, desc); |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 632 | ks_pcie_handle_legacy_irq(ks_pcie, irq_offset); |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 633 | chained_irq_exit(chip, desc); |
| 634 | } |
| 635 | |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 636 | static int ks_pcie_config_msi_irq(struct keystone_pcie *ks_pcie) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 637 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 638 | struct device *dev = ks_pcie->pci->dev; |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 639 | struct device_node *np = ks_pcie->np; |
| 640 | struct device_node *intc_np; |
Kishon Vijay Abraham I | f6f2900 | 2019-03-21 15:29:22 +0530 | [diff] [blame] | 641 | struct irq_data *irq_data; |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 642 | int irq_count, irq, ret, i; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 643 | |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 644 | if (!IS_ENABLED(CONFIG_PCI_MSI)) |
Murali Karicheri | 1e9f8dc | 2016-04-11 10:50:31 -0400 | [diff] [blame] | 645 | return 0; |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 646 | |
| 647 | intc_np = of_get_child_by_name(np, "msi-interrupt-controller"); |
| 648 | if (!intc_np) { |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 649 | if (ks_pcie->is_am6) |
| 650 | return 0; |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 651 | dev_warn(dev, "msi-interrupt-controller node is absent\n"); |
| 652 | return -EINVAL; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 653 | } |
Murali Karicheri | 1e9f8dc | 2016-04-11 10:50:31 -0400 | [diff] [blame] | 654 | |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 655 | irq_count = of_irq_count(intc_np); |
| 656 | if (!irq_count) { |
| 657 | dev_err(dev, "No IRQ entries in msi-interrupt-controller\n"); |
| 658 | ret = -EINVAL; |
| 659 | goto err; |
| 660 | } |
| 661 | |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 662 | for (i = 0; i < irq_count; i++) { |
| 663 | irq = irq_of_parse_and_map(intc_np, i); |
| 664 | if (!irq) { |
| 665 | ret = -EINVAL; |
| 666 | goto err; |
| 667 | } |
Kishon Vijay Abraham I | f6f2900 | 2019-03-21 15:29:22 +0530 | [diff] [blame] | 668 | |
| 669 | if (!ks_pcie->msi_host_irq) { |
| 670 | irq_data = irq_get_irq_data(irq); |
| 671 | if (!irq_data) { |
| 672 | ret = -EINVAL; |
| 673 | goto err; |
| 674 | } |
| 675 | ks_pcie->msi_host_irq = irq_data->hwirq; |
| 676 | } |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 677 | |
| 678 | irq_set_chained_handler_and_data(irq, ks_pcie_msi_irq_handler, |
| 679 | ks_pcie); |
| 680 | } |
| 681 | |
| 682 | of_node_put(intc_np); |
| 683 | return 0; |
| 684 | |
| 685 | err: |
| 686 | of_node_put(intc_np); |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | static int ks_pcie_config_legacy_irq(struct keystone_pcie *ks_pcie) |
| 691 | { |
| 692 | struct device *dev = ks_pcie->pci->dev; |
| 693 | struct irq_domain *legacy_irq_domain; |
| 694 | struct device_node *np = ks_pcie->np; |
| 695 | struct device_node *intc_np; |
| 696 | int irq_count, irq, ret = 0, i; |
| 697 | |
| 698 | intc_np = of_get_child_by_name(np, "legacy-interrupt-controller"); |
| 699 | if (!intc_np) { |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 700 | /* |
| 701 | * Since legacy interrupts are modeled as edge-interrupts in |
| 702 | * AM6, keep it disabled for now. |
| 703 | */ |
| 704 | if (ks_pcie->is_am6) |
| 705 | return 0; |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 706 | dev_warn(dev, "legacy-interrupt-controller node is absent\n"); |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | |
| 710 | irq_count = of_irq_count(intc_np); |
| 711 | if (!irq_count) { |
| 712 | dev_err(dev, "No IRQ entries in legacy-interrupt-controller\n"); |
| 713 | ret = -EINVAL; |
| 714 | goto err; |
| 715 | } |
| 716 | |
| 717 | for (i = 0; i < irq_count; i++) { |
| 718 | irq = irq_of_parse_and_map(intc_np, i); |
| 719 | if (!irq) { |
| 720 | ret = -EINVAL; |
| 721 | goto err; |
| 722 | } |
| 723 | ks_pcie->legacy_host_irqs[i] = irq; |
| 724 | |
| 725 | irq_set_chained_handler_and_data(irq, |
| 726 | ks_pcie_legacy_irq_handler, |
| 727 | ks_pcie); |
| 728 | } |
| 729 | |
| 730 | legacy_irq_domain = |
| 731 | irq_domain_add_linear(intc_np, PCI_NUM_INTX, |
| 732 | &ks_pcie_legacy_irq_domain_ops, NULL); |
| 733 | if (!legacy_irq_domain) { |
| 734 | dev_err(dev, "Failed to add irq domain for legacy irqs\n"); |
| 735 | ret = -EINVAL; |
| 736 | goto err; |
| 737 | } |
| 738 | ks_pcie->legacy_irq_domain = legacy_irq_domain; |
| 739 | |
| 740 | for (i = 0; i < PCI_NUM_INTX; i++) |
| 741 | ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET(i), INTx_EN); |
| 742 | |
| 743 | err: |
| 744 | of_node_put(intc_np); |
| 745 | return ret; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 746 | } |
| 747 | |
Kishon Vijay Abraham I | f316a2b | 2019-03-25 15:09:33 +0530 | [diff] [blame] | 748 | #ifdef CONFIG_ARM |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 749 | /* |
| 750 | * When a PCI device does not exist during config cycles, keystone host gets a |
| 751 | * bus error instead of returning 0xffffffff. This handler always returns 0 |
| 752 | * for this kind of faults. |
| 753 | */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 754 | static int ks_pcie_fault(unsigned long addr, unsigned int fsr, |
| 755 | struct pt_regs *regs) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 756 | { |
| 757 | unsigned long instr = *(unsigned long *) instruction_pointer(regs); |
| 758 | |
| 759 | if ((instr & 0x0e100090) == 0x00100090) { |
| 760 | int reg = (instr >> 12) & 15; |
| 761 | |
| 762 | regs->uregs[reg] = -1; |
| 763 | regs->ARM_pc += 4; |
| 764 | } |
| 765 | |
| 766 | return 0; |
| 767 | } |
Kishon Vijay Abraham I | f316a2b | 2019-03-25 15:09:33 +0530 | [diff] [blame] | 768 | #endif |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 769 | |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 770 | static int __init ks_pcie_init_id(struct keystone_pcie *ks_pcie) |
| 771 | { |
| 772 | int ret; |
| 773 | unsigned int id; |
| 774 | struct regmap *devctrl_regs; |
| 775 | struct dw_pcie *pci = ks_pcie->pci; |
| 776 | struct device *dev = pci->dev; |
| 777 | struct device_node *np = dev->of_node; |
| 778 | |
| 779 | devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-id"); |
| 780 | if (IS_ERR(devctrl_regs)) |
| 781 | return PTR_ERR(devctrl_regs); |
| 782 | |
| 783 | ret = regmap_read(devctrl_regs, 0, &id); |
| 784 | if (ret) |
| 785 | return ret; |
| 786 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 787 | dw_pcie_dbi_ro_wr_en(pci); |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 788 | dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, id & PCIE_VENDORID_MASK); |
| 789 | dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, id >> PCIE_DEVICEID_SHIFT); |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 790 | dw_pcie_dbi_ro_wr_dis(pci); |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 795 | static int __init ks_pcie_host_init(struct pcie_port *pp) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 796 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 797 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 798 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 799 | int ret; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 800 | |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 801 | pp->bridge->ops = &ks_pcie_ops; |
Kishon Vijay Abraham I | 3d0b2a3 | 2021-03-17 18:45:18 +0530 | [diff] [blame] | 802 | if (!ks_pcie->is_am6) |
| 803 | pp->bridge->child_ops = &ks_child_pcie_ops; |
Rob Herring | 10a797c | 2020-08-20 21:53:46 -0600 | [diff] [blame] | 804 | |
Kishon Vijay Abraham I | 1146c29 | 2019-03-21 15:29:21 +0530 | [diff] [blame] | 805 | ret = ks_pcie_config_legacy_irq(ks_pcie); |
| 806 | if (ret) |
| 807 | return ret; |
| 808 | |
| 809 | ret = ks_pcie_config_msi_irq(ks_pcie); |
| 810 | if (ret) |
| 811 | return ret; |
| 812 | |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 813 | ks_pcie_stop_link(pci); |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 814 | ks_pcie_setup_rc_app_regs(ks_pcie); |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 815 | writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8), |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 816 | pci->dbi_base + PCI_IO_BASE); |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 817 | |
Kishon Vijay Abraham I | b51a625 | 2018-10-17 13:11:03 +0530 | [diff] [blame] | 818 | ret = ks_pcie_init_id(ks_pcie); |
| 819 | if (ret < 0) |
| 820 | return ret; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 821 | |
Kishon Vijay Abraham I | f316a2b | 2019-03-25 15:09:33 +0530 | [diff] [blame] | 822 | #ifdef CONFIG_ARM |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 823 | /* |
| 824 | * PCIe access errors that result into OCP errors are caught by ARM as |
| 825 | * "External aborts" |
| 826 | */ |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 827 | hook_fault_code(17, ks_pcie_fault, SIGBUS, 0, |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 828 | "Asynchronous external abort"); |
Kishon Vijay Abraham I | f316a2b | 2019-03-25 15:09:33 +0530 | [diff] [blame] | 829 | #endif |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 830 | |
| 831 | return 0; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 832 | } |
| 833 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 834 | static const struct dw_pcie_host_ops ks_pcie_host_ops = { |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 835 | .host_init = ks_pcie_host_init, |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 836 | .msi_host_init = ks_pcie_msi_host_init, |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 837 | }; |
| 838 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 839 | static const struct dw_pcie_host_ops ks_pcie_am654_host_ops = { |
| 840 | .host_init = ks_pcie_host_init, |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 841 | }; |
| 842 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 843 | static irqreturn_t ks_pcie_err_irq_handler(int irq, void *priv) |
Murali Karicheri | 025dd3d | 2016-04-11 10:50:30 -0400 | [diff] [blame] | 844 | { |
| 845 | struct keystone_pcie *ks_pcie = priv; |
| 846 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 847 | return ks_pcie_handle_error_irq(ks_pcie); |
Murali Karicheri | 025dd3d | 2016-04-11 10:50:30 -0400 | [diff] [blame] | 848 | } |
| 849 | |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 850 | static void ks_pcie_am654_write_dbi2(struct dw_pcie *pci, void __iomem *base, |
| 851 | u32 reg, size_t size, u32 val) |
| 852 | { |
| 853 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 854 | |
| 855 | ks_pcie_set_dbi_mode(ks_pcie); |
| 856 | dw_pcie_write(base + reg, size, val); |
| 857 | ks_pcie_clear_dbi_mode(ks_pcie); |
| 858 | } |
| 859 | |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 860 | static const struct dw_pcie_ops ks_pcie_dw_pcie_ops = { |
Kishon Vijay Abraham I | 9afb20d | 2019-03-25 15:09:22 +0530 | [diff] [blame] | 861 | .start_link = ks_pcie_start_link, |
| 862 | .stop_link = ks_pcie_stop_link, |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 863 | .link_up = ks_pcie_link_up, |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 864 | .write_dbi2 = ks_pcie_am654_write_dbi2, |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 865 | }; |
| 866 | |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 867 | static void ks_pcie_am654_ep_init(struct dw_pcie_ep *ep) |
| 868 | { |
| 869 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 870 | int flags; |
| 871 | |
| 872 | ep->page_size = AM654_WIN_SIZE; |
| 873 | flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32; |
| 874 | dw_pcie_writel_dbi2(pci, PCI_BASE_ADDRESS_0, APP_ADDR_SPACE_0 - 1); |
| 875 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, flags); |
| 876 | } |
| 877 | |
| 878 | static void ks_pcie_am654_raise_legacy_irq(struct keystone_pcie *ks_pcie) |
| 879 | { |
| 880 | struct dw_pcie *pci = ks_pcie->pci; |
| 881 | u8 int_pin; |
| 882 | |
| 883 | int_pin = dw_pcie_readb_dbi(pci, PCI_INTERRUPT_PIN); |
| 884 | if (int_pin == 0 || int_pin > 4) |
| 885 | return; |
| 886 | |
| 887 | ks_pcie_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_SET(int_pin), |
| 888 | INT_ENABLE); |
| 889 | ks_pcie_app_writel(ks_pcie, PCIE_EP_IRQ_SET, INT_ENABLE); |
| 890 | mdelay(1); |
| 891 | ks_pcie_app_writel(ks_pcie, PCIE_EP_IRQ_CLR, INT_ENABLE); |
| 892 | ks_pcie_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_CLR(int_pin), |
| 893 | INT_ENABLE); |
| 894 | } |
| 895 | |
| 896 | static int ks_pcie_am654_raise_irq(struct dw_pcie_ep *ep, u8 func_no, |
| 897 | enum pci_epc_irq_type type, |
| 898 | u16 interrupt_num) |
| 899 | { |
| 900 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 901 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 902 | |
| 903 | switch (type) { |
| 904 | case PCI_EPC_IRQ_LEGACY: |
| 905 | ks_pcie_am654_raise_legacy_irq(ks_pcie); |
| 906 | break; |
| 907 | case PCI_EPC_IRQ_MSI: |
| 908 | dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num); |
| 909 | break; |
Kishon Vijay Abraham I | 146d328 | 2020-02-25 13:47:03 +0530 | [diff] [blame] | 910 | case PCI_EPC_IRQ_MSIX: |
| 911 | dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num); |
| 912 | break; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 913 | default: |
| 914 | dev_err(pci->dev, "UNKNOWN IRQ type\n"); |
| 915 | return -EINVAL; |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static const struct pci_epc_features ks_pcie_am654_epc_features = { |
| 922 | .linkup_notifier = false, |
| 923 | .msi_capable = true, |
Kishon Vijay Abraham I | 146d328 | 2020-02-25 13:47:03 +0530 | [diff] [blame] | 924 | .msix_capable = true, |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 925 | .reserved_bar = 1 << BAR_0 | 1 << BAR_1, |
| 926 | .bar_fixed_64bit = 1 << BAR_0, |
| 927 | .bar_fixed_size[2] = SZ_1M, |
| 928 | .bar_fixed_size[3] = SZ_64K, |
| 929 | .bar_fixed_size[4] = 256, |
| 930 | .bar_fixed_size[5] = SZ_1M, |
| 931 | .align = SZ_1M, |
| 932 | }; |
| 933 | |
| 934 | static const struct pci_epc_features* |
| 935 | ks_pcie_am654_get_features(struct dw_pcie_ep *ep) |
| 936 | { |
| 937 | return &ks_pcie_am654_epc_features; |
| 938 | } |
| 939 | |
| 940 | static const struct dw_pcie_ep_ops ks_pcie_am654_ep_ops = { |
| 941 | .ep_init = ks_pcie_am654_ep_init, |
| 942 | .raise_irq = ks_pcie_am654_raise_irq, |
| 943 | .get_features = &ks_pcie_am654_get_features, |
| 944 | }; |
| 945 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 946 | static void ks_pcie_disable_phy(struct keystone_pcie *ks_pcie) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 947 | { |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 948 | int num_lanes = ks_pcie->num_lanes; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 949 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 950 | while (num_lanes--) { |
| 951 | phy_power_off(ks_pcie->phy[num_lanes]); |
| 952 | phy_exit(ks_pcie->phy[num_lanes]); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | static int ks_pcie_enable_phy(struct keystone_pcie *ks_pcie) |
| 957 | { |
| 958 | int i; |
| 959 | int ret; |
| 960 | int num_lanes = ks_pcie->num_lanes; |
| 961 | |
| 962 | for (i = 0; i < num_lanes; i++) { |
Kishon Vijay Abraham I | b22af42 | 2019-03-25 15:09:36 +0530 | [diff] [blame] | 963 | ret = phy_reset(ks_pcie->phy[i]); |
| 964 | if (ret < 0) |
| 965 | goto err_phy; |
| 966 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 967 | ret = phy_init(ks_pcie->phy[i]); |
| 968 | if (ret < 0) |
| 969 | goto err_phy; |
| 970 | |
| 971 | ret = phy_power_on(ks_pcie->phy[i]); |
| 972 | if (ret < 0) { |
| 973 | phy_exit(ks_pcie->phy[i]); |
| 974 | goto err_phy; |
| 975 | } |
| 976 | } |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 977 | |
| 978 | return 0; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 979 | |
| 980 | err_phy: |
| 981 | while (--i >= 0) { |
| 982 | phy_power_off(ks_pcie->phy[i]); |
| 983 | phy_exit(ks_pcie->phy[i]); |
| 984 | } |
| 985 | |
| 986 | return ret; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 987 | } |
| 988 | |
Kishon Vijay Abraham I | 156c6fe | 2019-03-25 15:09:29 +0530 | [diff] [blame] | 989 | static int ks_pcie_set_mode(struct device *dev) |
| 990 | { |
| 991 | struct device_node *np = dev->of_node; |
| 992 | struct regmap *syscon; |
| 993 | u32 val; |
| 994 | u32 mask; |
| 995 | int ret = 0; |
| 996 | |
| 997 | syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-mode"); |
| 998 | if (IS_ERR(syscon)) |
| 999 | return 0; |
| 1000 | |
| 1001 | mask = KS_PCIE_DEV_TYPE_MASK | KS_PCIE_SYSCLOCKOUTEN; |
| 1002 | val = KS_PCIE_DEV_TYPE(RC) | KS_PCIE_SYSCLOCKOUTEN; |
| 1003 | |
| 1004 | ret = regmap_update_bits(syscon, 0, mask, val); |
| 1005 | if (ret) { |
| 1006 | dev_err(dev, "failed to set pcie mode\n"); |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1013 | static int ks_pcie_am654_set_mode(struct device *dev, |
| 1014 | enum dw_pcie_device_mode mode) |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1015 | { |
| 1016 | struct device_node *np = dev->of_node; |
| 1017 | struct regmap *syscon; |
| 1018 | u32 val; |
| 1019 | u32 mask; |
| 1020 | int ret = 0; |
| 1021 | |
| 1022 | syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-mode"); |
| 1023 | if (IS_ERR(syscon)) |
| 1024 | return 0; |
| 1025 | |
| 1026 | mask = AM654_PCIE_DEV_TYPE_MASK; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1027 | |
| 1028 | switch (mode) { |
| 1029 | case DW_PCIE_RC_TYPE: |
| 1030 | val = RC; |
| 1031 | break; |
| 1032 | case DW_PCIE_EP_TYPE: |
| 1033 | val = EP; |
| 1034 | break; |
| 1035 | default: |
| 1036 | dev_err(dev, "INVALID device type %d\n", mode); |
| 1037 | return -EINVAL; |
| 1038 | } |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1039 | |
| 1040 | ret = regmap_update_bits(syscon, 0, mask, val); |
| 1041 | if (ret) { |
| 1042 | dev_err(dev, "failed to set pcie mode\n"); |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static const struct ks_pcie_of_data ks_pcie_rc_of_data = { |
| 1050 | .host_ops = &ks_pcie_host_ops, |
| 1051 | .version = 0x365A, |
| 1052 | }; |
| 1053 | |
| 1054 | static const struct ks_pcie_of_data ks_pcie_am654_rc_of_data = { |
| 1055 | .host_ops = &ks_pcie_am654_host_ops, |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1056 | .mode = DW_PCIE_RC_TYPE, |
| 1057 | .version = 0x490A, |
| 1058 | }; |
| 1059 | |
| 1060 | static const struct ks_pcie_of_data ks_pcie_am654_ep_of_data = { |
| 1061 | .ep_ops = &ks_pcie_am654_ep_ops, |
| 1062 | .mode = DW_PCIE_EP_TYPE, |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1063 | .version = 0x490A, |
| 1064 | }; |
| 1065 | |
| 1066 | static const struct of_device_id ks_pcie_of_match[] = { |
| 1067 | { |
| 1068 | .type = "pci", |
| 1069 | .data = &ks_pcie_rc_of_data, |
| 1070 | .compatible = "ti,keystone-pcie", |
| 1071 | }, |
| 1072 | { |
| 1073 | .data = &ks_pcie_am654_rc_of_data, |
| 1074 | .compatible = "ti,am654-pcie-rc", |
| 1075 | }, |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1076 | { |
| 1077 | .data = &ks_pcie_am654_ep_of_data, |
| 1078 | .compatible = "ti,am654-pcie-ep", |
| 1079 | }, |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1080 | { }, |
| 1081 | }; |
| 1082 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1083 | static int __init ks_pcie_probe(struct platform_device *pdev) |
| 1084 | { |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1085 | const struct dw_pcie_host_ops *host_ops; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1086 | const struct dw_pcie_ep_ops *ep_ops; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1087 | struct device *dev = &pdev->dev; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1088 | struct device_node *np = dev->of_node; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1089 | const struct ks_pcie_of_data *data; |
| 1090 | const struct of_device_id *match; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1091 | enum dw_pcie_device_mode mode; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 1092 | struct dw_pcie *pci; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1093 | struct keystone_pcie *ks_pcie; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1094 | struct device_link **link; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1095 | struct gpio_desc *gpiod; |
Kishon Vijay Abraham I | b1dee41 | 2019-03-25 15:09:27 +0530 | [diff] [blame] | 1096 | struct resource *res; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1097 | unsigned int version; |
Kishon Vijay Abraham I | b1dee41 | 2019-03-25 15:09:27 +0530 | [diff] [blame] | 1098 | void __iomem *base; |
Rob Herring | fcde397 | 2020-11-05 15:11:57 -0600 | [diff] [blame] | 1099 | u32 num_viewport; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1100 | struct phy **phy; |
| 1101 | u32 num_lanes; |
| 1102 | char name[10]; |
Murali Karicheri | 1e9f8dc | 2016-04-11 10:50:31 -0400 | [diff] [blame] | 1103 | int ret; |
Kishon Vijay Abraham I | 0790eb1 | 2019-03-25 15:09:23 +0530 | [diff] [blame] | 1104 | int irq; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1105 | int i; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1106 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1107 | match = of_match_device(of_match_ptr(ks_pcie_of_match), dev); |
| 1108 | data = (struct ks_pcie_of_data *)match->data; |
| 1109 | if (!data) |
| 1110 | return -EINVAL; |
| 1111 | |
| 1112 | version = data->version; |
| 1113 | host_ops = data->host_ops; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1114 | ep_ops = data->ep_ops; |
| 1115 | mode = data->mode; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1116 | |
Bjorn Helgaas | 21fa0c5 | 2016-10-11 22:48:42 -0500 | [diff] [blame] | 1117 | ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL); |
Jingoo Han | 6670070 | 2014-11-12 12:22:56 +0900 | [diff] [blame] | 1118 | if (!ks_pcie) |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1119 | return -ENOMEM; |
Jingoo Han | 6670070 | 2014-11-12 12:22:56 +0900 | [diff] [blame] | 1120 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 1121 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 1122 | if (!pci) |
| 1123 | return -ENOMEM; |
| 1124 | |
Kishon Vijay Abraham I | b1dee41 | 2019-03-25 15:09:27 +0530 | [diff] [blame] | 1125 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "app"); |
| 1126 | ks_pcie->va_app_base = devm_ioremap_resource(dev, res); |
| 1127 | if (IS_ERR(ks_pcie->va_app_base)) |
| 1128 | return PTR_ERR(ks_pcie->va_app_base); |
| 1129 | |
| 1130 | ks_pcie->app = *res; |
| 1131 | |
| 1132 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbics"); |
| 1133 | base = devm_pci_remap_cfg_resource(dev, res); |
| 1134 | if (IS_ERR(base)) |
| 1135 | return PTR_ERR(base); |
| 1136 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1137 | if (of_device_is_compatible(np, "ti,am654-pcie-rc")) |
| 1138 | ks_pcie->is_am6 = true; |
| 1139 | |
Kishon Vijay Abraham I | b1dee41 | 2019-03-25 15:09:27 +0530 | [diff] [blame] | 1140 | pci->dbi_base = base; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1141 | pci->dbi_base2 = base; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 1142 | pci->dev = dev; |
Kishon Vijay Abraham I | a1cabd2 | 2018-10-17 13:11:01 +0530 | [diff] [blame] | 1143 | pci->ops = &ks_pcie_dw_pcie_ops; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1144 | pci->version = version; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1145 | |
Kishon Vijay Abraham I | 0790eb1 | 2019-03-25 15:09:23 +0530 | [diff] [blame] | 1146 | irq = platform_get_irq(pdev, 0); |
Krzysztof Wilczyński | caecb05 | 2020-08-02 14:25:53 +0000 | [diff] [blame] | 1147 | if (irq < 0) |
Kishon Vijay Abraham I | 0790eb1 | 2019-03-25 15:09:23 +0530 | [diff] [blame] | 1148 | return irq; |
Kishon Vijay Abraham I | 0790eb1 | 2019-03-25 15:09:23 +0530 | [diff] [blame] | 1149 | |
| 1150 | ret = request_irq(irq, ks_pcie_err_irq_handler, IRQF_SHARED, |
| 1151 | "ks-pcie-error-irq", ks_pcie); |
| 1152 | if (ret < 0) { |
| 1153 | dev_err(dev, "failed to request error IRQ %d\n", |
| 1154 | irq); |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1158 | ret = of_property_read_u32(np, "num-lanes", &num_lanes); |
| 1159 | if (ret) |
| 1160 | num_lanes = 1; |
Guenter Roeck | c046406 | 2017-02-25 02:08:12 -0800 | [diff] [blame] | 1161 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1162 | phy = devm_kzalloc(dev, sizeof(*phy) * num_lanes, GFP_KERNEL); |
| 1163 | if (!phy) |
| 1164 | return -ENOMEM; |
Shawn Lin | 25de15c9 | 2016-03-07 12:32:21 +0800 | [diff] [blame] | 1165 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1166 | link = devm_kzalloc(dev, sizeof(*link) * num_lanes, GFP_KERNEL); |
| 1167 | if (!link) |
| 1168 | return -ENOMEM; |
| 1169 | |
| 1170 | for (i = 0; i < num_lanes; i++) { |
| 1171 | snprintf(name, sizeof(name), "pcie-phy%d", i); |
| 1172 | phy[i] = devm_phy_optional_get(dev, name); |
| 1173 | if (IS_ERR(phy[i])) { |
| 1174 | ret = PTR_ERR(phy[i]); |
| 1175 | goto err_link; |
| 1176 | } |
| 1177 | |
| 1178 | if (!phy[i]) |
| 1179 | continue; |
| 1180 | |
| 1181 | link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS); |
| 1182 | if (!link[i]) { |
| 1183 | ret = -EINVAL; |
| 1184 | goto err_link; |
| 1185 | } |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1186 | } |
| 1187 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1188 | ks_pcie->np = np; |
| 1189 | ks_pcie->pci = pci; |
| 1190 | ks_pcie->link = link; |
| 1191 | ks_pcie->num_lanes = num_lanes; |
| 1192 | ks_pcie->phy = phy; |
| 1193 | |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1194 | gpiod = devm_gpiod_get_optional(dev, "reset", |
| 1195 | GPIOD_OUT_LOW); |
| 1196 | if (IS_ERR(gpiod)) { |
| 1197 | ret = PTR_ERR(gpiod); |
| 1198 | if (ret != -EPROBE_DEFER) |
| 1199 | dev_err(dev, "Failed to get reset GPIO\n"); |
| 1200 | goto err_link; |
| 1201 | } |
| 1202 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1203 | ret = ks_pcie_enable_phy(ks_pcie); |
| 1204 | if (ret) { |
| 1205 | dev_err(dev, "failed to enable phy\n"); |
| 1206 | goto err_link; |
| 1207 | } |
| 1208 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1209 | platform_set_drvdata(pdev, ks_pcie); |
Kishon Vijay Abraham I | 8047eb5 | 2018-10-17 13:11:05 +0530 | [diff] [blame] | 1210 | pm_runtime_enable(dev); |
| 1211 | ret = pm_runtime_get_sync(dev); |
| 1212 | if (ret < 0) { |
| 1213 | dev_err(dev, "pm_runtime_get_sync failed\n"); |
| 1214 | goto err_get_sync; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1215 | } |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1216 | |
Kunihiko Hayashi | b101a39 | 2020-09-30 14:36:07 +0900 | [diff] [blame] | 1217 | if (pci->version >= 0x480A) |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1218 | ret = ks_pcie_am654_set_mode(dev, mode); |
Kunihiko Hayashi | b101a39 | 2020-09-30 14:36:07 +0900 | [diff] [blame] | 1219 | else |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1220 | ret = ks_pcie_set_mode(dev); |
Kunihiko Hayashi | b101a39 | 2020-09-30 14:36:07 +0900 | [diff] [blame] | 1221 | if (ret < 0) |
| 1222 | goto err_get_sync; |
Kishon Vijay Abraham I | 18b0415 | 2019-03-25 15:09:35 +0530 | [diff] [blame] | 1223 | |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1224 | switch (mode) { |
| 1225 | case DW_PCIE_RC_TYPE: |
| 1226 | if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_HOST)) { |
| 1227 | ret = -ENODEV; |
| 1228 | goto err_get_sync; |
| 1229 | } |
| 1230 | |
Rob Herring | fcde397 | 2020-11-05 15:11:57 -0600 | [diff] [blame] | 1231 | ret = of_property_read_u32(np, "num-viewport", &num_viewport); |
| 1232 | if (ret < 0) { |
| 1233 | dev_err(dev, "unable to read *num-viewport* property\n"); |
| 1234 | goto err_get_sync; |
| 1235 | } |
| 1236 | |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1237 | /* |
| 1238 | * "Power Sequencing and Reset Signal Timings" table in |
| 1239 | * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 2.0 |
| 1240 | * indicates PERST# should be deasserted after minimum of 100us |
| 1241 | * once REFCLK is stable. The REFCLK to the connector in RC |
| 1242 | * mode is selected while enabling the PHY. So deassert PERST# |
| 1243 | * after 100 us. |
| 1244 | */ |
| 1245 | if (gpiod) { |
| 1246 | usleep_range(100, 200); |
| 1247 | gpiod_set_value_cansleep(gpiod, 1); |
| 1248 | } |
| 1249 | |
Rob Herring | fcde397 | 2020-11-05 15:11:57 -0600 | [diff] [blame] | 1250 | ks_pcie->num_viewport = num_viewport; |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1251 | pci->pp.ops = host_ops; |
Rob Herring | 60f5b73 | 2020-11-05 15:11:56 -0600 | [diff] [blame] | 1252 | ret = dw_pcie_host_init(&pci->pp); |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1253 | if (ret < 0) |
| 1254 | goto err_get_sync; |
| 1255 | break; |
| 1256 | case DW_PCIE_EP_TYPE: |
| 1257 | if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_EP)) { |
| 1258 | ret = -ENODEV; |
| 1259 | goto err_get_sync; |
| 1260 | } |
| 1261 | |
| 1262 | pci->ep.ops = ep_ops; |
Rob Herring | a0fd361 | 2020-11-05 15:11:46 -0600 | [diff] [blame] | 1263 | ret = dw_pcie_ep_init(&pci->ep); |
Kishon Vijay Abraham I | 23284ad | 2019-03-25 15:09:43 +0530 | [diff] [blame] | 1264 | if (ret < 0) |
| 1265 | goto err_get_sync; |
| 1266 | break; |
| 1267 | default: |
| 1268 | dev_err(dev, "INVALID device type %d\n", mode); |
| 1269 | } |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1270 | |
Kishon Vijay Abraham I | 0790eb1 | 2019-03-25 15:09:23 +0530 | [diff] [blame] | 1271 | ks_pcie_enable_error_irq(ks_pcie); |
| 1272 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1273 | return 0; |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1274 | |
Kishon Vijay Abraham I | 8047eb5 | 2018-10-17 13:11:05 +0530 | [diff] [blame] | 1275 | err_get_sync: |
| 1276 | pm_runtime_put(dev); |
| 1277 | pm_runtime_disable(dev); |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1278 | ks_pcie_disable_phy(ks_pcie); |
| 1279 | |
| 1280 | err_link: |
| 1281 | while (--i >= 0 && link[i]) |
| 1282 | device_link_del(link[i]); |
| 1283 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1284 | return ret; |
| 1285 | } |
| 1286 | |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1287 | static int __exit ks_pcie_remove(struct platform_device *pdev) |
| 1288 | { |
| 1289 | struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev); |
| 1290 | struct device_link **link = ks_pcie->link; |
| 1291 | int num_lanes = ks_pcie->num_lanes; |
Kishon Vijay Abraham I | 8047eb5 | 2018-10-17 13:11:05 +0530 | [diff] [blame] | 1292 | struct device *dev = &pdev->dev; |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1293 | |
Kishon Vijay Abraham I | 8047eb5 | 2018-10-17 13:11:05 +0530 | [diff] [blame] | 1294 | pm_runtime_put(dev); |
| 1295 | pm_runtime_disable(dev); |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1296 | ks_pcie_disable_phy(ks_pcie); |
Kishon Vijay Abraham I | 4922923 | 2018-10-17 13:11:04 +0530 | [diff] [blame] | 1297 | while (num_lanes--) |
| 1298 | device_link_del(link[num_lanes]); |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1303 | static struct platform_driver ks_pcie_driver __refdata = { |
| 1304 | .probe = ks_pcie_probe, |
| 1305 | .remove = __exit_p(ks_pcie_remove), |
| 1306 | .driver = { |
| 1307 | .name = "keystone-pcie", |
Murali Karicheri | 0c4ffcf | 2014-09-02 17:26:19 -0600 | [diff] [blame] | 1308 | .of_match_table = of_match_ptr(ks_pcie_of_match), |
| 1309 | }, |
| 1310 | }; |
Paul Gortmaker | 1481bf2 | 2016-07-02 19:13:26 -0400 | [diff] [blame] | 1311 | builtin_platform_driver(ks_pcie_driver); |