blob: fa7b893789047ed090aeffa50f753b6a0539a7e5 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Phil Edworthyc25da472014-05-12 11:57:48 +01002/*
3 * PCIe driver for Renesas R-Car SoCs
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01004 * Copyright (C) 2014-2020 Renesas Electronics Europe Ltd
Phil Edworthyc25da472014-05-12 11:57:48 +01005 *
6 * Based on:
7 * arch/sh/drivers/pci/pcie-sh7786.c
8 * arch/sh/drivers/pci/ops-sh7786.c
9 * Copyright (C) 2009 - 2011 Paul Mundt
10 *
Paul Gortmaker42d10712016-07-22 16:23:21 -050011 * Author: Phil Edworthy <phil.edworthy@renesas.com>
Phil Edworthyc25da472014-05-12 11:57:48 +010012 */
13
Marek Vasut0ee40822018-04-08 20:04:31 +020014#include <linux/bitops.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010015#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/interrupt.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010018#include <linux/irq.h>
19#include <linux/irqdomain.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010020#include <linux/kernel.h>
Paul Gortmaker42d10712016-07-22 16:23:21 -050021#include <linux/init.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010022#include <linux/msi.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010023#include <linux/of_address.h>
24#include <linux/of_irq.h>
25#include <linux/of_pci.h>
26#include <linux/of_platform.h>
27#include <linux/pci.h>
Sergei Shtylyov517ca932018-05-03 22:40:54 +030028#include <linux/phy/phy.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010029#include <linux/platform_device.h>
Phil Edworthyde1be9a2016-01-05 13:00:30 +000030#include <linux/pm_runtime.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010031#include <linux/slab.h>
32
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010033#include "pcie-rcar.h"
Phil Edworthyc25da472014-05-12 11:57:48 +010034
Phil Edworthy290c1fb2014-05-12 11:57:49 +010035struct rcar_msi {
36 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
37 struct irq_domain *domain;
Yijing Wangc2791b82014-11-11 17:45:45 -070038 struct msi_controller chip;
Phil Edworthy290c1fb2014-05-12 11:57:49 +010039 unsigned long pages;
40 struct mutex lock;
41 int irq1;
42 int irq2;
43};
44
Yijing Wangc2791b82014-11-11 17:45:45 -070045static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
Phil Edworthy290c1fb2014-05-12 11:57:49 +010046{
47 return container_of(chip, struct rcar_msi, chip);
48}
49
Phil Edworthyc25da472014-05-12 11:57:48 +010050/* Structure representing the PCIe interface */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010051struct rcar_pcie_host {
52 struct rcar_pcie pcie;
Phil Edworthyc25da472014-05-12 11:57:48 +010053 struct device *dev;
Sergei Shtylyov517ca932018-05-03 22:40:54 +030054 struct phy *phy;
Phil Edworthyc25da472014-05-12 11:57:48 +010055 void __iomem *base;
Phil Edworthyc25da472014-05-12 11:57:48 +010056 struct clk *bus_clk;
Phil Edworthy290c1fb2014-05-12 11:57:49 +010057 struct rcar_msi msi;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010058 int (*phy_init_fn)(struct rcar_pcie_host *host);
Phil Edworthyc25da472014-05-12 11:57:48 +010059};
60
Phil Edworthyc25da472014-05-12 11:57:48 +010061static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
62{
Marek Vasut29ffa6d2019-03-25 12:40:59 +010063 unsigned int shift = BITS_PER_BYTE * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +010064 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +010065
66 return val >> shift;
67}
68
69/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010070static int rcar_pcie_config_access(struct rcar_pcie_host *host,
Phil Edworthyc25da472014-05-12 11:57:48 +010071 unsigned char access_type, struct pci_bus *bus,
72 unsigned int devfn, int where, u32 *data)
73{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010074 struct rcar_pcie *pcie = &host->pcie;
Marek Vasutd8fa2662019-03-25 12:40:58 +010075 unsigned int dev, func, reg, index;
Phil Edworthyc25da472014-05-12 11:57:48 +010076
77 dev = PCI_SLOT(devfn);
78 func = PCI_FUNC(devfn);
79 reg = where & ~3;
80 index = reg / 4;
81
82 /*
83 * While each channel has its own memory-mapped extended config
84 * space, it's generally only accessible when in endpoint mode.
85 * When in root complex mode, the controller is unable to target
86 * itself with either type 0 or type 1 accesses, and indeed, any
87 * controller initiated target transfer to its own config space
88 * result in a completer abort.
89 *
90 * Each channel effectively only supports a single device, but as
91 * the same channel <-> device access works for any PCI_SLOT()
92 * value, we cheat a bit here and bind the controller's config
93 * space to devfn 0 in order to enable self-enumeration. In this
94 * case the regular ECAR/ECDR path is sidelined and the mangled
95 * config access itself is initiated as an internal bus transaction.
96 */
97 if (pci_is_root_bus(bus)) {
98 if (dev != 0)
99 return PCIBIOS_DEVICE_NOT_FOUND;
100
Rob Herring6176a5f2020-07-21 20:25:05 -0600101 if (access_type == RCAR_PCI_ACCESS_READ)
Phil Edworthyb77188492014-06-30 08:54:23 +0100102 *data = rcar_pci_read_reg(pcie, PCICONF(index));
Rob Herring6176a5f2020-07-21 20:25:05 -0600103 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100104 rcar_pci_write_reg(pcie, *data, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100105
106 return PCIBIOS_SUCCESSFUL;
107 }
108
Phil Edworthyc25da472014-05-12 11:57:48 +0100109 /* Clear errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100110 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100111
112 /* Set the PIO address */
Phil Edworthyb77188492014-06-30 08:54:23 +0100113 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
114 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100115
116 /* Enable the configuration access */
Rob Herring6176a5f2020-07-21 20:25:05 -0600117 if (pci_is_root_bus(bus->parent))
Phil Edworthyb77188492014-06-30 08:54:23 +0100118 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100119 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100120 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100121
122 /* Check for errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100123 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
Phil Edworthyc25da472014-05-12 11:57:48 +0100124 return PCIBIOS_DEVICE_NOT_FOUND;
125
126 /* Check for master and target aborts */
127 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
128 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
129 return PCIBIOS_DEVICE_NOT_FOUND;
130
Phil Edworthyb77188492014-06-30 08:54:23 +0100131 if (access_type == RCAR_PCI_ACCESS_READ)
132 *data = rcar_pci_read_reg(pcie, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100133 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100134 rcar_pci_write_reg(pcie, *data, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100135
136 /* Disable the configuration access */
Phil Edworthyb77188492014-06-30 08:54:23 +0100137 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100138
139 return PCIBIOS_SUCCESSFUL;
140}
141
142static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
143 int where, int size, u32 *val)
144{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100145 struct rcar_pcie_host *host = bus->sysdata;
Phil Edworthyc25da472014-05-12 11:57:48 +0100146 int ret;
147
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100148 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100149 bus, devfn, where, val);
150 if (ret != PCIBIOS_SUCCESSFUL) {
151 *val = 0xffffffff;
152 return ret;
153 }
154
155 if (size == 1)
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100156 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
Phil Edworthyc25da472014-05-12 11:57:48 +0100157 else if (size == 2)
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100158 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
Phil Edworthyc25da472014-05-12 11:57:48 +0100159
Marek Vasut42a58f72019-03-25 12:41:00 +0100160 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
161 bus->number, devfn, where, size, *val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100162
163 return ret;
164}
165
166/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
167static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
168 int where, int size, u32 val)
169{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100170 struct rcar_pcie_host *host = bus->sysdata;
Marek Vasutd8fa2662019-03-25 12:40:58 +0100171 unsigned int shift;
Phil Edworthyc25da472014-05-12 11:57:48 +0100172 u32 data;
Marek Vasutd8fa2662019-03-25 12:40:58 +0100173 int ret;
Phil Edworthyc25da472014-05-12 11:57:48 +0100174
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100175 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100176 bus, devfn, where, &data);
177 if (ret != PCIBIOS_SUCCESSFUL)
178 return ret;
179
Marek Vasut42a58f72019-03-25 12:41:00 +0100180 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
181 bus->number, devfn, where, size, val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100182
183 if (size == 1) {
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100184 shift = BITS_PER_BYTE * (where & 3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100185 data &= ~(0xff << shift);
186 data |= ((val & 0xff) << shift);
187 } else if (size == 2) {
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100188 shift = BITS_PER_BYTE * (where & 2);
Phil Edworthyc25da472014-05-12 11:57:48 +0100189 data &= ~(0xffff << shift);
190 data |= ((val & 0xffff) << shift);
191 } else
192 data = val;
193
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100194 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100195 bus, devfn, where, &data);
196
197 return ret;
198}
199
200static struct pci_ops rcar_pcie_ops = {
201 .read = rcar_pcie_read_conf,
202 .write = rcar_pcie_write_conf,
203};
204
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300205static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
206{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500207 struct device *dev = pcie->dev;
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300208 unsigned int timeout = 1000;
209 u32 macsr;
210
211 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
212 return;
213
214 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500215 dev_err(dev, "Speed change already in progress\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300216 return;
217 }
218
219 macsr = rcar_pci_read_reg(pcie, MACSR);
220 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
221 goto done;
222
223 /* Set target link speed to 5.0 GT/s */
224 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
225 PCI_EXP_LNKSTA_CLS_5_0GB);
226
227 /* Set speed change reason as intentional factor */
228 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
229
230 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
231 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
232 rcar_pci_write_reg(pcie, macsr, MACSR);
233
234 /* Start link speed change */
235 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
236
237 while (timeout--) {
238 macsr = rcar_pci_read_reg(pcie, MACSR);
239 if (macsr & SPCHGFIN) {
240 /* Clear the interrupt bits */
241 rcar_pci_write_reg(pcie, macsr, MACSR);
242
243 if (macsr & SPCHGFAIL)
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500244 dev_err(dev, "Speed change failed\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300245
246 goto done;
247 }
248
249 msleep(1);
Fengguang Wud1708672018-03-07 09:42:39 -0600250 }
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300251
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500252 dev_err(dev, "Speed change timed out\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300253
254done:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500255 dev_info(dev, "Current link speed is %s GT/s\n",
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300256 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
257}
258
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100259static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
Kazufumi Ikedace351632020-03-14 20:12:32 +0100260{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100261 struct rcar_pcie *pcie = &host->pcie;
Rob Herringb411b2e2020-07-21 20:25:10 -0600262 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100263 struct resource_entry *win;
264 LIST_HEAD(res);
265 int i = 0;
266
267 /* Try setting 5 GT/s link speed */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100268 rcar_pcie_force_speedup(pcie);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100269
270 /* Setup PCI resources */
Rob Herringb411b2e2020-07-21 20:25:10 -0600271 resource_list_for_each_entry(win, &bridge->windows) {
Kazufumi Ikedace351632020-03-14 20:12:32 +0100272 struct resource *res = win->res;
273
274 if (!res->flags)
275 continue;
276
277 switch (resource_type(res)) {
278 case IORESOURCE_IO:
279 case IORESOURCE_MEM:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100280 rcar_pcie_set_outbound(pcie, i, win);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100281 i++;
282 break;
283 }
284 }
285}
286
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100287static int rcar_pcie_enable(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100288{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100289 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100290
Rob Herringb411b2e2020-07-21 20:25:10 -0600291 rcar_pcie_hw_enable(host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100292
Bjorn Helgaas71538842017-11-30 11:21:57 -0600293 pci_add_flags(PCI_REASSIGN_ALL_BUS);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100294
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100295 bridge->sysdata = host;
Lorenzo Pieralisi90634e82017-06-28 15:13:57 -0500296 bridge->ops = &rcar_pcie_ops;
Lorenzo Pieralisi29db9912017-06-28 15:14:06 -0500297 bridge->map_irq = of_irq_parse_and_map_pci;
298 bridge->swizzle_irq = pci_common_swizzle;
Phil Edworthy79953dd2015-10-02 11:25:05 +0100299 if (IS_ENABLED(CONFIG_PCI_MSI))
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100300 bridge->msi = &host->msi.chip;
Phil Edworthy79953dd2015-10-02 11:25:05 +0100301
Rob Herring56d29232020-05-22 17:48:29 -0600302 return pci_host_probe(bridge);
Phil Edworthyc25da472014-05-12 11:57:48 +0100303}
304
305static int phy_wait_for_ack(struct rcar_pcie *pcie)
306{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500307 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100308 unsigned int timeout = 100;
309
310 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100311 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
Phil Edworthyc25da472014-05-12 11:57:48 +0100312 return 0;
313
314 udelay(100);
315 }
316
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500317 dev_err(dev, "Access to PCIe phy timed out\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100318
319 return -ETIMEDOUT;
320}
321
322static void phy_write_reg(struct rcar_pcie *pcie,
Marek Vasutd8fa2662019-03-25 12:40:58 +0100323 unsigned int rate, u32 addr,
324 unsigned int lane, u32 data)
Phil Edworthyc25da472014-05-12 11:57:48 +0100325{
Marek Vasutd8fa2662019-03-25 12:40:58 +0100326 u32 phyaddr;
Phil Edworthyc25da472014-05-12 11:57:48 +0100327
328 phyaddr = WRITE_CMD |
329 ((rate & 1) << RATE_POS) |
330 ((lane & 0xf) << LANE_POS) |
331 ((addr & 0xff) << ADR_POS);
332
333 /* Set write data */
Phil Edworthyb77188492014-06-30 08:54:23 +0100334 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
335 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100336
337 /* Ignore errors as they will be dealt with if the data link is down */
338 phy_wait_for_ack(pcie);
339
340 /* Clear command */
Phil Edworthyb77188492014-06-30 08:54:23 +0100341 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
342 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100343
344 /* Ignore errors as they will be dealt with if the data link is down */
345 phy_wait_for_ack(pcie);
346}
347
Phil Edworthyc25da472014-05-12 11:57:48 +0100348static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
349{
350 int err;
351
352 /* Begin initialization */
Phil Edworthyb77188492014-06-30 08:54:23 +0100353 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100354
355 /* Set mode */
Phil Edworthyb77188492014-06-30 08:54:23 +0100356 rcar_pci_write_reg(pcie, 1, PCIEMSR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100357
Sergei Shtylyov3ad1d322018-05-03 22:36:37 +0300358 err = rcar_pcie_wait_for_phyrdy(pcie);
359 if (err)
360 return err;
361
Phil Edworthyc25da472014-05-12 11:57:48 +0100362 /*
363 * Initial header for port config space is type 1, set the device
364 * class to match. Hardware takes care of propagating the IDSETR
365 * settings, so there is no need to bother with a quirk.
366 */
Phil Edworthyb77188492014-06-30 08:54:23 +0100367 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
Phil Edworthyc25da472014-05-12 11:57:48 +0100368
369 /*
370 * Setup Secondary Bus Number & Subordinate Bus Number, even though
371 * they aren't used, to avoid bridge being detected as broken.
372 */
373 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
374 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
375
376 /* Initialize default capabilities. */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100377 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
Phil Edworthyc25da472014-05-12 11:57:48 +0100378 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
379 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
380 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
381 PCI_HEADER_TYPE_BRIDGE);
382
383 /* Enable data link layer active state reporting */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100384 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
385 PCI_EXP_LNKCAP_DLLLARC);
Phil Edworthyc25da472014-05-12 11:57:48 +0100386
387 /* Write out the physical slot number = 0 */
388 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
389
390 /* Set the completion timer timeout to the maximum 50ms. */
Phil Edworthyb77188492014-06-30 08:54:23 +0100391 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
Phil Edworthyc25da472014-05-12 11:57:48 +0100392
393 /* Terminate list of capabilities (Next Capability Offset=0) */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100394 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
Phil Edworthyc25da472014-05-12 11:57:48 +0100395
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100396 /* Enable MSI */
397 if (IS_ENABLED(CONFIG_PCI_MSI))
Nobuhiro Iwamatsu1fc6aa92015-02-02 14:09:39 +0900398 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100399
Yoshihiro Shimoda7c7e53e2019-11-05 19:51:29 +0900400 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
401
Phil Edworthyc25da472014-05-12 11:57:48 +0100402 /* Finish initialization - establish a PCI Express link */
Phil Edworthyb77188492014-06-30 08:54:23 +0100403 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100404
405 /* This will timeout if we don't have a link. */
406 err = rcar_pcie_wait_for_dl(pcie);
407 if (err)
408 return err;
409
410 /* Enable INTx interrupts */
411 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
412
Phil Edworthyc25da472014-05-12 11:57:48 +0100413 wmb();
414
415 return 0;
416}
417
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100418static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100419{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100420 struct rcar_pcie *pcie = &host->pcie;
421
Phil Edworthyc25da472014-05-12 11:57:48 +0100422 /* Initialize the phy */
423 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
424 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
425 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
426 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
427 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
428 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
429 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
430 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
431 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
432 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
433 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
434 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
435
436 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
437 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
438 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
439
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300440 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100441}
442
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100443static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
Phil Edworthy581d9432016-01-05 13:00:31 +0000444{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100445 struct rcar_pcie *pcie = &host->pcie;
446
Phil Edworthy581d9432016-01-05 13:00:31 +0000447 /*
448 * These settings come from the R-Car Series, 2nd Generation User's
449 * Manual, section 50.3.1 (2) Initialization of the physical layer.
450 */
451 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
452 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
453 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
454 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
455
456 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
457 /* The following value is for DC connection, no termination resistor */
458 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
459 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
460 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
461
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300462 return 0;
Phil Edworthy581d9432016-01-05 13:00:31 +0000463}
464
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100465static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300466{
467 int err;
468
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100469 err = phy_init(host->phy);
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300470 if (err)
471 return err;
472
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100473 err = phy_power_on(host->phy);
Marek Vasut3c5777c2018-06-29 13:48:15 -0500474 if (err)
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100475 phy_exit(host->phy);
Marek Vasut3c5777c2018-06-29 13:48:15 -0500476
477 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100478}
479
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100480static int rcar_msi_alloc(struct rcar_msi *chip)
481{
482 int msi;
483
484 mutex_lock(&chip->lock);
485
486 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
487 if (msi < INT_PCI_MSI_NR)
488 set_bit(msi, chip->used);
489 else
490 msi = -ENOSPC;
491
492 mutex_unlock(&chip->lock);
493
494 return msi;
495}
496
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300497static int rcar_msi_alloc_region(struct rcar_msi *chip, int no_irqs)
498{
499 int msi;
500
501 mutex_lock(&chip->lock);
502 msi = bitmap_find_free_region(chip->used, INT_PCI_MSI_NR,
503 order_base_2(no_irqs));
504 mutex_unlock(&chip->lock);
505
506 return msi;
507}
508
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100509static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
510{
511 mutex_lock(&chip->lock);
512 clear_bit(irq, chip->used);
513 mutex_unlock(&chip->lock);
514}
515
516static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
517{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100518 struct rcar_pcie_host *host = data;
519 struct rcar_pcie *pcie = &host->pcie;
520 struct rcar_msi *msi = &host->msi;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500521 struct device *dev = pcie->dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100522 unsigned long reg;
523
Phil Edworthyb77188492014-06-30 08:54:23 +0100524 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100525
526 /* MSI & INTx share an interrupt - we only handle MSI here */
527 if (!reg)
528 return IRQ_NONE;
529
530 while (reg) {
531 unsigned int index = find_first_bit(&reg, 32);
Wolfram Sanga27beb52019-03-17 10:34:45 +0100532 unsigned int msi_irq;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100533
534 /* clear the interrupt */
Phil Edworthyb77188492014-06-30 08:54:23 +0100535 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100536
Wolfram Sanga27beb52019-03-17 10:34:45 +0100537 msi_irq = irq_find_mapping(msi->domain, index);
538 if (msi_irq) {
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100539 if (test_bit(index, msi->used))
Wolfram Sanga27beb52019-03-17 10:34:45 +0100540 generic_handle_irq(msi_irq);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100541 else
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500542 dev_info(dev, "unhandled MSI\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100543 } else {
544 /* Unknown MSI, just clear it */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500545 dev_dbg(dev, "unexpected MSI\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100546 }
547
548 /* see if there's any more pending in this vector */
Phil Edworthyb77188492014-06-30 08:54:23 +0100549 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100550 }
551
552 return IRQ_HANDLED;
553}
554
Yijing Wangc2791b82014-11-11 17:45:45 -0700555static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100556 struct msi_desc *desc)
557{
558 struct rcar_msi *msi = to_rcar_msi(chip);
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100559 struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
560 msi.chip);
561 struct rcar_pcie *pcie = &host->pcie;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100562 struct msi_msg msg;
563 unsigned int irq;
564 int hwirq;
565
566 hwirq = rcar_msi_alloc(msi);
567 if (hwirq < 0)
568 return hwirq;
569
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300570 irq = irq_find_mapping(msi->domain, hwirq);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100571 if (!irq) {
572 rcar_msi_free(msi, hwirq);
573 return -EINVAL;
574 }
575
576 irq_set_msi_desc(irq, desc);
577
Phil Edworthyb77188492014-06-30 08:54:23 +0100578 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
579 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100580 msg.data = hwirq;
581
Jiang Liu83a18912014-11-09 23:10:34 +0800582 pci_write_msi_msg(irq, &msg);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100583
584 return 0;
585}
586
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300587static int rcar_msi_setup_irqs(struct msi_controller *chip,
588 struct pci_dev *pdev, int nvec, int type)
589{
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300590 struct rcar_msi *msi = to_rcar_msi(chip);
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100591 struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
592 msi.chip);
593 struct rcar_pcie *pcie = &host->pcie;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300594 struct msi_desc *desc;
595 struct msi_msg msg;
596 unsigned int irq;
597 int hwirq;
598 int i;
599
600 /* MSI-X interrupts are not supported */
601 if (type == PCI_CAP_ID_MSIX)
602 return -EINVAL;
603
604 WARN_ON(!list_is_singular(&pdev->dev.msi_list));
605 desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
606
607 hwirq = rcar_msi_alloc_region(msi, nvec);
608 if (hwirq < 0)
609 return -ENOSPC;
610
611 irq = irq_find_mapping(msi->domain, hwirq);
612 if (!irq)
613 return -ENOSPC;
614
615 for (i = 0; i < nvec; i++) {
616 /*
617 * irq_create_mapping() called from rcar_pcie_probe() pre-
618 * allocates descs, so there is no need to allocate descs here.
619 * We can therefore assume that if irq_find_mapping() above
620 * returns non-zero, then the descs are also successfully
621 * allocated.
622 */
623 if (irq_set_msi_desc_off(irq, i, desc)) {
624 /* TODO: clear */
625 return -EINVAL;
626 }
627 }
628
629 desc->nvec_used = nvec;
630 desc->msi_attrib.multiple = order_base_2(nvec);
631
632 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
633 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
634 msg.data = hwirq;
635
636 pci_write_msi_msg(irq, &msg);
637
638 return 0;
639}
640
Yijing Wangc2791b82014-11-11 17:45:45 -0700641static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100642{
643 struct rcar_msi *msi = to_rcar_msi(chip);
644 struct irq_data *d = irq_get_irq_data(irq);
645
646 rcar_msi_free(msi, d->hwirq);
647}
648
649static struct irq_chip rcar_msi_irq_chip = {
650 .name = "R-Car PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100651 .irq_enable = pci_msi_unmask_irq,
652 .irq_disable = pci_msi_mask_irq,
653 .irq_mask = pci_msi_mask_irq,
654 .irq_unmask = pci_msi_unmask_irq,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100655};
656
657static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
658 irq_hw_number_t hwirq)
659{
660 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
661 irq_set_chip_data(irq, domain->host_data);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100662
663 return 0;
664}
665
666static const struct irq_domain_ops msi_domain_ops = {
667 .map = rcar_msi_map,
668};
669
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100670static void rcar_pcie_unmap_msi(struct rcar_pcie_host *host)
Marek Vasut0bbf6b92018-05-24 16:36:23 +0200671{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100672 struct rcar_msi *msi = &host->msi;
Marek Vasut0bbf6b92018-05-24 16:36:23 +0200673 int i, irq;
674
675 for (i = 0; i < INT_PCI_MSI_NR; i++) {
676 irq = irq_find_mapping(msi->domain, i);
677 if (irq > 0)
678 irq_dispose_mapping(irq);
679 }
680
681 irq_domain_remove(msi->domain);
682}
683
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100684static void rcar_pcie_hw_enable_msi(struct rcar_pcie_host *host)
Kazufumi Ikedace351632020-03-14 20:12:32 +0100685{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100686 struct rcar_pcie *pcie = &host->pcie;
687 struct rcar_msi *msi = &host->msi;
Kazufumi Ikedace351632020-03-14 20:12:32 +0100688 unsigned long base;
689
690 /* setup MSI data target */
691 base = virt_to_phys((void *)msi->pages);
692
693 rcar_pci_write_reg(pcie, lower_32_bits(base) | MSIFE, PCIEMSIALR);
694 rcar_pci_write_reg(pcie, upper_32_bits(base), PCIEMSIAUR);
695
696 /* enable all MSI interrupts */
697 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
698}
699
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100700static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100701{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100702 struct rcar_pcie *pcie = &host->pcie;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500703 struct device *dev = pcie->dev;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100704 struct rcar_msi *msi = &host->msi;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300705 int err, i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100706
707 mutex_init(&msi->lock);
708
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500709 msi->chip.dev = dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100710 msi->chip.setup_irq = rcar_msi_setup_irq;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300711 msi->chip.setup_irqs = rcar_msi_setup_irqs;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100712 msi->chip.teardown_irq = rcar_msi_teardown_irq;
713
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500714 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100715 &msi_domain_ops, &msi->chip);
716 if (!msi->domain) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500717 dev_err(dev, "failed to create IRQ domain\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100718 return -ENOMEM;
719 }
720
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300721 for (i = 0; i < INT_PCI_MSI_NR; i++)
722 irq_create_mapping(msi->domain, i);
723
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100724 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500725 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200726 IRQF_SHARED | IRQF_NO_THREAD,
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100727 rcar_msi_irq_chip.name, host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100728 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500729 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100730 goto err;
731 }
732
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500733 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200734 IRQF_SHARED | IRQF_NO_THREAD,
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100735 rcar_msi_irq_chip.name, host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100736 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500737 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100738 goto err;
739 }
740
741 /* setup MSI data target */
742 msi->pages = __get_free_pages(GFP_KERNEL, 0);
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100743 rcar_pcie_hw_enable_msi(host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100744
745 return 0;
746
747err:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100748 rcar_pcie_unmap_msi(host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100749 return err;
750}
751
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100752static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
Marek Vasut1aea58b2018-05-24 16:36:21 +0200753{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100754 struct rcar_pcie *pcie = &host->pcie;
755 struct rcar_msi *msi = &host->msi;
Marek Vasut1aea58b2018-05-24 16:36:21 +0200756
757 /* Disable all MSI interrupts */
758 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
759
760 /* Disable address decoding of the MSI interrupt, MSIFE */
761 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
762
763 free_pages(msi->pages, 0);
764
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100765 rcar_pcie_unmap_msi(host);
Marek Vasut1aea58b2018-05-24 16:36:21 +0200766}
767
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100768static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100769{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100770 struct rcar_pcie *pcie = &host->pcie;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500771 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100772 struct resource res;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100773 int err, i;
Phil Edworthyc25da472014-05-12 11:57:48 +0100774
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100775 host->phy = devm_phy_optional_get(dev, "pcie");
776 if (IS_ERR(host->phy))
777 return PTR_ERR(host->phy);
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300778
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500779 err = of_address_to_resource(dev->of_node, 0, &res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100780 if (err)
781 return err;
782
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500783 pcie->base = devm_ioremap_resource(dev, &res);
Bjorn Helgaas51afa3c2016-08-22 14:16:38 -0500784 if (IS_ERR(pcie->base))
785 return PTR_ERR(pcie->base);
786
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100787 host->bus_clk = devm_clk_get(dev, "pcie_bus");
788 if (IS_ERR(host->bus_clk)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500789 dev_err(dev, "cannot get pcie bus clock\n");
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100790 return PTR_ERR(host->bus_clk);
Phil Edworthyc25da472014-05-12 11:57:48 +0100791 }
Phil Edworthyc25da472014-05-12 11:57:48 +0100792
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500793 i = irq_of_parse_and_map(dev->of_node, 0);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800794 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500795 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100796 err = -ENOENT;
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200797 goto err_irq1;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100798 }
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100799 host->msi.irq1 = i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100800
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500801 i = irq_of_parse_and_map(dev->of_node, 1);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800802 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500803 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100804 err = -ENOENT;
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200805 goto err_irq2;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100806 }
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100807 host->msi.irq2 = i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100808
Phil Edworthyc25da472014-05-12 11:57:48 +0100809 return 0;
810
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200811err_irq2:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100812 irq_dispose_mapping(host->msi.irq1);
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200813err_irq1:
Phil Edworthyc25da472014-05-12 11:57:48 +0100814 return err;
815}
816
817static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
Rob Herring085f7932019-10-28 11:32:55 -0500818 struct resource_entry *entry,
Phil Edworthyc25da472014-05-12 11:57:48 +0100819 int *index)
820{
Rob Herring085f7932019-10-28 11:32:55 -0500821 u64 restype = entry->res->flags;
822 u64 cpu_addr = entry->res->start;
823 u64 cpu_end = entry->res->end;
824 u64 pci_addr = entry->res->start - entry->offset;
Phil Edworthyc25da472014-05-12 11:57:48 +0100825 u32 flags = LAM_64BIT | LAR_ENABLE;
826 u64 mask;
Rob Herring085f7932019-10-28 11:32:55 -0500827 u64 size = resource_size(entry->res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100828 int idx = *index;
829
830 if (restype & IORESOURCE_PREFETCH)
831 flags |= LAM_PREFETCH;
832
Phil Edworthyc25da472014-05-12 11:57:48 +0100833 while (cpu_addr < cpu_end) {
Marek Vasut85bff4c2019-10-26 20:26:58 +0200834 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
835 dev_err(pcie->dev, "Failed to map inbound regions!\n");
836 return -EINVAL;
837 }
Phil Edworthyc25da472014-05-12 11:57:48 +0100838 /*
Marek Vasut767c7842019-10-26 20:26:59 +0200839 * If the size of the range is larger than the alignment of
840 * the start address, we have to use multiple entries to
841 * perform the mapping.
842 */
843 if (cpu_addr > 0) {
844 unsigned long nr_zeros = __ffs64(cpu_addr);
845 u64 alignment = 1ULL << nr_zeros;
846
Bjorn Helgaas7bd4c4a2019-11-28 08:54:53 -0600847 size = min(size, alignment);
Marek Vasut767c7842019-10-26 20:26:59 +0200848 }
849 /* Hardware supports max 4GiB inbound region */
850 size = min(size, 1ULL << 32);
851
852 mask = roundup_pow_of_two(size) - 1;
853 mask &= ~0xf;
854
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100855 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
856 lower_32_bits(mask) | flags, idx, true);
Phil Edworthyc25da472014-05-12 11:57:48 +0100857
858 pci_addr += size;
859 cpu_addr += size;
860 idx += 2;
Phil Edworthyc25da472014-05-12 11:57:48 +0100861 }
862 *index = idx;
863
864 return 0;
865}
866
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100867static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100868{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100869 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
Rob Herring085f7932019-10-28 11:32:55 -0500870 struct resource_entry *entry;
871 int index = 0, err = 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100872
Rob Herring085f7932019-10-28 11:32:55 -0500873 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100874 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
Phil Edworthyc25da472014-05-12 11:57:48 +0100875 if (err)
Rob Herring085f7932019-10-28 11:32:55 -0500876 break;
Phil Edworthyc25da472014-05-12 11:57:48 +0100877 }
878
Rob Herring085f7932019-10-28 11:32:55 -0500879 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100880}
881
882static const struct of_device_id rcar_pcie_of_match[] = {
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300883 { .compatible = "renesas,pcie-r8a7779",
884 .data = rcar_pcie_phy_init_h1 },
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +0300885 { .compatible = "renesas,pcie-r8a7790",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300886 .data = rcar_pcie_phy_init_gen2 },
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +0300887 { .compatible = "renesas,pcie-r8a7791",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300888 .data = rcar_pcie_phy_init_gen2 },
Simon Hormand83a3282016-12-06 16:51:30 +0100889 { .compatible = "renesas,pcie-rcar-gen2",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300890 .data = rcar_pcie_phy_init_gen2 },
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300891 { .compatible = "renesas,pcie-r8a7795",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300892 .data = rcar_pcie_phy_init_gen3 },
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300893 { .compatible = "renesas,pcie-rcar-gen3",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300894 .data = rcar_pcie_phy_init_gen3 },
Phil Edworthyc25da472014-05-12 11:57:48 +0100895 {},
896};
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000897
Phil Edworthyc25da472014-05-12 11:57:48 +0100898static int rcar_pcie_probe(struct platform_device *pdev)
899{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500900 struct device *dev = &pdev->dev;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100901 struct rcar_pcie_host *host;
Phil Edworthyc25da472014-05-12 11:57:48 +0100902 struct rcar_pcie *pcie;
Marek Vasutd8fa2662019-03-25 12:40:58 +0100903 u32 data;
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000904 int err;
Lorenzo Pieralisi90634e82017-06-28 15:13:57 -0500905 struct pci_host_bridge *bridge;
Phil Edworthyc25da472014-05-12 11:57:48 +0100906
Rob Herring61f11f82020-07-21 20:25:09 -0600907 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
Lorenzo Pieralisi90634e82017-06-28 15:13:57 -0500908 if (!bridge)
Phil Edworthyc25da472014-05-12 11:57:48 +0100909 return -ENOMEM;
910
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100911 host = pci_host_bridge_priv(bridge);
912 pcie = &host->pcie;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500913 pcie->dev = dev;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100914 platform_set_drvdata(pdev, host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100915
Rob Herringb411b2e2020-07-21 20:25:10 -0600916 err = pci_parse_request_of_pci_ranges(dev, &bridge->windows,
Rob Herring331f6342019-10-30 17:30:57 -0500917 &bridge->dma_ranges, NULL);
Geert Uytterhoeven83c75dd2017-12-07 11:15:20 +0100918 if (err)
Rob Herring61f11f82020-07-21 20:25:09 -0600919 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100920
Dien Pham0df61502018-04-08 15:09:25 +0200921 pm_runtime_enable(pcie->dev);
922 err = pm_runtime_get_sync(pcie->dev);
923 if (err < 0) {
924 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
925 goto err_pm_disable;
926 }
927
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100928 err = rcar_pcie_get_resources(host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100929 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500930 dev_err(dev, "failed to request resources: %d\n", err);
Dien Pham0df61502018-04-08 15:09:25 +0200931 goto err_pm_put;
Phil Edworthyc25da472014-05-12 11:57:48 +0100932 }
933
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100934 err = clk_prepare_enable(host->bus_clk);
Marek Vasut80b84712018-05-24 16:36:19 +0200935 if (err) {
936 dev_err(dev, "failed to enable bus clock: %d\n", err);
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200937 goto err_unmap_msi_irqs;
Phil Edworthyc25da472014-05-12 11:57:48 +0100938 }
939
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100940 err = rcar_pcie_parse_map_dma_ranges(host);
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +0300941 if (err)
Marek Vasut80b84712018-05-24 16:36:19 +0200942 goto err_clk_disable;
Phil Edworthyc25da472014-05-12 11:57:48 +0100943
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100944 host->phy_init_fn = of_device_get_match_data(dev);
945 err = host->phy_init_fn(host);
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000946 if (err) {
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300947 dev_err(dev, "failed to init PCIe PHY\n");
Marek Vasut80b84712018-05-24 16:36:19 +0200948 goto err_clk_disable;
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000949 }
950
951 /* Failure to get a link might just be that no cards are inserted */
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300952 if (rcar_pcie_hw_init(pcie)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500953 dev_info(dev, "PCIe link down\n");
Harunobu Kurokawae94888d2016-12-16 12:50:04 +0100954 err = -ENODEV;
Marek Vasut40503602018-06-29 13:47:38 -0500955 goto err_phy_shutdown;
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000956 }
957
958 data = rcar_pci_read_reg(pcie, MACSR);
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500959 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000960
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100961 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100962 err = rcar_pcie_enable_msi(host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100963 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500964 dev_err(dev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100965 "failed to enable MSI support: %d\n",
966 err);
Marek Vasut40503602018-06-29 13:47:38 -0500967 goto err_phy_shutdown;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100968 }
969 }
970
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100971 err = rcar_pcie_enable(host);
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000972 if (err)
Marek Vasut1aea58b2018-05-24 16:36:21 +0200973 goto err_msi_teardown;
Phil Edworthyc25da472014-05-12 11:57:48 +0100974
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000975 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100976
Marek Vasut1aea58b2018-05-24 16:36:21 +0200977err_msi_teardown:
978 if (IS_ENABLED(CONFIG_PCI_MSI))
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100979 rcar_pcie_teardown_msi(host);
Marek Vasut1aea58b2018-05-24 16:36:21 +0200980
Marek Vasut40503602018-06-29 13:47:38 -0500981err_phy_shutdown:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100982 if (host->phy) {
983 phy_power_off(host->phy);
984 phy_exit(host->phy);
Marek Vasut40503602018-06-29 13:47:38 -0500985 }
986
Marek Vasut80b84712018-05-24 16:36:19 +0200987err_clk_disable:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100988 clk_disable_unprepare(host->bus_clk);
Marek Vasut80b84712018-05-24 16:36:19 +0200989
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200990err_unmap_msi_irqs:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100991 irq_dispose_mapping(host->msi.irq2);
992 irq_dispose_mapping(host->msi.irq1);
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200993
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000994err_pm_put:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500995 pm_runtime_put(dev);
Phil Edworthyc25da472014-05-12 11:57:48 +0100996
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000997err_pm_disable:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500998 pm_runtime_disable(dev);
Dien Pham0df61502018-04-08 15:09:25 +0200999
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001000 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +01001001}
1002
Kazufumi Ikedace351632020-03-14 20:12:32 +01001003static int __maybe_unused rcar_pcie_resume(struct device *dev)
1004{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001005 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1006 struct rcar_pcie *pcie = &host->pcie;
Kazufumi Ikedace351632020-03-14 20:12:32 +01001007 unsigned int data;
1008 int err;
1009
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001010 err = rcar_pcie_parse_map_dma_ranges(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +01001011 if (err)
1012 return 0;
1013
1014 /* Failure to get a link might just be that no cards are inserted */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001015 err = host->phy_init_fn(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +01001016 if (err) {
1017 dev_info(dev, "PCIe link down\n");
1018 return 0;
1019 }
1020
1021 data = rcar_pci_read_reg(pcie, MACSR);
1022 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1023
1024 /* Enable MSI */
1025 if (IS_ENABLED(CONFIG_PCI_MSI))
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001026 rcar_pcie_hw_enable_msi(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +01001027
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001028 rcar_pcie_hw_enable(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +01001029
1030 return 0;
1031}
1032
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001033static int rcar_pcie_resume_noirq(struct device *dev)
1034{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001035 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1036 struct rcar_pcie *pcie = &host->pcie;
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001037
1038 if (rcar_pci_read_reg(pcie, PMSR) &&
1039 !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1040 return 0;
1041
1042 /* Re-establish the PCIe link */
Yoshihiro Shimoda7c7e53e2019-11-05 19:51:29 +09001043 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001044 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1045 return rcar_pcie_wait_for_dl(pcie);
1046}
1047
1048static const struct dev_pm_ops rcar_pcie_pm_ops = {
Kazufumi Ikedace351632020-03-14 20:12:32 +01001049 SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001050 .resume_noirq = rcar_pcie_resume_noirq,
1051};
1052
Phil Edworthyc25da472014-05-12 11:57:48 +01001053static struct platform_driver rcar_pcie_driver = {
1054 .driver = {
Bjorn Helgaas3ff8e4a2016-10-06 13:40:28 -05001055 .name = "rcar-pcie",
Phil Edworthyc25da472014-05-12 11:57:48 +01001056 .of_match_table = rcar_pcie_of_match,
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001057 .pm = &rcar_pcie_pm_ops,
Phil Edworthyc25da472014-05-12 11:57:48 +01001058 .suppress_bind_attrs = true,
1059 },
1060 .probe = rcar_pcie_probe,
1061};
Paul Gortmaker42d10712016-07-22 16:23:21 -05001062builtin_platform_driver(rcar_pcie_driver);