1 // SPDX-License-Identifier: GPL-2.0
3 * Phy provider for USB 3.0 controller on HiSilicon 3660 platform
5 * Copyright (C) 2017-2018 Hilisicon Electronics Co., Ltd.
6 * http://www.huawei.com
8 * Authors: Yu Chen <chenyu56@huawei.com>
11 #include <linux/kernel.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
15 #include <linux/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 #define PERI_CRG_CLK_EN4 0x40
20 #define PERI_CRG_CLK_DIS4 0x44
21 #define GT_CLK_USB3OTG_REF BIT(0)
22 #define GT_ACLK_USB3OTG BIT(1)
24 #define PERI_CRG_RSTEN4 0x90
25 #define PERI_CRG_RSTDIS4 0x94
26 #define IP_RST_USB3OTGPHY_POR BIT(3)
27 #define IP_RST_USB3OTG BIT(5)
29 #define PERI_CRG_ISODIS 0x148
30 #define USB_REFCLK_ISO_EN BIT(25)
32 #define PCTRL_PERI_CTRL3 0x10
33 #define PCTRL_PERI_CTRL3_MSK_START 16
34 #define USB_TCXO_EN BIT(1)
36 #define PCTRL_PERI_CTRL24 0x64
37 #define SC_CLK_USB3PHY_3MUX1_SEL BIT(25)
39 #define USBOTG3_CTRL0 0x00
40 #define SC_USB3PHY_ABB_GT_EN BIT(15)
42 #define USBOTG3_CTRL2 0x08
43 #define USBOTG3CTRL2_POWERDOWN_HSP BIT(0)
44 #define USBOTG3CTRL2_POWERDOWN_SSP BIT(1)
46 #define USBOTG3_CTRL3 0x0C
47 #define USBOTG3_CTRL3_VBUSVLDEXT BIT(6)
48 #define USBOTG3_CTRL3_VBUSVLDEXTSEL BIT(5)
50 #define USBOTG3_CTRL4 0x10
52 #define USBOTG3_CTRL7 0x1c
53 #define REF_SSP_EN BIT(16)
55 /* This value config the default txtune parameter of the usb 2.0 phy */
56 #define HI3660_USB_DEFAULT_PHY_PARAM 0x1c466e3
60 struct regmap *peri_crg;
62 struct regmap *otg_bc;
63 u32 eye_diagram_param;
66 static int hi3660_phy_init(struct phy *phy)
68 struct hi3660_priv *priv = phy_get_drvdata(phy);
72 /* usb refclk iso disable */
73 ret = regmap_write(priv->peri_crg, PERI_CRG_ISODIS, USB_REFCLK_ISO_EN);
77 /* enable usb_tcxo_en */
78 val = USB_TCXO_EN | (USB_TCXO_EN << PCTRL_PERI_CTRL3_MSK_START);
79 ret = regmap_write(priv->pctrl, PCTRL_PERI_CTRL3, val);
84 val = IP_RST_USB3OTGPHY_POR | IP_RST_USB3OTG;
85 ret = regmap_write(priv->peri_crg, PERI_CRG_RSTEN4, val);
89 /* enable phy ref clk */
90 val = SC_USB3PHY_ABB_GT_EN;
92 ret = regmap_update_bits(priv->otg_bc, USBOTG3_CTRL0, mask, val);
98 ret = regmap_update_bits(priv->otg_bc, USBOTG3_CTRL7, mask, val);
102 /* exit from IDDQ mode */
103 mask = USBOTG3CTRL2_POWERDOWN_HSP | USBOTG3CTRL2_POWERDOWN_SSP;
104 ret = regmap_update_bits(priv->otg_bc, USBOTG3_CTRL2, mask, 0);
108 /* delay for exit from IDDQ mode */
109 usleep_range(100, 120);
112 val = IP_RST_USB3OTGPHY_POR | IP_RST_USB3OTG;
113 ret = regmap_write(priv->peri_crg, PERI_CRG_RSTDIS4, val);
117 /* delay for phy deasserted */
118 usleep_range(10000, 15000);
120 /* fake vbus valid signal */
121 val = USBOTG3_CTRL3_VBUSVLDEXT | USBOTG3_CTRL3_VBUSVLDEXTSEL;
123 ret = regmap_update_bits(priv->otg_bc, USBOTG3_CTRL3, mask, val);
127 /* delay for vbus valid */
128 usleep_range(100, 120);
130 ret = regmap_write(priv->otg_bc, USBOTG3_CTRL4,
131 priv->eye_diagram_param);
137 dev_err(priv->dev, "failed to init phy ret: %d\n", ret);
141 static int hi3660_phy_exit(struct phy *phy)
143 struct hi3660_priv *priv = phy_get_drvdata(phy);
148 val = IP_RST_USB3OTGPHY_POR;
149 ret = regmap_write(priv->peri_crg, PERI_CRG_RSTEN4, val);
153 /* disable usb_tcxo_en */
154 val = USB_TCXO_EN << PCTRL_PERI_CTRL3_MSK_START;
155 ret = regmap_write(priv->pctrl, PCTRL_PERI_CTRL3, val);
161 dev_err(priv->dev, "failed to exit phy ret: %d\n", ret);
165 static const struct phy_ops hi3660_phy_ops = {
166 .init = hi3660_phy_init,
167 .exit = hi3660_phy_exit,
168 .owner = THIS_MODULE,
171 static int hi3660_phy_probe(struct platform_device *pdev)
173 struct phy_provider *phy_provider;
174 struct device *dev = &pdev->dev;
176 struct hi3660_priv *priv;
178 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
183 priv->peri_crg = syscon_regmap_lookup_by_phandle(dev->of_node,
184 "hisilicon,pericrg-syscon");
185 if (IS_ERR(priv->peri_crg)) {
186 dev_err(dev, "no hisilicon,pericrg-syscon\n");
187 return PTR_ERR(priv->peri_crg);
190 priv->pctrl = syscon_regmap_lookup_by_phandle(dev->of_node,
191 "hisilicon,pctrl-syscon");
192 if (IS_ERR(priv->pctrl)) {
193 dev_err(dev, "no hisilicon,pctrl-syscon\n");
194 return PTR_ERR(priv->pctrl);
197 /* node of hi3660 phy is a sub-node of usb3_otg_bc */
198 priv->otg_bc = syscon_node_to_regmap(dev->parent->of_node);
199 if (IS_ERR(priv->otg_bc)) {
200 dev_err(dev, "no hisilicon,usb3-otg-bc-syscon\n");
201 return PTR_ERR(priv->otg_bc);
204 if (of_property_read_u32(dev->of_node, "hisilicon,eye-diagram-param",
205 &(priv->eye_diagram_param)))
206 priv->eye_diagram_param = HI3660_USB_DEFAULT_PHY_PARAM;
208 phy = devm_phy_create(dev, NULL, &hi3660_phy_ops);
212 phy_set_drvdata(phy, priv);
213 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
214 return PTR_ERR_OR_ZERO(phy_provider);
217 static const struct of_device_id hi3660_phy_of_match[] = {
218 {.compatible = "hisilicon,hi3660-usb-phy",},
221 MODULE_DEVICE_TABLE(of, hi3660_phy_of_match);
223 static struct platform_driver hi3660_phy_driver = {
224 .probe = hi3660_phy_probe,
226 .name = "hi3660-usb-phy",
227 .of_match_table = hi3660_phy_of_match,
230 module_platform_driver(hi3660_phy_driver);
232 MODULE_AUTHOR("Yu Chen <chenyu56@huawei.com>");
233 MODULE_LICENSE("GPL v2");
234 MODULE_DESCRIPTION("Hilisicon Hi3660 USB3 PHY Driver");