1 // SPDX-License-Identifier: GPL-2.0+
6 #include <linux/bitfield.h>
8 #include <linux/delay.h>
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
13 #include <linux/pci_regs.h>
14 #include <linux/phy/phy.h>
15 #include <linux/phy/pcie.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 #include <dt-bindings/phy/phy.h>
20 #include <dt-bindings/phy/phy-imx8-pcie.h>
22 #define MAX_NUM_LANE 3
23 #define LANE_NUM_CLKS 5
25 /* Parameters for the waiting for PCIe PHY PLL to lock */
26 #define PHY_INIT_WAIT_USLEEP_MAX 10
27 #define PHY_INIT_WAIT_TIMEOUT (1000 * PHY_INIT_WAIT_USLEEP_MAX)
29 /* i.MX8Q HSIO registers */
30 #define HSIO_CTRL0 0x0
31 #define HSIO_APB_RSTN_0 BIT(0)
32 #define HSIO_APB_RSTN_1 BIT(1)
33 #define HSIO_PIPE_RSTN_0_MASK GENMASK(25, 24)
34 #define HSIO_PIPE_RSTN_1_MASK GENMASK(27, 26)
35 #define HSIO_MODE_MASK GENMASK(20, 17)
36 #define HSIO_MODE_PCIE 0x0
37 #define HSIO_MODE_SATA 0x4
38 #define HSIO_DEVICE_TYPE_MASK GENMASK(27, 24)
39 #define HSIO_EPCS_TXDEEMP BIT(5)
40 #define HSIO_EPCS_TXDEEMP_SEL BIT(6)
41 #define HSIO_EPCS_PHYRESET_N BIT(7)
42 #define HSIO_RESET_N BIT(12)
44 #define HSIO_IOB_RXENA BIT(0)
45 #define HSIO_IOB_TXENA BIT(1)
46 #define HSIO_IOB_A_0_TXOE BIT(2)
47 #define HSIO_IOB_A_0_M1M0_2 BIT(4)
48 #define HSIO_IOB_A_0_M1M0_MASK GENMASK(4, 3)
49 #define HSIO_PHYX1_EPCS_SEL BIT(12)
50 #define HSIO_PCIE_AB_SELECT BIT(13)
52 #define HSIO_PHY_STS0 0x4
53 #define HSIO_LANE0_TX_PLL_LOCK BIT(4)
54 #define HSIO_LANE1_TX_PLL_LOCK BIT(12)
56 #define HSIO_CTRL2 0x8
57 #define HSIO_LTSSM_ENABLE BIT(4)
58 #define HSIO_BUTTON_RST_N BIT(21)
59 #define HSIO_PERST_N BIT(22)
60 #define HSIO_POWER_UP_RST_N BIT(23)
62 #define HSIO_PCIE_STS0 0xc
63 #define HSIO_PM_REQ_CORE_RST BIT(19)
65 #define HSIO_REG48_PMA_STATUS 0x30
66 #define HSIO_REG48_PMA_RDY BIT(7)
68 struct imx_hsio_drvdata
{
72 struct imx_hsio_lane
{
78 const char * const *clk_names
;
79 struct clk_bulk_data clks
[LANE_NUM_CLKS
];
80 struct imx_hsio_priv
*priv
;
82 enum phy_mode phy_mode
;
85 struct imx_hsio_priv
{
90 const char *refclk_pad
;
95 const struct imx_hsio_drvdata
*drvdata
;
96 struct imx_hsio_lane lane
[MAX_NUM_LANE
];
99 static const char * const lan0_pcie_clks
[] = {"apb_pclk0", "pclk0", "ctl0_crr",
100 "phy0_crr", "misc_crr"};
101 static const char * const lan1_pciea_clks
[] = {"apb_pclk1", "pclk1", "ctl0_crr",
102 "phy0_crr", "misc_crr"};
103 static const char * const lan1_pcieb_clks
[] = {"apb_pclk1", "pclk1", "ctl1_crr",
104 "phy0_crr", "misc_crr"};
105 static const char * const lan2_pcieb_clks
[] = {"apb_pclk2", "pclk2", "ctl1_crr",
106 "phy1_crr", "misc_crr"};
107 static const char * const lan2_sata_clks
[] = {"pclk2", "epcs_tx", "epcs_rx",
108 "phy1_crr", "misc_crr"};
110 static const struct regmap_config regmap_config
= {
116 static int imx_hsio_init(struct phy
*phy
)
119 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
120 struct imx_hsio_priv
*priv
= lane
->priv
;
121 struct device
*dev
= priv
->dev
;
123 /* Assign clocks refer to different modes */
124 switch (lane
->phy_type
) {
126 lane
->phy_mode
= PHY_MODE_PCIE
;
127 if (lane
->ctrl_index
== 0) { /* PCIEA */
131 for (i
= 0; i
< LANE_NUM_CLKS
; i
++) {
133 lane
->clks
[i
].id
= lan0_pcie_clks
[i
];
135 lane
->clks
[i
].id
= lan1_pciea_clks
[i
];
138 if (lane
->idx
== 0) { /* i.MX8QXP */
143 * On i.MX8QM, only second or third lane can be
146 lane
->ctrl_off
= SZ_64K
;
149 else /* the third lane is bound to PCIEB */
150 lane
->phy_off
= SZ_64K
;
153 for (i
= 0; i
< LANE_NUM_CLKS
; i
++) {
155 lane
->clks
[i
].id
= lan1_pcieb_clks
[i
];
156 else if (lane
->idx
== 2)
157 lane
->clks
[i
].id
= lan2_pcieb_clks
[i
];
158 else /* i.MX8QXP only has PCIEB, idx is 0 */
159 lane
->clks
[i
].id
= lan0_pcie_clks
[i
];
164 /* On i.MX8QM, only the third lane can be bound to SATA */
165 lane
->phy_mode
= PHY_MODE_SATA
;
166 lane
->ctrl_off
= SZ_128K
;
167 lane
->phy_off
= SZ_64K
;
169 for (i
= 0; i
< LANE_NUM_CLKS
; i
++)
170 lane
->clks
[i
].id
= lan2_sata_clks
[i
];
176 /* Fetch clocks and enable them */
177 ret
= devm_clk_bulk_get(dev
, LANE_NUM_CLKS
, lane
->clks
);
180 ret
= clk_bulk_prepare_enable(LANE_NUM_CLKS
, lane
->clks
);
184 /* allow the clocks to stabilize */
185 usleep_range(200, 500);
189 static int imx_hsio_exit(struct phy
*phy
)
191 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
193 clk_bulk_disable_unprepare(LANE_NUM_CLKS
, lane
->clks
);
198 static void imx_hsio_pcie_phy_resets(struct phy
*phy
)
200 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
201 struct imx_hsio_priv
*priv
= lane
->priv
;
203 regmap_clear_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
205 regmap_clear_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
207 regmap_clear_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
208 HSIO_POWER_UP_RST_N
);
209 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
211 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
213 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
214 HSIO_POWER_UP_RST_N
);
216 if (lane
->idx
== 1) {
217 regmap_set_bits(priv
->phy
, lane
->phy_off
+ HSIO_CTRL0
,
219 regmap_set_bits(priv
->phy
, lane
->phy_off
+ HSIO_CTRL0
,
220 HSIO_PIPE_RSTN_1_MASK
);
222 regmap_set_bits(priv
->phy
, lane
->phy_off
+ HSIO_CTRL0
,
224 regmap_set_bits(priv
->phy
, lane
->phy_off
+ HSIO_CTRL0
,
225 HSIO_PIPE_RSTN_0_MASK
);
229 static void imx_hsio_sata_phy_resets(struct phy
*phy
)
231 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
232 struct imx_hsio_priv
*priv
= lane
->priv
;
234 /* clear PHY RST, then set it */
235 regmap_clear_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
,
236 HSIO_EPCS_PHYRESET_N
);
237 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
,
238 HSIO_EPCS_PHYRESET_N
);
240 /* CTRL RST: SET -> delay 1 us -> CLEAR -> SET */
241 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
, HSIO_RESET_N
);
243 regmap_clear_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
,
245 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
, HSIO_RESET_N
);
248 static void imx_hsio_configure_clk_pad(struct phy
*phy
)
251 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
252 struct imx_hsio_priv
*priv
= lane
->priv
;
254 if (strncmp(priv
->refclk_pad
, "output", 6) == 0) {
256 regmap_update_bits(priv
->misc
, HSIO_CTRL0
,
257 HSIO_IOB_A_0_TXOE
| HSIO_IOB_A_0_M1M0_MASK
,
258 HSIO_IOB_A_0_TXOE
| HSIO_IOB_A_0_M1M0_2
);
260 regmap_update_bits(priv
->misc
, HSIO_CTRL0
,
261 HSIO_IOB_A_0_TXOE
| HSIO_IOB_A_0_M1M0_MASK
,
265 regmap_update_bits(priv
->misc
, HSIO_CTRL0
, HSIO_IOB_RXENA
,
266 pll
? 0 : HSIO_IOB_RXENA
);
267 regmap_update_bits(priv
->misc
, HSIO_CTRL0
, HSIO_IOB_TXENA
,
268 pll
? HSIO_IOB_TXENA
: 0);
271 static void imx_hsio_pre_set(struct phy
*phy
)
273 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
274 struct imx_hsio_priv
*priv
= lane
->priv
;
276 if (strncmp(priv
->hsio_cfg
, "pciea-x2-pcieb", 14) == 0) {
277 regmap_set_bits(priv
->misc
, HSIO_CTRL0
, HSIO_PCIE_AB_SELECT
);
278 } else if (strncmp(priv
->hsio_cfg
, "pciea-x2-sata", 13) == 0) {
279 regmap_set_bits(priv
->misc
, HSIO_CTRL0
, HSIO_PHYX1_EPCS_SEL
);
280 } else if (strncmp(priv
->hsio_cfg
, "pciea-pcieb-sata", 16) == 0) {
281 regmap_set_bits(priv
->misc
, HSIO_CTRL0
, HSIO_PCIE_AB_SELECT
);
282 regmap_set_bits(priv
->misc
, HSIO_CTRL0
, HSIO_PHYX1_EPCS_SEL
);
285 imx_hsio_configure_clk_pad(phy
);
288 static int imx_hsio_pcie_power_on(struct phy
*phy
)
292 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
293 struct imx_hsio_priv
*priv
= lane
->priv
;
295 imx_hsio_pcie_phy_resets(phy
);
297 /* Toggle apb_pclk to make sure PM_REQ_CORE_RST is cleared. */
298 clk_disable_unprepare(lane
->clks
[0].clk
);
300 ret
= clk_prepare_enable(lane
->clks
[0].clk
);
302 dev_err(priv
->dev
, "unable to enable phy apb_pclk\n");
306 addr
= lane
->ctrl_off
+ HSIO_PCIE_STS0
;
307 cond
= HSIO_PM_REQ_CORE_RST
;
308 ret
= regmap_read_poll_timeout(priv
->ctrl
, addr
, val
,
310 PHY_INIT_WAIT_USLEEP_MAX
,
311 PHY_INIT_WAIT_TIMEOUT
);
313 dev_err(priv
->dev
, "HSIO_PM_REQ_CORE_RST is set\n");
317 static int imx_hsio_sata_power_on(struct phy
*phy
)
321 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
322 struct imx_hsio_priv
*priv
= lane
->priv
;
324 regmap_set_bits(priv
->phy
, lane
->phy_off
+ HSIO_CTRL0
, HSIO_APB_RSTN_0
);
325 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
,
327 regmap_set_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
,
328 HSIO_EPCS_TXDEEMP_SEL
);
330 imx_hsio_sata_phy_resets(phy
);
332 cond
= HSIO_REG48_PMA_RDY
;
333 ret
= read_poll_timeout(readb
, val
, ((val
& cond
) == cond
),
334 PHY_INIT_WAIT_USLEEP_MAX
,
335 PHY_INIT_WAIT_TIMEOUT
, false,
336 priv
->base
+ HSIO_REG48_PMA_STATUS
);
338 dev_err(priv
->dev
, "PHY calibration is timeout\n");
340 dev_dbg(priv
->dev
, "PHY calibration is done\n");
345 static int imx_hsio_power_on(struct phy
*phy
)
349 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
350 struct imx_hsio_priv
*priv
= lane
->priv
;
352 scoped_guard(mutex
, &priv
->lock
) {
354 imx_hsio_pre_set(phy
);
358 if (lane
->phy_mode
== PHY_MODE_PCIE
)
359 ret
= imx_hsio_pcie_power_on(phy
);
361 ret
= imx_hsio_sata_power_on(phy
);
365 /* Polling to check the PHY is ready or not. */
367 cond
= HSIO_LANE1_TX_PLL_LOCK
;
370 * Except the phy_off, the bit-offset of lane2 is same to lane0.
371 * Merge the lane0 and lane2 bit-operations together.
373 cond
= HSIO_LANE0_TX_PLL_LOCK
;
375 ret
= regmap_read_poll_timeout(priv
->phy
, lane
->phy_off
+ HSIO_PHY_STS0
,
376 val
, ((val
& cond
) == cond
),
377 PHY_INIT_WAIT_USLEEP_MAX
,
378 PHY_INIT_WAIT_TIMEOUT
);
380 dev_err(priv
->dev
, "IMX8Q PHY%d PLL lock timeout\n", lane
->idx
);
383 dev_dbg(priv
->dev
, "IMX8Q PHY%d PLL is locked\n", lane
->idx
);
388 static int imx_hsio_power_off(struct phy
*phy
)
390 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
391 struct imx_hsio_priv
*priv
= lane
->priv
;
393 scoped_guard(mutex
, &priv
->lock
) {
395 if (priv
->open_cnt
== 0) {
396 regmap_clear_bits(priv
->misc
, HSIO_CTRL0
,
397 HSIO_PCIE_AB_SELECT
);
398 regmap_clear_bits(priv
->misc
, HSIO_CTRL0
,
399 HSIO_PHYX1_EPCS_SEL
);
401 if (lane
->phy_mode
== PHY_MODE_PCIE
) {
402 regmap_clear_bits(priv
->ctrl
,
403 lane
->ctrl_off
+ HSIO_CTRL2
,
405 regmap_clear_bits(priv
->ctrl
,
406 lane
->ctrl_off
+ HSIO_CTRL2
,
408 regmap_clear_bits(priv
->ctrl
,
409 lane
->ctrl_off
+ HSIO_CTRL2
,
410 HSIO_POWER_UP_RST_N
);
412 regmap_clear_bits(priv
->ctrl
,
413 lane
->ctrl_off
+ HSIO_CTRL0
,
415 regmap_clear_bits(priv
->ctrl
,
416 lane
->ctrl_off
+ HSIO_CTRL0
,
417 HSIO_EPCS_TXDEEMP_SEL
);
418 regmap_clear_bits(priv
->ctrl
,
419 lane
->ctrl_off
+ HSIO_CTRL0
,
423 if (lane
->idx
== 1) {
424 regmap_clear_bits(priv
->phy
,
425 lane
->phy_off
+ HSIO_CTRL0
,
427 regmap_clear_bits(priv
->phy
,
428 lane
->phy_off
+ HSIO_CTRL0
,
429 HSIO_PIPE_RSTN_1_MASK
);
432 * Except the phy_off, the bit-offset of lane2 is same
433 * to lane0. Merge the lane0 and lane2 bit-operations
436 regmap_clear_bits(priv
->phy
,
437 lane
->phy_off
+ HSIO_CTRL0
,
439 regmap_clear_bits(priv
->phy
,
440 lane
->phy_off
+ HSIO_CTRL0
,
441 HSIO_PIPE_RSTN_0_MASK
);
449 static int imx_hsio_set_mode(struct phy
*phy
, enum phy_mode mode
,
453 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
454 struct imx_hsio_priv
*priv
= lane
->priv
;
456 if (lane
->phy_mode
!= mode
)
459 val
= (mode
== PHY_MODE_PCIE
) ? HSIO_MODE_PCIE
: HSIO_MODE_SATA
;
460 val
= FIELD_PREP(HSIO_MODE_MASK
, val
);
461 regmap_update_bits(priv
->phy
, lane
->phy_off
+ HSIO_CTRL0
,
462 HSIO_MODE_MASK
, val
);
465 case PHY_MODE_PCIE_RC
:
466 val
= FIELD_PREP(HSIO_DEVICE_TYPE_MASK
, PCI_EXP_TYPE_ROOT_PORT
);
468 case PHY_MODE_PCIE_EP
:
469 val
= FIELD_PREP(HSIO_DEVICE_TYPE_MASK
, PCI_EXP_TYPE_ENDPOINT
);
471 default: /* Support only PCIe EP and RC now. */
475 regmap_update_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL0
,
476 HSIO_DEVICE_TYPE_MASK
, val
);
481 static int imx_hsio_set_speed(struct phy
*phy
, int speed
)
483 struct imx_hsio_lane
*lane
= phy_get_drvdata(phy
);
484 struct imx_hsio_priv
*priv
= lane
->priv
;
486 regmap_update_bits(priv
->ctrl
, lane
->ctrl_off
+ HSIO_CTRL2
,
488 speed
? HSIO_LTSSM_ENABLE
: 0);
492 static const struct phy_ops imx_hsio_ops
= {
493 .init
= imx_hsio_init
,
494 .exit
= imx_hsio_exit
,
495 .power_on
= imx_hsio_power_on
,
496 .power_off
= imx_hsio_power_off
,
497 .set_mode
= imx_hsio_set_mode
,
498 .set_speed
= imx_hsio_set_speed
,
499 .owner
= THIS_MODULE
,
502 static const struct imx_hsio_drvdata imx8qxp_hsio_drvdata
= {
506 static const struct imx_hsio_drvdata imx8qm_hsio_drvdata
= {
510 static const struct of_device_id imx_hsio_of_match
[] = {
511 {.compatible
= "fsl,imx8qm-hsio", .data
= &imx8qm_hsio_drvdata
},
512 {.compatible
= "fsl,imx8qxp-hsio", .data
= &imx8qxp_hsio_drvdata
},
515 MODULE_DEVICE_TABLE(of
, imx_hsio_of_match
);
517 static struct phy
*imx_hsio_xlate(struct device
*dev
,
518 const struct of_phandle_args
*args
)
520 struct imx_hsio_priv
*priv
= dev_get_drvdata(dev
);
521 int idx
= args
->args
[0];
522 int phy_type
= args
->args
[1];
523 int ctrl_index
= args
->args
[2];
525 if (idx
< 0 || idx
>= priv
->drvdata
->lane_num
)
526 return ERR_PTR(-EINVAL
);
527 priv
->lane
[idx
].idx
= idx
;
528 priv
->lane
[idx
].phy_type
= phy_type
;
529 priv
->lane
[idx
].ctrl_index
= ctrl_index
;
531 return priv
->lane
[idx
].phy
;
534 static int imx_hsio_probe(struct platform_device
*pdev
)
538 struct device
*dev
= &pdev
->dev
;
539 struct device_node
*np
= dev
->of_node
;
540 struct imx_hsio_priv
*priv
;
541 struct phy_provider
*provider
;
543 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
546 priv
->dev
= &pdev
->dev
;
547 priv
->drvdata
= of_device_get_match_data(dev
);
549 /* Get HSIO configuration mode */
550 if (of_property_read_string(np
, "fsl,hsio-cfg", &priv
->hsio_cfg
))
551 priv
->hsio_cfg
= "pciea-pcieb-sata";
552 /* Get PHY refclk pad mode */
553 if (of_property_read_string(np
, "fsl,refclk-pad-mode",
555 priv
->refclk_pad
= NULL
;
557 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
558 if (IS_ERR(priv
->base
))
559 return PTR_ERR(priv
->base
);
561 off
= devm_platform_ioremap_resource_byname(pdev
, "phy");
562 priv
->phy
= devm_regmap_init_mmio(dev
, off
, ®map_config
);
563 if (IS_ERR(priv
->phy
))
564 return dev_err_probe(dev
, PTR_ERR(priv
->phy
),
565 "unable to find phy csr registers\n");
567 off
= devm_platform_ioremap_resource_byname(pdev
, "ctrl");
568 priv
->ctrl
= devm_regmap_init_mmio(dev
, off
, ®map_config
);
569 if (IS_ERR(priv
->ctrl
))
570 return dev_err_probe(dev
, PTR_ERR(priv
->ctrl
),
571 "unable to find ctrl csr registers\n");
573 off
= devm_platform_ioremap_resource_byname(pdev
, "misc");
574 priv
->misc
= devm_regmap_init_mmio(dev
, off
, ®map_config
);
575 if (IS_ERR(priv
->misc
))
576 return dev_err_probe(dev
, PTR_ERR(priv
->misc
),
577 "unable to find misc csr registers\n");
579 for (i
= 0; i
< priv
->drvdata
->lane_num
; i
++) {
580 struct imx_hsio_lane
*lane
= &priv
->lane
[i
];
583 phy
= devm_phy_create(&pdev
->dev
, NULL
, &imx_hsio_ops
);
590 phy_set_drvdata(phy
, lane
);
593 dev_set_drvdata(dev
, priv
);
594 dev_set_drvdata(&pdev
->dev
, priv
);
596 provider
= devm_of_phy_provider_register(&pdev
->dev
, imx_hsio_xlate
);
598 return PTR_ERR_OR_ZERO(provider
);
601 static struct platform_driver imx_hsio_driver
= {
602 .probe
= imx_hsio_probe
,
604 .name
= "imx8qm-hsio-phy",
605 .of_match_table
= imx_hsio_of_match
,
608 module_platform_driver(imx_hsio_driver
);
610 MODULE_DESCRIPTION("FSL IMX8QM HSIO SERDES PHY driver");
611 MODULE_LICENSE("GPL");