174badb8bSNitheesh Sekar // SPDX-License-Identifier: GPL-2.0+
274badb8bSNitheesh Sekar /*
374badb8bSNitheesh Sekar * Copyright (c) 2025, The Linux Foundation. All rights reserved.
474badb8bSNitheesh Sekar */
574badb8bSNitheesh Sekar
674badb8bSNitheesh Sekar #include <linux/clk.h>
774badb8bSNitheesh Sekar #include <linux/clk-provider.h>
874badb8bSNitheesh Sekar #include <linux/delay.h>
974badb8bSNitheesh Sekar #include <linux/err.h>
1074badb8bSNitheesh Sekar #include <linux/io.h>
1174badb8bSNitheesh Sekar #include <linux/mfd/syscon.h>
1274badb8bSNitheesh Sekar #include <linux/module.h>
1374badb8bSNitheesh Sekar #include <linux/of_device.h>
1474badb8bSNitheesh Sekar #include <linux/of.h>
1574badb8bSNitheesh Sekar #include <linux/phy/phy.h>
1674badb8bSNitheesh Sekar #include <linux/platform_device.h>
1774badb8bSNitheesh Sekar #include <linux/regmap.h>
1874badb8bSNitheesh Sekar #include <linux/reset.h>
1974badb8bSNitheesh Sekar #include <linux/units.h>
2074badb8bSNitheesh Sekar
2174badb8bSNitheesh Sekar #define RST_ASSERT_DELAY_MIN_US 100
2274badb8bSNitheesh Sekar #define RST_ASSERT_DELAY_MAX_US 150
2374badb8bSNitheesh Sekar #define PIPE_CLK_DELAY_MIN_US 5000
2474badb8bSNitheesh Sekar #define PIPE_CLK_DELAY_MAX_US 5100
2574badb8bSNitheesh Sekar #define CLK_EN_DELAY_MIN_US 30
2674badb8bSNitheesh Sekar #define CLK_EN_DELAY_MAX_US 50
2774badb8bSNitheesh Sekar #define CDR_CTRL_REG_1 0x80
2874badb8bSNitheesh Sekar #define CDR_CTRL_REG_2 0x84
2974badb8bSNitheesh Sekar #define CDR_CTRL_REG_3 0x88
3074badb8bSNitheesh Sekar #define CDR_CTRL_REG_4 0x8c
3174badb8bSNitheesh Sekar #define CDR_CTRL_REG_5 0x90
3274badb8bSNitheesh Sekar #define CDR_CTRL_REG_6 0x94
3374badb8bSNitheesh Sekar #define CDR_CTRL_REG_7 0x98
3474badb8bSNitheesh Sekar #define SSCG_CTRL_REG_1 0x9c
3574badb8bSNitheesh Sekar #define SSCG_CTRL_REG_2 0xa0
3674badb8bSNitheesh Sekar #define SSCG_CTRL_REG_3 0xa4
3774badb8bSNitheesh Sekar #define SSCG_CTRL_REG_4 0xa8
3874badb8bSNitheesh Sekar #define SSCG_CTRL_REG_5 0xac
3974badb8bSNitheesh Sekar #define SSCG_CTRL_REG_6 0xb0
4074badb8bSNitheesh Sekar #define PCS_INTERNAL_CONTROL_2 0x2d8
4174badb8bSNitheesh Sekar
4274badb8bSNitheesh Sekar #define PHY_CFG_PLLCFG 0x220
4374badb8bSNitheesh Sekar #define PHY_CFG_EIOS_DTCT_REG 0x3e4
4474badb8bSNitheesh Sekar #define PHY_CFG_GEN3_ALIGN_HOLDOFF_TIME 0x3e8
4574badb8bSNitheesh Sekar
4674badb8bSNitheesh Sekar enum qcom_uniphy_pcie_type {
4774badb8bSNitheesh Sekar PHY_TYPE_PCIE = 1,
4874badb8bSNitheesh Sekar PHY_TYPE_PCIE_GEN2,
4974badb8bSNitheesh Sekar PHY_TYPE_PCIE_GEN3,
5074badb8bSNitheesh Sekar };
5174badb8bSNitheesh Sekar
5274badb8bSNitheesh Sekar struct qcom_uniphy_pcie_regs {
5374badb8bSNitheesh Sekar u32 offset;
5474badb8bSNitheesh Sekar u32 val;
5574badb8bSNitheesh Sekar };
5674badb8bSNitheesh Sekar
5774badb8bSNitheesh Sekar struct qcom_uniphy_pcie_data {
5874badb8bSNitheesh Sekar int lane_offset; /* offset between the lane register bases */
5974badb8bSNitheesh Sekar u32 phy_type;
6074badb8bSNitheesh Sekar const struct qcom_uniphy_pcie_regs *init_seq;
6174badb8bSNitheesh Sekar u32 init_seq_num;
6274badb8bSNitheesh Sekar u32 pipe_clk_rate;
6374badb8bSNitheesh Sekar };
6474badb8bSNitheesh Sekar
6574badb8bSNitheesh Sekar struct qcom_uniphy_pcie {
6674badb8bSNitheesh Sekar struct phy phy;
6774badb8bSNitheesh Sekar struct device *dev;
6874badb8bSNitheesh Sekar const struct qcom_uniphy_pcie_data *data;
6974badb8bSNitheesh Sekar struct clk_bulk_data *clks;
7074badb8bSNitheesh Sekar int num_clks;
7174badb8bSNitheesh Sekar struct reset_control *resets;
7274badb8bSNitheesh Sekar void __iomem *base;
7374badb8bSNitheesh Sekar int lanes;
7474badb8bSNitheesh Sekar };
7574badb8bSNitheesh Sekar
7674badb8bSNitheesh Sekar #define phy_to_dw_phy(x) container_of((x), struct qca_uni_pcie_phy, phy)
7774badb8bSNitheesh Sekar
78*dfc820d2SNitheesh Sekar static const struct qcom_uniphy_pcie_regs ipq5018_regs[] = {
79*dfc820d2SNitheesh Sekar {
80*dfc820d2SNitheesh Sekar .offset = SSCG_CTRL_REG_4,
81*dfc820d2SNitheesh Sekar .val = 0x1cb9,
82*dfc820d2SNitheesh Sekar }, {
83*dfc820d2SNitheesh Sekar .offset = SSCG_CTRL_REG_5,
84*dfc820d2SNitheesh Sekar .val = 0x023a,
85*dfc820d2SNitheesh Sekar }, {
86*dfc820d2SNitheesh Sekar .offset = SSCG_CTRL_REG_3,
87*dfc820d2SNitheesh Sekar .val = 0xd360,
88*dfc820d2SNitheesh Sekar }, {
89*dfc820d2SNitheesh Sekar .offset = SSCG_CTRL_REG_1,
90*dfc820d2SNitheesh Sekar .val = 0x1,
91*dfc820d2SNitheesh Sekar }, {
92*dfc820d2SNitheesh Sekar .offset = SSCG_CTRL_REG_2,
93*dfc820d2SNitheesh Sekar .val = 0xeb,
94*dfc820d2SNitheesh Sekar }, {
95*dfc820d2SNitheesh Sekar .offset = CDR_CTRL_REG_4,
96*dfc820d2SNitheesh Sekar .val = 0x3f9,
97*dfc820d2SNitheesh Sekar }, {
98*dfc820d2SNitheesh Sekar .offset = CDR_CTRL_REG_5,
99*dfc820d2SNitheesh Sekar .val = 0x1c9,
100*dfc820d2SNitheesh Sekar }, {
101*dfc820d2SNitheesh Sekar .offset = CDR_CTRL_REG_2,
102*dfc820d2SNitheesh Sekar .val = 0x419,
103*dfc820d2SNitheesh Sekar }, {
104*dfc820d2SNitheesh Sekar .offset = CDR_CTRL_REG_1,
105*dfc820d2SNitheesh Sekar .val = 0x200,
106*dfc820d2SNitheesh Sekar }, {
107*dfc820d2SNitheesh Sekar .offset = PCS_INTERNAL_CONTROL_2,
108*dfc820d2SNitheesh Sekar .val = 0xf101,
109*dfc820d2SNitheesh Sekar },
110*dfc820d2SNitheesh Sekar };
111*dfc820d2SNitheesh Sekar
11274badb8bSNitheesh Sekar static const struct qcom_uniphy_pcie_regs ipq5332_regs[] = {
11374badb8bSNitheesh Sekar {
11474badb8bSNitheesh Sekar .offset = PHY_CFG_PLLCFG,
11574badb8bSNitheesh Sekar .val = 0x30,
11674badb8bSNitheesh Sekar }, {
11774badb8bSNitheesh Sekar .offset = PHY_CFG_EIOS_DTCT_REG,
11874badb8bSNitheesh Sekar .val = 0x53ef,
11974badb8bSNitheesh Sekar }, {
12074badb8bSNitheesh Sekar .offset = PHY_CFG_GEN3_ALIGN_HOLDOFF_TIME,
12174badb8bSNitheesh Sekar .val = 0xcf,
12274badb8bSNitheesh Sekar },
12374badb8bSNitheesh Sekar };
12474badb8bSNitheesh Sekar
125*dfc820d2SNitheesh Sekar static const struct qcom_uniphy_pcie_data ipq5018_data = {
126*dfc820d2SNitheesh Sekar .lane_offset = 0x800,
127*dfc820d2SNitheesh Sekar .phy_type = PHY_TYPE_PCIE_GEN2,
128*dfc820d2SNitheesh Sekar .init_seq = ipq5018_regs,
129*dfc820d2SNitheesh Sekar .init_seq_num = ARRAY_SIZE(ipq5018_regs),
130*dfc820d2SNitheesh Sekar .pipe_clk_rate = 125 * MEGA,
131*dfc820d2SNitheesh Sekar };
132*dfc820d2SNitheesh Sekar
13374badb8bSNitheesh Sekar static const struct qcom_uniphy_pcie_data ipq5332_data = {
13474badb8bSNitheesh Sekar .lane_offset = 0x800,
13574badb8bSNitheesh Sekar .phy_type = PHY_TYPE_PCIE_GEN3,
13674badb8bSNitheesh Sekar .init_seq = ipq5332_regs,
13774badb8bSNitheesh Sekar .init_seq_num = ARRAY_SIZE(ipq5332_regs),
13874badb8bSNitheesh Sekar .pipe_clk_rate = 250 * MEGA,
13974badb8bSNitheesh Sekar };
14074badb8bSNitheesh Sekar
qcom_uniphy_pcie_init(struct qcom_uniphy_pcie * phy)14174badb8bSNitheesh Sekar static void qcom_uniphy_pcie_init(struct qcom_uniphy_pcie *phy)
14274badb8bSNitheesh Sekar {
14374badb8bSNitheesh Sekar const struct qcom_uniphy_pcie_data *data = phy->data;
14474badb8bSNitheesh Sekar const struct qcom_uniphy_pcie_regs *init_seq;
14574badb8bSNitheesh Sekar void __iomem *base = phy->base;
14674badb8bSNitheesh Sekar int lane, i;
14774badb8bSNitheesh Sekar
14874badb8bSNitheesh Sekar for (lane = 0; lane < phy->lanes; lane++) {
14974badb8bSNitheesh Sekar init_seq = data->init_seq;
15074badb8bSNitheesh Sekar
15174badb8bSNitheesh Sekar for (i = 0; i < data->init_seq_num; i++)
15274badb8bSNitheesh Sekar writel(init_seq[i].val, base + init_seq[i].offset);
15374badb8bSNitheesh Sekar
15474badb8bSNitheesh Sekar base += data->lane_offset;
15574badb8bSNitheesh Sekar }
15674badb8bSNitheesh Sekar }
15774badb8bSNitheesh Sekar
qcom_uniphy_pcie_power_off(struct phy * x)15874badb8bSNitheesh Sekar static int qcom_uniphy_pcie_power_off(struct phy *x)
15974badb8bSNitheesh Sekar {
16074badb8bSNitheesh Sekar struct qcom_uniphy_pcie *phy = phy_get_drvdata(x);
16174badb8bSNitheesh Sekar
16274badb8bSNitheesh Sekar clk_bulk_disable_unprepare(phy->num_clks, phy->clks);
16374badb8bSNitheesh Sekar
16474badb8bSNitheesh Sekar return reset_control_assert(phy->resets);
16574badb8bSNitheesh Sekar }
16674badb8bSNitheesh Sekar
qcom_uniphy_pcie_power_on(struct phy * x)16774badb8bSNitheesh Sekar static int qcom_uniphy_pcie_power_on(struct phy *x)
16874badb8bSNitheesh Sekar {
16974badb8bSNitheesh Sekar struct qcom_uniphy_pcie *phy = phy_get_drvdata(x);
17074badb8bSNitheesh Sekar int ret;
17174badb8bSNitheesh Sekar
17274badb8bSNitheesh Sekar ret = reset_control_assert(phy->resets);
17374badb8bSNitheesh Sekar if (ret) {
17474badb8bSNitheesh Sekar dev_err(phy->dev, "reset assert failed (%d)\n", ret);
17574badb8bSNitheesh Sekar return ret;
17674badb8bSNitheesh Sekar }
17774badb8bSNitheesh Sekar
17874badb8bSNitheesh Sekar usleep_range(RST_ASSERT_DELAY_MIN_US, RST_ASSERT_DELAY_MAX_US);
17974badb8bSNitheesh Sekar
18074badb8bSNitheesh Sekar ret = reset_control_deassert(phy->resets);
18174badb8bSNitheesh Sekar if (ret) {
18274badb8bSNitheesh Sekar dev_err(phy->dev, "reset deassert failed (%d)\n", ret);
18374badb8bSNitheesh Sekar return ret;
18474badb8bSNitheesh Sekar }
18574badb8bSNitheesh Sekar
18674badb8bSNitheesh Sekar usleep_range(PIPE_CLK_DELAY_MIN_US, PIPE_CLK_DELAY_MAX_US);
18774badb8bSNitheesh Sekar
18874badb8bSNitheesh Sekar ret = clk_bulk_prepare_enable(phy->num_clks, phy->clks);
18974badb8bSNitheesh Sekar if (ret) {
19074badb8bSNitheesh Sekar dev_err(phy->dev, "clk prepare and enable failed %d\n", ret);
19174badb8bSNitheesh Sekar return ret;
19274badb8bSNitheesh Sekar }
19374badb8bSNitheesh Sekar
19474badb8bSNitheesh Sekar usleep_range(CLK_EN_DELAY_MIN_US, CLK_EN_DELAY_MAX_US);
19574badb8bSNitheesh Sekar
19674badb8bSNitheesh Sekar qcom_uniphy_pcie_init(phy);
19774badb8bSNitheesh Sekar
19874badb8bSNitheesh Sekar return 0;
19974badb8bSNitheesh Sekar }
20074badb8bSNitheesh Sekar
qcom_uniphy_pcie_get_resources(struct platform_device * pdev,struct qcom_uniphy_pcie * phy)20174badb8bSNitheesh Sekar static inline int qcom_uniphy_pcie_get_resources(struct platform_device *pdev,
20274badb8bSNitheesh Sekar struct qcom_uniphy_pcie *phy)
20374badb8bSNitheesh Sekar {
20474badb8bSNitheesh Sekar struct resource *res;
20574badb8bSNitheesh Sekar
20674badb8bSNitheesh Sekar phy->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
20774badb8bSNitheesh Sekar if (IS_ERR(phy->base))
20874badb8bSNitheesh Sekar return PTR_ERR(phy->base);
20974badb8bSNitheesh Sekar
21074badb8bSNitheesh Sekar phy->num_clks = devm_clk_bulk_get_all(phy->dev, &phy->clks);
21174badb8bSNitheesh Sekar if (phy->num_clks < 0)
21274badb8bSNitheesh Sekar return phy->num_clks;
21374badb8bSNitheesh Sekar
21474badb8bSNitheesh Sekar phy->resets = devm_reset_control_array_get_exclusive(phy->dev);
21574badb8bSNitheesh Sekar if (IS_ERR(phy->resets))
21674badb8bSNitheesh Sekar return PTR_ERR(phy->resets);
21774badb8bSNitheesh Sekar
21874badb8bSNitheesh Sekar return 0;
21974badb8bSNitheesh Sekar }
22074badb8bSNitheesh Sekar
22174badb8bSNitheesh Sekar /*
22274badb8bSNitheesh Sekar * Register a fixed rate pipe clock.
22374badb8bSNitheesh Sekar *
22474badb8bSNitheesh Sekar * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
22574badb8bSNitheesh Sekar * controls it. The <s>_pipe_clk coming out of the GCC is requested
22674badb8bSNitheesh Sekar * by the PHY driver for its operations.
22774badb8bSNitheesh Sekar * We register the <s>_pipe_clksrc here. The gcc driver takes care
22874badb8bSNitheesh Sekar * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
22974badb8bSNitheesh Sekar * Below picture shows this relationship.
23074badb8bSNitheesh Sekar *
23174badb8bSNitheesh Sekar * +---------------+
23274badb8bSNitheesh Sekar * | PHY block |<<---------------------------------------+
23374badb8bSNitheesh Sekar * | | |
23474badb8bSNitheesh Sekar * | +-------+ | +-----+ |
23574badb8bSNitheesh Sekar * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
23674badb8bSNitheesh Sekar * clk | +-------+ | +-----+
23774badb8bSNitheesh Sekar * +---------------+
23874badb8bSNitheesh Sekar */
phy_pipe_clk_register(struct qcom_uniphy_pcie * phy,int id)23974badb8bSNitheesh Sekar static inline int phy_pipe_clk_register(struct qcom_uniphy_pcie *phy, int id)
24074badb8bSNitheesh Sekar {
24174badb8bSNitheesh Sekar const struct qcom_uniphy_pcie_data *data = phy->data;
24274badb8bSNitheesh Sekar struct clk_hw *hw;
24374badb8bSNitheesh Sekar char name[64];
24474badb8bSNitheesh Sekar
24574badb8bSNitheesh Sekar snprintf(name, sizeof(name), "phy%d_pipe_clk_src", id);
24674badb8bSNitheesh Sekar hw = devm_clk_hw_register_fixed_rate(phy->dev, name, NULL, 0,
24774badb8bSNitheesh Sekar data->pipe_clk_rate);
24874badb8bSNitheesh Sekar if (IS_ERR(hw))
24974badb8bSNitheesh Sekar return dev_err_probe(phy->dev, PTR_ERR(hw),
25074badb8bSNitheesh Sekar "Unable to register %s\n", name);
25174badb8bSNitheesh Sekar
25274badb8bSNitheesh Sekar return devm_of_clk_add_hw_provider(phy->dev, of_clk_hw_simple_get, hw);
25374badb8bSNitheesh Sekar }
25474badb8bSNitheesh Sekar
25574badb8bSNitheesh Sekar static const struct of_device_id qcom_uniphy_pcie_id_table[] = {
25674badb8bSNitheesh Sekar {
257*dfc820d2SNitheesh Sekar .compatible = "qcom,ipq5018-uniphy-pcie-phy",
258*dfc820d2SNitheesh Sekar .data = &ipq5018_data,
259*dfc820d2SNitheesh Sekar }, {
26074badb8bSNitheesh Sekar .compatible = "qcom,ipq5332-uniphy-pcie-phy",
26174badb8bSNitheesh Sekar .data = &ipq5332_data,
26274badb8bSNitheesh Sekar }, {
26374badb8bSNitheesh Sekar /* Sentinel */
26474badb8bSNitheesh Sekar },
26574badb8bSNitheesh Sekar };
26674badb8bSNitheesh Sekar MODULE_DEVICE_TABLE(of, qcom_uniphy_pcie_id_table);
26774badb8bSNitheesh Sekar
26874badb8bSNitheesh Sekar static const struct phy_ops pcie_ops = {
26974badb8bSNitheesh Sekar .power_on = qcom_uniphy_pcie_power_on,
27074badb8bSNitheesh Sekar .power_off = qcom_uniphy_pcie_power_off,
27174badb8bSNitheesh Sekar .owner = THIS_MODULE,
27274badb8bSNitheesh Sekar };
27374badb8bSNitheesh Sekar
qcom_uniphy_pcie_probe(struct platform_device * pdev)27474badb8bSNitheesh Sekar static int qcom_uniphy_pcie_probe(struct platform_device *pdev)
27574badb8bSNitheesh Sekar {
27674badb8bSNitheesh Sekar struct phy_provider *phy_provider;
27774badb8bSNitheesh Sekar struct device *dev = &pdev->dev;
27874badb8bSNitheesh Sekar struct qcom_uniphy_pcie *phy;
27974badb8bSNitheesh Sekar struct phy *generic_phy;
28074badb8bSNitheesh Sekar int ret;
28174badb8bSNitheesh Sekar
28274badb8bSNitheesh Sekar phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
28374badb8bSNitheesh Sekar if (!phy)
28474badb8bSNitheesh Sekar return -ENOMEM;
28574badb8bSNitheesh Sekar
28674badb8bSNitheesh Sekar platform_set_drvdata(pdev, phy);
28774badb8bSNitheesh Sekar phy->dev = &pdev->dev;
28874badb8bSNitheesh Sekar
28974badb8bSNitheesh Sekar phy->data = of_device_get_match_data(dev);
29074badb8bSNitheesh Sekar if (!phy->data)
29174badb8bSNitheesh Sekar return -EINVAL;
29274badb8bSNitheesh Sekar
29374badb8bSNitheesh Sekar ret = of_property_read_u32(dev_of_node(dev), "num-lanes", &phy->lanes);
29474badb8bSNitheesh Sekar if (ret)
29574badb8bSNitheesh Sekar return dev_err_probe(dev, ret, "Couldn't read num-lanes\n");
29674badb8bSNitheesh Sekar
29774badb8bSNitheesh Sekar ret = qcom_uniphy_pcie_get_resources(pdev, phy);
29874badb8bSNitheesh Sekar if (ret < 0)
29974badb8bSNitheesh Sekar return dev_err_probe(&pdev->dev, ret,
30074badb8bSNitheesh Sekar "failed to get resources: %d\n", ret);
30174badb8bSNitheesh Sekar
30274badb8bSNitheesh Sekar generic_phy = devm_phy_create(phy->dev, NULL, &pcie_ops);
30374badb8bSNitheesh Sekar if (IS_ERR(generic_phy))
30474badb8bSNitheesh Sekar return PTR_ERR(generic_phy);
30574badb8bSNitheesh Sekar
30674badb8bSNitheesh Sekar phy_set_drvdata(generic_phy, phy);
30774badb8bSNitheesh Sekar
30874badb8bSNitheesh Sekar ret = phy_pipe_clk_register(phy, generic_phy->id);
30974badb8bSNitheesh Sekar if (ret)
31074badb8bSNitheesh Sekar dev_err(&pdev->dev, "failed to register phy pipe clk\n");
31174badb8bSNitheesh Sekar
31274badb8bSNitheesh Sekar phy_provider = devm_of_phy_provider_register(phy->dev,
31374badb8bSNitheesh Sekar of_phy_simple_xlate);
31474badb8bSNitheesh Sekar if (IS_ERR(phy_provider))
31574badb8bSNitheesh Sekar return PTR_ERR(phy_provider);
31674badb8bSNitheesh Sekar
31774badb8bSNitheesh Sekar return 0;
31874badb8bSNitheesh Sekar }
31974badb8bSNitheesh Sekar
32074badb8bSNitheesh Sekar static struct platform_driver qcom_uniphy_pcie_driver = {
32174badb8bSNitheesh Sekar .probe = qcom_uniphy_pcie_probe,
32274badb8bSNitheesh Sekar .driver = {
32374badb8bSNitheesh Sekar .name = "qcom-uniphy-pcie",
32474badb8bSNitheesh Sekar .of_match_table = qcom_uniphy_pcie_id_table,
32574badb8bSNitheesh Sekar },
32674badb8bSNitheesh Sekar };
32774badb8bSNitheesh Sekar
32874badb8bSNitheesh Sekar module_platform_driver(qcom_uniphy_pcie_driver);
32974badb8bSNitheesh Sekar
33074badb8bSNitheesh Sekar MODULE_DESCRIPTION("PCIE QCOM UNIPHY driver");
33174badb8bSNitheesh Sekar MODULE_LICENSE("GPL");
332