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