blob: 26ddd3535272e89b9dcb9b1da48ee6acb6211666 [file] [log] [blame]
Shawn Line77f8472016-09-03 11:41:09 -05001/*
2 * Rockchip AXI PCIe host controller driver
3 *
4 * Copyright (c) 2016 Rockchip, Inc.
5 *
6 * Author: Shawn Lin <shawn.lin@rock-chips.com>
7 * Wenrui Li <wenrui.li@rock-chips.com>
8 *
9 * Bits taken from Synopsys Designware Host controller driver and
10 * ARM PCI Host generic driver.
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18#include <linux/clk.h>
19#include <linux/delay.h>
20#include <linux/gpio/consumer.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
Shawn Lin013dd3d2016-12-12 19:50:07 +080023#include <linux/iopoll.h>
Shawn Line77f8472016-09-03 11:41:09 -050024#include <linux/irq.h>
25#include <linux/irqchip/chained_irq.h>
26#include <linux/irqdomain.h>
27#include <linux/kernel.h>
28#include <linux/mfd/syscon.h>
29#include <linux/of_address.h>
30#include <linux/of_device.h>
31#include <linux/of_pci.h>
32#include <linux/of_platform.h>
33#include <linux/of_irq.h>
34#include <linux/pci.h>
35#include <linux/pci_ids.h>
36#include <linux/phy/phy.h>
37#include <linux/platform_device.h>
38#include <linux/reset.h>
39#include <linux/regmap.h>
40
41/*
42 * The upper 16 bits of PCIE_CLIENT_CONFIG are a write mask for the lower 16
43 * bits. This allows atomic updates of the register without locking.
44 */
45#define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val))
46#define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val)
47
48#define ENCODE_LANES(x) ((((x) >> 1) & 3) << 4)
49
50#define PCIE_CLIENT_BASE 0x0
51#define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00)
52#define PCIE_CLIENT_CONF_ENABLE HIWORD_UPDATE_BIT(0x0001)
53#define PCIE_CLIENT_LINK_TRAIN_ENABLE HIWORD_UPDATE_BIT(0x0002)
54#define PCIE_CLIENT_ARI_ENABLE HIWORD_UPDATE_BIT(0x0008)
55#define PCIE_CLIENT_CONF_LANE_NUM(x) HIWORD_UPDATE(0x0030, ENCODE_LANES(x))
56#define PCIE_CLIENT_MODE_RC HIWORD_UPDATE_BIT(0x0040)
Shawn Linf2fb5b82016-12-07 15:05:59 -060057#define PCIE_CLIENT_GEN_SEL_1 HIWORD_UPDATE(0x0080, 0)
Shawn Line77f8472016-09-03 11:41:09 -050058#define PCIE_CLIENT_GEN_SEL_2 HIWORD_UPDATE_BIT(0x0080)
Shawn Lin013dd3d2016-12-12 19:50:07 +080059#define PCIE_CLIENT_DEBUG_OUT_0 (PCIE_CLIENT_BASE + 0x3c)
60#define PCIE_CLIENT_DEBUG_LTSSM_MASK GENMASK(5, 0)
61#define PCIE_CLIENT_DEBUG_LTSSM_L1 0x18
62#define PCIE_CLIENT_DEBUG_LTSSM_L2 0x19
Shawn Line77f8472016-09-03 11:41:09 -050063#define PCIE_CLIENT_BASIC_STATUS1 (PCIE_CLIENT_BASE + 0x48)
64#define PCIE_CLIENT_LINK_STATUS_UP 0x00300000
65#define PCIE_CLIENT_LINK_STATUS_MASK 0x00300000
66#define PCIE_CLIENT_INT_MASK (PCIE_CLIENT_BASE + 0x4c)
67#define PCIE_CLIENT_INT_STATUS (PCIE_CLIENT_BASE + 0x50)
68#define PCIE_CLIENT_INTR_MASK GENMASK(8, 5)
69#define PCIE_CLIENT_INTR_SHIFT 5
70#define PCIE_CLIENT_INT_LEGACY_DONE BIT(15)
71#define PCIE_CLIENT_INT_MSG BIT(14)
72#define PCIE_CLIENT_INT_HOT_RST BIT(13)
73#define PCIE_CLIENT_INT_DPA BIT(12)
74#define PCIE_CLIENT_INT_FATAL_ERR BIT(11)
75#define PCIE_CLIENT_INT_NFATAL_ERR BIT(10)
76#define PCIE_CLIENT_INT_CORR_ERR BIT(9)
77#define PCIE_CLIENT_INT_INTD BIT(8)
78#define PCIE_CLIENT_INT_INTC BIT(7)
79#define PCIE_CLIENT_INT_INTB BIT(6)
80#define PCIE_CLIENT_INT_INTA BIT(5)
81#define PCIE_CLIENT_INT_LOCAL BIT(4)
82#define PCIE_CLIENT_INT_UDMA BIT(3)
83#define PCIE_CLIENT_INT_PHY BIT(2)
84#define PCIE_CLIENT_INT_HOT_PLUG BIT(1)
85#define PCIE_CLIENT_INT_PWR_STCG BIT(0)
86
87#define PCIE_CLIENT_INT_LEGACY \
88 (PCIE_CLIENT_INT_INTA | PCIE_CLIENT_INT_INTB | \
89 PCIE_CLIENT_INT_INTC | PCIE_CLIENT_INT_INTD)
90
91#define PCIE_CLIENT_INT_CLI \
92 (PCIE_CLIENT_INT_CORR_ERR | PCIE_CLIENT_INT_NFATAL_ERR | \
93 PCIE_CLIENT_INT_FATAL_ERR | PCIE_CLIENT_INT_DPA | \
94 PCIE_CLIENT_INT_HOT_RST | PCIE_CLIENT_INT_MSG | \
95 PCIE_CLIENT_INT_LEGACY_DONE | PCIE_CLIENT_INT_LEGACY | \
96 PCIE_CLIENT_INT_PHY)
97
98#define PCIE_CORE_CTRL_MGMT_BASE 0x900000
99#define PCIE_CORE_CTRL (PCIE_CORE_CTRL_MGMT_BASE + 0x000)
100#define PCIE_CORE_PL_CONF_SPEED_5G 0x00000008
101#define PCIE_CORE_PL_CONF_SPEED_MASK 0x00000018
102#define PCIE_CORE_PL_CONF_LANE_MASK 0x00000006
103#define PCIE_CORE_PL_CONF_LANE_SHIFT 1
Shawn Linca198902016-10-04 12:20:22 -0500104#define PCIE_CORE_CTRL_PLC1 (PCIE_CORE_CTRL_MGMT_BASE + 0x004)
105#define PCIE_CORE_CTRL_PLC1_FTS_MASK GENMASK(23, 8)
106#define PCIE_CORE_CTRL_PLC1_FTS_SHIFT 8
107#define PCIE_CORE_CTRL_PLC1_FTS_CNT 0xffff
Rajat Jain277743e2016-09-22 17:50:42 -0700108#define PCIE_CORE_TXCREDIT_CFG1 (PCIE_CORE_CTRL_MGMT_BASE + 0x020)
109#define PCIE_CORE_TXCREDIT_CFG1_MUI_MASK 0xFFFF0000
110#define PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT 16
111#define PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(x) \
112 (((x) >> 3) << PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT)
Shawn Line77f8472016-09-03 11:41:09 -0500113#define PCIE_CORE_INT_STATUS (PCIE_CORE_CTRL_MGMT_BASE + 0x20c)
114#define PCIE_CORE_INT_PRFPE BIT(0)
115#define PCIE_CORE_INT_CRFPE BIT(1)
116#define PCIE_CORE_INT_RRPE BIT(2)
117#define PCIE_CORE_INT_PRFO BIT(3)
118#define PCIE_CORE_INT_CRFO BIT(4)
119#define PCIE_CORE_INT_RT BIT(5)
120#define PCIE_CORE_INT_RTR BIT(6)
121#define PCIE_CORE_INT_PE BIT(7)
122#define PCIE_CORE_INT_MTR BIT(8)
123#define PCIE_CORE_INT_UCR BIT(9)
124#define PCIE_CORE_INT_FCE BIT(10)
125#define PCIE_CORE_INT_CT BIT(11)
126#define PCIE_CORE_INT_UTC BIT(18)
127#define PCIE_CORE_INT_MMVC BIT(19)
Shawn Lin58007902017-02-16 15:29:35 +0800128#define PCIE_CORE_CONFIG_VENDOR (PCIE_CORE_CTRL_MGMT_BASE + 0x44)
Shawn Line77f8472016-09-03 11:41:09 -0500129#define PCIE_CORE_INT_MASK (PCIE_CORE_CTRL_MGMT_BASE + 0x210)
130#define PCIE_RC_BAR_CONF (PCIE_CORE_CTRL_MGMT_BASE + 0x300)
131
132#define PCIE_CORE_INT \
133 (PCIE_CORE_INT_PRFPE | PCIE_CORE_INT_CRFPE | \
134 PCIE_CORE_INT_RRPE | PCIE_CORE_INT_CRFO | \
135 PCIE_CORE_INT_RT | PCIE_CORE_INT_RTR | \
136 PCIE_CORE_INT_PE | PCIE_CORE_INT_MTR | \
137 PCIE_CORE_INT_UCR | PCIE_CORE_INT_FCE | \
138 PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | \
139 PCIE_CORE_INT_MMVC)
140
141#define PCIE_RC_CONFIG_BASE 0xa00000
Shawn Line77f8472016-09-03 11:41:09 -0500142#define PCIE_RC_CONFIG_RID_CCR (PCIE_RC_CONFIG_BASE + 0x08)
143#define PCIE_RC_CONFIG_SCC_SHIFT 16
Shawn Lin4816c4c2016-12-07 15:05:58 -0600144#define PCIE_RC_CONFIG_DCR (PCIE_RC_CONFIG_BASE + 0xc4)
145#define PCIE_RC_CONFIG_DCR_CSPL_SHIFT 18
146#define PCIE_RC_CONFIG_DCR_CSPL_LIMIT 0xff
147#define PCIE_RC_CONFIG_DCR_CPLS_SHIFT 26
Shawn Linafc95952017-01-12 09:53:17 +0800148#define PCIE_RC_CONFIG_LINK_CAP (PCIE_RC_CONFIG_BASE + 0xcc)
149#define PCIE_RC_CONFIG_LINK_CAP_L0S BIT(10)
Shawn Line77f8472016-09-03 11:41:09 -0500150#define PCIE_RC_CONFIG_LCS (PCIE_RC_CONFIG_BASE + 0xd0)
Shawn Line77f8472016-09-03 11:41:09 -0500151#define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c)
Shawn Lin77bc68c2016-12-07 15:05:59 -0600152#define PCIE_RC_CONFIG_THP_CAP (PCIE_RC_CONFIG_BASE + 0x274)
153#define PCIE_RC_CONFIG_THP_CAP_NEXT_MASK GENMASK(31, 20)
Shawn Line77f8472016-09-03 11:41:09 -0500154
155#define PCIE_CORE_AXI_CONF_BASE 0xc00000
156#define PCIE_CORE_OB_REGION_ADDR0 (PCIE_CORE_AXI_CONF_BASE + 0x0)
157#define PCIE_CORE_OB_REGION_ADDR0_NUM_BITS 0x3f
158#define PCIE_CORE_OB_REGION_ADDR0_LO_ADDR 0xffffff00
159#define PCIE_CORE_OB_REGION_ADDR1 (PCIE_CORE_AXI_CONF_BASE + 0x4)
160#define PCIE_CORE_OB_REGION_DESC0 (PCIE_CORE_AXI_CONF_BASE + 0x8)
161#define PCIE_CORE_OB_REGION_DESC1 (PCIE_CORE_AXI_CONF_BASE + 0xc)
162
163#define PCIE_CORE_AXI_INBOUND_BASE 0xc00800
164#define PCIE_RP_IB_ADDR0 (PCIE_CORE_AXI_INBOUND_BASE + 0x0)
165#define PCIE_CORE_IB_REGION_ADDR0_NUM_BITS 0x3f
166#define PCIE_CORE_IB_REGION_ADDR0_LO_ADDR 0xffffff00
167#define PCIE_RP_IB_ADDR1 (PCIE_CORE_AXI_INBOUND_BASE + 0x4)
168
169/* Size of one AXI Region (not Region 0) */
170#define AXI_REGION_SIZE BIT(20)
171/* Size of Region 0, equal to sum of sizes of other regions */
172#define AXI_REGION_0_SIZE (32 * (0x1 << 20))
173#define OB_REG_SIZE_SHIFT 5
174#define IB_ROOT_PORT_REG_SIZE_SHIFT 3
175#define AXI_WRAPPER_IO_WRITE 0x6
176#define AXI_WRAPPER_MEM_WRITE 0x2
Shawn Lin013dd3d2016-12-12 19:50:07 +0800177#define AXI_WRAPPER_NOR_MSG 0xc
Shawn Line77f8472016-09-03 11:41:09 -0500178
179#define MAX_AXI_IB_ROOTPORT_REGION_NUM 3
180#define MIN_AXI_ADDR_BITS_PASSED 8
Shawn Lin013dd3d2016-12-12 19:50:07 +0800181#define PCIE_RC_SEND_PME_OFF 0x11960
Shawn Line77f8472016-09-03 11:41:09 -0500182#define ROCKCHIP_VENDOR_ID 0x1d87
183#define PCIE_ECAM_BUS(x) (((x) & 0xff) << 20)
184#define PCIE_ECAM_DEV(x) (((x) & 0x1f) << 15)
185#define PCIE_ECAM_FUNC(x) (((x) & 0x7) << 12)
186#define PCIE_ECAM_REG(x) (((x) & 0xfff) << 0)
187#define PCIE_ECAM_ADDR(bus, dev, func, reg) \
188 (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \
189 PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg))
Shawn Lin013dd3d2016-12-12 19:50:07 +0800190#define PCIE_LINK_IS_L2(x) \
Shawn Lin7faebda2017-01-18 16:29:15 +0800191 (((x) & PCIE_CLIENT_DEBUG_LTSSM_MASK) == PCIE_CLIENT_DEBUG_LTSSM_L2)
192#define PCIE_LINK_UP(x) \
193 (((x) & PCIE_CLIENT_LINK_STATUS_MASK) == PCIE_CLIENT_LINK_STATUS_UP)
194#define PCIE_LINK_IS_GEN2(x) \
195 (((x) & PCIE_CORE_PL_CONF_SPEED_MASK) == PCIE_CORE_PL_CONF_SPEED_5G)
Shawn Line77f8472016-09-03 11:41:09 -0500196
197#define RC_REGION_0_ADDR_TRANS_H 0x00000000
198#define RC_REGION_0_ADDR_TRANS_L 0x00000000
199#define RC_REGION_0_PASS_BITS (25 - 1)
200#define MAX_AXI_WRAPPER_REGION_NUM 33
201
202struct rockchip_pcie {
203 void __iomem *reg_base; /* DT axi-base */
204 void __iomem *apb_base; /* DT apb-base */
205 struct phy *phy;
206 struct reset_control *core_rst;
207 struct reset_control *mgmt_rst;
208 struct reset_control *mgmt_sticky_rst;
209 struct reset_control *pipe_rst;
Shawn Lin31a3a7b2016-11-10 11:14:37 -0600210 struct reset_control *pm_rst;
211 struct reset_control *aclk_rst;
212 struct reset_control *pclk_rst;
Shawn Line77f8472016-09-03 11:41:09 -0500213 struct clk *aclk_pcie;
214 struct clk *aclk_perf_pcie;
215 struct clk *hclk_pcie;
216 struct clk *clk_pcie_pm;
217 struct regulator *vpcie3v3; /* 3.3V power supply */
218 struct regulator *vpcie1v8; /* 1.8V power supply */
219 struct regulator *vpcie0v9; /* 0.9V power supply */
220 struct gpio_desc *ep_gpio;
221 u32 lanes;
222 u8 root_bus_nr;
Shawn Linf2fb5b82016-12-07 15:05:59 -0600223 int link_gen;
Shawn Line77f8472016-09-03 11:41:09 -0500224 struct device *dev;
225 struct irq_domain *irq_domain;
Shawn Lin9e663d32016-11-24 09:54:20 +0800226 u32 io_size;
227 int offset;
228 phys_addr_t io_bus_addr;
Shawn Lin013dd3d2016-12-12 19:50:07 +0800229 void __iomem *msg_region;
Shawn Lin9e663d32016-11-24 09:54:20 +0800230 u32 mem_size;
Shawn Lin013dd3d2016-12-12 19:50:07 +0800231 phys_addr_t msg_bus_addr;
Shawn Lin9e663d32016-11-24 09:54:20 +0800232 phys_addr_t mem_bus_addr;
Shawn Line77f8472016-09-03 11:41:09 -0500233};
234
235static u32 rockchip_pcie_read(struct rockchip_pcie *rockchip, u32 reg)
236{
237 return readl(rockchip->apb_base + reg);
238}
239
240static void rockchip_pcie_write(struct rockchip_pcie *rockchip, u32 val,
241 u32 reg)
242{
243 writel(val, rockchip->apb_base + reg);
244}
245
246static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip)
247{
248 u32 status;
249
250 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
Shawn Linf37500b82016-12-07 15:06:00 -0600251 status |= (PCI_EXP_LNKCTL_LBMIE | PCI_EXP_LNKCTL_LABIE);
Shawn Line77f8472016-09-03 11:41:09 -0500252 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
253}
254
255static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip)
256{
257 u32 status;
258
259 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
Shawn Linf37500b82016-12-07 15:06:00 -0600260 status |= (PCI_EXP_LNKSTA_LBMS | PCI_EXP_LNKSTA_LABS) << 16;
Shawn Line77f8472016-09-03 11:41:09 -0500261 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
262}
263
Rajat Jain277743e2016-09-22 17:50:42 -0700264static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip)
265{
266 u32 val;
267
268 /* Update Tx credit maximum update interval */
269 val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1);
270 val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK;
271 val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000); /* ns */
272 rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1);
273}
274
Shawn Line77f8472016-09-03 11:41:09 -0500275static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
276 struct pci_bus *bus, int dev)
277{
278 /* access only one slot on each root port */
279 if (bus->number == rockchip->root_bus_nr && dev > 0)
280 return 0;
281
282 /*
283 * do not read more than one device on the bus directly attached
284 * to RC's downstream side.
285 */
286 if (bus->primary == rockchip->root_bus_nr && dev > 0)
287 return 0;
288
289 return 1;
290}
291
292static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
293 int where, int size, u32 *val)
294{
295 void __iomem *addr = rockchip->apb_base + PCIE_RC_CONFIG_BASE + where;
296
297 if (!IS_ALIGNED((uintptr_t)addr, size)) {
298 *val = 0;
299 return PCIBIOS_BAD_REGISTER_NUMBER;
300 }
301
302 if (size == 4) {
303 *val = readl(addr);
304 } else if (size == 2) {
305 *val = readw(addr);
306 } else if (size == 1) {
307 *val = readb(addr);
308 } else {
309 *val = 0;
310 return PCIBIOS_BAD_REGISTER_NUMBER;
311 }
312 return PCIBIOS_SUCCESSFUL;
313}
314
315static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
316 int where, int size, u32 val)
317{
318 u32 mask, tmp, offset;
319
320 offset = where & ~0x3;
321
322 if (size == 4) {
323 writel(val, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
324 return PCIBIOS_SUCCESSFUL;
325 }
326
327 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
328
329 /*
330 * N.B. This read/modify/write isn't safe in general because it can
331 * corrupt RW1C bits in adjacent registers. But the hardware
332 * doesn't support smaller writes.
333 */
334 tmp = readl(rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset) & mask;
335 tmp |= val << ((where & 0x3) * 8);
336 writel(tmp, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
337
338 return PCIBIOS_SUCCESSFUL;
339}
340
341static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
342 struct pci_bus *bus, u32 devfn,
343 int where, int size, u32 *val)
344{
345 u32 busdev;
346
347 busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
348 PCI_FUNC(devfn), where);
349
350 if (!IS_ALIGNED(busdev, size)) {
351 *val = 0;
352 return PCIBIOS_BAD_REGISTER_NUMBER;
353 }
354
355 if (size == 4) {
356 *val = readl(rockchip->reg_base + busdev);
357 } else if (size == 2) {
358 *val = readw(rockchip->reg_base + busdev);
359 } else if (size == 1) {
360 *val = readb(rockchip->reg_base + busdev);
361 } else {
362 *val = 0;
363 return PCIBIOS_BAD_REGISTER_NUMBER;
364 }
365 return PCIBIOS_SUCCESSFUL;
366}
367
368static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
369 struct pci_bus *bus, u32 devfn,
370 int where, int size, u32 val)
371{
372 u32 busdev;
373
374 busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
375 PCI_FUNC(devfn), where);
376 if (!IS_ALIGNED(busdev, size))
377 return PCIBIOS_BAD_REGISTER_NUMBER;
378
379 if (size == 4)
380 writel(val, rockchip->reg_base + busdev);
381 else if (size == 2)
382 writew(val, rockchip->reg_base + busdev);
383 else if (size == 1)
384 writeb(val, rockchip->reg_base + busdev);
385 else
386 return PCIBIOS_BAD_REGISTER_NUMBER;
387
388 return PCIBIOS_SUCCESSFUL;
389}
390
391static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
392 int size, u32 *val)
393{
394 struct rockchip_pcie *rockchip = bus->sysdata;
395
396 if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn))) {
397 *val = 0xffffffff;
398 return PCIBIOS_DEVICE_NOT_FOUND;
399 }
400
401 if (bus->number == rockchip->root_bus_nr)
402 return rockchip_pcie_rd_own_conf(rockchip, where, size, val);
403
404 return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size, val);
405}
406
407static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
408 int where, int size, u32 val)
409{
410 struct rockchip_pcie *rockchip = bus->sysdata;
411
412 if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
413 return PCIBIOS_DEVICE_NOT_FOUND;
414
415 if (bus->number == rockchip->root_bus_nr)
416 return rockchip_pcie_wr_own_conf(rockchip, where, size, val);
417
418 return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size, val);
419}
420
421static struct pci_ops rockchip_pcie_ops = {
422 .read = rockchip_pcie_rd_conf,
423 .write = rockchip_pcie_wr_conf,
424};
425
Shawn Lin4816c4c2016-12-07 15:05:58 -0600426static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
427{
428 u32 status, curr, scale, power;
429
430 if (IS_ERR(rockchip->vpcie3v3))
431 return;
432
433 /*
434 * Set RC's captured slot power limit and scale if
435 * vpcie3v3 available. The default values are both zero
436 * which means the software should set these two according
437 * to the actual power supply.
438 */
439 curr = regulator_get_current_limit(rockchip->vpcie3v3);
440 if (curr > 0) {
441 scale = 3; /* 0.001x */
442 curr = curr / 1000; /* convert to mA */
443 power = (curr * 3300) / 1000; /* milliwatt */
444 while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
445 if (!scale) {
446 dev_warn(rockchip->dev, "invalid power supply\n");
447 return;
448 }
449 scale--;
450 power = power / 10;
451 }
452
453 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
454 status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
455 (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
456 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
457 }
458}
459
Shawn Line77f8472016-09-03 11:41:09 -0500460/**
461 * rockchip_pcie_init_port - Initialize hardware
462 * @rockchip: PCIe port information
463 */
464static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
465{
466 struct device *dev = rockchip->dev;
467 int err;
468 u32 status;
Shawn Line77f8472016-09-03 11:41:09 -0500469
470 gpiod_set_value(rockchip->ep_gpio, 0);
471
Shawn Lin31a3a7b2016-11-10 11:14:37 -0600472 err = reset_control_assert(rockchip->aclk_rst);
473 if (err) {
474 dev_err(dev, "assert aclk_rst err %d\n", err);
475 return err;
476 }
477
478 err = reset_control_assert(rockchip->pclk_rst);
479 if (err) {
480 dev_err(dev, "assert pclk_rst err %d\n", err);
481 return err;
482 }
483
484 err = reset_control_assert(rockchip->pm_rst);
485 if (err) {
486 dev_err(dev, "assert pm_rst err %d\n", err);
487 return err;
488 }
489
Shawn Line77f8472016-09-03 11:41:09 -0500490 err = phy_init(rockchip->phy);
491 if (err < 0) {
492 dev_err(dev, "fail to init phy, err %d\n", err);
493 return err;
494 }
495
496 err = reset_control_assert(rockchip->core_rst);
497 if (err) {
498 dev_err(dev, "assert core_rst err %d\n", err);
499 return err;
500 }
501
502 err = reset_control_assert(rockchip->mgmt_rst);
503 if (err) {
504 dev_err(dev, "assert mgmt_rst err %d\n", err);
505 return err;
506 }
507
508 err = reset_control_assert(rockchip->mgmt_sticky_rst);
509 if (err) {
510 dev_err(dev, "assert mgmt_sticky_rst err %d\n", err);
511 return err;
512 }
513
514 err = reset_control_assert(rockchip->pipe_rst);
515 if (err) {
516 dev_err(dev, "assert pipe_rst err %d\n", err);
517 return err;
518 }
519
Shawn Lin0722bdd2016-11-24 09:54:21 +0800520 udelay(10);
521
522 err = reset_control_deassert(rockchip->pm_rst);
523 if (err) {
524 dev_err(dev, "deassert pm_rst err %d\n", err);
525 return err;
526 }
527
528 err = reset_control_deassert(rockchip->aclk_rst);
529 if (err) {
530 dev_err(dev, "deassert aclk_rst err %d\n", err);
531 return err;
532 }
533
534 err = reset_control_deassert(rockchip->pclk_rst);
535 if (err) {
536 dev_err(dev, "deassert pclk_rst err %d\n", err);
537 return err;
538 }
539
Shawn Linf2fb5b82016-12-07 15:05:59 -0600540 if (rockchip->link_gen == 2)
541 rockchip_pcie_write(rockchip, PCIE_CLIENT_GEN_SEL_2,
542 PCIE_CLIENT_CONFIG);
543 else
544 rockchip_pcie_write(rockchip, PCIE_CLIENT_GEN_SEL_1,
545 PCIE_CLIENT_CONFIG);
546
Shawn Line77f8472016-09-03 11:41:09 -0500547 rockchip_pcie_write(rockchip,
548 PCIE_CLIENT_CONF_ENABLE |
549 PCIE_CLIENT_LINK_TRAIN_ENABLE |
550 PCIE_CLIENT_ARI_ENABLE |
551 PCIE_CLIENT_CONF_LANE_NUM(rockchip->lanes) |
Shawn Linf2fb5b82016-12-07 15:05:59 -0600552 PCIE_CLIENT_MODE_RC,
553 PCIE_CLIENT_CONFIG);
Shawn Line77f8472016-09-03 11:41:09 -0500554
555 err = phy_power_on(rockchip->phy);
556 if (err) {
557 dev_err(dev, "fail to power on phy, err %d\n", err);
558 return err;
559 }
560
Shawn Lin58c69902016-09-23 10:05:59 +0800561 /*
562 * Please don't reorder the deassert sequence of the following
563 * four reset pins.
564 */
565 err = reset_control_deassert(rockchip->mgmt_sticky_rst);
566 if (err) {
567 dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err);
568 return err;
569 }
570
Shawn Line77f8472016-09-03 11:41:09 -0500571 err = reset_control_deassert(rockchip->core_rst);
572 if (err) {
573 dev_err(dev, "deassert core_rst err %d\n", err);
574 return err;
575 }
576
577 err = reset_control_deassert(rockchip->mgmt_rst);
578 if (err) {
579 dev_err(dev, "deassert mgmt_rst err %d\n", err);
580 return err;
581 }
582
Shawn Line77f8472016-09-03 11:41:09 -0500583 err = reset_control_deassert(rockchip->pipe_rst);
584 if (err) {
585 dev_err(dev, "deassert pipe_rst err %d\n", err);
586 return err;
587 }
588
Shawn Linca198902016-10-04 12:20:22 -0500589 /* Fix the transmitted FTS count desired to exit from L0s. */
590 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1);
Brian Norrisa45e2612016-12-07 15:06:00 -0600591 status = (status & ~PCIE_CORE_CTRL_PLC1_FTS_MASK) |
Shawn Linca198902016-10-04 12:20:22 -0500592 (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT);
593 rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1);
594
Shawn Lin4816c4c2016-12-07 15:05:58 -0600595 rockchip_pcie_set_power_limit(rockchip);
596
Shawn Linb8ab8e02016-12-07 15:05:58 -0600597 /* Set RC's clock architecture as common clock */
598 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
Shawn Linf37500b82016-12-07 15:06:00 -0600599 status |= PCI_EXP_LNKCTL_CCC;
Shawn Linb8ab8e02016-12-07 15:05:58 -0600600 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
601
Shawn Line77f8472016-09-03 11:41:09 -0500602 /* Enable Gen1 training */
603 rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
604 PCIE_CLIENT_CONFIG);
605
606 gpiod_set_value(rockchip->ep_gpio, 1);
607
608 /* 500ms timeout value should be enough for Gen1/2 training */
Shawn Lin7faebda2017-01-18 16:29:15 +0800609 err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_BASIC_STATUS1,
610 status, PCIE_LINK_UP(status), 20,
611 500 * USEC_PER_MSEC);
612 if (err) {
613 dev_err(dev, "PCIe link training gen1 timeout!\n");
614 return -ETIMEDOUT;
Shawn Line77f8472016-09-03 11:41:09 -0500615 }
616
Shawn Linf2fb5b82016-12-07 15:05:59 -0600617 if (rockchip->link_gen == 2) {
618 /*
619 * Enable retrain for gen2. This should be configured only after
620 * gen1 finished.
621 */
622 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
Shawn Linf37500b82016-12-07 15:06:00 -0600623 status |= PCI_EXP_LNKCTL_RL;
Shawn Linf2fb5b82016-12-07 15:05:59 -0600624 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
Shawn Line77f8472016-09-03 11:41:09 -0500625
Shawn Lin7faebda2017-01-18 16:29:15 +0800626 err = readl_poll_timeout(rockchip->apb_base + PCIE_CORE_CTRL,
627 status, PCIE_LINK_IS_GEN2(status), 20,
628 500 * USEC_PER_MSEC);
629 if (err)
630 dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
Shawn Line77f8472016-09-03 11:41:09 -0500631 }
632
633 /* Check the final link width from negotiated lane counter from MGMT */
634 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
Shawn Lin45e93202016-12-07 15:05:59 -0600635 status = 0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >>
636 PCIE_CORE_PL_CONF_LANE_SHIFT);
Shawn Line77f8472016-09-03 11:41:09 -0500637 dev_dbg(dev, "current link width is x%d\n", status);
638
639 rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
Shawn Lin58007902017-02-16 15:29:35 +0800640 PCIE_CORE_CONFIG_VENDOR);
Shawn Line77f8472016-09-03 11:41:09 -0500641 rockchip_pcie_write(rockchip,
642 PCI_CLASS_BRIDGE_PCI << PCIE_RC_CONFIG_SCC_SHIFT,
643 PCIE_RC_CONFIG_RID_CCR);
Shawn Lin77bc68c2016-12-07 15:05:59 -0600644
645 /* Clear THP cap's next cap pointer to remove L1 substate cap */
646 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_THP_CAP);
647 status &= ~PCIE_RC_CONFIG_THP_CAP_NEXT_MASK;
648 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_THP_CAP);
649
Shawn Linafc95952017-01-12 09:53:17 +0800650 /* Clear L0s from RC's link cap */
651 if (of_property_read_bool(dev->of_node, "aspm-no-l0s")) {
652 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LINK_CAP);
653 status &= ~PCIE_RC_CONFIG_LINK_CAP_L0S;
654 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LINK_CAP);
655 }
656
Shawn Line77f8472016-09-03 11:41:09 -0500657 rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF);
658
659 rockchip_pcie_write(rockchip,
660 (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
661 PCIE_CORE_OB_REGION_ADDR0);
662 rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
663 PCIE_CORE_OB_REGION_ADDR1);
664 rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
665 rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
666
667 return 0;
668}
669
670static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg)
671{
672 struct rockchip_pcie *rockchip = arg;
673 struct device *dev = rockchip->dev;
674 u32 reg;
675 u32 sub_reg;
676
677 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
678 if (reg & PCIE_CLIENT_INT_LOCAL) {
679 dev_dbg(dev, "local interrupt received\n");
680 sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS);
681 if (sub_reg & PCIE_CORE_INT_PRFPE)
682 dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n");
683
684 if (sub_reg & PCIE_CORE_INT_CRFPE)
685 dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n");
686
687 if (sub_reg & PCIE_CORE_INT_RRPE)
688 dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n");
689
690 if (sub_reg & PCIE_CORE_INT_PRFO)
691 dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n");
692
693 if (sub_reg & PCIE_CORE_INT_CRFO)
694 dev_dbg(dev, "overflow occurred in the completion receive FIFO\n");
695
696 if (sub_reg & PCIE_CORE_INT_RT)
697 dev_dbg(dev, "replay timer timed out\n");
698
699 if (sub_reg & PCIE_CORE_INT_RTR)
700 dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n");
701
702 if (sub_reg & PCIE_CORE_INT_PE)
703 dev_dbg(dev, "phy error detected on receive side\n");
704
705 if (sub_reg & PCIE_CORE_INT_MTR)
706 dev_dbg(dev, "malformed TLP received from the link\n");
707
708 if (sub_reg & PCIE_CORE_INT_UCR)
709 dev_dbg(dev, "malformed TLP received from the link\n");
710
711 if (sub_reg & PCIE_CORE_INT_FCE)
712 dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n");
713
714 if (sub_reg & PCIE_CORE_INT_CT)
715 dev_dbg(dev, "a request timed out waiting for completion\n");
716
717 if (sub_reg & PCIE_CORE_INT_UTC)
718 dev_dbg(dev, "unmapped TC error\n");
719
720 if (sub_reg & PCIE_CORE_INT_MMVC)
721 dev_dbg(dev, "MSI mask register changes\n");
722
723 rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS);
724 } else if (reg & PCIE_CLIENT_INT_PHY) {
725 dev_dbg(dev, "phy link changes\n");
Rajat Jain277743e2016-09-22 17:50:42 -0700726 rockchip_pcie_update_txcredit_mui(rockchip);
Shawn Line77f8472016-09-03 11:41:09 -0500727 rockchip_pcie_clr_bw_int(rockchip);
728 }
729
730 rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL,
731 PCIE_CLIENT_INT_STATUS);
732
733 return IRQ_HANDLED;
734}
735
736static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg)
737{
738 struct rockchip_pcie *rockchip = arg;
739 struct device *dev = rockchip->dev;
740 u32 reg;
741
742 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
743 if (reg & PCIE_CLIENT_INT_LEGACY_DONE)
744 dev_dbg(dev, "legacy done interrupt received\n");
745
746 if (reg & PCIE_CLIENT_INT_MSG)
747 dev_dbg(dev, "message done interrupt received\n");
748
749 if (reg & PCIE_CLIENT_INT_HOT_RST)
750 dev_dbg(dev, "hot reset interrupt received\n");
751
752 if (reg & PCIE_CLIENT_INT_DPA)
753 dev_dbg(dev, "dpa interrupt received\n");
754
755 if (reg & PCIE_CLIENT_INT_FATAL_ERR)
756 dev_dbg(dev, "fatal error interrupt received\n");
757
758 if (reg & PCIE_CLIENT_INT_NFATAL_ERR)
759 dev_dbg(dev, "no fatal error interrupt received\n");
760
761 if (reg & PCIE_CLIENT_INT_CORR_ERR)
762 dev_dbg(dev, "correctable error interrupt received\n");
763
764 if (reg & PCIE_CLIENT_INT_PHY)
765 dev_dbg(dev, "phy interrupt received\n");
766
767 rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE |
768 PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST |
769 PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR |
770 PCIE_CLIENT_INT_NFATAL_ERR |
771 PCIE_CLIENT_INT_CORR_ERR |
772 PCIE_CLIENT_INT_PHY),
773 PCIE_CLIENT_INT_STATUS);
774
775 return IRQ_HANDLED;
776}
777
778static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc)
779{
780 struct irq_chip *chip = irq_desc_get_chip(desc);
781 struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
782 struct device *dev = rockchip->dev;
783 u32 reg;
784 u32 hwirq;
785 u32 virq;
786
787 chained_irq_enter(chip, desc);
788
789 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
790 reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT;
791
792 while (reg) {
793 hwirq = ffs(reg) - 1;
794 reg &= ~BIT(hwirq);
795
796 virq = irq_find_mapping(rockchip->irq_domain, hwirq);
797 if (virq)
798 generic_handle_irq(virq);
799 else
800 dev_err(dev, "unexpected IRQ, INT%d\n", hwirq);
801 }
802
803 chained_irq_exit(chip, desc);
804}
805
806
807/**
808 * rockchip_pcie_parse_dt - Parse Device Tree
809 * @rockchip: PCIe port information
810 *
811 * Return: '0' on success and error value on failure
812 */
813static int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip)
814{
815 struct device *dev = rockchip->dev;
816 struct platform_device *pdev = to_platform_device(dev);
817 struct device_node *node = dev->of_node;
818 struct resource *regs;
819 int irq;
820 int err;
821
822 regs = platform_get_resource_byname(pdev,
823 IORESOURCE_MEM,
824 "axi-base");
825 rockchip->reg_base = devm_ioremap_resource(dev, regs);
826 if (IS_ERR(rockchip->reg_base))
827 return PTR_ERR(rockchip->reg_base);
828
829 regs = platform_get_resource_byname(pdev,
830 IORESOURCE_MEM,
831 "apb-base");
832 rockchip->apb_base = devm_ioremap_resource(dev, regs);
833 if (IS_ERR(rockchip->apb_base))
834 return PTR_ERR(rockchip->apb_base);
835
836 rockchip->phy = devm_phy_get(dev, "pcie-phy");
837 if (IS_ERR(rockchip->phy)) {
838 if (PTR_ERR(rockchip->phy) != -EPROBE_DEFER)
839 dev_err(dev, "missing phy\n");
840 return PTR_ERR(rockchip->phy);
841 }
842
843 rockchip->lanes = 1;
844 err = of_property_read_u32(node, "num-lanes", &rockchip->lanes);
845 if (!err && (rockchip->lanes == 0 ||
846 rockchip->lanes == 3 ||
847 rockchip->lanes > 4)) {
848 dev_warn(dev, "invalid num-lanes, default to use one lane\n");
849 rockchip->lanes = 1;
850 }
851
Shawn Linf2fb5b82016-12-07 15:05:59 -0600852 rockchip->link_gen = of_pci_get_max_link_speed(node);
853 if (rockchip->link_gen < 0 || rockchip->link_gen > 2)
854 rockchip->link_gen = 2;
855
Shawn Line77f8472016-09-03 11:41:09 -0500856 rockchip->core_rst = devm_reset_control_get(dev, "core");
857 if (IS_ERR(rockchip->core_rst)) {
858 if (PTR_ERR(rockchip->core_rst) != -EPROBE_DEFER)
859 dev_err(dev, "missing core reset property in node\n");
860 return PTR_ERR(rockchip->core_rst);
861 }
862
863 rockchip->mgmt_rst = devm_reset_control_get(dev, "mgmt");
864 if (IS_ERR(rockchip->mgmt_rst)) {
865 if (PTR_ERR(rockchip->mgmt_rst) != -EPROBE_DEFER)
866 dev_err(dev, "missing mgmt reset property in node\n");
867 return PTR_ERR(rockchip->mgmt_rst);
868 }
869
870 rockchip->mgmt_sticky_rst = devm_reset_control_get(dev, "mgmt-sticky");
871 if (IS_ERR(rockchip->mgmt_sticky_rst)) {
872 if (PTR_ERR(rockchip->mgmt_sticky_rst) != -EPROBE_DEFER)
873 dev_err(dev, "missing mgmt-sticky reset property in node\n");
874 return PTR_ERR(rockchip->mgmt_sticky_rst);
875 }
876
877 rockchip->pipe_rst = devm_reset_control_get(dev, "pipe");
878 if (IS_ERR(rockchip->pipe_rst)) {
879 if (PTR_ERR(rockchip->pipe_rst) != -EPROBE_DEFER)
880 dev_err(dev, "missing pipe reset property in node\n");
881 return PTR_ERR(rockchip->pipe_rst);
882 }
883
Shawn Lin31a3a7b2016-11-10 11:14:37 -0600884 rockchip->pm_rst = devm_reset_control_get(dev, "pm");
885 if (IS_ERR(rockchip->pm_rst)) {
886 if (PTR_ERR(rockchip->pm_rst) != -EPROBE_DEFER)
887 dev_err(dev, "missing pm reset property in node\n");
888 return PTR_ERR(rockchip->pm_rst);
889 }
890
891 rockchip->pclk_rst = devm_reset_control_get(dev, "pclk");
892 if (IS_ERR(rockchip->pclk_rst)) {
893 if (PTR_ERR(rockchip->pclk_rst) != -EPROBE_DEFER)
894 dev_err(dev, "missing pclk reset property in node\n");
895 return PTR_ERR(rockchip->pclk_rst);
896 }
897
898 rockchip->aclk_rst = devm_reset_control_get(dev, "aclk");
899 if (IS_ERR(rockchip->aclk_rst)) {
900 if (PTR_ERR(rockchip->aclk_rst) != -EPROBE_DEFER)
901 dev_err(dev, "missing aclk reset property in node\n");
902 return PTR_ERR(rockchip->aclk_rst);
903 }
904
Shawn Line77f8472016-09-03 11:41:09 -0500905 rockchip->ep_gpio = devm_gpiod_get(dev, "ep", GPIOD_OUT_HIGH);
906 if (IS_ERR(rockchip->ep_gpio)) {
907 dev_err(dev, "missing ep-gpios property in node\n");
908 return PTR_ERR(rockchip->ep_gpio);
909 }
910
911 rockchip->aclk_pcie = devm_clk_get(dev, "aclk");
912 if (IS_ERR(rockchip->aclk_pcie)) {
913 dev_err(dev, "aclk clock not found\n");
914 return PTR_ERR(rockchip->aclk_pcie);
915 }
916
917 rockchip->aclk_perf_pcie = devm_clk_get(dev, "aclk-perf");
918 if (IS_ERR(rockchip->aclk_perf_pcie)) {
919 dev_err(dev, "aclk_perf clock not found\n");
920 return PTR_ERR(rockchip->aclk_perf_pcie);
921 }
922
923 rockchip->hclk_pcie = devm_clk_get(dev, "hclk");
924 if (IS_ERR(rockchip->hclk_pcie)) {
925 dev_err(dev, "hclk clock not found\n");
926 return PTR_ERR(rockchip->hclk_pcie);
927 }
928
929 rockchip->clk_pcie_pm = devm_clk_get(dev, "pm");
930 if (IS_ERR(rockchip->clk_pcie_pm)) {
931 dev_err(dev, "pm clock not found\n");
932 return PTR_ERR(rockchip->clk_pcie_pm);
933 }
934
935 irq = platform_get_irq_byname(pdev, "sys");
936 if (irq < 0) {
937 dev_err(dev, "missing sys IRQ resource\n");
938 return -EINVAL;
939 }
940
941 err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler,
942 IRQF_SHARED, "pcie-sys", rockchip);
943 if (err) {
944 dev_err(dev, "failed to request PCIe subsystem IRQ\n");
945 return err;
946 }
947
948 irq = platform_get_irq_byname(pdev, "legacy");
949 if (irq < 0) {
950 dev_err(dev, "missing legacy IRQ resource\n");
951 return -EINVAL;
952 }
953
954 irq_set_chained_handler_and_data(irq,
955 rockchip_pcie_legacy_int_handler,
956 rockchip);
957
958 irq = platform_get_irq_byname(pdev, "client");
959 if (irq < 0) {
960 dev_err(dev, "missing client IRQ resource\n");
961 return -EINVAL;
962 }
963
964 err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler,
965 IRQF_SHARED, "pcie-client", rockchip);
966 if (err) {
967 dev_err(dev, "failed to request PCIe client IRQ\n");
968 return err;
969 }
970
971 rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
972 if (IS_ERR(rockchip->vpcie3v3)) {
973 if (PTR_ERR(rockchip->vpcie3v3) == -EPROBE_DEFER)
974 return -EPROBE_DEFER;
975 dev_info(dev, "no vpcie3v3 regulator found\n");
976 }
977
978 rockchip->vpcie1v8 = devm_regulator_get_optional(dev, "vpcie1v8");
979 if (IS_ERR(rockchip->vpcie1v8)) {
980 if (PTR_ERR(rockchip->vpcie1v8) == -EPROBE_DEFER)
981 return -EPROBE_DEFER;
982 dev_info(dev, "no vpcie1v8 regulator found\n");
983 }
984
985 rockchip->vpcie0v9 = devm_regulator_get_optional(dev, "vpcie0v9");
986 if (IS_ERR(rockchip->vpcie0v9)) {
987 if (PTR_ERR(rockchip->vpcie0v9) == -EPROBE_DEFER)
988 return -EPROBE_DEFER;
989 dev_info(dev, "no vpcie0v9 regulator found\n");
990 }
991
992 return 0;
993}
994
995static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip)
996{
997 struct device *dev = rockchip->dev;
998 int err;
999
1000 if (!IS_ERR(rockchip->vpcie3v3)) {
1001 err = regulator_enable(rockchip->vpcie3v3);
1002 if (err) {
1003 dev_err(dev, "fail to enable vpcie3v3 regulator\n");
1004 goto err_out;
1005 }
1006 }
1007
1008 if (!IS_ERR(rockchip->vpcie1v8)) {
1009 err = regulator_enable(rockchip->vpcie1v8);
1010 if (err) {
1011 dev_err(dev, "fail to enable vpcie1v8 regulator\n");
1012 goto err_disable_3v3;
1013 }
1014 }
1015
1016 if (!IS_ERR(rockchip->vpcie0v9)) {
1017 err = regulator_enable(rockchip->vpcie0v9);
1018 if (err) {
1019 dev_err(dev, "fail to enable vpcie0v9 regulator\n");
1020 goto err_disable_1v8;
1021 }
1022 }
1023
1024 return 0;
1025
1026err_disable_1v8:
1027 if (!IS_ERR(rockchip->vpcie1v8))
1028 regulator_disable(rockchip->vpcie1v8);
1029err_disable_3v3:
1030 if (!IS_ERR(rockchip->vpcie3v3))
1031 regulator_disable(rockchip->vpcie3v3);
1032err_out:
1033 return err;
1034}
1035
1036static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip)
1037{
1038 rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) &
1039 (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK);
1040 rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT),
1041 PCIE_CORE_INT_MASK);
1042
1043 rockchip_pcie_enable_bw_int(rockchip);
1044}
1045
1046static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
1047 irq_hw_number_t hwirq)
1048{
1049 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
1050 irq_set_chip_data(irq, domain->host_data);
1051
1052 return 0;
1053}
1054
1055static const struct irq_domain_ops intx_domain_ops = {
1056 .map = rockchip_pcie_intx_map,
1057};
1058
1059static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
1060{
1061 struct device *dev = rockchip->dev;
1062 struct device_node *intc = of_get_next_child(dev->of_node, NULL);
1063
1064 if (!intc) {
1065 dev_err(dev, "missing child interrupt-controller node\n");
1066 return -EINVAL;
1067 }
1068
1069 rockchip->irq_domain = irq_domain_add_linear(intc, 4,
1070 &intx_domain_ops, rockchip);
1071 if (!rockchip->irq_domain) {
1072 dev_err(dev, "failed to get a INTx IRQ domain\n");
1073 return -EINVAL;
1074 }
1075
1076 return 0;
1077}
1078
1079static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
1080 int region_no, int type, u8 num_pass_bits,
1081 u32 lower_addr, u32 upper_addr)
1082{
1083 u32 ob_addr_0;
1084 u32 ob_addr_1;
1085 u32 ob_desc_0;
1086 u32 aw_offset;
1087
1088 if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
1089 return -EINVAL;
1090 if (num_pass_bits + 1 < 8)
1091 return -EINVAL;
1092 if (num_pass_bits > 63)
1093 return -EINVAL;
1094 if (region_no == 0) {
1095 if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
Dan Carpenter08015ee2016-10-12 07:14:09 -05001096 return -EINVAL;
Shawn Line77f8472016-09-03 11:41:09 -05001097 }
1098 if (region_no != 0) {
1099 if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
1100 return -EINVAL;
1101 }
1102
1103 aw_offset = (region_no << OB_REG_SIZE_SHIFT);
1104
1105 ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
1106 ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
1107 ob_addr_1 = upper_addr;
1108 ob_desc_0 = (1 << 23 | type);
1109
1110 rockchip_pcie_write(rockchip, ob_addr_0,
1111 PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
1112 rockchip_pcie_write(rockchip, ob_addr_1,
1113 PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
1114 rockchip_pcie_write(rockchip, ob_desc_0,
1115 PCIE_CORE_OB_REGION_DESC0 + aw_offset);
1116 rockchip_pcie_write(rockchip, 0,
1117 PCIE_CORE_OB_REGION_DESC1 + aw_offset);
1118
1119 return 0;
1120}
1121
1122static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
1123 int region_no, u8 num_pass_bits,
1124 u32 lower_addr, u32 upper_addr)
1125{
1126 u32 ib_addr_0;
1127 u32 ib_addr_1;
1128 u32 aw_offset;
1129
1130 if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM)
1131 return -EINVAL;
1132 if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED)
1133 return -EINVAL;
1134 if (num_pass_bits > 63)
1135 return -EINVAL;
1136
1137 aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT);
1138
1139 ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS;
1140 ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR;
1141 ib_addr_1 = upper_addr;
1142
1143 rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset);
1144 rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset);
1145
1146 return 0;
1147}
1148
Shawn Lin9e663d32016-11-24 09:54:20 +08001149static int rockchip_cfg_atu(struct rockchip_pcie *rockchip)
1150{
1151 struct device *dev = rockchip->dev;
1152 int offset;
1153 int err;
1154 int reg_no;
1155
1156 for (reg_no = 0; reg_no < (rockchip->mem_size >> 20); reg_no++) {
1157 err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
1158 AXI_WRAPPER_MEM_WRITE,
1159 20 - 1,
1160 rockchip->mem_bus_addr +
1161 (reg_no << 20),
1162 0);
1163 if (err) {
1164 dev_err(dev, "program RC mem outbound ATU failed\n");
1165 return err;
1166 }
1167 }
1168
1169 err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0);
1170 if (err) {
1171 dev_err(dev, "program RC mem inbound ATU failed\n");
1172 return err;
1173 }
1174
1175 offset = rockchip->mem_size >> 20;
1176 for (reg_no = 0; reg_no < (rockchip->io_size >> 20); reg_no++) {
1177 err = rockchip_pcie_prog_ob_atu(rockchip,
1178 reg_no + 1 + offset,
1179 AXI_WRAPPER_IO_WRITE,
1180 20 - 1,
1181 rockchip->io_bus_addr +
1182 (reg_no << 20),
1183 0);
1184 if (err) {
1185 dev_err(dev, "program RC io outbound ATU failed\n");
1186 return err;
1187 }
1188 }
1189
Shawn Lin013dd3d2016-12-12 19:50:07 +08001190 /* assign message regions */
1191 rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1 + offset,
1192 AXI_WRAPPER_NOR_MSG,
1193 20 - 1, 0, 0);
1194
1195 rockchip->msg_bus_addr = rockchip->mem_bus_addr +
1196 ((reg_no + offset) << 20);
1197 return err;
1198}
1199
1200static int rockchip_pcie_wait_l2(struct rockchip_pcie *rockchip)
1201{
1202 u32 value;
1203 int err;
1204
1205 /* send PME_TURN_OFF message */
1206 writel(0x0, rockchip->msg_region + PCIE_RC_SEND_PME_OFF);
1207
1208 /* read LTSSM and wait for falling into L2 link state */
1209 err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_DEBUG_OUT_0,
1210 value, PCIE_LINK_IS_L2(value), 20,
1211 jiffies_to_usecs(5 * HZ));
1212 if (err) {
1213 dev_err(rockchip->dev, "PCIe link enter L2 timeout!\n");
1214 return err;
1215 }
1216
1217 return 0;
1218}
1219
Arnd Bergmann0b351c92017-01-20 17:24:30 +01001220static int __maybe_unused rockchip_pcie_suspend_noirq(struct device *dev)
Shawn Lin013dd3d2016-12-12 19:50:07 +08001221{
1222 struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
1223 int ret;
1224
1225 /* disable core and cli int since we don't need to ack PME_ACK */
1226 rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) |
1227 PCIE_CLIENT_INT_CLI, PCIE_CLIENT_INT_MASK);
1228 rockchip_pcie_write(rockchip, (u32)PCIE_CORE_INT, PCIE_CORE_INT_MASK);
1229
1230 ret = rockchip_pcie_wait_l2(rockchip);
1231 if (ret) {
1232 rockchip_pcie_enable_interrupts(rockchip);
1233 return ret;
1234 }
1235
1236 phy_power_off(rockchip->phy);
1237 phy_exit(rockchip->phy);
1238
1239 clk_disable_unprepare(rockchip->clk_pcie_pm);
1240 clk_disable_unprepare(rockchip->hclk_pcie);
1241 clk_disable_unprepare(rockchip->aclk_perf_pcie);
1242 clk_disable_unprepare(rockchip->aclk_pcie);
1243
1244 return ret;
1245}
1246
Arnd Bergmann0b351c92017-01-20 17:24:30 +01001247static int __maybe_unused rockchip_pcie_resume_noirq(struct device *dev)
Shawn Lin013dd3d2016-12-12 19:50:07 +08001248{
1249 struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
1250 int err;
1251
1252 clk_prepare_enable(rockchip->clk_pcie_pm);
1253 clk_prepare_enable(rockchip->hclk_pcie);
1254 clk_prepare_enable(rockchip->aclk_perf_pcie);
1255 clk_prepare_enable(rockchip->aclk_pcie);
1256
1257 err = rockchip_pcie_init_port(rockchip);
1258 if (err)
1259 return err;
1260
1261 err = rockchip_cfg_atu(rockchip);
1262 if (err)
1263 return err;
1264
1265 /* Need this to enter L1 again */
1266 rockchip_pcie_update_txcredit_mui(rockchip);
1267 rockchip_pcie_enable_interrupts(rockchip);
1268
Shawn Lin9e663d32016-11-24 09:54:20 +08001269 return 0;
1270}
1271
Shawn Line77f8472016-09-03 11:41:09 -05001272static int rockchip_pcie_probe(struct platform_device *pdev)
1273{
1274 struct rockchip_pcie *rockchip;
1275 struct device *dev = &pdev->dev;
1276 struct pci_bus *bus, *child;
1277 struct resource_entry *win;
1278 resource_size_t io_base;
1279 struct resource *mem;
1280 struct resource *io;
Shawn Line77f8472016-09-03 11:41:09 -05001281 int err;
Shawn Line77f8472016-09-03 11:41:09 -05001282
1283 LIST_HEAD(res);
1284
1285 if (!dev->of_node)
1286 return -ENODEV;
1287
1288 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
1289 if (!rockchip)
1290 return -ENOMEM;
1291
Shawn Lin013dd3d2016-12-12 19:50:07 +08001292 platform_set_drvdata(pdev, rockchip);
Shawn Line77f8472016-09-03 11:41:09 -05001293 rockchip->dev = dev;
1294
1295 err = rockchip_pcie_parse_dt(rockchip);
1296 if (err)
1297 return err;
1298
1299 err = clk_prepare_enable(rockchip->aclk_pcie);
1300 if (err) {
1301 dev_err(dev, "unable to enable aclk_pcie clock\n");
1302 goto err_aclk_pcie;
1303 }
1304
1305 err = clk_prepare_enable(rockchip->aclk_perf_pcie);
1306 if (err) {
1307 dev_err(dev, "unable to enable aclk_perf_pcie clock\n");
1308 goto err_aclk_perf_pcie;
1309 }
1310
1311 err = clk_prepare_enable(rockchip->hclk_pcie);
1312 if (err) {
1313 dev_err(dev, "unable to enable hclk_pcie clock\n");
1314 goto err_hclk_pcie;
1315 }
1316
1317 err = clk_prepare_enable(rockchip->clk_pcie_pm);
1318 if (err) {
1319 dev_err(dev, "unable to enable hclk_pcie clock\n");
1320 goto err_pcie_pm;
1321 }
1322
1323 err = rockchip_pcie_set_vpcie(rockchip);
1324 if (err) {
1325 dev_err(dev, "failed to set vpcie regulator\n");
1326 goto err_set_vpcie;
1327 }
1328
1329 err = rockchip_pcie_init_port(rockchip);
1330 if (err)
1331 goto err_vpcie;
1332
Shawn Line77f8472016-09-03 11:41:09 -05001333 rockchip_pcie_enable_interrupts(rockchip);
1334
1335 err = rockchip_pcie_init_irq_domain(rockchip);
1336 if (err < 0)
1337 goto err_vpcie;
1338
1339 err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff,
1340 &res, &io_base);
1341 if (err)
1342 goto err_vpcie;
1343
1344 err = devm_request_pci_bus_resources(dev, &res);
1345 if (err)
Shawn Linf1d722b2017-02-10 14:52:02 +08001346 goto err_free_res;
Shawn Line77f8472016-09-03 11:41:09 -05001347
1348 /* Get the I/O and memory ranges from DT */
Shawn Line77f8472016-09-03 11:41:09 -05001349 resource_list_for_each_entry(win, &res) {
1350 switch (resource_type(win->res)) {
1351 case IORESOURCE_IO:
1352 io = win->res;
1353 io->name = "I/O";
Shawn Lin9e663d32016-11-24 09:54:20 +08001354 rockchip->io_size = resource_size(io);
1355 rockchip->io_bus_addr = io->start - win->offset;
Shawn Line77f8472016-09-03 11:41:09 -05001356 err = pci_remap_iospace(io, io_base);
1357 if (err) {
1358 dev_warn(dev, "error %d: failed to map resource %pR\n",
1359 err, io);
1360 continue;
1361 }
1362 break;
1363 case IORESOURCE_MEM:
1364 mem = win->res;
1365 mem->name = "MEM";
Shawn Lin9e663d32016-11-24 09:54:20 +08001366 rockchip->mem_size = resource_size(mem);
1367 rockchip->mem_bus_addr = mem->start - win->offset;
Shawn Line77f8472016-09-03 11:41:09 -05001368 break;
1369 case IORESOURCE_BUS:
1370 rockchip->root_bus_nr = win->res->start;
1371 break;
1372 default:
1373 continue;
1374 }
1375 }
1376
Shawn Lin9e663d32016-11-24 09:54:20 +08001377 err = rockchip_cfg_atu(rockchip);
1378 if (err)
Shawn Linf1d722b2017-02-10 14:52:02 +08001379 goto err_free_res;
Shawn Lin013dd3d2016-12-12 19:50:07 +08001380
1381 rockchip->msg_region = devm_ioremap(rockchip->dev,
1382 rockchip->msg_bus_addr, SZ_1M);
1383 if (!rockchip->msg_region) {
1384 err = -ENOMEM;
Shawn Linf1d722b2017-02-10 14:52:02 +08001385 goto err_free_res;
Shawn Lin013dd3d2016-12-12 19:50:07 +08001386 }
1387
Shawn Line77f8472016-09-03 11:41:09 -05001388 bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
1389 if (!bus) {
1390 err = -ENOMEM;
Shawn Linf1d722b2017-02-10 14:52:02 +08001391 goto err_free_res;
Shawn Line77f8472016-09-03 11:41:09 -05001392 }
1393
1394 pci_bus_size_bridges(bus);
1395 pci_bus_assign_resources(bus);
1396 list_for_each_entry(child, &bus->children, node)
1397 pcie_bus_configure_settings(child);
1398
1399 pci_bus_add_devices(bus);
Shawn Line77f8472016-09-03 11:41:09 -05001400 return err;
1401
Shawn Linf1d722b2017-02-10 14:52:02 +08001402err_free_res:
1403 pci_free_resource_list(&res);
Shawn Line77f8472016-09-03 11:41:09 -05001404err_vpcie:
1405 if (!IS_ERR(rockchip->vpcie3v3))
1406 regulator_disable(rockchip->vpcie3v3);
1407 if (!IS_ERR(rockchip->vpcie1v8))
1408 regulator_disable(rockchip->vpcie1v8);
1409 if (!IS_ERR(rockchip->vpcie0v9))
1410 regulator_disable(rockchip->vpcie0v9);
1411err_set_vpcie:
1412 clk_disable_unprepare(rockchip->clk_pcie_pm);
1413err_pcie_pm:
1414 clk_disable_unprepare(rockchip->hclk_pcie);
1415err_hclk_pcie:
1416 clk_disable_unprepare(rockchip->aclk_perf_pcie);
1417err_aclk_perf_pcie:
1418 clk_disable_unprepare(rockchip->aclk_pcie);
1419err_aclk_pcie:
1420 return err;
1421}
1422
Shawn Lin013dd3d2016-12-12 19:50:07 +08001423static const struct dev_pm_ops rockchip_pcie_pm_ops = {
1424 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_pcie_suspend_noirq,
1425 rockchip_pcie_resume_noirq)
1426};
1427
Shawn Line77f8472016-09-03 11:41:09 -05001428static const struct of_device_id rockchip_pcie_of_match[] = {
1429 { .compatible = "rockchip,rk3399-pcie", },
1430 {}
1431};
1432
1433static struct platform_driver rockchip_pcie_driver = {
1434 .driver = {
1435 .name = "rockchip-pcie",
1436 .of_match_table = rockchip_pcie_of_match,
Shawn Lin013dd3d2016-12-12 19:50:07 +08001437 .pm = &rockchip_pcie_pm_ops,
Shawn Line77f8472016-09-03 11:41:09 -05001438 },
1439 .probe = rockchip_pcie_probe,
1440
1441};
1442builtin_platform_driver(rockchip_pcie_driver);