blob: a32d6dde7a579f6c21fe3daf79f10c566b48bb8d [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -06002/*
3 * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
4 *
5 * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Authors: Kishon Vijay Abraham I <kishon@ti.com>
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -06008 */
9
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +053010#include <linux/delay.h>
Kishon Vijay Abraham I7a4db652017-10-09 14:33:37 +053011#include <linux/device.h>
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060012#include <linux/err.h>
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/irqdomain.h>
16#include <linux/kernel.h>
Paul Gortmakerd29438d2016-08-24 16:57:47 -040017#include <linux/init.h>
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +053018#include <linux/of_device.h>
Kishon Vijay Abraham I78bdcad2015-07-28 19:09:09 +053019#include <linux/of_gpio.h>
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +053020#include <linux/of_pci.h>
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060021#include <linux/pci.h>
22#include <linux/phy/phy.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/resource.h>
26#include <linux/types.h>
Kishon Vijay Abraham If7a27572017-03-27 15:15:11 +053027#include <linux/mfd/syscon.h>
28#include <linux/regmap.h>
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060029
Shawn Lin6e0832f2018-05-31 09:12:37 +080030#include "../../pci.h"
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060031#include "pcie-designware.h"
32
33/* PCIe controller wrapper DRA7XX configuration registers */
34
35#define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024
36#define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028
37#define ERR_SYS BIT(0)
38#define ERR_FATAL BIT(1)
39#define ERR_NONFATAL BIT(2)
40#define ERR_COR BIT(3)
41#define ERR_AXI BIT(4)
42#define ERR_ECRC BIT(5)
43#define PME_TURN_OFF BIT(8)
44#define PME_TO_ACK BIT(9)
45#define PM_PME BIT(10)
46#define LINK_REQ_RST BIT(11)
47#define LINK_UP_EVT BIT(12)
48#define CFG_BME_EVT BIT(13)
49#define CFG_MSE_EVT BIT(14)
50#define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
51 ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
52 LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
53
54#define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034
55#define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038
56#define INTA BIT(0)
57#define INTB BIT(1)
58#define INTC BIT(2)
59#define INTD BIT(3)
60#define MSI BIT(4)
61#define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
62
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +053063#define PCIECTRL_TI_CONF_DEVICE_TYPE 0x0100
64#define DEVICE_TYPE_EP 0x0
65#define DEVICE_TYPE_LEG_EP 0x1
66#define DEVICE_TYPE_RC 0x4
67
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060068#define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104
69#define LTSSM_EN 0x1
70
71#define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C
72#define LINK_UP BIT(16)
Gabriele Paoloni883cc172015-10-29 19:56:51 -050073#define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060074
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +053075#define EXP_CAP_ID_OFFSET 0x70
76
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +053077#define PCIECTRL_TI_CONF_INTX_ASSERT 0x0124
78#define PCIECTRL_TI_CONF_INTX_DEASSERT 0x0128
79
80#define PCIECTRL_TI_CONF_MSI_XMT 0x012c
81#define MSI_REQ_GRANT BIT(0)
82#define MSI_VECTOR_SHIFT 7
83
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060084struct dra7xx_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053085 struct dw_pcie *pci;
Bjorn Helgaas8e5ec412016-10-06 13:33:06 -050086 void __iomem *base; /* DT ti_conf */
87 int phy_count; /* DT phy-names count */
88 struct phy **phy;
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +053089 int link_gen;
Kishon Vijay Abraham Iebe85a42017-01-11 17:36:54 +053090 struct irq_domain *irq_domain;
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +053091 enum dw_pcie_device_mode mode;
92};
93
94struct dra7xx_pcie_of_data {
95 enum dw_pcie_device_mode mode;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060096};
97
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053098#define to_dra7xx_pcie(x) dev_get_drvdata((x)->dev)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060099
100static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
101{
102 return readl(pcie->base + offset);
103}
104
105static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
106 u32 value)
107{
108 writel(value, pcie->base + offset);
109}
110
Niklas Casselb6900ae2017-12-20 00:29:36 +0100111static u64 dra7xx_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 pci_addr)
Kishon Vijay Abraham I2ed6cc72017-03-13 19:13:23 +0530112{
113 return pci_addr & DRA7XX_CPU_TO_BUS_ADDR;
114}
115
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530116static int dra7xx_pcie_link_up(struct dw_pcie *pci)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600117{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530118 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600119 u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
120
121 return !!(reg & LINK_UP);
122}
123
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530124static void dra7xx_pcie_stop_link(struct dw_pcie *pci)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600125{
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530126 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
127 u32 reg;
128
129 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
130 reg &= ~LTSSM_EN;
131 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
132}
133
134static int dra7xx_pcie_establish_link(struct dw_pcie *pci)
135{
136 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530137 struct device *dev = pci->dev;
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500138 u32 reg;
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530139 u32 exp_cap_off = EXP_CAP_ID_OFFSET;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600140
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530141 if (dw_pcie_link_up(pci)) {
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500142 dev_err(dev, "link is already up\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600143 return 0;
144 }
145
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530146 if (dra7xx->link_gen == 1) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530147 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530148 4, &reg);
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530149 if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
150 reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
151 reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530152 dw_pcie_write(pci->dbi_base + exp_cap_off +
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530153 PCI_EXP_LNKCAP, 4, reg);
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530154 }
155
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530156 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530157 2, &reg);
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530158 if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
159 reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
160 reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530161 dw_pcie_write(pci->dbi_base + exp_cap_off +
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +0530162 PCI_EXP_LNKCTL2, 2, reg);
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530163 }
164 }
165
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600166 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
167 reg |= LTSSM_EN;
168 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
169
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530170 return 0;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600171}
172
Kishon Vijay Abraham I5ffd90a2017-03-27 15:15:07 +0530173static void dra7xx_pcie_enable_msi_interrupts(struct dra7xx_pcie *dra7xx)
174{
175 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
Arvind Yadav40aa52c2017-06-19 15:21:36 +0530176 LEG_EP_INTERRUPTS | MSI);
Kishon Vijay Abraham I5ffd90a2017-03-27 15:15:07 +0530177
178 dra7xx_pcie_writel(dra7xx,
179 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
180 MSI | LEG_EP_INTERRUPTS);
181}
182
183static void dra7xx_pcie_enable_wrapper_interrupts(struct dra7xx_pcie *dra7xx)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600184{
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600185 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
Arvind Yadav40aa52c2017-06-19 15:21:36 +0530186 INTERRUPTS);
Kishon Vijay Abraham I5ffd90a2017-03-27 15:15:07 +0530187 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN,
188 INTERRUPTS);
189}
190
191static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
192{
193 dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
194 dra7xx_pcie_enable_msi_interrupts(dra7xx);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600195}
196
Bjorn Andersson4a301762017-07-15 23:39:45 -0700197static int dra7xx_pcie_host_init(struct pcie_port *pp)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600198{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530199 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
200 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
Bjorn Helgaas21baa1c2016-10-06 13:33:05 -0500201
Jisheng Zhang7e57fd12016-03-16 19:40:33 +0800202 dw_pcie_setup_rc(pp);
203
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530204 dra7xx_pcie_establish_link(pci);
205 dw_pcie_wait_for_link(pci);
Kishon Vijay Abraham Iebe85a42017-01-11 17:36:54 +0530206 dw_pcie_msi_init(pp);
Bjorn Helgaas21baa1c2016-10-06 13:33:05 -0500207 dra7xx_pcie_enable_interrupts(dra7xx);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700208
209 return 0;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600210}
211
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800212static const struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600213 .host_init = dra7xx_pcie_host_init,
214};
215
216static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
217 irq_hw_number_t hwirq)
218{
219 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
220 irq_set_chip_data(irq, domain->host_data);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600221
222 return 0;
223}
224
225static const struct irq_domain_ops intx_domain_ops = {
226 .map = dra7xx_pcie_intx_map,
Vignesh R524d59f2017-12-29 17:11:30 +0530227 .xlate = pci_irqd_intx_xlate,
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600228};
229
230static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
231{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530232 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
233 struct device *dev = pci->dev;
234 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600235 struct device_node *node = dev->of_node;
236 struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
237
238 if (!pcie_intc_node) {
239 dev_err(dev, "No PCIe Intc node found\n");
Christophe JAILLET991bfef2016-07-14 23:18:27 +0200240 return -ENODEV;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600241 }
242
Bjorn Helgaas61534d12017-08-15 16:28:27 -0500243 dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Kishon Vijay Abraham Iebe85a42017-01-11 17:36:54 +0530244 &intx_domain_ops, pp);
245 if (!dra7xx->irq_domain) {
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600246 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Christophe JAILLET991bfef2016-07-14 23:18:27 +0200247 return -ENODEV;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600248 }
249
250 return 0;
251}
252
253static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
254{
Bjorn Helgaas21baa1c2016-10-06 13:33:05 -0500255 struct dra7xx_pcie *dra7xx = arg;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530256 struct dw_pcie *pci = dra7xx->pci;
257 struct pcie_port *pp = &pci->pp;
Vignesh R09b2d202017-12-29 17:11:31 +0530258 unsigned long reg;
259 u32 virq, bit;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600260
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:
Vignesh R09b2d202017-12-29 17:11:31 +0530271 for_each_set_bit(bit, &reg, PCI_NUM_INTX) {
272 virq = irq_find_mapping(dra7xx->irq_domain, bit);
273 if (virq)
274 generic_handle_irq(virq);
275 }
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600276 break;
277 }
278
279 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
280
281 return IRQ_HANDLED;
282}
283
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600284static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
285{
286 struct dra7xx_pcie *dra7xx = arg;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530287 struct dw_pcie *pci = dra7xx->pci;
288 struct device *dev = pci->dev;
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530289 struct dw_pcie_ep *ep = &pci->ep;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600290 u32 reg;
291
292 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
293
294 if (reg & ERR_SYS)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500295 dev_dbg(dev, "System Error\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600296
297 if (reg & ERR_FATAL)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500298 dev_dbg(dev, "Fatal Error\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600299
300 if (reg & ERR_NONFATAL)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500301 dev_dbg(dev, "Non Fatal Error\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600302
303 if (reg & ERR_COR)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500304 dev_dbg(dev, "Correctable Error\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600305
306 if (reg & ERR_AXI)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500307 dev_dbg(dev, "AXI tag lookup fatal Error\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600308
309 if (reg & ERR_ECRC)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500310 dev_dbg(dev, "ECRC Error\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600311
312 if (reg & PME_TURN_OFF)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500313 dev_dbg(dev,
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600314 "Power Management Event Turn-Off message received\n");
315
316 if (reg & PME_TO_ACK)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500317 dev_dbg(dev,
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600318 "Power Management Turn-Off Ack message received\n");
319
320 if (reg & PM_PME)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500321 dev_dbg(dev, "PM Power Management Event message received\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600322
323 if (reg & LINK_REQ_RST)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500324 dev_dbg(dev, "Link Request Reset\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600325
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530326 if (reg & LINK_UP_EVT) {
327 if (dra7xx->mode == DW_PCIE_EP_TYPE)
328 dw_pcie_ep_linkup(ep);
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500329 dev_dbg(dev, "Link-up state change\n");
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530330 }
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600331
332 if (reg & CFG_BME_EVT)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500333 dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600334
335 if (reg & CFG_MSE_EVT)
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500336 dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600337
338 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
339
340 return IRQ_HANDLED;
341}
342
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530343static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
344{
345 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
346 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
Kishon Vijay Abraham I85aa1392017-08-18 20:28:03 +0530347 enum pci_barno bar;
348
349 for (bar = BAR_0; bar <= BAR_5; bar++)
350 dw_pcie_ep_reset_bar(pci, bar);
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530351
352 dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
353}
354
355static void dra7xx_pcie_raise_legacy_irq(struct dra7xx_pcie *dra7xx)
356{
357 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1);
358 mdelay(1);
359 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1);
360}
361
362static void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx,
363 u8 interrupt_num)
364{
365 u32 reg;
366
367 reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT;
368 reg |= MSI_REQ_GRANT;
369 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg);
370}
371
Bjorn Helgaas16093362018-02-01 11:36:07 -0600372static int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
Gustavo Pimenteld3c70a92018-07-19 10:32:13 +0200373 enum pci_epc_irq_type type, u16 interrupt_num)
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530374{
375 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
376 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
377
378 switch (type) {
379 case PCI_EPC_IRQ_LEGACY:
380 dra7xx_pcie_raise_legacy_irq(dra7xx);
381 break;
382 case PCI_EPC_IRQ_MSI:
383 dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num);
384 break;
385 default:
386 dev_err(pci->dev, "UNKNOWN IRQ type\n");
387 }
388
389 return 0;
390}
391
392static struct dw_pcie_ep_ops pcie_ep_ops = {
393 .ep_init = dra7xx_pcie_ep_init,
394 .raise_irq = dra7xx_pcie_raise_irq,
395};
396
397static int __init dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx,
398 struct platform_device *pdev)
399{
400 int ret;
401 struct dw_pcie_ep *ep;
402 struct resource *res;
403 struct device *dev = &pdev->dev;
404 struct dw_pcie *pci = dra7xx->pci;
405
406 ep = &pci->ep;
407 ep->ops = &pcie_ep_ops;
408
409 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics");
Gustavo Pimentel011cb232018-05-14 18:32:35 +0100410 pci->dbi_base = devm_ioremap_resource(dev, res);
411 if (IS_ERR(pci->dbi_base))
412 return PTR_ERR(pci->dbi_base);
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530413
414 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics2");
Gustavo Pimentel011cb232018-05-14 18:32:35 +0100415 pci->dbi_base2 = devm_ioremap_resource(dev, res);
416 if (IS_ERR(pci->dbi_base2))
417 return PTR_ERR(pci->dbi_base2);
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530418
419 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
420 if (!res)
421 return -EINVAL;
422
423 ep->phys_base = res->start;
424 ep->addr_size = resource_size(res);
425
426 ret = dw_pcie_ep_init(ep);
427 if (ret) {
428 dev_err(dev, "failed to initialize endpoint\n");
429 return ret;
430 }
431
432 return 0;
433}
434
Jingoo Hane73044a2014-11-06 14:37:39 +0900435static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
436 struct platform_device *pdev)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600437{
438 int ret;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530439 struct dw_pcie *pci = dra7xx->pci;
440 struct pcie_port *pp = &pci->pp;
441 struct device *dev = pci->dev;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600442 struct resource *res;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600443
444 pp->irq = platform_get_irq(pdev, 1);
445 if (pp->irq < 0) {
446 dev_err(dev, "missing IRQ resource\n");
Fabio Estevam2f3ec752017-08-31 14:52:02 -0300447 return pp->irq;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600448 }
449
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500450 ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200451 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaas21baa1c2016-10-06 13:33:05 -0500452 "dra7-pcie-msi", dra7xx);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600453 if (ret) {
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500454 dev_err(dev, "failed to request irq\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600455 return ret;
456 }
457
Kishon Vijay Abraham Iebe85a42017-01-11 17:36:54 +0530458 ret = dra7xx_pcie_init_irq_domain(pp);
459 if (ret < 0)
460 return ret;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600461
462 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
Gustavo Pimentel011cb232018-05-14 18:32:35 +0100463 pci->dbi_base = devm_ioremap_resource(dev, res);
464 if (IS_ERR(pci->dbi_base))
465 return PTR_ERR(pci->dbi_base);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600466
Niklas Cassel71890ea2017-12-20 00:29:29 +0100467 pp->ops = &dra7xx_pcie_host_ops;
468
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600469 ret = dw_pcie_host_init(pp);
470 if (ret) {
Bjorn Helgaasc7f81462016-10-06 13:33:06 -0500471 dev_err(dev, "failed to initialize host\n");
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600472 return ret;
473 }
474
475 return 0;
476}
477
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530478static const struct dw_pcie_ops dw_pcie_ops = {
Kishon Vijay Abraham I2ed6cc72017-03-13 19:13:23 +0530479 .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530480 .start_link = dra7xx_pcie_establish_link,
481 .stop_link = dra7xx_pcie_stop_link,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530482 .link_up = dra7xx_pcie_link_up,
483};
484
Kishon Vijay Abraham I1f6c4502017-01-11 17:36:55 +0530485static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
486{
487 int phy_count = dra7xx->phy_count;
488
489 while (phy_count--) {
490 phy_power_off(dra7xx->phy[phy_count]);
491 phy_exit(dra7xx->phy[phy_count]);
492 }
493}
494
495static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
496{
497 int phy_count = dra7xx->phy_count;
498 int ret;
499 int i;
500
501 for (i = 0; i < phy_count; i++) {
502 ret = phy_init(dra7xx->phy[i]);
503 if (ret < 0)
504 goto err_phy;
505
506 ret = phy_power_on(dra7xx->phy[i]);
507 if (ret < 0) {
508 phy_exit(dra7xx->phy[i]);
509 goto err_phy;
510 }
511 }
512
513 return 0;
514
515err_phy:
516 while (--i >= 0) {
517 phy_power_off(dra7xx->phy[i]);
518 phy_exit(dra7xx->phy[i]);
519 }
520
521 return ret;
522}
523
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530524static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
525 .mode = DW_PCIE_RC_TYPE,
526};
527
528static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
529 .mode = DW_PCIE_EP_TYPE,
530};
531
532static const struct of_device_id of_dra7xx_pcie_match[] = {
533 {
534 .compatible = "ti,dra7-pcie",
535 .data = &dra7xx_pcie_rc_of_data,
536 },
537 {
538 .compatible = "ti,dra7-pcie-ep",
539 .data = &dra7xx_pcie_ep_of_data,
540 },
541 {},
542};
543
Kishon Vijay Abraham If7a27572017-03-27 15:15:11 +0530544/*
Vignesh R726d75a2018-09-25 14:00:24 +0530545 * dra7xx_pcie_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
Kishon Vijay Abraham If7a27572017-03-27 15:15:11 +0530546 * @dra7xx: the dra7xx device where the workaround should be applied
547 *
548 * Access to the PCIe slave port that are not 32-bit aligned will result
549 * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
550 * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
551 * 0x3.
552 *
553 * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
554 */
Vignesh R726d75a2018-09-25 14:00:24 +0530555static int dra7xx_pcie_unaligned_memaccess(struct device *dev)
Kishon Vijay Abraham If7a27572017-03-27 15:15:11 +0530556{
557 int ret;
558 struct device_node *np = dev->of_node;
559 struct of_phandle_args args;
560 struct regmap *regmap;
561
562 regmap = syscon_regmap_lookup_by_phandle(np,
563 "ti,syscon-unaligned-access");
564 if (IS_ERR(regmap)) {
565 dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
566 return -EINVAL;
567 }
568
569 ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access",
570 2, 0, &args);
571 if (ret) {
572 dev_err(dev, "failed to parse ti,syscon-unaligned-access\n");
573 return ret;
574 }
575
576 ret = regmap_update_bits(regmap, args.args[0], args.args[1],
577 args.args[1]);
578 if (ret)
579 dev_err(dev, "failed to enable unaligned access\n");
580
581 of_node_put(args.np);
582
583 return ret;
584}
585
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600586static int __init dra7xx_pcie_probe(struct platform_device *pdev)
587{
588 u32 reg;
589 int ret;
590 int irq;
591 int i;
592 int phy_count;
593 struct phy **phy;
Kishon Vijay Abraham I7a4db652017-10-09 14:33:37 +0530594 struct device_link **link;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600595 void __iomem *base;
596 struct resource *res;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530597 struct dw_pcie *pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530598 struct dra7xx_pcie *dra7xx;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600599 struct device *dev = &pdev->dev;
600 struct device_node *np = dev->of_node;
601 char name[10];
Kishon Vijay Abraham I602d38b2017-01-11 17:36:52 +0530602 struct gpio_desc *reset;
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530603 const struct of_device_id *match;
604 const struct dra7xx_pcie_of_data *data;
605 enum dw_pcie_device_mode mode;
606
607 match = of_match_device(of_match_ptr(of_dra7xx_pcie_match), dev);
608 if (!match)
609 return -EINVAL;
610
611 data = (struct dra7xx_pcie_of_data *)match->data;
612 mode = (enum dw_pcie_device_mode)data->mode;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600613
614 dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
615 if (!dra7xx)
616 return -ENOMEM;
617
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530618 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
619 if (!pci)
620 return -ENOMEM;
621
622 pci->dev = dev;
623 pci->ops = &dw_pcie_ops;
624
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600625 irq = platform_get_irq(pdev, 0);
626 if (irq < 0) {
Gustavo A. R. Silvaa0d21ba2017-08-09 11:16:03 -0500627 dev_err(dev, "missing IRQ resource: %d\n", irq);
628 return irq;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600629 }
630
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600631 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
632 base = devm_ioremap_nocache(dev, res->start, resource_size(res));
633 if (!base)
634 return -ENOMEM;
635
636 phy_count = of_property_count_strings(np, "phy-names");
637 if (phy_count < 0) {
638 dev_err(dev, "unable to find the strings\n");
639 return phy_count;
640 }
641
Kees Cooka86854d2018-06-12 14:07:58 -0700642 phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600643 if (!phy)
644 return -ENOMEM;
645
Kees Cooka86854d2018-06-12 14:07:58 -0700646 link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
Kishon Vijay Abraham I7a4db652017-10-09 14:33:37 +0530647 if (!link)
648 return -ENOMEM;
649
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600650 for (i = 0; i < phy_count; i++) {
651 snprintf(name, sizeof(name), "pcie-phy%d", i);
652 phy[i] = devm_phy_get(dev, name);
653 if (IS_ERR(phy[i]))
654 return PTR_ERR(phy[i]);
Kishon Vijay Abraham I7a4db652017-10-09 14:33:37 +0530655
656 link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
657 if (!link[i]) {
658 ret = -EINVAL;
659 goto err_link;
660 }
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600661 }
662
663 dra7xx->base = base;
664 dra7xx->phy = phy;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530665 dra7xx->pci = pci;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600666 dra7xx->phy_count = phy_count;
667
Kishon Vijay Abraham I1f6c4502017-01-11 17:36:55 +0530668 ret = dra7xx_pcie_enable_phy(dra7xx);
669 if (ret) {
670 dev_err(dev, "failed to enable phy\n");
671 return ret;
672 }
673
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530674 platform_set_drvdata(pdev, dra7xx);
675
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600676 pm_runtime_enable(dev);
677 ret = pm_runtime_get_sync(dev);
Fabio Estevamd3f4caa2015-08-20 01:30:36 -0500678 if (ret < 0) {
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600679 dev_err(dev, "pm_runtime_get_sync failed\n");
Kishon Vijay Abraham I0e2bdb02015-07-31 17:55:10 +0530680 goto err_get_sync;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600681 }
682
Kishon Vijay Abraham I602d38b2017-01-11 17:36:52 +0530683 reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
684 if (IS_ERR(reset)) {
685 ret = PTR_ERR(reset);
686 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
Kishon Vijay Abraham I78bdcad2015-07-28 19:09:09 +0530687 goto err_gpio;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600688 }
689
690 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
691 reg &= ~LTSSM_EN;
692 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
693
Kishon Vijay Abraham Iab5fe4f2017-01-11 17:36:53 +0530694 dra7xx->link_gen = of_pci_get_max_link_speed(np);
695 if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2)
696 dra7xx->link_gen = 2;
697
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530698 switch (mode) {
699 case DW_PCIE_RC_TYPE:
Niklas Casself1aba0a2017-12-20 00:29:30 +0100700 if (!IS_ENABLED(CONFIG_PCI_DRA7XX_HOST)) {
701 ret = -ENODEV;
702 goto err_gpio;
703 }
704
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530705 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
706 DEVICE_TYPE_RC);
Vignesh R726d75a2018-09-25 14:00:24 +0530707
708 ret = dra7xx_pcie_unaligned_memaccess(dev);
709 if (ret)
710 dev_err(dev, "WA for Errata i870 not applied\n");
711
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530712 ret = dra7xx_add_pcie_port(dra7xx, pdev);
713 if (ret < 0)
714 goto err_gpio;
715 break;
716 case DW_PCIE_EP_TYPE:
Niklas Casself1aba0a2017-12-20 00:29:30 +0100717 if (!IS_ENABLED(CONFIG_PCI_DRA7XX_EP)) {
718 ret = -ENODEV;
719 goto err_gpio;
720 }
721
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530722 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
723 DEVICE_TYPE_EP);
Kishon Vijay Abraham If7a27572017-03-27 15:15:11 +0530724
Vignesh R726d75a2018-09-25 14:00:24 +0530725 ret = dra7xx_pcie_unaligned_memaccess(dev);
Kishon Vijay Abraham If7a27572017-03-27 15:15:11 +0530726 if (ret)
727 goto err_gpio;
728
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530729 ret = dra7xx_add_pcie_ep(dra7xx, pdev);
730 if (ret < 0)
731 goto err_gpio;
732 break;
733 default:
734 dev_err(dev, "INVALID device type %d\n", mode);
735 }
736 dra7xx->mode = mode;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600737
Keerthyd4c7d1a2017-03-13 19:13:28 +0530738 ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
739 IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
740 if (ret) {
741 dev_err(dev, "failed to request irq\n");
742 goto err_gpio;
743 }
744
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600745 return 0;
746
Kishon Vijay Abraham I78bdcad2015-07-28 19:09:09 +0530747err_gpio:
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600748 pm_runtime_put(dev);
Kishon Vijay Abraham I0e2bdb02015-07-31 17:55:10 +0530749
750err_get_sync:
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600751 pm_runtime_disable(dev);
Kishon Vijay Abraham I1f6c4502017-01-11 17:36:55 +0530752 dra7xx_pcie_disable_phy(dra7xx);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600753
Kishon Vijay Abraham I7a4db652017-10-09 14:33:37 +0530754err_link:
755 while (--i >= 0)
756 device_link_del(link[i]);
757
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600758 return ret;
759}
760
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530761#ifdef CONFIG_PM_SLEEP
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530762static int dra7xx_pcie_suspend(struct device *dev)
763{
764 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530765 struct dw_pcie *pci = dra7xx->pci;
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530766 u32 val;
767
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530768 if (dra7xx->mode != DW_PCIE_RC_TYPE)
769 return 0;
770
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530771 /* clear MSE */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530772 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530773 val &= ~PCI_COMMAND_MEMORY;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530774 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530775
776 return 0;
777}
778
779static int dra7xx_pcie_resume(struct device *dev)
780{
781 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530782 struct dw_pcie *pci = dra7xx->pci;
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530783 u32 val;
784
Kishon Vijay Abraham I608793e2017-03-27 15:15:08 +0530785 if (dra7xx->mode != DW_PCIE_RC_TYPE)
786 return 0;
787
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530788 /* set MSE */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530789 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530790 val |= PCI_COMMAND_MEMORY;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530791 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530792
793 return 0;
794}
795
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530796static int dra7xx_pcie_suspend_noirq(struct device *dev)
797{
798 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530799
Kishon Vijay Abraham I1f6c4502017-01-11 17:36:55 +0530800 dra7xx_pcie_disable_phy(dra7xx);
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530801
802 return 0;
803}
804
805static int dra7xx_pcie_resume_noirq(struct device *dev)
806{
807 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530808 int ret;
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530809
Kishon Vijay Abraham I1f6c4502017-01-11 17:36:55 +0530810 ret = dra7xx_pcie_enable_phy(dra7xx);
811 if (ret) {
812 dev_err(dev, "failed to enable phy\n");
813 return ret;
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530814 }
815
816 return 0;
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530817}
818#endif
819
Vignesh R4751fac2017-12-01 10:36:52 +0530820static void dra7xx_pcie_shutdown(struct platform_device *pdev)
Keerthy9c049be2017-09-20 10:54:15 +0530821{
822 struct device *dev = &pdev->dev;
823 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
824 int ret;
825
826 dra7xx_pcie_stop_link(dra7xx->pci);
827
828 ret = pm_runtime_put_sync(dev);
829 if (ret < 0)
830 dev_dbg(dev, "pm_runtime_put_sync failed\n");
831
832 pm_runtime_disable(dev);
833 dra7xx_pcie_disable_phy(dra7xx);
834}
835
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530836static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530837 SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530838 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
839 dra7xx_pcie_resume_noirq)
840};
841
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600842static struct platform_driver dra7xx_pcie_driver = {
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600843 .driver = {
844 .name = "dra7-pcie",
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600845 .of_match_table = of_dra7xx_pcie_match,
Paul Gortmakerd29438d2016-08-24 16:57:47 -0400846 .suppress_bind_attrs = true,
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530847 .pm = &dra7xx_pcie_pm_ops,
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600848 },
Keerthy9c049be2017-09-20 10:54:15 +0530849 .shutdown = dra7xx_pcie_shutdown,
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600850};
Paul Gortmakerd29438d2016-08-24 16:57:47 -0400851builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);