162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Allwinner sun50i(H6) USB 3.0 phy driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2017 Icenowy Zheng <icenowy@aosc.io>
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Based on phy-sun9i-usb.c, which is:
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (C) 2014-2015 Chen-Yu Tsai <wens@csie.org>
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * Based on code from Allwinner BSP, which is:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Copyright (c) 2010-2015 Allwinner Technology Co., Ltd.
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <linux/clk.h>
1762306a36Sopenharmony_ci#include <linux/err.h>
1862306a36Sopenharmony_ci#include <linux/io.h>
1962306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
2062306a36Sopenharmony_ci#include <linux/module.h>
2162306a36Sopenharmony_ci#include <linux/phy/phy.h>
2262306a36Sopenharmony_ci#include <linux/platform_device.h>
2362306a36Sopenharmony_ci#include <linux/reset.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/* Interface Status and Control Registers */
2662306a36Sopenharmony_ci#define SUNXI_ISCR			0x00
2762306a36Sopenharmony_ci#define SUNXI_PIPE_CLOCK_CONTROL	0x14
2862306a36Sopenharmony_ci#define SUNXI_PHY_TUNE_LOW		0x18
2962306a36Sopenharmony_ci#define SUNXI_PHY_TUNE_HIGH		0x1c
3062306a36Sopenharmony_ci#define SUNXI_PHY_EXTERNAL_CONTROL	0x20
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* USB2.0 Interface Status and Control Register */
3362306a36Sopenharmony_ci#define SUNXI_ISCR_FORCE_VBUS		(3 << 12)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* PIPE Clock Control Register */
3662306a36Sopenharmony_ci#define SUNXI_PCC_PIPE_CLK_OPEN		(1 << 6)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* PHY External Control Register */
3962306a36Sopenharmony_ci#define SUNXI_PEC_EXTERN_VBUS		(3 << 1)
4062306a36Sopenharmony_ci#define SUNXI_PEC_SSC_EN		(1 << 24)
4162306a36Sopenharmony_ci#define SUNXI_PEC_REF_SSP_EN		(1 << 26)
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* PHY Tune High Register */
4462306a36Sopenharmony_ci#define SUNXI_TX_DEEMPH_3P5DB(n)	((n) << 19)
4562306a36Sopenharmony_ci#define SUNXI_TX_DEEMPH_3P5DB_MASK	GENMASK(24, 19)
4662306a36Sopenharmony_ci#define SUNXI_TX_DEEMPH_6DB(n)		((n) << 13)
4762306a36Sopenharmony_ci#define SUNXI_TX_DEEMPH_6GB_MASK	GENMASK(18, 13)
4862306a36Sopenharmony_ci#define SUNXI_TX_SWING_FULL(n)		((n) << 6)
4962306a36Sopenharmony_ci#define SUNXI_TX_SWING_FULL_MASK	GENMASK(12, 6)
5062306a36Sopenharmony_ci#define SUNXI_LOS_BIAS(n)		((n) << 3)
5162306a36Sopenharmony_ci#define SUNXI_LOS_BIAS_MASK		GENMASK(5, 3)
5262306a36Sopenharmony_ci#define SUNXI_TXVBOOSTLVL(n)		((n) << 0)
5362306a36Sopenharmony_ci#define SUNXI_TXVBOOSTLVL_MASK		GENMASK(2, 0)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistruct sun50i_usb3_phy {
5662306a36Sopenharmony_ci	struct phy *phy;
5762306a36Sopenharmony_ci	void __iomem *regs;
5862306a36Sopenharmony_ci	struct reset_control *reset;
5962306a36Sopenharmony_ci	struct clk *clk;
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic void sun50i_usb3_phy_open(struct sun50i_usb3_phy *phy)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	u32 val;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	val = readl(phy->regs + SUNXI_PHY_EXTERNAL_CONTROL);
6762306a36Sopenharmony_ci	val |= SUNXI_PEC_EXTERN_VBUS;
6862306a36Sopenharmony_ci	val |= SUNXI_PEC_SSC_EN | SUNXI_PEC_REF_SSP_EN;
6962306a36Sopenharmony_ci	writel(val, phy->regs + SUNXI_PHY_EXTERNAL_CONTROL);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	val = readl(phy->regs + SUNXI_PIPE_CLOCK_CONTROL);
7262306a36Sopenharmony_ci	val |= SUNXI_PCC_PIPE_CLK_OPEN;
7362306a36Sopenharmony_ci	writel(val, phy->regs + SUNXI_PIPE_CLOCK_CONTROL);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	val = readl(phy->regs + SUNXI_ISCR);
7662306a36Sopenharmony_ci	val |= SUNXI_ISCR_FORCE_VBUS;
7762306a36Sopenharmony_ci	writel(val, phy->regs + SUNXI_ISCR);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/*
8062306a36Sopenharmony_ci	 * All the magic numbers written to the PHY_TUNE_{LOW_HIGH}
8162306a36Sopenharmony_ci	 * registers are directly taken from the BSP USB3 driver from
8262306a36Sopenharmony_ci	 * Allwiner.
8362306a36Sopenharmony_ci	 */
8462306a36Sopenharmony_ci	writel(0x0047fc87, phy->regs + SUNXI_PHY_TUNE_LOW);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	val = readl(phy->regs + SUNXI_PHY_TUNE_HIGH);
8762306a36Sopenharmony_ci	val &= ~(SUNXI_TXVBOOSTLVL_MASK | SUNXI_LOS_BIAS_MASK |
8862306a36Sopenharmony_ci		 SUNXI_TX_SWING_FULL_MASK | SUNXI_TX_DEEMPH_6GB_MASK |
8962306a36Sopenharmony_ci		 SUNXI_TX_DEEMPH_3P5DB_MASK);
9062306a36Sopenharmony_ci	val |= SUNXI_TXVBOOSTLVL(0x7);
9162306a36Sopenharmony_ci	val |= SUNXI_LOS_BIAS(0x7);
9262306a36Sopenharmony_ci	val |= SUNXI_TX_SWING_FULL(0x55);
9362306a36Sopenharmony_ci	val |= SUNXI_TX_DEEMPH_6DB(0x20);
9462306a36Sopenharmony_ci	val |= SUNXI_TX_DEEMPH_3P5DB(0x15);
9562306a36Sopenharmony_ci	writel(val, phy->regs + SUNXI_PHY_TUNE_HIGH);
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic int sun50i_usb3_phy_init(struct phy *_phy)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	struct sun50i_usb3_phy *phy = phy_get_drvdata(_phy);
10162306a36Sopenharmony_ci	int ret;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	ret = clk_prepare_enable(phy->clk);
10462306a36Sopenharmony_ci	if (ret)
10562306a36Sopenharmony_ci		return ret;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	ret = reset_control_deassert(phy->reset);
10862306a36Sopenharmony_ci	if (ret) {
10962306a36Sopenharmony_ci		clk_disable_unprepare(phy->clk);
11062306a36Sopenharmony_ci		return ret;
11162306a36Sopenharmony_ci	}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	sun50i_usb3_phy_open(phy);
11462306a36Sopenharmony_ci	return 0;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic int sun50i_usb3_phy_exit(struct phy *_phy)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	struct sun50i_usb3_phy *phy = phy_get_drvdata(_phy);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	reset_control_assert(phy->reset);
12262306a36Sopenharmony_ci	clk_disable_unprepare(phy->clk);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	return 0;
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic const struct phy_ops sun50i_usb3_phy_ops = {
12862306a36Sopenharmony_ci	.init		= sun50i_usb3_phy_init,
12962306a36Sopenharmony_ci	.exit		= sun50i_usb3_phy_exit,
13062306a36Sopenharmony_ci	.owner		= THIS_MODULE,
13162306a36Sopenharmony_ci};
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic int sun50i_usb3_phy_probe(struct platform_device *pdev)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct sun50i_usb3_phy *phy;
13662306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
13762306a36Sopenharmony_ci	struct phy_provider *phy_provider;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
14062306a36Sopenharmony_ci	if (!phy)
14162306a36Sopenharmony_ci		return -ENOMEM;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	phy->clk = devm_clk_get(dev, NULL);
14462306a36Sopenharmony_ci	if (IS_ERR(phy->clk)) {
14562306a36Sopenharmony_ci		if (PTR_ERR(phy->clk) != -EPROBE_DEFER)
14662306a36Sopenharmony_ci			dev_err(dev, "failed to get phy clock\n");
14762306a36Sopenharmony_ci		return PTR_ERR(phy->clk);
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	phy->reset = devm_reset_control_get(dev, NULL);
15162306a36Sopenharmony_ci	if (IS_ERR(phy->reset)) {
15262306a36Sopenharmony_ci		dev_err(dev, "failed to get reset control\n");
15362306a36Sopenharmony_ci		return PTR_ERR(phy->reset);
15462306a36Sopenharmony_ci	}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	phy->regs = devm_platform_ioremap_resource(pdev, 0);
15762306a36Sopenharmony_ci	if (IS_ERR(phy->regs))
15862306a36Sopenharmony_ci		return PTR_ERR(phy->regs);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	phy->phy = devm_phy_create(dev, NULL, &sun50i_usb3_phy_ops);
16162306a36Sopenharmony_ci	if (IS_ERR(phy->phy)) {
16262306a36Sopenharmony_ci		dev_err(dev, "failed to create PHY\n");
16362306a36Sopenharmony_ci		return PTR_ERR(phy->phy);
16462306a36Sopenharmony_ci	}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	phy_set_drvdata(phy->phy, phy);
16762306a36Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_cistatic const struct of_device_id sun50i_usb3_phy_of_match[] = {
17362306a36Sopenharmony_ci	{ .compatible = "allwinner,sun50i-h6-usb3-phy" },
17462306a36Sopenharmony_ci	{ },
17562306a36Sopenharmony_ci};
17662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sun50i_usb3_phy_of_match);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic struct platform_driver sun50i_usb3_phy_driver = {
17962306a36Sopenharmony_ci	.probe	= sun50i_usb3_phy_probe,
18062306a36Sopenharmony_ci	.driver = {
18162306a36Sopenharmony_ci		.of_match_table	= sun50i_usb3_phy_of_match,
18262306a36Sopenharmony_ci		.name  = "sun50i-usb3-phy",
18362306a36Sopenharmony_ci	}
18462306a36Sopenharmony_ci};
18562306a36Sopenharmony_cimodule_platform_driver(sun50i_usb3_phy_driver);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ciMODULE_DESCRIPTION("Allwinner H6 USB 3.0 phy driver");
18862306a36Sopenharmony_ciMODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
18962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
190