xref: /linux/drivers/phy/qualcomm/phy-qcom-ipq806x-sata.c (revision c771600c6af14749609b49565ffb4cac2959710d)
197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24f6160d4SKumar Gala /*
34f6160d4SKumar Gala  * Copyright (c) 2014, The Linux Foundation. All rights reserved.
44f6160d4SKumar Gala  */
54f6160d4SKumar Gala 
64f6160d4SKumar Gala #include <linux/io.h>
74f6160d4SKumar Gala #include <linux/kernel.h>
84f6160d4SKumar Gala #include <linux/module.h>
94f6160d4SKumar Gala #include <linux/of.h>
104f6160d4SKumar Gala #include <linux/of_address.h>
114f6160d4SKumar Gala #include <linux/time.h>
124f6160d4SKumar Gala #include <linux/delay.h>
134f6160d4SKumar Gala #include <linux/clk.h>
144f6160d4SKumar Gala #include <linux/slab.h>
154f6160d4SKumar Gala #include <linux/platform_device.h>
164f6160d4SKumar Gala #include <linux/phy/phy.h>
174f6160d4SKumar Gala 
184f6160d4SKumar Gala struct qcom_ipq806x_sata_phy {
194f6160d4SKumar Gala 	void __iomem *mmio;
204f6160d4SKumar Gala 	struct clk *cfg_clk;
214f6160d4SKumar Gala 	struct device *dev;
224f6160d4SKumar Gala };
234f6160d4SKumar Gala 
244f6160d4SKumar Gala #define __set(v, a, b)	(((v) << (b)) & GENMASK(a, b))
254f6160d4SKumar Gala 
264f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0		0x200
274f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(x)	__set(x, 17, 12)
284f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK	GENMASK(17, 12)
294f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2(x)	__set(x, 11, 6)
304f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK	GENMASK(11, 6)
314f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1(x)	__set(x, 5, 0)
324f6160d4SKumar Gala #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK	GENMASK(5, 0)
334f6160d4SKumar Gala 
344f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1		0x204
354f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_RESERVED_BITS31_21(x)	__set(x, 31, 21)
364f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(x)	__set(x, 20, 14)
374f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK	GENMASK(20, 14)
384f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(x)	__set(x, 13, 7)
394f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK	GENMASK(13, 7)
404f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(x)	__set(x, 6, 0)
414f6160d4SKumar Gala #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK	GENMASK(6, 0)
424f6160d4SKumar Gala 
434f6160d4SKumar Gala #define SATA_PHY_P0_PARAM2		0x208
444f6160d4SKumar Gala #define SATA_PHY_P0_PARAM2_RX_EQ(x)	__set(x, 20, 18)
454f6160d4SKumar Gala #define SATA_PHY_P0_PARAM2_RX_EQ_MASK	GENMASK(20, 18)
464f6160d4SKumar Gala 
474f6160d4SKumar Gala #define SATA_PHY_P0_PARAM3		0x20C
484f6160d4SKumar Gala #define SATA_PHY_SSC_EN			0x8
494f6160d4SKumar Gala #define SATA_PHY_P0_PARAM4		0x210
504f6160d4SKumar Gala #define SATA_PHY_REF_SSP_EN		0x2
514f6160d4SKumar Gala #define SATA_PHY_RESET			0x1
524f6160d4SKumar Gala 
qcom_ipq806x_sata_phy_init(struct phy * generic_phy)534f6160d4SKumar Gala static int qcom_ipq806x_sata_phy_init(struct phy *generic_phy)
544f6160d4SKumar Gala {
554f6160d4SKumar Gala 	struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
564f6160d4SKumar Gala 	u32 reg;
574f6160d4SKumar Gala 
584f6160d4SKumar Gala 	/* Setting SSC_EN to 1 */
594f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM3);
604f6160d4SKumar Gala 	reg = reg | SATA_PHY_SSC_EN;
614f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM3);
624f6160d4SKumar Gala 
634f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM0) &
644f6160d4SKumar Gala 			~(SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK |
654f6160d4SKumar Gala 			  SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK |
664f6160d4SKumar Gala 			  SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK);
674f6160d4SKumar Gala 	reg |= SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(0xf);
684f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM0);
694f6160d4SKumar Gala 
704f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM1) &
714f6160d4SKumar Gala 			~(SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK |
724f6160d4SKumar Gala 			  SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK |
734f6160d4SKumar Gala 			  SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK);
744f6160d4SKumar Gala 	reg |= SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(0x55) |
754f6160d4SKumar Gala 		SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(0x55) |
764f6160d4SKumar Gala 		SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(0x55);
774f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM1);
784f6160d4SKumar Gala 
794f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM2) &
804f6160d4SKumar Gala 		~SATA_PHY_P0_PARAM2_RX_EQ_MASK;
814f6160d4SKumar Gala 	reg |= SATA_PHY_P0_PARAM2_RX_EQ(0x3);
824f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM2);
834f6160d4SKumar Gala 
844f6160d4SKumar Gala 	/* Setting PHY_RESET to 1 */
854f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
864f6160d4SKumar Gala 	reg = reg | SATA_PHY_RESET;
874f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
884f6160d4SKumar Gala 
894f6160d4SKumar Gala 	/* Setting REF_SSP_EN to 1 */
904f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
914f6160d4SKumar Gala 	reg = reg | SATA_PHY_REF_SSP_EN | SATA_PHY_RESET;
924f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
934f6160d4SKumar Gala 
944f6160d4SKumar Gala 	/* make sure all changes complete before we let the PHY out of reset */
954f6160d4SKumar Gala 	mb();
964f6160d4SKumar Gala 
974f6160d4SKumar Gala 	/* sleep for max. 50us more to combine processor wakeups */
984f6160d4SKumar Gala 	usleep_range(20, 20 + 50);
994f6160d4SKumar Gala 
1004f6160d4SKumar Gala 	/* Clearing PHY_RESET to 0 */
1014f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
1024f6160d4SKumar Gala 	reg = reg & ~SATA_PHY_RESET;
1034f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
1044f6160d4SKumar Gala 
1054f6160d4SKumar Gala 	return 0;
1064f6160d4SKumar Gala }
1074f6160d4SKumar Gala 
qcom_ipq806x_sata_phy_exit(struct phy * generic_phy)1084f6160d4SKumar Gala static int qcom_ipq806x_sata_phy_exit(struct phy *generic_phy)
1094f6160d4SKumar Gala {
1104f6160d4SKumar Gala 	struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
1114f6160d4SKumar Gala 	u32 reg;
1124f6160d4SKumar Gala 
1134f6160d4SKumar Gala 	/* Setting PHY_RESET to 1 */
1144f6160d4SKumar Gala 	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
1154f6160d4SKumar Gala 	reg = reg | SATA_PHY_RESET;
1164f6160d4SKumar Gala 	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
1174f6160d4SKumar Gala 
1184f6160d4SKumar Gala 	return 0;
1194f6160d4SKumar Gala }
1204f6160d4SKumar Gala 
1214a9e5ca1SAxel Lin static const struct phy_ops qcom_ipq806x_sata_phy_ops = {
1224f6160d4SKumar Gala 	.init		= qcom_ipq806x_sata_phy_init,
1234f6160d4SKumar Gala 	.exit		= qcom_ipq806x_sata_phy_exit,
1244f6160d4SKumar Gala 	.owner		= THIS_MODULE,
1254f6160d4SKumar Gala };
1264f6160d4SKumar Gala 
qcom_ipq806x_sata_phy_probe(struct platform_device * pdev)1274f6160d4SKumar Gala static int qcom_ipq806x_sata_phy_probe(struct platform_device *pdev)
1284f6160d4SKumar Gala {
1294f6160d4SKumar Gala 	struct qcom_ipq806x_sata_phy *phy;
1304f6160d4SKumar Gala 	struct device *dev = &pdev->dev;
1314f6160d4SKumar Gala 	struct phy_provider *phy_provider;
1324f6160d4SKumar Gala 	struct phy *generic_phy;
1334f6160d4SKumar Gala 	int ret;
1344f6160d4SKumar Gala 
1354f6160d4SKumar Gala 	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
1364f6160d4SKumar Gala 	if (!phy)
1374f6160d4SKumar Gala 		return -ENOMEM;
1384f6160d4SKumar Gala 
1398a7772cdSChunfeng Yun 	phy->mmio = devm_platform_ioremap_resource(pdev, 0);
1404f6160d4SKumar Gala 	if (IS_ERR(phy->mmio))
1414f6160d4SKumar Gala 		return PTR_ERR(phy->mmio);
1424f6160d4SKumar Gala 
143dbc98635SHeikki Krogerus 	generic_phy = devm_phy_create(dev, NULL, &qcom_ipq806x_sata_phy_ops);
1444f6160d4SKumar Gala 	if (IS_ERR(generic_phy)) {
1454f6160d4SKumar Gala 		dev_err(dev, "%s: failed to create phy\n", __func__);
1464f6160d4SKumar Gala 		return PTR_ERR(generic_phy);
1474f6160d4SKumar Gala 	}
1484f6160d4SKumar Gala 
1494f6160d4SKumar Gala 	phy->dev = dev;
1504f6160d4SKumar Gala 	phy_set_drvdata(generic_phy, phy);
1514f6160d4SKumar Gala 	platform_set_drvdata(pdev, phy);
1524f6160d4SKumar Gala 
1534f6160d4SKumar Gala 	phy->cfg_clk = devm_clk_get(dev, "cfg");
1544f6160d4SKumar Gala 	if (IS_ERR(phy->cfg_clk)) {
1554f6160d4SKumar Gala 		dev_err(dev, "Failed to get sata cfg clock\n");
1564f6160d4SKumar Gala 		return PTR_ERR(phy->cfg_clk);
1574f6160d4SKumar Gala 	}
1584f6160d4SKumar Gala 
1594f6160d4SKumar Gala 	ret = clk_prepare_enable(phy->cfg_clk);
1604f6160d4SKumar Gala 	if (ret)
1614f6160d4SKumar Gala 		return ret;
1624f6160d4SKumar Gala 
1634f6160d4SKumar Gala 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1644f6160d4SKumar Gala 	if (IS_ERR(phy_provider)) {
1654f6160d4SKumar Gala 		clk_disable_unprepare(phy->cfg_clk);
1664f6160d4SKumar Gala 		dev_err(dev, "%s: failed to register phy\n", __func__);
1674f6160d4SKumar Gala 		return PTR_ERR(phy_provider);
1684f6160d4SKumar Gala 	}
1694f6160d4SKumar Gala 
1704f6160d4SKumar Gala 	return 0;
1714f6160d4SKumar Gala }
1724f6160d4SKumar Gala 
qcom_ipq806x_sata_phy_remove(struct platform_device * pdev)173de6862d1SUwe Kleine-König static void qcom_ipq806x_sata_phy_remove(struct platform_device *pdev)
1744f6160d4SKumar Gala {
1754f6160d4SKumar Gala 	struct qcom_ipq806x_sata_phy *phy = platform_get_drvdata(pdev);
1764f6160d4SKumar Gala 
1774f6160d4SKumar Gala 	clk_disable_unprepare(phy->cfg_clk);
1784f6160d4SKumar Gala }
1794f6160d4SKumar Gala 
1804f6160d4SKumar Gala static const struct of_device_id qcom_ipq806x_sata_phy_of_match[] = {
1814f6160d4SKumar Gala 	{ .compatible = "qcom,ipq806x-sata-phy" },
1824f6160d4SKumar Gala 	{ },
1834f6160d4SKumar Gala };
1844f6160d4SKumar Gala MODULE_DEVICE_TABLE(of, qcom_ipq806x_sata_phy_of_match);
1854f6160d4SKumar Gala 
1864f6160d4SKumar Gala static struct platform_driver qcom_ipq806x_sata_phy_driver = {
1874f6160d4SKumar Gala 	.probe = qcom_ipq806x_sata_phy_probe,
188*54234e3aSUwe Kleine-König 	.remove = qcom_ipq806x_sata_phy_remove,
1894f6160d4SKumar Gala 	.driver = {
1904f6160d4SKumar Gala 		.name = "qcom-ipq806x-sata-phy",
1914f6160d4SKumar Gala 		.of_match_table = qcom_ipq806x_sata_phy_of_match,
1924f6160d4SKumar Gala 	}
1934f6160d4SKumar Gala };
1944f6160d4SKumar Gala module_platform_driver(qcom_ipq806x_sata_phy_driver);
1954f6160d4SKumar Gala 
1964f6160d4SKumar Gala MODULE_DESCRIPTION("QCOM IPQ806x SATA PHY driver");
1974f6160d4SKumar Gala MODULE_LICENSE("GPL v2");
198