1 // SPDX-License-Identifier: GPL-2.0-only
3 * phy-brcm-usb.c - Broadcom USB Phy Driver
5 * Copyright (C) 2015-2017 Broadcom
9 #include <linux/delay.h>
10 #include <linux/err.h>
12 #include <linux/module.h>
14 #include <linux/phy/phy.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/soc/brcmstb/brcmstb.h>
18 #include <dt-bindings/phy/phy.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/suspend.h>
22 #include "phy-brcm-usb-init.h"
24 static DEFINE_MUTEX(sysfs_lock);
26 enum brcm_usb_phy_id {
32 struct value_to_name_map {
37 struct match_chip_info {
39 u8 required_regs[BRCM_REGS_MAX + 1];
43 static const struct value_to_name_map brcm_dr_mode_to_name[] = {
44 { USB_CTLR_MODE_HOST, "host" },
45 { USB_CTLR_MODE_DEVICE, "peripheral" },
46 { USB_CTLR_MODE_DRD, "drd" },
47 { USB_CTLR_MODE_TYPEC_PD, "typec-pd" }
50 static const struct value_to_name_map brcm_dual_mode_to_name[] = {
62 struct brcm_usb_phy_data {
63 struct brcm_usb_init_params ini;
66 struct clk *usb_20_clk;
67 struct clk *usb_30_clk;
68 struct clk *suspend_clk;
69 struct mutex mutex; /* serialize phy init */
72 struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX];
73 struct notifier_block pm_notifier;
77 static s8 *node_reg_names[BRCM_REGS_MAX] = {
78 "crtl", "xhci_ec", "xhci_gbl", "usb_phy", "usb_mdio", "bdc_ec"
81 static int brcm_pm_notifier(struct notifier_block *notifier,
82 unsigned long pm_event,
85 struct brcm_usb_phy_data *priv =
86 container_of(notifier, struct brcm_usb_phy_data, pm_notifier);
89 case PM_HIBERNATION_PREPARE:
90 case PM_SUSPEND_PREPARE:
91 priv->pm_active = true;
94 case PM_POST_HIBERNATION:
96 priv->pm_active = false;
102 static irqreturn_t brcm_usb_phy_wake_isr(int irq, void *dev_id)
104 struct phy *gphy = dev_id;
106 pm_wakeup_event(&gphy->dev, 0);
111 static int brcm_usb_phy_init(struct phy *gphy)
113 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
114 struct brcm_usb_phy_data *priv =
115 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
121 * Use a lock to make sure a second caller waits until
122 * the base phy is inited before using it.
124 mutex_lock(&priv->mutex);
125 if (priv->init_count++ == 0) {
126 clk_prepare_enable(priv->usb_20_clk);
127 clk_prepare_enable(priv->usb_30_clk);
128 clk_prepare_enable(priv->suspend_clk);
129 brcm_usb_init_common(&priv->ini);
131 mutex_unlock(&priv->mutex);
132 if (phy->id == BRCM_USB_PHY_2_0)
133 brcm_usb_init_eohci(&priv->ini);
134 else if (phy->id == BRCM_USB_PHY_3_0)
135 brcm_usb_init_xhci(&priv->ini);
137 dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
143 static int brcm_usb_phy_exit(struct phy *gphy)
145 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
146 struct brcm_usb_phy_data *priv =
147 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
152 dev_dbg(&gphy->dev, "EXIT\n");
153 if (phy->id == BRCM_USB_PHY_2_0)
154 brcm_usb_uninit_eohci(&priv->ini);
155 if (phy->id == BRCM_USB_PHY_3_0)
156 brcm_usb_uninit_xhci(&priv->ini);
158 /* If both xhci and eohci are gone, reset everything else */
159 mutex_lock(&priv->mutex);
160 if (--priv->init_count == 0) {
161 brcm_usb_uninit_common(&priv->ini);
162 clk_disable_unprepare(priv->usb_20_clk);
163 clk_disable_unprepare(priv->usb_30_clk);
164 clk_disable_unprepare(priv->suspend_clk);
166 mutex_unlock(&priv->mutex);
171 static const struct phy_ops brcm_usb_phy_ops = {
172 .init = brcm_usb_phy_init,
173 .exit = brcm_usb_phy_exit,
174 .owner = THIS_MODULE,
177 static struct phy *brcm_usb_phy_xlate(struct device *dev,
178 struct of_phandle_args *args)
180 struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
183 * values 0 and 1 are for backward compatibility with
184 * device tree nodes from older bootloaders.
186 switch (args->args[0]) {
189 if (data->phys[BRCM_USB_PHY_2_0].phy)
190 return data->phys[BRCM_USB_PHY_2_0].phy;
191 dev_warn(dev, "Error, 2.0 Phy not found\n");
195 if (data->phys[BRCM_USB_PHY_3_0].phy)
196 return data->phys[BRCM_USB_PHY_3_0].phy;
197 dev_warn(dev, "Error, 3.0 Phy not found\n");
200 return ERR_PTR(-ENODEV);
203 static int name_to_value(const struct value_to_name_map *table, int count,
204 const char *name, int *value)
209 for (x = 0; x < count; x++) {
210 if (sysfs_streq(name, table[x].name)) {
218 static const char *value_to_name(const struct value_to_name_map *table, int count,
223 return table[value].name;
226 static ssize_t dr_mode_show(struct device *dev,
227 struct device_attribute *attr,
230 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
232 return sprintf(buf, "%s\n",
233 value_to_name(&brcm_dr_mode_to_name[0],
234 ARRAY_SIZE(brcm_dr_mode_to_name),
237 static DEVICE_ATTR_RO(dr_mode);
239 static ssize_t dual_select_store(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf, size_t len)
243 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
247 mutex_lock(&sysfs_lock);
248 res = name_to_value(&brcm_dual_mode_to_name[0],
249 ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value);
251 brcm_usb_set_dual_select(&priv->ini, value);
254 mutex_unlock(&sysfs_lock);
258 static ssize_t dual_select_show(struct device *dev,
259 struct device_attribute *attr,
262 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
265 mutex_lock(&sysfs_lock);
266 value = brcm_usb_get_dual_select(&priv->ini);
267 mutex_unlock(&sysfs_lock);
268 return sprintf(buf, "%s\n",
269 value_to_name(&brcm_dual_mode_to_name[0],
270 ARRAY_SIZE(brcm_dual_mode_to_name),
273 static DEVICE_ATTR_RW(dual_select);
275 static struct attribute *brcm_usb_phy_attrs[] = {
276 &dev_attr_dr_mode.attr,
277 &dev_attr_dual_select.attr,
281 static const struct attribute_group brcm_usb_phy_group = {
282 .attrs = brcm_usb_phy_attrs,
285 static const struct match_chip_info chip_info_7216 = {
286 .init_func = &brcm_usb_dvr_init_7216,
295 static const struct match_chip_info chip_info_7211b0 = {
296 .init_func = &brcm_usb_dvr_init_7211b0,
305 .optional_reg = BRCM_REGS_BDC_EC,
308 static const struct match_chip_info chip_info_7445 = {
309 .init_func = &brcm_usb_dvr_init_7445,
317 static const struct of_device_id brcm_usb_dt_ids[] = {
319 .compatible = "brcm,bcm7216-usb-phy",
320 .data = &chip_info_7216,
323 .compatible = "brcm,bcm7211-usb-phy",
324 .data = &chip_info_7211b0,
327 .compatible = "brcm,brcmstb-usb-phy",
328 .data = &chip_info_7445,
333 static int brcm_usb_get_regs(struct platform_device *pdev,
334 enum brcmusb_reg_sel regs,
335 struct brcm_usb_init_params *ini,
338 struct resource *res;
340 /* Older DT nodes have ctrl and optional xhci_ec by index only */
341 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
342 node_reg_names[regs]);
344 if (regs == BRCM_REGS_CTRL) {
345 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
346 } else if (regs == BRCM_REGS_XHCI_EC) {
347 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
348 /* XHCI_EC registers are optional */
355 "Optional reg %s not found\n",
356 node_reg_names[regs]);
359 dev_err(&pdev->dev, "can't get %s base addr\n",
360 node_reg_names[regs]);
364 ini->regs[regs] = devm_ioremap_resource(&pdev->dev, res);
365 if (IS_ERR(ini->regs[regs])) {
366 dev_err(&pdev->dev, "can't map %s register space\n",
367 node_reg_names[regs]);
373 static int brcm_usb_phy_dvr_init(struct platform_device *pdev,
374 struct brcm_usb_phy_data *priv,
375 struct device_node *dn)
377 struct device *dev = &pdev->dev;
378 struct phy *gphy = NULL;
381 priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
382 if (IS_ERR(priv->usb_20_clk)) {
383 if (PTR_ERR(priv->usb_20_clk) == -EPROBE_DEFER)
384 return -EPROBE_DEFER;
385 dev_info(dev, "Clock not found in Device Tree\n");
386 priv->usb_20_clk = NULL;
388 err = clk_prepare_enable(priv->usb_20_clk);
392 if (priv->has_eohci) {
393 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
395 dev_err(dev, "failed to create EHCI/OHCI PHY\n");
396 return PTR_ERR(gphy);
398 priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
399 priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
400 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
403 if (priv->has_xhci) {
404 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
406 dev_err(dev, "failed to create XHCI PHY\n");
407 return PTR_ERR(gphy);
409 priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
410 priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
411 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
413 priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
414 if (IS_ERR(priv->usb_30_clk)) {
415 if (PTR_ERR(priv->usb_30_clk) == -EPROBE_DEFER)
416 return -EPROBE_DEFER;
418 "USB3.0 clock not found in Device Tree\n");
419 priv->usb_30_clk = NULL;
421 err = clk_prepare_enable(priv->usb_30_clk);
426 priv->suspend_clk = clk_get(dev, "usb0_freerun");
427 if (IS_ERR(priv->suspend_clk)) {
428 if (PTR_ERR(priv->suspend_clk) == -EPROBE_DEFER)
429 return -EPROBE_DEFER;
430 dev_err(dev, "Suspend Clock not found in Device Tree\n");
431 priv->suspend_clk = NULL;
434 priv->wake_irq = platform_get_irq_byname(pdev, "wake");
435 if (priv->wake_irq < 0)
436 priv->wake_irq = platform_get_irq_byname(pdev, "wakeup");
437 if (priv->wake_irq >= 0) {
438 err = devm_request_irq(dev, priv->wake_irq,
439 brcm_usb_phy_wake_isr, 0,
440 dev_name(dev), gphy);
443 device_set_wakeup_capable(dev, 1);
446 "Wake interrupt missing, system wake not supported\n");
452 static int brcm_usb_phy_probe(struct platform_device *pdev)
454 struct device *dev = &pdev->dev;
455 struct brcm_usb_phy_data *priv;
456 struct phy_provider *phy_provider;
457 struct device_node *dn = pdev->dev.of_node;
460 const struct of_device_id *match;
461 void (*dvr_init)(struct brcm_usb_init_params *params);
462 const struct match_chip_info *info;
466 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
469 platform_set_drvdata(pdev, priv);
471 priv->ini.family_id = brcmstb_get_family_id();
472 priv->ini.product_id = brcmstb_get_product_id();
474 match = of_match_node(brcm_usb_dt_ids, dev->of_node);
476 dvr_init = info->init_func;
477 (*dvr_init)(&priv->ini);
479 dev_dbg(dev, "Best mapping table is for %s\n",
480 priv->ini.family_name);
482 of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
483 of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
485 priv->ini.mode = USB_CTLR_MODE_HOST;
486 err = of_property_read_string(dn, "dr_mode", &mode);
488 name_to_value(&brcm_dr_mode_to_name[0],
489 ARRAY_SIZE(brcm_dr_mode_to_name),
490 mode, &priv->ini.mode);
492 if (of_property_read_bool(dn, "brcm,has-xhci"))
493 priv->has_xhci = true;
494 if (of_property_read_bool(dn, "brcm,has-eohci"))
495 priv->has_eohci = true;
497 for (x = 0; x < BRCM_REGS_MAX; x++) {
498 if (info->required_regs[x] >= BRCM_REGS_MAX)
501 err = brcm_usb_get_regs(pdev, info->required_regs[x],
506 if (info->optional_reg) {
507 err = brcm_usb_get_regs(pdev, info->optional_reg,
513 err = brcm_usb_phy_dvr_init(pdev, priv, dn);
517 priv->pm_notifier.notifier_call = brcm_pm_notifier;
518 register_pm_notifier(&priv->pm_notifier);
520 mutex_init(&priv->mutex);
522 /* make sure invert settings are correct */
523 brcm_usb_init_ipp(&priv->ini);
526 * Create sysfs entries for mode.
527 * Remove "dual_select" attribute if not in dual mode
529 if (priv->ini.mode != USB_CTLR_MODE_DRD)
530 brcm_usb_phy_attrs[1] = NULL;
531 err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
533 dev_warn(dev, "Error creating sysfs attributes\n");
535 /* Get piarbctl syscon if it exists */
536 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
539 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
540 "brcm,syscon-piarbctl");
542 priv->ini.syscon_piarbctl = rmap;
544 /* start with everything off */
546 brcm_usb_uninit_xhci(&priv->ini);
548 brcm_usb_uninit_eohci(&priv->ini);
549 brcm_usb_uninit_common(&priv->ini);
550 clk_disable_unprepare(priv->usb_20_clk);
551 clk_disable_unprepare(priv->usb_30_clk);
553 phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
555 return PTR_ERR_OR_ZERO(phy_provider);
558 static int brcm_usb_phy_remove(struct platform_device *pdev)
560 struct brcm_usb_phy_data *priv = dev_get_drvdata(&pdev->dev);
562 sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group);
563 unregister_pm_notifier(&priv->pm_notifier);
568 #ifdef CONFIG_PM_SLEEP
569 static int brcm_usb_phy_suspend(struct device *dev)
571 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
573 if (priv->init_count) {
574 dev_dbg(dev, "SUSPEND\n");
575 priv->ini.wake_enabled = device_may_wakeup(dev);
576 if (priv->phys[BRCM_USB_PHY_3_0].inited)
577 brcm_usb_uninit_xhci(&priv->ini);
578 if (priv->phys[BRCM_USB_PHY_2_0].inited)
579 brcm_usb_uninit_eohci(&priv->ini);
580 brcm_usb_uninit_common(&priv->ini);
583 * Handle the clocks unless needed for wake. This has
584 * to work for both older XHCI->3.0-clks, EOHCI->2.0-clks
585 * and newer XHCI->2.0-clks/3.0-clks.
588 if (!priv->ini.suspend_with_clocks) {
589 if (priv->phys[BRCM_USB_PHY_3_0].inited)
590 clk_disable_unprepare(priv->usb_30_clk);
591 if (priv->phys[BRCM_USB_PHY_2_0].inited ||
593 clk_disable_unprepare(priv->usb_20_clk);
595 if (priv->wake_irq >= 0)
596 enable_irq_wake(priv->wake_irq);
601 static int brcm_usb_phy_resume(struct device *dev)
603 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
605 clk_prepare_enable(priv->usb_20_clk);
606 clk_prepare_enable(priv->usb_30_clk);
607 brcm_usb_init_ipp(&priv->ini);
610 * Initialize anything that was previously initialized.
611 * Uninitialize anything that wasn't previously initialized.
613 if (priv->init_count) {
614 dev_dbg(dev, "RESUME\n");
615 if (priv->wake_irq >= 0)
616 disable_irq_wake(priv->wake_irq);
617 brcm_usb_init_common(&priv->ini);
618 if (priv->phys[BRCM_USB_PHY_2_0].inited) {
619 brcm_usb_init_eohci(&priv->ini);
620 } else if (priv->has_eohci) {
621 brcm_usb_uninit_eohci(&priv->ini);
622 clk_disable_unprepare(priv->usb_20_clk);
624 if (priv->phys[BRCM_USB_PHY_3_0].inited) {
625 brcm_usb_init_xhci(&priv->ini);
626 } else if (priv->has_xhci) {
627 brcm_usb_uninit_xhci(&priv->ini);
628 clk_disable_unprepare(priv->usb_30_clk);
629 if (!priv->has_eohci)
630 clk_disable_unprepare(priv->usb_20_clk);
634 brcm_usb_uninit_xhci(&priv->ini);
636 brcm_usb_uninit_eohci(&priv->ini);
637 brcm_usb_uninit_common(&priv->ini);
638 clk_disable_unprepare(priv->usb_20_clk);
639 clk_disable_unprepare(priv->usb_30_clk);
641 priv->ini.wake_enabled = false;
644 #endif /* CONFIG_PM_SLEEP */
646 static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
647 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
650 MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
652 static struct platform_driver brcm_usb_driver = {
653 .probe = brcm_usb_phy_probe,
654 .remove = brcm_usb_phy_remove,
656 .name = "brcmstb-usb-phy",
657 .pm = &brcm_usb_phy_pm_ops,
658 .of_match_table = brcm_usb_dt_ids,
662 module_platform_driver(brcm_usb_driver);
664 MODULE_ALIAS("platform:brcmstb-usb-phy");
665 MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
666 MODULE_DESCRIPTION("BRCM USB PHY driver");
667 MODULE_LICENSE("GPL v2");