blob: e12c2d8be05a34e2b189e349934e3a4deb98c7bc [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>
Marek Vasuta115b1b2021-08-15 20:16:50 +020016#include <linux/clk-provider.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010017#include <linux/delay.h>
18#include <linux/interrupt.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010019#include <linux/irq.h>
20#include <linux/irqdomain.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010021#include <linux/kernel.h>
Paul Gortmaker42d10712016-07-22 16:23:21 -050022#include <linux/init.h>
Marek Vasuta115b1b2021-08-15 20:16:50 +020023#include <linux/iopoll.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010024#include <linux/msi.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010025#include <linux/of_address.h>
26#include <linux/of_irq.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010027#include <linux/of_platform.h>
28#include <linux/pci.h>
Sergei Shtylyov517ca932018-05-03 22:40:54 +030029#include <linux/phy/phy.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010030#include <linux/platform_device.h>
Phil Edworthyde1be9a2016-01-05 13:00:30 +000031#include <linux/pm_runtime.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010032
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;
Marc Zyngier83ed8d42021-03-30 16:11:34 +010038 struct mutex map_lock;
39 spinlock_t mask_lock;
Phil Edworthy290c1fb2014-05-12 11:57:49 +010040 int irq1;
41 int irq2;
42};
43
Marek Vasuta115b1b2021-08-15 20:16:50 +020044#ifdef CONFIG_ARM
45/*
46 * Here we keep a static copy of the remapped PCIe controller address.
47 * This is only used on aarch32 systems, all of which have one single
48 * PCIe controller, to provide quick access to the PCIe controller in
49 * the L1 link state fixup function, called from the ARM fault handler.
50 */
51static void __iomem *pcie_base;
52/*
53 * Static copy of bus clock pointer, so we can check whether the clock
54 * is enabled or not.
55 */
56static struct clk *pcie_bus_clk;
57#endif
58
Phil Edworthyc25da472014-05-12 11:57:48 +010059/* Structure representing the PCIe interface */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010060struct rcar_pcie_host {
61 struct rcar_pcie pcie;
Sergei Shtylyov517ca932018-05-03 22:40:54 +030062 struct phy *phy;
Phil Edworthyc25da472014-05-12 11:57:48 +010063 struct clk *bus_clk;
Phil Edworthy290c1fb2014-05-12 11:57:49 +010064 struct rcar_msi msi;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010065 int (*phy_init_fn)(struct rcar_pcie_host *host);
Phil Edworthyc25da472014-05-12 11:57:48 +010066};
67
Marc Zyngier83ed8d42021-03-30 16:11:34 +010068static struct rcar_pcie_host *msi_to_host(struct rcar_msi *msi)
69{
70 return container_of(msi, struct rcar_pcie_host, msi);
71}
72
Phil Edworthyc25da472014-05-12 11:57:48 +010073static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
74{
Marek Vasut29ffa6d2019-03-25 12:40:59 +010075 unsigned int shift = BITS_PER_BYTE * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +010076 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +010077
78 return val >> shift;
79}
80
81/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010082static int rcar_pcie_config_access(struct rcar_pcie_host *host,
Phil Edworthyc25da472014-05-12 11:57:48 +010083 unsigned char access_type, struct pci_bus *bus,
84 unsigned int devfn, int where, u32 *data)
85{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +010086 struct rcar_pcie *pcie = &host->pcie;
Marek Vasutd8fa2662019-03-25 12:40:58 +010087 unsigned int dev, func, reg, index;
Phil Edworthyc25da472014-05-12 11:57:48 +010088
89 dev = PCI_SLOT(devfn);
90 func = PCI_FUNC(devfn);
91 reg = where & ~3;
92 index = reg / 4;
93
94 /*
95 * While each channel has its own memory-mapped extended config
96 * space, it's generally only accessible when in endpoint mode.
97 * When in root complex mode, the controller is unable to target
98 * itself with either type 0 or type 1 accesses, and indeed, any
99 * controller initiated target transfer to its own config space
100 * result in a completer abort.
101 *
102 * Each channel effectively only supports a single device, but as
103 * the same channel <-> device access works for any PCI_SLOT()
104 * value, we cheat a bit here and bind the controller's config
105 * space to devfn 0 in order to enable self-enumeration. In this
106 * case the regular ECAR/ECDR path is sidelined and the mangled
107 * config access itself is initiated as an internal bus transaction.
108 */
109 if (pci_is_root_bus(bus)) {
110 if (dev != 0)
111 return PCIBIOS_DEVICE_NOT_FOUND;
112
Rob Herring6176a5f2020-07-21 20:25:05 -0600113 if (access_type == RCAR_PCI_ACCESS_READ)
Phil Edworthyb77188492014-06-30 08:54:23 +0100114 *data = rcar_pci_read_reg(pcie, PCICONF(index));
Rob Herring6176a5f2020-07-21 20:25:05 -0600115 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100116 rcar_pci_write_reg(pcie, *data, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100117
118 return PCIBIOS_SUCCESSFUL;
119 }
120
Phil Edworthyc25da472014-05-12 11:57:48 +0100121 /* Clear errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100122 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100123
124 /* Set the PIO address */
Phil Edworthyb77188492014-06-30 08:54:23 +0100125 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
126 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100127
128 /* Enable the configuration access */
Rob Herring6176a5f2020-07-21 20:25:05 -0600129 if (pci_is_root_bus(bus->parent))
Phil Edworthyb77188492014-06-30 08:54:23 +0100130 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100131 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100132 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100133
134 /* Check for errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100135 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
Phil Edworthyc25da472014-05-12 11:57:48 +0100136 return PCIBIOS_DEVICE_NOT_FOUND;
137
138 /* Check for master and target aborts */
139 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
140 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
141 return PCIBIOS_DEVICE_NOT_FOUND;
142
Phil Edworthyb77188492014-06-30 08:54:23 +0100143 if (access_type == RCAR_PCI_ACCESS_READ)
144 *data = rcar_pci_read_reg(pcie, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100145 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100146 rcar_pci_write_reg(pcie, *data, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100147
148 /* Disable the configuration access */
Phil Edworthyb77188492014-06-30 08:54:23 +0100149 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100150
151 return PCIBIOS_SUCCESSFUL;
152}
153
154static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
155 int where, int size, u32 *val)
156{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100157 struct rcar_pcie_host *host = bus->sysdata;
Phil Edworthyc25da472014-05-12 11:57:48 +0100158 int ret;
159
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100160 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100161 bus, devfn, where, val);
162 if (ret != PCIBIOS_SUCCESSFUL) {
163 *val = 0xffffffff;
164 return ret;
165 }
166
167 if (size == 1)
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100168 *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
Phil Edworthyc25da472014-05-12 11:57:48 +0100169 else if (size == 2)
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100170 *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
Phil Edworthyc25da472014-05-12 11:57:48 +0100171
Marek Vasut42a58f72019-03-25 12:41:00 +0100172 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
173 bus->number, devfn, where, size, *val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100174
175 return ret;
176}
177
178/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
179static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
180 int where, int size, u32 val)
181{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100182 struct rcar_pcie_host *host = bus->sysdata;
Marek Vasutd8fa2662019-03-25 12:40:58 +0100183 unsigned int shift;
Phil Edworthyc25da472014-05-12 11:57:48 +0100184 u32 data;
Marek Vasutd8fa2662019-03-25 12:40:58 +0100185 int ret;
Phil Edworthyc25da472014-05-12 11:57:48 +0100186
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100187 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100188 bus, devfn, where, &data);
189 if (ret != PCIBIOS_SUCCESSFUL)
190 return ret;
191
Marek Vasut42a58f72019-03-25 12:41:00 +0100192 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
193 bus->number, devfn, where, size, val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100194
195 if (size == 1) {
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100196 shift = BITS_PER_BYTE * (where & 3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100197 data &= ~(0xff << shift);
198 data |= ((val & 0xff) << shift);
199 } else if (size == 2) {
Marek Vasut29ffa6d2019-03-25 12:40:59 +0100200 shift = BITS_PER_BYTE * (where & 2);
Phil Edworthyc25da472014-05-12 11:57:48 +0100201 data &= ~(0xffff << shift);
202 data |= ((val & 0xffff) << shift);
203 } else
204 data = val;
205
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100206 ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100207 bus, devfn, where, &data);
208
209 return ret;
210}
211
212static struct pci_ops rcar_pcie_ops = {
213 .read = rcar_pcie_read_conf,
214 .write = rcar_pcie_write_conf,
215};
216
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300217static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
218{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500219 struct device *dev = pcie->dev;
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300220 unsigned int timeout = 1000;
221 u32 macsr;
222
223 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
224 return;
225
226 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500227 dev_err(dev, "Speed change already in progress\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300228 return;
229 }
230
231 macsr = rcar_pci_read_reg(pcie, MACSR);
232 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
233 goto done;
234
235 /* Set target link speed to 5.0 GT/s */
236 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
237 PCI_EXP_LNKSTA_CLS_5_0GB);
238
239 /* Set speed change reason as intentional factor */
240 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
241
242 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
243 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
244 rcar_pci_write_reg(pcie, macsr, MACSR);
245
246 /* Start link speed change */
247 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
248
249 while (timeout--) {
250 macsr = rcar_pci_read_reg(pcie, MACSR);
251 if (macsr & SPCHGFIN) {
252 /* Clear the interrupt bits */
253 rcar_pci_write_reg(pcie, macsr, MACSR);
254
255 if (macsr & SPCHGFAIL)
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500256 dev_err(dev, "Speed change failed\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300257
258 goto done;
259 }
260
261 msleep(1);
Fengguang Wud1708672018-03-07 09:42:39 -0600262 }
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300263
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500264 dev_err(dev, "Speed change timed out\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300265
266done:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500267 dev_info(dev, "Current link speed is %s GT/s\n",
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300268 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
269}
270
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100271static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
Kazufumi Ikedace351632020-03-14 20:12:32 +0100272{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100273 struct rcar_pcie *pcie = &host->pcie;
Rob Herringb411b2e2020-07-21 20:25:10 -0600274 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100275 struct resource_entry *win;
276 LIST_HEAD(res);
277 int i = 0;
278
279 /* Try setting 5 GT/s link speed */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100280 rcar_pcie_force_speedup(pcie);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100281
282 /* Setup PCI resources */
Rob Herringb411b2e2020-07-21 20:25:10 -0600283 resource_list_for_each_entry(win, &bridge->windows) {
Kazufumi Ikedace351632020-03-14 20:12:32 +0100284 struct resource *res = win->res;
285
286 if (!res->flags)
287 continue;
288
289 switch (resource_type(res)) {
290 case IORESOURCE_IO:
291 case IORESOURCE_MEM:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100292 rcar_pcie_set_outbound(pcie, i, win);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100293 i++;
294 break;
295 }
296 }
297}
298
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100299static int rcar_pcie_enable(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100300{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100301 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100302
Rob Herringb411b2e2020-07-21 20:25:10 -0600303 rcar_pcie_hw_enable(host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100304
Bjorn Helgaas71538842017-11-30 11:21:57 -0600305 pci_add_flags(PCI_REASSIGN_ALL_BUS);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100306
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100307 bridge->sysdata = host;
Lorenzo Pieralisi90634e82017-06-28 15:13:57 -0500308 bridge->ops = &rcar_pcie_ops;
Phil Edworthy79953dd2015-10-02 11:25:05 +0100309
Rob Herring56d29232020-05-22 17:48:29 -0600310 return pci_host_probe(bridge);
Phil Edworthyc25da472014-05-12 11:57:48 +0100311}
312
313static int phy_wait_for_ack(struct rcar_pcie *pcie)
314{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500315 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100316 unsigned int timeout = 100;
317
318 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100319 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
Phil Edworthyc25da472014-05-12 11:57:48 +0100320 return 0;
321
322 udelay(100);
323 }
324
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500325 dev_err(dev, "Access to PCIe phy timed out\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100326
327 return -ETIMEDOUT;
328}
329
330static void phy_write_reg(struct rcar_pcie *pcie,
Marek Vasutd8fa2662019-03-25 12:40:58 +0100331 unsigned int rate, u32 addr,
332 unsigned int lane, u32 data)
Phil Edworthyc25da472014-05-12 11:57:48 +0100333{
Marek Vasutd8fa2662019-03-25 12:40:58 +0100334 u32 phyaddr;
Phil Edworthyc25da472014-05-12 11:57:48 +0100335
336 phyaddr = WRITE_CMD |
337 ((rate & 1) << RATE_POS) |
338 ((lane & 0xf) << LANE_POS) |
339 ((addr & 0xff) << ADR_POS);
340
341 /* Set write data */
Phil Edworthyb77188492014-06-30 08:54:23 +0100342 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
343 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100344
345 /* Ignore errors as they will be dealt with if the data link is down */
346 phy_wait_for_ack(pcie);
347
348 /* Clear command */
Phil Edworthyb77188492014-06-30 08:54:23 +0100349 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
350 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100351
352 /* Ignore errors as they will be dealt with if the data link is down */
353 phy_wait_for_ack(pcie);
354}
355
Phil Edworthyc25da472014-05-12 11:57:48 +0100356static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
357{
358 int err;
359
360 /* Begin initialization */
Phil Edworthyb77188492014-06-30 08:54:23 +0100361 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100362
363 /* Set mode */
Phil Edworthyb77188492014-06-30 08:54:23 +0100364 rcar_pci_write_reg(pcie, 1, PCIEMSR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100365
Sergei Shtylyov3ad1d322018-05-03 22:36:37 +0300366 err = rcar_pcie_wait_for_phyrdy(pcie);
367 if (err)
368 return err;
369
Phil Edworthyc25da472014-05-12 11:57:48 +0100370 /*
371 * Initial header for port config space is type 1, set the device
372 * class to match. Hardware takes care of propagating the IDSETR
373 * settings, so there is no need to bother with a quirk.
374 */
Phil Edworthyb77188492014-06-30 08:54:23 +0100375 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
Phil Edworthyc25da472014-05-12 11:57:48 +0100376
377 /*
378 * Setup Secondary Bus Number & Subordinate Bus Number, even though
379 * they aren't used, to avoid bridge being detected as broken.
380 */
381 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
382 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
383
384 /* Initialize default capabilities. */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100385 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
Phil Edworthyc25da472014-05-12 11:57:48 +0100386 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
387 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
388 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
389 PCI_HEADER_TYPE_BRIDGE);
390
391 /* Enable data link layer active state reporting */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100392 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
393 PCI_EXP_LNKCAP_DLLLARC);
Phil Edworthyc25da472014-05-12 11:57:48 +0100394
395 /* Write out the physical slot number = 0 */
396 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
397
398 /* Set the completion timer timeout to the maximum 50ms. */
Phil Edworthyb77188492014-06-30 08:54:23 +0100399 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
Phil Edworthyc25da472014-05-12 11:57:48 +0100400
401 /* Terminate list of capabilities (Next Capability Offset=0) */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100402 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
Phil Edworthyc25da472014-05-12 11:57:48 +0100403
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100404 /* Enable MSI */
405 if (IS_ENABLED(CONFIG_PCI_MSI))
Nobuhiro Iwamatsu1fc6aa92015-02-02 14:09:39 +0900406 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100407
Yoshihiro Shimoda7c7e53e2019-11-05 19:51:29 +0900408 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
409
Phil Edworthyc25da472014-05-12 11:57:48 +0100410 /* Finish initialization - establish a PCI Express link */
Phil Edworthyb77188492014-06-30 08:54:23 +0100411 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100412
413 /* This will timeout if we don't have a link. */
414 err = rcar_pcie_wait_for_dl(pcie);
415 if (err)
416 return err;
417
418 /* Enable INTx interrupts */
419 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
420
Phil Edworthyc25da472014-05-12 11:57:48 +0100421 wmb();
422
423 return 0;
424}
425
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100426static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100427{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100428 struct rcar_pcie *pcie = &host->pcie;
429
Phil Edworthyc25da472014-05-12 11:57:48 +0100430 /* Initialize the phy */
431 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
432 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
433 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
434 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
435 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
436 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
437 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
438 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
439 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
440 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
441 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
442 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
443
444 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
445 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
446 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
447
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300448 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100449}
450
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100451static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
Phil Edworthy581d9432016-01-05 13:00:31 +0000452{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100453 struct rcar_pcie *pcie = &host->pcie;
454
Phil Edworthy581d9432016-01-05 13:00:31 +0000455 /*
456 * These settings come from the R-Car Series, 2nd Generation User's
457 * Manual, section 50.3.1 (2) Initialization of the physical layer.
458 */
459 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
460 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
461 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
462 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
463
464 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
465 /* The following value is for DC connection, no termination resistor */
466 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
467 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
468 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
469
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300470 return 0;
Phil Edworthy581d9432016-01-05 13:00:31 +0000471}
472
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100473static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300474{
475 int err;
476
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100477 err = phy_init(host->phy);
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300478 if (err)
479 return err;
480
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100481 err = phy_power_on(host->phy);
Marek Vasut3c5777c2018-06-29 13:48:15 -0500482 if (err)
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100483 phy_exit(host->phy);
Marek Vasut3c5777c2018-06-29 13:48:15 -0500484
485 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100486}
487
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100488static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
489{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100490 struct rcar_pcie_host *host = data;
491 struct rcar_pcie *pcie = &host->pcie;
492 struct rcar_msi *msi = &host->msi;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500493 struct device *dev = pcie->dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100494 unsigned long reg;
495
Phil Edworthyb77188492014-06-30 08:54:23 +0100496 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100497
498 /* MSI & INTx share an interrupt - we only handle MSI here */
499 if (!reg)
500 return IRQ_NONE;
501
502 while (reg) {
503 unsigned int index = find_first_bit(&reg, 32);
Marc Zyngierd21faba12021-08-02 17:26:19 +0100504 int ret;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100505
Marc Zyngierd21faba12021-08-02 17:26:19 +0100506 ret = generic_handle_domain_irq(msi->domain->parent, index);
507 if (ret) {
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100508 /* Unknown MSI, just clear it */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500509 dev_dbg(dev, "unexpected MSI\n");
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100510 rcar_pci_write_reg(pcie, BIT(index), PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100511 }
512
513 /* see if there's any more pending in this vector */
Phil Edworthyb77188492014-06-30 08:54:23 +0100514 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100515 }
516
517 return IRQ_HANDLED;
518}
519
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100520static void rcar_msi_top_irq_ack(struct irq_data *d)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100521{
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100522 irq_chip_ack_parent(d);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100523}
524
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100525static void rcar_msi_top_irq_mask(struct irq_data *d)
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300526{
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100527 pci_msi_mask_irq(d);
528 irq_chip_mask_parent(d);
529}
530
531static void rcar_msi_top_irq_unmask(struct irq_data *d)
532{
533 pci_msi_unmask_irq(d);
534 irq_chip_unmask_parent(d);
535}
536
537static struct irq_chip rcar_msi_top_chip = {
538 .name = "PCIe MSI",
539 .irq_ack = rcar_msi_top_irq_ack,
540 .irq_mask = rcar_msi_top_irq_mask,
541 .irq_unmask = rcar_msi_top_irq_unmask,
542};
543
544static void rcar_msi_irq_ack(struct irq_data *d)
545{
546 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
547 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
548
549 /* clear the interrupt */
550 rcar_pci_write_reg(pcie, BIT(d->hwirq), PCIEMSIFR);
551}
552
553static void rcar_msi_irq_mask(struct irq_data *d)
554{
555 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
556 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
557 unsigned long flags;
558 u32 value;
559
560 spin_lock_irqsave(&msi->mask_lock, flags);
561 value = rcar_pci_read_reg(pcie, PCIEMSIIER);
562 value &= ~BIT(d->hwirq);
563 rcar_pci_write_reg(pcie, value, PCIEMSIIER);
564 spin_unlock_irqrestore(&msi->mask_lock, flags);
565}
566
567static void rcar_msi_irq_unmask(struct irq_data *d)
568{
569 struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
570 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
571 unsigned long flags;
572 u32 value;
573
574 spin_lock_irqsave(&msi->mask_lock, flags);
575 value = rcar_pci_read_reg(pcie, PCIEMSIIER);
576 value |= BIT(d->hwirq);
577 rcar_pci_write_reg(pcie, value, PCIEMSIIER);
578 spin_unlock_irqrestore(&msi->mask_lock, flags);
579}
580
581static int rcar_msi_set_affinity(struct irq_data *d, const struct cpumask *mask, bool force)
582{
583 return -EINVAL;
584}
585
586static void rcar_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
587{
588 struct rcar_msi *msi = irq_data_get_irq_chip_data(data);
589 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
590
591 msg->address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
592 msg->address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
593 msg->data = data->hwirq;
594}
595
596static struct irq_chip rcar_msi_bottom_chip = {
597 .name = "Rcar MSI",
598 .irq_ack = rcar_msi_irq_ack,
599 .irq_mask = rcar_msi_irq_mask,
600 .irq_unmask = rcar_msi_irq_unmask,
601 .irq_set_affinity = rcar_msi_set_affinity,
602 .irq_compose_msi_msg = rcar_compose_msi_msg,
603};
604
605static int rcar_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
606 unsigned int nr_irqs, void *args)
607{
608 struct rcar_msi *msi = domain->host_data;
609 unsigned int i;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300610 int hwirq;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300611
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100612 mutex_lock(&msi->map_lock);
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300613
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100614 hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs));
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300615
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100616 mutex_unlock(&msi->map_lock);
617
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300618 if (hwirq < 0)
619 return -ENOSPC;
620
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100621 for (i = 0; i < nr_irqs; i++)
622 irq_domain_set_info(domain, virq + i, hwirq + i,
623 &rcar_msi_bottom_chip, domain->host_data,
624 handle_edge_irq, NULL, NULL);
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300625
626 return 0;
627}
628
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100629static void rcar_msi_domain_free(struct irq_domain *domain, unsigned int virq,
630 unsigned int nr_irqs)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100631{
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100632 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
633 struct rcar_msi *msi = domain->host_data;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100634
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100635 mutex_lock(&msi->map_lock);
636
637 bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs));
638
639 mutex_unlock(&msi->map_lock);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100640}
641
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100642static const struct irq_domain_ops rcar_msi_domain_ops = {
643 .alloc = rcar_msi_domain_alloc,
644 .free = rcar_msi_domain_free,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100645};
646
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100647static struct msi_domain_info rcar_msi_info = {
648 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
649 MSI_FLAG_MULTI_PCI_MSI),
650 .chip = &rcar_msi_top_chip,
651};
652
653static int rcar_allocate_domains(struct rcar_msi *msi)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100654{
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100655 struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
656 struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
657 struct irq_domain *parent;
658
659 parent = irq_domain_create_linear(fwnode, INT_PCI_MSI_NR,
660 &rcar_msi_domain_ops, msi);
661 if (!parent) {
662 dev_err(pcie->dev, "failed to create IRQ domain\n");
663 return -ENOMEM;
664 }
665 irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
666
667 msi->domain = pci_msi_create_irq_domain(fwnode, &rcar_msi_info, parent);
668 if (!msi->domain) {
669 dev_err(pcie->dev, "failed to create MSI domain\n");
670 irq_domain_remove(parent);
671 return -ENOMEM;
672 }
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100673
674 return 0;
675}
676
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100677static void rcar_free_domains(struct rcar_msi *msi)
Marek Vasut0bbf6b92018-05-24 16:36:23 +0200678{
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100679 struct irq_domain *parent = msi->domain->parent;
Marek Vasut0bbf6b92018-05-24 16:36:23 +0200680
681 irq_domain_remove(msi->domain);
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100682 irq_domain_remove(parent);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100683}
684
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100685static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100686{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100687 struct rcar_pcie *pcie = &host->pcie;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500688 struct device *dev = pcie->dev;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100689 struct rcar_msi *msi = &host->msi;
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100690 struct resource res;
691 int err;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100692
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100693 mutex_init(&msi->map_lock);
694 spin_lock_init(&msi->mask_lock);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100695
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100696 err = of_address_to_resource(dev->of_node, 0, &res);
697 if (err)
698 return err;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100699
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100700 err = rcar_allocate_domains(msi);
701 if (err)
702 return err;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300703
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100704 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500705 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200706 IRQF_SHARED | IRQF_NO_THREAD,
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100707 rcar_msi_bottom_chip.name, host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100708 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500709 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100710 goto err;
711 }
712
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500713 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200714 IRQF_SHARED | IRQF_NO_THREAD,
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100715 rcar_msi_bottom_chip.name, host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100716 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500717 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100718 goto err;
719 }
720
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100721 /* disable all MSIs */
722 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
723
724 /*
725 * Setup MSI data target using RC base address address, which
726 * is guaranteed to be in the low 32bit range on any RCar HW.
727 */
728 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
729 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100730
731 return 0;
732
733err:
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100734 rcar_free_domains(msi);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100735 return err;
736}
737
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100738static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
Marek Vasut1aea58b2018-05-24 16:36:21 +0200739{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100740 struct rcar_pcie *pcie = &host->pcie;
Marek Vasut1aea58b2018-05-24 16:36:21 +0200741
742 /* Disable all MSI interrupts */
743 rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
744
745 /* Disable address decoding of the MSI interrupt, MSIFE */
746 rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
747
Marc Zyngier83ed8d42021-03-30 16:11:34 +0100748 rcar_free_domains(&host->msi);
Marek Vasut1aea58b2018-05-24 16:36:21 +0200749}
750
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100751static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100752{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100753 struct rcar_pcie *pcie = &host->pcie;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500754 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100755 struct resource res;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100756 int err, i;
Phil Edworthyc25da472014-05-12 11:57:48 +0100757
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100758 host->phy = devm_phy_optional_get(dev, "pcie");
759 if (IS_ERR(host->phy))
760 return PTR_ERR(host->phy);
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300761
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500762 err = of_address_to_resource(dev->of_node, 0, &res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100763 if (err)
764 return err;
765
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500766 pcie->base = devm_ioremap_resource(dev, &res);
Bjorn Helgaas51afa3c2016-08-22 14:16:38 -0500767 if (IS_ERR(pcie->base))
768 return PTR_ERR(pcie->base);
769
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100770 host->bus_clk = devm_clk_get(dev, "pcie_bus");
771 if (IS_ERR(host->bus_clk)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500772 dev_err(dev, "cannot get pcie bus clock\n");
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100773 return PTR_ERR(host->bus_clk);
Phil Edworthyc25da472014-05-12 11:57:48 +0100774 }
Phil Edworthyc25da472014-05-12 11:57:48 +0100775
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500776 i = irq_of_parse_and_map(dev->of_node, 0);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800777 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500778 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100779 err = -ENOENT;
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200780 goto err_irq1;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100781 }
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100782 host->msi.irq1 = i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100783
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500784 i = irq_of_parse_and_map(dev->of_node, 1);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800785 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500786 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100787 err = -ENOENT;
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200788 goto err_irq2;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100789 }
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100790 host->msi.irq2 = i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100791
Marek Vasuta115b1b2021-08-15 20:16:50 +0200792#ifdef CONFIG_ARM
793 /* Cache static copy for L1 link state fixup hook on aarch32 */
794 pcie_base = pcie->base;
795 pcie_bus_clk = host->bus_clk;
796#endif
797
Phil Edworthyc25da472014-05-12 11:57:48 +0100798 return 0;
799
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200800err_irq2:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100801 irq_dispose_mapping(host->msi.irq1);
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200802err_irq1:
Phil Edworthyc25da472014-05-12 11:57:48 +0100803 return err;
804}
805
806static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
Rob Herring085f7932019-10-28 11:32:55 -0500807 struct resource_entry *entry,
Phil Edworthyc25da472014-05-12 11:57:48 +0100808 int *index)
809{
Rob Herring085f7932019-10-28 11:32:55 -0500810 u64 restype = entry->res->flags;
811 u64 cpu_addr = entry->res->start;
812 u64 cpu_end = entry->res->end;
813 u64 pci_addr = entry->res->start - entry->offset;
Phil Edworthyc25da472014-05-12 11:57:48 +0100814 u32 flags = LAM_64BIT | LAR_ENABLE;
815 u64 mask;
Rob Herring085f7932019-10-28 11:32:55 -0500816 u64 size = resource_size(entry->res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100817 int idx = *index;
818
819 if (restype & IORESOURCE_PREFETCH)
820 flags |= LAM_PREFETCH;
821
Phil Edworthyc25da472014-05-12 11:57:48 +0100822 while (cpu_addr < cpu_end) {
Marek Vasut85bff4c2019-10-26 20:26:58 +0200823 if (idx >= MAX_NR_INBOUND_MAPS - 1) {
824 dev_err(pcie->dev, "Failed to map inbound regions!\n");
825 return -EINVAL;
826 }
Phil Edworthyc25da472014-05-12 11:57:48 +0100827 /*
Marek Vasut767c7842019-10-26 20:26:59 +0200828 * If the size of the range is larger than the alignment of
829 * the start address, we have to use multiple entries to
830 * perform the mapping.
831 */
832 if (cpu_addr > 0) {
833 unsigned long nr_zeros = __ffs64(cpu_addr);
834 u64 alignment = 1ULL << nr_zeros;
835
Bjorn Helgaas7bd4c4a2019-11-28 08:54:53 -0600836 size = min(size, alignment);
Marek Vasut767c7842019-10-26 20:26:59 +0200837 }
838 /* Hardware supports max 4GiB inbound region */
839 size = min(size, 1ULL << 32);
840
841 mask = roundup_pow_of_two(size) - 1;
842 mask &= ~0xf;
843
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100844 rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
845 lower_32_bits(mask) | flags, idx, true);
Phil Edworthyc25da472014-05-12 11:57:48 +0100846
847 pci_addr += size;
848 cpu_addr += size;
849 idx += 2;
Phil Edworthyc25da472014-05-12 11:57:48 +0100850 }
851 *index = idx;
852
853 return 0;
854}
855
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100856static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
Phil Edworthyc25da472014-05-12 11:57:48 +0100857{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100858 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
Rob Herring085f7932019-10-28 11:32:55 -0500859 struct resource_entry *entry;
860 int index = 0, err = 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100861
Rob Herring085f7932019-10-28 11:32:55 -0500862 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100863 err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
Phil Edworthyc25da472014-05-12 11:57:48 +0100864 if (err)
Rob Herring085f7932019-10-28 11:32:55 -0500865 break;
Phil Edworthyc25da472014-05-12 11:57:48 +0100866 }
867
Rob Herring085f7932019-10-28 11:32:55 -0500868 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100869}
870
871static const struct of_device_id rcar_pcie_of_match[] = {
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300872 { .compatible = "renesas,pcie-r8a7779",
873 .data = rcar_pcie_phy_init_h1 },
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +0300874 { .compatible = "renesas,pcie-r8a7790",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300875 .data = rcar_pcie_phy_init_gen2 },
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +0300876 { .compatible = "renesas,pcie-r8a7791",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300877 .data = rcar_pcie_phy_init_gen2 },
Simon Hormand83a3282016-12-06 16:51:30 +0100878 { .compatible = "renesas,pcie-rcar-gen2",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300879 .data = rcar_pcie_phy_init_gen2 },
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300880 { .compatible = "renesas,pcie-r8a7795",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300881 .data = rcar_pcie_phy_init_gen3 },
Sergei Shtylyov517ca932018-05-03 22:40:54 +0300882 { .compatible = "renesas,pcie-rcar-gen3",
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300883 .data = rcar_pcie_phy_init_gen3 },
Phil Edworthyc25da472014-05-12 11:57:48 +0100884 {},
885};
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000886
Phil Edworthyc25da472014-05-12 11:57:48 +0100887static int rcar_pcie_probe(struct platform_device *pdev)
888{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500889 struct device *dev = &pdev->dev;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100890 struct rcar_pcie_host *host;
Phil Edworthyc25da472014-05-12 11:57:48 +0100891 struct rcar_pcie *pcie;
Marek Vasutd8fa2662019-03-25 12:40:58 +0100892 u32 data;
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000893 int err;
Lorenzo Pieralisi90634e82017-06-28 15:13:57 -0500894 struct pci_host_bridge *bridge;
Phil Edworthyc25da472014-05-12 11:57:48 +0100895
Rob Herring61f11f82020-07-21 20:25:09 -0600896 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
Lorenzo Pieralisi90634e82017-06-28 15:13:57 -0500897 if (!bridge)
Phil Edworthyc25da472014-05-12 11:57:48 +0100898 return -ENOMEM;
899
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100900 host = pci_host_bridge_priv(bridge);
901 pcie = &host->pcie;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500902 pcie->dev = dev;
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100903 platform_set_drvdata(pdev, host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100904
Dien Pham0df61502018-04-08 15:09:25 +0200905 pm_runtime_enable(pcie->dev);
906 err = pm_runtime_get_sync(pcie->dev);
907 if (err < 0) {
908 dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
Dinghao Liua68e06e2020-07-09 14:43:56 +0800909 goto err_pm_put;
Dien Pham0df61502018-04-08 15:09:25 +0200910 }
911
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100912 err = rcar_pcie_get_resources(host);
Phil Edworthyc25da472014-05-12 11:57:48 +0100913 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500914 dev_err(dev, "failed to request resources: %d\n", err);
Dien Pham0df61502018-04-08 15:09:25 +0200915 goto err_pm_put;
Phil Edworthyc25da472014-05-12 11:57:48 +0100916 }
917
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100918 err = clk_prepare_enable(host->bus_clk);
Marek Vasut80b84712018-05-24 16:36:19 +0200919 if (err) {
920 dev_err(dev, "failed to enable bus clock: %d\n", err);
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200921 goto err_unmap_msi_irqs;
Phil Edworthyc25da472014-05-12 11:57:48 +0100922 }
923
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100924 err = rcar_pcie_parse_map_dma_ranges(host);
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +0300925 if (err)
Marek Vasut80b84712018-05-24 16:36:19 +0200926 goto err_clk_disable;
Phil Edworthyc25da472014-05-12 11:57:48 +0100927
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100928 host->phy_init_fn = of_device_get_match_data(dev);
929 err = host->phy_init_fn(host);
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000930 if (err) {
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300931 dev_err(dev, "failed to init PCIe PHY\n");
Marek Vasut80b84712018-05-24 16:36:19 +0200932 goto err_clk_disable;
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000933 }
934
935 /* Failure to get a link might just be that no cards are inserted */
Sergei Shtylyov9d5014e2018-05-03 22:43:13 +0300936 if (rcar_pcie_hw_init(pcie)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500937 dev_info(dev, "PCIe link down\n");
Harunobu Kurokawae94888d2016-12-16 12:50:04 +0100938 err = -ENODEV;
Marek Vasut40503602018-06-29 13:47:38 -0500939 goto err_phy_shutdown;
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000940 }
941
942 data = rcar_pci_read_reg(pcie, MACSR);
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500943 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000944
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100945 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100946 err = rcar_pcie_enable_msi(host);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100947 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500948 dev_err(dev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100949 "failed to enable MSI support: %d\n",
950 err);
Marek Vasut40503602018-06-29 13:47:38 -0500951 goto err_phy_shutdown;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100952 }
953 }
954
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100955 err = rcar_pcie_enable(host);
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000956 if (err)
Marek Vasut1aea58b2018-05-24 16:36:21 +0200957 goto err_msi_teardown;
Phil Edworthyc25da472014-05-12 11:57:48 +0100958
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000959 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100960
Marek Vasut1aea58b2018-05-24 16:36:21 +0200961err_msi_teardown:
962 if (IS_ENABLED(CONFIG_PCI_MSI))
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100963 rcar_pcie_teardown_msi(host);
Marek Vasut1aea58b2018-05-24 16:36:21 +0200964
Marek Vasut40503602018-06-29 13:47:38 -0500965err_phy_shutdown:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100966 if (host->phy) {
967 phy_power_off(host->phy);
968 phy_exit(host->phy);
Marek Vasut40503602018-06-29 13:47:38 -0500969 }
970
Marek Vasut80b84712018-05-24 16:36:19 +0200971err_clk_disable:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100972 clk_disable_unprepare(host->bus_clk);
Marek Vasut80b84712018-05-24 16:36:19 +0200973
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200974err_unmap_msi_irqs:
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100975 irq_dispose_mapping(host->msi.irq2);
976 irq_dispose_mapping(host->msi.irq1);
Marek Vasut53f1aeb2018-05-24 16:36:20 +0200977
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000978err_pm_put:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500979 pm_runtime_put(dev);
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500980 pm_runtime_disable(dev);
Lorenzo Pieralisi3c60e012017-08-04 12:32:54 +0900981
Phil Edworthyde1be9a2016-01-05 13:00:30 +0000982 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100983}
984
Kazufumi Ikedace351632020-03-14 20:12:32 +0100985static int __maybe_unused rcar_pcie_resume(struct device *dev)
986{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100987 struct rcar_pcie_host *host = dev_get_drvdata(dev);
988 struct rcar_pcie *pcie = &host->pcie;
Kazufumi Ikedace351632020-03-14 20:12:32 +0100989 unsigned int data;
990 int err;
991
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100992 err = rcar_pcie_parse_map_dma_ranges(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100993 if (err)
994 return 0;
995
996 /* Failure to get a link might just be that no cards are inserted */
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +0100997 err = host->phy_init_fn(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +0100998 if (err) {
999 dev_info(dev, "PCIe link down\n");
1000 return 0;
1001 }
1002
1003 data = rcar_pci_read_reg(pcie, MACSR);
1004 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
1005
1006 /* Enable MSI */
Marc Zyngier83ed8d42021-03-30 16:11:34 +01001007 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1008 struct resource res;
1009 u32 val;
1010
1011 of_address_to_resource(dev->of_node, 0, &res);
1012 rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
1013 rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
1014
1015 bitmap_to_arr32(&val, host->msi.used, INT_PCI_MSI_NR);
1016 rcar_pci_write_reg(pcie, val, PCIEMSIIER);
1017 }
Kazufumi Ikedace351632020-03-14 20:12:32 +01001018
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001019 rcar_pcie_hw_enable(host);
Kazufumi Ikedace351632020-03-14 20:12:32 +01001020
1021 return 0;
1022}
1023
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001024static int rcar_pcie_resume_noirq(struct device *dev)
1025{
Lad Prabhakar78a0d7f2020-05-07 13:33:13 +01001026 struct rcar_pcie_host *host = dev_get_drvdata(dev);
1027 struct rcar_pcie *pcie = &host->pcie;
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001028
1029 if (rcar_pci_read_reg(pcie, PMSR) &&
1030 !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
1031 return 0;
1032
1033 /* Re-establish the PCIe link */
Yoshihiro Shimoda7c7e53e2019-11-05 19:51:29 +09001034 rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001035 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
1036 return rcar_pcie_wait_for_dl(pcie);
1037}
1038
1039static const struct dev_pm_ops rcar_pcie_pm_ops = {
Kazufumi Ikedace351632020-03-14 20:12:32 +01001040 SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001041 .resume_noirq = rcar_pcie_resume_noirq,
1042};
1043
Phil Edworthyc25da472014-05-12 11:57:48 +01001044static struct platform_driver rcar_pcie_driver = {
1045 .driver = {
Bjorn Helgaas3ff8e4a2016-10-06 13:40:28 -05001046 .name = "rcar-pcie",
Phil Edworthyc25da472014-05-12 11:57:48 +01001047 .of_match_table = rcar_pcie_of_match,
Kazufumi Ikedabe20bbc2019-03-25 20:43:19 +01001048 .pm = &rcar_pcie_pm_ops,
Phil Edworthyc25da472014-05-12 11:57:48 +01001049 .suppress_bind_attrs = true,
1050 },
1051 .probe = rcar_pcie_probe,
1052};
Marek Vasuta115b1b2021-08-15 20:16:50 +02001053
1054#ifdef CONFIG_ARM
1055static DEFINE_SPINLOCK(pmsr_lock);
1056static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
1057 unsigned int fsr, struct pt_regs *regs)
1058{
1059 unsigned long flags;
1060 u32 pmsr, val;
1061 int ret = 0;
1062
1063 spin_lock_irqsave(&pmsr_lock, flags);
1064
1065 if (!pcie_base || !__clk_is_enabled(pcie_bus_clk)) {
1066 ret = 1;
1067 goto unlock_exit;
1068 }
1069
1070 pmsr = readl(pcie_base + PMSR);
1071
1072 /*
1073 * Test if the PCIe controller received PM_ENTER_L1 DLLP and
1074 * the PCIe controller is not in L1 link state. If true, apply
1075 * fix, which will put the controller into L1 link state, from
1076 * which it can return to L0s/L0 on its own.
1077 */
1078 if ((pmsr & PMEL1RX) && ((pmsr & PMSTATE) != PMSTATE_L1)) {
1079 writel(L1IATN, pcie_base + PMCTLR);
1080 ret = readl_poll_timeout_atomic(pcie_base + PMSR, val,
1081 val & L1FAEG, 10, 1000);
1082 WARN(ret, "Timeout waiting for L1 link state, ret=%d\n", ret);
1083 writel(L1FAEG | PMEL1RX, pcie_base + PMSR);
1084 }
1085
1086unlock_exit:
1087 spin_unlock_irqrestore(&pmsr_lock, flags);
1088 return ret;
1089}
1090
1091static const struct of_device_id rcar_pcie_abort_handler_of_match[] __initconst = {
1092 { .compatible = "renesas,pcie-r8a7779" },
1093 { .compatible = "renesas,pcie-r8a7790" },
1094 { .compatible = "renesas,pcie-r8a7791" },
1095 { .compatible = "renesas,pcie-rcar-gen2" },
1096 {},
1097};
1098
1099static int __init rcar_pcie_init(void)
1100{
1101 if (of_find_matching_node(NULL, rcar_pcie_abort_handler_of_match)) {
1102#ifdef CONFIG_ARM_LPAE
1103 hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
1104 "asynchronous external abort");
1105#else
1106 hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
1107 "imprecise external abort");
1108#endif
1109 }
1110
1111 return platform_driver_register(&rcar_pcie_driver);
1112}
1113device_initcall(rcar_pcie_init);
1114#else
Paul Gortmaker42d10712016-07-22 16:23:21 -05001115builtin_platform_driver(rcar_pcie_driver);
Marek Vasuta115b1b2021-08-15 20:16:50 +02001116#endif