18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014, The Linux Foundation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/io.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/of.h>
108c2ecf20Sopenharmony_ci#include <linux/of_address.h>
118c2ecf20Sopenharmony_ci#include <linux/time.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct qcom_ipq806x_sata_phy {
198c2ecf20Sopenharmony_ci	void __iomem *mmio;
208c2ecf20Sopenharmony_ci	struct clk *cfg_clk;
218c2ecf20Sopenharmony_ci	struct device *dev;
228c2ecf20Sopenharmony_ci};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define __set(v, a, b)	(((v) << (b)) & GENMASK(a, b))
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0		0x200
278c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(x)	__set(x, 17, 12)
288c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK	GENMASK(17, 12)
298c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2(x)	__set(x, 11, 6)
308c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK	GENMASK(11, 6)
318c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1(x)	__set(x, 5, 0)
328c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK	GENMASK(5, 0)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1		0x204
358c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_RESERVED_BITS31_21(x)	__set(x, 31, 21)
368c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(x)	__set(x, 20, 14)
378c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK	GENMASK(20, 14)
388c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(x)	__set(x, 13, 7)
398c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK	GENMASK(13, 7)
408c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(x)	__set(x, 6, 0)
418c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK	GENMASK(6, 0)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM2		0x208
448c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM2_RX_EQ(x)	__set(x, 20, 18)
458c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM2_RX_EQ_MASK	GENMASK(20, 18)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM3		0x20C
488c2ecf20Sopenharmony_ci#define SATA_PHY_SSC_EN			0x8
498c2ecf20Sopenharmony_ci#define SATA_PHY_P0_PARAM4		0x210
508c2ecf20Sopenharmony_ci#define SATA_PHY_REF_SSP_EN		0x2
518c2ecf20Sopenharmony_ci#define SATA_PHY_RESET			0x1
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int qcom_ipq806x_sata_phy_init(struct phy *generic_phy)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
568c2ecf20Sopenharmony_ci	u32 reg;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/* Setting SSC_EN to 1 */
598c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM3);
608c2ecf20Sopenharmony_ci	reg = reg | SATA_PHY_SSC_EN;
618c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM3);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM0) &
648c2ecf20Sopenharmony_ci			~(SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK |
658c2ecf20Sopenharmony_ci			  SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK |
668c2ecf20Sopenharmony_ci			  SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK);
678c2ecf20Sopenharmony_ci	reg |= SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(0xf);
688c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM0);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM1) &
718c2ecf20Sopenharmony_ci			~(SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK |
728c2ecf20Sopenharmony_ci			  SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK |
738c2ecf20Sopenharmony_ci			  SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK);
748c2ecf20Sopenharmony_ci	reg |= SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(0x55) |
758c2ecf20Sopenharmony_ci		SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(0x55) |
768c2ecf20Sopenharmony_ci		SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(0x55);
778c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM1);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM2) &
808c2ecf20Sopenharmony_ci		~SATA_PHY_P0_PARAM2_RX_EQ_MASK;
818c2ecf20Sopenharmony_ci	reg |= SATA_PHY_P0_PARAM2_RX_EQ(0x3);
828c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM2);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* Setting PHY_RESET to 1 */
858c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
868c2ecf20Sopenharmony_ci	reg = reg | SATA_PHY_RESET;
878c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Setting REF_SSP_EN to 1 */
908c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
918c2ecf20Sopenharmony_ci	reg = reg | SATA_PHY_REF_SSP_EN | SATA_PHY_RESET;
928c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* make sure all changes complete before we let the PHY out of reset */
958c2ecf20Sopenharmony_ci	mb();
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* sleep for max. 50us more to combine processor wakeups */
988c2ecf20Sopenharmony_ci	usleep_range(20, 20 + 50);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* Clearing PHY_RESET to 0 */
1018c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
1028c2ecf20Sopenharmony_ci	reg = reg & ~SATA_PHY_RESET;
1038c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return 0;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int qcom_ipq806x_sata_phy_exit(struct phy *generic_phy)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
1118c2ecf20Sopenharmony_ci	u32 reg;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* Setting PHY_RESET to 1 */
1148c2ecf20Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
1158c2ecf20Sopenharmony_ci	reg = reg | SATA_PHY_RESET;
1168c2ecf20Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return 0;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic const struct phy_ops qcom_ipq806x_sata_phy_ops = {
1228c2ecf20Sopenharmony_ci	.init		= qcom_ipq806x_sata_phy_init,
1238c2ecf20Sopenharmony_ci	.exit		= qcom_ipq806x_sata_phy_exit,
1248c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic int qcom_ipq806x_sata_phy_probe(struct platform_device *pdev)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy;
1308c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1318c2ecf20Sopenharmony_ci	struct resource *res;
1328c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
1338c2ecf20Sopenharmony_ci	struct phy *generic_phy;
1348c2ecf20Sopenharmony_ci	int ret;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
1378c2ecf20Sopenharmony_ci	if (!phy)
1388c2ecf20Sopenharmony_ci		return -ENOMEM;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1418c2ecf20Sopenharmony_ci	phy->mmio = devm_ioremap_resource(dev, res);
1428c2ecf20Sopenharmony_ci	if (IS_ERR(phy->mmio))
1438c2ecf20Sopenharmony_ci		return PTR_ERR(phy->mmio);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	generic_phy = devm_phy_create(dev, NULL, &qcom_ipq806x_sata_phy_ops);
1468c2ecf20Sopenharmony_ci	if (IS_ERR(generic_phy)) {
1478c2ecf20Sopenharmony_ci		dev_err(dev, "%s: failed to create phy\n", __func__);
1488c2ecf20Sopenharmony_ci		return PTR_ERR(generic_phy);
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	phy->dev = dev;
1528c2ecf20Sopenharmony_ci	phy_set_drvdata(generic_phy, phy);
1538c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, phy);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	phy->cfg_clk = devm_clk_get(dev, "cfg");
1568c2ecf20Sopenharmony_ci	if (IS_ERR(phy->cfg_clk)) {
1578c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get sata cfg clock\n");
1588c2ecf20Sopenharmony_ci		return PTR_ERR(phy->cfg_clk);
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(phy->cfg_clk);
1628c2ecf20Sopenharmony_ci	if (ret)
1638c2ecf20Sopenharmony_ci		return ret;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1668c2ecf20Sopenharmony_ci	if (IS_ERR(phy_provider)) {
1678c2ecf20Sopenharmony_ci		clk_disable_unprepare(phy->cfg_clk);
1688c2ecf20Sopenharmony_ci		dev_err(dev, "%s: failed to register phy\n", __func__);
1698c2ecf20Sopenharmony_ci		return PTR_ERR(phy_provider);
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic int qcom_ipq806x_sata_phy_remove(struct platform_device *pdev)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy = platform_get_drvdata(pdev);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	clk_disable_unprepare(phy->cfg_clk);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic const struct of_device_id qcom_ipq806x_sata_phy_of_match[] = {
1858c2ecf20Sopenharmony_ci	{ .compatible = "qcom,ipq806x-sata-phy" },
1868c2ecf20Sopenharmony_ci	{ },
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, qcom_ipq806x_sata_phy_of_match);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic struct platform_driver qcom_ipq806x_sata_phy_driver = {
1918c2ecf20Sopenharmony_ci	.probe	= qcom_ipq806x_sata_phy_probe,
1928c2ecf20Sopenharmony_ci	.remove	= qcom_ipq806x_sata_phy_remove,
1938c2ecf20Sopenharmony_ci	.driver = {
1948c2ecf20Sopenharmony_ci		.name	= "qcom-ipq806x-sata-phy",
1958c2ecf20Sopenharmony_ci		.of_match_table	= qcom_ipq806x_sata_phy_of_match,
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_cimodule_platform_driver(qcom_ipq806x_sata_phy_driver);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("QCOM IPQ806x SATA PHY driver");
2018c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
202