1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 MediaTek Inc.
4 * Author: Jianjun Wang <jianjun.wang@mediatek.com>
7 #include <linux/bitfield.h>
8 #include <linux/module.h>
9 #include <linux/nvmem-consumer.h>
11 #include <linux/phy/phy.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
15 #include "phy-mtk-io.h"
17 #define PEXTP_ANA_GLB_00_REG 0x9000
18 /* Internal Resistor Selection of TX Bias Current */
19 #define EFUSE_GLB_INTR_SEL GENMASK(28, 24)
21 #define PEXTP_ANA_LN0_TRX_REG 0xa000
23 #define PEXTP_ANA_TX_REG 0x04
24 /* TX PMOS impedance selection */
25 #define EFUSE_LN_TX_PMOS_SEL GENMASK(5, 2)
26 /* TX NMOS impedance selection */
27 #define EFUSE_LN_TX_NMOS_SEL GENMASK(11, 8)
29 #define PEXTP_ANA_RX_REG 0x3c
30 /* RX impedance selection */
31 #define EFUSE_LN_RX_SEL GENMASK(3, 0)
33 #define PEXTP_ANA_LANE_OFFSET 0x100
36 * struct mtk_pcie_lane_efuse - eFuse data for each lane
37 * @tx_pmos: TX PMOS impedance selection data
38 * @tx_nmos: TX NMOS impedance selection data
39 * @rx_data: RX impedance selection data
40 * @lane_efuse_supported: software eFuse data is supported for this lane
42 struct mtk_pcie_lane_efuse
{
46 bool lane_efuse_supported
;
50 * struct mtk_pcie_phy_data - phy data for each SoC
51 * @num_lanes: supported lane numbers
52 * @sw_efuse_supported: support software to load eFuse data
54 struct mtk_pcie_phy_data
{
56 bool sw_efuse_supported
;
60 * struct mtk_pcie_phy - PCIe phy driver main structure
61 * @dev: pointer to device
62 * @phy: pointer to generic phy
63 * @sif_base: IO mapped register base address of system interface
64 * @data: pointer to SoC dependent data
65 * @sw_efuse_en: software eFuse enable status
66 * @efuse_glb_intr: internal resistor selection of TX bias current data
67 * @efuse: pointer to eFuse data for each lane
72 void __iomem
*sif_base
;
73 const struct mtk_pcie_phy_data
*data
;
77 struct mtk_pcie_lane_efuse
*efuse
;
80 static void mtk_pcie_efuse_set_lane(struct mtk_pcie_phy
*pcie_phy
,
83 struct mtk_pcie_lane_efuse
*data
= &pcie_phy
->efuse
[lane
];
86 if (!data
->lane_efuse_supported
)
89 addr
= pcie_phy
->sif_base
+ PEXTP_ANA_LN0_TRX_REG
+
90 lane
* PEXTP_ANA_LANE_OFFSET
;
92 mtk_phy_update_field(addr
+ PEXTP_ANA_TX_REG
, EFUSE_LN_TX_PMOS_SEL
,
95 mtk_phy_update_field(addr
+ PEXTP_ANA_TX_REG
, EFUSE_LN_TX_NMOS_SEL
,
98 mtk_phy_update_field(addr
+ PEXTP_ANA_RX_REG
, EFUSE_LN_RX_SEL
,
103 * mtk_pcie_phy_init() - Initialize the phy
104 * @phy: the phy to be initialized
106 * Initialize the phy by setting the efuse data.
107 * The hardware settings will be reset during suspend, it should be
108 * reinitialized when the consumer calls phy_init() again on resume.
110 static int mtk_pcie_phy_init(struct phy
*phy
)
112 struct mtk_pcie_phy
*pcie_phy
= phy_get_drvdata(phy
);
115 if (!pcie_phy
->sw_efuse_en
)
118 /* Set global data */
119 mtk_phy_update_field(pcie_phy
->sif_base
+ PEXTP_ANA_GLB_00_REG
,
120 EFUSE_GLB_INTR_SEL
, pcie_phy
->efuse_glb_intr
);
122 for (i
= 0; i
< pcie_phy
->data
->num_lanes
; i
++)
123 mtk_pcie_efuse_set_lane(pcie_phy
, i
);
128 static const struct phy_ops mtk_pcie_phy_ops
= {
129 .init
= mtk_pcie_phy_init
,
130 .owner
= THIS_MODULE
,
133 static int mtk_pcie_efuse_read_for_lane(struct mtk_pcie_phy
*pcie_phy
,
136 struct mtk_pcie_lane_efuse
*efuse
= &pcie_phy
->efuse
[lane
];
137 struct device
*dev
= pcie_phy
->dev
;
141 snprintf(efuse_id
, sizeof(efuse_id
), "tx_ln%d_pmos", lane
);
142 ret
= nvmem_cell_read_variable_le_u32(dev
, efuse_id
, &efuse
->tx_pmos
);
144 return dev_err_probe(dev
, ret
, "Failed to read %s\n", efuse_id
);
146 snprintf(efuse_id
, sizeof(efuse_id
), "tx_ln%d_nmos", lane
);
147 ret
= nvmem_cell_read_variable_le_u32(dev
, efuse_id
, &efuse
->tx_nmos
);
149 return dev_err_probe(dev
, ret
, "Failed to read %s\n", efuse_id
);
151 snprintf(efuse_id
, sizeof(efuse_id
), "rx_ln%d", lane
);
152 ret
= nvmem_cell_read_variable_le_u32(dev
, efuse_id
, &efuse
->rx_data
);
154 return dev_err_probe(dev
, ret
, "Failed to read %s\n", efuse_id
);
156 if (!(efuse
->tx_pmos
|| efuse
->tx_nmos
|| efuse
->rx_data
))
157 return dev_err_probe(dev
, -EINVAL
,
158 "No eFuse data found for lane%d, but dts enable it\n",
161 efuse
->lane_efuse_supported
= true;
166 static int mtk_pcie_read_efuse(struct mtk_pcie_phy
*pcie_phy
)
168 struct device
*dev
= pcie_phy
->dev
;
172 /* nvmem data is optional */
173 nvmem_enabled
= device_property_present(dev
, "nvmem-cells");
177 ret
= nvmem_cell_read_variable_le_u32(dev
, "glb_intr",
178 &pcie_phy
->efuse_glb_intr
);
180 return dev_err_probe(dev
, ret
, "Failed to read glb_intr\n");
182 pcie_phy
->sw_efuse_en
= true;
184 pcie_phy
->efuse
= devm_kzalloc(dev
, pcie_phy
->data
->num_lanes
*
185 sizeof(*pcie_phy
->efuse
), GFP_KERNEL
);
186 if (!pcie_phy
->efuse
)
189 for (i
= 0; i
< pcie_phy
->data
->num_lanes
; i
++) {
190 ret
= mtk_pcie_efuse_read_for_lane(pcie_phy
, i
);
198 static int mtk_pcie_phy_probe(struct platform_device
*pdev
)
200 struct device
*dev
= &pdev
->dev
;
201 struct phy_provider
*provider
;
202 struct mtk_pcie_phy
*pcie_phy
;
205 pcie_phy
= devm_kzalloc(dev
, sizeof(*pcie_phy
), GFP_KERNEL
);
209 pcie_phy
->sif_base
= devm_platform_ioremap_resource_byname(pdev
, "sif");
210 if (IS_ERR(pcie_phy
->sif_base
))
211 return dev_err_probe(dev
, PTR_ERR(pcie_phy
->sif_base
),
212 "Failed to map phy-sif base\n");
214 pcie_phy
->phy
= devm_phy_create(dev
, dev
->of_node
, &mtk_pcie_phy_ops
);
215 if (IS_ERR(pcie_phy
->phy
))
216 return dev_err_probe(dev
, PTR_ERR(pcie_phy
->phy
),
217 "Failed to create PCIe phy\n");
220 pcie_phy
->data
= of_device_get_match_data(dev
);
222 return dev_err_probe(dev
, -EINVAL
, "Failed to get phy data\n");
224 if (pcie_phy
->data
->sw_efuse_supported
) {
226 * Failed to read the efuse data is not a fatal problem,
227 * ignore the failure and keep going.
229 ret
= mtk_pcie_read_efuse(pcie_phy
);
230 if (ret
== -EPROBE_DEFER
|| ret
== -ENOMEM
)
234 phy_set_drvdata(pcie_phy
->phy
, pcie_phy
);
236 provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
237 if (IS_ERR(provider
))
238 return dev_err_probe(dev
, PTR_ERR(provider
),
239 "PCIe phy probe failed\n");
244 static const struct mtk_pcie_phy_data mt8195_data
= {
246 .sw_efuse_supported
= true,
249 static const struct of_device_id mtk_pcie_phy_of_match
[] = {
250 { .compatible
= "mediatek,mt8195-pcie-phy", .data
= &mt8195_data
},
253 MODULE_DEVICE_TABLE(of
, mtk_pcie_phy_of_match
);
255 static struct platform_driver mtk_pcie_phy_driver
= {
256 .probe
= mtk_pcie_phy_probe
,
258 .name
= "mtk-pcie-phy",
259 .of_match_table
= mtk_pcie_phy_of_match
,
262 module_platform_driver(mtk_pcie_phy_driver
);
264 MODULE_DESCRIPTION("MediaTek PCIe PHY driver");
265 MODULE_AUTHOR("Jianjun Wang <jianjun.wang@mediatek.com>");
266 MODULE_LICENSE("GPL");