162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2014, The Linux Foundation. All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/io.h>
762306a36Sopenharmony_ci#include <linux/kernel.h>
862306a36Sopenharmony_ci#include <linux/module.h>
962306a36Sopenharmony_ci#include <linux/of.h>
1062306a36Sopenharmony_ci#include <linux/of_address.h>
1162306a36Sopenharmony_ci#include <linux/time.h>
1262306a36Sopenharmony_ci#include <linux/delay.h>
1362306a36Sopenharmony_ci#include <linux/clk.h>
1462306a36Sopenharmony_ci#include <linux/slab.h>
1562306a36Sopenharmony_ci#include <linux/platform_device.h>
1662306a36Sopenharmony_ci#include <linux/phy/phy.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct qcom_ipq806x_sata_phy {
1962306a36Sopenharmony_ci	void __iomem *mmio;
2062306a36Sopenharmony_ci	struct clk *cfg_clk;
2162306a36Sopenharmony_ci	struct device *dev;
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define __set(v, a, b)	(((v) << (b)) & GENMASK(a, b))
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0		0x200
2762306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(x)	__set(x, 17, 12)
2862306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK	GENMASK(17, 12)
2962306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2(x)	__set(x, 11, 6)
3062306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK	GENMASK(11, 6)
3162306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1(x)	__set(x, 5, 0)
3262306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK	GENMASK(5, 0)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1		0x204
3562306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_RESERVED_BITS31_21(x)	__set(x, 31, 21)
3662306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(x)	__set(x, 20, 14)
3762306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK	GENMASK(20, 14)
3862306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(x)	__set(x, 13, 7)
3962306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK	GENMASK(13, 7)
4062306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(x)	__set(x, 6, 0)
4162306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK	GENMASK(6, 0)
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM2		0x208
4462306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM2_RX_EQ(x)	__set(x, 20, 18)
4562306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM2_RX_EQ_MASK	GENMASK(20, 18)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM3		0x20C
4862306a36Sopenharmony_ci#define SATA_PHY_SSC_EN			0x8
4962306a36Sopenharmony_ci#define SATA_PHY_P0_PARAM4		0x210
5062306a36Sopenharmony_ci#define SATA_PHY_REF_SSP_EN		0x2
5162306a36Sopenharmony_ci#define SATA_PHY_RESET			0x1
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic int qcom_ipq806x_sata_phy_init(struct phy *generic_phy)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
5662306a36Sopenharmony_ci	u32 reg;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	/* Setting SSC_EN to 1 */
5962306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM3);
6062306a36Sopenharmony_ci	reg = reg | SATA_PHY_SSC_EN;
6162306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM3);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM0) &
6462306a36Sopenharmony_ci			~(SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK |
6562306a36Sopenharmony_ci			  SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK |
6662306a36Sopenharmony_ci			  SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK);
6762306a36Sopenharmony_ci	reg |= SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(0xf);
6862306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM0);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM1) &
7162306a36Sopenharmony_ci			~(SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK |
7262306a36Sopenharmony_ci			  SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK |
7362306a36Sopenharmony_ci			  SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK);
7462306a36Sopenharmony_ci	reg |= SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(0x55) |
7562306a36Sopenharmony_ci		SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(0x55) |
7662306a36Sopenharmony_ci		SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(0x55);
7762306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM1);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM2) &
8062306a36Sopenharmony_ci		~SATA_PHY_P0_PARAM2_RX_EQ_MASK;
8162306a36Sopenharmony_ci	reg |= SATA_PHY_P0_PARAM2_RX_EQ(0x3);
8262306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM2);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	/* Setting PHY_RESET to 1 */
8562306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
8662306a36Sopenharmony_ci	reg = reg | SATA_PHY_RESET;
8762306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	/* Setting REF_SSP_EN to 1 */
9062306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
9162306a36Sopenharmony_ci	reg = reg | SATA_PHY_REF_SSP_EN | SATA_PHY_RESET;
9262306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/* make sure all changes complete before we let the PHY out of reset */
9562306a36Sopenharmony_ci	mb();
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* sleep for max. 50us more to combine processor wakeups */
9862306a36Sopenharmony_ci	usleep_range(20, 20 + 50);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/* Clearing PHY_RESET to 0 */
10162306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
10262306a36Sopenharmony_ci	reg = reg & ~SATA_PHY_RESET;
10362306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	return 0;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic int qcom_ipq806x_sata_phy_exit(struct phy *generic_phy)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
11162306a36Sopenharmony_ci	u32 reg;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	/* Setting PHY_RESET to 1 */
11462306a36Sopenharmony_ci	reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
11562306a36Sopenharmony_ci	reg = reg | SATA_PHY_RESET;
11662306a36Sopenharmony_ci	writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	return 0;
11962306a36Sopenharmony_ci}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistatic const struct phy_ops qcom_ipq806x_sata_phy_ops = {
12262306a36Sopenharmony_ci	.init		= qcom_ipq806x_sata_phy_init,
12362306a36Sopenharmony_ci	.exit		= qcom_ipq806x_sata_phy_exit,
12462306a36Sopenharmony_ci	.owner		= THIS_MODULE,
12562306a36Sopenharmony_ci};
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic int qcom_ipq806x_sata_phy_probe(struct platform_device *pdev)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy;
13062306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
13162306a36Sopenharmony_ci	struct phy_provider *phy_provider;
13262306a36Sopenharmony_ci	struct phy *generic_phy;
13362306a36Sopenharmony_ci	int ret;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
13662306a36Sopenharmony_ci	if (!phy)
13762306a36Sopenharmony_ci		return -ENOMEM;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	phy->mmio = devm_platform_ioremap_resource(pdev, 0);
14062306a36Sopenharmony_ci	if (IS_ERR(phy->mmio))
14162306a36Sopenharmony_ci		return PTR_ERR(phy->mmio);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	generic_phy = devm_phy_create(dev, NULL, &qcom_ipq806x_sata_phy_ops);
14462306a36Sopenharmony_ci	if (IS_ERR(generic_phy)) {
14562306a36Sopenharmony_ci		dev_err(dev, "%s: failed to create phy\n", __func__);
14662306a36Sopenharmony_ci		return PTR_ERR(generic_phy);
14762306a36Sopenharmony_ci	}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	phy->dev = dev;
15062306a36Sopenharmony_ci	phy_set_drvdata(generic_phy, phy);
15162306a36Sopenharmony_ci	platform_set_drvdata(pdev, phy);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	phy->cfg_clk = devm_clk_get(dev, "cfg");
15462306a36Sopenharmony_ci	if (IS_ERR(phy->cfg_clk)) {
15562306a36Sopenharmony_ci		dev_err(dev, "Failed to get sata cfg clock\n");
15662306a36Sopenharmony_ci		return PTR_ERR(phy->cfg_clk);
15762306a36Sopenharmony_ci	}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	ret = clk_prepare_enable(phy->cfg_clk);
16062306a36Sopenharmony_ci	if (ret)
16162306a36Sopenharmony_ci		return ret;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
16462306a36Sopenharmony_ci	if (IS_ERR(phy_provider)) {
16562306a36Sopenharmony_ci		clk_disable_unprepare(phy->cfg_clk);
16662306a36Sopenharmony_ci		dev_err(dev, "%s: failed to register phy\n", __func__);
16762306a36Sopenharmony_ci		return PTR_ERR(phy_provider);
16862306a36Sopenharmony_ci	}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	return 0;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic void qcom_ipq806x_sata_phy_remove(struct platform_device *pdev)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	struct qcom_ipq806x_sata_phy *phy = platform_get_drvdata(pdev);
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	clk_disable_unprepare(phy->cfg_clk);
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistatic const struct of_device_id qcom_ipq806x_sata_phy_of_match[] = {
18162306a36Sopenharmony_ci	{ .compatible = "qcom,ipq806x-sata-phy" },
18262306a36Sopenharmony_ci	{ },
18362306a36Sopenharmony_ci};
18462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, qcom_ipq806x_sata_phy_of_match);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cistatic struct platform_driver qcom_ipq806x_sata_phy_driver = {
18762306a36Sopenharmony_ci	.probe	= qcom_ipq806x_sata_phy_probe,
18862306a36Sopenharmony_ci	.remove_new = qcom_ipq806x_sata_phy_remove,
18962306a36Sopenharmony_ci	.driver = {
19062306a36Sopenharmony_ci		.name	= "qcom-ipq806x-sata-phy",
19162306a36Sopenharmony_ci		.of_match_table	= qcom_ipq806x_sata_phy_of_match,
19262306a36Sopenharmony_ci	}
19362306a36Sopenharmony_ci};
19462306a36Sopenharmony_cimodule_platform_driver(qcom_ipq806x_sata_phy_driver);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ciMODULE_DESCRIPTION("QCOM IPQ806x SATA PHY driver");
19762306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
198