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
|
// SPDX-License-Identifier: GPL-2.0
/*
* SpacemiT K1 PCIe and PCIe/USB 3 combo PHY driver
*
* Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reserved.
*/
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/phy/phy.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <dt-bindings/phy/phy.h>
/*
* Three PCIe ports are supported in the SpacemiT K1 SoC, and this driver
* supports their PHYs.
*
* The PHY for PCIe port A is different from the PHYs for ports B and C:
* - It has one lane, while ports B and C have two
* - It is a combo PHY can be used for PCIe or USB 3
* - It can automatically calibrate PCIe TX and RX termination settings
*
* The PHY functionality for PCIe ports B and C is identical:
* - They have two PCIe lanes (but can be restricted to 1 via device tree)
* - They are used for PCIe only
* - They are configured using TX and RX values computed for port A
*
* A given board is designed to use the combo PHY for either PCIe or USB 3.
* Whether the combo PHY is configured for PCIe or USB 3 is specified in
* device tree using a phandle plus an argument. The argument indicates
* the type (either PHY_TYPE_PCIE or PHY_TYPE_USB3).
*
* Each PHY has a reset that it gets and deasserts during initialization.
* Each depends also on other clocks and resets provided by the controller
* hardware (PCIe or USB) it is associated with. The controller drivers
* are required to enable any clocks and de-assert any resets that affect
* PHY operation. In addition each PHY implements an internal PLL, driven
* by an external (24 MHz) oscillator.
*
* PCIe PHYs must be programmed with RX and TX calibration values. The
* combo PHY is the only one that can determine these values. They are
* determined by temporarily enabling the combo PHY in PCIe mode at probe
* time (if necessary). This calibration only needs to be done once, and
* when it has completed the TX and RX values are saved.
*
* To allow the combo PHY to be enabled for calibration, the resets and
* clocks it uses in PCIe mode must be supplied.
*/
struct k1_pcie_phy {
struct device *dev; /* PHY provider device */
struct phy *phy;
void __iomem *regs;
u32 pcie_lanes; /* Max (1 or 2) unless limited by DT */
struct clk *pll;
struct clk_hw pll_hw; /* Private PLL clock */
/* The remaining fields are only used for the combo PHY */
u32 type; /* PHY_TYPE_PCIE or PHY_TYPE_USB3 */
struct regmap *pmu; /* MMIO regmap (no errors) */
};
#define CALIBRATION_TIMEOUT 500000 /* For combo PHY (usec) */
#define PLL_TIMEOUT 500000 /* For PHY PLL lock (usec) */
#define POLL_DELAY 500 /* Time between polls (usec) */
/* Selecting the combo PHY operating mode requires APMU regmap access */
#define SYSCON_APMU "spacemit,apmu"
/* PMU space, for selecting between PCIe and USB 3 mode (combo PHY only) */
#define PMUA_USB_PHY_CTRL0 0x0110
#define COMBO_PHY_SEL BIT(3) /* 0: PCIe; 1: USB 3 */
#define PCIE_CLK_RES_CTRL 0x03cc
#define PCIE_APP_HOLD_PHY_RST BIT(30)
/* PHY register space */
/* Offset between lane 0 and lane 1 registers when there are two */
#define PHY_LANE_OFFSET 0x0400
/* PHY PLL configuration */
#define PCIE_PU_ADDR_CLK_CFG 0x0008
#define PLL_READY BIT(0) /* read-only */
#define CFG_INTERNAL_TIMER_ADJ GENMASK(10, 7)
#define TIMER_ADJ_USB 0x2
#define TIMER_ADJ_PCIE 0x6
#define CFG_SW_PHY_INIT_DONE BIT(11) /* We set after PLL config */
#define PCIE_RC_DONE_STATUS 0x0018
#define CFG_FORCE_RCV_RETRY BIT(10) /* Used for PCIe */
/* PCIe PHY lane calibration; assumes 24MHz input clock */
#define PCIE_RC_CAL_REG2 0x0020
#define RC_CAL_TOGGLE BIT(22)
#define CLKSEL GENMASK(31, 29)
#define CLKSEL_24M 0x3
/* Additional PHY PLL configuration (USB 3 and PCIe) */
#define PCIE_PU_PLL_1 0x0048
#define REF_100_WSSC BIT(12) /* 1: input is 100MHz, SSC */
#define FREF_SEL GENMASK(15, 13)
#define FREF_24M 0x1
#define SSC_DEP_SEL GENMASK(19, 16)
#define SSC_DEP_NONE 0x0
#define SSC_DEP_5000PPM 0xa
/* PCIe PHY configuration */
#define PCIE_PU_PLL_2 0x004c
#define GEN_REF100 BIT(4) /* 1: generate 100MHz clk */
#define PCIE_RX_REG1 0x0050
#define EN_RTERM BIT(3)
#define AFE_RTERM_REG GENMASK(11, 8)
#define PCIE_RX_REG2 0x0054
#define RX_RTERM_SEL BIT(5) /* 0: use AFE_RTERM_REG value */
#define PCIE_LTSSM_DIS_ENTRY 0x005c
#define CFG_REFCLK_MODE GENMASK(9, 8)
#define RFCLK_MODE_DRIVER 0x1
#define OVRD_REFCLK_MODE BIT(10) /* 1: use CFG_RFCLK_MODE */
#define PCIE_TX_REG1 0x0064
#define TX_RTERM_REG GENMASK(15, 12)
#define TX_RTERM_SEL BIT(25) /* 1: use TX_RTERM_REG */
/* Zeroed for the combo PHY operating in USB mode */
#define USB3_TEST_CTRL 0x0068
/* PHY calibration values, determined by the combo PHY at probe time */
#define PCIE_RCAL_RESULT 0x0084 /* Port A PHY only */
#define RTERM_VALUE_RX GENMASK(3, 0)
#define RTERM_VALUE_TX GENMASK(7, 4)
#define R_TUNE_DONE BIT(10)
static u32 k1_phy_rterm = ~0; /* Invalid initial value */
/* Save the RX and TX receiver termination values */
static void k1_phy_rterm_set(u32 val)
{
k1_phy_rterm = val & (RTERM_VALUE_RX | RTERM_VALUE_TX);
}
static bool k1_phy_rterm_valid(void)
{
/* Valid if no bits outside those we care about are set */
return !(k1_phy_rterm & ~(RTERM_VALUE_RX | RTERM_VALUE_TX));
}
static u32 k1_phy_rterm_rx(void)
{
return FIELD_GET(RTERM_VALUE_RX, k1_phy_rterm);
}
static u32 k1_phy_rterm_tx(void)
{
return FIELD_GET(RTERM_VALUE_TX, k1_phy_rterm);
}
/* Only the combo PHY has a PMU pointer defined */
static bool k1_phy_port_a(struct k1_pcie_phy *k1_phy)
{
return !!k1_phy->pmu;
}
/* The PLL clocks are driven by the external oscillator */
static const struct clk_parent_data k1_pcie_phy_data[] = {
{ .fw_name = "refclk", },
};
static struct k1_pcie_phy *clk_hw_to_k1_phy(struct clk_hw *clk_hw)
{
return container_of(clk_hw, struct k1_pcie_phy, pll_hw);
}
/* USB mode only works on the combo PHY, which has only one lane */
static void k1_pcie_phy_pll_prepare_usb(struct k1_pcie_phy *k1_phy)
{
void __iomem *regs = k1_phy->regs;
u32 val;
val = readl(regs + PCIE_PU_ADDR_CLK_CFG);
val &= ~CFG_INTERNAL_TIMER_ADJ;
val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, TIMER_ADJ_USB);
writel(val, regs + PCIE_PU_ADDR_CLK_CFG);
val = readl(regs + PCIE_PU_PLL_1);
val &= ~SSC_DEP_SEL;
val |= FIELD_PREP(SSC_DEP_SEL, SSC_DEP_5000PPM);
writel(val, regs + PCIE_PU_PLL_1);
}
/* Perform PCIe-specific register updates before starting the PLL clock */
static void k1_pcie_phy_pll_prepare_pcie(struct k1_pcie_phy *k1_phy)
{
void __iomem *regs = k1_phy->regs;
u32 val;
u32 i;
for (i = 0; i < k1_phy->pcie_lanes; i++) {
val = readl(regs + PCIE_PU_ADDR_CLK_CFG);
val &= ~CFG_INTERNAL_TIMER_ADJ;
val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, TIMER_ADJ_PCIE);
writel(val, regs + PCIE_PU_ADDR_CLK_CFG);
regs += PHY_LANE_OFFSET; /* Next lane */
}
regs = k1_phy->regs;
val = readl(regs + PCIE_RC_DONE_STATUS);
val |= CFG_FORCE_RCV_RETRY;
writel(val, regs + PCIE_RC_DONE_STATUS);
val = readl(regs + PCIE_PU_PLL_1);
val &= ~SSC_DEP_SEL;
val |= FIELD_PREP(SSC_DEP_SEL, SSC_DEP_NONE);
writel(val, regs + PCIE_PU_PLL_1);
val = readl(regs + PCIE_PU_PLL_2);
val |= GEN_REF100; /* Enable 100 MHz PLL output clock */
writel(val, regs + PCIE_PU_PLL_2);
}
static int k1_pcie_phy_pll_prepare(struct clk_hw *clk_hw)
{
struct k1_pcie_phy *k1_phy = clk_hw_to_k1_phy(clk_hw);
void __iomem *regs = k1_phy->regs;
u32 val;
u32 i;
if (k1_phy_port_a(k1_phy) && k1_phy->type == PHY_TYPE_USB3)
k1_pcie_phy_pll_prepare_usb(k1_phy);
else
k1_pcie_phy_pll_prepare_pcie(k1_phy);
/*
* Disable 100 MHz input reference with spread-spectrum
* clocking and select the 24 MHz clock input frequency
*/
val = readl(regs + PCIE_PU_PLL_1);
val &= ~REF_100_WSSC;
val &= ~FREF_SEL;
val |= FIELD_PREP(FREF_SEL, FREF_24M);
writel(val, regs + PCIE_PU_PLL_1);
/* Mark PLL configuration done on all lanes */
for (i = 0; i < k1_phy->pcie_lanes; i++) {
val = readl(regs + PCIE_PU_ADDR_CLK_CFG);
val |= CFG_SW_PHY_INIT_DONE;
writel(val, regs + PCIE_PU_ADDR_CLK_CFG);
regs += PHY_LANE_OFFSET; /* Next lane */
}
/*
* Wait for indication the PHY PLL is locked. Lanes for ports
* B and C share a PLL, so it's enough to sample just lane 0.
*/
return readl_poll_timeout(k1_phy->regs + PCIE_PU_ADDR_CLK_CFG,
val, val & PLL_READY,
POLL_DELAY, PLL_TIMEOUT);
}
/* Prepare implies enable, and once enabled, it's always on */
static const struct clk_ops k1_pcie_phy_pll_ops = {
.prepare = k1_pcie_phy_pll_prepare,
};
/* We represent the PHY PLL as a private clock */
static int k1_pcie_phy_pll_setup(
|