1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2021 Nuvoton Technology Corp.
8 #include <generic-phy.h>
12 #include <dm/device_compat.h>
13 #include <linux/bitfield.h>
14 #include <linux/delay.h>
16 /* GCR Register Offsets */
17 #define GCR_INTCR3 0x9C
18 #define GCR_USB1PHYCTL 0x140
19 #define GCR_USB2PHYCTL 0x144
20 #define GCR_USB3PHYCTL 0x148
22 /* USBnPHYCTL bit fields */
23 #define PHYCTL_RS BIT(28)
25 #define USBPHY2SW GENMASK(13, 12)
26 #define USBPHY3SW GENMASK(15, 14)
28 #define USBPHY2SW_DEV9_PHY1 FIELD_PREP(USBPHY2SW, 0)
29 #define USBPHY2SW_HOST1 FIELD_PREP(USBPHY2SW, 1)
30 #define USBPHY2SW_DEV9_PHY2 FIELD_PREP(USBPHY2SW, 3)
31 #define USBPHY3SW_DEV8_PHY1 FIELD_PREP(USBPHY3SW, 0)
32 #define USBPHY3SW_HOST2 FIELD_PREP(USBPHY3SW, 1)
33 #define USBPHY3SW_DEV8_PHY3 FIELD_PREP(USBPHY3SW, 3)
49 /* Phy Switch Settings */
50 #define USBDPHY1 ((PHY1 << 8) | UDC0_7) /* Connect UDC0~7 to PHY1 */
51 #define USBD8PHY1 ((PHY1 << 8) | UDC8) /* Connect UDC8 to PHY1 */
52 #define USBD9PHY1 ((PHY1 << 8) | UDC9) /* Connect UDC9 to PHY1 */
53 #define USBD9PHY2 ((PHY2 << 8) | UDC9) /* Connect UDC9 to PHY2 */
54 #define USBH1PHY2 ((PHY2 << 8) | USBH1) /* Connect USBH1 to PHY2 */
55 #define USBD8PHY3 ((PHY3 << 8) | UDC8) /* Connect UDC8 to PHY3 */
56 #define USBH2PHY3 ((PHY3 << 8) | USBH2) /* Connect USBH2 to PHY3 */
59 struct regmap *syscon;
61 u16 phy_switch; /* (phy_id << 8) | controller_id */
64 static int npcm_usb_phy_init(struct phy *phy)
66 struct npcm_usbphy *priv = dev_get_priv(phy->dev);
67 struct reset_ctl reset;
70 ret = reset_get_by_index(phy->dev, 0, &reset);
71 if (ret && ret != -ENOENT && ret != -ENOTSUPP) {
72 dev_err(phy->dev, "can't get phy reset ctrl (err %d)", ret);
76 /* setup PHY switch */
77 switch (priv->phy_switch) {
79 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
83 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
87 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
91 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
95 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
99 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
106 if (reset_valid(&reset))
107 reset_assert(&reset);
109 /* Wait for PHY clocks to stablize for 50us or more */
112 /* release phy from reset */
113 if (reset_valid(&reset))
114 reset_deassert(&reset);
116 /* PHY RS bit should be set after reset */
119 regmap_update_bits(priv->syscon, GCR_USB1PHYCTL, PHYCTL_RS, PHYCTL_RS);
122 regmap_update_bits(priv->syscon, GCR_USB2PHYCTL, PHYCTL_RS, PHYCTL_RS);
125 regmap_update_bits(priv->syscon, GCR_USB3PHYCTL, PHYCTL_RS, PHYCTL_RS);
134 static int npcm_usb_phy_exit(struct phy *phy)
136 struct npcm_usbphy *priv = dev_get_priv(phy->dev);
138 /* set PHY switch to default state */
139 switch (priv->phy_switch) {
142 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
147 regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
156 static int npcm_usb_phy_xlate(struct phy *phy, struct ofnode_phandle_args *args)
158 struct npcm_usbphy *priv = dev_get_priv(phy->dev);
161 if (args->args_count < 1 || args->args[0] > USBH2)
164 phy_switch = (priv->id << 8) | args->args[0];
165 switch (phy_switch) {
169 if (!IS_ENABLED(CONFIG_ARCH_NPCM8XX))
175 priv->phy_switch = phy_switch;
182 static int npcm_usb_phy_probe(struct udevice *dev)
184 struct npcm_usbphy *priv = dev_get_priv(dev);
186 priv->syscon = syscon_regmap_lookup_by_phandle(dev->parent, "syscon");
187 if (IS_ERR(priv->syscon)) {
188 dev_err(dev, "%s: unable to get syscon\n", __func__);
189 return PTR_ERR(priv->syscon);
191 priv->id = dev_read_u32_default(dev, "reg", -1);
196 static const struct udevice_id npcm_phy_ids[] = {
197 { .compatible = "nuvoton,npcm845-usb-phy",},
198 { .compatible = "nuvoton,npcm750-usb-phy",},
202 static struct phy_ops npcm_phy_ops = {
203 .init = npcm_usb_phy_init,
204 .exit = npcm_usb_phy_exit,
205 .of_xlate = npcm_usb_phy_xlate,
208 U_BOOT_DRIVER(npcm_phy) = {
209 .name = "npcm-usb-phy",
211 .of_match = npcm_phy_ids,
212 .ops = &npcm_phy_ops,
213 .probe = npcm_usb_phy_probe,
214 .priv_auto = sizeof(struct npcm_usbphy),