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>
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 {
38 void (*init_func)(struct brcm_usb_init_params *params);
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];
75 static s8 *node_reg_names[BRCM_REGS_MAX] = {
76 "crtl", "xhci_ec", "xhci_gbl", "usb_phy", "usb_mdio", "bdc_ec"
79 static irqreturn_t brcm_usb_phy_wake_isr(int irq, void *dev_id)
81 struct phy *gphy = dev_id;
83 pm_wakeup_event(&gphy->dev, 0);
88 static int brcm_usb_phy_init(struct phy *gphy)
90 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
91 struct brcm_usb_phy_data *priv =
92 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
95 * Use a lock to make sure a second caller waits until
96 * the base phy is inited before using it.
98 mutex_lock(&priv->mutex);
99 if (priv->init_count++ == 0) {
100 clk_prepare_enable(priv->usb_20_clk);
101 clk_prepare_enable(priv->usb_30_clk);
102 clk_prepare_enable(priv->suspend_clk);
103 brcm_usb_init_common(&priv->ini);
105 mutex_unlock(&priv->mutex);
106 if (phy->id == BRCM_USB_PHY_2_0)
107 brcm_usb_init_eohci(&priv->ini);
108 else if (phy->id == BRCM_USB_PHY_3_0)
109 brcm_usb_init_xhci(&priv->ini);
111 dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
117 static int brcm_usb_phy_exit(struct phy *gphy)
119 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
120 struct brcm_usb_phy_data *priv =
121 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
123 dev_dbg(&gphy->dev, "EXIT\n");
124 if (phy->id == BRCM_USB_PHY_2_0)
125 brcm_usb_uninit_eohci(&priv->ini);
126 if (phy->id == BRCM_USB_PHY_3_0)
127 brcm_usb_uninit_xhci(&priv->ini);
129 /* If both xhci and eohci are gone, reset everything else */
130 mutex_lock(&priv->mutex);
131 if (--priv->init_count == 0) {
132 brcm_usb_uninit_common(&priv->ini);
133 clk_disable_unprepare(priv->usb_20_clk);
134 clk_disable_unprepare(priv->usb_30_clk);
135 clk_disable_unprepare(priv->suspend_clk);
137 mutex_unlock(&priv->mutex);
142 static const struct phy_ops brcm_usb_phy_ops = {
143 .init = brcm_usb_phy_init,
144 .exit = brcm_usb_phy_exit,
145 .owner = THIS_MODULE,
148 static struct phy *brcm_usb_phy_xlate(struct device *dev,
149 struct of_phandle_args *args)
151 struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
154 * values 0 and 1 are for backward compatibility with
155 * device tree nodes from older bootloaders.
157 switch (args->args[0]) {
160 if (data->phys[BRCM_USB_PHY_2_0].phy)
161 return data->phys[BRCM_USB_PHY_2_0].phy;
162 dev_warn(dev, "Error, 2.0 Phy not found\n");
166 if (data->phys[BRCM_USB_PHY_3_0].phy)
167 return data->phys[BRCM_USB_PHY_3_0].phy;
168 dev_warn(dev, "Error, 3.0 Phy not found\n");
171 return ERR_PTR(-ENODEV);
174 static int name_to_value(const struct value_to_name_map *table, int count,
175 const char *name, int *value)
180 for (x = 0; x < count; x++) {
181 if (sysfs_streq(name, table[x].name)) {
189 static const char *value_to_name(const struct value_to_name_map *table, int count,
194 return table[value].name;
197 static ssize_t dr_mode_show(struct device *dev,
198 struct device_attribute *attr,
201 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
203 return sprintf(buf, "%s\n",
204 value_to_name(&brcm_dr_mode_to_name[0],
205 ARRAY_SIZE(brcm_dr_mode_to_name),
208 static DEVICE_ATTR_RO(dr_mode);
210 static ssize_t dual_select_store(struct device *dev,
211 struct device_attribute *attr,
212 const char *buf, size_t len)
214 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
218 mutex_lock(&sysfs_lock);
219 res = name_to_value(&brcm_dual_mode_to_name[0],
220 ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value);
222 brcm_usb_set_dual_select(&priv->ini, value);
225 mutex_unlock(&sysfs_lock);
229 static ssize_t dual_select_show(struct device *dev,
230 struct device_attribute *attr,
233 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
236 mutex_lock(&sysfs_lock);
237 value = brcm_usb_get_dual_select(&priv->ini);
238 mutex_unlock(&sysfs_lock);
239 return sprintf(buf, "%s\n",
240 value_to_name(&brcm_dual_mode_to_name[0],
241 ARRAY_SIZE(brcm_dual_mode_to_name),
244 static DEVICE_ATTR_RW(dual_select);
246 static struct attribute *brcm_usb_phy_attrs[] = {
247 &dev_attr_dr_mode.attr,
248 &dev_attr_dual_select.attr,
252 static const struct attribute_group brcm_usb_phy_group = {
253 .attrs = brcm_usb_phy_attrs,
256 static const struct match_chip_info chip_info_7216 = {
257 .init_func = &brcm_usb_dvr_init_7216,
266 static const struct match_chip_info chip_info_7211b0 = {
267 .init_func = &brcm_usb_dvr_init_7211b0,
276 .optional_reg = BRCM_REGS_BDC_EC,
279 static const struct match_chip_info chip_info_7445 = {
280 .init_func = &brcm_usb_dvr_init_7445,
288 static const struct of_device_id brcm_usb_dt_ids[] = {
290 .compatible = "brcm,bcm4908-usb-phy",
291 .data = &chip_info_7445,
294 .compatible = "brcm,bcm7216-usb-phy",
295 .data = &chip_info_7216,
298 .compatible = "brcm,bcm7211-usb-phy",
299 .data = &chip_info_7211b0,
302 .compatible = "brcm,brcmstb-usb-phy",
303 .data = &chip_info_7445,
308 static int brcm_usb_get_regs(struct platform_device *pdev,
309 enum brcmusb_reg_sel regs,
310 struct brcm_usb_init_params *ini,
313 struct resource *res;
315 /* Older DT nodes have ctrl and optional xhci_ec by index only */
316 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
317 node_reg_names[regs]);
319 if (regs == BRCM_REGS_CTRL) {
320 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
321 } else if (regs == BRCM_REGS_XHCI_EC) {
322 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
323 /* XHCI_EC registers are optional */
330 "Optional reg %s not found\n",
331 node_reg_names[regs]);
334 dev_err(&pdev->dev, "can't get %s base addr\n",
335 node_reg_names[regs]);
339 ini->regs[regs] = devm_ioremap_resource(&pdev->dev, res);
340 if (IS_ERR(ini->regs[regs])) {
341 dev_err(&pdev->dev, "can't map %s register space\n",
342 node_reg_names[regs]);
348 static int brcm_usb_phy_dvr_init(struct platform_device *pdev,
349 struct brcm_usb_phy_data *priv,
350 struct device_node *dn)
352 struct device *dev = &pdev->dev;
353 struct phy *gphy = NULL;
356 priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
357 if (IS_ERR(priv->usb_20_clk)) {
358 if (PTR_ERR(priv->usb_20_clk) == -EPROBE_DEFER)
359 return -EPROBE_DEFER;
360 dev_info(dev, "Clock not found in Device Tree\n");
361 priv->usb_20_clk = NULL;
363 err = clk_prepare_enable(priv->usb_20_clk);
367 if (priv->has_eohci) {
368 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
370 dev_err(dev, "failed to create EHCI/OHCI PHY\n");
371 return PTR_ERR(gphy);
373 priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
374 priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
375 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
378 if (priv->has_xhci) {
379 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
381 dev_err(dev, "failed to create XHCI PHY\n");
382 return PTR_ERR(gphy);
384 priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
385 priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
386 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
388 priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
389 if (IS_ERR(priv->usb_30_clk)) {
390 if (PTR_ERR(priv->usb_30_clk) == -EPROBE_DEFER)
391 return -EPROBE_DEFER;
393 "USB3.0 clock not found in Device Tree\n");
394 priv->usb_30_clk = NULL;
396 err = clk_prepare_enable(priv->usb_30_clk);
401 priv->suspend_clk = clk_get(dev, "usb0_freerun");
402 if (IS_ERR(priv->suspend_clk)) {
403 if (PTR_ERR(priv->suspend_clk) == -EPROBE_DEFER)
404 return -EPROBE_DEFER;
405 dev_err(dev, "Suspend Clock not found in Device Tree\n");
406 priv->suspend_clk = NULL;
409 priv->wake_irq = platform_get_irq_byname(pdev, "wake");
410 if (priv->wake_irq < 0)
411 priv->wake_irq = platform_get_irq_byname(pdev, "wakeup");
412 if (priv->wake_irq >= 0) {
413 err = devm_request_irq(dev, priv->wake_irq,
414 brcm_usb_phy_wake_isr, 0,
415 dev_name(dev), gphy);
418 device_set_wakeup_capable(dev, 1);
421 "Wake interrupt missing, system wake not supported\n");
427 static int brcm_usb_phy_probe(struct platform_device *pdev)
429 struct device *dev = &pdev->dev;
430 struct brcm_usb_phy_data *priv;
431 struct phy_provider *phy_provider;
432 struct device_node *dn = pdev->dev.of_node;
435 const struct match_chip_info *info;
439 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
442 platform_set_drvdata(pdev, priv);
444 priv->ini.family_id = brcmstb_get_family_id();
445 priv->ini.product_id = brcmstb_get_product_id();
447 info = of_device_get_match_data(&pdev->dev);
451 info->init_func(&priv->ini);
453 dev_dbg(dev, "Best mapping table is for %s\n",
454 priv->ini.family_name);
456 of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
457 of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
459 priv->ini.mode = USB_CTLR_MODE_HOST;
460 err = of_property_read_string(dn, "dr_mode", &mode);
462 name_to_value(&brcm_dr_mode_to_name[0],
463 ARRAY_SIZE(brcm_dr_mode_to_name),
464 mode, &priv->ini.mode);
466 if (of_property_read_bool(dn, "brcm,has-xhci"))
467 priv->has_xhci = true;
468 if (of_property_read_bool(dn, "brcm,has-eohci"))
469 priv->has_eohci = true;
471 for (x = 0; x < BRCM_REGS_MAX; x++) {
472 if (info->required_regs[x] >= BRCM_REGS_MAX)
475 err = brcm_usb_get_regs(pdev, info->required_regs[x],
480 if (info->optional_reg) {
481 err = brcm_usb_get_regs(pdev, info->optional_reg,
487 err = brcm_usb_phy_dvr_init(pdev, priv, dn);
491 mutex_init(&priv->mutex);
493 /* make sure invert settings are correct */
494 brcm_usb_init_ipp(&priv->ini);
497 * Create sysfs entries for mode.
498 * Remove "dual_select" attribute if not in dual mode
500 if (priv->ini.mode != USB_CTLR_MODE_DRD)
501 brcm_usb_phy_attrs[1] = NULL;
502 err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
504 dev_warn(dev, "Error creating sysfs attributes\n");
506 /* Get piarbctl syscon if it exists */
507 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
510 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
511 "brcm,syscon-piarbctl");
513 priv->ini.syscon_piarbctl = rmap;
515 /* start with everything off */
517 brcm_usb_uninit_xhci(&priv->ini);
519 brcm_usb_uninit_eohci(&priv->ini);
520 brcm_usb_uninit_common(&priv->ini);
521 clk_disable_unprepare(priv->usb_20_clk);
522 clk_disable_unprepare(priv->usb_30_clk);
524 phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
526 return PTR_ERR_OR_ZERO(phy_provider);
529 static int brcm_usb_phy_remove(struct platform_device *pdev)
531 sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group);
536 #ifdef CONFIG_PM_SLEEP
537 static int brcm_usb_phy_suspend(struct device *dev)
539 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
541 if (priv->init_count) {
542 priv->ini.wake_enabled = device_may_wakeup(dev);
543 if (priv->phys[BRCM_USB_PHY_3_0].inited)
544 brcm_usb_uninit_xhci(&priv->ini);
545 if (priv->phys[BRCM_USB_PHY_2_0].inited)
546 brcm_usb_uninit_eohci(&priv->ini);
547 brcm_usb_uninit_common(&priv->ini);
550 * Handle the clocks unless needed for wake. This has
551 * to work for both older XHCI->3.0-clks, EOHCI->2.0-clks
552 * and newer XHCI->2.0-clks/3.0-clks.
555 if (!priv->ini.suspend_with_clocks) {
556 if (priv->phys[BRCM_USB_PHY_3_0].inited)
557 clk_disable_unprepare(priv->usb_30_clk);
558 if (priv->phys[BRCM_USB_PHY_2_0].inited ||
560 clk_disable_unprepare(priv->usb_20_clk);
562 if (priv->wake_irq >= 0)
563 enable_irq_wake(priv->wake_irq);
568 static int brcm_usb_phy_resume(struct device *dev)
570 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
572 clk_prepare_enable(priv->usb_20_clk);
573 clk_prepare_enable(priv->usb_30_clk);
574 brcm_usb_init_ipp(&priv->ini);
577 * Initialize anything that was previously initialized.
578 * Uninitialize anything that wasn't previously initialized.
580 if (priv->init_count) {
581 if (priv->wake_irq >= 0)
582 disable_irq_wake(priv->wake_irq);
583 brcm_usb_init_common(&priv->ini);
584 if (priv->phys[BRCM_USB_PHY_2_0].inited) {
585 brcm_usb_init_eohci(&priv->ini);
586 } else if (priv->has_eohci) {
587 brcm_usb_uninit_eohci(&priv->ini);
588 clk_disable_unprepare(priv->usb_20_clk);
590 if (priv->phys[BRCM_USB_PHY_3_0].inited) {
591 brcm_usb_init_xhci(&priv->ini);
592 } else if (priv->has_xhci) {
593 brcm_usb_uninit_xhci(&priv->ini);
594 clk_disable_unprepare(priv->usb_30_clk);
595 if (!priv->has_eohci)
596 clk_disable_unprepare(priv->usb_20_clk);
600 brcm_usb_uninit_xhci(&priv->ini);
602 brcm_usb_uninit_eohci(&priv->ini);
603 brcm_usb_uninit_common(&priv->ini);
604 clk_disable_unprepare(priv->usb_20_clk);
605 clk_disable_unprepare(priv->usb_30_clk);
607 priv->ini.wake_enabled = false;
610 #endif /* CONFIG_PM_SLEEP */
612 static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
613 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
616 MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
618 static struct platform_driver brcm_usb_driver = {
619 .probe = brcm_usb_phy_probe,
620 .remove = brcm_usb_phy_remove,
622 .name = "brcmstb-usb-phy",
623 .pm = &brcm_usb_phy_pm_ops,
624 .of_match_table = brcm_usb_dt_ids,
628 module_platform_driver(brcm_usb_driver);
630 MODULE_ALIAS("platform:brcmstb-usb-phy");
631 MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
632 MODULE_DESCRIPTION("BRCM USB PHY driver");
633 MODULE_LICENSE("GPL v2");