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