xref: /linux/drivers/phy/renesas/phy-rcar-gen3-usb3.c (revision 0b5604affbec0241d72996924b2f5a33d96b860a)
1eee0e5daSWolfram Sang // SPDX-License-Identifier: GPL-2.0
27c7356baSYoshihiro Shimoda /*
37c7356baSYoshihiro Shimoda  * Renesas R-Car Gen3 for USB3.0 PHY driver
47c7356baSYoshihiro Shimoda  *
57c7356baSYoshihiro Shimoda  * Copyright (C) 2017 Renesas Electronics Corporation
67c7356baSYoshihiro Shimoda  */
77c7356baSYoshihiro Shimoda 
87c7356baSYoshihiro Shimoda #include <linux/clk.h>
97c7356baSYoshihiro Shimoda #include <linux/delay.h>
107c7356baSYoshihiro Shimoda #include <linux/io.h>
117c7356baSYoshihiro Shimoda #include <linux/module.h>
127c7356baSYoshihiro Shimoda #include <linux/of.h>
137c7356baSYoshihiro Shimoda #include <linux/phy/phy.h>
147c7356baSYoshihiro Shimoda #include <linux/platform_device.h>
157c7356baSYoshihiro Shimoda #include <linux/pm_runtime.h>
167c7356baSYoshihiro Shimoda 
177c7356baSYoshihiro Shimoda #define USB30_CLKSET0		0x034
187c7356baSYoshihiro Shimoda #define USB30_CLKSET1		0x036
197c7356baSYoshihiro Shimoda #define USB30_SSC_SET		0x038
207c7356baSYoshihiro Shimoda #define USB30_PHY_ENABLE	0x060
217c7356baSYoshihiro Shimoda #define USB30_VBUS_EN		0x064
227c7356baSYoshihiro Shimoda 
237c7356baSYoshihiro Shimoda /* USB30_CLKSET0 */
247c7356baSYoshihiro Shimoda #define CLKSET0_PRIVATE			0x05c0
257c7356baSYoshihiro Shimoda #define CLKSET0_USB30_FSEL_USB_EXTAL	0x0002
267c7356baSYoshihiro Shimoda 
277c7356baSYoshihiro Shimoda /* USB30_CLKSET1 */
287c7356baSYoshihiro Shimoda #define CLKSET1_USB30_PLL_MULTI_SHIFT		6
297c7356baSYoshihiro Shimoda #define CLKSET1_USB30_PLL_MULTI_USB_EXTAL	(0x64 << \
307c7356baSYoshihiro Shimoda 						 CLKSET1_USB30_PLL_MULTI_SHIFT)
317c7356baSYoshihiro Shimoda #define CLKSET1_PHYRESET	BIT(4)	/* 1: reset */
327c7356baSYoshihiro Shimoda #define CLKSET1_REF_CLKDIV	BIT(3)	/* 1: USB_EXTAL */
337c7356baSYoshihiro Shimoda #define CLKSET1_PRIVATE_2_1	BIT(1)	/* Write B'01 */
347c7356baSYoshihiro Shimoda #define CLKSET1_REF_CLK_SEL	BIT(0)	/* 1: USB3S0_CLK_P */
357c7356baSYoshihiro Shimoda 
367c7356baSYoshihiro Shimoda /* USB30_SSC_SET */
377c7356baSYoshihiro Shimoda #define SSC_SET_SSC_EN		BIT(12)
387c7356baSYoshihiro Shimoda #define SSC_SET_RANGE_SHIFT	9
397c7356baSYoshihiro Shimoda #define SSC_SET_RANGE_4980	(0x0 << SSC_SET_RANGE_SHIFT)
407c7356baSYoshihiro Shimoda #define SSC_SET_RANGE_4492	(0x1 << SSC_SET_RANGE_SHIFT)
417c7356baSYoshihiro Shimoda #define SSC_SET_RANGE_4003	(0x2 << SSC_SET_RANGE_SHIFT)
427c7356baSYoshihiro Shimoda 
437c7356baSYoshihiro Shimoda /* USB30_PHY_ENABLE */
447c7356baSYoshihiro Shimoda #define PHY_ENABLE_RESET_EN	BIT(4)
457c7356baSYoshihiro Shimoda 
467c7356baSYoshihiro Shimoda /* USB30_VBUS_EN */
477c7356baSYoshihiro Shimoda #define VBUS_EN_VBUS_EN		BIT(1)
487c7356baSYoshihiro Shimoda 
497c7356baSYoshihiro Shimoda struct rcar_gen3_usb3 {
507c7356baSYoshihiro Shimoda 	void __iomem *base;
517c7356baSYoshihiro Shimoda 	struct phy *phy;
527c7356baSYoshihiro Shimoda 	u32 ssc_range;
537c7356baSYoshihiro Shimoda 	bool usb3s_clk;
547c7356baSYoshihiro Shimoda 	bool usb_extal;
557c7356baSYoshihiro Shimoda };
567c7356baSYoshihiro Shimoda 
577c7356baSYoshihiro Shimoda static void write_clkset1_for_usb_extal(struct rcar_gen3_usb3 *r, bool reset)
587c7356baSYoshihiro Shimoda {
597c7356baSYoshihiro Shimoda 	u16 val = CLKSET1_USB30_PLL_MULTI_USB_EXTAL |
607c7356baSYoshihiro Shimoda 		  CLKSET1_REF_CLKDIV | CLKSET1_PRIVATE_2_1;
617c7356baSYoshihiro Shimoda 
627c7356baSYoshihiro Shimoda 	if (reset)
637c7356baSYoshihiro Shimoda 		val |= CLKSET1_PHYRESET;
647c7356baSYoshihiro Shimoda 
657c7356baSYoshihiro Shimoda 	writew(val, r->base + USB30_CLKSET1);
667c7356baSYoshihiro Shimoda }
677c7356baSYoshihiro Shimoda 
687c7356baSYoshihiro Shimoda static void rcar_gen3_phy_usb3_enable_ssc(struct rcar_gen3_usb3 *r)
697c7356baSYoshihiro Shimoda {
707c7356baSYoshihiro Shimoda 	u16 val = SSC_SET_SSC_EN;
717c7356baSYoshihiro Shimoda 
727c7356baSYoshihiro Shimoda 	switch (r->ssc_range) {
737c7356baSYoshihiro Shimoda 	case 4980:
747c7356baSYoshihiro Shimoda 		val |= SSC_SET_RANGE_4980;
757c7356baSYoshihiro Shimoda 		break;
767c7356baSYoshihiro Shimoda 	case 4492:
777c7356baSYoshihiro Shimoda 		val |= SSC_SET_RANGE_4492;
787c7356baSYoshihiro Shimoda 		break;
797c7356baSYoshihiro Shimoda 	case 4003:
807c7356baSYoshihiro Shimoda 		val |= SSC_SET_RANGE_4003;
817c7356baSYoshihiro Shimoda 		break;
827c7356baSYoshihiro Shimoda 	default:
837c7356baSYoshihiro Shimoda 		dev_err(&r->phy->dev, "%s: unsupported range (%x)\n", __func__,
847c7356baSYoshihiro Shimoda 			r->ssc_range);
857c7356baSYoshihiro Shimoda 		return;
867c7356baSYoshihiro Shimoda 	}
877c7356baSYoshihiro Shimoda 
887c7356baSYoshihiro Shimoda 	writew(val, r->base + USB30_SSC_SET);
897c7356baSYoshihiro Shimoda }
907c7356baSYoshihiro Shimoda 
917c7356baSYoshihiro Shimoda static void rcar_gen3_phy_usb3_select_usb_extal(struct rcar_gen3_usb3 *r)
927c7356baSYoshihiro Shimoda {
937c7356baSYoshihiro Shimoda 	write_clkset1_for_usb_extal(r, false);
947c7356baSYoshihiro Shimoda 	if (r->ssc_range)
957c7356baSYoshihiro Shimoda 		rcar_gen3_phy_usb3_enable_ssc(r);
967c7356baSYoshihiro Shimoda 	writew(CLKSET0_PRIVATE | CLKSET0_USB30_FSEL_USB_EXTAL,
977c7356baSYoshihiro Shimoda 	       r->base + USB30_CLKSET0);
987c7356baSYoshihiro Shimoda 	writew(PHY_ENABLE_RESET_EN, r->base + USB30_PHY_ENABLE);
997c7356baSYoshihiro Shimoda 	write_clkset1_for_usb_extal(r, true);
1007c7356baSYoshihiro Shimoda 	usleep_range(10, 20);
1017c7356baSYoshihiro Shimoda 	write_clkset1_for_usb_extal(r, false);
1027c7356baSYoshihiro Shimoda }
1037c7356baSYoshihiro Shimoda 
1047c7356baSYoshihiro Shimoda static int rcar_gen3_phy_usb3_init(struct phy *p)
1057c7356baSYoshihiro Shimoda {
1067c7356baSYoshihiro Shimoda 	struct rcar_gen3_usb3 *r = phy_get_drvdata(p);
1077c7356baSYoshihiro Shimoda 
1087c7356baSYoshihiro Shimoda 	dev_vdbg(&r->phy->dev, "%s: enter (%d, %d, %d)\n", __func__,
1097c7356baSYoshihiro Shimoda 		 r->usb3s_clk, r->usb_extal, r->ssc_range);
1107c7356baSYoshihiro Shimoda 
1117c7356baSYoshihiro Shimoda 	if (!r->usb3s_clk && r->usb_extal)
1127c7356baSYoshihiro Shimoda 		rcar_gen3_phy_usb3_select_usb_extal(r);
1137c7356baSYoshihiro Shimoda 
1147c7356baSYoshihiro Shimoda 	/* Enables VBUS detection anyway */
1157c7356baSYoshihiro Shimoda 	writew(VBUS_EN_VBUS_EN, r->base + USB30_VBUS_EN);
1167c7356baSYoshihiro Shimoda 
1177c7356baSYoshihiro Shimoda 	return 0;
1187c7356baSYoshihiro Shimoda }
1197c7356baSYoshihiro Shimoda 
1207c7356baSYoshihiro Shimoda static const struct phy_ops rcar_gen3_phy_usb3_ops = {
1217c7356baSYoshihiro Shimoda 	.init		= rcar_gen3_phy_usb3_init,
1227c7356baSYoshihiro Shimoda 	.owner		= THIS_MODULE,
1237c7356baSYoshihiro Shimoda };
1247c7356baSYoshihiro Shimoda 
1257c7356baSYoshihiro Shimoda static const struct of_device_id rcar_gen3_phy_usb3_match_table[] = {
1267c7356baSYoshihiro Shimoda 	{ .compatible = "renesas,rcar-gen3-usb3-phy" },
1277c7356baSYoshihiro Shimoda 	{ }
1287c7356baSYoshihiro Shimoda };
1297c7356baSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb3_match_table);
1307c7356baSYoshihiro Shimoda 
1317c7356baSYoshihiro Shimoda static int rcar_gen3_phy_usb3_probe(struct platform_device *pdev)
1327c7356baSYoshihiro Shimoda {
1337c7356baSYoshihiro Shimoda 	struct device *dev = &pdev->dev;
1347c7356baSYoshihiro Shimoda 	struct rcar_gen3_usb3 *r;
1357c7356baSYoshihiro Shimoda 	struct phy_provider *provider;
1367c7356baSYoshihiro Shimoda 	int ret = 0;
1377c7356baSYoshihiro Shimoda 	struct clk *clk;
1387c7356baSYoshihiro Shimoda 
1397c7356baSYoshihiro Shimoda 	if (!dev->of_node) {
1407c7356baSYoshihiro Shimoda 		dev_err(dev, "This driver needs device tree\n");
1417c7356baSYoshihiro Shimoda 		return -EINVAL;
1427c7356baSYoshihiro Shimoda 	}
1437c7356baSYoshihiro Shimoda 
1447c7356baSYoshihiro Shimoda 	r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
1457c7356baSYoshihiro Shimoda 	if (!r)
1467c7356baSYoshihiro Shimoda 		return -ENOMEM;
1477c7356baSYoshihiro Shimoda 
148*0b5604afSChunfeng Yun 	r->base = devm_platform_ioremap_resource(pdev, 0);
1497c7356baSYoshihiro Shimoda 	if (IS_ERR(r->base))
1507c7356baSYoshihiro Shimoda 		return PTR_ERR(r->base);
1517c7356baSYoshihiro Shimoda 
1527c7356baSYoshihiro Shimoda 	clk = devm_clk_get(dev, "usb3s_clk");
1537c7356baSYoshihiro Shimoda 	if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
1547c7356baSYoshihiro Shimoda 		r->usb3s_clk = !!clk_get_rate(clk);
1557c7356baSYoshihiro Shimoda 		clk_disable_unprepare(clk);
1567c7356baSYoshihiro Shimoda 	}
1577c7356baSYoshihiro Shimoda 	clk = devm_clk_get(dev, "usb_extal");
1587c7356baSYoshihiro Shimoda 	if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
1597c7356baSYoshihiro Shimoda 		r->usb_extal = !!clk_get_rate(clk);
1607c7356baSYoshihiro Shimoda 		clk_disable_unprepare(clk);
1617c7356baSYoshihiro Shimoda 	}
1627c7356baSYoshihiro Shimoda 
1637c7356baSYoshihiro Shimoda 	if (!r->usb3s_clk && !r->usb_extal) {
1647c7356baSYoshihiro Shimoda 		dev_err(dev, "This driver needs usb3s_clk and/or usb_extal\n");
1657c7356baSYoshihiro Shimoda 		ret = -EINVAL;
1667c7356baSYoshihiro Shimoda 		goto error;
1677c7356baSYoshihiro Shimoda 	}
1687c7356baSYoshihiro Shimoda 
1697c7356baSYoshihiro Shimoda 	/*
1707c7356baSYoshihiro Shimoda 	 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
1717c7356baSYoshihiro Shimoda 	 * And then, phy-core will manage runtime pm for this device.
1727c7356baSYoshihiro Shimoda 	 */
1737c7356baSYoshihiro Shimoda 	pm_runtime_enable(dev);
1747c7356baSYoshihiro Shimoda 
1757c7356baSYoshihiro Shimoda 	r->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb3_ops);
1767c7356baSYoshihiro Shimoda 	if (IS_ERR(r->phy)) {
1777c7356baSYoshihiro Shimoda 		dev_err(dev, "Failed to create USB3 PHY\n");
1787c7356baSYoshihiro Shimoda 		ret = PTR_ERR(r->phy);
1797c7356baSYoshihiro Shimoda 		goto error;
1807c7356baSYoshihiro Shimoda 	}
1817c7356baSYoshihiro Shimoda 
1827c7356baSYoshihiro Shimoda 	of_property_read_u32(dev->of_node, "renesas,ssc-range", &r->ssc_range);
1837c7356baSYoshihiro Shimoda 
1847c7356baSYoshihiro Shimoda 	platform_set_drvdata(pdev, r);
1857c7356baSYoshihiro Shimoda 	phy_set_drvdata(r->phy, r);
1867c7356baSYoshihiro Shimoda 
1877c7356baSYoshihiro Shimoda 	provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1887c7356baSYoshihiro Shimoda 	if (IS_ERR(provider)) {
1897c7356baSYoshihiro Shimoda 		dev_err(dev, "Failed to register PHY provider\n");
1907c7356baSYoshihiro Shimoda 		ret = PTR_ERR(provider);
1917c7356baSYoshihiro Shimoda 		goto error;
1927c7356baSYoshihiro Shimoda 	}
1937c7356baSYoshihiro Shimoda 
1947c7356baSYoshihiro Shimoda 	return 0;
1957c7356baSYoshihiro Shimoda 
1967c7356baSYoshihiro Shimoda error:
1977c7356baSYoshihiro Shimoda 	pm_runtime_disable(dev);
1987c7356baSYoshihiro Shimoda 
1997c7356baSYoshihiro Shimoda 	return ret;
2007c7356baSYoshihiro Shimoda }
2017c7356baSYoshihiro Shimoda 
2027c7356baSYoshihiro Shimoda static int rcar_gen3_phy_usb3_remove(struct platform_device *pdev)
2037c7356baSYoshihiro Shimoda {
2047c7356baSYoshihiro Shimoda 	pm_runtime_disable(&pdev->dev);
2057c7356baSYoshihiro Shimoda 
2067c7356baSYoshihiro Shimoda 	return 0;
2077c7356baSYoshihiro Shimoda };
2087c7356baSYoshihiro Shimoda 
2097c7356baSYoshihiro Shimoda static struct platform_driver rcar_gen3_phy_usb3_driver = {
2107c7356baSYoshihiro Shimoda 	.driver = {
2117c7356baSYoshihiro Shimoda 		.name		= "phy_rcar_gen3_usb3",
2127c7356baSYoshihiro Shimoda 		.of_match_table	= rcar_gen3_phy_usb3_match_table,
2137c7356baSYoshihiro Shimoda 	},
2147c7356baSYoshihiro Shimoda 	.probe	= rcar_gen3_phy_usb3_probe,
2157c7356baSYoshihiro Shimoda 	.remove = rcar_gen3_phy_usb3_remove,
2167c7356baSYoshihiro Shimoda };
2177c7356baSYoshihiro Shimoda module_platform_driver(rcar_gen3_phy_usb3_driver);
2187c7356baSYoshihiro Shimoda 
2197c7356baSYoshihiro Shimoda MODULE_LICENSE("GPL v2");
2207c7356baSYoshihiro Shimoda MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 3.0 PHY");
2217c7356baSYoshihiro Shimoda MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
222