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/of_device.h>
15 #include <linux/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/interrupt.h>
18 #include <linux/soc/brcmstb/brcmstb.h>
19 #include <dt-bindings/phy/phy.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/suspend.h>
23 #include "phy-brcm-usb-init.h"
25 static DEFINE_MUTEX(sysfs_lock);
27 enum brcm_usb_phy_id {
33 struct value_to_name_map {
38 struct match_chip_info {
39 void (*init_func)(struct brcm_usb_init_params *params);
40 u8 required_regs[BRCM_REGS_MAX + 1];
44 static const struct value_to_name_map brcm_dr_mode_to_name[] = {
45 { USB_CTLR_MODE_HOST, "host" },
46 { USB_CTLR_MODE_DEVICE, "peripheral" },
47 { USB_CTLR_MODE_DRD, "drd" },
48 { USB_CTLR_MODE_TYPEC_PD, "typec-pd" }
51 static const struct value_to_name_map brcm_dual_mode_to_name[] = {
63 struct brcm_usb_phy_data {
64 struct brcm_usb_init_params ini;
67 struct clk *usb_20_clk;
68 struct clk *usb_30_clk;
69 struct clk *suspend_clk;
70 struct mutex mutex; /* serialize phy init */
73 struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX];
74 struct notifier_block pm_notifier;
78 static s8 *node_reg_names[BRCM_REGS_MAX] = {
79 "crtl", "xhci_ec", "xhci_gbl", "usb_phy", "usb_mdio", "bdc_ec"
82 static int brcm_pm_notifier(struct notifier_block *notifier,
83 unsigned long pm_event,
86 struct brcm_usb_phy_data *priv =
87 container_of(notifier, struct brcm_usb_phy_data, pm_notifier);
90 case PM_HIBERNATION_PREPARE:
91 case PM_SUSPEND_PREPARE:
92 priv->pm_active = true;
95 case PM_POST_HIBERNATION:
97 priv->pm_active = false;
103 static irqreturn_t brcm_usb_phy_wake_isr(int irq, void *dev_id)
105 struct phy *gphy = dev_id;
107 pm_wakeup_event(&gphy->dev, 0);
112 static int brcm_usb_phy_init(struct phy *gphy)
114 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
115 struct brcm_usb_phy_data *priv =
116 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
122 * Use a lock to make sure a second caller waits until
123 * the base phy is inited before using it.
125 mutex_lock(&priv->mutex);
126 if (priv->init_count++ == 0) {
127 clk_prepare_enable(priv->usb_20_clk);
128 clk_prepare_enable(priv->usb_30_clk);
129 clk_prepare_enable(priv->suspend_clk);
130 brcm_usb_init_common(&priv->ini);
132 mutex_unlock(&priv->mutex);
133 if (phy->id == BRCM_USB_PHY_2_0)
134 brcm_usb_init_eohci(&priv->ini);
135 else if (phy->id == BRCM_USB_PHY_3_0)
136 brcm_usb_init_xhci(&priv->ini);
138 dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
144 static int brcm_usb_phy_exit(struct phy *gphy)
146 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
147 struct brcm_usb_phy_data *priv =
148 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
153 dev_dbg(&gphy->dev, "EXIT\n");
154 if (phy->id == BRCM_USB_PHY_2_0)
155 brcm_usb_uninit_eohci(&priv->ini);
156 if (phy->id == BRCM_USB_PHY_3_0)
157 brcm_usb_uninit_xhci(&priv->ini);
159 /* If both xhci and eohci are gone, reset everything else */
160 mutex_lock(&priv->mutex);
161 if (--priv->init_count == 0) {
162 brcm_usb_uninit_common(&priv->ini);
163 clk_disable_unprepare(priv->usb_20_clk);
164 clk_disable_unprepare(priv->usb_30_clk);
165 clk_disable_unprepare(priv->suspend_clk);
167 mutex_unlock(&priv->mutex);
172 static const struct phy_ops brcm_usb_phy_ops = {
173 .init = brcm_usb_phy_init,
174 .exit = brcm_usb_phy_exit,
175 .owner = THIS_MODULE,
178 static struct phy *brcm_usb_phy_xlate(struct device *dev,
179 struct of_phandle_args *args)
181 struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
184 * values 0 and 1 are for backward compatibility with
185 * device tree nodes from older bootloaders.
187 switch (args->args[0]) {
190 if (data->phys[BRCM_USB_PHY_2_0].phy)
191 return data->phys[BRCM_USB_PHY_2_0].phy;
192 dev_warn(dev, "Error, 2.0 Phy not found\n");
196 if (data->phys[BRCM_USB_PHY_3_0].phy)
197 return data->phys[BRCM_USB_PHY_3_0].phy;
198 dev_warn(dev, "Error, 3.0 Phy not found\n");
201 return ERR_PTR(-ENODEV);
204 static int name_to_value(const struct value_to_name_map *table, int count,
205 const char *name, int *value)
210 for (x = 0; x < count; x++) {
211 if (sysfs_streq(name, table[x].name)) {
219 static const char *value_to_name(const struct value_to_name_map *table, int count,
224 return table[value].name;
227 static ssize_t dr_mode_show(struct device *dev,
228 struct device_attribute *attr,
231 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
233 return sprintf(buf, "%s\n",
234 value_to_name(&brcm_dr_mode_to_name[0],
235 ARRAY_SIZE(brcm_dr_mode_to_name),
238 static DEVICE_ATTR_RO(dr_mode);
240 static ssize_t dual_select_store(struct device *dev,
241 struct device_attribute *attr,
242 const char *buf, size_t len)
244 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
248 mutex_lock(&sysfs_lock);
249 res = name_to_value(&brcm_dual_mode_to_name[0],
250 ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value);
252 brcm_usb_set_dual_select(&priv->ini, value);
255 mutex_unlock(&sysfs_lock);
259 static ssize_t dual_select_show(struct device *dev,
260 struct device_attribute *attr,
263 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
266 mutex_lock(&sysfs_lock);
267 value = brcm_usb_get_dual_select(&priv->ini);
268 mutex_unlock(&sysfs_lock);
269 return sprintf(buf, "%s\n",
270 value_to_name(&brcm_dual_mode_to_name[0],
271 ARRAY_SIZE(brcm_dual_mode_to_name),
274 static DEVICE_ATTR_RW(dual_select);
276 static struct attribute *brcm_usb_phy_attrs[] = {
277 &dev_attr_dr_mode.attr,
278 &dev_attr_dual_select.attr,
282 static const struct attribute_group brcm_usb_phy_group = {
283 .attrs = brcm_usb_phy_attrs,
286 static const struct match_chip_info chip_info_7216 = {
287 .init_func = &brcm_usb_dvr_init_7216,
296 static const struct match_chip_info chip_info_7211b0 = {
297 .init_func = &brcm_usb_dvr_init_7211b0,
306 .optional_reg = BRCM_REGS_BDC_EC,
309 static const struct match_chip_info chip_info_7445 = {
310 .init_func = &brcm_usb_dvr_init_7445,
318 static const struct of_device_id brcm_usb_dt_ids[] = {
320 .compatible = "brcm,bcm4908-usb-phy",
321 .data = &chip_info_7445,
324 .compatible = "brcm,bcm7216-usb-phy",
325 .data = &chip_info_7216,
328 .compatible = "brcm,bcm7211-usb-phy",
329 .data = &chip_info_7211b0,
332 .compatible = "brcm,brcmstb-usb-phy",
333 .data = &chip_info_7445,
338 static int brcm_usb_get_regs(struct platform_device *pdev,
339 enum brcmusb_reg_sel regs,
340 struct brcm_usb_init_params *ini,
343 struct resource *res;
345 /* Older DT nodes have ctrl and optional xhci_ec by index only */
346 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
347 node_reg_names[regs]);
349 if (regs == BRCM_REGS_CTRL) {
350 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
351 } else if (regs == BRCM_REGS_XHCI_EC) {
352 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
353 /* XHCI_EC registers are optional */
360 "Optional reg %s not found\n",
361 node_reg_names[regs]);
364 dev_err(&pdev->dev, "can't get %s base addr\n",
365 node_reg_names[regs]);
369 ini->regs[regs] = devm_ioremap_resource(&pdev->dev, res);
370 if (IS_ERR(ini->regs[regs])) {
371 dev_err(&pdev->dev, "can't map %s register space\n",
372 node_reg_names[regs]);
378 static int brcm_usb_phy_dvr_init(struct platform_device *pdev,
379 struct brcm_usb_phy_data *priv,
380 struct device_node *dn)
382 struct device *dev = &pdev->dev;
383 struct phy *gphy = NULL;
386 priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
387 if (IS_ERR(priv->usb_20_clk)) {
388 if (PTR_ERR(priv->usb_20_clk) == -EPROBE_DEFER)
389 return -EPROBE_DEFER;
390 dev_info(dev, "Clock not found in Device Tree\n");
391 priv->usb_20_clk = NULL;
393 err = clk_prepare_enable(priv->usb_20_clk);
397 if (priv->has_eohci) {
398 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
400 dev_err(dev, "failed to create EHCI/OHCI PHY\n");
401 return PTR_ERR(gphy);
403 priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
404 priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
405 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
408 if (priv->has_xhci) {
409 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
411 dev_err(dev, "failed to create XHCI PHY\n");
412 return PTR_ERR(gphy);
414 priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
415 priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
416 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
418 priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
419 if (IS_ERR(priv->usb_30_clk)) {
420 if (PTR_ERR(priv->usb_30_clk) == -EPROBE_DEFER)
421 return -EPROBE_DEFER;
423 "USB3.0 clock not found in Device Tree\n");
424 priv->usb_30_clk = NULL;
426 err = clk_prepare_enable(priv->usb_30_clk);
431 priv->suspend_clk = clk_get(dev, "usb0_freerun");
432 if (IS_ERR(priv->suspend_clk)) {
433 if (PTR_ERR(priv->suspend_clk) == -EPROBE_DEFER)
434 return -EPROBE_DEFER;
435 dev_err(dev, "Suspend Clock not found in Device Tree\n");
436 priv->suspend_clk = NULL;
439 priv->wake_irq = platform_get_irq_byname(pdev, "wake");
440 if (priv->wake_irq < 0)
441 priv->wake_irq = platform_get_irq_byname(pdev, "wakeup");
442 if (priv->wake_irq >= 0) {
443 err = devm_request_irq(dev, priv->wake_irq,
444 brcm_usb_phy_wake_isr, 0,
445 dev_name(dev), gphy);
448 device_set_wakeup_capable(dev, 1);
451 "Wake interrupt missing, system wake not supported\n");
457 static int brcm_usb_phy_probe(struct platform_device *pdev)
459 struct device *dev = &pdev->dev;
460 struct brcm_usb_phy_data *priv;
461 struct phy_provider *phy_provider;
462 struct device_node *dn = pdev->dev.of_node;
465 const struct match_chip_info *info;
469 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
472 platform_set_drvdata(pdev, priv);
474 priv->ini.family_id = brcmstb_get_family_id();
475 priv->ini.product_id = brcmstb_get_product_id();
477 info = of_device_get_match_data(&pdev->dev);
481 info->init_func(&priv->ini);
483 dev_dbg(dev, "Best mapping table is for %s\n",
484 priv->ini.family_name);
486 of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
487 of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
489 priv->ini.mode = USB_CTLR_MODE_HOST;
490 err = of_property_read_string(dn, "dr_mode", &mode);
492 name_to_value(&brcm_dr_mode_to_name[0],
493 ARRAY_SIZE(brcm_dr_mode_to_name),
494 mode, &priv->ini.mode);
496 if (of_property_read_bool(dn, "brcm,has-xhci"))
497 priv->has_xhci = true;
498 if (of_property_read_bool(dn, "brcm,has-eohci"))
499 priv->has_eohci = true;
501 for (x = 0; x < BRCM_REGS_MAX; x++) {
502 if (info->required_regs[x] >= BRCM_REGS_MAX)
505 err = brcm_usb_get_regs(pdev, info->required_regs[x],
510 if (info->optional_reg) {
511 err = brcm_usb_get_regs(pdev, info->optional_reg,
517 err = brcm_usb_phy_dvr_init(pdev, priv, dn);
521 priv->pm_notifier.notifier_call = brcm_pm_notifier;
522 register_pm_notifier(&priv->pm_notifier);
524 mutex_init(&priv->mutex);
526 /* make sure invert settings are correct */
527 brcm_usb_init_ipp(&priv->ini);
530 * Create sysfs entries for mode.
531 * Remove "dual_select" attribute if not in dual mode
533 if (priv->ini.mode != USB_CTLR_MODE_DRD)
534 brcm_usb_phy_attrs[1] = NULL;
535 err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
537 dev_warn(dev, "Error creating sysfs attributes\n");
539 /* Get piarbctl syscon if it exists */
540 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
543 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
544 "brcm,syscon-piarbctl");
546 priv->ini.syscon_piarbctl = rmap;
548 /* start with everything off */
550 brcm_usb_uninit_xhci(&priv->ini);
552 brcm_usb_uninit_eohci(&priv->ini);
553 brcm_usb_uninit_common(&priv->ini);
554 clk_disable_unprepare(priv->usb_20_clk);
555 clk_disable_unprepare(priv->usb_30_clk);
557 phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
559 return PTR_ERR_OR_ZERO(phy_provider);
562 static int brcm_usb_phy_remove(struct platform_device *pdev)
564 struct brcm_usb_phy_data *priv = dev_get_drvdata(&pdev->dev);
566 sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group);
567 unregister_pm_notifier(&priv->pm_notifier);
572 #ifdef CONFIG_PM_SLEEP
573 static int brcm_usb_phy_suspend(struct device *dev)
575 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
577 if (priv->init_count) {
578 dev_dbg(dev, "SUSPEND\n");
579 priv->ini.wake_enabled = device_may_wakeup(dev);
580 if (priv->phys[BRCM_USB_PHY_3_0].inited)
581 brcm_usb_uninit_xhci(&priv->ini);
582 if (priv->phys[BRCM_USB_PHY_2_0].inited)
583 brcm_usb_uninit_eohci(&priv->ini);
584 brcm_usb_uninit_common(&priv->ini);
587 * Handle the clocks unless needed for wake. This has
588 * to work for both older XHCI->3.0-clks, EOHCI->2.0-clks
589 * and newer XHCI->2.0-clks/3.0-clks.
592 if (!priv->ini.suspend_with_clocks) {
593 if (priv->phys[BRCM_USB_PHY_3_0].inited)
594 clk_disable_unprepare(priv->usb_30_clk);
595 if (priv->phys[BRCM_USB_PHY_2_0].inited ||
597 clk_disable_unprepare(priv->usb_20_clk);
599 if (priv->wake_irq >= 0)
600 enable_irq_wake(priv->wake_irq);
605 static int brcm_usb_phy_resume(struct device *dev)
607 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
609 clk_prepare_enable(priv->usb_20_clk);
610 clk_prepare_enable(priv->usb_30_clk);
611 brcm_usb_init_ipp(&priv->ini);
614 * Initialize anything that was previously initialized.
615 * Uninitialize anything that wasn't previously initialized.
617 if (priv->init_count) {
618 dev_dbg(dev, "RESUME\n");
619 if (priv->wake_irq >= 0)
620 disable_irq_wake(priv->wake_irq);
621 brcm_usb_init_common(&priv->ini);
622 if (priv->phys[BRCM_USB_PHY_2_0].inited) {
623 brcm_usb_init_eohci(&priv->ini);
624 } else if (priv->has_eohci) {
625 brcm_usb_uninit_eohci(&priv->ini);
626 clk_disable_unprepare(priv->usb_20_clk);
628 if (priv->phys[BRCM_USB_PHY_3_0].inited) {
629 brcm_usb_init_xhci(&priv->ini);
630 } else if (priv->has_xhci) {
631 brcm_usb_uninit_xhci(&priv->ini);
632 clk_disable_unprepare(priv->usb_30_clk);
633 if (!priv->has_eohci)
634 clk_disable_unprepare(priv->usb_20_clk);
638 brcm_usb_uninit_xhci(&priv->ini);
640 brcm_usb_uninit_eohci(&priv->ini);
641 brcm_usb_uninit_common(&priv->ini);
642 clk_disable_unprepare(priv->usb_20_clk);
643 clk_disable_unprepare(priv->usb_30_clk);
645 priv->ini.wake_enabled = false;
648 #endif /* CONFIG_PM_SLEEP */
650 static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
651 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
654 MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
656 static struct platform_driver brcm_usb_driver = {
657 .probe = brcm_usb_phy_probe,
658 .remove = brcm_usb_phy_remove,
660 .name = "brcmstb-usb-phy",
661 .pm = &brcm_usb_phy_pm_ops,
662 .of_match_table = brcm_usb_dt_ids,
666 module_platform_driver(brcm_usb_driver);
668 MODULE_ALIAS("platform:brcmstb-usb-phy");
669 MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
670 MODULE_DESCRIPTION("BRCM USB PHY driver");
671 MODULE_LICENSE("GPL v2");