-
Notifications
You must be signed in to change notification settings - Fork 103
/
pci-imx6.c
2956 lines (2650 loc) · 80.3 KB
/
pci-imx6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* PCIe host controller driver for Freescale i.MX6 SoCs
*
* Copyright (C) 2013 Kosagi
* http://www.kosagi.com
*
* Author: Sean Cross <xobs@kosagi.com>
*/
#include <dt-bindings/soc/imx8_hsio.h>
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/of_pci.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/resource.h>
#include <linux/signal.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/phy/phy.h>
#include <linux/reset.h>
#include <linux/pm_domain.h>
#include <linux/pm_runtime.h>
#include <linux/busfreq-imx.h>
#include "../../pci.h"
#include "pcie-designware.h"
#define IMX8MQ_PCIE_LINK_CAP_REG_OFFSET 0x7c
#define IMX8MQ_PCIE_LINK_CAP_L1EL_64US GENMASK(18, 17)
#define IMX8MQ_PCIE_L1SUB_CTRL1_REG_EN_MASK 0xf
#define IMX8MQ_GPR_PCIE_REF_USE_PAD BIT(9)
#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN BIT(10)
#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE BIT(11)
#define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE GENMASK(11, 8)
#define IMX8MQ_PCIE2_BASE_ADDR 0x33c00000
#define IMX8_HSIO_PCIEB_BASE_ADDR 0x5f010000
#define IMX8MP_GPR_REG0 0x0
#define IMX8MP_GPR_REG0_CLK_MOD_EN BIT(0)
#define IMX8MP_GPR_REG0_PHY_APB_RST BIT(4)
#define IMX8MP_GPR_REG0_PHY_INIT_RST BIT(5)
#define IMX8MP_GPR_REG1 0x4
#define IMX8MP_GPR_REG1_PM_EN_CORE_CLK BIT(0)
#define IMX8MP_GPR_REG1_PLL_LOCK BIT(13)
#define IMX8MP_GPR_REG2 0x8
#define IMX8MP_GPR_REG2_P_PLL_MASK GENMASK(5, 0)
#define IMX8MP_GPR_REG2_M_PLL_MASK GENMASK(15, 6)
#define IMX8MP_GPR_REG2_S_PLL_MASK GENMASK(18, 16)
#define IMX8MP_GPR_REG2_P_PLL (0xc << 0)
#define IMX8MP_GPR_REG2_M_PLL (0x320 << 6)
#define IMX8MP_GPR_REG2_S_PLL (0x4 << 16)
#define IMX8MP_GPR_REG3 0xc
#define IMX8MP_GPR_REG3_PLL_CKE BIT(17)
#define IMX8MP_GPR_REG3_PLL_RST BIT(31)
#define IMX8MP_GPR_PCIE_SSC_EN BIT(16)
#define IMX8MP_GPR_PCIE_PWR_OFF BIT(17)
#define IMX8MP_GPR_PCIE_CMN_RSTN BIT(18)
#define IMX8MP_GPR_PCIE_AUX_EN BIT(19)
#define IMX8MP_GPR_PCIE_REF_SEL_MASK GENMASK(25, 24)
#define IMX8MP_GPR_PCIE_REF_PLL_SYS GENMASK(25, 24)
#define IMX8MP_GPR_PCIE_REF_EXT_OSC BIT(25)
#define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
enum imx6_pcie_variants {
IMX6Q,
IMX6SX,
IMX6QP,
IMX7D,
IMX8MQ,
IMX8MM,
IMX8QM,
IMX8QXP,
IMX8MP,
IMX8QXP_EP,
IMX8QM_EP,
IMX8MQ_EP,
IMX8MM_EP,
IMX8MP_EP,
IMX6SX_EP,
IMX7D_EP,
IMX6Q_EP,
IMX6QP_EP,
};
#define IMX6_PCIE_FLAG_IMX6_PHY BIT(0)
#define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE BIT(1)
#define IMX6_PCIE_FLAG_SUPPORTS_SUSPEND BIT(2)
#define IMX6_PCIE_FLAG_IMX6_CPU_ADDR_FIXUP BIT(3)
#define IMX6_PCIE_FLAG_SUPPORTS_L1SS BIT(4)
struct imx6_pcie_drvdata {
enum imx6_pcie_variants variant;
enum dw_pcie_device_mode mode;
u32 flags;
int dbi_length;
};
struct imx6_pcie {
struct dw_pcie *pci;
int clkreq_gpio;
int dis_gpio;
int reset_gpio;
bool gpio_active_high;
struct clk *pcie_bus;
struct clk *pcie_phy;
struct clk *pcie_phy_pclk;
struct clk *pcie_per;
struct clk *pciex2_per;
struct clk *pcie_inbound_axi;
struct clk *pcie;
struct clk *pcie_aux;
struct clk *phy_per;
struct clk *misc_per;
struct regmap *iomuxc_gpr;
u32 controller_id;
struct reset_control *pciephy_reset;
struct reset_control *pciephy_perst;
struct reset_control *apps_reset;
struct reset_control *turnoff_reset;
struct reset_control *clkreq_reset;
u32 tx_deemph_gen1;
u32 tx_deemph_gen2_3p5db;
u32 tx_deemph_gen2_6db;
u32 tx_swing_full;
u32 tx_swing_low;
u32 hsio_cfg;
u32 ext_osc;
u32 local_addr;
int link_gen;
struct regulator *vpcie;
void __iomem *phy_base;
void __iomem *hsmix_base;
/* power domain for pcie */
struct device *pd_pcie;
/* power domain for pcie csr access */
struct device *pd_pcie_per;
/* power domain for pcie phy */
struct device *pd_pcie_phy;
/* power domain for hsio gpio used by pcie */
struct device *pd_hsio_gpio;
struct device_link *pd_link;
struct device_link *pd_per_link;
struct device_link *pd_phy_link;
struct device_link *pd_hsio_link;
const struct imx6_pcie_drvdata *drvdata;
struct regulator *epdev_on;
struct phy *phy;
};
/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
#define PHY_PLL_LOCK_WAIT_MAX_RETRIES 2000
#define PHY_PLL_LOCK_WAIT_USLEEP_MIN 50
#define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
#define PHY_PLL_LOCK_WAIT_TIMEOUT (2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
/* PCIe Root Complex registers (memory-mapped) */
#define PCIE_RC_IMX6_MSI_CAP 0x50
#define PCIE_RC_LCR 0x7c
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
#define PCIE_RC_LCSR 0x80
#define PCIE_RC_LC2SR 0xa0
/* PCIe Port Logic registers (memory-mapped) */
#define PL_OFFSET 0x700
#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
#define PCIE_PHY_CTRL_DATA(x) FIELD_PREP(GENMASK(15, 0), (x))
#define PCIE_PHY_CTRL_CAP_ADR BIT(16)
#define PCIE_PHY_CTRL_CAP_DAT BIT(17)
#define PCIE_PHY_CTRL_WR BIT(18)
#define PCIE_PHY_CTRL_RD BIT(19)
#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
#define PCIE_PHY_STAT_ACK BIT(16)
#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
/* PHY registers (not memory-mapped) */
#define PCIE_PHY_ATEOVRD 0x10
#define PCIE_PHY_ATEOVRD_EN BIT(2)
#define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0
#define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1
#define PCIE_PHY_MPLL_OVRD_IN_LO 0x11
#define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2
#define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f
#define PCIE_PHY_MPLL_MULTIPLIER_OVRD BIT(9)
/* iMX7 PCIe PHY registers */
#define PCIE_PHY_CMN_REG4 0x14
/* These are probably the bits that *aren't* DCC_FB_EN */
#define PCIE_PHY_CMN_REG4_DCC_FB_EN 0x29
#define PCIE_PHY_CMN_REG24 0x90
#define PCIE_PHY_CMN_REG24_RX_EQ BIT(6)
#define PCIE_PHY_CMN_REG24_RX_EQ_SEL BIT(3)
#define PCIE_PHY_CMN_REG26 0x98
#define PCIE_PHY_CMN_REG26_ATT_MODE 0xBC
#define PCIE_PHY_CMN_REG62 0x188
#define PCIE_PHY_CMN_REG62_PLL_CLK_OUT 0x08
#define PCIE_PHY_CMN_REG64 0x190
#define PCIE_PHY_CMN_REG64_AUX_RX_TX_TERM 0x8C
#define PCIE_PHY_CMN_REG75 0x1D4
#define PCIE_PHY_CMN_REG75_PLL_DONE 0x3
#define PCIE_PHY_TRSV_REG5 0x414
#define PCIE_PHY_TRSV_REG5_GEN1_DEEMP 0x2D
#define PCIE_PHY_TRSV_REG6 0x418
#define PCIE_PHY_TRSV_REG6_GEN2_DEEMP 0xF
#define PHY_RX_OVRD_IN_LO 0x1005
#define PHY_RX_OVRD_IN_LO_RX_DATA_EN BIT(5)
#define PHY_RX_OVRD_IN_LO_RX_PLL_EN BIT(3)
/* iMX8 HSIO registers */
#define IMX8QM_PHYX2_LPCG_OFFSET 0x00000
#define IMX8QM_PHYX2_LPCG_PCLK0_MASK GENMASK(17, 16)
#define IMX8QM_PHYX2_LPCG_PCLK1_MASK GENMASK(21, 20)
#define IMX8QM_CSR_PHYX2_OFFSET 0x90000
#define IMX8QM_CSR_PHYX1_OFFSET 0xA0000
#define IMX8QM_CSR_PHYX_STTS0_OFFSET 0x4
#define IMX8QM_CSR_PCIEA_OFFSET 0xB0000
#define IMX8QM_CSR_PCIEB_OFFSET 0xC0000
#define IMX8QM_CSR_PCIE_CTRL1_OFFSET 0x4
#define IMX8QM_CSR_PCIE_CTRL2_OFFSET 0x8
#define IMX8QM_CSR_PCIE_STTS0_OFFSET 0xC
#define IMX8QM_CSR_MISC_OFFSET 0xE0000
#define IMX8QM_CTRL_LTSSM_ENABLE BIT(4)
#define IMX8QM_CTRL_READY_ENTR_L23 BIT(5)
#define IMX8QM_CTRL_PM_XMT_TURNOFF BIT(9)
#define IMX8QM_CTRL_BUTTON_RST_N BIT(21)
#define IMX8QM_CTRL_PERST_N BIT(22)
#define IMX8QM_CTRL_POWER_UP_RST_N BIT(23)
#define IMX8QM_CTRL_STTS0_PM_LINKST_IN_L2 BIT(13)
#define IMX8QM_CTRL_STTS0_PM_REQ_CORE_RST BIT(19)
#define IMX8QM_STTS0_LANE0_TX_PLL_LOCK BIT(4)
#define IMX8QM_STTS0_LANE1_TX_PLL_LOCK BIT(12)
#define IMX8QM_PCIE_TYPE_MASK GENMASK(27, 24)
#define IMX8QM_PHYX2_CTRL0_APB_MASK 0x3
#define IMX8QM_PHY_APB_RSTN_0 BIT(0)
#define IMX8QM_PHY_APB_RSTN_1 BIT(1)
#define IMX8QM_MISC_IOB_RXENA BIT(0)
#define IMX8QM_MISC_IOB_TXENA BIT(1)
#define IMX8QM_CSR_MISC_IOB_A_0_TXOE BIT(2)
#define IMX8QM_CSR_MISC_IOB_A_0_M1M0_MASK (0x3 << 3)
#define IMX8QM_CSR_MISC_IOB_A_0_M1M0_2 BIT(4)
#define IMX8QM_MISC_PHYX1_EPCS_SEL BIT(12)
#define IMX8QM_MISC_PCIE_AB_SELECT BIT(13)
#define IMX8QM_MISC_CLKREQ_1 BIT(22)
#define IMX8QM_MISC_CLKREQ_0 BIT(23)
#define IMX8QM_MISC_CLKREQ_OVERRIDE_EN_1 BIT(24)
#define IMX8QM_MISC_CLKREQ_OVERRIDE_EN_0 BIT(25)
#define IMX8MM_GPR_PCIE_REF_CLK_SEL (0x3 << 24)
#define IMX8MM_GPR_PCIE_REF_CLK_PLL (0x3 << 24)
#define IMX8MM_GPR_PCIE_REF_CLK_EXT (0x2 << 24)
#define IMX8MM_GPR_PCIE_AUX_EN BIT(19)
#define IMX8MM_GPR_PCIE_CMN_RST BIT(18)
#define IMX8MM_GPR_PCIE_POWER_OFF BIT(17)
#define IMX8MM_GPR_PCIE_SSC_EN BIT(16)
static void imx6_pcie_ltssm_disable(struct device *dev);
static bool imx6_pcie_readable_reg(struct device *dev, unsigned int reg)
{
enum imx6_pcie_variants variant;
struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
variant = imx6_pcie->drvdata->variant;
if (variant == IMX8QXP || variant == IMX8QXP_EP) {
switch (reg) {
case IMX8QM_CSR_PHYX1_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET:
case IMX8QM_CSR_MISC_OFFSET:
case IMX8QM_CSR_PHYX1_OFFSET + IMX8QM_CSR_PHYX_STTS0_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL1_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_STTS0_OFFSET:
return true;
default:
return false;
}
} else {
switch (reg) {
case IMX8QM_PHYX2_LPCG_OFFSET:
case IMX8QM_CSR_PHYX2_OFFSET:
case IMX8QM_CSR_PHYX1_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET:
case IMX8QM_CSR_MISC_OFFSET:
case IMX8QM_CSR_PHYX2_OFFSET + IMX8QM_CSR_PHYX_STTS0_OFFSET:
case IMX8QM_CSR_PHYX1_OFFSET + IMX8QM_CSR_PHYX_STTS0_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_CTRL1_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_STTS0_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL1_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_STTS0_OFFSET:
return true;
default:
return false;
}
}
}
static bool imx6_pcie_writeable_reg(struct device *dev, unsigned int reg)
{
enum imx6_pcie_variants variant;
struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
variant = imx6_pcie->drvdata->variant;
if (variant == IMX8QXP || variant == IMX8QXP_EP) {
switch (reg) {
case IMX8QM_CSR_PHYX1_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET:
case IMX8QM_CSR_MISC_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL1_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET:
return true;
default:
return false;
}
} else {
switch (reg) {
case IMX8QM_PHYX2_LPCG_OFFSET:
case IMX8QM_CSR_PHYX2_OFFSET:
case IMX8QM_CSR_PHYX1_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET:
case IMX8QM_CSR_MISC_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_CTRL1_OFFSET:
case IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL1_OFFSET:
case IMX8QM_CSR_PCIEB_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET:
return true;
default:
return false;
}
}
}
static const struct regmap_config imx6_pcie_regconfig = {
.max_register = IMX8QM_CSR_MISC_OFFSET,
.reg_bits = 32,
.val_bits = 32,
.reg_stride = 4,
.val_format_endian = REGMAP_ENDIAN_NATIVE,
.num_reg_defaults_raw = IMX8QM_CSR_MISC_OFFSET / sizeof(uint32_t) + 1,
.readable_reg = imx6_pcie_readable_reg,
.writeable_reg = imx6_pcie_writeable_reg,
.cache_type = REGCACHE_NONE,
};
static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, bool exp_val)
{
struct dw_pcie *pci = imx6_pcie->pci;
bool val;
u32 max_iterations = 10;
u32 wait_counter = 0;
do {
val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT) &
PCIE_PHY_STAT_ACK;
wait_counter++;
if (val == exp_val)
return 0;
udelay(1);
} while (wait_counter < max_iterations);
return -ETIMEDOUT;
}
static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
{
struct dw_pcie *pci = imx6_pcie->pci;
u32 val;
int ret;
val = PCIE_PHY_CTRL_DATA(addr);
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
val |= PCIE_PHY_CTRL_CAP_ADR;
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
ret = pcie_phy_poll_ack(imx6_pcie, true);
if (ret)
return ret;
val = PCIE_PHY_CTRL_DATA(addr);
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
return pcie_phy_poll_ack(imx6_pcie, false);
}
/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, u16 *data)
{
struct dw_pcie *pci = imx6_pcie->pci;
u32 phy_ctl;
int ret;
ret = pcie_phy_wait_ack(imx6_pcie, addr);
if (ret)
return ret;
/* assert Read signal */
phy_ctl = PCIE_PHY_CTRL_RD;
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
ret = pcie_phy_poll_ack(imx6_pcie, true);
if (ret)
return ret;
*data = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
/* deassert Read signal */
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
return pcie_phy_poll_ack(imx6_pcie, false);
}
static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, u16 data)
{
struct dw_pcie *pci = imx6_pcie->pci;
u32 var;
int ret;
/* write addr */
/* cap addr */
ret = pcie_phy_wait_ack(imx6_pcie, addr);
if (ret)
return ret;
var = PCIE_PHY_CTRL_DATA(data);
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
/* capture data */
var |= PCIE_PHY_CTRL_CAP_DAT;
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
ret = pcie_phy_poll_ack(imx6_pcie, true);
if (ret)
return ret;
/* deassert cap data */
var = PCIE_PHY_CTRL_DATA(data);
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
/* wait for ack de-assertion */
ret = pcie_phy_poll_ack(imx6_pcie, false);
if (ret)
return ret;
/* assert wr signal */
var = PCIE_PHY_CTRL_WR;
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
/* wait for ack */
ret = pcie_phy_poll_ack(imx6_pcie, true);
if (ret)
return ret;
/* deassert wr signal */
var = PCIE_PHY_CTRL_DATA(data);
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
/* wait for ack de-assertion */
ret = pcie_phy_poll_ack(imx6_pcie, false);
if (ret)
return ret;
dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
return 0;
}
static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
{
u16 tmp;
if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
return;
pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
PHY_RX_OVRD_IN_LO_RX_PLL_EN);
pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
usleep_range(2000, 3000);
pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
PHY_RX_OVRD_IN_LO_RX_PLL_EN);
pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
}
#ifdef CONFIG_ARM
/* Added for PCI abort handling */
static int imx6q_pcie_abort_handler(unsigned long addr,
unsigned int fsr, struct pt_regs *regs)
{
unsigned long pc = instruction_pointer(regs);
unsigned long instr;
int reg ;
/* if the abort from user-space, just return and report it */
if (user_mode(regs))
return 1;
instr = *(unsigned long *)pc;
reg = (instr >> 12) & 15;
/*
* If the instruction being executed was a read,
* make it look like it read all-ones.
*/
if ((instr & 0x0c100000) == 0x04100000) {
unsigned long val;
if (instr & 0x00400000)
val = 255;
else
val = -1;
regs->uregs[reg] = val;
regs->ARM_pc += 4;
return 0;
}
if ((instr & 0x0e100090) == 0x00100090) {
regs->uregs[reg] = -1;
regs->ARM_pc += 4;
return 0;
}
return 1;
}
#endif
static void imx6_pcie_detach_pd(struct device *dev)
{
struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
if (imx6_pcie->pd_hsio_link && !IS_ERR(imx6_pcie->pd_hsio_link))
device_link_del(imx6_pcie->pd_hsio_link);
if (imx6_pcie->pd_hsio_gpio && !IS_ERR(imx6_pcie->pd_hsio_gpio))
dev_pm_domain_detach(imx6_pcie->pd_hsio_gpio, true);
if (imx6_pcie->pd_phy_link && !IS_ERR(imx6_pcie->pd_phy_link))
device_link_del(imx6_pcie->pd_phy_link);
if (imx6_pcie->pd_pcie_phy && !IS_ERR(imx6_pcie->pd_pcie_phy))
dev_pm_domain_detach(imx6_pcie->pd_pcie_phy, true);
if (imx6_pcie->pd_per_link && !IS_ERR(imx6_pcie->pd_per_link))
device_link_del(imx6_pcie->pd_per_link);
if (imx6_pcie->pd_pcie_per && !IS_ERR(imx6_pcie->pd_pcie_per))
dev_pm_domain_detach(imx6_pcie->pd_pcie_per, true);
if (imx6_pcie->pd_link && !IS_ERR(imx6_pcie->pd_link))
device_link_del(imx6_pcie->pd_link);
if (imx6_pcie->pd_pcie && !IS_ERR(imx6_pcie->pd_pcie))
dev_pm_domain_detach(imx6_pcie->pd_pcie, true);
imx6_pcie->pd_hsio_gpio = NULL;
imx6_pcie->pd_hsio_link = NULL;
imx6_pcie->pd_pcie_phy = NULL;
imx6_pcie->pd_phy_link = NULL;
imx6_pcie->pd_pcie_per = NULL;
imx6_pcie->pd_per_link = NULL;
imx6_pcie->pd_pcie = NULL;
imx6_pcie->pd_link = NULL;
}
static int imx6_pcie_attach_pd(struct device *dev)
{
int ret = 0;
struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
struct device_link *link;
struct device *pd_dev;
/* Do nothing when in a single power domain */
if (dev->pm_domain)
return 0;
imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
if (IS_ERR(imx6_pcie->pd_pcie))
return PTR_ERR(imx6_pcie->pd_pcie);
/* Do nothing when power domain missing */
if (!imx6_pcie->pd_pcie)
return 0;
link = device_link_add(dev, imx6_pcie->pd_pcie,
DL_FLAG_STATELESS |
DL_FLAG_PM_RUNTIME |
DL_FLAG_RPM_ACTIVE);
if (!link) {
dev_err(dev, "Failed to add device_link to pcie pd.\n");
return -EINVAL;
} else {
imx6_pcie->pd_link = link;
}
imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
if (IS_ERR(imx6_pcie->pd_pcie_phy)) {
ret = PTR_ERR(imx6_pcie->pd_pcie_phy);
goto err_ret;
}
link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
DL_FLAG_STATELESS |
DL_FLAG_PM_RUNTIME |
DL_FLAG_RPM_ACTIVE);
if (!link) {
dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
ret = -EINVAL;
goto err_ret;
} else {
imx6_pcie->pd_phy_link = link;
}
switch (imx6_pcie->drvdata->variant) {
case IMX8QM:
case IMX8QM_EP:
/*
* PCIA CSR would be touched during the initialization of the
* PCIEB of 8QM.
* Enable the PCIEA PD for this case here.
*/
if (imx6_pcie->controller_id) {
pd_dev = dev_pm_domain_attach_by_name(dev, "pcie_per");
if (IS_ERR(pd_dev)) {
ret = PTR_ERR(pd_dev);
goto err_ret;
} else {
imx6_pcie->pd_pcie_per = pd_dev;
}
link = device_link_add(dev, imx6_pcie->pd_pcie_per,
DL_FLAG_STATELESS |
DL_FLAG_PM_RUNTIME |
DL_FLAG_RPM_ACTIVE);
if (!link) {
dev_err(dev, "Failed to link pcie_per pd\n");
ret = -EINVAL;
goto err_ret;
} else {
imx6_pcie->pd_per_link = link;
}
}
/* fall through */
case IMX8QXP:
case IMX8QXP_EP:
pd_dev = dev_pm_domain_attach_by_name(dev, "hsio_gpio");
if (IS_ERR(pd_dev)) {
ret = PTR_ERR(pd_dev);
goto err_ret;
} else {
imx6_pcie->pd_hsio_gpio = pd_dev;
}
link = device_link_add(dev, imx6_pcie->pd_hsio_gpio,
DL_FLAG_STATELESS |
DL_FLAG_PM_RUNTIME |
DL_FLAG_RPM_ACTIVE);
if (!link) {
dev_err(dev, "Failed to add device_link to hsio_gpio pd.\n");
ret = -EINVAL;
goto err_ret;
} else {
imx6_pcie->pd_hsio_link = link;
}
break;
default:
break;
}
return 0;
err_ret:
imx6_pcie_detach_pd(dev);
return ret;
}
static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
{
return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
}
static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
{
struct dw_pcie *pci = imx6_pcie->pci;
struct device *dev = pci->dev;
unsigned int offset;
int ret = 0;
switch (imx6_pcie->drvdata->variant) {
case IMX6SX:
case IMX6SX_EP:
ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
if (ret) {
dev_err(dev, "unable to enable pcie_axi clock\n");
break;
}
regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
break;
case IMX6QP: /* FALLTHROUGH */
case IMX6QP_EP:
case IMX6Q:
case IMX6Q_EP:
/* power up core phy and enable ref clock */
regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
/*
* the async reset input need ref clock to sync internally,
* when the ref clock comes after reset, internal synced
* reset time is too short, cannot meet the requirement.
* add one ~10us delay here.
*/
usleep_range(10, 100);
regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
break;
case IMX7D:
case IMX7D_EP:
break;
case IMX8MQ:
case IMX8MM:
case IMX8MP:
case IMX8MQ_EP:
case IMX8MM_EP:
case IMX8MP_EP:
ret = clk_prepare_enable(imx6_pcie->pcie_aux);
if (ret) {
dev_err(dev, "unable to enable pcie_aux clock\n");
break;
}
offset = imx6_pcie_grp_offset(imx6_pcie);
/*
* Set the over ride low and enabled
* make sure that REF_CLK is turned on.
*/
regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
0);
regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
break;
case IMX8QXP:
case IMX8QXP_EP:
case IMX8QM:
case IMX8QM_EP:
ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
if (ret) {
dev_err(dev, "unable to enable pcie_axi clock\n");
return ret;
}
ret = clk_prepare_enable(imx6_pcie->pcie_per);
if (ret) {
dev_err(dev, "unable to enable pcie_per clock\n");
goto err_pcie_per;
}
ret = clk_prepare_enable(imx6_pcie->phy_per);
if (unlikely(ret)) {
dev_err(dev, "unable to enable phy per clock\n");
goto err_phy_per;
}
ret = clk_prepare_enable(imx6_pcie->misc_per);
if (unlikely(ret)) {
dev_err(dev, "unable to enable misc per clock\n");
goto err_misc_per;
}
/*
* PCIA CSR would be touched during the initialization of the
* PCIEB of 8QM.
* Enable the PCIEA peripheral clock for this case here.
*/
if (imx6_pcie->drvdata->variant == IMX8QM
&& imx6_pcie->controller_id == 1) {
ret = clk_prepare_enable(imx6_pcie->pcie_phy_pclk);
if (unlikely(ret)) {
dev_err(dev, "can't enable pciephyp clock\n");
goto err_pcie_phy_pclk;
}
ret = clk_prepare_enable(imx6_pcie->pciex2_per);
if (unlikely(ret)) {
dev_err(dev, "can't enable pciex2 per clock\n");
goto err_pciex2_per;
}
}
break;
default:
break;
}
return ret;
err_pciex2_per:
clk_disable_unprepare(imx6_pcie->pcie_phy_pclk);
err_pcie_phy_pclk:
clk_disable_unprepare(imx6_pcie->misc_per);
err_misc_per:
clk_disable_unprepare(imx6_pcie->phy_per);
err_phy_per:
clk_disable_unprepare(imx6_pcie->pcie_per);
err_pcie_per:
clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
return ret;
}
static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
{
u32 val;
struct device *dev = imx6_pcie->pci->dev;
if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
IOMUXC_GPR22, val,
val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
PHY_PLL_LOCK_WAIT_USLEEP_MAX,
PHY_PLL_LOCK_WAIT_TIMEOUT))
dev_err(dev, "PCIe PLL lock timeout\n");
}
static void imx8_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
{
u32 val, retries = 0, tmp = 0, orig = 0;
struct dw_pcie *pci = imx6_pcie->pci;
struct device *dev = pci->dev;
switch (imx6_pcie->drvdata->variant) {
case IMX8MP:
case IMX8MP_EP:
if (phy_init(imx6_pcie->phy) != 0)
dev_err(dev, "Waiting for PHY PLL ready timeout!\n");
/* wait for core_clk enabled */
for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES;
retries++) {
tmp = readl(imx6_pcie->hsmix_base + IMX8MP_GPR_REG1);
if (tmp & IMX8MP_GPR_REG1_PM_EN_CORE_CLK)
break;
udelay(10);
}
break;
case IMX8MM:
case IMX8MM_EP:
for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES;
retries++) {
tmp = readl(imx6_pcie->phy_base + PCIE_PHY_CMN_REG75);
if (tmp == PCIE_PHY_CMN_REG75_PLL_DONE)
break;
udelay(10);
}
break;
case IMX8QXP:
case IMX8QXP_EP:
case IMX8QM:
case IMX8QM_EP:
for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES;
retries++) {
if (imx6_pcie->hsio_cfg == PCIEAX1PCIEBX1SATA) {
regmap_read(imx6_pcie->iomuxc_gpr,
IMX8QM_CSR_PHYX2_OFFSET + 0x4,
&tmp);
if (imx6_pcie->controller_id == 0) /* pciea 1 lanes */
orig = IMX8QM_STTS0_LANE0_TX_PLL_LOCK;
else /* pcieb 1 lanes */
orig = IMX8QM_STTS0_LANE1_TX_PLL_LOCK;
} else if (imx6_pcie->hsio_cfg == PCIEAX2PCIEBX1) {
val = IMX8QM_CSR_PHYX2_OFFSET
+ imx6_pcie->controller_id * SZ_64K;
regmap_read(imx6_pcie->iomuxc_gpr,
val + IMX8QM_CSR_PHYX_STTS0_OFFSET,
&tmp);
orig = IMX8QM_STTS0_LANE0_TX_PLL_LOCK;
if (imx6_pcie->controller_id == 0) /* pciea 2 lanes */
orig |= IMX8QM_STTS0_LANE1_TX_PLL_LOCK;
} else if (imx6_pcie->hsio_cfg == PCIEAX2SATA) {
regmap_read(imx6_pcie->iomuxc_gpr,
IMX8QM_CSR_PHYX2_OFFSET + 0x4,
&tmp);
orig = IMX8QM_STTS0_LANE0_TX_PLL_LOCK;
orig |= IMX8QM_STTS0_LANE1_TX_PLL_LOCK;
}
tmp &= orig;
if (tmp == orig)
break;
udelay(10);
}
break;
default:
break;
}
if (retries >= PHY_PLL_LOCK_WAIT_MAX_RETRIES)
dev_err(dev, "PCIe PLL lock timeout\n");
else
dev_info(dev, "PCIe PLL locked after %d us.\n", retries * 10);
}
static void imx6_pcie_clk_enable(struct imx6_pcie *imx6_pcie)
{
int ret;
struct dw_pcie *pci = imx6_pcie->pci;
struct device *dev = pci->dev;
ret = clk_prepare_enable(imx6_pcie->pcie_phy);
if (ret)
dev_err(dev, "unable to enable pcie_phy clock\n");
ret = clk_prepare_enable(imx6_pcie->pcie_bus);
if (ret)
dev_err(dev, "unable to enable pcie_bus clock\n");
ret = clk_prepare_enable(imx6_pcie->pcie);
if (ret)
dev_err(dev, "unable to enable pcie clock\n");
ret = imx6_pcie_enable_ref_clk(imx6_pcie);
if (ret)
dev_err(dev, "unable to enable pcie ref clock\n");
/* allow the clocks to stabilize */
usleep_range(200, 500);
}
static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
{
clk_disable_unprepare(imx6_pcie->pcie);
clk_disable_unprepare(imx6_pcie->pcie_phy);
clk_disable_unprepare(imx6_pcie->pcie_bus);
switch (imx6_pcie->drvdata->variant) {
case IMX6Q:
case IMX6Q_EP:
case IMX6QP:
case IMX6QP_EP:
regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
IMX6Q_GPR1_PCIE_REF_CLK_EN, 0);
regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
IMX6Q_GPR1_PCIE_TEST_PD,
IMX6Q_GPR1_PCIE_TEST_PD);
break;
case IMX6SX:
case IMX6SX_EP:
clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
break;
case IMX7D:
case IMX7D_EP:
regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
break;
case IMX8MP:
case IMX8MP_EP:
phy_exit(imx6_pcie->phy);
phy_power_off(imx6_pcie->phy);
/* fall through */
case IMX8MQ:
case IMX8MM:
case IMX8MQ_EP:
case IMX8MM_EP:
clk_disable_unprepare(imx6_pcie->pcie_aux);
break;
case IMX8QM:
case IMX8QM_EP:
if (imx6_pcie->controller_id == 1) {
clk_disable_unprepare(imx6_pcie->pciex2_per);
clk_disable_unprepare(imx6_pcie->pcie_phy_pclk);