18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Lantiq XWAY SoC RCU module based USB 1.1/2.0 PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2017 Hauke Mehrtens <hauke@hauke-m.de>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/of.h>
148c2ecf20Sopenharmony_ci#include <linux/of_address.h>
158c2ecf20Sopenharmony_ci#include <linux/of_device.h>
168c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/property.h>
198c2ecf20Sopenharmony_ci#include <linux/regmap.h>
208c2ecf20Sopenharmony_ci#include <linux/reset.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Transmitter HS Pre-Emphasis Enable */
238c2ecf20Sopenharmony_ci#define RCU_CFG1_TX_PEE		BIT(0)
248c2ecf20Sopenharmony_ci/* Disconnect Threshold */
258c2ecf20Sopenharmony_ci#define RCU_CFG1_DIS_THR_MASK	0x00038000
268c2ecf20Sopenharmony_ci#define RCU_CFG1_DIS_THR_SHIFT	15
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct ltq_rcu_usb2_bits {
298c2ecf20Sopenharmony_ci	u8 hostmode;
308c2ecf20Sopenharmony_ci	u8 slave_endianness;
318c2ecf20Sopenharmony_ci	u8 host_endianness;
328c2ecf20Sopenharmony_ci	bool have_ana_cfg;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct ltq_rcu_usb2_priv {
368c2ecf20Sopenharmony_ci	struct regmap			*regmap;
378c2ecf20Sopenharmony_ci	unsigned int			phy_reg_offset;
388c2ecf20Sopenharmony_ci	unsigned int			ana_cfg1_reg_offset;
398c2ecf20Sopenharmony_ci	const struct ltq_rcu_usb2_bits	*reg_bits;
408c2ecf20Sopenharmony_ci	struct device			*dev;
418c2ecf20Sopenharmony_ci	struct phy			*phy;
428c2ecf20Sopenharmony_ci	struct clk			*phy_gate_clk;
438c2ecf20Sopenharmony_ci	struct reset_control		*ctrl_reset;
448c2ecf20Sopenharmony_ci	struct reset_control		*phy_reset;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic const struct ltq_rcu_usb2_bits xway_rcu_usb2_reg_bits = {
488c2ecf20Sopenharmony_ci	.hostmode = 11,
498c2ecf20Sopenharmony_ci	.slave_endianness = 9,
508c2ecf20Sopenharmony_ci	.host_endianness = 10,
518c2ecf20Sopenharmony_ci	.have_ana_cfg = false,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const struct ltq_rcu_usb2_bits xrx100_rcu_usb2_reg_bits = {
558c2ecf20Sopenharmony_ci	.hostmode = 11,
568c2ecf20Sopenharmony_ci	.slave_endianness = 17,
578c2ecf20Sopenharmony_ci	.host_endianness = 10,
588c2ecf20Sopenharmony_ci	.have_ana_cfg = false,
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const struct ltq_rcu_usb2_bits xrx200_rcu_usb2_reg_bits = {
628c2ecf20Sopenharmony_ci	.hostmode = 11,
638c2ecf20Sopenharmony_ci	.slave_endianness = 9,
648c2ecf20Sopenharmony_ci	.host_endianness = 10,
658c2ecf20Sopenharmony_ci	.have_ana_cfg = true,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic const struct of_device_id ltq_rcu_usb2_phy_of_match[] = {
698c2ecf20Sopenharmony_ci	{
708c2ecf20Sopenharmony_ci		.compatible = "lantiq,ase-usb2-phy",
718c2ecf20Sopenharmony_ci		.data = &xway_rcu_usb2_reg_bits,
728c2ecf20Sopenharmony_ci	},
738c2ecf20Sopenharmony_ci	{
748c2ecf20Sopenharmony_ci		.compatible = "lantiq,danube-usb2-phy",
758c2ecf20Sopenharmony_ci		.data = &xway_rcu_usb2_reg_bits,
768c2ecf20Sopenharmony_ci	},
778c2ecf20Sopenharmony_ci	{
788c2ecf20Sopenharmony_ci		.compatible = "lantiq,xrx100-usb2-phy",
798c2ecf20Sopenharmony_ci		.data = &xrx100_rcu_usb2_reg_bits,
808c2ecf20Sopenharmony_ci	},
818c2ecf20Sopenharmony_ci	{
828c2ecf20Sopenharmony_ci		.compatible = "lantiq,xrx200-usb2-phy",
838c2ecf20Sopenharmony_ci		.data = &xrx200_rcu_usb2_reg_bits,
848c2ecf20Sopenharmony_ci	},
858c2ecf20Sopenharmony_ci	{
868c2ecf20Sopenharmony_ci		.compatible = "lantiq,xrx300-usb2-phy",
878c2ecf20Sopenharmony_ci		.data = &xrx200_rcu_usb2_reg_bits,
888c2ecf20Sopenharmony_ci	},
898c2ecf20Sopenharmony_ci	{ },
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ltq_rcu_usb2_phy_of_match);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int ltq_rcu_usb2_phy_init(struct phy *phy)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct ltq_rcu_usb2_priv *priv = phy_get_drvdata(phy);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (priv->reg_bits->have_ana_cfg) {
988c2ecf20Sopenharmony_ci		regmap_update_bits(priv->regmap, priv->ana_cfg1_reg_offset,
998c2ecf20Sopenharmony_ci			RCU_CFG1_TX_PEE, RCU_CFG1_TX_PEE);
1008c2ecf20Sopenharmony_ci		regmap_update_bits(priv->regmap, priv->ana_cfg1_reg_offset,
1018c2ecf20Sopenharmony_ci			RCU_CFG1_DIS_THR_MASK, 7 << RCU_CFG1_DIS_THR_SHIFT);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* Configure core to host mode */
1058c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, priv->phy_reg_offset,
1068c2ecf20Sopenharmony_ci			   BIT(priv->reg_bits->hostmode), 0);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* Select DMA endianness (Host-endian: big-endian) */
1098c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, priv->phy_reg_offset,
1108c2ecf20Sopenharmony_ci		BIT(priv->reg_bits->slave_endianness), 0);
1118c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, priv->phy_reg_offset,
1128c2ecf20Sopenharmony_ci		BIT(priv->reg_bits->host_endianness),
1138c2ecf20Sopenharmony_ci		BIT(priv->reg_bits->host_endianness));
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return 0;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int ltq_rcu_usb2_phy_power_on(struct phy *phy)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct ltq_rcu_usb2_priv *priv = phy_get_drvdata(phy);
1218c2ecf20Sopenharmony_ci	struct device *dev = priv->dev;
1228c2ecf20Sopenharmony_ci	int ret;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	reset_control_deassert(priv->phy_reset);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->phy_gate_clk);
1278c2ecf20Sopenharmony_ci	if (ret) {
1288c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable PHY gate\n");
1298c2ecf20Sopenharmony_ci		return ret;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/*
1338c2ecf20Sopenharmony_ci	 * at least the xrx200 usb2 phy requires some extra time to be
1348c2ecf20Sopenharmony_ci	 * operational after enabling the clock
1358c2ecf20Sopenharmony_ci	 */
1368c2ecf20Sopenharmony_ci	usleep_range(100, 200);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return ret;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int ltq_rcu_usb2_phy_power_off(struct phy *phy)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct ltq_rcu_usb2_priv *priv = phy_get_drvdata(phy);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	reset_control_assert(priv->phy_reset);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->phy_gate_clk);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return 0;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct phy_ops ltq_rcu_usb2_phy_ops = {
1538c2ecf20Sopenharmony_ci	.init		= ltq_rcu_usb2_phy_init,
1548c2ecf20Sopenharmony_ci	.power_on	= ltq_rcu_usb2_phy_power_on,
1558c2ecf20Sopenharmony_ci	.power_off	= ltq_rcu_usb2_phy_power_off,
1568c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic int ltq_rcu_usb2_of_parse(struct ltq_rcu_usb2_priv *priv,
1608c2ecf20Sopenharmony_ci				 struct platform_device *pdev)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct device *dev = priv->dev;
1638c2ecf20Sopenharmony_ci	const __be32 *offset;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	priv->reg_bits = of_device_get_match_data(dev);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	priv->regmap = syscon_node_to_regmap(dev->of_node->parent);
1688c2ecf20Sopenharmony_ci	if (IS_ERR(priv->regmap)) {
1698c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to lookup RCU regmap\n");
1708c2ecf20Sopenharmony_ci		return PTR_ERR(priv->regmap);
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	offset = of_get_address(dev->of_node, 0, NULL, NULL);
1748c2ecf20Sopenharmony_ci	if (!offset) {
1758c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get RCU PHY reg offset\n");
1768c2ecf20Sopenharmony_ci		return -ENOENT;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci	priv->phy_reg_offset = __be32_to_cpu(*offset);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	if (priv->reg_bits->have_ana_cfg) {
1818c2ecf20Sopenharmony_ci		offset = of_get_address(dev->of_node, 1, NULL, NULL);
1828c2ecf20Sopenharmony_ci		if (!offset) {
1838c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to get RCU ANA CFG1 reg offset\n");
1848c2ecf20Sopenharmony_ci			return -ENOENT;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci		priv->ana_cfg1_reg_offset = __be32_to_cpu(*offset);
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	priv->phy_gate_clk = devm_clk_get(dev, "phy");
1908c2ecf20Sopenharmony_ci	if (IS_ERR(priv->phy_gate_clk)) {
1918c2ecf20Sopenharmony_ci		dev_err(dev, "Unable to get USB phy gate clk\n");
1928c2ecf20Sopenharmony_ci		return PTR_ERR(priv->phy_gate_clk);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	priv->ctrl_reset = devm_reset_control_get_shared(dev, "ctrl");
1968c2ecf20Sopenharmony_ci	if (IS_ERR(priv->ctrl_reset)) {
1978c2ecf20Sopenharmony_ci		if (PTR_ERR(priv->ctrl_reset) != -EPROBE_DEFER)
1988c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get 'ctrl' reset\n");
1998c2ecf20Sopenharmony_ci		return PTR_ERR(priv->ctrl_reset);
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	priv->phy_reset = devm_reset_control_get_optional(dev, "phy");
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(priv->phy_reset);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int ltq_rcu_usb2_phy_probe(struct platform_device *pdev)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2108c2ecf20Sopenharmony_ci	struct ltq_rcu_usb2_priv *priv;
2118c2ecf20Sopenharmony_ci	struct phy_provider *provider;
2128c2ecf20Sopenharmony_ci	int ret;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
2158c2ecf20Sopenharmony_ci	if (!priv)
2168c2ecf20Sopenharmony_ci		return -ENOMEM;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	priv->dev = dev;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	ret = ltq_rcu_usb2_of_parse(priv, pdev);
2218c2ecf20Sopenharmony_ci	if (ret)
2228c2ecf20Sopenharmony_ci		return ret;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* Reset USB core through reset controller */
2258c2ecf20Sopenharmony_ci	reset_control_deassert(priv->ctrl_reset);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	reset_control_assert(priv->phy_reset);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	priv->phy = devm_phy_create(dev, dev->of_node, &ltq_rcu_usb2_phy_ops);
2308c2ecf20Sopenharmony_ci	if (IS_ERR(priv->phy)) {
2318c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create PHY\n");
2328c2ecf20Sopenharmony_ci		return PTR_ERR(priv->phy);
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	phy_set_drvdata(priv->phy, priv);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
2388c2ecf20Sopenharmony_ci	if (IS_ERR(provider))
2398c2ecf20Sopenharmony_ci		return PTR_ERR(provider);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	dev_set_drvdata(priv->dev, priv);
2428c2ecf20Sopenharmony_ci	return 0;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic struct platform_driver ltq_rcu_usb2_phy_driver = {
2468c2ecf20Sopenharmony_ci	.probe	= ltq_rcu_usb2_phy_probe,
2478c2ecf20Sopenharmony_ci	.driver = {
2488c2ecf20Sopenharmony_ci		.name	= "lantiq-rcu-usb2-phy",
2498c2ecf20Sopenharmony_ci		.of_match_table	= ltq_rcu_usb2_phy_of_match,
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_cimodule_platform_driver(ltq_rcu_usb2_phy_driver);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
2558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Lantiq XWAY USB2 PHY driver");
2568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
257