18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014 Linaro Ltd.
48c2ecf20Sopenharmony_ci * Copyright (c) 2014 Hisilicon Limited.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/delay.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/regmap.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define SATA_PHY0_CTLL		0xa0
168c2ecf20Sopenharmony_ci#define MPLL_MULTIPLIER_SHIFT	1
178c2ecf20Sopenharmony_ci#define MPLL_MULTIPLIER_MASK	0xfe
188c2ecf20Sopenharmony_ci#define MPLL_MULTIPLIER_50M	0x3c
198c2ecf20Sopenharmony_ci#define MPLL_MULTIPLIER_100M	0x1e
208c2ecf20Sopenharmony_ci#define PHY_RESET		BIT(0)
218c2ecf20Sopenharmony_ci#define REF_SSP_EN		BIT(9)
228c2ecf20Sopenharmony_ci#define SSC_EN			BIT(10)
238c2ecf20Sopenharmony_ci#define REF_USE_PAD		BIT(23)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define SATA_PORT_PHYCTL	0x174
268c2ecf20Sopenharmony_ci#define SPEED_MODE_MASK		0x6f0000
278c2ecf20Sopenharmony_ci#define HALF_RATE_SHIFT		16
288c2ecf20Sopenharmony_ci#define PHY_CONFIG_SHIFT	18
298c2ecf20Sopenharmony_ci#define GEN2_EN_SHIFT		21
308c2ecf20Sopenharmony_ci#define SPEED_CTRL		BIT(20)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define SATA_PORT_PHYCTL1	0x148
338c2ecf20Sopenharmony_ci#define AMPLITUDE_MASK		0x3ffffe
348c2ecf20Sopenharmony_ci#define AMPLITUDE_GEN3		0x68
358c2ecf20Sopenharmony_ci#define AMPLITUDE_GEN3_SHIFT	15
368c2ecf20Sopenharmony_ci#define AMPLITUDE_GEN2		0x56
378c2ecf20Sopenharmony_ci#define AMPLITUDE_GEN2_SHIFT	8
388c2ecf20Sopenharmony_ci#define AMPLITUDE_GEN1		0x56
398c2ecf20Sopenharmony_ci#define AMPLITUDE_GEN1_SHIFT	1
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define SATA_PORT_PHYCTL2	0x14c
428c2ecf20Sopenharmony_ci#define PREEMPH_MASK		0x3ffff
438c2ecf20Sopenharmony_ci#define PREEMPH_GEN3		0x20
448c2ecf20Sopenharmony_ci#define PREEMPH_GEN3_SHIFT	12
458c2ecf20Sopenharmony_ci#define PREEMPH_GEN2		0x15
468c2ecf20Sopenharmony_ci#define PREEMPH_GEN2_SHIFT	6
478c2ecf20Sopenharmony_ci#define PREEMPH_GEN1		0x5
488c2ecf20Sopenharmony_ci#define PREEMPH_GEN1_SHIFT	0
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistruct hix5hd2_priv {
518c2ecf20Sopenharmony_ci	void __iomem	*base;
528c2ecf20Sopenharmony_ci	struct regmap	*peri_ctrl;
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cienum phy_speed_mode {
568c2ecf20Sopenharmony_ci	SPEED_MODE_GEN1 = 0,
578c2ecf20Sopenharmony_ci	SPEED_MODE_GEN2 = 1,
588c2ecf20Sopenharmony_ci	SPEED_MODE_GEN3 = 2,
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int hix5hd2_sata_phy_init(struct phy *phy)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = phy_get_drvdata(phy);
648c2ecf20Sopenharmony_ci	u32 val, data[2];
658c2ecf20Sopenharmony_ci	int ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (priv->peri_ctrl) {
688c2ecf20Sopenharmony_ci		ret = of_property_read_u32_array(phy->dev.of_node,
698c2ecf20Sopenharmony_ci						 "hisilicon,power-reg",
708c2ecf20Sopenharmony_ci						 &data[0], 2);
718c2ecf20Sopenharmony_ci		if (ret) {
728c2ecf20Sopenharmony_ci			dev_err(&phy->dev, "Fail read hisilicon,power-reg\n");
738c2ecf20Sopenharmony_ci			return ret;
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		regmap_update_bits(priv->peri_ctrl, data[0],
778c2ecf20Sopenharmony_ci				   BIT(data[1]), BIT(data[1]));
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* reset phy */
818c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + SATA_PHY0_CTLL);
828c2ecf20Sopenharmony_ci	val &= ~(MPLL_MULTIPLIER_MASK | REF_USE_PAD);
838c2ecf20Sopenharmony_ci	val |= MPLL_MULTIPLIER_50M << MPLL_MULTIPLIER_SHIFT |
848c2ecf20Sopenharmony_ci	       REF_SSP_EN | PHY_RESET;
858c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PHY0_CTLL);
868c2ecf20Sopenharmony_ci	msleep(20);
878c2ecf20Sopenharmony_ci	val &= ~PHY_RESET;
888c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PHY0_CTLL);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + SATA_PORT_PHYCTL1);
918c2ecf20Sopenharmony_ci	val &= ~AMPLITUDE_MASK;
928c2ecf20Sopenharmony_ci	val |= AMPLITUDE_GEN3 << AMPLITUDE_GEN3_SHIFT |
938c2ecf20Sopenharmony_ci	       AMPLITUDE_GEN2 << AMPLITUDE_GEN2_SHIFT |
948c2ecf20Sopenharmony_ci	       AMPLITUDE_GEN1 << AMPLITUDE_GEN1_SHIFT;
958c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PORT_PHYCTL1);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + SATA_PORT_PHYCTL2);
988c2ecf20Sopenharmony_ci	val &= ~PREEMPH_MASK;
998c2ecf20Sopenharmony_ci	val |= PREEMPH_GEN3 << PREEMPH_GEN3_SHIFT |
1008c2ecf20Sopenharmony_ci	       PREEMPH_GEN2 << PREEMPH_GEN2_SHIFT |
1018c2ecf20Sopenharmony_ci	       PREEMPH_GEN1 << PREEMPH_GEN1_SHIFT;
1028c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PORT_PHYCTL2);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* ensure PHYCTRL setting takes effect */
1058c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + SATA_PORT_PHYCTL);
1068c2ecf20Sopenharmony_ci	val &= ~SPEED_MODE_MASK;
1078c2ecf20Sopenharmony_ci	val |= SPEED_MODE_GEN1 << HALF_RATE_SHIFT |
1088c2ecf20Sopenharmony_ci	       SPEED_MODE_GEN1 << PHY_CONFIG_SHIFT |
1098c2ecf20Sopenharmony_ci	       SPEED_MODE_GEN1 << GEN2_EN_SHIFT | SPEED_CTRL;
1108c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PORT_PHYCTL);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	msleep(20);
1138c2ecf20Sopenharmony_ci	val &= ~SPEED_MODE_MASK;
1148c2ecf20Sopenharmony_ci	val |= SPEED_MODE_GEN3 << HALF_RATE_SHIFT |
1158c2ecf20Sopenharmony_ci	       SPEED_MODE_GEN3 << PHY_CONFIG_SHIFT |
1168c2ecf20Sopenharmony_ci	       SPEED_MODE_GEN3 << GEN2_EN_SHIFT | SPEED_CTRL;
1178c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PORT_PHYCTL);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	val &= ~(SPEED_MODE_MASK | SPEED_CTRL);
1208c2ecf20Sopenharmony_ci	val |= SPEED_MODE_GEN2 << HALF_RATE_SHIFT |
1218c2ecf20Sopenharmony_ci	       SPEED_MODE_GEN2 << PHY_CONFIG_SHIFT |
1228c2ecf20Sopenharmony_ci	       SPEED_MODE_GEN2 << GEN2_EN_SHIFT;
1238c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + SATA_PORT_PHYCTL);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	return 0;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic const struct phy_ops hix5hd2_sata_phy_ops = {
1298c2ecf20Sopenharmony_ci	.init		= hix5hd2_sata_phy_init,
1308c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int hix5hd2_sata_phy_probe(struct platform_device *pdev)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
1368c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1378c2ecf20Sopenharmony_ci	struct resource *res;
1388c2ecf20Sopenharmony_ci	struct phy *phy;
1398c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1428c2ecf20Sopenharmony_ci	if (!priv)
1438c2ecf20Sopenharmony_ci		return -ENOMEM;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1468c2ecf20Sopenharmony_ci	if (!res)
1478c2ecf20Sopenharmony_ci		return -EINVAL;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	priv->base = devm_ioremap(dev, res->start, resource_size(res));
1508c2ecf20Sopenharmony_ci	if (!priv->base)
1518c2ecf20Sopenharmony_ci		return -ENOMEM;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	priv->peri_ctrl = syscon_regmap_lookup_by_phandle(dev->of_node,
1548c2ecf20Sopenharmony_ci					"hisilicon,peripheral-syscon");
1558c2ecf20Sopenharmony_ci	if (IS_ERR(priv->peri_ctrl))
1568c2ecf20Sopenharmony_ci		priv->peri_ctrl = NULL;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	phy = devm_phy_create(dev, NULL, &hix5hd2_sata_phy_ops);
1598c2ecf20Sopenharmony_ci	if (IS_ERR(phy)) {
1608c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create PHY\n");
1618c2ecf20Sopenharmony_ci		return PTR_ERR(phy);
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	phy_set_drvdata(phy, priv);
1658c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1668c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic const struct of_device_id hix5hd2_sata_phy_of_match[] = {
1708c2ecf20Sopenharmony_ci	{.compatible = "hisilicon,hix5hd2-sata-phy",},
1718c2ecf20Sopenharmony_ci	{ },
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hix5hd2_sata_phy_of_match);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct platform_driver hix5hd2_sata_phy_driver = {
1768c2ecf20Sopenharmony_ci	.probe	= hix5hd2_sata_phy_probe,
1778c2ecf20Sopenharmony_ci	.driver = {
1788c2ecf20Sopenharmony_ci		.name	= "hix5hd2-sata-phy",
1798c2ecf20Sopenharmony_ci		.of_match_table	= hix5hd2_sata_phy_of_match,
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_cimodule_platform_driver(hix5hd2_sata_phy_driver);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jiancheng Xue <xuejiancheng@huawei.com>");
1858c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HISILICON HIX5HD2 SATA PHY driver");
1868c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:hix5hd2-sata-phy");
1878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
188