Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 1 | /* |
| 2 | * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * |
| 6 | * Authors: Kishon Vijay Abraham I <kishon@ti.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 13 | #include <linux/delay.h> |
Kishon Vijay Abraham I | 7a4db65 | 2017-10-09 14:33:37 +0530 | [diff] [blame] | 14 | #include <linux/device.h> |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/irqdomain.h> |
| 19 | #include <linux/kernel.h> |
Paul Gortmaker | d29438d | 2016-08-24 16:57:47 -0400 | [diff] [blame] | 20 | #include <linux/init.h> |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Kishon Vijay Abraham I | 78bdcad | 2015-07-28 19:09:09 +0530 | [diff] [blame] | 22 | #include <linux/of_gpio.h> |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 23 | #include <linux/of_pci.h> |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 24 | #include <linux/pci.h> |
| 25 | #include <linux/phy/phy.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/pm_runtime.h> |
| 28 | #include <linux/resource.h> |
| 29 | #include <linux/types.h> |
Kishon Vijay Abraham I | f7a2757 | 2017-03-27 15:15:11 +0530 | [diff] [blame] | 30 | #include <linux/mfd/syscon.h> |
| 31 | #include <linux/regmap.h> |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 32 | |
| 33 | #include "pcie-designware.h" |
| 34 | |
| 35 | /* PCIe controller wrapper DRA7XX configuration registers */ |
| 36 | |
| 37 | #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024 |
| 38 | #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028 |
| 39 | #define ERR_SYS BIT(0) |
| 40 | #define ERR_FATAL BIT(1) |
| 41 | #define ERR_NONFATAL BIT(2) |
| 42 | #define ERR_COR BIT(3) |
| 43 | #define ERR_AXI BIT(4) |
| 44 | #define ERR_ECRC BIT(5) |
| 45 | #define PME_TURN_OFF BIT(8) |
| 46 | #define PME_TO_ACK BIT(9) |
| 47 | #define PM_PME BIT(10) |
| 48 | #define LINK_REQ_RST BIT(11) |
| 49 | #define LINK_UP_EVT BIT(12) |
| 50 | #define CFG_BME_EVT BIT(13) |
| 51 | #define CFG_MSE_EVT BIT(14) |
| 52 | #define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \ |
| 53 | ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \ |
| 54 | LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT) |
| 55 | |
| 56 | #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034 |
| 57 | #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038 |
| 58 | #define INTA BIT(0) |
| 59 | #define INTB BIT(1) |
| 60 | #define INTC BIT(2) |
| 61 | #define INTD BIT(3) |
| 62 | #define MSI BIT(4) |
| 63 | #define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD) |
| 64 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 65 | #define PCIECTRL_TI_CONF_DEVICE_TYPE 0x0100 |
| 66 | #define DEVICE_TYPE_EP 0x0 |
| 67 | #define DEVICE_TYPE_LEG_EP 0x1 |
| 68 | #define DEVICE_TYPE_RC 0x4 |
| 69 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 70 | #define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104 |
| 71 | #define LTSSM_EN 0x1 |
| 72 | |
| 73 | #define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C |
| 74 | #define LINK_UP BIT(16) |
Gabriele Paoloni | 883cc17 | 2015-10-29 19:56:51 -0500 | [diff] [blame] | 75 | #define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 76 | |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 77 | #define EXP_CAP_ID_OFFSET 0x70 |
| 78 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 79 | #define PCIECTRL_TI_CONF_INTX_ASSERT 0x0124 |
| 80 | #define PCIECTRL_TI_CONF_INTX_DEASSERT 0x0128 |
| 81 | |
| 82 | #define PCIECTRL_TI_CONF_MSI_XMT 0x012c |
| 83 | #define MSI_REQ_GRANT BIT(0) |
| 84 | #define MSI_VECTOR_SHIFT 7 |
| 85 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 86 | struct dra7xx_pcie { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 87 | struct dw_pcie *pci; |
Bjorn Helgaas | 8e5ec41 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 88 | void __iomem *base; /* DT ti_conf */ |
| 89 | int phy_count; /* DT phy-names count */ |
| 90 | struct phy **phy; |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 91 | int link_gen; |
Kishon Vijay Abraham I | ebe85a4 | 2017-01-11 17:36:54 +0530 | [diff] [blame] | 92 | struct irq_domain *irq_domain; |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 93 | enum dw_pcie_device_mode mode; |
| 94 | }; |
| 95 | |
| 96 | struct dra7xx_pcie_of_data { |
| 97 | enum dw_pcie_device_mode mode; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 98 | }; |
| 99 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 100 | #define to_dra7xx_pcie(x) dev_get_drvdata((x)->dev) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 101 | |
| 102 | static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset) |
| 103 | { |
| 104 | return readl(pcie->base + offset); |
| 105 | } |
| 106 | |
| 107 | static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset, |
| 108 | u32 value) |
| 109 | { |
| 110 | writel(value, pcie->base + offset); |
| 111 | } |
| 112 | |
Kishon Vijay Abraham I | 2ed6cc7 | 2017-03-13 19:13:23 +0530 | [diff] [blame] | 113 | static u64 dra7xx_pcie_cpu_addr_fixup(u64 pci_addr) |
| 114 | { |
| 115 | return pci_addr & DRA7XX_CPU_TO_BUS_ADDR; |
| 116 | } |
| 117 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 118 | static int dra7xx_pcie_link_up(struct dw_pcie *pci) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 119 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 120 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 121 | u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS); |
| 122 | |
| 123 | return !!(reg & LINK_UP); |
| 124 | } |
| 125 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 126 | static void dra7xx_pcie_stop_link(struct dw_pcie *pci) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 127 | { |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 128 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
| 129 | u32 reg; |
| 130 | |
| 131 | reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD); |
| 132 | reg &= ~LTSSM_EN; |
| 133 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg); |
| 134 | } |
| 135 | |
| 136 | static int dra7xx_pcie_establish_link(struct dw_pcie *pci) |
| 137 | { |
| 138 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 139 | struct device *dev = pci->dev; |
Bjorn Helgaas | 6cbb247 | 2015-06-02 16:47:17 -0500 | [diff] [blame] | 140 | u32 reg; |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 141 | u32 exp_cap_off = EXP_CAP_ID_OFFSET; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 142 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 143 | if (dw_pcie_link_up(pci)) { |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 144 | dev_err(dev, "link is already up\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 148 | if (dra7xx->link_gen == 1) { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 149 | dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP, |
Kishon Vijay Abraham I | 19ce01cc | 2017-02-15 18:48:12 +0530 | [diff] [blame] | 150 | 4, ®); |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 151 | if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) { |
| 152 | reg &= ~((u32)PCI_EXP_LNKCAP_SLS); |
| 153 | reg |= PCI_EXP_LNKCAP_SLS_2_5GB; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 154 | dw_pcie_write(pci->dbi_base + exp_cap_off + |
Kishon Vijay Abraham I | 19ce01cc | 2017-02-15 18:48:12 +0530 | [diff] [blame] | 155 | PCI_EXP_LNKCAP, 4, reg); |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 156 | } |
| 157 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 158 | dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2, |
Kishon Vijay Abraham I | 19ce01cc | 2017-02-15 18:48:12 +0530 | [diff] [blame] | 159 | 2, ®); |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 160 | if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) { |
| 161 | reg &= ~((u32)PCI_EXP_LNKCAP_SLS); |
| 162 | reg |= PCI_EXP_LNKCAP_SLS_2_5GB; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 163 | dw_pcie_write(pci->dbi_base + exp_cap_off + |
Kishon Vijay Abraham I | 19ce01cc | 2017-02-15 18:48:12 +0530 | [diff] [blame] | 164 | PCI_EXP_LNKCTL2, 2, reg); |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 168 | reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD); |
| 169 | reg |= LTSSM_EN; |
| 170 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg); |
| 171 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 172 | return 0; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Kishon Vijay Abraham I | 5ffd90a | 2017-03-27 15:15:07 +0530 | [diff] [blame] | 175 | static void dra7xx_pcie_enable_msi_interrupts(struct dra7xx_pcie *dra7xx) |
| 176 | { |
| 177 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, |
Arvind Yadav | 40aa52c | 2017-06-19 15:21:36 +0530 | [diff] [blame] | 178 | LEG_EP_INTERRUPTS | MSI); |
Kishon Vijay Abraham I | 5ffd90a | 2017-03-27 15:15:07 +0530 | [diff] [blame] | 179 | |
| 180 | dra7xx_pcie_writel(dra7xx, |
| 181 | PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI, |
| 182 | MSI | LEG_EP_INTERRUPTS); |
| 183 | } |
| 184 | |
| 185 | static void dra7xx_pcie_enable_wrapper_interrupts(struct dra7xx_pcie *dra7xx) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 186 | { |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 187 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, |
Arvind Yadav | 40aa52c | 2017-06-19 15:21:36 +0530 | [diff] [blame] | 188 | INTERRUPTS); |
Kishon Vijay Abraham I | 5ffd90a | 2017-03-27 15:15:07 +0530 | [diff] [blame] | 189 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, |
| 190 | INTERRUPTS); |
| 191 | } |
| 192 | |
| 193 | static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx) |
| 194 | { |
| 195 | dra7xx_pcie_enable_wrapper_interrupts(dra7xx); |
| 196 | dra7xx_pcie_enable_msi_interrupts(dra7xx); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 199 | static int dra7xx_pcie_host_init(struct pcie_port *pp) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 200 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 201 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 202 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
Bjorn Helgaas | 21baa1c | 2016-10-06 13:33:05 -0500 | [diff] [blame] | 203 | |
Jisheng Zhang | 7e57fd1 | 2016-03-16 19:40:33 +0800 | [diff] [blame] | 204 | dw_pcie_setup_rc(pp); |
| 205 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 206 | dra7xx_pcie_establish_link(pci); |
| 207 | dw_pcie_wait_for_link(pci); |
Kishon Vijay Abraham I | ebe85a4 | 2017-01-11 17:36:54 +0530 | [diff] [blame] | 208 | dw_pcie_msi_init(pp); |
Bjorn Helgaas | 21baa1c | 2016-10-06 13:33:05 -0500 | [diff] [blame] | 209 | dra7xx_pcie_enable_interrupts(dra7xx); |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 210 | |
| 211 | return 0; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 212 | } |
| 213 | |
Jisheng Zhang | 4ab2e7c | 2017-06-05 16:53:46 +0800 | [diff] [blame] | 214 | static const struct dw_pcie_host_ops dra7xx_pcie_host_ops = { |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 215 | .host_init = dra7xx_pcie_host_init, |
| 216 | }; |
| 217 | |
| 218 | static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq, |
| 219 | irq_hw_number_t hwirq) |
| 220 | { |
| 221 | irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq); |
| 222 | irq_set_chip_data(irq, domain->host_data); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static const struct irq_domain_ops intx_domain_ops = { |
| 228 | .map = dra7xx_pcie_intx_map, |
| 229 | }; |
| 230 | |
| 231 | static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp) |
| 232 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 233 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 234 | struct device *dev = pci->dev; |
| 235 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 236 | struct device_node *node = dev->of_node; |
| 237 | struct device_node *pcie_intc_node = of_get_next_child(node, NULL); |
| 238 | |
| 239 | if (!pcie_intc_node) { |
| 240 | dev_err(dev, "No PCIe Intc node found\n"); |
Christophe JAILLET | 991bfef | 2016-07-14 23:18:27 +0200 | [diff] [blame] | 241 | return -ENODEV; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 242 | } |
| 243 | |
Bjorn Helgaas | 61534d1 | 2017-08-15 16:28:27 -0500 | [diff] [blame] | 244 | dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX, |
Kishon Vijay Abraham I | ebe85a4 | 2017-01-11 17:36:54 +0530 | [diff] [blame] | 245 | &intx_domain_ops, pp); |
| 246 | if (!dra7xx->irq_domain) { |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 247 | dev_err(dev, "Failed to get a INTx IRQ domain\n"); |
Christophe JAILLET | 991bfef | 2016-07-14 23:18:27 +0200 | [diff] [blame] | 248 | return -ENODEV; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg) |
| 255 | { |
Bjorn Helgaas | 21baa1c | 2016-10-06 13:33:05 -0500 | [diff] [blame] | 256 | struct dra7xx_pcie *dra7xx = arg; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 257 | struct dw_pcie *pci = dra7xx->pci; |
| 258 | struct pcie_port *pp = &pci->pp; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 259 | u32 reg; |
| 260 | |
| 261 | reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI); |
| 262 | |
| 263 | switch (reg) { |
| 264 | case MSI: |
| 265 | dw_handle_msi_irq(pp); |
| 266 | break; |
| 267 | case INTA: |
| 268 | case INTB: |
| 269 | case INTC: |
| 270 | case INTD: |
Kishon Vijay Abraham I | ebe85a4 | 2017-01-11 17:36:54 +0530 | [diff] [blame] | 271 | generic_handle_irq(irq_find_mapping(dra7xx->irq_domain, |
| 272 | ffs(reg))); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | |
| 276 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg); |
| 277 | |
| 278 | return IRQ_HANDLED; |
| 279 | } |
| 280 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 281 | static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg) |
| 282 | { |
| 283 | struct dra7xx_pcie *dra7xx = arg; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 284 | struct dw_pcie *pci = dra7xx->pci; |
| 285 | struct device *dev = pci->dev; |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 286 | struct dw_pcie_ep *ep = &pci->ep; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 287 | u32 reg; |
| 288 | |
| 289 | reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN); |
| 290 | |
| 291 | if (reg & ERR_SYS) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 292 | dev_dbg(dev, "System Error\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 293 | |
| 294 | if (reg & ERR_FATAL) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 295 | dev_dbg(dev, "Fatal Error\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 296 | |
| 297 | if (reg & ERR_NONFATAL) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 298 | dev_dbg(dev, "Non Fatal Error\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 299 | |
| 300 | if (reg & ERR_COR) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 301 | dev_dbg(dev, "Correctable Error\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 302 | |
| 303 | if (reg & ERR_AXI) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 304 | dev_dbg(dev, "AXI tag lookup fatal Error\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 305 | |
| 306 | if (reg & ERR_ECRC) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 307 | dev_dbg(dev, "ECRC Error\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 308 | |
| 309 | if (reg & PME_TURN_OFF) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 310 | dev_dbg(dev, |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 311 | "Power Management Event Turn-Off message received\n"); |
| 312 | |
| 313 | if (reg & PME_TO_ACK) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 314 | dev_dbg(dev, |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 315 | "Power Management Turn-Off Ack message received\n"); |
| 316 | |
| 317 | if (reg & PM_PME) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 318 | dev_dbg(dev, "PM Power Management Event message received\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 319 | |
| 320 | if (reg & LINK_REQ_RST) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 321 | dev_dbg(dev, "Link Request Reset\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 322 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 323 | if (reg & LINK_UP_EVT) { |
| 324 | if (dra7xx->mode == DW_PCIE_EP_TYPE) |
| 325 | dw_pcie_ep_linkup(ep); |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 326 | dev_dbg(dev, "Link-up state change\n"); |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 327 | } |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 328 | |
| 329 | if (reg & CFG_BME_EVT) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 330 | dev_dbg(dev, "CFG 'Bus Master Enable' change\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 331 | |
| 332 | if (reg & CFG_MSE_EVT) |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 333 | dev_dbg(dev, "CFG 'Memory Space Enable' change\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 334 | |
| 335 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg); |
| 336 | |
| 337 | return IRQ_HANDLED; |
| 338 | } |
| 339 | |
Kishon Vijay Abraham I | 85aa139 | 2017-08-18 20:28:03 +0530 | [diff] [blame] | 340 | static void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar) |
| 341 | { |
| 342 | u32 reg; |
| 343 | |
| 344 | reg = PCI_BASE_ADDRESS_0 + (4 * bar); |
| 345 | dw_pcie_writel_dbi2(pci, reg, 0x0); |
| 346 | dw_pcie_writel_dbi(pci, reg, 0x0); |
| 347 | } |
| 348 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 349 | static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep) |
| 350 | { |
| 351 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 352 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
Kishon Vijay Abraham I | 85aa139 | 2017-08-18 20:28:03 +0530 | [diff] [blame] | 353 | enum pci_barno bar; |
| 354 | |
| 355 | for (bar = BAR_0; bar <= BAR_5; bar++) |
| 356 | dw_pcie_ep_reset_bar(pci, bar); |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 357 | |
| 358 | dra7xx_pcie_enable_wrapper_interrupts(dra7xx); |
| 359 | } |
| 360 | |
| 361 | static void dra7xx_pcie_raise_legacy_irq(struct dra7xx_pcie *dra7xx) |
| 362 | { |
| 363 | dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1); |
| 364 | mdelay(1); |
| 365 | dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1); |
| 366 | } |
| 367 | |
| 368 | static void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx, |
| 369 | u8 interrupt_num) |
| 370 | { |
| 371 | u32 reg; |
| 372 | |
| 373 | reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT; |
| 374 | reg |= MSI_REQ_GRANT; |
| 375 | dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg); |
| 376 | } |
| 377 | |
| 378 | static int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep, |
| 379 | enum pci_epc_irq_type type, u8 interrupt_num) |
| 380 | { |
| 381 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 382 | struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci); |
| 383 | |
| 384 | switch (type) { |
| 385 | case PCI_EPC_IRQ_LEGACY: |
| 386 | dra7xx_pcie_raise_legacy_irq(dra7xx); |
| 387 | break; |
| 388 | case PCI_EPC_IRQ_MSI: |
| 389 | dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num); |
| 390 | break; |
| 391 | default: |
| 392 | dev_err(pci->dev, "UNKNOWN IRQ type\n"); |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static struct dw_pcie_ep_ops pcie_ep_ops = { |
| 399 | .ep_init = dra7xx_pcie_ep_init, |
| 400 | .raise_irq = dra7xx_pcie_raise_irq, |
| 401 | }; |
| 402 | |
| 403 | static int __init dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx, |
| 404 | struct platform_device *pdev) |
| 405 | { |
| 406 | int ret; |
| 407 | struct dw_pcie_ep *ep; |
| 408 | struct resource *res; |
| 409 | struct device *dev = &pdev->dev; |
| 410 | struct dw_pcie *pci = dra7xx->pci; |
| 411 | |
| 412 | ep = &pci->ep; |
| 413 | ep->ops = &pcie_ep_ops; |
| 414 | |
| 415 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics"); |
| 416 | pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res)); |
| 417 | if (!pci->dbi_base) |
| 418 | return -ENOMEM; |
| 419 | |
| 420 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics2"); |
| 421 | pci->dbi_base2 = devm_ioremap(dev, res->start, resource_size(res)); |
| 422 | if (!pci->dbi_base2) |
| 423 | return -ENOMEM; |
| 424 | |
| 425 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space"); |
| 426 | if (!res) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | ep->phys_base = res->start; |
| 430 | ep->addr_size = resource_size(res); |
| 431 | |
| 432 | ret = dw_pcie_ep_init(ep); |
| 433 | if (ret) { |
| 434 | dev_err(dev, "failed to initialize endpoint\n"); |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
Jingoo Han | e73044a | 2014-11-06 14:37:39 +0900 | [diff] [blame] | 441 | static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx, |
| 442 | struct platform_device *pdev) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 443 | { |
| 444 | int ret; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 445 | struct dw_pcie *pci = dra7xx->pci; |
| 446 | struct pcie_port *pp = &pci->pp; |
| 447 | struct device *dev = pci->dev; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 448 | struct resource *res; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 449 | |
| 450 | pp->irq = platform_get_irq(pdev, 1); |
| 451 | if (pp->irq < 0) { |
| 452 | dev_err(dev, "missing IRQ resource\n"); |
Fabio Estevam | 2f3ec75 | 2017-08-31 14:52:02 -0300 | [diff] [blame] | 453 | return pp->irq; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 454 | } |
| 455 | |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 456 | ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler, |
Grygorii Strashko | 8ff0ef9 | 2015-12-10 21:18:20 +0200 | [diff] [blame] | 457 | IRQF_SHARED | IRQF_NO_THREAD, |
Bjorn Helgaas | 21baa1c | 2016-10-06 13:33:05 -0500 | [diff] [blame] | 458 | "dra7-pcie-msi", dra7xx); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 459 | if (ret) { |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 460 | dev_err(dev, "failed to request irq\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 461 | return ret; |
| 462 | } |
| 463 | |
Kishon Vijay Abraham I | ebe85a4 | 2017-01-11 17:36:54 +0530 | [diff] [blame] | 464 | ret = dra7xx_pcie_init_irq_domain(pp); |
| 465 | if (ret < 0) |
| 466 | return ret; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 467 | |
| 468 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics"); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 469 | pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res)); |
| 470 | if (!pci->dbi_base) |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 471 | return -ENOMEM; |
| 472 | |
| 473 | ret = dw_pcie_host_init(pp); |
| 474 | if (ret) { |
Bjorn Helgaas | c7f8146 | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 475 | dev_err(dev, "failed to initialize host\n"); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 482 | static const struct dw_pcie_ops dw_pcie_ops = { |
Kishon Vijay Abraham I | 2ed6cc7 | 2017-03-13 19:13:23 +0530 | [diff] [blame] | 483 | .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup, |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 484 | .start_link = dra7xx_pcie_establish_link, |
| 485 | .stop_link = dra7xx_pcie_stop_link, |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 486 | .link_up = dra7xx_pcie_link_up, |
| 487 | }; |
| 488 | |
Kishon Vijay Abraham I | 1f6c450 | 2017-01-11 17:36:55 +0530 | [diff] [blame] | 489 | static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx) |
| 490 | { |
| 491 | int phy_count = dra7xx->phy_count; |
| 492 | |
| 493 | while (phy_count--) { |
| 494 | phy_power_off(dra7xx->phy[phy_count]); |
| 495 | phy_exit(dra7xx->phy[phy_count]); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx) |
| 500 | { |
| 501 | int phy_count = dra7xx->phy_count; |
| 502 | int ret; |
| 503 | int i; |
| 504 | |
| 505 | for (i = 0; i < phy_count; i++) { |
| 506 | ret = phy_init(dra7xx->phy[i]); |
| 507 | if (ret < 0) |
| 508 | goto err_phy; |
| 509 | |
| 510 | ret = phy_power_on(dra7xx->phy[i]); |
| 511 | if (ret < 0) { |
| 512 | phy_exit(dra7xx->phy[i]); |
| 513 | goto err_phy; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | |
| 519 | err_phy: |
| 520 | while (--i >= 0) { |
| 521 | phy_power_off(dra7xx->phy[i]); |
| 522 | phy_exit(dra7xx->phy[i]); |
| 523 | } |
| 524 | |
| 525 | return ret; |
| 526 | } |
| 527 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 528 | static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = { |
| 529 | .mode = DW_PCIE_RC_TYPE, |
| 530 | }; |
| 531 | |
| 532 | static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = { |
| 533 | .mode = DW_PCIE_EP_TYPE, |
| 534 | }; |
| 535 | |
| 536 | static const struct of_device_id of_dra7xx_pcie_match[] = { |
| 537 | { |
| 538 | .compatible = "ti,dra7-pcie", |
| 539 | .data = &dra7xx_pcie_rc_of_data, |
| 540 | }, |
| 541 | { |
| 542 | .compatible = "ti,dra7-pcie-ep", |
| 543 | .data = &dra7xx_pcie_ep_of_data, |
| 544 | }, |
| 545 | {}, |
| 546 | }; |
| 547 | |
Kishon Vijay Abraham I | f7a2757 | 2017-03-27 15:15:11 +0530 | [diff] [blame] | 548 | /* |
| 549 | * dra7xx_pcie_ep_unaligned_memaccess: workaround for AM572x/AM571x Errata i870 |
| 550 | * @dra7xx: the dra7xx device where the workaround should be applied |
| 551 | * |
| 552 | * Access to the PCIe slave port that are not 32-bit aligned will result |
| 553 | * in incorrect mapping to TLP Address and Byte enable fields. Therefore, |
| 554 | * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or |
| 555 | * 0x3. |
| 556 | * |
| 557 | * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1. |
| 558 | */ |
| 559 | static int dra7xx_pcie_ep_unaligned_memaccess(struct device *dev) |
| 560 | { |
| 561 | int ret; |
| 562 | struct device_node *np = dev->of_node; |
| 563 | struct of_phandle_args args; |
| 564 | struct regmap *regmap; |
| 565 | |
| 566 | regmap = syscon_regmap_lookup_by_phandle(np, |
| 567 | "ti,syscon-unaligned-access"); |
| 568 | if (IS_ERR(regmap)) { |
| 569 | dev_dbg(dev, "can't get ti,syscon-unaligned-access\n"); |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access", |
| 574 | 2, 0, &args); |
| 575 | if (ret) { |
| 576 | dev_err(dev, "failed to parse ti,syscon-unaligned-access\n"); |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | ret = regmap_update_bits(regmap, args.args[0], args.args[1], |
| 581 | args.args[1]); |
| 582 | if (ret) |
| 583 | dev_err(dev, "failed to enable unaligned access\n"); |
| 584 | |
| 585 | of_node_put(args.np); |
| 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 590 | static int __init dra7xx_pcie_probe(struct platform_device *pdev) |
| 591 | { |
| 592 | u32 reg; |
| 593 | int ret; |
| 594 | int irq; |
| 595 | int i; |
| 596 | int phy_count; |
| 597 | struct phy **phy; |
Kishon Vijay Abraham I | 7a4db65 | 2017-10-09 14:33:37 +0530 | [diff] [blame] | 598 | struct device_link **link; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 599 | void __iomem *base; |
| 600 | struct resource *res; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 601 | struct dw_pcie *pci; |
Bjorn Helgaas | 150645b | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 602 | struct pcie_port *pp; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 603 | struct dra7xx_pcie *dra7xx; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 604 | struct device *dev = &pdev->dev; |
| 605 | struct device_node *np = dev->of_node; |
| 606 | char name[10]; |
Kishon Vijay Abraham I | 602d38b | 2017-01-11 17:36:52 +0530 | [diff] [blame] | 607 | struct gpio_desc *reset; |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 608 | const struct of_device_id *match; |
| 609 | const struct dra7xx_pcie_of_data *data; |
| 610 | enum dw_pcie_device_mode mode; |
| 611 | |
| 612 | match = of_match_device(of_match_ptr(of_dra7xx_pcie_match), dev); |
| 613 | if (!match) |
| 614 | return -EINVAL; |
| 615 | |
| 616 | data = (struct dra7xx_pcie_of_data *)match->data; |
| 617 | mode = (enum dw_pcie_device_mode)data->mode; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 618 | |
| 619 | dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL); |
| 620 | if (!dra7xx) |
| 621 | return -ENOMEM; |
| 622 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 623 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 624 | if (!pci) |
| 625 | return -ENOMEM; |
| 626 | |
| 627 | pci->dev = dev; |
| 628 | pci->ops = &dw_pcie_ops; |
| 629 | |
| 630 | pp = &pci->pp; |
Bjorn Helgaas | 150645b | 2016-10-06 13:33:06 -0500 | [diff] [blame] | 631 | pp->ops = &dra7xx_pcie_host_ops; |
| 632 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 633 | irq = platform_get_irq(pdev, 0); |
| 634 | if (irq < 0) { |
Gustavo A. R. Silva | a0d21ba | 2017-08-09 11:16:03 -0500 | [diff] [blame] | 635 | dev_err(dev, "missing IRQ resource: %d\n", irq); |
| 636 | return irq; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 637 | } |
| 638 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 639 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf"); |
| 640 | base = devm_ioremap_nocache(dev, res->start, resource_size(res)); |
| 641 | if (!base) |
| 642 | return -ENOMEM; |
| 643 | |
| 644 | phy_count = of_property_count_strings(np, "phy-names"); |
| 645 | if (phy_count < 0) { |
| 646 | dev_err(dev, "unable to find the strings\n"); |
| 647 | return phy_count; |
| 648 | } |
| 649 | |
| 650 | phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL); |
| 651 | if (!phy) |
| 652 | return -ENOMEM; |
| 653 | |
Kishon Vijay Abraham I | 7a4db65 | 2017-10-09 14:33:37 +0530 | [diff] [blame] | 654 | link = devm_kzalloc(dev, sizeof(*link) * phy_count, GFP_KERNEL); |
| 655 | if (!link) |
| 656 | return -ENOMEM; |
| 657 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 658 | for (i = 0; i < phy_count; i++) { |
| 659 | snprintf(name, sizeof(name), "pcie-phy%d", i); |
| 660 | phy[i] = devm_phy_get(dev, name); |
| 661 | if (IS_ERR(phy[i])) |
| 662 | return PTR_ERR(phy[i]); |
Kishon Vijay Abraham I | 7a4db65 | 2017-10-09 14:33:37 +0530 | [diff] [blame] | 663 | |
| 664 | link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS); |
| 665 | if (!link[i]) { |
| 666 | ret = -EINVAL; |
| 667 | goto err_link; |
| 668 | } |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | dra7xx->base = base; |
| 672 | dra7xx->phy = phy; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 673 | dra7xx->pci = pci; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 674 | dra7xx->phy_count = phy_count; |
| 675 | |
Kishon Vijay Abraham I | 1f6c450 | 2017-01-11 17:36:55 +0530 | [diff] [blame] | 676 | ret = dra7xx_pcie_enable_phy(dra7xx); |
| 677 | if (ret) { |
| 678 | dev_err(dev, "failed to enable phy\n"); |
| 679 | return ret; |
| 680 | } |
| 681 | |
Kishon Vijay Abraham I | 9bcf0a6 | 2017-02-15 18:48:11 +0530 | [diff] [blame] | 682 | platform_set_drvdata(pdev, dra7xx); |
| 683 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 684 | pm_runtime_enable(dev); |
| 685 | ret = pm_runtime_get_sync(dev); |
Fabio Estevam | d3f4caa | 2015-08-20 01:30:36 -0500 | [diff] [blame] | 686 | if (ret < 0) { |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 687 | dev_err(dev, "pm_runtime_get_sync failed\n"); |
Kishon Vijay Abraham I | 0e2bdb0 | 2015-07-31 17:55:10 +0530 | [diff] [blame] | 688 | goto err_get_sync; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 689 | } |
| 690 | |
Kishon Vijay Abraham I | 602d38b | 2017-01-11 17:36:52 +0530 | [diff] [blame] | 691 | reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH); |
| 692 | if (IS_ERR(reset)) { |
| 693 | ret = PTR_ERR(reset); |
| 694 | dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret); |
Kishon Vijay Abraham I | 78bdcad | 2015-07-28 19:09:09 +0530 | [diff] [blame] | 695 | goto err_gpio; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD); |
| 699 | reg &= ~LTSSM_EN; |
| 700 | dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg); |
| 701 | |
Kishon Vijay Abraham I | ab5fe4f | 2017-01-11 17:36:53 +0530 | [diff] [blame] | 702 | dra7xx->link_gen = of_pci_get_max_link_speed(np); |
| 703 | if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2) |
| 704 | dra7xx->link_gen = 2; |
| 705 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 706 | switch (mode) { |
| 707 | case DW_PCIE_RC_TYPE: |
| 708 | dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE, |
| 709 | DEVICE_TYPE_RC); |
| 710 | ret = dra7xx_add_pcie_port(dra7xx, pdev); |
| 711 | if (ret < 0) |
| 712 | goto err_gpio; |
| 713 | break; |
| 714 | case DW_PCIE_EP_TYPE: |
| 715 | dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE, |
| 716 | DEVICE_TYPE_EP); |
Kishon Vijay Abraham I | f7a2757 | 2017-03-27 15:15:11 +0530 | [diff] [blame] | 717 | |
| 718 | ret = dra7xx_pcie_ep_unaligned_memaccess(dev); |
| 719 | if (ret) |
| 720 | goto err_gpio; |
| 721 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 722 | ret = dra7xx_add_pcie_ep(dra7xx, pdev); |
| 723 | if (ret < 0) |
| 724 | goto err_gpio; |
| 725 | break; |
| 726 | default: |
| 727 | dev_err(dev, "INVALID device type %d\n", mode); |
| 728 | } |
| 729 | dra7xx->mode = mode; |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 730 | |
Keerthy | d4c7d1a | 2017-03-13 19:13:28 +0530 | [diff] [blame] | 731 | ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler, |
| 732 | IRQF_SHARED, "dra7xx-pcie-main", dra7xx); |
| 733 | if (ret) { |
| 734 | dev_err(dev, "failed to request irq\n"); |
| 735 | goto err_gpio; |
| 736 | } |
| 737 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 738 | return 0; |
| 739 | |
Kishon Vijay Abraham I | 78bdcad | 2015-07-28 19:09:09 +0530 | [diff] [blame] | 740 | err_gpio: |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 741 | pm_runtime_put(dev); |
Kishon Vijay Abraham I | 0e2bdb0 | 2015-07-31 17:55:10 +0530 | [diff] [blame] | 742 | |
| 743 | err_get_sync: |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 744 | pm_runtime_disable(dev); |
Kishon Vijay Abraham I | 1f6c450 | 2017-01-11 17:36:55 +0530 | [diff] [blame] | 745 | dra7xx_pcie_disable_phy(dra7xx); |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 746 | |
Kishon Vijay Abraham I | 7a4db65 | 2017-10-09 14:33:37 +0530 | [diff] [blame] | 747 | err_link: |
| 748 | while (--i >= 0) |
| 749 | device_link_del(link[i]); |
| 750 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 751 | return ret; |
| 752 | } |
| 753 | |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 754 | #ifdef CONFIG_PM_SLEEP |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 755 | static int dra7xx_pcie_suspend(struct device *dev) |
| 756 | { |
| 757 | struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 758 | struct dw_pcie *pci = dra7xx->pci; |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 759 | u32 val; |
| 760 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 761 | if (dra7xx->mode != DW_PCIE_RC_TYPE) |
| 762 | return 0; |
| 763 | |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 764 | /* clear MSE */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 765 | val = dw_pcie_readl_dbi(pci, PCI_COMMAND); |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 766 | val &= ~PCI_COMMAND_MEMORY; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 767 | dw_pcie_writel_dbi(pci, PCI_COMMAND, val); |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static int dra7xx_pcie_resume(struct device *dev) |
| 773 | { |
| 774 | struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 775 | struct dw_pcie *pci = dra7xx->pci; |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 776 | u32 val; |
| 777 | |
Kishon Vijay Abraham I | 608793e | 2017-03-27 15:15:08 +0530 | [diff] [blame] | 778 | if (dra7xx->mode != DW_PCIE_RC_TYPE) |
| 779 | return 0; |
| 780 | |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 781 | /* set MSE */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 782 | val = dw_pcie_readl_dbi(pci, PCI_COMMAND); |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 783 | val |= PCI_COMMAND_MEMORY; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 784 | dw_pcie_writel_dbi(pci, PCI_COMMAND, val); |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 789 | static int dra7xx_pcie_suspend_noirq(struct device *dev) |
| 790 | { |
| 791 | struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev); |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 792 | |
Kishon Vijay Abraham I | 1f6c450 | 2017-01-11 17:36:55 +0530 | [diff] [blame] | 793 | dra7xx_pcie_disable_phy(dra7xx); |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | static int dra7xx_pcie_resume_noirq(struct device *dev) |
| 799 | { |
| 800 | struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev); |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 801 | int ret; |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 802 | |
Kishon Vijay Abraham I | 1f6c450 | 2017-01-11 17:36:55 +0530 | [diff] [blame] | 803 | ret = dra7xx_pcie_enable_phy(dra7xx); |
| 804 | if (ret) { |
| 805 | dev_err(dev, "failed to enable phy\n"); |
| 806 | return ret; |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | return 0; |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 810 | } |
| 811 | #endif |
| 812 | |
Keerthy | 9c049be | 2017-09-20 10:54:15 +0530 | [diff] [blame] | 813 | void dra7xx_pcie_shutdown(struct platform_device *pdev) |
| 814 | { |
| 815 | struct device *dev = &pdev->dev; |
| 816 | struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev); |
| 817 | int ret; |
| 818 | |
| 819 | dra7xx_pcie_stop_link(dra7xx->pci); |
| 820 | |
| 821 | ret = pm_runtime_put_sync(dev); |
| 822 | if (ret < 0) |
| 823 | dev_dbg(dev, "pm_runtime_put_sync failed\n"); |
| 824 | |
| 825 | pm_runtime_disable(dev); |
| 826 | dra7xx_pcie_disable_phy(dra7xx); |
| 827 | } |
| 828 | |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 829 | static const struct dev_pm_ops dra7xx_pcie_pm_ops = { |
Kishon Vijay Abraham I | 389c709 | 2015-07-31 17:55:12 +0530 | [diff] [blame] | 830 | SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume) |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 831 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq, |
| 832 | dra7xx_pcie_resume_noirq) |
| 833 | }; |
| 834 | |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 835 | static struct platform_driver dra7xx_pcie_driver = { |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 836 | .driver = { |
| 837 | .name = "dra7-pcie", |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 838 | .of_match_table = of_dra7xx_pcie_match, |
Paul Gortmaker | d29438d | 2016-08-24 16:57:47 -0400 | [diff] [blame] | 839 | .suppress_bind_attrs = true, |
Kishon Vijay Abraham I | e52eb44 | 2015-07-31 17:55:11 +0530 | [diff] [blame] | 840 | .pm = &dra7xx_pcie_pm_ops, |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 841 | }, |
Keerthy | 9c049be | 2017-09-20 10:54:15 +0530 | [diff] [blame] | 842 | .shutdown = dra7xx_pcie_shutdown, |
Kishon Vijay Abraham I | 47ff3de | 2014-07-22 15:23:45 -0600 | [diff] [blame] | 843 | }; |
Paul Gortmaker | d29438d | 2016-08-24 16:57:47 -0400 | [diff] [blame] | 844 | builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe); |