1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
8 #include <asm/global_data.h>
14 #include <generic-phy.h>
15 #include <linux/libfdt.h>
17 #include <reset-uclass.h>
20 #include <linux/printk.h>
22 #include <linux/bitops.h>
23 #include <linux/compat.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 /* Default PHY_SEL and REFCLKSEL configuration */
28 #define STIH407_USB_PICOPHY_CTRL_PORT_CONF 0x6
30 /* ports parameters overriding */
31 #define STIH407_USB_PICOPHY_PARAM_DEF 0x39a4dc
33 #define PHYPARAM_REG 1
38 struct regmap *regmap;
39 struct reset_ctl global_ctl;
40 struct reset_ctl port_ctl;
45 static int sti_usb_phy_deassert(struct sti_usb_phy *phy)
49 ret = reset_deassert(&phy->global_ctl);
51 pr_err("PHY global deassert failed: %d", ret);
55 ret = reset_deassert(&phy->port_ctl);
57 pr_err("PHY port deassert failed: %d", ret);
62 static int sti_usb_phy_init(struct phy *usb_phy)
64 struct udevice *dev = usb_phy->dev;
65 struct sti_usb_phy *phy = dev_get_priv(dev);
68 /* set ctrl picophy value */
69 reg = (void __iomem *)phy->regmap->ranges[0].start + phy->ctrl;
70 /* CTRL_PORT mask is 0x1f */
71 clrsetbits_le32(reg, 0x1f, STIH407_USB_PICOPHY_CTRL_PORT_CONF);
73 /* set ports parameters overriding */
74 reg = (void __iomem *)phy->regmap->ranges[0].start + phy->param;
75 /* PARAM_DEF mask is 0xffffffff */
76 clrsetbits_le32(reg, 0xffffffff, STIH407_USB_PICOPHY_PARAM_DEF);
78 return sti_usb_phy_deassert(phy);
81 static int sti_usb_phy_exit(struct phy *usb_phy)
83 struct udevice *dev = usb_phy->dev;
84 struct sti_usb_phy *phy = dev_get_priv(dev);
87 ret = reset_assert(&phy->port_ctl);
89 pr_err("PHY port assert failed: %d", ret);
93 ret = reset_assert(&phy->global_ctl);
95 pr_err("PHY global assert failed: %d", ret);
100 struct phy_ops sti_usb_phy_ops = {
101 .init = sti_usb_phy_init,
102 .exit = sti_usb_phy_exit,
105 int sti_usb_phy_probe(struct udevice *dev)
107 struct sti_usb_phy *priv = dev_get_priv(dev);
108 struct udevice *syscon;
109 struct ofnode_phandle_args syscfg_phandle;
110 u32 cells[PHYPARAM_NB];
113 /* get corresponding syscon phandle */
114 ret = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
118 pr_err("Can't get syscfg phandle: %d\n", ret);
122 ret = uclass_get_device_by_ofnode(UCLASS_SYSCON, syscfg_phandle.node,
125 pr_err("unable to find syscon device (%d)\n", ret);
129 priv->regmap = syscon_get_regmap(syscon);
131 pr_err("unable to find regmap\n");
135 /* get phy param offset */
136 count = fdtdec_get_int_array_count(gd->fdt_blob, dev_of_offset(dev),
141 pr_err("Bad PHY st,syscfg property %d\n", count);
145 if (count > PHYPARAM_NB) {
146 pr_err("Unsupported PHY param count %d\n", count);
150 priv->param = cells[PHYPARAM_REG];
151 priv->ctrl = cells[PHYCTRL_REG];
153 /* get global reset control */
154 ret = reset_get_by_name(dev, "global", &priv->global_ctl);
156 pr_err("can't get global reset for %s (%d)", dev->name, ret);
160 /* get port reset control */
161 ret = reset_get_by_name(dev, "port", &priv->port_ctl);
163 pr_err("can't get port reset for %s (%d)", dev->name, ret);
170 static const struct udevice_id sti_usb_phy_ids[] = {
171 { .compatible = "st,stih407-usb2-phy" },
175 U_BOOT_DRIVER(sti_usb_phy) = {
176 .name = "sti_usb_phy",
178 .of_match = sti_usb_phy_ids,
179 .probe = sti_usb_phy_probe,
180 .ops = &sti_usb_phy_ops,
181 .priv_auto = sizeof(struct sti_usb_phy),