blob: 3ff423220df61d133aa3c895c678760f1d1b4e22 [file] [log] [blame]
Phil Edworthyc25da472014-05-12 11:57:48 +01001/*
2 * PCIe driver for Renesas R-Car SoCs
3 * Copyright (C) 2014 Renesas Electronics Europe Ltd
4 *
5 * Based on:
6 * arch/sh/drivers/pci/pcie-sh7786.c
7 * arch/sh/drivers/pci/ops-sh7786.c
8 * Copyright (C) 2009 - 2011 Paul Mundt
9 *
Paul Gortmaker42d10712016-07-22 16:23:21 -050010 * Author: Phil Edworthy <phil.edworthy@renesas.com>
11 *
Phil Edworthyc25da472014-05-12 11:57:48 +010012 * This file is licensed under the terms of the GNU General Public
13 * License version 2. This program is licensed "as is" without any
14 * warranty of any kind, whether express or implied.
15 */
16
17#include <linux/clk.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
Phil Edworthy290c1fb2014-05-12 11:57:49 +010020#include <linux/irq.h>
21#include <linux/irqdomain.h>
Phil Edworthyc25da472014-05-12 11:57:48 +010022#include <linux/kernel.h>
Paul Gortmaker42d10712016-07-22 16:23:21 -050023#include <linux/init.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>
27#include <linux/of_pci.h>
28#include <linux/of_platform.h>
29#include <linux/pci.h>
30#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#include <linux/slab.h>
33
Phil Edworthyc25da472014-05-12 11:57:48 +010034#define PCIECAR 0x000010
35#define PCIECCTLR 0x000018
36#define CONFIG_SEND_ENABLE (1 << 31)
37#define TYPE0 (0 << 8)
38#define TYPE1 (1 << 8)
39#define PCIECDR 0x000020
40#define PCIEMSR 0x000028
41#define PCIEINTXR 0x000400
Phil Edworthy290c1fb2014-05-12 11:57:49 +010042#define PCIEMSITXR 0x000840
Phil Edworthyc25da472014-05-12 11:57:48 +010043
44/* Transfer control */
45#define PCIETCTLR 0x02000
46#define CFINIT 1
47#define PCIETSTR 0x02004
48#define DATA_LINK_ACTIVE 1
49#define PCIEERRFR 0x02020
50#define UNSUPPORTED_REQUEST (1 << 4)
Phil Edworthy290c1fb2014-05-12 11:57:49 +010051#define PCIEMSIFR 0x02044
52#define PCIEMSIALR 0x02048
53#define MSIFE 1
54#define PCIEMSIAUR 0x0204c
55#define PCIEMSIIER 0x02050
Phil Edworthyc25da472014-05-12 11:57:48 +010056
57/* root port address */
58#define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
59
60/* local address reg & mask */
61#define PCIELAR(x) (0x02200 + ((x) * 0x20))
62#define PCIELAMR(x) (0x02208 + ((x) * 0x20))
63#define LAM_PREFETCH (1 << 3)
64#define LAM_64BIT (1 << 2)
65#define LAR_ENABLE (1 << 1)
66
67/* PCIe address reg & mask */
Nobuhiro Iwamatsuecd06302015-02-04 18:02:55 +090068#define PCIEPALR(x) (0x03400 + ((x) * 0x20))
69#define PCIEPAUR(x) (0x03404 + ((x) * 0x20))
Phil Edworthyc25da472014-05-12 11:57:48 +010070#define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
71#define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
72#define PAR_ENABLE (1 << 31)
73#define IO_SPACE (1 << 8)
74
75/* Configuration */
76#define PCICONF(x) (0x010000 + ((x) * 0x4))
77#define PMCAP(x) (0x010040 + ((x) * 0x4))
78#define EXPCAP(x) (0x010070 + ((x) * 0x4))
79#define VCCAP(x) (0x010100 + ((x) * 0x4))
80
81/* link layer */
82#define IDSETR1 0x011004
83#define TLCTLR 0x011048
84#define MACSR 0x011054
Sergei Shtylyovb3327f72016-09-22 23:20:18 +030085#define SPCHGFIN (1 << 4)
86#define SPCHGFAIL (1 << 6)
87#define SPCHGSUC (1 << 7)
88#define LINK_SPEED (0xf << 16)
89#define LINK_SPEED_2_5GTS (1 << 16)
90#define LINK_SPEED_5_0GTS (2 << 16)
Phil Edworthyc25da472014-05-12 11:57:48 +010091#define MACCTLR 0x011058
Sergei Shtylyovb3327f72016-09-22 23:20:18 +030092#define SPEED_CHANGE (1 << 24)
Phil Edworthyc25da472014-05-12 11:57:48 +010093#define SCRAMBLE_DISABLE (1 << 27)
Sergei Shtylyovb3327f72016-09-22 23:20:18 +030094#define MACS2R 0x011078
95#define MACCGSPSETR 0x011084
96#define SPCNGRSN (1 << 31)
Phil Edworthyc25da472014-05-12 11:57:48 +010097
98/* R-Car H1 PHY */
99#define H1_PCIEPHYADRR 0x04000c
100#define WRITE_CMD (1 << 16)
101#define PHY_ACK (1 << 24)
102#define RATE_POS 12
103#define LANE_POS 8
104#define ADR_POS 0
105#define H1_PCIEPHYDOUTR 0x040014
106#define H1_PCIEPHYSR 0x040018
107
Phil Edworthy581d9432016-01-05 13:00:31 +0000108/* R-Car Gen2 PHY */
109#define GEN2_PCIEPHYADDR 0x780
110#define GEN2_PCIEPHYDATA 0x784
111#define GEN2_PCIEPHYCTRL 0x78c
112
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100113#define INT_PCI_MSI_NR 32
114
Phil Edworthyc25da472014-05-12 11:57:48 +0100115#define RCONF(x) (PCICONF(0)+(x))
116#define RPMCAP(x) (PMCAP(0)+(x))
117#define REXPCAP(x) (EXPCAP(0)+(x))
118#define RVCCAP(x) (VCCAP(0)+(x))
119
120#define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
121#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
122#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
123
Phil Edworthyb77188492014-06-30 08:54:23 +0100124#define RCAR_PCI_MAX_RESOURCES 4
Phil Edworthyc25da472014-05-12 11:57:48 +0100125#define MAX_NR_INBOUND_MAPS 6
126
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100127struct rcar_msi {
128 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
129 struct irq_domain *domain;
Yijing Wangc2791b82014-11-11 17:45:45 -0700130 struct msi_controller chip;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100131 unsigned long pages;
132 struct mutex lock;
133 int irq1;
134 int irq2;
135};
136
Yijing Wangc2791b82014-11-11 17:45:45 -0700137static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100138{
139 return container_of(chip, struct rcar_msi, chip);
140}
141
Phil Edworthyc25da472014-05-12 11:57:48 +0100142/* Structure representing the PCIe interface */
143struct rcar_pcie {
144 struct device *dev;
145 void __iomem *base;
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000146 struct list_head resources;
Phil Edworthyc25da472014-05-12 11:57:48 +0100147 int root_bus_nr;
148 struct clk *clk;
149 struct clk *bus_clk;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100150 struct rcar_msi msi;
Phil Edworthyc25da472014-05-12 11:57:48 +0100151};
152
Phil Edworthyb77188492014-06-30 08:54:23 +0100153static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
154 unsigned long reg)
Phil Edworthyc25da472014-05-12 11:57:48 +0100155{
156 writel(val, pcie->base + reg);
157}
158
Phil Edworthyb77188492014-06-30 08:54:23 +0100159static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
160 unsigned long reg)
Phil Edworthyc25da472014-05-12 11:57:48 +0100161{
162 return readl(pcie->base + reg);
163}
164
165enum {
Phil Edworthyb77188492014-06-30 08:54:23 +0100166 RCAR_PCI_ACCESS_READ,
167 RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100168};
169
170static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
171{
172 int shift = 8 * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +0100173 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100174
175 val &= ~(mask << shift);
176 val |= data << shift;
Phil Edworthyb77188492014-06-30 08:54:23 +0100177 rcar_pci_write_reg(pcie, val, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100178}
179
180static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
181{
182 int shift = 8 * (where & 3);
Phil Edworthyb77188492014-06-30 08:54:23 +0100183 u32 val = rcar_pci_read_reg(pcie, where & ~3);
Phil Edworthyc25da472014-05-12 11:57:48 +0100184
185 return val >> shift;
186}
187
188/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
189static int rcar_pcie_config_access(struct rcar_pcie *pcie,
190 unsigned char access_type, struct pci_bus *bus,
191 unsigned int devfn, int where, u32 *data)
192{
193 int dev, func, reg, index;
194
195 dev = PCI_SLOT(devfn);
196 func = PCI_FUNC(devfn);
197 reg = where & ~3;
198 index = reg / 4;
199
200 /*
201 * While each channel has its own memory-mapped extended config
202 * space, it's generally only accessible when in endpoint mode.
203 * When in root complex mode, the controller is unable to target
204 * itself with either type 0 or type 1 accesses, and indeed, any
205 * controller initiated target transfer to its own config space
206 * result in a completer abort.
207 *
208 * Each channel effectively only supports a single device, but as
209 * the same channel <-> device access works for any PCI_SLOT()
210 * value, we cheat a bit here and bind the controller's config
211 * space to devfn 0 in order to enable self-enumeration. In this
212 * case the regular ECAR/ECDR path is sidelined and the mangled
213 * config access itself is initiated as an internal bus transaction.
214 */
215 if (pci_is_root_bus(bus)) {
216 if (dev != 0)
217 return PCIBIOS_DEVICE_NOT_FOUND;
218
Phil Edworthyb77188492014-06-30 08:54:23 +0100219 if (access_type == RCAR_PCI_ACCESS_READ) {
220 *data = rcar_pci_read_reg(pcie, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100221 } else {
222 /* Keep an eye out for changes to the root bus number */
223 if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
224 pcie->root_bus_nr = *data & 0xff;
225
Phil Edworthyb77188492014-06-30 08:54:23 +0100226 rcar_pci_write_reg(pcie, *data, PCICONF(index));
Phil Edworthyc25da472014-05-12 11:57:48 +0100227 }
228
229 return PCIBIOS_SUCCESSFUL;
230 }
231
232 if (pcie->root_bus_nr < 0)
233 return PCIBIOS_DEVICE_NOT_FOUND;
234
235 /* Clear errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100236 rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100237
238 /* Set the PIO address */
Phil Edworthyb77188492014-06-30 08:54:23 +0100239 rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
240 PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100241
242 /* Enable the configuration access */
243 if (bus->parent->number == pcie->root_bus_nr)
Phil Edworthyb77188492014-06-30 08:54:23 +0100244 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100245 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100246 rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100247
248 /* Check for errors */
Phil Edworthyb77188492014-06-30 08:54:23 +0100249 if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
Phil Edworthyc25da472014-05-12 11:57:48 +0100250 return PCIBIOS_DEVICE_NOT_FOUND;
251
252 /* Check for master and target aborts */
253 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
254 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
255 return PCIBIOS_DEVICE_NOT_FOUND;
256
Phil Edworthyb77188492014-06-30 08:54:23 +0100257 if (access_type == RCAR_PCI_ACCESS_READ)
258 *data = rcar_pci_read_reg(pcie, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100259 else
Phil Edworthyb77188492014-06-30 08:54:23 +0100260 rcar_pci_write_reg(pcie, *data, PCIECDR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100261
262 /* Disable the configuration access */
Phil Edworthyb77188492014-06-30 08:54:23 +0100263 rcar_pci_write_reg(pcie, 0, PCIECCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100264
265 return PCIBIOS_SUCCESSFUL;
266}
267
268static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
269 int where, int size, u32 *val)
270{
Phil Edworthy79953dd2015-10-02 11:25:05 +0100271 struct rcar_pcie *pcie = bus->sysdata;
Phil Edworthyc25da472014-05-12 11:57:48 +0100272 int ret;
273
Phil Edworthyb77188492014-06-30 08:54:23 +0100274 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100275 bus, devfn, where, val);
276 if (ret != PCIBIOS_SUCCESSFUL) {
277 *val = 0xffffffff;
278 return ret;
279 }
280
281 if (size == 1)
282 *val = (*val >> (8 * (where & 3))) & 0xff;
283 else if (size == 2)
284 *val = (*val >> (8 * (where & 2))) & 0xffff;
285
Ryan Desfosses227f0642014-04-18 20:13:50 -0400286 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
287 bus->number, devfn, where, size, (unsigned long)*val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100288
289 return ret;
290}
291
292/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
293static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
294 int where, int size, u32 val)
295{
Phil Edworthy79953dd2015-10-02 11:25:05 +0100296 struct rcar_pcie *pcie = bus->sysdata;
Phil Edworthyc25da472014-05-12 11:57:48 +0100297 int shift, ret;
298 u32 data;
299
Phil Edworthyb77188492014-06-30 08:54:23 +0100300 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
Phil Edworthyc25da472014-05-12 11:57:48 +0100301 bus, devfn, where, &data);
302 if (ret != PCIBIOS_SUCCESSFUL)
303 return ret;
304
Ryan Desfosses227f0642014-04-18 20:13:50 -0400305 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
306 bus->number, devfn, where, size, (unsigned long)val);
Phil Edworthyc25da472014-05-12 11:57:48 +0100307
308 if (size == 1) {
309 shift = 8 * (where & 3);
310 data &= ~(0xff << shift);
311 data |= ((val & 0xff) << shift);
312 } else if (size == 2) {
313 shift = 8 * (where & 2);
314 data &= ~(0xffff << shift);
315 data |= ((val & 0xffff) << shift);
316 } else
317 data = val;
318
Phil Edworthyb77188492014-06-30 08:54:23 +0100319 ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
Phil Edworthyc25da472014-05-12 11:57:48 +0100320 bus, devfn, where, &data);
321
322 return ret;
323}
324
325static struct pci_ops rcar_pcie_ops = {
326 .read = rcar_pcie_read_conf,
327 .write = rcar_pcie_write_conf,
328};
329
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000330static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie,
Andrew Murrayf84bd312019-10-04 14:29:41 +0100331 struct resource_entry *window)
Phil Edworthyc25da472014-05-12 11:57:48 +0100332{
333 /* Setup PCIe address space mappings for each resource */
334 resource_size_t size;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100335 resource_size_t res_start;
Andrew Murrayf84bd312019-10-04 14:29:41 +0100336 struct resource *res = window->res;
Phil Edworthyc25da472014-05-12 11:57:48 +0100337 u32 mask;
338
Phil Edworthyb77188492014-06-30 08:54:23 +0100339 rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100340
341 /*
342 * The PAMR mask is calculated in units of 128Bytes, which
343 * keeps things pretty simple.
344 */
345 size = resource_size(res);
346 mask = (roundup_pow_of_two(size) / SZ_128) - 1;
Phil Edworthyb77188492014-06-30 08:54:23 +0100347 rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100348
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100349 if (res->flags & IORESOURCE_IO)
Andrew Murrayf84bd312019-10-04 14:29:41 +0100350 res_start = pci_pio_to_address(res->start) - window->offset;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100351 else
Andrew Murrayf84bd312019-10-04 14:29:41 +0100352 res_start = res->start - window->offset;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100353
Nobuhiro Iwamatsuecd06302015-02-04 18:02:55 +0900354 rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win));
Nobuhiro Iwamatsu2ea2a272015-02-02 14:09:58 +0900355 rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
Nobuhiro Iwamatsuecd06302015-02-04 18:02:55 +0900356 PCIEPALR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100357
358 /* First resource is for IO */
359 mask = PAR_ENABLE;
360 if (res->flags & IORESOURCE_IO)
361 mask |= IO_SPACE;
362
Phil Edworthyb77188492014-06-30 08:54:23 +0100363 rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
Phil Edworthyc25da472014-05-12 11:57:48 +0100364}
365
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000366static int rcar_pcie_setup(struct list_head *resource, struct rcar_pcie *pci)
Phil Edworthyc25da472014-05-12 11:57:48 +0100367{
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000368 struct resource_entry *win;
369 int i = 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100370
371 /* Setup PCI resources */
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000372 resource_list_for_each_entry(win, &pci->resources) {
373 struct resource *res = win->res;
Phil Edworthyc25da472014-05-12 11:57:48 +0100374
Phil Edworthyc25da472014-05-12 11:57:48 +0100375 if (!res->flags)
376 continue;
377
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000378 switch (resource_type(res)) {
379 case IORESOURCE_IO:
380 case IORESOURCE_MEM:
Andrew Murrayf84bd312019-10-04 14:29:41 +0100381 rcar_pcie_setup_window(i, pci, win);
Phil Edworthy5d2917d2015-11-25 15:30:37 +0000382 i++;
383 break;
384 case IORESOURCE_BUS:
385 pci->root_bus_nr = res->start;
386 break;
387 default:
388 continue;
Phil Edworthyd0c3f4d2015-10-02 11:25:04 +0100389 }
390
Phil Edworthy79953dd2015-10-02 11:25:05 +0100391 pci_add_resource(resource, res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100392 }
Phil Edworthyc25da472014-05-12 11:57:48 +0100393
394 return 1;
395}
396
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300397static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
398{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500399 struct device *dev = pcie->dev;
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300400 unsigned int timeout = 1000;
401 u32 macsr;
402
403 if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
404 return;
405
406 if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500407 dev_err(dev, "Speed change already in progress\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300408 return;
409 }
410
411 macsr = rcar_pci_read_reg(pcie, MACSR);
412 if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
413 goto done;
414
415 /* Set target link speed to 5.0 GT/s */
416 rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
417 PCI_EXP_LNKSTA_CLS_5_0GB);
418
419 /* Set speed change reason as intentional factor */
420 rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
421
422 /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
423 if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
424 rcar_pci_write_reg(pcie, macsr, MACSR);
425
426 /* Start link speed change */
427 rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
428
429 while (timeout--) {
430 macsr = rcar_pci_read_reg(pcie, MACSR);
431 if (macsr & SPCHGFIN) {
432 /* Clear the interrupt bits */
433 rcar_pci_write_reg(pcie, macsr, MACSR);
434
435 if (macsr & SPCHGFAIL)
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500436 dev_err(dev, "Speed change failed\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300437
438 goto done;
439 }
440
441 msleep(1);
442 };
443
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500444 dev_err(dev, "Speed change timed out\n");
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300445
446done:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500447 dev_info(dev, "Current link speed is %s GT/s\n",
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300448 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
449}
450
Phil Edworthy79953dd2015-10-02 11:25:05 +0100451static int rcar_pcie_enable(struct rcar_pcie *pcie)
Phil Edworthyc25da472014-05-12 11:57:48 +0100452{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500453 struct device *dev = pcie->dev;
Phil Edworthy79953dd2015-10-02 11:25:05 +0100454 struct pci_bus *bus, *child;
455 LIST_HEAD(res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100456
Sergei Shtylyovb3327f72016-09-22 23:20:18 +0300457 /* Try setting 5 GT/s link speed */
458 rcar_pcie_force_speedup(pcie);
459
Phil Edworthy8c53e8e2015-10-02 11:25:07 +0100460 rcar_pcie_setup(&res, pcie);
Phil Edworthyc25da472014-05-12 11:57:48 +0100461
Lorenzo Pieralisi3487c652016-01-29 11:29:31 +0000462 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100463
464 if (IS_ENABLED(CONFIG_PCI_MSI))
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500465 bus = pci_scan_root_bus_msi(dev, pcie->root_bus_nr,
Phil Edworthy79953dd2015-10-02 11:25:05 +0100466 &rcar_pcie_ops, pcie, &res, &pcie->msi.chip);
467 else
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500468 bus = pci_scan_root_bus(dev, pcie->root_bus_nr,
Phil Edworthy79953dd2015-10-02 11:25:05 +0100469 &rcar_pcie_ops, pcie, &res);
470
471 if (!bus) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500472 dev_err(dev, "Scanning rootbus failed");
Phil Edworthy79953dd2015-10-02 11:25:05 +0100473 return -ENODEV;
474 }
475
476 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
477
Lorenzo Pieralisi3487c652016-01-29 11:29:31 +0000478 pci_bus_size_bridges(bus);
479 pci_bus_assign_resources(bus);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100480
Lorenzo Pieralisi3487c652016-01-29 11:29:31 +0000481 list_for_each_entry(child, &bus->children, node)
482 pcie_bus_configure_settings(child);
Phil Edworthy79953dd2015-10-02 11:25:05 +0100483
484 pci_bus_add_devices(bus);
485
486 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +0100487}
488
489static int phy_wait_for_ack(struct rcar_pcie *pcie)
490{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500491 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100492 unsigned int timeout = 100;
493
494 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100495 if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
Phil Edworthyc25da472014-05-12 11:57:48 +0100496 return 0;
497
498 udelay(100);
499 }
500
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500501 dev_err(dev, "Access to PCIe phy timed out\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100502
503 return -ETIMEDOUT;
504}
505
506static void phy_write_reg(struct rcar_pcie *pcie,
507 unsigned int rate, unsigned int addr,
508 unsigned int lane, unsigned int data)
509{
510 unsigned long phyaddr;
511
512 phyaddr = WRITE_CMD |
513 ((rate & 1) << RATE_POS) |
514 ((lane & 0xf) << LANE_POS) |
515 ((addr & 0xff) << ADR_POS);
516
517 /* Set write data */
Phil Edworthyb77188492014-06-30 08:54:23 +0100518 rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
519 rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100520
521 /* Ignore errors as they will be dealt with if the data link is down */
522 phy_wait_for_ack(pcie);
523
524 /* Clear command */
Phil Edworthyb77188492014-06-30 08:54:23 +0100525 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
526 rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100527
528 /* Ignore errors as they will be dealt with if the data link is down */
529 phy_wait_for_ack(pcie);
530}
531
532static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
533{
534 unsigned int timeout = 10;
535
536 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100537 if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
Phil Edworthyc25da472014-05-12 11:57:48 +0100538 return 0;
539
540 msleep(5);
541 }
542
543 return -ETIMEDOUT;
544}
545
546static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
547{
548 int err;
549
550 /* Begin initialization */
Phil Edworthyb77188492014-06-30 08:54:23 +0100551 rcar_pci_write_reg(pcie, 0, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100552
553 /* Set mode */
Phil Edworthyb77188492014-06-30 08:54:23 +0100554 rcar_pci_write_reg(pcie, 1, PCIEMSR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100555
556 /*
557 * Initial header for port config space is type 1, set the device
558 * class to match. Hardware takes care of propagating the IDSETR
559 * settings, so there is no need to bother with a quirk.
560 */
Phil Edworthyb77188492014-06-30 08:54:23 +0100561 rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
Phil Edworthyc25da472014-05-12 11:57:48 +0100562
563 /*
564 * Setup Secondary Bus Number & Subordinate Bus Number, even though
565 * they aren't used, to avoid bridge being detected as broken.
566 */
567 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
568 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
569
570 /* Initialize default capabilities. */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100571 rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
Phil Edworthyc25da472014-05-12 11:57:48 +0100572 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
573 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
574 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
575 PCI_HEADER_TYPE_BRIDGE);
576
577 /* Enable data link layer active state reporting */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100578 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
579 PCI_EXP_LNKCAP_DLLLARC);
Phil Edworthyc25da472014-05-12 11:57:48 +0100580
581 /* Write out the physical slot number = 0 */
582 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
583
584 /* Set the completion timer timeout to the maximum 50ms. */
Phil Edworthyb77188492014-06-30 08:54:23 +0100585 rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
Phil Edworthyc25da472014-05-12 11:57:48 +0100586
587 /* Terminate list of capabilities (Next Capability Offset=0) */
Phil Edworthy2c3fd4c2014-06-30 08:54:22 +0100588 rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
Phil Edworthyc25da472014-05-12 11:57:48 +0100589
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100590 /* Enable MSI */
591 if (IS_ENABLED(CONFIG_PCI_MSI))
Nobuhiro Iwamatsu1fc6aa92015-02-02 14:09:39 +0900592 rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100593
Phil Edworthyc25da472014-05-12 11:57:48 +0100594 /* Finish initialization - establish a PCI Express link */
Phil Edworthyb77188492014-06-30 08:54:23 +0100595 rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
Phil Edworthyc25da472014-05-12 11:57:48 +0100596
597 /* This will timeout if we don't have a link. */
598 err = rcar_pcie_wait_for_dl(pcie);
599 if (err)
600 return err;
601
602 /* Enable INTx interrupts */
603 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
604
Phil Edworthyc25da472014-05-12 11:57:48 +0100605 wmb();
606
607 return 0;
608}
609
610static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
611{
612 unsigned int timeout = 10;
613
614 /* Initialize the phy */
615 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
616 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
617 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
618 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
619 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
620 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
621 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
622 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
623 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
624 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
625 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
626 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
627
628 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
629 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
630 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
631
632 while (timeout--) {
Phil Edworthyb77188492014-06-30 08:54:23 +0100633 if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
Phil Edworthyc25da472014-05-12 11:57:48 +0100634 return rcar_pcie_hw_init(pcie);
635
636 msleep(5);
637 }
638
639 return -ETIMEDOUT;
640}
641
Phil Edworthy581d9432016-01-05 13:00:31 +0000642static int rcar_pcie_hw_init_gen2(struct rcar_pcie *pcie)
643{
644 /*
645 * These settings come from the R-Car Series, 2nd Generation User's
646 * Manual, section 50.3.1 (2) Initialization of the physical layer.
647 */
648 rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
649 rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
650 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
651 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
652
653 rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
654 /* The following value is for DC connection, no termination resistor */
655 rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
656 rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
657 rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
658
659 return rcar_pcie_hw_init(pcie);
660}
661
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100662static int rcar_msi_alloc(struct rcar_msi *chip)
663{
664 int msi;
665
666 mutex_lock(&chip->lock);
667
668 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
669 if (msi < INT_PCI_MSI_NR)
670 set_bit(msi, chip->used);
671 else
672 msi = -ENOSPC;
673
674 mutex_unlock(&chip->lock);
675
676 return msi;
677}
678
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300679static int rcar_msi_alloc_region(struct rcar_msi *chip, int no_irqs)
680{
681 int msi;
682
683 mutex_lock(&chip->lock);
684 msi = bitmap_find_free_region(chip->used, INT_PCI_MSI_NR,
685 order_base_2(no_irqs));
686 mutex_unlock(&chip->lock);
687
688 return msi;
689}
690
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100691static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
692{
693 mutex_lock(&chip->lock);
694 clear_bit(irq, chip->used);
695 mutex_unlock(&chip->lock);
696}
697
698static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
699{
700 struct rcar_pcie *pcie = data;
701 struct rcar_msi *msi = &pcie->msi;
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500702 struct device *dev = pcie->dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100703 unsigned long reg;
704
Phil Edworthyb77188492014-06-30 08:54:23 +0100705 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100706
707 /* MSI & INTx share an interrupt - we only handle MSI here */
708 if (!reg)
709 return IRQ_NONE;
710
711 while (reg) {
712 unsigned int index = find_first_bit(&reg, 32);
713 unsigned int irq;
714
715 /* clear the interrupt */
Phil Edworthyb77188492014-06-30 08:54:23 +0100716 rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100717
718 irq = irq_find_mapping(msi->domain, index);
719 if (irq) {
720 if (test_bit(index, msi->used))
721 generic_handle_irq(irq);
722 else
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500723 dev_info(dev, "unhandled MSI\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100724 } else {
725 /* Unknown MSI, just clear it */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500726 dev_dbg(dev, "unexpected MSI\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100727 }
728
729 /* see if there's any more pending in this vector */
Phil Edworthyb77188492014-06-30 08:54:23 +0100730 reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100731 }
732
733 return IRQ_HANDLED;
734}
735
Yijing Wangc2791b82014-11-11 17:45:45 -0700736static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100737 struct msi_desc *desc)
738{
739 struct rcar_msi *msi = to_rcar_msi(chip);
740 struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
741 struct msi_msg msg;
742 unsigned int irq;
743 int hwirq;
744
745 hwirq = rcar_msi_alloc(msi);
746 if (hwirq < 0)
747 return hwirq;
748
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300749 irq = irq_find_mapping(msi->domain, hwirq);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100750 if (!irq) {
751 rcar_msi_free(msi, hwirq);
752 return -EINVAL;
753 }
754
755 irq_set_msi_desc(irq, desc);
756
Phil Edworthyb77188492014-06-30 08:54:23 +0100757 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
758 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100759 msg.data = hwirq;
760
Jiang Liu83a18912014-11-09 23:10:34 +0800761 pci_write_msi_msg(irq, &msg);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100762
763 return 0;
764}
765
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300766static int rcar_msi_setup_irqs(struct msi_controller *chip,
767 struct pci_dev *pdev, int nvec, int type)
768{
769 struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
770 struct rcar_msi *msi = to_rcar_msi(chip);
771 struct msi_desc *desc;
772 struct msi_msg msg;
773 unsigned int irq;
774 int hwirq;
775 int i;
776
777 /* MSI-X interrupts are not supported */
778 if (type == PCI_CAP_ID_MSIX)
779 return -EINVAL;
780
781 WARN_ON(!list_is_singular(&pdev->dev.msi_list));
782 desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
783
784 hwirq = rcar_msi_alloc_region(msi, nvec);
785 if (hwirq < 0)
786 return -ENOSPC;
787
788 irq = irq_find_mapping(msi->domain, hwirq);
789 if (!irq)
790 return -ENOSPC;
791
792 for (i = 0; i < nvec; i++) {
793 /*
794 * irq_create_mapping() called from rcar_pcie_probe() pre-
795 * allocates descs, so there is no need to allocate descs here.
796 * We can therefore assume that if irq_find_mapping() above
797 * returns non-zero, then the descs are also successfully
798 * allocated.
799 */
800 if (irq_set_msi_desc_off(irq, i, desc)) {
801 /* TODO: clear */
802 return -EINVAL;
803 }
804 }
805
806 desc->nvec_used = nvec;
807 desc->msi_attrib.multiple = order_base_2(nvec);
808
809 msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
810 msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
811 msg.data = hwirq;
812
813 pci_write_msi_msg(irq, &msg);
814
815 return 0;
816}
817
Yijing Wangc2791b82014-11-11 17:45:45 -0700818static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100819{
820 struct rcar_msi *msi = to_rcar_msi(chip);
821 struct irq_data *d = irq_get_irq_data(irq);
822
823 rcar_msi_free(msi, d->hwirq);
824}
825
826static struct irq_chip rcar_msi_irq_chip = {
827 .name = "R-Car PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100828 .irq_enable = pci_msi_unmask_irq,
829 .irq_disable = pci_msi_mask_irq,
830 .irq_mask = pci_msi_mask_irq,
831 .irq_unmask = pci_msi_unmask_irq,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100832};
833
834static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
835 irq_hw_number_t hwirq)
836{
837 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
838 irq_set_chip_data(irq, domain->host_data);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100839
840 return 0;
841}
842
843static const struct irq_domain_ops msi_domain_ops = {
844 .map = rcar_msi_map,
845};
846
847static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
848{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500849 struct device *dev = pcie->dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100850 struct rcar_msi *msi = &pcie->msi;
Marek Vasuta0bb0592019-03-25 12:41:01 +0100851 phys_addr_t base;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300852 int err, i;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100853
854 mutex_init(&msi->lock);
855
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500856 msi->chip.dev = dev;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100857 msi->chip.setup_irq = rcar_msi_setup_irq;
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300858 msi->chip.setup_irqs = rcar_msi_setup_irqs;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100859 msi->chip.teardown_irq = rcar_msi_teardown_irq;
860
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500861 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100862 &msi_domain_ops, &msi->chip);
863 if (!msi->domain) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500864 dev_err(dev, "failed to create IRQ domain\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100865 return -ENOMEM;
866 }
867
Grigory Kletskoe3123c22016-09-08 22:32:59 +0300868 for (i = 0; i < INT_PCI_MSI_NR; i++)
869 irq_create_mapping(msi->domain, i);
870
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100871 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500872 err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200873 IRQF_SHARED | IRQF_NO_THREAD,
874 rcar_msi_irq_chip.name, pcie);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100875 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500876 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100877 goto err;
878 }
879
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500880 err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200881 IRQF_SHARED | IRQF_NO_THREAD,
882 rcar_msi_irq_chip.name, pcie);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100883 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500884 dev_err(dev, "failed to request IRQ: %d\n", err);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100885 goto err;
886 }
887
888 /* setup MSI data target */
889 msi->pages = __get_free_pages(GFP_KERNEL, 0);
Kangjie Lufd217932019-03-15 02:29:43 -0500890 if (!msi->pages) {
891 err = -ENOMEM;
892 goto err;
893 }
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100894 base = virt_to_phys((void *)msi->pages);
895
Marek Vasuta0bb0592019-03-25 12:41:01 +0100896 rcar_pci_write_reg(pcie, lower_32_bits(base) | MSIFE, PCIEMSIALR);
897 rcar_pci_write_reg(pcie, upper_32_bits(base), PCIEMSIAUR);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100898
899 /* enable all MSI interrupts */
Phil Edworthyb77188492014-06-30 08:54:23 +0100900 rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100901
902 return 0;
903
904err:
905 irq_domain_remove(msi->domain);
906 return err;
907}
908
Bjorn Helgaasd0206f22016-10-06 13:40:27 -0500909static int rcar_pcie_get_resources(struct rcar_pcie *pcie)
Phil Edworthyc25da472014-05-12 11:57:48 +0100910{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500911 struct device *dev = pcie->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +0100912 struct resource res;
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100913 int err, i;
Phil Edworthyc25da472014-05-12 11:57:48 +0100914
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500915 err = of_address_to_resource(dev->of_node, 0, &res);
Phil Edworthyc25da472014-05-12 11:57:48 +0100916 if (err)
917 return err;
918
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500919 pcie->base = devm_ioremap_resource(dev, &res);
Bjorn Helgaas51afa3c2016-08-22 14:16:38 -0500920 if (IS_ERR(pcie->base))
921 return PTR_ERR(pcie->base);
922
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500923 pcie->clk = devm_clk_get(dev, "pcie");
Phil Edworthyc25da472014-05-12 11:57:48 +0100924 if (IS_ERR(pcie->clk)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500925 dev_err(dev, "cannot get platform clock\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100926 return PTR_ERR(pcie->clk);
927 }
928 err = clk_prepare_enable(pcie->clk);
929 if (err)
Geert Uytterhoeven3d664b02016-08-31 11:28:22 +0200930 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +0100931
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500932 pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
Phil Edworthyc25da472014-05-12 11:57:48 +0100933 if (IS_ERR(pcie->bus_clk)) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500934 dev_err(dev, "cannot get pcie bus clock\n");
Phil Edworthyc25da472014-05-12 11:57:48 +0100935 err = PTR_ERR(pcie->bus_clk);
936 goto fail_clk;
937 }
938 err = clk_prepare_enable(pcie->bus_clk);
939 if (err)
Geert Uytterhoeven3d664b02016-08-31 11:28:22 +0200940 goto fail_clk;
Phil Edworthyc25da472014-05-12 11:57:48 +0100941
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500942 i = irq_of_parse_and_map(dev->of_node, 0);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800943 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500944 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100945 err = -ENOENT;
946 goto err_map_reg;
947 }
948 pcie->msi.irq1 = i;
949
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500950 i = irq_of_parse_and_map(dev->of_node, 1);
Dmitry Torokhovc51d4112014-11-14 14:21:53 -0800951 if (!i) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -0500952 dev_err(dev, "cannot get platform resources for msi interrupt\n");
Phil Edworthy290c1fb2014-05-12 11:57:49 +0100953 err = -ENOENT;
954 goto err_map_reg;
955 }
956 pcie->msi.irq2 = i;
957
Phil Edworthyc25da472014-05-12 11:57:48 +0100958 return 0;
959
960err_map_reg:
961 clk_disable_unprepare(pcie->bus_clk);
962fail_clk:
963 clk_disable_unprepare(pcie->clk);
964
965 return err;
966}
967
968static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
969 struct of_pci_range *range,
970 int *index)
971{
972 u64 restype = range->flags;
973 u64 cpu_addr = range->cpu_addr;
974 u64 cpu_end = range->cpu_addr + range->size;
975 u64 pci_addr = range->pci_addr;
976 u32 flags = LAM_64BIT | LAR_ENABLE;
977 u64 mask;
978 u64 size;
979 int idx = *index;
980
981 if (restype & IORESOURCE_PREFETCH)
982 flags |= LAM_PREFETCH;
983
984 /*
985 * If the size of the range is larger than the alignment of the start
986 * address, we have to use multiple entries to perform the mapping.
987 */
988 if (cpu_addr > 0) {
989 unsigned long nr_zeros = __ffs64(cpu_addr);
990 u64 alignment = 1ULL << nr_zeros;
Phil Edworthyb77188492014-06-30 08:54:23 +0100991
Phil Edworthyc25da472014-05-12 11:57:48 +0100992 size = min(range->size, alignment);
993 } else {
994 size = range->size;
995 }
996 /* Hardware supports max 4GiB inbound region */
997 size = min(size, 1ULL << 32);
998
999 mask = roundup_pow_of_two(size) - 1;
1000 mask &= ~0xf;
1001
1002 while (cpu_addr < cpu_end) {
1003 /*
1004 * Set up 64-bit inbound regions as the range parser doesn't
1005 * distinguish between 32 and 64-bit types.
1006 */
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001007 rcar_pci_write_reg(pcie, lower_32_bits(pci_addr),
1008 PCIEPRAR(idx));
Phil Edworthyb77188492014-06-30 08:54:23 +01001009 rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001010 rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags,
1011 PCIELAMR(idx));
Phil Edworthyc25da472014-05-12 11:57:48 +01001012
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001013 rcar_pci_write_reg(pcie, upper_32_bits(pci_addr),
1014 PCIEPRAR(idx + 1));
1015 rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr),
1016 PCIELAR(idx + 1));
Phil Edworthyb77188492014-06-30 08:54:23 +01001017 rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
Phil Edworthyc25da472014-05-12 11:57:48 +01001018
1019 pci_addr += size;
1020 cpu_addr += size;
1021 idx += 2;
1022
1023 if (idx > MAX_NR_INBOUND_MAPS) {
1024 dev_err(pcie->dev, "Failed to map inbound regions!\n");
1025 return -EINVAL;
1026 }
1027 }
1028 *index = idx;
1029
1030 return 0;
1031}
1032
1033static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
1034 struct device_node *node)
1035{
1036 const int na = 3, ns = 2;
1037 int rlen;
1038
1039 parser->node = node;
1040 parser->pna = of_n_addr_cells(node);
1041 parser->np = parser->pna + na + ns;
1042
1043 parser->range = of_get_property(node, "dma-ranges", &rlen);
1044 if (!parser->range)
1045 return -ENOENT;
1046
1047 parser->end = parser->range + rlen / sizeof(__be32);
1048 return 0;
1049}
1050
1051static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
1052 struct device_node *np)
1053{
1054 struct of_pci_range range;
1055 struct of_pci_range_parser parser;
1056 int index = 0;
1057 int err;
1058
1059 if (pci_dma_range_parser_init(&parser, np))
1060 return -EINVAL;
1061
1062 /* Get the dma-ranges from DT */
1063 for_each_of_pci_range(&parser, &range) {
1064 u64 end = range.cpu_addr + range.size - 1;
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001065
Phil Edworthyc25da472014-05-12 11:57:48 +01001066 dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
1067 range.flags, range.cpu_addr, end, range.pci_addr);
1068
1069 err = rcar_pcie_inbound_ranges(pcie, &range, &index);
1070 if (err)
1071 return err;
1072 }
1073
1074 return 0;
1075}
1076
1077static const struct of_device_id rcar_pcie_of_match[] = {
1078 { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001079 { .compatible = "renesas,pcie-rcar-gen2",
1080 .data = rcar_pcie_hw_init_gen2 },
1081 { .compatible = "renesas,pcie-r8a7790",
1082 .data = rcar_pcie_hw_init_gen2 },
1083 { .compatible = "renesas,pcie-r8a7791",
1084 .data = rcar_pcie_hw_init_gen2 },
Harunobu Kurokawae015f882015-11-25 15:30:39 +00001085 { .compatible = "renesas,pcie-r8a7795", .data = rcar_pcie_hw_init },
Phil Edworthyc25da472014-05-12 11:57:48 +01001086 {},
1087};
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001088
1089static int rcar_pcie_parse_request_of_pci_ranges(struct rcar_pcie *pci)
1090{
1091 int err;
1092 struct device *dev = pci->dev;
1093 struct device_node *np = dev->of_node;
1094 resource_size_t iobase;
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001095 struct resource_entry *win, *tmp;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001096
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001097 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
1098 &iobase);
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001099 if (err)
1100 return err;
1101
Bjorn Helgaas6fd7f552016-05-31 12:20:57 -05001102 err = devm_request_pci_bus_resources(dev, &pci->resources);
1103 if (err)
1104 goto out_release_res;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001105
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001106 resource_list_for_each_entry_safe(win, tmp, &pci->resources) {
Bjorn Helgaas6fd7f552016-05-31 12:20:57 -05001107 struct resource *res = win->res;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001108
Bjorn Helgaas4c540a32016-05-28 18:37:46 -05001109 if (resource_type(res) == IORESOURCE_IO) {
Sergei Shtylyov3934e012018-07-18 15:40:26 -05001110 err = devm_pci_remap_iospace(dev, res, iobase);
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001111 if (err) {
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001112 dev_warn(dev, "error %d: failed to map resource %pR\n",
1113 err, res);
Lorenzo Pieralisi5e8c8732016-08-15 17:50:44 +01001114
1115 resource_list_destroy_entry(win);
1116 }
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001117 }
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001118 }
1119
1120 return 0;
1121
1122out_release_res:
Bjorn Helgaas4c540a32016-05-28 18:37:46 -05001123 pci_free_resource_list(&pci->resources);
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001124 return err;
1125}
1126
Phil Edworthyc25da472014-05-12 11:57:48 +01001127static int rcar_pcie_probe(struct platform_device *pdev)
1128{
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001129 struct device *dev = &pdev->dev;
Phil Edworthyc25da472014-05-12 11:57:48 +01001130 struct rcar_pcie *pcie;
1131 unsigned int data;
Phil Edworthyc25da472014-05-12 11:57:48 +01001132 const struct of_device_id *of_id;
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001133 int err;
Phil Edworthyc25da472014-05-12 11:57:48 +01001134 int (*hw_init_fn)(struct rcar_pcie *);
1135
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001136 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Phil Edworthyc25da472014-05-12 11:57:48 +01001137 if (!pcie)
1138 return -ENOMEM;
1139
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001140 pcie->dev = dev;
Phil Edworthyc25da472014-05-12 11:57:48 +01001141
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001142 INIT_LIST_HEAD(&pcie->resources);
Phil Edworthyc25da472014-05-12 11:57:48 +01001143
Phil Edworthy5d2917d2015-11-25 15:30:37 +00001144 rcar_pcie_parse_request_of_pci_ranges(pcie);
Phil Edworthyc25da472014-05-12 11:57:48 +01001145
Bjorn Helgaasd0206f22016-10-06 13:40:27 -05001146 err = rcar_pcie_get_resources(pcie);
Phil Edworthyc25da472014-05-12 11:57:48 +01001147 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001148 dev_err(dev, "failed to request resources: %d\n", err);
Phil Edworthyc25da472014-05-12 11:57:48 +01001149 return err;
1150 }
1151
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001152 err = rcar_pcie_parse_map_dma_ranges(pcie, dev->of_node);
Sergei Shtylyovf7bc6382016-09-09 01:26:18 +03001153 if (err)
Phil Edworthyc25da472014-05-12 11:57:48 +01001154 return err;
1155
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001156 of_id = of_match_device(rcar_pcie_of_match, dev);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001157 if (!of_id || !of_id->data)
1158 return -EINVAL;
1159 hw_init_fn = of_id->data;
1160
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001161 pm_runtime_enable(dev);
1162 err = pm_runtime_get_sync(dev);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001163 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001164 dev_err(dev, "pm_runtime_get_sync failed\n");
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001165 goto err_pm_disable;
1166 }
1167
1168 /* Failure to get a link might just be that no cards are inserted */
1169 err = hw_init_fn(pcie);
1170 if (err) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001171 dev_info(dev, "PCIe link down\n");
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001172 err = 0;
1173 goto err_pm_put;
1174 }
1175
1176 data = rcar_pci_read_reg(pcie, MACSR);
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001177 dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001178
Phil Edworthy290c1fb2014-05-12 11:57:49 +01001179 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1180 err = rcar_pcie_enable_msi(pcie);
1181 if (err < 0) {
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001182 dev_err(dev,
Phil Edworthy290c1fb2014-05-12 11:57:49 +01001183 "failed to enable MSI support: %d\n",
1184 err);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001185 goto err_pm_put;
Phil Edworthy290c1fb2014-05-12 11:57:49 +01001186 }
1187 }
1188
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001189 err = rcar_pcie_enable(pcie);
1190 if (err)
1191 goto err_pm_put;
Phil Edworthyc25da472014-05-12 11:57:48 +01001192
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001193 return 0;
Phil Edworthyc25da472014-05-12 11:57:48 +01001194
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001195err_pm_put:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001196 pm_runtime_put(dev);
Phil Edworthyc25da472014-05-12 11:57:48 +01001197
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001198err_pm_disable:
Bjorn Helgaas4ef80d72016-10-10 14:31:28 -05001199 pm_runtime_disable(dev);
Phil Edworthyde1be9a2016-01-05 13:00:30 +00001200 return err;
Phil Edworthyc25da472014-05-12 11:57:48 +01001201}
1202
1203static struct platform_driver rcar_pcie_driver = {
1204 .driver = {
Bjorn Helgaas3ff8e4a2016-10-06 13:40:28 -05001205 .name = "rcar-pcie",
Phil Edworthyc25da472014-05-12 11:57:48 +01001206 .of_match_table = rcar_pcie_of_match,
1207 .suppress_bind_attrs = true,
1208 },
1209 .probe = rcar_pcie_probe,
1210};
Paul Gortmaker42d10712016-07-22 16:23:21 -05001211builtin_platform_driver(rcar_pcie_driver);