18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Linaro, Ltd.
48c2ecf20Sopenharmony_ci * Rob Herring <robh@kernel.org>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Based on vendor driver:
78c2ecf20Sopenharmony_ci * Copyright (C) 2013 Marvell Inc.
88c2ecf20Sopenharmony_ci * Author: Chao Xie <xiechao.mail@gmail.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/delay.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/of.h>
148c2ecf20Sopenharmony_ci#include <linux/io.h>
158c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/clk.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_CTRL			0x08
238c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_IMPCAL_CAL		0x18
248c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_PLL_CTRL01		0x1c
258c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_PLL_CTRL2			0x20
268c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_INT			0x28
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_PLL_SELLPFR_SHIFT		26
298c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_PLL_FBDIV_SHIFT		0
308c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_PLL_REFDIV_SHIFT		9
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_S2H_PU_PLL		BIT(10)
338c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_H2S_PLL_LOCK		BIT(15)
348c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_S2H_HSIC_EN		BIT(7)
358c2ecf20Sopenharmony_ci#define S2H_DRV_SE0_4RESUME			BIT(14)
368c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_H2S_IMPCAL_DONE		BIT(27)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_CONNECT_INT		BIT(1)
398c2ecf20Sopenharmony_ci#define PHY_28NM_HSIC_HS_READY_INT		BIT(2)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct mv_hsic_phy {
428c2ecf20Sopenharmony_ci	struct phy		*phy;
438c2ecf20Sopenharmony_ci	struct platform_device	*pdev;
448c2ecf20Sopenharmony_ci	void __iomem		*base;
458c2ecf20Sopenharmony_ci	struct clk		*clk;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic int wait_for_reg(void __iomem *reg, u32 mask, u32 ms)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	u32 val;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	return readl_poll_timeout(reg, val, ((val & mask) == mask),
538c2ecf20Sopenharmony_ci				  1000, 1000 * ms);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int mv_hsic_phy_init(struct phy *phy)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct mv_hsic_phy *mv_phy = phy_get_drvdata(phy);
598c2ecf20Sopenharmony_ci	struct platform_device *pdev = mv_phy->pdev;
608c2ecf20Sopenharmony_ci	void __iomem *base = mv_phy->base;
618c2ecf20Sopenharmony_ci	int ret;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	clk_prepare_enable(mv_phy->clk);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/* Set reference clock */
668c2ecf20Sopenharmony_ci	writel(0x1 << PHY_28NM_HSIC_PLL_SELLPFR_SHIFT |
678c2ecf20Sopenharmony_ci		0xf0 << PHY_28NM_HSIC_PLL_FBDIV_SHIFT |
688c2ecf20Sopenharmony_ci		0xd << PHY_28NM_HSIC_PLL_REFDIV_SHIFT,
698c2ecf20Sopenharmony_ci		base + PHY_28NM_HSIC_PLL_CTRL01);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Turn on PLL */
728c2ecf20Sopenharmony_ci	writel(readl(base + PHY_28NM_HSIC_PLL_CTRL2) |
738c2ecf20Sopenharmony_ci		PHY_28NM_HSIC_S2H_PU_PLL,
748c2ecf20Sopenharmony_ci		base + PHY_28NM_HSIC_PLL_CTRL2);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* Make sure PHY PLL is locked */
778c2ecf20Sopenharmony_ci	ret = wait_for_reg(base + PHY_28NM_HSIC_PLL_CTRL2,
788c2ecf20Sopenharmony_ci			   PHY_28NM_HSIC_H2S_PLL_LOCK, 100);
798c2ecf20Sopenharmony_ci	if (ret) {
808c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "HSIC PHY PLL not locked after 100mS.");
818c2ecf20Sopenharmony_ci		clk_disable_unprepare(mv_phy->clk);
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return ret;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int mv_hsic_phy_power_on(struct phy *phy)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct mv_hsic_phy *mv_phy = phy_get_drvdata(phy);
908c2ecf20Sopenharmony_ci	struct platform_device *pdev = mv_phy->pdev;
918c2ecf20Sopenharmony_ci	void __iomem *base = mv_phy->base;
928c2ecf20Sopenharmony_ci	u32 reg;
938c2ecf20Sopenharmony_ci	int ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	reg = readl(base + PHY_28NM_HSIC_CTRL);
968c2ecf20Sopenharmony_ci	/* Avoid SE0 state when resume for some device will take it as reset */
978c2ecf20Sopenharmony_ci	reg &= ~S2H_DRV_SE0_4RESUME;
988c2ecf20Sopenharmony_ci	reg |= PHY_28NM_HSIC_S2H_HSIC_EN;	/* Enable HSIC PHY */
998c2ecf20Sopenharmony_ci	writel(reg, base + PHY_28NM_HSIC_CTRL);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/*
1028c2ecf20Sopenharmony_ci	 *  Calibration Timing
1038c2ecf20Sopenharmony_ci	 *		   ____________________________
1048c2ecf20Sopenharmony_ci	 *  CAL START   ___|
1058c2ecf20Sopenharmony_ci	 *			   ____________________
1068c2ecf20Sopenharmony_ci	 *  CAL_DONE    ___________|
1078c2ecf20Sopenharmony_ci	 *		   | 400us |
1088c2ecf20Sopenharmony_ci	 */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Make sure PHY Calibration is ready */
1118c2ecf20Sopenharmony_ci	ret = wait_for_reg(base + PHY_28NM_HSIC_IMPCAL_CAL,
1128c2ecf20Sopenharmony_ci			   PHY_28NM_HSIC_H2S_IMPCAL_DONE, 100);
1138c2ecf20Sopenharmony_ci	if (ret) {
1148c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "HSIC PHY READY not set after 100mS.");
1158c2ecf20Sopenharmony_ci		return ret;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Waiting for HSIC connect int*/
1198c2ecf20Sopenharmony_ci	ret = wait_for_reg(base + PHY_28NM_HSIC_INT,
1208c2ecf20Sopenharmony_ci			   PHY_28NM_HSIC_CONNECT_INT, 200);
1218c2ecf20Sopenharmony_ci	if (ret)
1228c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "HSIC wait for connect interrupt timeout.");
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return ret;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic int mv_hsic_phy_power_off(struct phy *phy)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct mv_hsic_phy *mv_phy = phy_get_drvdata(phy);
1308c2ecf20Sopenharmony_ci	void __iomem *base = mv_phy->base;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	writel(readl(base + PHY_28NM_HSIC_CTRL) & ~PHY_28NM_HSIC_S2H_HSIC_EN,
1338c2ecf20Sopenharmony_ci		base + PHY_28NM_HSIC_CTRL);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int mv_hsic_phy_exit(struct phy *phy)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct mv_hsic_phy *mv_phy = phy_get_drvdata(phy);
1418c2ecf20Sopenharmony_ci	void __iomem *base = mv_phy->base;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* Turn off PLL */
1448c2ecf20Sopenharmony_ci	writel(readl(base + PHY_28NM_HSIC_PLL_CTRL2) &
1458c2ecf20Sopenharmony_ci		~PHY_28NM_HSIC_S2H_PU_PLL,
1468c2ecf20Sopenharmony_ci		base + PHY_28NM_HSIC_PLL_CTRL2);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	clk_disable_unprepare(mv_phy->clk);
1498c2ecf20Sopenharmony_ci	return 0;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic const struct phy_ops hsic_ops = {
1548c2ecf20Sopenharmony_ci	.init		= mv_hsic_phy_init,
1558c2ecf20Sopenharmony_ci	.power_on	= mv_hsic_phy_power_on,
1568c2ecf20Sopenharmony_ci	.power_off	= mv_hsic_phy_power_off,
1578c2ecf20Sopenharmony_ci	.exit		= mv_hsic_phy_exit,
1588c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int mv_hsic_phy_probe(struct platform_device *pdev)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
1648c2ecf20Sopenharmony_ci	struct mv_hsic_phy *mv_phy;
1658c2ecf20Sopenharmony_ci	struct resource *r;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	mv_phy = devm_kzalloc(&pdev->dev, sizeof(*mv_phy), GFP_KERNEL);
1688c2ecf20Sopenharmony_ci	if (!mv_phy)
1698c2ecf20Sopenharmony_ci		return -ENOMEM;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	mv_phy->pdev = pdev;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	mv_phy->clk = devm_clk_get(&pdev->dev, NULL);
1748c2ecf20Sopenharmony_ci	if (IS_ERR(mv_phy->clk)) {
1758c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get clock.\n");
1768c2ecf20Sopenharmony_ci		return PTR_ERR(mv_phy->clk);
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1808c2ecf20Sopenharmony_ci	mv_phy->base = devm_ioremap_resource(&pdev->dev, r);
1818c2ecf20Sopenharmony_ci	if (IS_ERR(mv_phy->base))
1828c2ecf20Sopenharmony_ci		return PTR_ERR(mv_phy->base);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	mv_phy->phy = devm_phy_create(&pdev->dev, pdev->dev.of_node, &hsic_ops);
1858c2ecf20Sopenharmony_ci	if (IS_ERR(mv_phy->phy))
1868c2ecf20Sopenharmony_ci		return PTR_ERR(mv_phy->phy);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	phy_set_drvdata(mv_phy->phy, mv_phy);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
1918c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic const struct of_device_id mv_hsic_phy_dt_match[] = {
1958c2ecf20Sopenharmony_ci	{ .compatible = "marvell,pxa1928-hsic-phy", },
1968c2ecf20Sopenharmony_ci	{},
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mv_hsic_phy_dt_match);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic struct platform_driver mv_hsic_phy_driver = {
2018c2ecf20Sopenharmony_ci	.probe	= mv_hsic_phy_probe,
2028c2ecf20Sopenharmony_ci	.driver = {
2038c2ecf20Sopenharmony_ci		.name   = "mv-hsic-phy",
2048c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(mv_hsic_phy_dt_match),
2058c2ecf20Sopenharmony_ci	},
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_cimodule_platform_driver(mv_hsic_phy_driver);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rob Herring <robh@kernel.org>");
2108c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Marvell HSIC phy driver");
2118c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
212