1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2018 Broadcom
6 #include <linux/delay.h>
8 #include <linux/module.h>
10 #include <linux/phy/phy.h>
11 #include <linux/platform_device.h>
13 enum bcm_usb_phy_version {
18 enum bcm_usb_phy_reg {
24 /* USB PHY registers */
26 static const u8 bcm_usb_combo_phy_ss[] = {
31 static const u8 bcm_usb_combo_phy_hs[] = {
36 static const u8 bcm_usb_hs_phy[] = {
48 static const u8 u3pll_ctrl[] = {
50 [SSPLL_SUSPEND_EN] = 1,
55 #define HSPLL_PDIV_MASK 0xF
56 #define HSPLL_PDIV_VAL 0x1
58 static const u8 u2pll_ctrl[] = {
63 enum bcm_usb_phy_ctrl_bits {
69 #define PHY_PCTL_MASK 0xffff
70 #define SSPHY_PCTL_VAL 0x0006
72 static const u8 u3phy_ctrl[] = {
77 static const u8 u2phy_ctrl[] = {
83 struct bcm_usb_phy_cfg {
91 #define PLL_LOCK_RETRY_COUNT 1000
93 enum bcm_usb_phy_type {
98 #define NUM_BCM_SR_USB_COMBO_PHYS 2
100 static inline void bcm_usb_reg32_clrbits(void __iomem *addr, uint32_t clear)
102 writel(readl(addr) & ~clear, addr);
105 static inline void bcm_usb_reg32_setbits(void __iomem *addr, uint32_t set)
107 writel(readl(addr) | set, addr);
110 static int bcm_usb_pll_lock_check(void __iomem *addr, u32 bit)
115 retry = PLL_LOCK_RETRY_COUNT;
117 rd_data = readl(addr);
121 } while (--retry > 0);
123 pr_err("%s: FAIL\n", __func__);
127 static int bcm_usb_ss_phy_init(struct bcm_usb_phy_cfg *phy_cfg)
130 void __iomem *regs = phy_cfg->regs;
134 offset = phy_cfg->offset;
136 /* Set pctl with mode and soft reset */
137 rd_data = readl(regs + offset[PHY_CTRL]);
138 rd_data &= ~(PHY_PCTL_MASK << u3phy_ctrl[PHY_PCTL]);
139 rd_data |= (SSPHY_PCTL_VAL << u3phy_ctrl[PHY_PCTL]);
140 writel(rd_data, regs + offset[PHY_CTRL]);
142 bcm_usb_reg32_clrbits(regs + offset[PLL_CTRL],
143 BIT(u3pll_ctrl[SSPLL_SUSPEND_EN]));
144 bcm_usb_reg32_setbits(regs + offset[PLL_CTRL],
145 BIT(u3pll_ctrl[PLL_SEQ_START]));
146 bcm_usb_reg32_setbits(regs + offset[PLL_CTRL],
147 BIT(u3pll_ctrl[PLL_RESETB]));
149 /* Maximum timeout for PLL reset done */
152 ret = bcm_usb_pll_lock_check(regs + offset[PLL_CTRL],
153 BIT(u3pll_ctrl[PLL_LOCK]));
158 static int bcm_usb_hs_phy_init(struct bcm_usb_phy_cfg *phy_cfg)
161 void __iomem *regs = phy_cfg->regs;
164 offset = phy_cfg->offset;
166 bcm_usb_reg32_clrbits(regs + offset[PLL_CTRL],
167 BIT(u2pll_ctrl[PLL_RESETB]));
168 bcm_usb_reg32_setbits(regs + offset[PLL_CTRL],
169 BIT(u2pll_ctrl[PLL_RESETB]));
171 ret = bcm_usb_pll_lock_check(regs + offset[PLL_CTRL],
172 BIT(u2pll_ctrl[PLL_LOCK]));
177 static int bcm_usb_phy_reset(struct phy *phy)
179 struct bcm_usb_phy_cfg *phy_cfg = phy_get_drvdata(phy);
180 void __iomem *regs = phy_cfg->regs;
183 offset = phy_cfg->offset;
185 if (phy_cfg->type == USB_HS_PHY) {
186 bcm_usb_reg32_clrbits(regs + offset[PHY_CTRL],
187 BIT(u2phy_ctrl[CORERDY]));
188 bcm_usb_reg32_setbits(regs + offset[PHY_CTRL],
189 BIT(u2phy_ctrl[CORERDY]));
195 static int bcm_usb_phy_init(struct phy *phy)
197 struct bcm_usb_phy_cfg *phy_cfg = phy_get_drvdata(phy);
200 if (phy_cfg->type == USB_SS_PHY)
201 ret = bcm_usb_ss_phy_init(phy_cfg);
202 else if (phy_cfg->type == USB_HS_PHY)
203 ret = bcm_usb_hs_phy_init(phy_cfg);
208 static const struct phy_ops sr_phy_ops = {
209 .init = bcm_usb_phy_init,
210 .reset = bcm_usb_phy_reset,
211 .owner = THIS_MODULE,
214 static struct phy *bcm_usb_phy_xlate(struct device *dev,
215 struct of_phandle_args *args)
217 struct bcm_usb_phy_cfg *phy_cfg;
220 phy_cfg = dev_get_drvdata(dev);
222 return ERR_PTR(-EINVAL);
224 if (phy_cfg->version == BCM_SR_USB_COMBO_PHY) {
225 phy_idx = args->args[0];
227 if (WARN_ON(phy_idx > 1))
228 return ERR_PTR(-ENODEV);
230 return phy_cfg[phy_idx].phy;
235 static int bcm_usb_phy_create(struct device *dev, struct device_node *node,
236 void __iomem *regs, uint32_t version)
238 struct bcm_usb_phy_cfg *phy_cfg;
241 if (version == BCM_SR_USB_COMBO_PHY) {
242 phy_cfg = devm_kzalloc(dev, NUM_BCM_SR_USB_COMBO_PHYS *
243 sizeof(struct bcm_usb_phy_cfg),
248 for (idx = 0; idx < NUM_BCM_SR_USB_COMBO_PHYS; idx++) {
249 phy_cfg[idx].regs = regs;
250 phy_cfg[idx].version = version;
252 phy_cfg[idx].offset = bcm_usb_combo_phy_hs;
253 phy_cfg[idx].type = USB_HS_PHY;
255 phy_cfg[idx].offset = bcm_usb_combo_phy_ss;
256 phy_cfg[idx].type = USB_SS_PHY;
258 phy_cfg[idx].phy = devm_phy_create(dev, node,
260 if (IS_ERR(phy_cfg[idx].phy))
261 return PTR_ERR(phy_cfg[idx].phy);
263 phy_set_drvdata(phy_cfg[idx].phy, &phy_cfg[idx]);
265 } else if (version == BCM_SR_USB_HS_PHY) {
266 phy_cfg = devm_kzalloc(dev, sizeof(struct bcm_usb_phy_cfg),
271 phy_cfg->regs = regs;
272 phy_cfg->version = version;
273 phy_cfg->offset = bcm_usb_hs_phy;
274 phy_cfg->type = USB_HS_PHY;
275 phy_cfg->phy = devm_phy_create(dev, node, &sr_phy_ops);
276 if (IS_ERR(phy_cfg->phy))
277 return PTR_ERR(phy_cfg->phy);
279 phy_set_drvdata(phy_cfg->phy, phy_cfg);
283 dev_set_drvdata(dev, phy_cfg);
288 static const struct of_device_id bcm_usb_phy_of_match[] = {
290 .compatible = "brcm,sr-usb-combo-phy",
291 .data = (void *)BCM_SR_USB_COMBO_PHY,
294 .compatible = "brcm,sr-usb-hs-phy",
295 .data = (void *)BCM_SR_USB_HS_PHY,
299 MODULE_DEVICE_TABLE(of, bcm_usb_phy_of_match);
301 static int bcm_usb_phy_probe(struct platform_device *pdev)
303 struct device *dev = &pdev->dev;
304 struct device_node *dn = dev->of_node;
305 const struct of_device_id *of_id;
306 struct resource *res;
309 enum bcm_usb_phy_version version;
310 struct phy_provider *phy_provider;
312 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313 regs = devm_ioremap_resource(dev, res);
315 return PTR_ERR(regs);
317 of_id = of_match_node(bcm_usb_phy_of_match, dn);
319 version = (enum bcm_usb_phy_version)of_id->data;
323 ret = bcm_usb_phy_create(dev, dn, regs, version);
327 phy_provider = devm_of_phy_provider_register(dev, bcm_usb_phy_xlate);
329 return PTR_ERR_OR_ZERO(phy_provider);
332 static struct platform_driver bcm_usb_phy_driver = {
334 .name = "phy-bcm-sr-usb",
335 .of_match_table = bcm_usb_phy_of_match,
337 .probe = bcm_usb_phy_probe,
339 module_platform_driver(bcm_usb_phy_driver);
341 MODULE_AUTHOR("Broadcom");
342 MODULE_DESCRIPTION("Broadcom stingray USB Phy driver");
343 MODULE_LICENSE("GPL v2");