1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RCar Gen2 USB PHY driver
5 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
13 #include <generic-phy.h>
19 #include <dm/device_compat.h>
20 #include <linux/bitops.h>
21 #include <linux/delay.h>
22 #include <power/regulator.h>
24 #define USBHS_LPSTS 0x02
25 #define USBHS_UGCTRL 0x80
26 #define USBHS_UGCTRL2 0x84
27 #define USBHS_UGSTS 0x88 /* From technical update */
29 /* Low Power Status register (LPSTS) */
30 #define USBHS_LPSTS_SUSPM 0x4000
32 /* USB General control register (UGCTRL) */
33 #define USBHS_UGCTRL_CONNECT BIT(2)
34 #define USBHS_UGCTRL_PLLRESET BIT(0)
36 /* USB General control register 2 (UGCTRL2) */
37 #define USBHS_UGCTRL2_USB2SEL 0x80000000
38 #define USBHS_UGCTRL2_USB2SEL_PCI 0x00000000
39 #define USBHS_UGCTRL2_USB2SEL_USB30 0x80000000
40 #define USBHS_UGCTRL2_USB0SEL 0x00000030
41 #define USBHS_UGCTRL2_USB0SEL_PCI 0x00000010
42 #define USBHS_UGCTRL2_USB0SEL_HS_USB 0x00000030
44 /* USB General status register (UGSTS) */
45 #define USBHS_UGSTS_LOCK 0x00000100 /* From technical update */
47 #define PHYS_PER_CHANNEL 2
49 struct rcar_gen2_phy {
54 static int rcar_gen2_phy_phy_init(struct phy *phy)
56 struct rcar_gen2_phy *priv = dev_get_priv(phy->dev);
57 u16 chan = phy->id & 0xffff;
58 u16 mode = (phy->id >> 16) & 0xffff;
62 clrmask = USBHS_UGCTRL2_USB0SEL;
63 setmask = mode ? USBHS_UGCTRL2_USB0SEL_HS_USB :
64 USBHS_UGCTRL2_USB0SEL_PCI;
66 clrmask = USBHS_UGCTRL2_USB2SEL;
67 setmask = mode ? USBHS_UGCTRL2_USB2SEL_USB30 :
68 USBHS_UGCTRL2_USB2SEL_PCI;
70 clrsetbits_le32(priv->regs + USBHS_UGCTRL2, clrmask, setmask);
75 static int rcar_gen2_phy_phy_power_on(struct phy *phy)
77 struct rcar_gen2_phy *priv = dev_get_priv(phy->dev);
81 /* Power on USBHS PHY */
82 clrbits_le32(priv->regs + USBHS_UGCTRL, USBHS_UGCTRL_PLLRESET);
84 setbits_le16(priv->regs + USBHS_LPSTS, USBHS_LPSTS_SUSPM);
86 for (i = 0; i < 20; i++) {
87 value = readl(priv->regs + USBHS_UGSTS);
88 if ((value & USBHS_UGSTS_LOCK) == USBHS_UGSTS_LOCK) {
89 setbits_le32(priv->regs + USBHS_UGCTRL,
90 USBHS_UGCTRL_CONNECT);
99 static int rcar_gen2_phy_phy_power_off(struct phy *phy)
101 struct rcar_gen2_phy *priv = dev_get_priv(phy->dev);
103 /* Power off USBHS PHY */
104 clrbits_le32(priv->regs + USBHS_UGCTRL, USBHS_UGCTRL_CONNECT);
106 clrbits_le16(priv->regs + USBHS_LPSTS, USBHS_LPSTS_SUSPM);
108 setbits_le32(priv->regs + USBHS_UGCTRL, USBHS_UGCTRL_PLLRESET);
113 static int rcar_gen2_phy_of_xlate(struct phy *phy,
114 struct ofnode_phandle_args *args)
116 if (args->args_count != 2) {
117 dev_err(phy->dev, "Invalid DT PHY argument count: %d\n",
122 if (args->args[0] != 0 && args->args[0] != 2) {
123 dev_err(phy->dev, "Invalid DT PHY channel: %d\n",
128 if (args->args[1] != 0 && args->args[1] != 1) {
129 dev_err(phy->dev, "Invalid DT PHY mode: %d\n",
134 if (args->args_count)
135 phy->id = args->args[0] | (args->args[1] << 16);
142 static const struct phy_ops rcar_gen2_phy_phy_ops = {
143 .init = rcar_gen2_phy_phy_init,
144 .power_on = rcar_gen2_phy_phy_power_on,
145 .power_off = rcar_gen2_phy_phy_power_off,
146 .of_xlate = rcar_gen2_phy_of_xlate,
149 static int rcar_gen2_phy_probe(struct udevice *dev)
151 struct rcar_gen2_phy *priv = dev_get_priv(dev);
154 priv->regs = dev_read_addr(dev);
155 if (priv->regs == FDT_ADDR_T_NONE)
159 ret = clk_get_by_index(dev, 0, &priv->clk);
163 ret = clk_enable(&priv->clk);
170 static int rcar_gen2_phy_remove(struct udevice *dev)
172 struct rcar_gen2_phy *priv = dev_get_priv(dev);
174 clk_disable(&priv->clk);
175 clk_free(&priv->clk);
180 static const struct udevice_id rcar_gen2_phy_of_match[] = {
181 { .compatible = "renesas,rcar-gen2-usb-phy", },
185 U_BOOT_DRIVER(rcar_gen2_phy) = {
186 .name = "rcar-gen2-phy",
188 .of_match = rcar_gen2_phy_of_match,
189 .ops = &rcar_gen2_phy_phy_ops,
190 .probe = rcar_gen2_phy_probe,
191 .remove = rcar_gen2_phy_remove,
192 .priv_auto_alloc_size = sizeof(struct rcar_gen2_phy),