7d16669b4e7203df4feb64f6245b3c7b932298d8
[firefly-linux-kernel-4.4.55.git] / drivers / pci / host / pcie-rockchip.c
1 /*
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>
23 #include <linux/irq.h>
24 #include <linux/irqchip/chained_irq.h>
25 #include <linux/irqdomain.h>
26 #include <linux/kernel.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/of_pci.h>
31 #include <linux/of_platform.h>
32 #include <linux/of_irq.h>
33 #include <linux/pci.h>
34 #include <linux/pci_ids.h>
35 #include <linux/phy/phy.h>
36 #include <linux/platform_device.h>
37 #include <linux/reset.h>
38 #include <linux/regmap.h>
39
40 /*
41  * The upper 16 bits of PCIE_CLIENT_CONFIG are a write mask for the lower 16
42  * bits.  This allows atomic updates of the register without locking.
43  */
44 #define HIWORD_UPDATE(mask, val)        (((mask) << 16) | (val))
45 #define HIWORD_UPDATE_BIT(val)          HIWORD_UPDATE(val, val)
46
47 #define ENCODE_LANES(x)                 ((((x) >> 1) & 3) << 4)
48
49 #define PCIE_CLIENT_BASE                0x0
50 #define PCIE_CLIENT_CONFIG              (PCIE_CLIENT_BASE + 0x00)
51 #define   PCIE_CLIENT_CONF_ENABLE         HIWORD_UPDATE_BIT(0x0001)
52 #define   PCIE_CLIENT_LINK_TRAIN_ENABLE   HIWORD_UPDATE_BIT(0x0002)
53 #define   PCIE_CLIENT_ARI_ENABLE          HIWORD_UPDATE_BIT(0x0008)
54 #define   PCIE_CLIENT_CONF_LANE_NUM(x)    HIWORD_UPDATE(0x0030, ENCODE_LANES(x))
55 #define   PCIE_CLIENT_MODE_RC             HIWORD_UPDATE_BIT(0x0040)
56 #define   PCIE_CLIENT_GEN_SEL_2           HIWORD_UPDATE_BIT(0x0080)
57 #define PCIE_CLIENT_BASIC_STATUS1       (PCIE_CLIENT_BASE + 0x48)
58 #define   PCIE_CLIENT_LINK_STATUS_UP            0x00300000
59 #define   PCIE_CLIENT_LINK_STATUS_MASK          0x00300000
60 #define PCIE_CLIENT_INT_MASK            (PCIE_CLIENT_BASE + 0x4c)
61 #define PCIE_CLIENT_INT_STATUS          (PCIE_CLIENT_BASE + 0x50)
62 #define   PCIE_CLIENT_INTR_MASK                 GENMASK(8, 5)
63 #define   PCIE_CLIENT_INTR_SHIFT                5
64 #define   PCIE_CLIENT_INT_LEGACY_DONE           BIT(15)
65 #define   PCIE_CLIENT_INT_MSG                   BIT(14)
66 #define   PCIE_CLIENT_INT_HOT_RST               BIT(13)
67 #define   PCIE_CLIENT_INT_DPA                   BIT(12)
68 #define   PCIE_CLIENT_INT_FATAL_ERR             BIT(11)
69 #define   PCIE_CLIENT_INT_NFATAL_ERR            BIT(10)
70 #define   PCIE_CLIENT_INT_CORR_ERR              BIT(9)
71 #define   PCIE_CLIENT_INT_INTD                  BIT(8)
72 #define   PCIE_CLIENT_INT_INTC                  BIT(7)
73 #define   PCIE_CLIENT_INT_INTB                  BIT(6)
74 #define   PCIE_CLIENT_INT_INTA                  BIT(5)
75 #define   PCIE_CLIENT_INT_LOCAL                 BIT(4)
76 #define   PCIE_CLIENT_INT_UDMA                  BIT(3)
77 #define   PCIE_CLIENT_INT_PHY                   BIT(2)
78 #define   PCIE_CLIENT_INT_HOT_PLUG              BIT(1)
79 #define   PCIE_CLIENT_INT_PWR_STCG              BIT(0)
80
81 #define PCIE_CLIENT_INT_LEGACY \
82         (PCIE_CLIENT_INT_INTA | PCIE_CLIENT_INT_INTB | \
83         PCIE_CLIENT_INT_INTC | PCIE_CLIENT_INT_INTD)
84
85 #define PCIE_CLIENT_INT_CLI \
86         (PCIE_CLIENT_INT_CORR_ERR | PCIE_CLIENT_INT_NFATAL_ERR | \
87         PCIE_CLIENT_INT_FATAL_ERR | PCIE_CLIENT_INT_DPA | \
88         PCIE_CLIENT_INT_HOT_RST | PCIE_CLIENT_INT_MSG | \
89         PCIE_CLIENT_INT_LEGACY_DONE | PCIE_CLIENT_INT_LEGACY | \
90         PCIE_CLIENT_INT_PHY)
91
92 #define PCIE_CORE_CTRL_MGMT_BASE        0x900000
93 #define PCIE_CORE_CTRL                  (PCIE_CORE_CTRL_MGMT_BASE + 0x000)
94 #define   PCIE_CORE_PL_CONF_SPEED_5G            0x00000008
95 #define   PCIE_CORE_PL_CONF_SPEED_MASK          0x00000018
96 #define   PCIE_CORE_PL_CONF_LANE_MASK           0x00000006
97 #define   PCIE_CORE_PL_CONF_LANE_SHIFT          1
98 #define PCIE_CORE_CTRL_PLC1             (PCIE_CORE_CTRL_MGMT_BASE + 0x004)
99 #define   PCIE_CORE_CTRL_PLC1_FTS_MASK          GENMASK(23, 8)
100 #define   PCIE_CORE_CTRL_PLC1_FTS_SHIFT         8
101 #define   PCIE_CORE_CTRL_PLC1_FTS_CNT           0xffff
102 #define PCIE_CORE_TXCREDIT_CFG1         (PCIE_CORE_CTRL_MGMT_BASE + 0x020)
103 #define   PCIE_CORE_TXCREDIT_CFG1_MUI_MASK      0xFFFF0000
104 #define   PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT     16
105 #define   PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(x) \
106                 (((x) >> 3) << PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT)
107 #define PCIE_CORE_INT_STATUS            (PCIE_CORE_CTRL_MGMT_BASE + 0x20c)
108 #define   PCIE_CORE_INT_PRFPE                   BIT(0)
109 #define   PCIE_CORE_INT_CRFPE                   BIT(1)
110 #define   PCIE_CORE_INT_RRPE                    BIT(2)
111 #define   PCIE_CORE_INT_PRFO                    BIT(3)
112 #define   PCIE_CORE_INT_CRFO                    BIT(4)
113 #define   PCIE_CORE_INT_RT                      BIT(5)
114 #define   PCIE_CORE_INT_RTR                     BIT(6)
115 #define   PCIE_CORE_INT_PE                      BIT(7)
116 #define   PCIE_CORE_INT_MTR                     BIT(8)
117 #define   PCIE_CORE_INT_UCR                     BIT(9)
118 #define   PCIE_CORE_INT_FCE                     BIT(10)
119 #define   PCIE_CORE_INT_CT                      BIT(11)
120 #define   PCIE_CORE_INT_UTC                     BIT(18)
121 #define   PCIE_CORE_INT_MMVC                    BIT(19)
122 #define PCIE_CORE_INT_MASK              (PCIE_CORE_CTRL_MGMT_BASE + 0x210)
123 #define PCIE_RC_BAR_CONF                (PCIE_CORE_CTRL_MGMT_BASE + 0x300)
124
125 #define PCIE_CORE_INT \
126                 (PCIE_CORE_INT_PRFPE | PCIE_CORE_INT_CRFPE | \
127                  PCIE_CORE_INT_RRPE | PCIE_CORE_INT_CRFO | \
128                  PCIE_CORE_INT_RT | PCIE_CORE_INT_RTR | \
129                  PCIE_CORE_INT_PE | PCIE_CORE_INT_MTR | \
130                  PCIE_CORE_INT_UCR | PCIE_CORE_INT_FCE | \
131                  PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | \
132                  PCIE_CORE_INT_MMVC)
133
134 #define PCIE_RC_CONFIG_BASE             0xa00000
135 #define PCIE_RC_CONFIG_VENDOR           (PCIE_RC_CONFIG_BASE + 0x00)
136 #define PCIE_RC_CONFIG_RID_CCR          (PCIE_RC_CONFIG_BASE + 0x08)
137 #define   PCIE_RC_CONFIG_SCC_SHIFT              16
138 #define PCIE_RC_CONFIG_DCR              (PCIE_RC_CONFIG_BASE + 0xc4)
139 #define   PCIE_RC_CONFIG_DCR_CSPL_SHIFT         18
140 #define   PCIE_RC_CONFIG_DCR_CSPL_LIMIT         0xff
141 #define   PCIE_RC_CONFIG_DCR_CPLS_SHIFT         26
142 #define PCIE_RC_CONFIG_LCS              (PCIE_RC_CONFIG_BASE + 0xd0)
143 #define   PCIE_RC_CONFIG_LCS_RETRAIN_LINK       BIT(5)
144 #define   PCIE_RC_CONFIG_LCS_LBMIE              BIT(10)
145 #define   PCIE_RC_CONFIG_LCS_LABIE              BIT(11)
146 #define   PCIE_RC_CONFIG_LCS_LBMS               BIT(30)
147 #define   PCIE_RC_CONFIG_LCS_LAMS               BIT(31)
148 #define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c)
149
150 #define PCIE_CORE_AXI_CONF_BASE         0xc00000
151 #define PCIE_CORE_OB_REGION_ADDR0       (PCIE_CORE_AXI_CONF_BASE + 0x0)
152 #define   PCIE_CORE_OB_REGION_ADDR0_NUM_BITS    0x3f
153 #define   PCIE_CORE_OB_REGION_ADDR0_LO_ADDR     0xffffff00
154 #define PCIE_CORE_OB_REGION_ADDR1       (PCIE_CORE_AXI_CONF_BASE + 0x4)
155 #define PCIE_CORE_OB_REGION_DESC0       (PCIE_CORE_AXI_CONF_BASE + 0x8)
156 #define PCIE_CORE_OB_REGION_DESC1       (PCIE_CORE_AXI_CONF_BASE + 0xc)
157
158 #define PCIE_CORE_AXI_INBOUND_BASE      0xc00800
159 #define PCIE_RP_IB_ADDR0                (PCIE_CORE_AXI_INBOUND_BASE + 0x0)
160 #define   PCIE_CORE_IB_REGION_ADDR0_NUM_BITS    0x3f
161 #define   PCIE_CORE_IB_REGION_ADDR0_LO_ADDR     0xffffff00
162 #define PCIE_RP_IB_ADDR1                (PCIE_CORE_AXI_INBOUND_BASE + 0x4)
163
164 /* Size of one AXI Region (not Region 0) */
165 #define AXI_REGION_SIZE                         BIT(20)
166 /* Size of Region 0, equal to sum of sizes of other regions */
167 #define AXI_REGION_0_SIZE                       (32 * (0x1 << 20))
168 #define OB_REG_SIZE_SHIFT                       5
169 #define IB_ROOT_PORT_REG_SIZE_SHIFT             3
170 #define AXI_WRAPPER_IO_WRITE                    0x6
171 #define AXI_WRAPPER_MEM_WRITE                   0x2
172
173 #define MAX_AXI_IB_ROOTPORT_REGION_NUM          3
174 #define MIN_AXI_ADDR_BITS_PASSED                8
175 #define ROCKCHIP_VENDOR_ID                      0x1d87
176 #define PCIE_ECAM_BUS(x)                        (((x) & 0xff) << 20)
177 #define PCIE_ECAM_DEV(x)                        (((x) & 0x1f) << 15)
178 #define PCIE_ECAM_FUNC(x)                       (((x) & 0x7) << 12)
179 #define PCIE_ECAM_REG(x)                        (((x) & 0xfff) << 0)
180 #define PCIE_ECAM_ADDR(bus, dev, func, reg) \
181           (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \
182            PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg))
183
184 #define RC_REGION_0_ADDR_TRANS_H                0x00000000
185 #define RC_REGION_0_ADDR_TRANS_L                0x00000000
186 #define RC_REGION_0_PASS_BITS                   (25 - 1)
187 #define MAX_AXI_WRAPPER_REGION_NUM              33
188
189 struct rockchip_pcie {
190         void    __iomem *reg_base;              /* DT axi-base */
191         void    __iomem *apb_base;              /* DT apb-base */
192         struct  phy *phy;
193         struct  reset_control *core_rst;
194         struct  reset_control *mgmt_rst;
195         struct  reset_control *mgmt_sticky_rst;
196         struct  reset_control *pipe_rst;
197         struct  clk *aclk_pcie;
198         struct  clk *aclk_perf_pcie;
199         struct  clk *hclk_pcie;
200         struct  clk *clk_pcie_pm;
201         struct  regulator *vpcie3v3; /* 3.3V power supply */
202         struct  regulator *vpcie1v8; /* 1.8V power supply */
203         struct  regulator *vpcie0v9; /* 0.9V power supply */
204         struct  gpio_desc *ep_gpio;
205         u32     lanes;
206         u8      root_bus_nr;
207         struct  device *dev;
208         struct  irq_domain *irq_domain;
209 };
210
211 static u32 rockchip_pcie_read(struct rockchip_pcie *rockchip, u32 reg)
212 {
213         return readl(rockchip->apb_base + reg);
214 }
215
216 static void rockchip_pcie_write(struct rockchip_pcie *rockchip, u32 val,
217                                 u32 reg)
218 {
219         writel(val, rockchip->apb_base + reg);
220 }
221
222 static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip)
223 {
224         u32 status;
225
226         status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
227         status |= (PCIE_RC_CONFIG_LCS_LBMIE | PCIE_RC_CONFIG_LCS_LABIE);
228         rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
229 }
230
231 static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip)
232 {
233         u32 status;
234
235         status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
236         status |= (PCIE_RC_CONFIG_LCS_LBMS | PCIE_RC_CONFIG_LCS_LAMS);
237         rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
238 }
239
240 static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip)
241 {
242         u32 val;
243
244         /* Update Tx credit maximum update interval */
245         val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1);
246         val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK;
247         val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000);       /* ns */
248         rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1);
249 }
250
251 static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
252                                       struct pci_bus *bus, int dev)
253 {
254         /* access only one slot on each root port */
255         if (bus->number == rockchip->root_bus_nr && dev > 0)
256                 return 0;
257
258         /*
259          * do not read more than one device on the bus directly attached
260          * to RC's downstream side.
261          */
262         if (bus->primary == rockchip->root_bus_nr && dev > 0)
263                 return 0;
264
265         return 1;
266 }
267
268 static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
269                                      int where, int size, u32 *val)
270 {
271         void __iomem *addr = rockchip->apb_base + PCIE_RC_CONFIG_BASE + where;
272
273         if (!IS_ALIGNED((uintptr_t)addr, size)) {
274                 *val = 0;
275                 return PCIBIOS_BAD_REGISTER_NUMBER;
276         }
277
278         if (size == 4) {
279                 *val = readl(addr);
280         } else if (size == 2) {
281                 *val = readw(addr);
282         } else if (size == 1) {
283                 *val = readb(addr);
284         } else {
285                 *val = 0;
286                 return PCIBIOS_BAD_REGISTER_NUMBER;
287         }
288         return PCIBIOS_SUCCESSFUL;
289 }
290
291 static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
292                                      int where, int size, u32 val)
293 {
294         u32 mask, tmp, offset;
295
296         offset = where & ~0x3;
297
298         if (size == 4) {
299                 writel(val, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
300                 return PCIBIOS_SUCCESSFUL;
301         }
302
303         mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
304
305         /*
306          * N.B. This read/modify/write isn't safe in general because it can
307          * corrupt RW1C bits in adjacent registers.  But the hardware
308          * doesn't support smaller writes.
309          */
310         tmp = readl(rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset) & mask;
311         tmp |= val << ((where & 0x3) * 8);
312         writel(tmp, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
313
314         return PCIBIOS_SUCCESSFUL;
315 }
316
317 static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
318                                        struct pci_bus *bus, u32 devfn,
319                                        int where, int size, u32 *val)
320 {
321         u32 busdev;
322
323         busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
324                                 PCI_FUNC(devfn), where);
325
326         if (!IS_ALIGNED(busdev, size)) {
327                 *val = 0;
328                 return PCIBIOS_BAD_REGISTER_NUMBER;
329         }
330
331         if (size == 4) {
332                 *val = readl(rockchip->reg_base + busdev);
333         } else if (size == 2) {
334                 *val = readw(rockchip->reg_base + busdev);
335         } else if (size == 1) {
336                 *val = readb(rockchip->reg_base + busdev);
337         } else {
338                 *val = 0;
339                 return PCIBIOS_BAD_REGISTER_NUMBER;
340         }
341         return PCIBIOS_SUCCESSFUL;
342 }
343
344 static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
345                                        struct pci_bus *bus, u32 devfn,
346                                        int where, int size, u32 val)
347 {
348         u32 busdev;
349
350         busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
351                                 PCI_FUNC(devfn), where);
352         if (!IS_ALIGNED(busdev, size))
353                 return PCIBIOS_BAD_REGISTER_NUMBER;
354
355         if (size == 4)
356                 writel(val, rockchip->reg_base + busdev);
357         else if (size == 2)
358                 writew(val, rockchip->reg_base + busdev);
359         else if (size == 1)
360                 writeb(val, rockchip->reg_base + busdev);
361         else
362                 return PCIBIOS_BAD_REGISTER_NUMBER;
363
364         return PCIBIOS_SUCCESSFUL;
365 }
366
367 static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
368                                  int size, u32 *val)
369 {
370         struct rockchip_pcie *rockchip = bus->sysdata;
371
372         if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn))) {
373                 *val = 0xffffffff;
374                 return PCIBIOS_DEVICE_NOT_FOUND;
375         }
376
377         if (bus->number == rockchip->root_bus_nr)
378                 return rockchip_pcie_rd_own_conf(rockchip, where, size, val);
379
380         return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size, val);
381 }
382
383 static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
384                                  int where, int size, u32 val)
385 {
386         struct rockchip_pcie *rockchip = bus->sysdata;
387
388         if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
389                 return PCIBIOS_DEVICE_NOT_FOUND;
390
391         if (bus->number == rockchip->root_bus_nr)
392                 return rockchip_pcie_wr_own_conf(rockchip, where, size, val);
393
394         return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size, val);
395 }
396
397 static struct pci_ops rockchip_pcie_ops = {
398         .read = rockchip_pcie_rd_conf,
399         .write = rockchip_pcie_wr_conf,
400 };
401
402 static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
403 {
404         u32 status, curr, scale, power;
405
406         if (IS_ERR(rockchip->vpcie3v3))
407                 return;
408
409         /*
410          * Set RC's captured slot power limit and scale if
411          * vpcie3v3 available. The default values are both zero
412          * which means the software should set these two according
413          * to the actual power supply.
414          */
415         curr = regulator_get_current_limit(rockchip->vpcie3v3);
416         if (curr > 0) {
417                 scale = 3; /* 0.001x */
418                 curr = curr / 1000; /* convert to mA */
419                 power = (curr * 3300) / 1000; /* milliwatt */
420                 while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
421                         if (!scale) {
422                                 dev_warn(rockchip->dev, "invalid power supply\n");
423                                 return;
424                         }
425                         scale--;
426                         power = power / 10;
427                 }
428
429                 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
430                 status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
431                           (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
432                 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
433         }
434 }
435
436 /**
437  * rockchip_pcie_init_port - Initialize hardware
438  * @rockchip: PCIe port information
439  */
440 static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
441 {
442         struct device *dev = rockchip->dev;
443         int err;
444         u32 status;
445         unsigned long timeout;
446
447         gpiod_set_value(rockchip->ep_gpio, 0);
448
449         err = phy_init(rockchip->phy);
450         if (err < 0) {
451                 dev_err(dev, "fail to init phy, err %d\n", err);
452                 return err;
453         }
454
455         err = reset_control_assert(rockchip->core_rst);
456         if (err) {
457                 dev_err(dev, "assert core_rst err %d\n", err);
458                 return err;
459         }
460
461         err = reset_control_assert(rockchip->mgmt_rst);
462         if (err) {
463                 dev_err(dev, "assert mgmt_rst err %d\n", err);
464                 return err;
465         }
466
467         err = reset_control_assert(rockchip->mgmt_sticky_rst);
468         if (err) {
469                 dev_err(dev, "assert mgmt_sticky_rst err %d\n", err);
470                 return err;
471         }
472
473         err = reset_control_assert(rockchip->pipe_rst);
474         if (err) {
475                 dev_err(dev, "assert pipe_rst err %d\n", err);
476                 return err;
477         }
478
479         rockchip_pcie_write(rockchip,
480                             PCIE_CLIENT_CONF_ENABLE |
481                             PCIE_CLIENT_LINK_TRAIN_ENABLE |
482                             PCIE_CLIENT_ARI_ENABLE |
483                             PCIE_CLIENT_CONF_LANE_NUM(rockchip->lanes) |
484                             PCIE_CLIENT_MODE_RC |
485                             PCIE_CLIENT_GEN_SEL_2,
486                                 PCIE_CLIENT_CONFIG);
487
488         err = phy_power_on(rockchip->phy);
489         if (err) {
490                 dev_err(dev, "fail to power on phy, err %d\n", err);
491                 return err;
492         }
493
494         /*
495          * Please don't reorder the deassert sequence of the following
496          * four reset pins.
497          */
498         err = reset_control_deassert(rockchip->mgmt_sticky_rst);
499         if (err) {
500                 dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err);
501                 return err;
502         }
503
504         err = reset_control_deassert(rockchip->core_rst);
505         if (err) {
506                 dev_err(dev, "deassert core_rst err %d\n", err);
507                 return err;
508         }
509
510         err = reset_control_deassert(rockchip->mgmt_rst);
511         if (err) {
512                 dev_err(dev, "deassert mgmt_rst err %d\n", err);
513                 return err;
514         }
515
516         err = reset_control_deassert(rockchip->pipe_rst);
517         if (err) {
518                 dev_err(dev, "deassert pipe_rst err %d\n", err);
519                 return err;
520         }
521
522         /*
523          * We need to read/write PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 before
524          * enabling ASPM.  Otherwise L1PwrOnSc and L1PwrOnVal isn't
525          * reliable and enabling ASPM doesn't work.  This is a controller
526          * bug we need to work around.
527          */
528         status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2);
529         rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2);
530
531         /* Fix the transmitted FTS count desired to exit from L0s. */
532         status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1);
533         status = (status & PCIE_CORE_CTRL_PLC1_FTS_MASK) |
534                  (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT);
535         rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1);
536
537         rockchip_pcie_set_power_limit(rockchip);
538
539         /* Enable Gen1 training */
540         rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
541                             PCIE_CLIENT_CONFIG);
542
543         gpiod_set_value(rockchip->ep_gpio, 1);
544
545         /* 500ms timeout value should be enough for Gen1/2 training */
546         timeout = jiffies + msecs_to_jiffies(500);
547
548         for (;;) {
549                 status = rockchip_pcie_read(rockchip,
550                                             PCIE_CLIENT_BASIC_STATUS1);
551                 if ((status & PCIE_CLIENT_LINK_STATUS_MASK) ==
552                     PCIE_CLIENT_LINK_STATUS_UP) {
553                         dev_dbg(dev, "PCIe link training gen1 pass!\n");
554                         break;
555                 }
556
557                 if (time_after(jiffies, timeout)) {
558                         dev_err(dev, "PCIe link training gen1 timeout!\n");
559                         return -ETIMEDOUT;
560                 }
561
562                 msleep(20);
563         }
564
565         /*
566          * Enable retrain for gen2. This should be configured only after
567          * gen1 finished.
568          */
569         status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
570         status |= PCIE_RC_CONFIG_LCS_RETRAIN_LINK;
571         rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
572
573         timeout = jiffies + msecs_to_jiffies(500);
574         for (;;) {
575                 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
576                 if ((status & PCIE_CORE_PL_CONF_SPEED_MASK) ==
577                     PCIE_CORE_PL_CONF_SPEED_5G) {
578                         dev_dbg(dev, "PCIe link training gen2 pass!\n");
579                         break;
580                 }
581
582                 if (time_after(jiffies, timeout)) {
583                         dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
584                         break;
585                 }
586
587                 msleep(20);
588         }
589
590         /* Check the final link width from negotiated lane counter from MGMT */
591         status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
592         status =  0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >>
593                           PCIE_CORE_PL_CONF_LANE_MASK);
594         dev_dbg(dev, "current link width is x%d\n", status);
595
596         rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
597                             PCIE_RC_CONFIG_VENDOR);
598         rockchip_pcie_write(rockchip,
599                             PCI_CLASS_BRIDGE_PCI << PCIE_RC_CONFIG_SCC_SHIFT,
600                             PCIE_RC_CONFIG_RID_CCR);
601         rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF);
602
603         rockchip_pcie_write(rockchip,
604                             (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
605                             PCIE_CORE_OB_REGION_ADDR0);
606         rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
607                             PCIE_CORE_OB_REGION_ADDR1);
608         rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
609         rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
610
611         return 0;
612 }
613
614 static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg)
615 {
616         struct rockchip_pcie *rockchip = arg;
617         struct device *dev = rockchip->dev;
618         u32 reg;
619         u32 sub_reg;
620
621         reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
622         if (reg & PCIE_CLIENT_INT_LOCAL) {
623                 dev_dbg(dev, "local interrupt received\n");
624                 sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS);
625                 if (sub_reg & PCIE_CORE_INT_PRFPE)
626                         dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n");
627
628                 if (sub_reg & PCIE_CORE_INT_CRFPE)
629                         dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n");
630
631                 if (sub_reg & PCIE_CORE_INT_RRPE)
632                         dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n");
633
634                 if (sub_reg & PCIE_CORE_INT_PRFO)
635                         dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n");
636
637                 if (sub_reg & PCIE_CORE_INT_CRFO)
638                         dev_dbg(dev, "overflow occurred in the completion receive FIFO\n");
639
640                 if (sub_reg & PCIE_CORE_INT_RT)
641                         dev_dbg(dev, "replay timer timed out\n");
642
643                 if (sub_reg & PCIE_CORE_INT_RTR)
644                         dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n");
645
646                 if (sub_reg & PCIE_CORE_INT_PE)
647                         dev_dbg(dev, "phy error detected on receive side\n");
648
649                 if (sub_reg & PCIE_CORE_INT_MTR)
650                         dev_dbg(dev, "malformed TLP received from the link\n");
651
652                 if (sub_reg & PCIE_CORE_INT_UCR)
653                         dev_dbg(dev, "malformed TLP received from the link\n");
654
655                 if (sub_reg & PCIE_CORE_INT_FCE)
656                         dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n");
657
658                 if (sub_reg & PCIE_CORE_INT_CT)
659                         dev_dbg(dev, "a request timed out waiting for completion\n");
660
661                 if (sub_reg & PCIE_CORE_INT_UTC)
662                         dev_dbg(dev, "unmapped TC error\n");
663
664                 if (sub_reg & PCIE_CORE_INT_MMVC)
665                         dev_dbg(dev, "MSI mask register changes\n");
666
667                 rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS);
668         } else if (reg & PCIE_CLIENT_INT_PHY) {
669                 dev_dbg(dev, "phy link changes\n");
670                 rockchip_pcie_update_txcredit_mui(rockchip);
671                 rockchip_pcie_clr_bw_int(rockchip);
672         }
673
674         rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL,
675                             PCIE_CLIENT_INT_STATUS);
676
677         return IRQ_HANDLED;
678 }
679
680 static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg)
681 {
682         struct rockchip_pcie *rockchip = arg;
683         struct device *dev = rockchip->dev;
684         u32 reg;
685
686         reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
687         if (reg & PCIE_CLIENT_INT_LEGACY_DONE)
688                 dev_dbg(dev, "legacy done interrupt received\n");
689
690         if (reg & PCIE_CLIENT_INT_MSG)
691                 dev_dbg(dev, "message done interrupt received\n");
692
693         if (reg & PCIE_CLIENT_INT_HOT_RST)
694                 dev_dbg(dev, "hot reset interrupt received\n");
695
696         if (reg & PCIE_CLIENT_INT_DPA)
697                 dev_dbg(dev, "dpa interrupt received\n");
698
699         if (reg & PCIE_CLIENT_INT_FATAL_ERR)
700                 dev_dbg(dev, "fatal error interrupt received\n");
701
702         if (reg & PCIE_CLIENT_INT_NFATAL_ERR)
703                 dev_dbg(dev, "no fatal error interrupt received\n");
704
705         if (reg & PCIE_CLIENT_INT_CORR_ERR)
706                 dev_dbg(dev, "correctable error interrupt received\n");
707
708         if (reg & PCIE_CLIENT_INT_PHY)
709                 dev_dbg(dev, "phy interrupt received\n");
710
711         rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE |
712                               PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST |
713                               PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR |
714                               PCIE_CLIENT_INT_NFATAL_ERR |
715                               PCIE_CLIENT_INT_CORR_ERR |
716                               PCIE_CLIENT_INT_PHY),
717                    PCIE_CLIENT_INT_STATUS);
718
719         return IRQ_HANDLED;
720 }
721
722 static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc)
723 {
724         struct irq_chip *chip = irq_desc_get_chip(desc);
725         struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
726         struct device *dev = rockchip->dev;
727         u32 reg;
728         u32 hwirq;
729         u32 virq;
730
731         chained_irq_enter(chip, desc);
732
733         reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
734         reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT;
735
736         while (reg) {
737                 hwirq = ffs(reg) - 1;
738                 reg &= ~BIT(hwirq);
739
740                 virq = irq_find_mapping(rockchip->irq_domain, hwirq);
741                 if (virq)
742                         generic_handle_irq(virq);
743                 else
744                         dev_err(dev, "unexpected IRQ, INT%d\n", hwirq);
745         }
746
747         chained_irq_exit(chip, desc);
748 }
749
750
751 /**
752  * rockchip_pcie_parse_dt - Parse Device Tree
753  * @rockchip: PCIe port information
754  *
755  * Return: '0' on success and error value on failure
756  */
757 static int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip)
758 {
759         struct device *dev = rockchip->dev;
760         struct platform_device *pdev = to_platform_device(dev);
761         struct device_node *node = dev->of_node;
762         struct resource *regs;
763         int irq;
764         int err;
765
766         regs = platform_get_resource_byname(pdev,
767                                             IORESOURCE_MEM,
768                                             "axi-base");
769         rockchip->reg_base = devm_ioremap_resource(dev, regs);
770         if (IS_ERR(rockchip->reg_base))
771                 return PTR_ERR(rockchip->reg_base);
772
773         regs = platform_get_resource_byname(pdev,
774                                             IORESOURCE_MEM,
775                                             "apb-base");
776         rockchip->apb_base = devm_ioremap_resource(dev, regs);
777         if (IS_ERR(rockchip->apb_base))
778                 return PTR_ERR(rockchip->apb_base);
779
780         rockchip->phy = devm_phy_get(dev, "pcie-phy");
781         if (IS_ERR(rockchip->phy)) {
782                 if (PTR_ERR(rockchip->phy) != -EPROBE_DEFER)
783                         dev_err(dev, "missing phy\n");
784                 return PTR_ERR(rockchip->phy);
785         }
786
787         rockchip->lanes = 1;
788         err = of_property_read_u32(node, "num-lanes", &rockchip->lanes);
789         if (!err && (rockchip->lanes == 0 ||
790                      rockchip->lanes == 3 ||
791                      rockchip->lanes > 4)) {
792                 dev_warn(dev, "invalid num-lanes, default to use one lane\n");
793                 rockchip->lanes = 1;
794         }
795
796         rockchip->core_rst = devm_reset_control_get(dev, "core");
797         if (IS_ERR(rockchip->core_rst)) {
798                 if (PTR_ERR(rockchip->core_rst) != -EPROBE_DEFER)
799                         dev_err(dev, "missing core reset property in node\n");
800                 return PTR_ERR(rockchip->core_rst);
801         }
802
803         rockchip->mgmt_rst = devm_reset_control_get(dev, "mgmt");
804         if (IS_ERR(rockchip->mgmt_rst)) {
805                 if (PTR_ERR(rockchip->mgmt_rst) != -EPROBE_DEFER)
806                         dev_err(dev, "missing mgmt reset property in node\n");
807                 return PTR_ERR(rockchip->mgmt_rst);
808         }
809
810         rockchip->mgmt_sticky_rst = devm_reset_control_get(dev, "mgmt-sticky");
811         if (IS_ERR(rockchip->mgmt_sticky_rst)) {
812                 if (PTR_ERR(rockchip->mgmt_sticky_rst) != -EPROBE_DEFER)
813                         dev_err(dev, "missing mgmt-sticky reset property in node\n");
814                 return PTR_ERR(rockchip->mgmt_sticky_rst);
815         }
816
817         rockchip->pipe_rst = devm_reset_control_get(dev, "pipe");
818         if (IS_ERR(rockchip->pipe_rst)) {
819                 if (PTR_ERR(rockchip->pipe_rst) != -EPROBE_DEFER)
820                         dev_err(dev, "missing pipe reset property in node\n");
821                 return PTR_ERR(rockchip->pipe_rst);
822         }
823
824         rockchip->ep_gpio = devm_gpiod_get(dev, "ep", GPIOD_OUT_HIGH);
825         if (IS_ERR(rockchip->ep_gpio)) {
826                 dev_err(dev, "missing ep-gpios property in node\n");
827                 return PTR_ERR(rockchip->ep_gpio);
828         }
829
830         rockchip->aclk_pcie = devm_clk_get(dev, "aclk");
831         if (IS_ERR(rockchip->aclk_pcie)) {
832                 dev_err(dev, "aclk clock not found\n");
833                 return PTR_ERR(rockchip->aclk_pcie);
834         }
835
836         rockchip->aclk_perf_pcie = devm_clk_get(dev, "aclk-perf");
837         if (IS_ERR(rockchip->aclk_perf_pcie)) {
838                 dev_err(dev, "aclk_perf clock not found\n");
839                 return PTR_ERR(rockchip->aclk_perf_pcie);
840         }
841
842         rockchip->hclk_pcie = devm_clk_get(dev, "hclk");
843         if (IS_ERR(rockchip->hclk_pcie)) {
844                 dev_err(dev, "hclk clock not found\n");
845                 return PTR_ERR(rockchip->hclk_pcie);
846         }
847
848         rockchip->clk_pcie_pm = devm_clk_get(dev, "pm");
849         if (IS_ERR(rockchip->clk_pcie_pm)) {
850                 dev_err(dev, "pm clock not found\n");
851                 return PTR_ERR(rockchip->clk_pcie_pm);
852         }
853
854         irq = platform_get_irq_byname(pdev, "sys");
855         if (irq < 0) {
856                 dev_err(dev, "missing sys IRQ resource\n");
857                 return -EINVAL;
858         }
859
860         err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler,
861                                IRQF_SHARED, "pcie-sys", rockchip);
862         if (err) {
863                 dev_err(dev, "failed to request PCIe subsystem IRQ\n");
864                 return err;
865         }
866
867         irq = platform_get_irq_byname(pdev, "legacy");
868         if (irq < 0) {
869                 dev_err(dev, "missing legacy IRQ resource\n");
870                 return -EINVAL;
871         }
872
873         irq_set_chained_handler_and_data(irq,
874                                          rockchip_pcie_legacy_int_handler,
875                                          rockchip);
876
877         irq = platform_get_irq_byname(pdev, "client");
878         if (irq < 0) {
879                 dev_err(dev, "missing client IRQ resource\n");
880                 return -EINVAL;
881         }
882
883         err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler,
884                                IRQF_SHARED, "pcie-client", rockchip);
885         if (err) {
886                 dev_err(dev, "failed to request PCIe client IRQ\n");
887                 return err;
888         }
889
890         rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
891         if (IS_ERR(rockchip->vpcie3v3)) {
892                 if (PTR_ERR(rockchip->vpcie3v3) == -EPROBE_DEFER)
893                         return -EPROBE_DEFER;
894                 dev_info(dev, "no vpcie3v3 regulator found\n");
895         }
896
897         rockchip->vpcie1v8 = devm_regulator_get_optional(dev, "vpcie1v8");
898         if (IS_ERR(rockchip->vpcie1v8)) {
899                 if (PTR_ERR(rockchip->vpcie1v8) == -EPROBE_DEFER)
900                         return -EPROBE_DEFER;
901                 dev_info(dev, "no vpcie1v8 regulator found\n");
902         }
903
904         rockchip->vpcie0v9 = devm_regulator_get_optional(dev, "vpcie0v9");
905         if (IS_ERR(rockchip->vpcie0v9)) {
906                 if (PTR_ERR(rockchip->vpcie0v9) == -EPROBE_DEFER)
907                         return -EPROBE_DEFER;
908                 dev_info(dev, "no vpcie0v9 regulator found\n");
909         }
910
911         return 0;
912 }
913
914 static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip)
915 {
916         struct device *dev = rockchip->dev;
917         int err;
918
919         if (!IS_ERR(rockchip->vpcie3v3)) {
920                 err = regulator_enable(rockchip->vpcie3v3);
921                 if (err) {
922                         dev_err(dev, "fail to enable vpcie3v3 regulator\n");
923                         goto err_out;
924                 }
925         }
926
927         if (!IS_ERR(rockchip->vpcie1v8)) {
928                 err = regulator_enable(rockchip->vpcie1v8);
929                 if (err) {
930                         dev_err(dev, "fail to enable vpcie1v8 regulator\n");
931                         goto err_disable_3v3;
932                 }
933         }
934
935         if (!IS_ERR(rockchip->vpcie0v9)) {
936                 err = regulator_enable(rockchip->vpcie0v9);
937                 if (err) {
938                         dev_err(dev, "fail to enable vpcie0v9 regulator\n");
939                         goto err_disable_1v8;
940                 }
941         }
942
943         return 0;
944
945 err_disable_1v8:
946         if (!IS_ERR(rockchip->vpcie1v8))
947                 regulator_disable(rockchip->vpcie1v8);
948 err_disable_3v3:
949         if (!IS_ERR(rockchip->vpcie3v3))
950                 regulator_disable(rockchip->vpcie3v3);
951 err_out:
952         return err;
953 }
954
955 static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip)
956 {
957         rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) &
958                             (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK);
959         rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT),
960                             PCIE_CORE_INT_MASK);
961
962         rockchip_pcie_enable_bw_int(rockchip);
963 }
964
965 static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
966                                   irq_hw_number_t hwirq)
967 {
968         irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
969         irq_set_chip_data(irq, domain->host_data);
970
971         return 0;
972 }
973
974 static const struct irq_domain_ops intx_domain_ops = {
975         .map = rockchip_pcie_intx_map,
976 };
977
978 static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
979 {
980         struct device *dev = rockchip->dev;
981         struct device_node *intc = of_get_next_child(dev->of_node, NULL);
982
983         if (!intc) {
984                 dev_err(dev, "missing child interrupt-controller node\n");
985                 return -EINVAL;
986         }
987
988         rockchip->irq_domain = irq_domain_add_linear(intc, 4,
989                                                     &intx_domain_ops, rockchip);
990         if (!rockchip->irq_domain) {
991                 dev_err(dev, "failed to get a INTx IRQ domain\n");
992                 return -EINVAL;
993         }
994
995         return 0;
996 }
997
998 static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
999                                      int region_no, int type, u8 num_pass_bits,
1000                                      u32 lower_addr, u32 upper_addr)
1001 {
1002         u32 ob_addr_0;
1003         u32 ob_addr_1;
1004         u32 ob_desc_0;
1005         u32 aw_offset;
1006
1007         if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
1008                 return -EINVAL;
1009         if (num_pass_bits + 1 < 8)
1010                 return -EINVAL;
1011         if (num_pass_bits > 63)
1012                 return -EINVAL;
1013         if (region_no == 0) {
1014                 if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
1015                 return -EINVAL;
1016         }
1017         if (region_no != 0) {
1018                 if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
1019                         return -EINVAL;
1020         }
1021
1022         aw_offset = (region_no << OB_REG_SIZE_SHIFT);
1023
1024         ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
1025         ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
1026         ob_addr_1 = upper_addr;
1027         ob_desc_0 = (1 << 23 | type);
1028
1029         rockchip_pcie_write(rockchip, ob_addr_0,
1030                             PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
1031         rockchip_pcie_write(rockchip, ob_addr_1,
1032                             PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
1033         rockchip_pcie_write(rockchip, ob_desc_0,
1034                             PCIE_CORE_OB_REGION_DESC0 + aw_offset);
1035         rockchip_pcie_write(rockchip, 0,
1036                             PCIE_CORE_OB_REGION_DESC1 + aw_offset);
1037
1038         return 0;
1039 }
1040
1041 static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
1042                                      int region_no, u8 num_pass_bits,
1043                                      u32 lower_addr, u32 upper_addr)
1044 {
1045         u32 ib_addr_0;
1046         u32 ib_addr_1;
1047         u32 aw_offset;
1048
1049         if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM)
1050                 return -EINVAL;
1051         if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED)
1052                 return -EINVAL;
1053         if (num_pass_bits > 63)
1054                 return -EINVAL;
1055
1056         aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT);
1057
1058         ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS;
1059         ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR;
1060         ib_addr_1 = upper_addr;
1061
1062         rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset);
1063         rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset);
1064
1065         return 0;
1066 }
1067
1068 static int rockchip_pcie_probe(struct platform_device *pdev)
1069 {
1070         struct rockchip_pcie *rockchip;
1071         struct device *dev = &pdev->dev;
1072         struct pci_bus *bus, *child;
1073         struct resource_entry *win;
1074         resource_size_t io_base;
1075         struct resource *mem;
1076         struct resource *io;
1077         phys_addr_t io_bus_addr = 0;
1078         u32 io_size;
1079         phys_addr_t mem_bus_addr = 0;
1080         u32 mem_size = 0;
1081         int reg_no;
1082         int err;
1083         int offset;
1084
1085         LIST_HEAD(res);
1086
1087         if (!dev->of_node)
1088                 return -ENODEV;
1089
1090         rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
1091         if (!rockchip)
1092                 return -ENOMEM;
1093
1094         rockchip->dev = dev;
1095
1096         err = rockchip_pcie_parse_dt(rockchip);
1097         if (err)
1098                 return err;
1099
1100         err = clk_prepare_enable(rockchip->aclk_pcie);
1101         if (err) {
1102                 dev_err(dev, "unable to enable aclk_pcie clock\n");
1103                 goto err_aclk_pcie;
1104         }
1105
1106         err = clk_prepare_enable(rockchip->aclk_perf_pcie);
1107         if (err) {
1108                 dev_err(dev, "unable to enable aclk_perf_pcie clock\n");
1109                 goto err_aclk_perf_pcie;
1110         }
1111
1112         err = clk_prepare_enable(rockchip->hclk_pcie);
1113         if (err) {
1114                 dev_err(dev, "unable to enable hclk_pcie clock\n");
1115                 goto err_hclk_pcie;
1116         }
1117
1118         err = clk_prepare_enable(rockchip->clk_pcie_pm);
1119         if (err) {
1120                 dev_err(dev, "unable to enable hclk_pcie clock\n");
1121                 goto err_pcie_pm;
1122         }
1123
1124         err = rockchip_pcie_set_vpcie(rockchip);
1125         if (err) {
1126                 dev_err(dev, "failed to set vpcie regulator\n");
1127                 goto err_set_vpcie;
1128         }
1129
1130         err = rockchip_pcie_init_port(rockchip);
1131         if (err)
1132                 goto err_vpcie;
1133
1134         platform_set_drvdata(pdev, rockchip);
1135
1136         rockchip_pcie_enable_interrupts(rockchip);
1137
1138         err = rockchip_pcie_init_irq_domain(rockchip);
1139         if (err < 0)
1140                 goto err_vpcie;
1141
1142         err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff,
1143                                                &res, &io_base);
1144         if (err)
1145                 goto err_vpcie;
1146
1147         err = devm_request_pci_bus_resources(dev, &res);
1148         if (err)
1149                 goto err_vpcie;
1150
1151         /* Get the I/O and memory ranges from DT */
1152         io_size = 0;
1153         resource_list_for_each_entry(win, &res) {
1154                 switch (resource_type(win->res)) {
1155                 case IORESOURCE_IO:
1156                         io = win->res;
1157                         io->name = "I/O";
1158                         io_size = resource_size(io);
1159                         io_bus_addr = io->start - win->offset;
1160                         err = pci_remap_iospace(io, io_base);
1161                         if (err) {
1162                                 dev_warn(dev, "error %d: failed to map resource %pR\n",
1163                                          err, io);
1164                                 continue;
1165                         }
1166                         break;
1167                 case IORESOURCE_MEM:
1168                         mem = win->res;
1169                         mem->name = "MEM";
1170                         mem_size = resource_size(mem);
1171                         mem_bus_addr = mem->start - win->offset;
1172                         break;
1173                 case IORESOURCE_BUS:
1174                         rockchip->root_bus_nr = win->res->start;
1175                         break;
1176                 default:
1177                         continue;
1178                 }
1179         }
1180
1181         if (mem_size) {
1182                 for (reg_no = 0; reg_no < (mem_size >> 20); reg_no++) {
1183                         err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
1184                                                         AXI_WRAPPER_MEM_WRITE,
1185                                                         20 - 1,
1186                                                         mem_bus_addr +
1187                                                         (reg_no << 20),
1188                                                         0);
1189                         if (err) {
1190                                 dev_err(dev, "program RC mem outbound ATU failed\n");
1191                                 goto err_vpcie;
1192                         }
1193                 }
1194         }
1195
1196         err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0);
1197         if (err) {
1198                 dev_err(dev, "program RC mem inbound ATU failed\n");
1199                 goto err_vpcie;
1200         }
1201
1202         offset = mem_size >> 20;
1203
1204         if (io_size) {
1205                 for (reg_no = 0; reg_no < (io_size >> 20); reg_no++) {
1206                         err = rockchip_pcie_prog_ob_atu(rockchip,
1207                                                         reg_no + 1 + offset,
1208                                                         AXI_WRAPPER_IO_WRITE,
1209                                                         20 - 1,
1210                                                         io_bus_addr +
1211                                                         (reg_no << 20),
1212                                                         0);
1213                         if (err) {
1214                                 dev_err(dev, "program RC io outbound ATU failed\n");
1215                                 goto err_vpcie;
1216                         }
1217                 }
1218         }
1219
1220         bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
1221         if (!bus) {
1222                 err = -ENOMEM;
1223                 goto err_vpcie;
1224         }
1225
1226         pci_bus_size_bridges(bus);
1227         pci_bus_assign_resources(bus);
1228         list_for_each_entry(child, &bus->children, node)
1229                 pcie_bus_configure_settings(child);
1230
1231         pci_bus_add_devices(bus);
1232
1233         dev_warn(dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
1234
1235         return err;
1236
1237 err_vpcie:
1238         if (!IS_ERR(rockchip->vpcie3v3))
1239                 regulator_disable(rockchip->vpcie3v3);
1240         if (!IS_ERR(rockchip->vpcie1v8))
1241                 regulator_disable(rockchip->vpcie1v8);
1242         if (!IS_ERR(rockchip->vpcie0v9))
1243                 regulator_disable(rockchip->vpcie0v9);
1244 err_set_vpcie:
1245         clk_disable_unprepare(rockchip->clk_pcie_pm);
1246 err_pcie_pm:
1247         clk_disable_unprepare(rockchip->hclk_pcie);
1248 err_hclk_pcie:
1249         clk_disable_unprepare(rockchip->aclk_perf_pcie);
1250 err_aclk_perf_pcie:
1251         clk_disable_unprepare(rockchip->aclk_pcie);
1252 err_aclk_pcie:
1253         return err;
1254 }
1255
1256 static const struct of_device_id rockchip_pcie_of_match[] = {
1257         { .compatible = "rockchip,rk3399-pcie", },
1258         {}
1259 };
1260
1261 static struct platform_driver rockchip_pcie_driver = {
1262         .driver = {
1263                 .name = "rockchip-pcie",
1264                 .of_match_table = rockchip_pcie_of_match,
1265         },
1266         .probe = rockchip_pcie_probe,
1267
1268 };
1269 builtin_platform_driver(rockchip_pcie_driver);