forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 ee930fffee469d076998274a2ca55e13dc1efb67
kernel/drivers/pci/controller/dwc/pcie-dw-rockchip.c
....@@ -8,12 +8,14 @@
88 * Author: Simon Xue <xxm@rock-chips.com>
99 */
1010
11
+#include <dt-bindings/phy/phy.h>
1112 #include <linux/clk.h>
1213 #include <linux/delay.h>
1314 #include <linux/fs.h>
1415 #include <linux/gpio.h>
1516 #include <linux/init.h>
1617 #include <linux/interrupt.h>
18
+#include <linux/iopoll.h>
1719 #include <linux/irq.h>
1820 #include <linux/irqchip/chained_irq.h>
1921 #include <linux/irqdomain.h>
....@@ -29,11 +31,13 @@
2931 #include <linux/of_pci.h>
3032 #include <linux/pci.h>
3133 #include <linux/phy/phy.h>
34
+#include <linux/phy/pcie.h>
3235 #include <linux/platform_device.h>
3336 #include <linux/poll.h>
3437 #include <linux/regmap.h>
3538 #include <linux/reset.h>
3639 #include <linux/resource.h>
40
+#include <linux/rfkill-wlan.h>
3741 #include <linux/signal.h>
3842 #include <linux/types.h>
3943 #include <linux/uaccess.h>
....@@ -49,13 +53,11 @@
4953 RK_PCIE_RC_TYPE,
5054 };
5155
52
-struct reset_bulk_data {
53
- const char *id;
54
- struct reset_control *rst;
55
-};
56
+#define RK_PCIE_DBG 0
5657
5758 #define PCIE_DMA_OFFSET 0x380000
5859
60
+#define PCIE_DMA_CTRL_OFF 0x8
5961 #define PCIE_DMA_WR_ENB 0xc
6062 #define PCIE_DMA_WR_CTRL_LO 0x200
6163 #define PCIE_DMA_WR_CTRL_HI 0x204
....@@ -99,6 +101,8 @@
99101
100102 #define PCIE_CAP_LINK_CONTROL2_LINK_STATUS 0xa0
101103
104
+#define PCIE_CLIENT_INTR_STATUS_MSG_RX 0x04
105
+#define PME_TO_ACK (BIT(9) | BIT(25))
102106 #define PCIE_CLIENT_INTR_STATUS_LEGACY 0x08
103107 #define PCIE_CLIENT_INTR_STATUS_MISC 0x10
104108 #define PCIE_CLIENT_INTR_MASK_LEGACY 0x1c
....@@ -106,12 +110,21 @@
106110 #define MASK_LEGACY_INT(x) (0x00110011 << x)
107111 #define UNMASK_LEGACY_INT(x) (0x00110000 << x)
108112 #define PCIE_CLIENT_INTR_MASK 0x24
113
+#define PCIE_CLIENT_POWER 0x2c
114
+#define READY_ENTER_L23 BIT(3)
115
+#define PCIE_CLIENT_MSG_GEN 0x34
116
+#define PME_TURN_OFF (BIT(4) | BIT(20))
109117 #define PCIE_CLIENT_GENERAL_DEBUG 0x104
110118 #define PCIE_CLIENT_HOT_RESET_CTRL 0x180
119
+#define PCIE_LTSSM_APP_DLY1_EN BIT(0)
120
+#define PCIE_LTSSM_APP_DLY2_EN BIT(1)
121
+#define PCIE_LTSSM_APP_DLY1_DONE BIT(2)
122
+#define PCIE_LTSSM_APP_DLY2_DONE BIT(3)
111123 #define PCIE_LTSSM_ENABLE_ENHANCE BIT(4)
112124 #define PCIE_CLIENT_LTSSM_STATUS 0x300
113125 #define SMLH_LINKUP BIT(16)
114126 #define RDLH_LINKUP BIT(17)
127
+#define PCIE_CLIENT_CDM_RASDES_TBA_INFO_CMN 0x154
115128 #define PCIE_CLIENT_DBG_FIFO_MODE_CON 0x310
116129 #define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0 0x320
117130 #define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1 0x324
....@@ -119,21 +132,32 @@
119132 #define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1 0x32c
120133 #define PCIE_CLIENT_DBG_FIFO_STATUS 0x350
121134 #define PCIE_CLIENT_DBG_TRANSITION_DATA 0xffff0000
122
-#define PCIE_CLIENT_DBF_EN 0xffff0003
123
-#define RK_PCIE_DBG 0
135
+#define PCIE_CLIENT_DBF_EN 0xffff0007
124136
125137 #define PCIE_PHY_LINKUP BIT(0)
126138 #define PCIE_DATA_LINKUP BIT(1)
127139
128
-#define PCIE_RESBAR_CTRL_REG0_REG 0x2a8
140
+#define PCIE_TYPE0_HDR_DBI2_OFFSET 0x100000
129141 #define PCIE_SB_BAR0_MASK_REG 0x100010
130142
131143 #define PCIE_PL_ORDER_RULE_CTRL_OFF 0x8B4
144
+#define RK_PCIE_L2_TMOUT_US 5000
145
+#define RK_PCIE_HOTRESET_TMOUT_US 10000
146
+#define RK_PCIE_ENUM_HW_RETRYIES 2
147
+
148
+enum rk_pcie_ltssm_code {
149
+ S_L0 = 0x11,
150
+ S_L0S = 0x12,
151
+ S_L1_IDLE = 0x14,
152
+ S_L2_IDLE = 0x15,
153
+ S_MAX = 0x1f,
154
+};
132155
133156 struct rk_pcie {
134157 struct dw_pcie *pci;
135158 enum rk_pcie_device_mode mode;
136159 enum phy_mode phy_mode;
160
+ int phy_sub_mode;
137161 unsigned char bar_to_atu[6];
138162 phys_addr_t *outbound_addr;
139163 unsigned long *ib_window_map;
....@@ -144,9 +168,11 @@
144168 void __iomem *apb_base;
145169 struct phy *phy;
146170 struct clk_bulk_data *clks;
171
+ struct reset_control *rsts;
147172 unsigned int clk_cnt;
148
- struct reset_bulk_data *rsts;
149173 struct gpio_desc *rst_gpio;
174
+ u32 perst_inactive_ms;
175
+ struct gpio_desc *prsnt_gpio;
150176 phys_addr_t mem_start;
151177 size_t mem_size;
152178 struct pcie_port pp;
....@@ -154,21 +180,32 @@
154180 struct regmap *pmu_grf;
155181 struct dma_trx_obj *dma_obj;
156182 bool in_suspend;
183
+ bool skip_scan_in_resume;
157184 bool is_rk1808;
158185 bool is_signal_test;
159186 bool bifurcation;
187
+ bool supports_clkreq;
160188 struct regulator *vpcie3v3;
161189 struct irq_domain *irq_domain;
162190 raw_spinlock_t intx_lock;
191
+ u16 aspm;
192
+ u32 l1ss_ctl1;
163193 struct dentry *debugfs;
194
+ u32 msi_vector_num;
195
+ struct workqueue_struct *hot_rst_wq;
196
+ struct work_struct hot_rst_work;
197
+ u32 comp_prst[2];
198
+ u32 intx;
164199 };
165200
166201 struct rk_pcie_of_data {
167202 enum rk_pcie_device_mode mode;
203
+ u32 msi_vector_num;
168204 };
169205
170206 #define to_rk_pcie(x) dev_get_drvdata((x)->dev)
171
-static const struct dev_pm_ops rockchip_dw_pcie_pm_ops;
207
+static int rk_pcie_disable_power(struct rk_pcie *rk_pcie);
208
+static int rk_pcie_enable_power(struct rk_pcie *rk_pcie);
172209
173210 static int rk_pcie_read(void __iomem *addr, int size, u32 *val)
174211 {
....@@ -253,12 +290,154 @@
253290 return 0;
254291 }
255292
293
+static void rk_pcie_writel_atu(struct dw_pcie *pci, u32 reg, u32 val)
294
+{
295
+ int ret;
296
+
297
+ if (pci->ops->write_dbi) {
298
+ pci->ops->write_dbi(pci, pci->atu_base, reg, 4, val);
299
+ return;
300
+ }
301
+
302
+ ret = dw_pcie_write(pci->atu_base + reg, 4, val);
303
+ if (ret)
304
+ dev_err(pci->dev, "Write ATU address failed\n");
305
+}
306
+
307
+static void rk_pcie_writel_ib_unroll(struct dw_pcie *pci, u32 index, u32 reg,
308
+ u32 val)
309
+{
310
+ u32 offset = PCIE_GET_ATU_INB_UNR_REG_OFFSET(index);
311
+
312
+ rk_pcie_writel_atu(pci, offset + reg, val);
313
+}
314
+
315
+static u32 rk_pcie_readl_atu(struct dw_pcie *pci, u32 reg)
316
+{
317
+ int ret;
318
+ u32 val;
319
+
320
+ if (pci->ops->read_dbi)
321
+ return pci->ops->read_dbi(pci, pci->atu_base, reg, 4);
322
+
323
+ ret = dw_pcie_read(pci->atu_base + reg, 4, &val);
324
+ if (ret)
325
+ dev_err(pci->dev, "Read ATU address failed\n");
326
+
327
+ return val;
328
+}
329
+
330
+static u32 rk_pcie_readl_ib_unroll(struct dw_pcie *pci, u32 index, u32 reg)
331
+{
332
+ u32 offset = PCIE_GET_ATU_INB_UNR_REG_OFFSET(index);
333
+
334
+ return rk_pcie_readl_atu(pci, offset + reg);
335
+}
336
+
337
+static int rk_pcie_prog_inbound_atu_unroll(struct dw_pcie *pci, u8 func_no,
338
+ int index, int bar, u64 cpu_addr,
339
+ enum dw_pcie_as_type as_type)
340
+{
341
+ int type;
342
+ u32 retries, val;
343
+
344
+ rk_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
345
+ lower_32_bits(cpu_addr));
346
+ rk_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
347
+ upper_32_bits(cpu_addr));
348
+
349
+ switch (as_type) {
350
+ case DW_PCIE_AS_MEM:
351
+ type = PCIE_ATU_TYPE_MEM;
352
+ break;
353
+ case DW_PCIE_AS_IO:
354
+ type = PCIE_ATU_TYPE_IO;
355
+ break;
356
+ default:
357
+ return -EINVAL;
358
+ }
359
+
360
+ rk_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1, type |
361
+ PCIE_ATU_FUNC_NUM(func_no));
362
+ rk_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
363
+ PCIE_ATU_FUNC_NUM_MATCH_EN |
364
+ PCIE_ATU_ENABLE |
365
+ PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
366
+
367
+ /*
368
+ * Make sure ATU enable takes effect before any subsequent config
369
+ * and I/O accesses.
370
+ */
371
+ for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
372
+ val = rk_pcie_readl_ib_unroll(pci, index,
373
+ PCIE_ATU_UNR_REGION_CTRL2);
374
+ if (val & PCIE_ATU_ENABLE)
375
+ return 0;
376
+
377
+ mdelay(LINK_WAIT_IATU);
378
+ }
379
+ dev_err(pci->dev, "Inbound iATU is not being enabled\n");
380
+
381
+ return -EBUSY;
382
+}
383
+
384
+
385
+static int rk_pcie_prog_inbound_atu(struct dw_pcie *pci, u8 func_no, int index,
386
+ int bar, u64 cpu_addr,
387
+ enum dw_pcie_as_type as_type)
388
+{
389
+ int type;
390
+ u32 retries, val;
391
+
392
+ if (pci->iatu_unroll_enabled)
393
+ return rk_pcie_prog_inbound_atu_unroll(pci, func_no, index, bar,
394
+ cpu_addr, as_type);
395
+
396
+ dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_INBOUND |
397
+ index);
398
+ dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET, lower_32_bits(cpu_addr));
399
+ dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET, upper_32_bits(cpu_addr));
400
+
401
+ switch (as_type) {
402
+ case DW_PCIE_AS_MEM:
403
+ type = PCIE_ATU_TYPE_MEM;
404
+ break;
405
+ case DW_PCIE_AS_IO:
406
+ type = PCIE_ATU_TYPE_IO;
407
+ break;
408
+ default:
409
+ return -EINVAL;
410
+ }
411
+
412
+ dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, type |
413
+ PCIE_ATU_FUNC_NUM(func_no));
414
+ dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE |
415
+ PCIE_ATU_FUNC_NUM_MATCH_EN |
416
+ PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
417
+
418
+ /*
419
+ * Make sure ATU enable takes effect before any subsequent config
420
+ * and I/O accesses.
421
+ */
422
+ for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
423
+ val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2);
424
+ if (val & PCIE_ATU_ENABLE)
425
+ return 0;
426
+
427
+ mdelay(LINK_WAIT_IATU);
428
+ }
429
+ dev_err(pci->dev, "Inbound iATU is not being enabled\n");
430
+
431
+ return -EBUSY;
432
+}
433
+
256434 static int rk_pcie_ep_inbound_atu(struct rk_pcie *rk_pcie,
257435 enum pci_barno bar, dma_addr_t cpu_addr,
258436 enum dw_pcie_as_type as_type)
259437 {
260438 int ret;
261439 u32 free_win;
440
+ u8 func_no = 0x0;
262441
263442 if (rk_pcie->in_suspend) {
264443 free_win = rk_pcie->bar_to_atu[bar];
....@@ -271,8 +450,8 @@
271450 }
272451 }
273452
274
- ret = dw_pcie_prog_inbound_atu(rk_pcie->pci, free_win, bar, cpu_addr,
275
- as_type);
453
+ ret = rk_pcie_prog_inbound_atu(rk_pcie->pci, func_no, free_win, bar,
454
+ cpu_addr, as_type);
276455 if (ret < 0) {
277456 dev_err(rk_pcie->pci->dev, "Failed to program IB window\n");
278457 return ret;
....@@ -285,6 +464,105 @@
285464 set_bit(free_win, rk_pcie->ib_window_map);
286465
287466 return 0;
467
+}
468
+
469
+static void rk_pcie_writel_ob_unroll(struct dw_pcie *pci, u32 index, u32 reg,
470
+ u32 val)
471
+{
472
+ u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
473
+
474
+ rk_pcie_writel_atu(pci, offset + reg, val);
475
+}
476
+
477
+static u32 rk_pcie_readl_ob_unroll(struct dw_pcie *pci, u32 index, u32 reg)
478
+{
479
+ u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
480
+
481
+ return rk_pcie_readl_atu(pci, offset + reg);
482
+}
483
+
484
+static void rk_pcie_prog_outbound_atu_unroll(struct dw_pcie *pci, u8 func_no,
485
+ int index, int type,
486
+ u64 cpu_addr, u64 pci_addr,
487
+ u32 size)
488
+{
489
+ u32 retries, val;
490
+ u64 limit_addr = cpu_addr + size - 1;
491
+
492
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
493
+ lower_32_bits(cpu_addr));
494
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
495
+ upper_32_bits(cpu_addr));
496
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_LIMIT,
497
+ lower_32_bits(limit_addr));
498
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_LIMIT,
499
+ upper_32_bits(limit_addr));
500
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
501
+ lower_32_bits(pci_addr));
502
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
503
+ upper_32_bits(pci_addr));
504
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
505
+ type | PCIE_ATU_FUNC_NUM(func_no));
506
+ rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
507
+ PCIE_ATU_ENABLE);
508
+
509
+ /*
510
+ * Make sure ATU enable takes effect before any subsequent config
511
+ * and I/O accesses.
512
+ */
513
+ for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
514
+ val = rk_pcie_readl_ob_unroll(pci, index,
515
+ PCIE_ATU_UNR_REGION_CTRL2);
516
+ if (val & PCIE_ATU_ENABLE)
517
+ return;
518
+
519
+ mdelay(LINK_WAIT_IATU);
520
+ }
521
+ dev_err(pci->dev, "Outbound iATU is not being enabled\n");
522
+}
523
+
524
+static void rk_pcie_prog_outbound_atu(struct dw_pcie *pci, int index,
525
+ int type, u64 cpu_addr, u64 pci_addr, u32 size)
526
+{
527
+ u32 retries, val;
528
+
529
+ if (pci->ops->cpu_addr_fixup)
530
+ cpu_addr = pci->ops->cpu_addr_fixup(pci, cpu_addr);
531
+
532
+ if (pci->iatu_unroll_enabled) {
533
+ rk_pcie_prog_outbound_atu_unroll(pci, 0x0, index, type,
534
+ cpu_addr, pci_addr, size);
535
+ return;
536
+ }
537
+
538
+ dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT,
539
+ PCIE_ATU_REGION_OUTBOUND | index);
540
+ dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_BASE,
541
+ lower_32_bits(cpu_addr));
542
+ dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_BASE,
543
+ upper_32_bits(cpu_addr));
544
+ dw_pcie_writel_dbi(pci, PCIE_ATU_LIMIT,
545
+ lower_32_bits(cpu_addr + size - 1));
546
+ dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET,
547
+ lower_32_bits(pci_addr));
548
+ dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET,
549
+ upper_32_bits(pci_addr));
550
+ dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, type |
551
+ PCIE_ATU_FUNC_NUM(0x0));
552
+ dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE);
553
+
554
+ /*
555
+ * Make sure ATU enable takes effect before any subsequent config
556
+ * and I/O accesses.
557
+ */
558
+ for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
559
+ val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2);
560
+ if (val & PCIE_ATU_ENABLE)
561
+ return;
562
+
563
+ mdelay(LINK_WAIT_IATU);
564
+ }
565
+ dev_err(pci->dev, "Outbound iATU is not being enabled\n");
288566 }
289567
290568 static int rk_pcie_ep_outbound_atu(struct rk_pcie *rk_pcie,
....@@ -305,7 +583,7 @@
305583 }
306584 }
307585
308
- dw_pcie_prog_outbound_atu(rk_pcie->pci, free_win, PCIE_ATU_TYPE_MEM,
586
+ rk_pcie_prog_outbound_atu(rk_pcie->pci, free_win, PCIE_ATU_TYPE_MEM,
309587 phys_addr, pci_addr, size);
310588
311589 if (rk_pcie->in_suspend)
....@@ -362,6 +640,28 @@
362640 return 0;
363641 }
364642
643
+#if defined(CONFIG_PCIEASPM)
644
+static void disable_aspm_l1ss(struct rk_pcie *rk_pcie)
645
+{
646
+ u32 val, cfg_link_cap_l1sub;
647
+
648
+ val = dw_pcie_find_ext_capability(rk_pcie->pci, PCI_EXT_CAP_ID_L1SS);
649
+ if (!val) {
650
+ dev_err(rk_pcie->pci->dev, "can't find l1ss cap\n");
651
+
652
+ return;
653
+ }
654
+
655
+ cfg_link_cap_l1sub = val + PCI_L1SS_CAP;
656
+
657
+ val = dw_pcie_readl_dbi(rk_pcie->pci, cfg_link_cap_l1sub);
658
+ val &= ~(PCI_L1SS_CAP_ASPM_L1_1 | PCI_L1SS_CAP_ASPM_L1_2 | PCI_L1SS_CAP_L1_PM_SS);
659
+ dw_pcie_writel_dbi(rk_pcie->pci, cfg_link_cap_l1sub, val);
660
+}
661
+#else
662
+static inline void disable_aspm_l1ss(struct rk_pcie *rk_pcie) { return; }
663
+#endif
664
+
365665 static inline void rk_pcie_set_mode(struct rk_pcie *rk_pcie)
366666 {
367667 switch (rk_pcie->mode) {
....@@ -369,6 +669,14 @@
369669 rk_pcie_writel_apb(rk_pcie, 0x0, 0xf00000);
370670 break;
371671 case RK_PCIE_RC_TYPE:
672
+ if (rk_pcie->supports_clkreq) {
673
+ /* Application is ready to have reference clock removed */
674
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_POWER, 0x00010001);
675
+ } else {
676
+ /* Pull down CLKREQ# to assert the connecting CLOCK_GEN OE */
677
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_POWER, 0x30011000);
678
+ disable_aspm_l1ss(rk_pcie);
679
+ }
372680 rk_pcie_writel_apb(rk_pcie, 0x0, 0xf00040);
373681 /*
374682 * Disable order rule for CPL can't pass halted P queue.
....@@ -398,29 +706,10 @@
398706 rk_pcie_writel_apb(rk_pcie, 0x0, 0xC000C);
399707 }
400708
401
-static int rk_pcie_link_up(struct dw_pcie *pci)
402
-{
403
- struct rk_pcie *rk_pcie = to_rk_pcie(pci);
404
- u32 val;
405
-
406
- if (rk_pcie->is_rk1808) {
407
- val = rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG);
408
- if ((val & (PCIE_PHY_LINKUP | PCIE_DATA_LINKUP)) == 0x3 &&
409
- ((val & GENMASK(15, 10)) >> 10) == 0x11)
410
- return 1;
411
- } else {
412
- val = rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS);
413
- if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 &&
414
- (val & GENMASK(5, 0)) == 0x11)
415
- return 1;
416
- }
417
-
418
- return 0;
419
-}
420
-
421709 static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie)
422710 {
423
-#if RK_PCIE_DBG
711
+ if (!IS_ENABLED(CONFIG_DEBUG_FS))
712
+ return;
424713 if (rk_pcie->is_rk1808 == true)
425714 return;
426715
....@@ -434,7 +723,6 @@
434723 PCIE_CLIENT_DBG_TRANSITION_DATA);
435724 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON,
436725 PCIE_CLIENT_DBF_EN);
437
-#endif
438726 }
439727
440728 static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie)
....@@ -453,75 +741,137 @@
453741
454742 static int rk_pcie_establish_link(struct dw_pcie *pci)
455743 {
456
- int retries;
744
+ int retries, power;
457745 struct rk_pcie *rk_pcie = to_rk_pcie(pci);
746
+ bool std_rc = rk_pcie->mode == RK_PCIE_RC_TYPE && !rk_pcie->dma_obj;
747
+ int hw_retries = 0;
748
+ u32 ltssm;
458749
459
- if (dw_pcie_link_up(pci)) {
750
+ /*
751
+ * For standard RC, even if the link has been setup by firmware,
752
+ * we still need to reset link as we need to remove all resource info
753
+ * from devices, for instance BAR, as it wasn't assigned by kernel.
754
+ */
755
+ if (dw_pcie_link_up(pci) && !std_rc) {
460756 dev_err(pci->dev, "link is already up\n");
461757 return 0;
462758 }
463759
464
- rk_pcie_disable_ltssm(rk_pcie);
465
- rk_pcie_link_status_clear(rk_pcie);
466
- rk_pcie_enable_debug(rk_pcie);
760
+ for (hw_retries = 0; hw_retries < RK_PCIE_ENUM_HW_RETRYIES; hw_retries++) {
761
+ /* Rest the device */
762
+ gpiod_set_value_cansleep(rk_pcie->rst_gpio, 0);
467763
468
- /* Enable client reset or link down interrupt */
469
- rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_MASK, 0x40000);
764
+ rk_pcie_disable_ltssm(rk_pcie);
765
+ rk_pcie_link_status_clear(rk_pcie);
766
+ rk_pcie_enable_debug(rk_pcie);
470767
471
- /* Enable LTSSM */
472
- rk_pcie_enable_ltssm(rk_pcie);
768
+ /* Enable client reset or link down interrupt */
769
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_MASK, 0x40000);
473770
474
- /*
475
- * PCIe requires the refclk to be stable for 100µs prior to releasing
476
- * PERST and T_PVPERL (Power stable to PERST# inactive) should be a
477
- * minimum of 100ms. See table 2-4 in section 2.6.2 AC, the PCI Express
478
- * Card Electromechanical Specification 3.0. So 100ms in total is the min
479
- * requuirement here. We add a 1s for sake of hoping everthings work fine.
480
- */
481
- msleep(1000);
482
- gpiod_set_value_cansleep(rk_pcie->rst_gpio, 1);
771
+ /* Enable LTSSM */
772
+ rk_pcie_enable_ltssm(rk_pcie);
483773
484
- for (retries = 0; retries < 10; retries++) {
485
- if (dw_pcie_link_up(pci)) {
486
- /*
487
- * We may be here in case of L0 in Gen1. But if EP is capable
488
- * of Gen2 or Gen3, Gen switch may happen just in this time, but
489
- * we keep on accessing devices in unstable link status. Given
490
- * that LTSSM max timeout is 24ms per period, we can wait a bit
491
- * more for Gen switch.
492
- */
493
- msleep(100);
494
- dev_info(pci->dev, "PCIe Link up, LTSSM is 0x%x\n",
495
- rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
496
- rk_pcie_debug_dump(rk_pcie);
497
- return 0;
774
+ /*
775
+ * In resume routine, function devices' resume function must be late after
776
+ * controllers'. Some devices, such as Wi-Fi, need special IO setting before
777
+ * finishing training. So there must be timeout here. These kinds of devices
778
+ * need rescan devices by its driver when used. So no need to waste time waiting
779
+ * for training pass.
780
+ */
781
+ if (rk_pcie->in_suspend && rk_pcie->skip_scan_in_resume) {
782
+ rfkill_get_wifi_power_state(&power);
783
+ if (!power) {
784
+ gpiod_set_value_cansleep(rk_pcie->rst_gpio, 1);
785
+ return 0;
786
+ }
498787 }
499788
500
- dev_info_ratelimited(pci->dev, "PCIe Linking... LTSSM is 0x%x\n",
501
- rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
502
- rk_pcie_debug_dump(rk_pcie);
503
- msleep(1000);
504
- }
789
+ /*
790
+ * PCIe requires the refclk to be stable for 100µs prior to releasing
791
+ * PERST and T_PVPERL (Power stable to PERST# inactive) should be a
792
+ * minimum of 100ms. See table 2-4 in section 2.6.2 AC, the PCI Express
793
+ * Card Electromechanical Specification 3.0. So 100ms in total is the min
794
+ * requuirement here. We add a 200ms by default for sake of hoping everthings
795
+ * work fine. If it doesn't, please add more in DT node by add rockchip,perst-inactive-ms.
796
+ */
797
+ msleep(rk_pcie->perst_inactive_ms);
798
+ gpiod_set_value_cansleep(rk_pcie->rst_gpio, 1);
505799
506
- dev_err(pci->dev, "PCIe Link Fail\n");
800
+ /*
801
+ * Add this 1ms delay because we observe link is always up stably after it and
802
+ * could help us save 20ms for scanning devices.
803
+ */
804
+ usleep_range(1000, 1100);
805
+
806
+ for (retries = 0; retries < 100; retries++) {
807
+ if (dw_pcie_link_up(pci)) {
808
+ /*
809
+ * We may be here in case of L0 in Gen1. But if EP is capable
810
+ * of Gen2 or Gen3, Gen switch may happen just in this time, but
811
+ * we keep on accessing devices in unstable link status. Given
812
+ * that LTSSM max timeout is 24ms per period, we can wait a bit
813
+ * more for Gen switch.
814
+ */
815
+ msleep(50);
816
+ /* In case link drop after linkup, double check it */
817
+ if (dw_pcie_link_up(pci)) {
818
+ dev_info(pci->dev, "PCIe Link up, LTSSM is 0x%x\n",
819
+ rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
820
+ rk_pcie_debug_dump(rk_pcie);
821
+ return 0;
822
+ }
823
+ }
824
+
825
+ dev_info_ratelimited(pci->dev, "PCIe Linking... LTSSM is 0x%x\n",
826
+ rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
827
+ rk_pcie_debug_dump(rk_pcie);
828
+ msleep(20);
829
+ }
830
+
831
+ /*
832
+ * In response to the situation where PCIe peripherals cannot be
833
+ * enumerated due tosignal abnormalities, reset PERST# and reset
834
+ * the peripheral power supply, then restart the enumeration.
835
+ */
836
+ ltssm = rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS);
837
+ dev_err(pci->dev, "PCIe Link Fail, LTSSM is 0x%x, hw_retries=%d\n", ltssm, hw_retries);
838
+ if (ltssm >= 3 && !rk_pcie->is_signal_test) {
839
+ rk_pcie_disable_power(rk_pcie);
840
+ msleep(1000);
841
+ rk_pcie_enable_power(rk_pcie);
842
+ } else {
843
+ break;
844
+ }
845
+ }
507846
508847 return rk_pcie->is_signal_test == true ? 0 : -EINVAL;
509848 }
510849
850
+static bool rk_pcie_udma_enabled(struct rk_pcie *rk_pcie)
851
+{
852
+ return dw_pcie_readl_dbi(rk_pcie->pci, PCIE_DMA_OFFSET +
853
+ PCIE_DMA_CTRL_OFF);
854
+}
855
+
511856 static int rk_pcie_init_dma_trx(struct rk_pcie *rk_pcie)
512857 {
858
+ if (!rk_pcie_udma_enabled(rk_pcie))
859
+ return 0;
860
+
513861 rk_pcie->dma_obj = rk_pcie_dma_obj_probe(rk_pcie->pci->dev);
514862 if (IS_ERR(rk_pcie->dma_obj)) {
515863 dev_err(rk_pcie->pci->dev, "failed to prepare dma object\n");
516864 return -EINVAL;
865
+ } else if (rk_pcie->dma_obj) {
866
+ goto out;
517867 }
518868
519
- rk_pcie->dma_obj = pcie_dw_dmatest_register(rk_pcie->pci, true);
869
+ rk_pcie->dma_obj = pcie_dw_dmatest_register(rk_pcie->pci->dev, true);
520870 if (IS_ERR(rk_pcie->dma_obj)) {
521871 dev_err(rk_pcie->pci->dev, "failed to prepare dmatest\n");
522872 return -EINVAL;
523873 }
524
-
874
+out:
525875 /* Enable client write and read interrupt */
526876 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_MASK, 0xc000000);
527877
....@@ -534,6 +884,76 @@
534884 return 0;
535885 }
536886
887
+static int rk_pci_find_resbar_capability(struct rk_pcie *rk_pcie)
888
+{
889
+ u32 header;
890
+ int ttl;
891
+ int start = 0;
892
+ int pos = PCI_CFG_SPACE_SIZE;
893
+ int cap = PCI_EXT_CAP_ID_REBAR;
894
+
895
+ /* minimum 8 bytes per capability */
896
+ ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
897
+
898
+ header = dw_pcie_readl_dbi(rk_pcie->pci, pos);
899
+
900
+ /*
901
+ * If we have no capabilities, this is indicated by cap ID,
902
+ * cap version and next pointer all being 0.
903
+ */
904
+ if (header == 0)
905
+ return 0;
906
+
907
+ while (ttl-- > 0) {
908
+ if (PCI_EXT_CAP_ID(header) == cap && pos != start)
909
+ return pos;
910
+
911
+ pos = PCI_EXT_CAP_NEXT(header);
912
+ if (pos < PCI_CFG_SPACE_SIZE)
913
+ break;
914
+
915
+ header = dw_pcie_readl_dbi(rk_pcie->pci, pos);
916
+ if (!header)
917
+ break;
918
+ }
919
+
920
+ return 0;
921
+}
922
+
923
+#ifdef MODULE
924
+void dw_pcie_write_dbi2(struct dw_pcie *pci, u32 reg, size_t size, u32 val)
925
+{
926
+ int ret;
927
+
928
+ if (pci->ops && pci->ops->write_dbi2) {
929
+ pci->ops->write_dbi2(pci, pci->dbi_base2, reg, size, val);
930
+ return;
931
+ }
932
+
933
+ ret = dw_pcie_write(pci->dbi_base2 + reg, size, val);
934
+ if (ret)
935
+ dev_err(pci->dev, "write DBI address failed\n");
936
+}
937
+#endif
938
+
939
+static int rk_pcie_ep_set_bar_flag(struct rk_pcie *rk_pcie, enum pci_barno barno, int flags)
940
+{
941
+ enum pci_barno bar = barno;
942
+ u32 reg;
943
+
944
+ reg = PCI_BASE_ADDRESS_0 + (4 * bar);
945
+
946
+ /* Disabled the upper 32bits BAR to make a 64bits bar pair */
947
+ if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
948
+ dw_pcie_writel_dbi2(rk_pcie->pci, reg + 4, 0);
949
+
950
+ dw_pcie_writel_dbi(rk_pcie->pci, reg, flags);
951
+ if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
952
+ dw_pcie_writel_dbi(rk_pcie->pci, reg + 4, 0);
953
+
954
+ return 0;
955
+}
956
+
537957 static void rk_pcie_ep_setup(struct rk_pcie *rk_pcie)
538958 {
539959 int ret;
....@@ -541,6 +961,8 @@
541961 u32 lanes;
542962 struct device *dev = rk_pcie->pci->dev;
543963 struct device_node *np = dev->of_node;
964
+ int resbar_base;
965
+ int bar;
544966
545967 /* Enable client write and read interrupt */
546968 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_MASK, 0xc000000);
....@@ -604,17 +1026,36 @@
6041026 /* Enable bus master and memory space */
6051027 dw_pcie_writel_dbi(rk_pcie->pci, PCIE_TYPE0_STATUS_COMMAND_REG, 0x6);
6061028
607
- /* Resize BAR0 to 4GB */
608
- /* bit13-8 set to 6 means 64MB */
609
- dw_pcie_writel_dbi(rk_pcie->pci, PCIE_RESBAR_CTRL_REG0_REG, 0x600);
1029
+ resbar_base = rk_pci_find_resbar_capability(rk_pcie);
1030
+ if (!resbar_base) {
1031
+ dev_warn(dev, "failed to find resbar_base\n");
1032
+ } else {
1033
+ /* Resize BAR0 to support 512GB, BAR1 to support 8M, BAR2~5 to support 64M */
1034
+ dw_pcie_writel_dbi(rk_pcie->pci, resbar_base + 0x4, 0xfffff0);
1035
+ dw_pcie_writel_dbi(rk_pcie->pci, resbar_base + 0x8, 0x13c0);
1036
+ dw_pcie_writel_dbi(rk_pcie->pci, resbar_base + 0xc, 0xfffff0);
1037
+ dw_pcie_writel_dbi(rk_pcie->pci, resbar_base + 0x10, 0x3c0);
1038
+ for (bar = 2; bar < 6; bar++) {
1039
+ dw_pcie_writel_dbi(rk_pcie->pci, resbar_base + 0x4 + bar * 0x8, 0xfffff0);
1040
+ dw_pcie_writel_dbi(rk_pcie->pci, resbar_base + 0x8 + bar * 0x8, 0x6c0);
1041
+ }
6101042
611
- /* Set shadow BAR0 according 64MB */
612
- val = rk_pcie->mem_size - 1;
613
- dw_pcie_writel_dbi(rk_pcie->pci, PCIE_SB_BAR0_MASK_REG, val);
1043
+ /* Set flags */
1044
+ rk_pcie_ep_set_bar_flag(rk_pcie, BAR_0, PCI_BASE_ADDRESS_MEM_TYPE_32);
1045
+ rk_pcie_ep_set_bar_flag(rk_pcie, BAR_1, PCI_BASE_ADDRESS_MEM_TYPE_32);
1046
+ rk_pcie_ep_set_bar_flag(rk_pcie, BAR_2, PCI_BASE_ADDRESS_MEM_PREFETCH | PCI_BASE_ADDRESS_MEM_TYPE_64);
1047
+ rk_pcie_ep_set_bar_flag(rk_pcie, BAR_4, PCI_BASE_ADDRESS_MEM_PREFETCH | PCI_BASE_ADDRESS_MEM_TYPE_64);
1048
+ }
6141049
615
- /* Set reserved memory address to BAR0 */
616
- dw_pcie_writel_dbi(rk_pcie->pci, PCIE_TYPE0_BAR0_REG,
617
- rk_pcie->mem_start);
1050
+ /* Device id and class id needed for request bar address */
1051
+ dw_pcie_writew_dbi(rk_pcie->pci, PCI_DEVICE_ID, 0x356a);
1052
+ dw_pcie_writew_dbi(rk_pcie->pci, PCI_CLASS_DEVICE, 0x0580);
1053
+
1054
+ /* Set shadow BAR0 */
1055
+ if (rk_pcie->is_rk1808) {
1056
+ val = rk_pcie->mem_size - 1;
1057
+ dw_pcie_writel_dbi(rk_pcie->pci, PCIE_SB_BAR0_MASK_REG, val);
1058
+ }
6181059 }
6191060
6201061 static int rk_pcie_ep_win_parse(struct rk_pcie *rk_pcie)
....@@ -675,12 +1116,24 @@
6751116 return 0;
6761117 }
6771118
1119
+static void rk_pcie_msi_set_num_vectors(struct pcie_port *pp)
1120
+{
1121
+ struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1122
+ struct rk_pcie *rk_pcie = to_rk_pcie(pci);
1123
+
1124
+ pp->num_vectors = rk_pcie->msi_vector_num;
1125
+}
1126
+
6781127 static int rk_pcie_host_init(struct pcie_port *pp)
6791128 {
6801129 int ret;
6811130 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
6821131
6831132 dw_pcie_setup_rc(pp);
1133
+
1134
+ /* Disable BAR0 BAR1 */
1135
+ dw_pcie_writel_dbi2(pci, PCI_BASE_ADDRESS_0, 0x0);
1136
+ dw_pcie_writel_dbi2(pci, PCI_BASE_ADDRESS_1, 0x0);
6841137
6851138 ret = rk_pcie_establish_link(pci);
6861139
....@@ -707,13 +1160,13 @@
7071160 if (pp->msi_irq < 0) {
7081161 dev_info(dev, "use outband MSI support");
7091162 rk_pcie_host_ops.msi_host_init = rk_pcie_msi_host_init;
1163
+ } else {
1164
+ dev_info(dev, "max MSI vector is %d\n", rk_pcie->msi_vector_num);
1165
+ rk_pcie_host_ops.set_num_vectors = rk_pcie_msi_set_num_vectors;
7101166 }
7111167 }
7121168
7131169 pp->ops = &rk_pcie_host_ops;
714
-
715
- if (device_property_read_bool(dev, "msi-map"))
716
- pp->msi_ext = 1;
7171170
7181171 ret = dw_pcie_host_init(pp);
7191172 if (ret) {
....@@ -753,6 +1206,7 @@
7531206 return ret;
7541207 }
7551208
1209
+ rk_pcie->pci->atu_base = rk_pcie->pci->dbi_base + DEFAULT_DBI_ATU_OFFSET;
7561210 rk_pcie->pci->iatu_unroll_enabled = rk_pcie_iatu_unroll_enabled(rk_pcie->pci);
7571211
7581212 ret = rk_pcie_ep_atu_init(rk_pcie);
....@@ -769,52 +1223,24 @@
7691223 return ret;
7701224 }
7711225
772
- return 0;
773
-}
1226
+ if (!rk_pcie_udma_enabled(rk_pcie))
1227
+ return 0;
7741228
775
-static void rk_pcie_clk_deinit(struct rk_pcie *rk_pcie)
776
-{
777
- clk_bulk_disable(rk_pcie->clk_cnt, rk_pcie->clks);
778
- clk_bulk_unprepare(rk_pcie->clk_cnt, rk_pcie->clks);
1229
+ return 0;
7791230 }
7801231
7811232 static int rk_pcie_clk_init(struct rk_pcie *rk_pcie)
7821233 {
7831234 struct device *dev = rk_pcie->pci->dev;
784
- struct property *prop;
785
- const char *name;
786
- int i = 0, ret, count;
1235
+ int ret;
7871236
788
- count = of_property_count_strings(dev->of_node, "clock-names");
789
- if (count < 1)
1237
+ rk_pcie->clk_cnt = devm_clk_bulk_get_all(dev, &rk_pcie->clks);
1238
+ if (rk_pcie->clk_cnt < 1)
7901239 return -ENODEV;
7911240
792
- rk_pcie->clks = devm_kcalloc(dev, count,
793
- sizeof(struct clk_bulk_data),
794
- GFP_KERNEL);
795
- if (!rk_pcie->clks)
796
- return -ENOMEM;
797
-
798
- rk_pcie->clk_cnt = count;
799
-
800
- of_property_for_each_string(dev->of_node, "clock-names", prop, name) {
801
- rk_pcie->clks[i].id = name;
802
- if (!rk_pcie->clks[i].id)
803
- return -ENOMEM;
804
- i++;
805
- }
806
-
807
- ret = devm_clk_bulk_get(dev, count, rk_pcie->clks);
808
- if (ret)
809
- return ret;
810
-
811
- ret = clk_bulk_prepare(count, rk_pcie->clks);
812
- if (ret)
813
- return ret;
814
-
815
- ret = clk_bulk_enable(count, rk_pcie->clks);
1241
+ ret = clk_bulk_prepare_enable(rk_pcie->clk_cnt, rk_pcie->clks);
8161242 if (ret) {
817
- clk_bulk_unprepare(count, rk_pcie->clks);
1243
+ dev_err(dev, "failed to prepare enable pcie bulk clks: %d\n", ret);
8181244 return ret;
8191245 }
8201246
....@@ -839,6 +1265,7 @@
8391265 return PTR_ERR(rk_pcie->dbi_base);
8401266
8411267 rk_pcie->pci->dbi_base = rk_pcie->dbi_base;
1268
+ rk_pcie->pci->dbi_base2 = rk_pcie->pci->dbi_base + PCIE_TYPE0_HDR_DBI2_OFFSET;
8421269
8431270 apb_base = platform_get_resource_byname(pdev, IORESOURCE_MEM,
8441271 "pcie-apb");
....@@ -865,6 +1292,14 @@
8651292 return PTR_ERR(rk_pcie->rst_gpio);
8661293 }
8671294
1295
+ if (device_property_read_u32(&pdev->dev, "rockchip,perst-inactive-ms",
1296
+ &rk_pcie->perst_inactive_ms))
1297
+ rk_pcie->perst_inactive_ms = 200;
1298
+
1299
+ rk_pcie->prsnt_gpio = devm_gpiod_get_optional(&pdev->dev, "prsnt", GPIOD_IN);
1300
+ if (IS_ERR_OR_NULL(rk_pcie->prsnt_gpio))
1301
+ dev_info(&pdev->dev, "invalid prsnt-gpios property in node\n");
1302
+
8681303 return 0;
8691304 }
8701305
....@@ -873,7 +1308,7 @@
8731308 int ret;
8741309 struct device *dev = rk_pcie->pci->dev;
8751310
876
- rk_pcie->phy = devm_phy_get(dev, "pcie-phy");
1311
+ rk_pcie->phy = devm_phy_optional_get(dev, "pcie-phy");
8771312 if (IS_ERR(rk_pcie->phy)) {
8781313 if (PTR_ERR(rk_pcie->phy) != -EPROBE_DEFER)
8791314 dev_info(dev, "missing phy\n");
....@@ -882,23 +1317,27 @@
8821317
8831318 switch (rk_pcie->mode) {
8841319 case RK_PCIE_RC_TYPE:
885
- rk_pcie->phy_mode = PHY_MODE_PCIE_RC;
1320
+ rk_pcie->phy_mode = PHY_MODE_PCIE; /* make no sense */
1321
+ rk_pcie->phy_sub_mode = PHY_MODE_PCIE_RC;
8861322 break;
8871323 case RK_PCIE_EP_TYPE:
888
- rk_pcie->phy_mode = PHY_MODE_PCIE_EP;
1324
+ rk_pcie->phy_mode = PHY_MODE_PCIE;
1325
+ rk_pcie->phy_sub_mode = PHY_MODE_PCIE_EP;
8891326 break;
8901327 }
8911328
892
- ret = phy_set_mode(rk_pcie->phy, rk_pcie->phy_mode);
1329
+ ret = phy_set_mode_ext(rk_pcie->phy, rk_pcie->phy_mode,
1330
+ rk_pcie->phy_sub_mode);
8931331 if (ret) {
8941332 dev_err(dev, "fail to set phy to mode %s, err %d\n",
895
- (rk_pcie->phy_mode == PHY_MODE_PCIE_RC) ? "RC" : "EP",
1333
+ (rk_pcie->phy_sub_mode == PHY_MODE_PCIE_RC) ? "RC" : "EP",
8961334 ret);
8971335 return ret;
8981336 }
8991337
9001338 if (rk_pcie->bifurcation)
901
- ret = phy_set_mode(rk_pcie->phy, PHY_MODE_PCIE_BIFURCATION);
1339
+ phy_set_mode_ext(rk_pcie->phy, rk_pcie->phy_mode,
1340
+ PHY_MODE_PCIE_BIFURCATION);
9021341
9031342 ret = phy_init(rk_pcie->phy);
9041343 if (ret < 0) {
....@@ -907,53 +1346,6 @@
9071346 }
9081347
9091348 phy_power_on(rk_pcie->phy);
910
-
911
- return 0;
912
-}
913
-
914
-static int rk_pcie_reset_control_release(struct rk_pcie *rk_pcie)
915
-{
916
- struct device *dev = rk_pcie->pci->dev;
917
- struct property *prop;
918
- const char *name;
919
- int ret, count, i = 0;
920
-
921
- count = of_property_count_strings(dev->of_node, "reset-names");
922
- if (count < 1)
923
- return -ENODEV;
924
-
925
- rk_pcie->rsts = devm_kcalloc(dev, count,
926
- sizeof(struct reset_bulk_data),
927
- GFP_KERNEL);
928
- if (!rk_pcie->rsts)
929
- return -ENOMEM;
930
-
931
- of_property_for_each_string(dev->of_node, "reset-names",
932
- prop, name) {
933
- rk_pcie->rsts[i].id = name;
934
- if (!rk_pcie->rsts[i].id)
935
- return -ENOMEM;
936
- i++;
937
- }
938
-
939
- for (i = 0; i < count; i++) {
940
- rk_pcie->rsts[i].rst = devm_reset_control_get_exclusive(dev,
941
- rk_pcie->rsts[i].id);
942
- if (IS_ERR_OR_NULL(rk_pcie->rsts[i].rst)) {
943
- dev_err(dev, "failed to get %s\n",
944
- rk_pcie->clks[i].id);
945
- return -PTR_ERR(rk_pcie->rsts[i].rst);
946
- }
947
- }
948
-
949
- for (i = 0; i < count; i++) {
950
- ret = reset_control_deassert(rk_pcie->rsts[i].rst);
951
- if (ret) {
952
- dev_err(dev, "failed to release %s\n",
953
- rk_pcie->rsts[i].id);
954
- return ret;
955
- }
956
- }
9571349
9581350 return 0;
9591351 }
....@@ -1058,13 +1450,37 @@
10581450 table->start.chnl = table->chn;
10591451 }
10601452
1453
+static void rk_pcie_hot_rst_work(struct work_struct *work)
1454
+{
1455
+ struct rk_pcie *rk_pcie = container_of(work, struct rk_pcie, hot_rst_work);
1456
+ u32 val, status;
1457
+ int ret;
1458
+
1459
+ /* Setup command register */
1460
+ val = dw_pcie_readl_dbi(rk_pcie->pci, PCI_COMMAND);
1461
+ val &= 0xffff0000;
1462
+ val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
1463
+ PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
1464
+ dw_pcie_writel_dbi(rk_pcie->pci, PCI_COMMAND, val);
1465
+
1466
+ if (rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_HOT_RESET_CTRL) & PCIE_LTSSM_APP_DLY2_EN) {
1467
+ ret = readl_poll_timeout(rk_pcie->apb_base + PCIE_CLIENT_LTSSM_STATUS,
1468
+ status, ((status & 0x3F) == 0), 100, RK_PCIE_HOTRESET_TMOUT_US);
1469
+ if (ret)
1470
+ dev_err(rk_pcie->pci->dev, "wait for detect quiet failed!\n");
1471
+
1472
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_HOT_RESET_CTRL,
1473
+ (PCIE_LTSSM_APP_DLY2_DONE) | ((PCIE_LTSSM_APP_DLY2_DONE) << 16));
1474
+ }
1475
+}
1476
+
10611477 static irqreturn_t rk_pcie_sys_irq_handler(int irq, void *arg)
10621478 {
10631479 struct rk_pcie *rk_pcie = arg;
10641480 u32 chn;
10651481 union int_status status;
10661482 union int_clear clears;
1067
- u32 reg, val;
1483
+ u32 reg;
10681484
10691485 status.asdword = dw_pcie_readl_dbi(rk_pcie->pci, PCIE_DMA_OFFSET +
10701486 PCIE_DMA_WR_INT_STATUS);
....@@ -1105,14 +1521,8 @@
11051521 }
11061522
11071523 reg = rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_INTR_STATUS_MISC);
1108
- if (reg & BIT(2)) {
1109
- /* Setup command register */
1110
- val = dw_pcie_readl_dbi(rk_pcie->pci, PCI_COMMAND);
1111
- val &= 0xffff0000;
1112
- val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
1113
- PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
1114
- dw_pcie_writel_dbi(rk_pcie->pci, PCI_COMMAND, val);
1115
- }
1524
+ if (reg & BIT(2))
1525
+ queue_work(rk_pcie->hot_rst_wq, &rk_pcie->hot_rst_work);
11161526
11171527 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_STATUS_MISC, reg);
11181528
....@@ -1149,6 +1559,11 @@
11491559 .mode = RK_PCIE_EP_TYPE,
11501560 };
11511561
1562
+static const struct rk_pcie_of_data rk3528_pcie_rc_of_data = {
1563
+ .mode = RK_PCIE_RC_TYPE,
1564
+ .msi_vector_num = 8,
1565
+};
1566
+
11521567 static const struct of_device_id rk_pcie_of_match[] = {
11531568 {
11541569 .compatible = "rockchip,rk1808-pcie",
....@@ -1159,11 +1574,27 @@
11591574 .data = &rk_pcie_ep_of_data,
11601575 },
11611576 {
1577
+ .compatible = "rockchip,rk3528-pcie",
1578
+ .data = &rk3528_pcie_rc_of_data,
1579
+ },
1580
+ {
1581
+ .compatible = "rockchip,rk3562-pcie",
1582
+ .data = &rk3528_pcie_rc_of_data,
1583
+ },
1584
+ {
11621585 .compatible = "rockchip,rk3568-pcie",
11631586 .data = &rk_pcie_rc_of_data,
11641587 },
11651588 {
11661589 .compatible = "rockchip,rk3568-pcie-ep",
1590
+ .data = &rk_pcie_ep_of_data,
1591
+ },
1592
+ {
1593
+ .compatible = "rockchip,rk3588-pcie",
1594
+ .data = &rk_pcie_rc_of_data,
1595
+ },
1596
+ {
1597
+ .compatible = "rockchip,rk3588-pcie-ep",
11671598 .data = &rk_pcie_ep_of_data,
11681599 },
11691600 {},
....@@ -1173,7 +1604,6 @@
11731604
11741605 static const struct dw_pcie_ops dw_pcie_ops = {
11751606 .start_link = rk_pcie_establish_link,
1176
- .link_up = rk_pcie_link_up,
11771607 };
11781608
11791609 static int rk1808_pcie_fixup(struct rk_pcie *rk_pcie, struct device_node *np)
....@@ -1215,7 +1645,8 @@
12151645
12161646 /* LTSSM EN ctrl mode */
12171647 val = rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_HOT_RESET_CTRL);
1218
- val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16);
1648
+ val |= (PCIE_LTSSM_ENABLE_ENHANCE | PCIE_LTSSM_APP_DLY2_EN)
1649
+ | ((PCIE_LTSSM_APP_DLY2_EN | PCIE_LTSSM_ENABLE_ENHANCE) << 16);
12191650 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_HOT_RESET_CTRL, val);
12201651 }
12211652
....@@ -1253,7 +1684,7 @@
12531684 static int rk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
12541685 irq_hw_number_t hwirq)
12551686 {
1256
- irq_set_chip_and_handler(irq, &rk_pcie_legacy_irq_chip, handle_simple_irq);
1687
+ irq_set_chip_and_handler(irq, &rk_pcie_legacy_irq_chip, handle_level_irq);
12571688 irq_set_chip_data(irq, domain->host_data);
12581689
12591690 return 0;
....@@ -1342,41 +1773,6 @@
13421773 return ret;
13431774 }
13441775
1345
-static int rk_pci_find_capability(struct rk_pcie *rk_pcie, int cap)
1346
-{
1347
- u32 header;
1348
- int ttl;
1349
- int start = 0;
1350
- int pos = PCI_CFG_SPACE_SIZE;
1351
-
1352
- /* minimum 8 bytes per capability */
1353
- ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1354
-
1355
- header = dw_pcie_readl_dbi(rk_pcie->pci, pos);
1356
-
1357
- /*
1358
- * If we have no capabilities, this is indicated by cap ID,
1359
- * cap version and next pointer all being 0.
1360
- */
1361
- if (header == 0)
1362
- return 0;
1363
-
1364
- while (ttl-- > 0) {
1365
- if (PCI_EXT_CAP_ID(header) == cap && pos != start)
1366
- return pos;
1367
-
1368
- pos = PCI_EXT_CAP_NEXT(header);
1369
- if (pos < PCI_CFG_SPACE_SIZE)
1370
- break;
1371
-
1372
- header = dw_pcie_readl_dbi(rk_pcie->pci, pos);
1373
- if (!header)
1374
- break;
1375
- }
1376
-
1377
- return 0;
1378
-}
1379
-
13801776 #define RAS_DES_EVENT(ss, v) \
13811777 do { \
13821778 dw_pcie_writel_dbi(pcie->pci, cap_base + 8, v); \
....@@ -1387,8 +1783,27 @@
13871783 {
13881784 struct rk_pcie *pcie = s->private;
13891785 int cap_base;
1786
+ u32 val = rk_pcie_readl_apb(pcie, PCIE_CLIENT_CDM_RASDES_TBA_INFO_CMN);
1787
+ char *pm;
13901788
1391
- cap_base = rk_pci_find_capability(pcie, PCI_EXT_CAP_ID_VNDR);
1789
+ if (val & BIT(6))
1790
+ pm = "In training";
1791
+ else if (val & BIT(5))
1792
+ pm = "L1.2";
1793
+ else if (val & BIT(4))
1794
+ pm = "L1.1";
1795
+ else if (val & BIT(3))
1796
+ pm = "L1";
1797
+ else if (val & BIT(2))
1798
+ pm = "L0";
1799
+ else if (val & 0x3)
1800
+ pm = (val == 0x3) ? "L0s" : (val & BIT(1) ? "RX L0s" : "TX L0s");
1801
+ else
1802
+ pm = "Invalid";
1803
+
1804
+ seq_printf(s, "Common event signal status: 0x%s\n", pm);
1805
+
1806
+ cap_base = dw_pcie_find_ext_capability(pcie->pci, PCI_EXT_CAP_ID_VNDR);
13921807 if (!cap_base) {
13931808 dev_err(pcie->pci->dev, "Not able to find RASDES CAP!\n");
13941809 return 0;
....@@ -1424,7 +1839,6 @@
14241839
14251840 return 0;
14261841 }
1427
-
14281842 static int rockchip_pcie_rasdes_open(struct inode *inode, struct file *file)
14291843 {
14301844 return single_open(file, rockchip_pcie_rasdes_show,
....@@ -1443,7 +1857,7 @@
14431857 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
14441858 return -EFAULT;
14451859
1446
- cap_base = rk_pci_find_capability(pcie, PCI_EXT_CAP_ID_VNDR);
1860
+ cap_base = dw_pcie_find_ext_capability(pcie->pci, PCI_EXT_CAP_ID_VNDR);
14471861 if (!cap_base) {
14481862 dev_err(pcie->pci->dev, "Not able to find RASDES CAP!\n");
14491863 return 0;
....@@ -1528,29 +1942,35 @@
15281942 const struct rk_pcie_of_data *data;
15291943 enum rk_pcie_device_mode mode;
15301944 struct device_node *np = pdev->dev.of_node;
1531
- struct platform_driver *drv = to_platform_driver(dev->driver);
1532
- u32 val;
1945
+ u32 val = 0;
15331946 int irq;
15341947
15351948 match = of_match_device(rk_pcie_of_match, dev);
1536
- if (!match)
1537
- return -EINVAL;
1949
+ if (!match) {
1950
+ ret = -EINVAL;
1951
+ goto release_driver;
1952
+ }
15381953
15391954 data = (struct rk_pcie_of_data *)match->data;
15401955 mode = (enum rk_pcie_device_mode)data->mode;
15411956
15421957 rk_pcie = devm_kzalloc(dev, sizeof(*rk_pcie), GFP_KERNEL);
1543
- if (!rk_pcie)
1544
- return -ENOMEM;
1958
+ if (!rk_pcie) {
1959
+ ret = -ENOMEM;
1960
+ goto release_driver;
1961
+ }
15451962
15461963 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1547
- if (!pci)
1548
- return -ENOMEM;
1964
+ if (!pci) {
1965
+ ret = -ENOMEM;
1966
+ goto release_driver;
1967
+ }
15491968
15501969 pci->dev = dev;
15511970 pci->ops = &dw_pcie_ops;
15521971
15531972 rk_pcie->mode = mode;
1973
+ rk_pcie->msi_vector_num = data->msi_vector_num;
15541974 rk_pcie->pci = pci;
15551975
15561976 if (of_device_is_compatible(np, "rockchip,rk1808-pcie") ||
....@@ -1565,20 +1985,41 @@
15651985 ret = rk_pcie_resource_get(pdev, rk_pcie);
15661986 if (ret) {
15671987 dev_err(dev, "resource init failed\n");
1568
- return ret;
1988
+ goto release_driver;
15691989 }
15701990
1991
+ if (!IS_ERR_OR_NULL(rk_pcie->prsnt_gpio)) {
1992
+ if (!gpiod_get_value(rk_pcie->prsnt_gpio)) {
1993
+ dev_info(dev, "device isn't present\n");
1994
+ ret = -ENODEV;
1995
+ goto release_driver;
1996
+ }
1997
+ }
1998
+
1999
+ rk_pcie->supports_clkreq = device_property_read_bool(dev, "supports-clkreq");
2000
+
2001
+retry_regulator:
15712002 /* DON'T MOVE ME: must be enable before phy init */
15722003 rk_pcie->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
15732004 if (IS_ERR(rk_pcie->vpcie3v3)) {
1574
- if (PTR_ERR(rk_pcie->vpcie3v3) != -ENODEV)
1575
- return PTR_ERR(rk_pcie->vpcie3v3);
2005
+ if (PTR_ERR(rk_pcie->vpcie3v3) != -ENODEV) {
2006
+ if (IS_ENABLED(CONFIG_PCIE_RK_THREADED_INIT)) {
2007
+ /* Deferred but in threaded context for most 10s */
2008
+ msleep(20);
2009
+ if (++val < 500)
2010
+ goto retry_regulator;
2011
+ }
2012
+
2013
+ ret = PTR_ERR(rk_pcie->vpcie3v3);
2014
+ goto release_driver;
2015
+ }
2016
+
15762017 dev_info(dev, "no vpcie3v3 regulator found\n");
15772018 }
15782019
15792020 ret = rk_pcie_enable_power(rk_pcie);
15802021 if (ret)
1581
- return ret;
2022
+ goto release_driver;
15822023
15832024 ret = rk_pcie_phy_init(rk_pcie);
15842025 if (ret) {
....@@ -1586,16 +2027,19 @@
15862027 goto disable_vpcie3v3;
15872028 }
15882029
1589
- ret = rk_pcie_reset_control_release(rk_pcie);
1590
- if (ret) {
1591
- dev_err(dev, "reset control init failed\n");
1592
- goto disable_vpcie3v3;
2030
+ rk_pcie->rsts = devm_reset_control_array_get_exclusive(dev);
2031
+ if (IS_ERR(rk_pcie->rsts)) {
2032
+ ret = PTR_ERR(rk_pcie->rsts);
2033
+ dev_err(dev, "failed to get reset lines\n");
2034
+ goto disable_phy;
15932035 }
2036
+
2037
+ reset_control_deassert(rk_pcie->rsts);
15942038
15952039 ret = rk_pcie_request_sys_irq(rk_pcie, pdev);
15962040 if (ret) {
15972041 dev_err(dev, "pcie irq init failed\n");
1598
- goto disable_vpcie3v3;
2042
+ goto disable_phy;
15992043 }
16002044
16012045 platform_set_drvdata(pdev, rk_pcie);
....@@ -1603,7 +2047,7 @@
16032047 ret = rk_pcie_clk_init(rk_pcie);
16042048 if (ret) {
16052049 dev_err(dev, "clock init failed\n");
1606
- goto disable_vpcie3v3;
2050
+ goto disable_phy;
16072051 }
16082052
16092053 dw_pcie_dbi_ro_wr_en(pci);
....@@ -1626,9 +2070,9 @@
16262070 /* Unmask all legacy interrupt from INTA~INTD */
16272071 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_MASK_LEGACY,
16282072 UNMASK_ALL_LEGACY_INT);
2073
+ } else {
2074
+ dev_info(dev, "missing legacy IRQ resource\n");
16292075 }
1630
-
1631
- dev_info(dev, "missing legacy IRQ resource\n");
16322076 }
16332077
16342078 /* Set PCIe mode */
....@@ -1642,13 +2086,39 @@
16422086 rk_pcie->is_signal_test = true;
16432087 }
16442088
1645
- /* Force into compliance mode */
1646
- if (device_property_read_bool(dev, "rockchip,compliance-mode")) {
1647
- val = dw_pcie_readl_dbi(pci, PCIE_CAP_LINK_CONTROL2_LINK_STATUS);
1648
- val |= BIT(4);
1649
- dw_pcie_writel_dbi(pci, PCIE_CAP_LINK_CONTROL2_LINK_STATUS, val);
2089
+ /*
2090
+ * Force into compliance mode
2091
+ * comp_prst is a two dimensional array of which the first element
2092
+ * stands for speed mode, and the second one is preset value encoding:
2093
+ * [0] 0->SMA tool control the signal switch, 1/2/3 is for manual Gen setting
2094
+ * [1] transmitter setting for manual Gen setting, valid only if [0] isn't zero.
2095
+ */
2096
+ if (!device_property_read_u32_array(dev, "rockchip,compliance-mode",
2097
+ rk_pcie->comp_prst, 2)) {
2098
+ BUG_ON(rk_pcie->comp_prst[0] > 3 || rk_pcie->comp_prst[1] > 10);
2099
+ if (!rk_pcie->comp_prst[0]) {
2100
+ dev_info(dev, "Auto compliance mode for SMA tool.\n");
2101
+ } else {
2102
+ dev_info(dev, "compliance mode for soldered board Gen%d, P%d.\n",
2103
+ rk_pcie->comp_prst[0], rk_pcie->comp_prst[1]);
2104
+ val = dw_pcie_readl_dbi(pci, PCIE_CAP_LINK_CONTROL2_LINK_STATUS);
2105
+ val |= BIT(4) | rk_pcie->comp_prst[0] | (rk_pcie->comp_prst[1] << 12);
2106
+ dw_pcie_writel_dbi(pci, PCIE_CAP_LINK_CONTROL2_LINK_STATUS, val);
2107
+ }
16502108 rk_pcie->is_signal_test = true;
16512109 }
2110
+
2111
+ /* Skip waiting for training to pass in system PM routine */
2112
+ if (device_property_read_bool(dev, "rockchip,skip-scan-in-resume"))
2113
+ rk_pcie->skip_scan_in_resume = true;
2114
+
2115
+ rk_pcie->hot_rst_wq = create_singlethread_workqueue("rk_pcie_hot_rst_wq");
2116
+ if (!rk_pcie->hot_rst_wq) {
2117
+ dev_err(dev, "failed to create hot_rst workqueue\n");
2118
+ ret = -ENOMEM;
2119
+ goto remove_irq_domain;
2120
+ }
2121
+ INIT_WORK(&rk_pcie->hot_rst_work, rk_pcie_hot_rst_work);
16522122
16532123 switch (rk_pcie->mode) {
16542124 case RK_PCIE_RC_TYPE:
....@@ -1663,12 +2133,12 @@
16632133 return 0;
16642134
16652135 if (ret)
1666
- goto remove_irq_domain;
2136
+ goto remove_rst_wq;
16672137
16682138 ret = rk_pcie_init_dma_trx(rk_pcie);
16692139 if (ret) {
16702140 dev_err(dev, "failed to add dma extension\n");
1671
- return ret;
2141
+ goto remove_rst_wq;
16722142 }
16732143
16742144 if (rk_pcie->dma_obj) {
....@@ -1680,13 +2150,15 @@
16802150 /* hold link reset grant after link-up */
16812151 ret = rk_pcie_reset_grant_ctrl(rk_pcie, false);
16822152 if (ret)
1683
- goto remove_irq_domain;
2153
+ goto remove_rst_wq;
16842154 }
16852155
16862156 dw_pcie_dbi_ro_wr_dis(pci);
16872157
16882158 device_init_wakeup(dev, true);
1689
- drv->driver.pm = &rockchip_dw_pcie_pm_ops;
2159
+
2160
+ /* Enable async system PM for multiports SoC */
2161
+ device_enable_async_suspend(dev);
16902162
16912163 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
16922164 ret = rockchip_pcie_debugfs_init(rk_pcie);
....@@ -1694,7 +2166,7 @@
16942166 dev_err(dev, "failed to setup debugfs: %d\n", ret);
16952167
16962168 /* Enable RASDES Error event by default */
1697
- val = rk_pci_find_capability(rk_pcie, PCI_EXT_CAP_ID_VNDR);
2169
+ val = dw_pcie_find_ext_capability(rk_pcie->pci, PCI_EXT_CAP_ID_VNDR);
16982170 if (!val) {
16992171 dev_err(dev, "Not able to find RASDES CAP!\n");
17002172 return 0;
....@@ -1706,36 +2178,190 @@
17062178
17072179 return 0;
17082180
2181
+remove_rst_wq:
2182
+ destroy_workqueue(rk_pcie->hot_rst_wq);
17092183 remove_irq_domain:
17102184 if (rk_pcie->irq_domain)
17112185 irq_domain_remove(rk_pcie->irq_domain);
2186
+disable_phy:
2187
+ phy_power_off(rk_pcie->phy);
2188
+ phy_exit(rk_pcie->phy);
17122189 deinit_clk:
1713
- rk_pcie_clk_deinit(rk_pcie);
2190
+ clk_bulk_disable_unprepare(rk_pcie->clk_cnt, rk_pcie->clks);
17142191 disable_vpcie3v3:
17152192 rk_pcie_disable_power(rk_pcie);
2193
+release_driver:
2194
+ if (IS_ENABLED(CONFIG_PCIE_RK_THREADED_INIT))
2195
+ device_release_driver(dev);
17162196
17172197 return ret;
17182198 }
17192199
17202200 static int rk_pcie_probe(struct platform_device *pdev)
17212201 {
1722
- struct task_struct *tsk;
2202
+ if (IS_ENABLED(CONFIG_PCIE_RK_THREADED_INIT)) {
2203
+ struct task_struct *tsk;
17232204
1724
- tsk = kthread_run(rk_pcie_really_probe, pdev, "rk-pcie");
1725
- if (IS_ERR(tsk)) {
1726
- dev_err(&pdev->dev, "start rk-pcie thread failed\n");
1727
- return PTR_ERR(tsk);
2205
+ tsk = kthread_run(rk_pcie_really_probe, pdev, "rk-pcie");
2206
+ if (IS_ERR(tsk)) {
2207
+ dev_err(&pdev->dev, "start rk-pcie thread failed\n");
2208
+ return PTR_ERR(tsk);
2209
+ }
2210
+
2211
+ return 0;
17282212 }
1729
- return 0;
2213
+
2214
+ return rk_pcie_really_probe(pdev);
17302215 }
2216
+
2217
+#ifdef CONFIG_PCIEASPM
2218
+static void rk_pcie_downstream_dev_to_d0(struct rk_pcie *rk_pcie, bool enable)
2219
+{
2220
+ struct pcie_port *pp = &rk_pcie->pci->pp;
2221
+ struct pci_bus *child, *root_bus = NULL;
2222
+ struct pci_dev *pdev, *bridge;
2223
+ u32 val;
2224
+
2225
+ list_for_each_entry(child, &pp->bridge->bus->children, node) {
2226
+ /* Bring downstream devices to D3 if they are not already in */
2227
+ if (child->parent == pp->bridge->bus) {
2228
+ root_bus = child;
2229
+ bridge = root_bus->self;
2230
+ break;
2231
+ }
2232
+ }
2233
+
2234
+ if (!root_bus) {
2235
+ dev_err(rk_pcie->pci->dev, "Failed to find downstream devices\n");
2236
+ return;
2237
+ }
2238
+
2239
+ /* Save and restore root bus ASPM */
2240
+ if (enable) {
2241
+ if (rk_pcie->l1ss_ctl1)
2242
+ dw_pcie_writel_dbi(rk_pcie->pci, bridge->l1ss + PCI_L1SS_CTL1, rk_pcie->l1ss_ctl1);
2243
+
2244
+ /* rk_pcie->aspm woule be saved in advance when enable is false */
2245
+ dw_pcie_writel_dbi(rk_pcie->pci, bridge->pcie_cap + PCI_EXP_LNKCTL, rk_pcie->aspm);
2246
+ } else {
2247
+ val = dw_pcie_readl_dbi(rk_pcie->pci, bridge->l1ss + PCI_L1SS_CTL1);
2248
+ if (val & PCI_L1SS_CTL1_L1SS_MASK)
2249
+ rk_pcie->l1ss_ctl1 = val;
2250
+ else
2251
+ rk_pcie->l1ss_ctl1 = 0;
2252
+
2253
+ val = dw_pcie_readl_dbi(rk_pcie->pci, bridge->pcie_cap + PCI_EXP_LNKCTL);
2254
+ rk_pcie->aspm = val & PCI_EXP_LNKCTL_ASPMC;
2255
+ val &= ~(PCI_EXP_LNKCAP_ASPM_L1 | PCI_EXP_LNKCAP_ASPM_L0S);
2256
+ dw_pcie_writel_dbi(rk_pcie->pci, bridge->pcie_cap + PCI_EXP_LNKCTL, val);
2257
+ }
2258
+
2259
+ list_for_each_entry(pdev, &root_bus->devices, bus_list) {
2260
+ if (PCI_SLOT(pdev->devfn) == 0) {
2261
+ if (pci_set_power_state(pdev, PCI_D0))
2262
+ dev_err(rk_pcie->pci->dev,
2263
+ "Failed to transition %s to D3hot state\n",
2264
+ dev_name(&pdev->dev));
2265
+ if (enable) {
2266
+ if (rk_pcie->l1ss_ctl1) {
2267
+ pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1, &val);
2268
+ val &= ~PCI_L1SS_CTL1_L1SS_MASK;
2269
+ val |= (rk_pcie->l1ss_ctl1 & PCI_L1SS_CTL1_L1SS_MASK);
2270
+ pci_write_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1, val);
2271
+ }
2272
+
2273
+ pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
2274
+ PCI_EXP_LNKCTL_ASPMC, rk_pcie->aspm);
2275
+ } else {
2276
+ pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
2277
+ }
2278
+ }
2279
+ }
2280
+}
2281
+#endif
17312282
17322283 static int __maybe_unused rockchip_dw_pcie_suspend(struct device *dev)
17332284 {
17342285 struct rk_pcie *rk_pcie = dev_get_drvdata(dev);
1735
- int ret;
2286
+ int ret = 0, power;
2287
+ struct dw_pcie *pci = rk_pcie->pci;
2288
+ u32 status;
2289
+
2290
+ /*
2291
+ * This is as per PCI Express Base r5.0 r1.0 May 22-2019,
2292
+ * 5.2 Link State Power Management (Page #440).
2293
+ *
2294
+ * L2/L3 Ready entry negotiations happen while in the L0 state.
2295
+ * L2/L3 Ready are entered only after the negotiation completes.
2296
+ *
2297
+ * The following example sequence illustrates the multi-step Link state
2298
+ * transition process leading up to entering a system sleep state:
2299
+ * 1. System software directs all Functions of a Downstream component to D3Hot.
2300
+ * 2. The Downstream component then initiates the transition of the Link to L1
2301
+ * as required.
2302
+ * 3. System software then causes the Root Complex to broadcast the PME_Turn_Off
2303
+ * Message in preparation for removing the main power source.
2304
+ * 4. This Message causes the subject Link to transition back to L0 in order to
2305
+ * send it and to enable the Downstream component to respond with PME_TO_Ack.
2306
+ * 5. After sending the PME_TO_Ack, the Downstream component initiates the L2/L3
2307
+ * Ready transition protocol.
2308
+ */
2309
+
2310
+ /* 1. All sub-devices are in D3hot by PCIe stack */
2311
+ dw_pcie_dbi_ro_wr_dis(rk_pcie->pci);
17362312
17372313 rk_pcie_link_status_clear(rk_pcie);
2314
+
2315
+ /*
2316
+ * Wlan devices will be shutdown from function driver now, so doing L2 here
2317
+ * must fail. Skip L2 routine.
2318
+ */
2319
+ if (rk_pcie->skip_scan_in_resume) {
2320
+ rfkill_get_wifi_power_state(&power);
2321
+ if (!power)
2322
+ goto no_l2;
2323
+ }
2324
+
2325
+ /* 2. Broadcast PME_Turn_Off Message */
2326
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_MSG_GEN, PME_TURN_OFF);
2327
+ ret = readl_poll_timeout(rk_pcie->apb_base + PCIE_CLIENT_MSG_GEN,
2328
+ status, !(status & BIT(4)), 20, RK_PCIE_L2_TMOUT_US);
2329
+ if (ret) {
2330
+ dev_err(dev, "Failed to send PME_Turn_Off\n");
2331
+ goto no_l2;
2332
+ }
2333
+
2334
+ /* 3. Wait for PME_TO_Ack */
2335
+ ret = readl_poll_timeout(rk_pcie->apb_base + PCIE_CLIENT_INTR_STATUS_MSG_RX,
2336
+ status, status & BIT(9), 20, RK_PCIE_L2_TMOUT_US);
2337
+ if (ret) {
2338
+ dev_err(dev, "Failed to receive PME_TO_Ack\n");
2339
+ goto no_l2;
2340
+ }
2341
+
2342
+ /* 4. Clear PME_TO_Ack and Wait for ready to enter L23 message */
2343
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_STATUS_MSG_RX, PME_TO_ACK);
2344
+ ret = readl_poll_timeout(rk_pcie->apb_base + PCIE_CLIENT_POWER,
2345
+ status, status & READY_ENTER_L23, 20, RK_PCIE_L2_TMOUT_US);
2346
+ if (ret) {
2347
+ dev_err(dev, "Failed to ready to enter L23\n");
2348
+ goto no_l2;
2349
+ }
2350
+
2351
+ /* 5. Check we are in L2 */
2352
+ ret = readl_poll_timeout(rk_pcie->apb_base + PCIE_CLIENT_LTSSM_STATUS,
2353
+ status, ((status & S_MAX) == S_L2_IDLE), 20, RK_PCIE_L2_TMOUT_US);
2354
+ if (ret)
2355
+ dev_err(pci->dev, "Link isn't in L2 idle!\n");
2356
+
2357
+no_l2:
17382358 rk_pcie_disable_ltssm(rk_pcie);
2359
+
2360
+ ret = phy_validate(rk_pcie->phy, PHY_TYPE_PCIE, 0, NULL);
2361
+ if (ret && ret != -EOPNOTSUPP) {
2362
+ dev_err(dev, "PHY is reused by other controller, check the dts!\n");
2363
+ return ret;
2364
+ }
17392365
17402366 /* make sure assert phy success */
17412367 usleep_range(200, 300);
....@@ -1743,15 +2369,14 @@
17432369 phy_power_off(rk_pcie->phy);
17442370 phy_exit(rk_pcie->phy);
17452371
1746
- clk_bulk_disable(rk_pcie->clk_cnt, rk_pcie->clks);
2372
+ rk_pcie->intx = rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_INTR_MASK_LEGACY);
2373
+
2374
+ clk_bulk_disable_unprepare(rk_pcie->clk_cnt, rk_pcie->clks);
17472375
17482376 rk_pcie->in_suspend = true;
17492377
17502378 gpiod_set_value_cansleep(rk_pcie->rst_gpio, 0);
17512379 ret = rk_pcie_disable_power(rk_pcie);
1752
-
1753
- if (rk_pcie->pci->pp.msi_irq > 0)
1754
- dw_pcie_free_msi(&rk_pcie->pci->pp);
17552380
17562381 return ret;
17572382 }
....@@ -1762,20 +2387,25 @@
17622387 bool std_rc = rk_pcie->mode == RK_PCIE_RC_TYPE && !rk_pcie->dma_obj;
17632388 int ret;
17642389
2390
+ reset_control_assert(rk_pcie->rsts);
2391
+ udelay(10);
2392
+ reset_control_deassert(rk_pcie->rsts);
2393
+
17652394 ret = rk_pcie_enable_power(rk_pcie);
17662395 if (ret)
17672396 return ret;
17682397
1769
- ret = clk_bulk_enable(rk_pcie->clk_cnt, rk_pcie->clks);
2398
+ ret = clk_bulk_prepare_enable(rk_pcie->clk_cnt, rk_pcie->clks);
17702399 if (ret) {
1771
- clk_bulk_unprepare(rk_pcie->clk_cnt, rk_pcie->clks);
2400
+ dev_err(dev, "failed to prepare enable pcie bulk clks: %d\n", ret);
17722401 return ret;
17732402 }
17742403
1775
- ret = phy_set_mode(rk_pcie->phy, rk_pcie->phy_mode);
2404
+ ret = phy_set_mode_ext(rk_pcie->phy, rk_pcie->phy_mode,
2405
+ rk_pcie->phy_sub_mode);
17762406 if (ret) {
17772407 dev_err(dev, "fail to set phy to mode %s, err %d\n",
1778
- (rk_pcie->phy_mode == PHY_MODE_PCIE_RC) ? "RC" : "EP",
2408
+ (rk_pcie->phy_sub_mode == PHY_MODE_PCIE_RC) ? "RC" : "EP",
17792409 ret);
17802410 return ret;
17812411 }
....@@ -1805,14 +2435,14 @@
18052435 if (std_rc)
18062436 dw_pcie_setup_rc(&rk_pcie->pci->pp);
18072437
2438
+ rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_INTR_MASK_LEGACY,
2439
+ rk_pcie->intx | 0xffff0000);
2440
+
18082441 ret = rk_pcie_establish_link(rk_pcie->pci);
18092442 if (ret) {
18102443 dev_err(dev, "failed to establish pcie link\n");
18112444 goto err;
18122445 }
1813
-
1814
- if (rk_pcie->pci->pp.msi_irq > 0)
1815
- dw_pcie_msi_init(&rk_pcie->pci->pp);
18162446
18172447 if (std_rc)
18182448 goto std_rc_done;
....@@ -1836,6 +2466,9 @@
18362466 goto err;
18372467 }
18382468
2469
+ if (rk_pcie->pci->pp.msi_irq > 0)
2470
+ dw_pcie_msi_init(&rk_pcie->pci->pp);
2471
+
18392472 return 0;
18402473 err:
18412474 rk_pcie_disable_power(rk_pcie);
....@@ -1843,7 +2476,33 @@
18432476 return ret;
18442477 }
18452478
2479
+#ifdef CONFIG_PCIEASPM
2480
+static int rockchip_dw_pcie_prepare(struct device *dev)
2481
+{
2482
+ struct rk_pcie *rk_pcie = dev_get_drvdata(dev);
2483
+
2484
+ dw_pcie_dbi_ro_wr_en(rk_pcie->pci);
2485
+ rk_pcie_downstream_dev_to_d0(rk_pcie, false);
2486
+ dw_pcie_dbi_ro_wr_dis(rk_pcie->pci);
2487
+
2488
+ return 0;
2489
+}
2490
+
2491
+static void rockchip_dw_pcie_complete(struct device *dev)
2492
+{
2493
+ struct rk_pcie *rk_pcie = dev_get_drvdata(dev);
2494
+
2495
+ dw_pcie_dbi_ro_wr_en(rk_pcie->pci);
2496
+ rk_pcie_downstream_dev_to_d0(rk_pcie, true);
2497
+ dw_pcie_dbi_ro_wr_dis(rk_pcie->pci);
2498
+}
2499
+#endif
2500
+
18462501 static const struct dev_pm_ops rockchip_dw_pcie_pm_ops = {
2502
+#ifdef CONFIG_PCIEASPM
2503
+ .prepare = rockchip_dw_pcie_prepare,
2504
+ .complete = rockchip_dw_pcie_complete,
2505
+#endif
18472506 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_dw_pcie_suspend,
18482507 rockchip_dw_pcie_resume)
18492508 };
....@@ -1853,10 +2512,12 @@
18532512 .name = "rk-pcie",
18542513 .of_match_table = rk_pcie_of_match,
18552514 .suppress_bind_attrs = true,
2515
+ .pm = &rockchip_dw_pcie_pm_ops,
18562516 },
2517
+ .probe = rk_pcie_probe,
18572518 };
18582519
1859
-module_platform_driver_probe(rk_plat_pcie_driver, rk_pcie_probe);
2520
+module_platform_driver(rk_plat_pcie_driver);
18602521
18612522 MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com>");
18622523 MODULE_DESCRIPTION("RockChip PCIe Controller driver");