18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Allwinner sun50i(H6) USB 3.0 phy driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Icenowy Zheng <icenowy@aosc.io>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on phy-sun9i-usb.c, which is:
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2014-2015 Chen-Yu Tsai <wens@csie.org>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Based on code from Allwinner BSP, which is:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright (c) 2010-2015 Allwinner Technology Co., Ltd.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/clk.h>
178c2ecf20Sopenharmony_ci#include <linux/err.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
218c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
228c2ecf20Sopenharmony_ci#include <linux/reset.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Interface Status and Control Registers */
258c2ecf20Sopenharmony_ci#define SUNXI_ISCR			0x00
268c2ecf20Sopenharmony_ci#define SUNXI_PIPE_CLOCK_CONTROL	0x14
278c2ecf20Sopenharmony_ci#define SUNXI_PHY_TUNE_LOW		0x18
288c2ecf20Sopenharmony_ci#define SUNXI_PHY_TUNE_HIGH		0x1c
298c2ecf20Sopenharmony_ci#define SUNXI_PHY_EXTERNAL_CONTROL	0x20
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* USB2.0 Interface Status and Control Register */
328c2ecf20Sopenharmony_ci#define SUNXI_ISCR_FORCE_VBUS		(3 << 12)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* PIPE Clock Control Register */
358c2ecf20Sopenharmony_ci#define SUNXI_PCC_PIPE_CLK_OPEN		(1 << 6)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* PHY External Control Register */
388c2ecf20Sopenharmony_ci#define SUNXI_PEC_EXTERN_VBUS		(3 << 1)
398c2ecf20Sopenharmony_ci#define SUNXI_PEC_SSC_EN		(1 << 24)
408c2ecf20Sopenharmony_ci#define SUNXI_PEC_REF_SSP_EN		(1 << 26)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* PHY Tune High Register */
438c2ecf20Sopenharmony_ci#define SUNXI_TX_DEEMPH_3P5DB(n)	((n) << 19)
448c2ecf20Sopenharmony_ci#define SUNXI_TX_DEEMPH_3P5DB_MASK	GENMASK(24, 19)
458c2ecf20Sopenharmony_ci#define SUNXI_TX_DEEMPH_6DB(n)		((n) << 13)
468c2ecf20Sopenharmony_ci#define SUNXI_TX_DEEMPH_6GB_MASK	GENMASK(18, 13)
478c2ecf20Sopenharmony_ci#define SUNXI_TX_SWING_FULL(n)		((n) << 6)
488c2ecf20Sopenharmony_ci#define SUNXI_TX_SWING_FULL_MASK	GENMASK(12, 6)
498c2ecf20Sopenharmony_ci#define SUNXI_LOS_BIAS(n)		((n) << 3)
508c2ecf20Sopenharmony_ci#define SUNXI_LOS_BIAS_MASK		GENMASK(5, 3)
518c2ecf20Sopenharmony_ci#define SUNXI_TXVBOOSTLVL(n)		((n) << 0)
528c2ecf20Sopenharmony_ci#define SUNXI_TXVBOOSTLVL_MASK		GENMASK(2, 0)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistruct sun50i_usb3_phy {
558c2ecf20Sopenharmony_ci	struct phy *phy;
568c2ecf20Sopenharmony_ci	void __iomem *regs;
578c2ecf20Sopenharmony_ci	struct reset_control *reset;
588c2ecf20Sopenharmony_ci	struct clk *clk;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic void sun50i_usb3_phy_open(struct sun50i_usb3_phy *phy)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	u32 val;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	val = readl(phy->regs + SUNXI_PHY_EXTERNAL_CONTROL);
668c2ecf20Sopenharmony_ci	val |= SUNXI_PEC_EXTERN_VBUS;
678c2ecf20Sopenharmony_ci	val |= SUNXI_PEC_SSC_EN | SUNXI_PEC_REF_SSP_EN;
688c2ecf20Sopenharmony_ci	writel(val, phy->regs + SUNXI_PHY_EXTERNAL_CONTROL);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	val = readl(phy->regs + SUNXI_PIPE_CLOCK_CONTROL);
718c2ecf20Sopenharmony_ci	val |= SUNXI_PCC_PIPE_CLK_OPEN;
728c2ecf20Sopenharmony_ci	writel(val, phy->regs + SUNXI_PIPE_CLOCK_CONTROL);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	val = readl(phy->regs + SUNXI_ISCR);
758c2ecf20Sopenharmony_ci	val |= SUNXI_ISCR_FORCE_VBUS;
768c2ecf20Sopenharmony_ci	writel(val, phy->regs + SUNXI_ISCR);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/*
798c2ecf20Sopenharmony_ci	 * All the magic numbers written to the PHY_TUNE_{LOW_HIGH}
808c2ecf20Sopenharmony_ci	 * registers are directly taken from the BSP USB3 driver from
818c2ecf20Sopenharmony_ci	 * Allwiner.
828c2ecf20Sopenharmony_ci	 */
838c2ecf20Sopenharmony_ci	writel(0x0047fc87, phy->regs + SUNXI_PHY_TUNE_LOW);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	val = readl(phy->regs + SUNXI_PHY_TUNE_HIGH);
868c2ecf20Sopenharmony_ci	val &= ~(SUNXI_TXVBOOSTLVL_MASK | SUNXI_LOS_BIAS_MASK |
878c2ecf20Sopenharmony_ci		 SUNXI_TX_SWING_FULL_MASK | SUNXI_TX_DEEMPH_6GB_MASK |
888c2ecf20Sopenharmony_ci		 SUNXI_TX_DEEMPH_3P5DB_MASK);
898c2ecf20Sopenharmony_ci	val |= SUNXI_TXVBOOSTLVL(0x7);
908c2ecf20Sopenharmony_ci	val |= SUNXI_LOS_BIAS(0x7);
918c2ecf20Sopenharmony_ci	val |= SUNXI_TX_SWING_FULL(0x55);
928c2ecf20Sopenharmony_ci	val |= SUNXI_TX_DEEMPH_6DB(0x20);
938c2ecf20Sopenharmony_ci	val |= SUNXI_TX_DEEMPH_3P5DB(0x15);
948c2ecf20Sopenharmony_ci	writel(val, phy->regs + SUNXI_PHY_TUNE_HIGH);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int sun50i_usb3_phy_init(struct phy *_phy)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct sun50i_usb3_phy *phy = phy_get_drvdata(_phy);
1008c2ecf20Sopenharmony_ci	int ret;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(phy->clk);
1038c2ecf20Sopenharmony_ci	if (ret)
1048c2ecf20Sopenharmony_ci		return ret;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	ret = reset_control_deassert(phy->reset);
1078c2ecf20Sopenharmony_ci	if (ret) {
1088c2ecf20Sopenharmony_ci		clk_disable_unprepare(phy->clk);
1098c2ecf20Sopenharmony_ci		return ret;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	sun50i_usb3_phy_open(phy);
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int sun50i_usb3_phy_exit(struct phy *_phy)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct sun50i_usb3_phy *phy = phy_get_drvdata(_phy);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	reset_control_assert(phy->reset);
1218c2ecf20Sopenharmony_ci	clk_disable_unprepare(phy->clk);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return 0;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic const struct phy_ops sun50i_usb3_phy_ops = {
1278c2ecf20Sopenharmony_ci	.init		= sun50i_usb3_phy_init,
1288c2ecf20Sopenharmony_ci	.exit		= sun50i_usb3_phy_exit,
1298c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic int sun50i_usb3_phy_probe(struct platform_device *pdev)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct sun50i_usb3_phy *phy;
1358c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1368c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
1378c2ecf20Sopenharmony_ci	struct resource *res;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
1408c2ecf20Sopenharmony_ci	if (!phy)
1418c2ecf20Sopenharmony_ci		return -ENOMEM;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	phy->clk = devm_clk_get(dev, NULL);
1448c2ecf20Sopenharmony_ci	if (IS_ERR(phy->clk)) {
1458c2ecf20Sopenharmony_ci		if (PTR_ERR(phy->clk) != -EPROBE_DEFER)
1468c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get phy clock\n");
1478c2ecf20Sopenharmony_ci		return PTR_ERR(phy->clk);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	phy->reset = devm_reset_control_get(dev, NULL);
1518c2ecf20Sopenharmony_ci	if (IS_ERR(phy->reset)) {
1528c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get reset control\n");
1538c2ecf20Sopenharmony_ci		return PTR_ERR(phy->reset);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1578c2ecf20Sopenharmony_ci	phy->regs = devm_ioremap_resource(dev, res);
1588c2ecf20Sopenharmony_ci	if (IS_ERR(phy->regs))
1598c2ecf20Sopenharmony_ci		return PTR_ERR(phy->regs);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	phy->phy = devm_phy_create(dev, NULL, &sun50i_usb3_phy_ops);
1628c2ecf20Sopenharmony_ci	if (IS_ERR(phy->phy)) {
1638c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create PHY\n");
1648c2ecf20Sopenharmony_ci		return PTR_ERR(phy->phy);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	phy_set_drvdata(phy->phy, phy);
1688c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic const struct of_device_id sun50i_usb3_phy_of_match[] = {
1748c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun50i-h6-usb3-phy" },
1758c2ecf20Sopenharmony_ci	{ },
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sun50i_usb3_phy_of_match);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic struct platform_driver sun50i_usb3_phy_driver = {
1808c2ecf20Sopenharmony_ci	.probe	= sun50i_usb3_phy_probe,
1818c2ecf20Sopenharmony_ci	.driver = {
1828c2ecf20Sopenharmony_ci		.of_match_table	= sun50i_usb3_phy_of_match,
1838c2ecf20Sopenharmony_ci		.name  = "sun50i-usb3-phy",
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_cimodule_platform_driver(sun50i_usb3_phy_driver);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner H6 USB 3.0 phy driver");
1898c2ecf20Sopenharmony_ciMODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
1908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
191