18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Allwinner sun9i USB phy driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2014-2015 Chen-Yu Tsai <wens@csie.org> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on phy-sun4i-usb.c from 88c2ecf20Sopenharmony_ci * Hans de Goede <hdegoede@redhat.com> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * and code from 118c2ecf20Sopenharmony_ci * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/clk.h> 158c2ecf20Sopenharmony_ci#include <linux/err.h> 168c2ecf20Sopenharmony_ci#include <linux/io.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 198c2ecf20Sopenharmony_ci#include <linux/usb/of.h> 208c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 218c2ecf20Sopenharmony_ci#include <linux/reset.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define SUNXI_AHB_INCR16_BURST_EN BIT(11) 248c2ecf20Sopenharmony_ci#define SUNXI_AHB_INCR8_BURST_EN BIT(10) 258c2ecf20Sopenharmony_ci#define SUNXI_AHB_INCR4_BURST_EN BIT(9) 268c2ecf20Sopenharmony_ci#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8) 278c2ecf20Sopenharmony_ci#define SUNXI_ULPI_BYPASS_EN BIT(0) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* usb1 HSIC specific bits */ 308c2ecf20Sopenharmony_ci#define SUNXI_EHCI_HS_FORCE BIT(20) 318c2ecf20Sopenharmony_ci#define SUNXI_HSIC_CONNECT_DET BIT(17) 328c2ecf20Sopenharmony_ci#define SUNXI_HSIC_CONNECT_INT BIT(16) 338c2ecf20Sopenharmony_ci#define SUNXI_HSIC BIT(1) 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistruct sun9i_usb_phy { 368c2ecf20Sopenharmony_ci struct phy *phy; 378c2ecf20Sopenharmony_ci void __iomem *pmu; 388c2ecf20Sopenharmony_ci struct reset_control *reset; 398c2ecf20Sopenharmony_ci struct clk *clk; 408c2ecf20Sopenharmony_ci struct clk *hsic_clk; 418c2ecf20Sopenharmony_ci enum usb_phy_interface type; 428c2ecf20Sopenharmony_ci}; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic void sun9i_usb_phy_passby(struct sun9i_usb_phy *phy, int enable) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci u32 bits, reg_value; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci bits = SUNXI_AHB_INCR16_BURST_EN | SUNXI_AHB_INCR8_BURST_EN | 498c2ecf20Sopenharmony_ci SUNXI_AHB_INCR4_BURST_EN | SUNXI_AHB_INCRX_ALIGN_EN | 508c2ecf20Sopenharmony_ci SUNXI_ULPI_BYPASS_EN; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (phy->type == USBPHY_INTERFACE_MODE_HSIC) 538c2ecf20Sopenharmony_ci bits |= SUNXI_HSIC | SUNXI_EHCI_HS_FORCE | 548c2ecf20Sopenharmony_ci SUNXI_HSIC_CONNECT_DET | SUNXI_HSIC_CONNECT_INT; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci reg_value = readl(phy->pmu); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if (enable) 598c2ecf20Sopenharmony_ci reg_value |= bits; 608c2ecf20Sopenharmony_ci else 618c2ecf20Sopenharmony_ci reg_value &= ~bits; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci writel(reg_value, phy->pmu); 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic int sun9i_usb_phy_init(struct phy *_phy) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct sun9i_usb_phy *phy = phy_get_drvdata(_phy); 698c2ecf20Sopenharmony_ci int ret; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci ret = clk_prepare_enable(phy->clk); 728c2ecf20Sopenharmony_ci if (ret) 738c2ecf20Sopenharmony_ci goto err_clk; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci ret = clk_prepare_enable(phy->hsic_clk); 768c2ecf20Sopenharmony_ci if (ret) 778c2ecf20Sopenharmony_ci goto err_hsic_clk; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci ret = reset_control_deassert(phy->reset); 808c2ecf20Sopenharmony_ci if (ret) 818c2ecf20Sopenharmony_ci goto err_reset; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci sun9i_usb_phy_passby(phy, 1); 848c2ecf20Sopenharmony_ci return 0; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cierr_reset: 878c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->hsic_clk); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cierr_hsic_clk: 908c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->clk); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cierr_clk: 938c2ecf20Sopenharmony_ci return ret; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic int sun9i_usb_phy_exit(struct phy *_phy) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci struct sun9i_usb_phy *phy = phy_get_drvdata(_phy); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci sun9i_usb_phy_passby(phy, 0); 1018c2ecf20Sopenharmony_ci reset_control_assert(phy->reset); 1028c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->hsic_clk); 1038c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->clk); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci return 0; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic const struct phy_ops sun9i_usb_phy_ops = { 1098c2ecf20Sopenharmony_ci .init = sun9i_usb_phy_init, 1108c2ecf20Sopenharmony_ci .exit = sun9i_usb_phy_exit, 1118c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic int sun9i_usb_phy_probe(struct platform_device *pdev) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci struct sun9i_usb_phy *phy; 1178c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 1188c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 1198c2ecf20Sopenharmony_ci struct phy_provider *phy_provider; 1208c2ecf20Sopenharmony_ci struct resource *res; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); 1238c2ecf20Sopenharmony_ci if (!phy) 1248c2ecf20Sopenharmony_ci return -ENOMEM; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci phy->type = of_usb_get_phy_mode(np); 1278c2ecf20Sopenharmony_ci if (phy->type == USBPHY_INTERFACE_MODE_HSIC) { 1288c2ecf20Sopenharmony_ci phy->clk = devm_clk_get(dev, "hsic_480M"); 1298c2ecf20Sopenharmony_ci if (IS_ERR(phy->clk)) { 1308c2ecf20Sopenharmony_ci dev_err(dev, "failed to get hsic_480M clock\n"); 1318c2ecf20Sopenharmony_ci return PTR_ERR(phy->clk); 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci phy->hsic_clk = devm_clk_get(dev, "hsic_12M"); 1358c2ecf20Sopenharmony_ci if (IS_ERR(phy->hsic_clk)) { 1368c2ecf20Sopenharmony_ci dev_err(dev, "failed to get hsic_12M clock\n"); 1378c2ecf20Sopenharmony_ci return PTR_ERR(phy->hsic_clk); 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci phy->reset = devm_reset_control_get(dev, "hsic"); 1418c2ecf20Sopenharmony_ci if (IS_ERR(phy->reset)) { 1428c2ecf20Sopenharmony_ci dev_err(dev, "failed to get reset control\n"); 1438c2ecf20Sopenharmony_ci return PTR_ERR(phy->reset); 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci } else { 1468c2ecf20Sopenharmony_ci phy->clk = devm_clk_get(dev, "phy"); 1478c2ecf20Sopenharmony_ci if (IS_ERR(phy->clk)) { 1488c2ecf20Sopenharmony_ci dev_err(dev, "failed to get phy clock\n"); 1498c2ecf20Sopenharmony_ci return PTR_ERR(phy->clk); 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci phy->reset = devm_reset_control_get(dev, "phy"); 1538c2ecf20Sopenharmony_ci if (IS_ERR(phy->reset)) { 1548c2ecf20Sopenharmony_ci dev_err(dev, "failed to get reset control\n"); 1558c2ecf20Sopenharmony_ci return PTR_ERR(phy->reset); 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1608c2ecf20Sopenharmony_ci phy->pmu = devm_ioremap_resource(dev, res); 1618c2ecf20Sopenharmony_ci if (IS_ERR(phy->pmu)) 1628c2ecf20Sopenharmony_ci return PTR_ERR(phy->pmu); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci phy->phy = devm_phy_create(dev, NULL, &sun9i_usb_phy_ops); 1658c2ecf20Sopenharmony_ci if (IS_ERR(phy->phy)) { 1668c2ecf20Sopenharmony_ci dev_err(dev, "failed to create PHY\n"); 1678c2ecf20Sopenharmony_ci return PTR_ERR(phy->phy); 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci phy_set_drvdata(phy->phy, phy); 1718c2ecf20Sopenharmony_ci phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(phy_provider); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic const struct of_device_id sun9i_usb_phy_of_match[] = { 1778c2ecf20Sopenharmony_ci { .compatible = "allwinner,sun9i-a80-usb-phy" }, 1788c2ecf20Sopenharmony_ci { }, 1798c2ecf20Sopenharmony_ci}; 1808c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sun9i_usb_phy_of_match); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_cistatic struct platform_driver sun9i_usb_phy_driver = { 1838c2ecf20Sopenharmony_ci .probe = sun9i_usb_phy_probe, 1848c2ecf20Sopenharmony_ci .driver = { 1858c2ecf20Sopenharmony_ci .of_match_table = sun9i_usb_phy_of_match, 1868c2ecf20Sopenharmony_ci .name = "sun9i-usb-phy", 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci}; 1898c2ecf20Sopenharmony_cimodule_platform_driver(sun9i_usb_phy_driver); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner sun9i USB phy driver"); 1928c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>"); 1938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 194