Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * phy-brcm-usb.c - Broadcom USB Phy Driver
4 *
5 * Copyright (C) 2015-2017 Broadcom
6 */
7
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/err.h>
11#include <linux/io.h>
12#include <linux/module.h>
13#include <linux/of.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>
22
23#include "phy-brcm-usb-init.h"
24
25static DEFINE_MUTEX(sysfs_lock);
26
27enum brcm_usb_phy_id {
28 BRCM_USB_PHY_2_0 = 0,
29 BRCM_USB_PHY_3_0,
30 BRCM_USB_PHY_ID_MAX
31};
32
33struct value_to_name_map {
34 int value;
35 const char *name;
36};
37
38struct match_chip_info {
39 void (*init_func)(struct brcm_usb_init_params *params);
40 u8 required_regs[BRCM_REGS_MAX + 1];
41 u8 optional_reg;
42};
43
44static 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" }
49};
50
51static const struct value_to_name_map brcm_dual_mode_to_name[] = {
52 { 0, "host" },
53 { 1, "device" },
54 { 2, "auto" },
55};
56
57struct brcm_usb_phy {
58 struct phy *phy;
59 unsigned int id;
60 bool inited;
61};
62
63struct brcm_usb_phy_data {
64 struct brcm_usb_init_params ini;
65 bool has_eohci;
66 bool has_xhci;
67 struct clk *usb_20_clk;
68 struct clk *usb_30_clk;
69 struct clk *suspend_clk;
70 struct mutex mutex; /* serialize phy init */
71 int init_count;
72 int wake_irq;
73 struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX];
74 struct notifier_block pm_notifier;
75 bool pm_active;
76};
77
78static s8 *node_reg_names[BRCM_REGS_MAX] = {
79 "crtl", "xhci_ec", "xhci_gbl", "usb_phy", "usb_mdio", "bdc_ec"
80};
81
82static int brcm_pm_notifier(struct notifier_block *notifier,
83 unsigned long pm_event,
84 void *unused)
85{
86 struct brcm_usb_phy_data *priv =
87 container_of(notifier, struct brcm_usb_phy_data, pm_notifier);
88
89 switch (pm_event) {
90 case PM_HIBERNATION_PREPARE:
91 case PM_SUSPEND_PREPARE:
92 priv->pm_active = true;
93 break;
94 case PM_POST_RESTORE:
95 case PM_POST_HIBERNATION:
96 case PM_POST_SUSPEND:
97 priv->pm_active = false;
98 break;
99 }
100 return NOTIFY_DONE;
101}
102
103static irqreturn_t brcm_usb_phy_wake_isr(int irq, void *dev_id)
104{
105 struct device *dev = dev_id;
106
107 pm_wakeup_event(dev, 0);
108
109 return IRQ_HANDLED;
110}
111
112static int brcm_usb_phy_init(struct phy *gphy)
113{
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]);
117
118 if (priv->pm_active)
119 return 0;
120
121 /*
122 * Use a lock to make sure a second caller waits until
123 * the base phy is inited before using it.
124 */
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);
131 }
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);
137 phy->inited = true;
138 dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
139 priv->init_count);
140
141 return 0;
142}
143
144static int brcm_usb_phy_exit(struct phy *gphy)
145{
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]);
149
150 if (priv->pm_active)
151 return 0;
152
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);
158
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);
166 }
167 mutex_unlock(&priv->mutex);
168 phy->inited = false;
169 return 0;
170}
171
172static const struct phy_ops brcm_usb_phy_ops = {
173 .init = brcm_usb_phy_init,
174 .exit = brcm_usb_phy_exit,
175 .owner = THIS_MODULE,
176};
177
178static struct phy *brcm_usb_phy_xlate(struct device *dev,
179 struct of_phandle_args *args)
180{
181 struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
182
183 /*
184 * values 0 and 1 are for backward compatibility with
185 * device tree nodes from older bootloaders.
186 */
187 switch (args->args[0]) {
188 case 0:
189 case PHY_TYPE_USB2:
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");
193 break;
194 case 1:
195 case PHY_TYPE_USB3:
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");
199 break;
200 }
201 return ERR_PTR(-ENODEV);
202}
203
204static int name_to_value(const struct value_to_name_map *table, int count,
205 const char *name, int *value)
206{
207 int x;
208
209 *value = 0;
210 for (x = 0; x < count; x++) {
211 if (sysfs_streq(name, table[x].name)) {
212 *value = x;
213 return 0;
214 }
215 }
216 return -EINVAL;
217}
218
219static const char *value_to_name(const struct value_to_name_map *table, int count,
220 int value)
221{
222 if (value >= count)
223 return "unknown";
224 return table[value].name;
225}
226
227static ssize_t dr_mode_show(struct device *dev,
228 struct device_attribute *attr,
229 char *buf)
230{
231 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
232
233 return sprintf(buf, "%s\n",
234 value_to_name(&brcm_dr_mode_to_name[0],
235 ARRAY_SIZE(brcm_dr_mode_to_name),
236 priv->ini.supported_port_modes));
237}
238static DEVICE_ATTR_RO(dr_mode);
239
240static ssize_t dual_select_store(struct device *dev,
241 struct device_attribute *attr,
242 const char *buf, size_t len)
243{
244 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
245 int value;
246 int res;
247
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);
251 if (!res) {
252 priv->ini.port_mode = value;
253 brcm_usb_set_dual_select(&priv->ini);
254 res = len;
255 }
256 mutex_unlock(&sysfs_lock);
257 return res;
258}
259
260static ssize_t dual_select_show(struct device *dev,
261 struct device_attribute *attr,
262 char *buf)
263{
264 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
265 int value;
266
267 mutex_lock(&sysfs_lock);
268 value = brcm_usb_get_dual_select(&priv->ini);
269 mutex_unlock(&sysfs_lock);
270 return sprintf(buf, "%s\n",
271 value_to_name(&brcm_dual_mode_to_name[0],
272 ARRAY_SIZE(brcm_dual_mode_to_name),
273 value));
274}
275static DEVICE_ATTR_RW(dual_select);
276
277static struct attribute *brcm_usb_phy_attrs[] = {
278 &dev_attr_dr_mode.attr,
279 &dev_attr_dual_select.attr,
280 NULL
281};
282
283static const struct attribute_group brcm_usb_phy_group = {
284 .attrs = brcm_usb_phy_attrs,
285};
286
287static const struct match_chip_info chip_info_4908 = {
288 .init_func = &brcm_usb_dvr_init_4908,
289 .required_regs = {
290 BRCM_REGS_CTRL,
291 BRCM_REGS_XHCI_EC,
292 -1,
293 },
294};
295
296static const struct match_chip_info chip_info_7216 = {
297 .init_func = &brcm_usb_dvr_init_7216,
298 .required_regs = {
299 BRCM_REGS_CTRL,
300 BRCM_REGS_XHCI_EC,
301 BRCM_REGS_XHCI_GBL,
302 -1,
303 },
304};
305
306static const struct match_chip_info chip_info_7211b0 = {
307 .init_func = &brcm_usb_dvr_init_7211b0,
308 .required_regs = {
309 BRCM_REGS_CTRL,
310 BRCM_REGS_XHCI_EC,
311 BRCM_REGS_XHCI_GBL,
312 BRCM_REGS_USB_PHY,
313 BRCM_REGS_USB_MDIO,
314 -1,
315 },
316 .optional_reg = BRCM_REGS_BDC_EC,
317};
318
319static const struct match_chip_info chip_info_7445 = {
320 .init_func = &brcm_usb_dvr_init_7445,
321 .required_regs = {
322 BRCM_REGS_CTRL,
323 BRCM_REGS_XHCI_EC,
324 -1,
325 },
326};
327
328static const struct of_device_id brcm_usb_dt_ids[] = {
329 {
330 .compatible = "brcm,bcm4908-usb-phy",
331 .data = &chip_info_4908,
332 },
333 {
334 .compatible = "brcm,bcm7216-usb-phy",
335 .data = &chip_info_7216,
336 },
337 {
338 .compatible = "brcm,bcm7211-usb-phy",
339 .data = &chip_info_7211b0,
340 },
341 {
342 .compatible = "brcm,brcmstb-usb-phy",
343 .data = &chip_info_7445,
344 },
345 { /* sentinel */ }
346};
347
348static int brcm_usb_get_regs(struct platform_device *pdev,
349 enum brcmusb_reg_sel regs,
350 struct brcm_usb_init_params *ini,
351 bool optional)
352{
353 struct resource *res;
354
355 /* Older DT nodes have ctrl and optional xhci_ec by index only */
356 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
357 node_reg_names[regs]);
358 if (res == NULL) {
359 if (regs == BRCM_REGS_CTRL) {
360 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
361 } else if (regs == BRCM_REGS_XHCI_EC) {
362 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
363 /* XHCI_EC registers are optional */
364 if (res == NULL)
365 return 0;
366 }
367 if (res == NULL) {
368 if (optional) {
369 dev_dbg(&pdev->dev,
370 "Optional reg %s not found\n",
371 node_reg_names[regs]);
372 return 0;
373 }
374 dev_err(&pdev->dev, "can't get %s base addr\n",
375 node_reg_names[regs]);
376 return 1;
377 }
378 }
379 ini->regs[regs] = devm_ioremap_resource(&pdev->dev, res);
380 if (IS_ERR(ini->regs[regs])) {
381 dev_err(&pdev->dev, "can't map %s register space\n",
382 node_reg_names[regs]);
383 return 1;
384 }
385 return 0;
386}
387
388static int brcm_usb_phy_dvr_init(struct platform_device *pdev,
389 struct brcm_usb_phy_data *priv,
390 struct device_node *dn)
391{
392 struct device *dev = &pdev->dev;
393 struct phy *gphy = NULL;
394 int err;
395
396 priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
397 if (IS_ERR(priv->usb_20_clk)) {
398 if (PTR_ERR(priv->usb_20_clk) == -EPROBE_DEFER)
399 return -EPROBE_DEFER;
400 dev_info(dev, "Clock not found in Device Tree\n");
401 priv->usb_20_clk = NULL;
402 }
403 err = clk_prepare_enable(priv->usb_20_clk);
404 if (err)
405 return err;
406
407 if (priv->has_eohci) {
408 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
409 if (IS_ERR(gphy)) {
410 dev_err(dev, "failed to create EHCI/OHCI PHY\n");
411 return PTR_ERR(gphy);
412 }
413 priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
414 priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
415 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
416 }
417
418 if (priv->has_xhci) {
419 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
420 if (IS_ERR(gphy)) {
421 dev_err(dev, "failed to create XHCI PHY\n");
422 return PTR_ERR(gphy);
423 }
424 priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
425 priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
426 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
427
428 priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
429 if (IS_ERR(priv->usb_30_clk)) {
430 if (PTR_ERR(priv->usb_30_clk) == -EPROBE_DEFER)
431 return -EPROBE_DEFER;
432 dev_info(dev,
433 "USB3.0 clock not found in Device Tree\n");
434 priv->usb_30_clk = NULL;
435 }
436 err = clk_prepare_enable(priv->usb_30_clk);
437 if (err)
438 return err;
439 }
440
441 priv->suspend_clk = clk_get(dev, "usb0_freerun");
442 if (IS_ERR(priv->suspend_clk)) {
443 if (PTR_ERR(priv->suspend_clk) == -EPROBE_DEFER)
444 return -EPROBE_DEFER;
445 dev_err(dev, "Suspend Clock not found in Device Tree\n");
446 priv->suspend_clk = NULL;
447 }
448
449 priv->wake_irq = platform_get_irq_byname_optional(pdev, "wake");
450 if (priv->wake_irq < 0)
451 priv->wake_irq = platform_get_irq_byname_optional(pdev, "wakeup");
452 if (priv->wake_irq >= 0) {
453 err = devm_request_irq(dev, priv->wake_irq,
454 brcm_usb_phy_wake_isr, 0,
455 dev_name(dev), dev);
456 if (err < 0)
457 return err;
458 device_set_wakeup_capable(dev, 1);
459 } else {
460 dev_info(dev,
461 "Wake interrupt missing, system wake not supported\n");
462 }
463
464 return 0;
465}
466
467static int brcm_usb_phy_probe(struct platform_device *pdev)
468{
469 struct device *dev = &pdev->dev;
470 struct brcm_usb_phy_data *priv;
471 struct phy_provider *phy_provider;
472 struct device_node *dn = pdev->dev.of_node;
473 int err;
474 const char *mode;
475 const struct match_chip_info *info;
476 struct regmap *rmap;
477 int x;
478
479 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
480 if (!priv)
481 return -ENOMEM;
482 platform_set_drvdata(pdev, priv);
483
484 priv->ini.family_id = brcmstb_get_family_id();
485 priv->ini.product_id = brcmstb_get_product_id();
486
487 info = of_device_get_match_data(&pdev->dev);
488 if (!info)
489 return -ENOENT;
490
491 info->init_func(&priv->ini);
492
493 dev_dbg(dev, "Best mapping table is for %s\n",
494 priv->ini.family_name);
495
496 of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
497 of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
498
499 priv->ini.supported_port_modes = USB_CTLR_MODE_HOST;
500 err = of_property_read_string(dn, "dr_mode", &mode);
501 if (err == 0) {
502 name_to_value(&brcm_dr_mode_to_name[0],
503 ARRAY_SIZE(brcm_dr_mode_to_name),
504 mode, &priv->ini.supported_port_modes);
505 }
506 /* Default port_mode to supported port_modes */
507 priv->ini.port_mode = priv->ini.supported_port_modes;
508
509 if (of_property_read_bool(dn, "brcm,has-xhci"))
510 priv->has_xhci = true;
511 if (of_property_read_bool(dn, "brcm,has-eohci"))
512 priv->has_eohci = true;
513
514 for (x = 0; x < BRCM_REGS_MAX; x++) {
515 if (info->required_regs[x] >= BRCM_REGS_MAX)
516 break;
517
518 err = brcm_usb_get_regs(pdev, info->required_regs[x],
519 &priv->ini, false);
520 if (err)
521 return -EINVAL;
522 }
523 if (info->optional_reg) {
524 err = brcm_usb_get_regs(pdev, info->optional_reg,
525 &priv->ini, true);
526 if (err)
527 return -EINVAL;
528 }
529
530 err = brcm_usb_phy_dvr_init(pdev, priv, dn);
531 if (err)
532 return err;
533
534 priv->pm_notifier.notifier_call = brcm_pm_notifier;
535 register_pm_notifier(&priv->pm_notifier);
536
537 mutex_init(&priv->mutex);
538
539 /* make sure invert settings are correct */
540 brcm_usb_init_ipp(&priv->ini);
541
542 /*
543 * Create sysfs entries for mode.
544 * Remove "dual_select" attribute if not in dual mode
545 */
546 if (priv->ini.supported_port_modes != USB_CTLR_MODE_DRD)
547 brcm_usb_phy_attrs[1] = NULL;
548 err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
549 if (err)
550 dev_warn(dev, "Error creating sysfs attributes\n");
551
552 /* Get piarbctl syscon if it exists */
553 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
554 "syscon-piarbctl");
555 if (IS_ERR(rmap))
556 rmap = syscon_regmap_lookup_by_phandle(dev->of_node,
557 "brcm,syscon-piarbctl");
558 if (!IS_ERR(rmap))
559 priv->ini.syscon_piarbctl = rmap;
560
561 /* start with everything off */
562 if (priv->has_xhci)
563 brcm_usb_uninit_xhci(&priv->ini);
564 if (priv->has_eohci)
565 brcm_usb_uninit_eohci(&priv->ini);
566 brcm_usb_uninit_common(&priv->ini);
567 clk_disable_unprepare(priv->usb_20_clk);
568 clk_disable_unprepare(priv->usb_30_clk);
569
570 phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
571
572 return PTR_ERR_OR_ZERO(phy_provider);
573}
574
575static int brcm_usb_phy_remove(struct platform_device *pdev)
576{
577 struct brcm_usb_phy_data *priv = dev_get_drvdata(&pdev->dev);
578
579 sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group);
580 unregister_pm_notifier(&priv->pm_notifier);
581
582 return 0;
583}
584
585#ifdef CONFIG_PM_SLEEP
586static int brcm_usb_phy_suspend(struct device *dev)
587{
588 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
589
590 if (priv->init_count) {
591 dev_dbg(dev, "SUSPEND\n");
592 priv->ini.wake_enabled = device_may_wakeup(dev);
593 if (priv->phys[BRCM_USB_PHY_3_0].inited)
594 brcm_usb_uninit_xhci(&priv->ini);
595 if (priv->phys[BRCM_USB_PHY_2_0].inited)
596 brcm_usb_uninit_eohci(&priv->ini);
597 brcm_usb_uninit_common(&priv->ini);
598
599 /*
600 * Handle the clocks unless needed for wake. This has
601 * to work for both older XHCI->3.0-clks, EOHCI->2.0-clks
602 * and newer XHCI->2.0-clks/3.0-clks.
603 */
604
605 if (!priv->ini.wake_enabled) {
606 if (priv->phys[BRCM_USB_PHY_3_0].inited)
607 clk_disable_unprepare(priv->usb_30_clk);
608 if (priv->phys[BRCM_USB_PHY_2_0].inited ||
609 !priv->has_eohci)
610 clk_disable_unprepare(priv->usb_20_clk);
611 }
612 if (priv->wake_irq >= 0)
613 enable_irq_wake(priv->wake_irq);
614 }
615 return 0;
616}
617
618static int brcm_usb_phy_resume(struct device *dev)
619{
620 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
621
622 if (!priv->ini.wake_enabled) {
623 clk_prepare_enable(priv->usb_20_clk);
624 clk_prepare_enable(priv->usb_30_clk);
625 }
626 brcm_usb_init_ipp(&priv->ini);
627
628 /*
629 * Initialize anything that was previously initialized.
630 * Uninitialize anything that wasn't previously initialized.
631 */
632 if (priv->init_count) {
633 dev_dbg(dev, "RESUME\n");
634 if (priv->wake_irq >= 0)
635 disable_irq_wake(priv->wake_irq);
636 brcm_usb_init_common(&priv->ini);
637 if (priv->phys[BRCM_USB_PHY_2_0].inited) {
638 brcm_usb_init_eohci(&priv->ini);
639 } else if (priv->has_eohci) {
640 brcm_usb_uninit_eohci(&priv->ini);
641 clk_disable_unprepare(priv->usb_20_clk);
642 }
643 if (priv->phys[BRCM_USB_PHY_3_0].inited) {
644 brcm_usb_init_xhci(&priv->ini);
645 } else if (priv->has_xhci) {
646 brcm_usb_uninit_xhci(&priv->ini);
647 clk_disable_unprepare(priv->usb_30_clk);
648 if (!priv->has_eohci)
649 clk_disable_unprepare(priv->usb_20_clk);
650 }
651 } else {
652 if (priv->has_xhci)
653 brcm_usb_uninit_xhci(&priv->ini);
654 if (priv->has_eohci)
655 brcm_usb_uninit_eohci(&priv->ini);
656 brcm_usb_uninit_common(&priv->ini);
657 clk_disable_unprepare(priv->usb_20_clk);
658 clk_disable_unprepare(priv->usb_30_clk);
659 }
660 priv->ini.wake_enabled = false;
661 return 0;
662}
663#endif /* CONFIG_PM_SLEEP */
664
665static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
666 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
667};
668
669MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
670
671static struct platform_driver brcm_usb_driver = {
672 .probe = brcm_usb_phy_probe,
673 .remove = brcm_usb_phy_remove,
674 .driver = {
675 .name = "brcmstb-usb-phy",
676 .pm = &brcm_usb_phy_pm_ops,
677 .of_match_table = brcm_usb_dt_ids,
678 },
679};
680
681module_platform_driver(brcm_usb_driver);
682
683MODULE_ALIAS("platform:brcmstb-usb-phy");
684MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
685MODULE_DESCRIPTION("BRCM USB PHY driver");
686MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * phy-brcm-usb.c - Broadcom USB Phy Driver
4 *
5 * Copyright (C) 2015-2017 Broadcom
6 */
7
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/err.h>
11#include <linux/io.h>
12#include <linux/module.h>
13#include <linux/of.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
20#include "phy-brcm-usb-init.h"
21
22static DEFINE_MUTEX(sysfs_lock);
23
24enum brcm_usb_phy_id {
25 BRCM_USB_PHY_2_0 = 0,
26 BRCM_USB_PHY_3_0,
27 BRCM_USB_PHY_ID_MAX
28};
29
30struct value_to_name_map {
31 int value;
32 const char *name;
33};
34
35static struct value_to_name_map brcm_dr_mode_to_name[] = {
36 { USB_CTLR_MODE_HOST, "host" },
37 { USB_CTLR_MODE_DEVICE, "peripheral" },
38 { USB_CTLR_MODE_DRD, "drd" },
39 { USB_CTLR_MODE_TYPEC_PD, "typec-pd" }
40};
41
42static struct value_to_name_map brcm_dual_mode_to_name[] = {
43 { 0, "host" },
44 { 1, "device" },
45 { 2, "auto" },
46};
47
48struct brcm_usb_phy {
49 struct phy *phy;
50 unsigned int id;
51 bool inited;
52};
53
54struct brcm_usb_phy_data {
55 struct brcm_usb_init_params ini;
56 bool has_eohci;
57 bool has_xhci;
58 struct clk *usb_20_clk;
59 struct clk *usb_30_clk;
60 struct mutex mutex; /* serialize phy init */
61 int init_count;
62 struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX];
63};
64
65static int brcm_usb_phy_init(struct phy *gphy)
66{
67 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
68 struct brcm_usb_phy_data *priv =
69 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
70
71 /*
72 * Use a lock to make sure a second caller waits until
73 * the base phy is inited before using it.
74 */
75 mutex_lock(&priv->mutex);
76 if (priv->init_count++ == 0) {
77 clk_enable(priv->usb_20_clk);
78 clk_enable(priv->usb_30_clk);
79 brcm_usb_init_common(&priv->ini);
80 }
81 mutex_unlock(&priv->mutex);
82 if (phy->id == BRCM_USB_PHY_2_0)
83 brcm_usb_init_eohci(&priv->ini);
84 else if (phy->id == BRCM_USB_PHY_3_0)
85 brcm_usb_init_xhci(&priv->ini);
86 phy->inited = true;
87 dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
88 priv->init_count);
89
90 return 0;
91}
92
93static int brcm_usb_phy_exit(struct phy *gphy)
94{
95 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
96 struct brcm_usb_phy_data *priv =
97 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
98
99 dev_dbg(&gphy->dev, "EXIT\n");
100 if (phy->id == BRCM_USB_PHY_2_0)
101 brcm_usb_uninit_eohci(&priv->ini);
102 if (phy->id == BRCM_USB_PHY_3_0)
103 brcm_usb_uninit_xhci(&priv->ini);
104
105 /* If both xhci and eohci are gone, reset everything else */
106 mutex_lock(&priv->mutex);
107 if (--priv->init_count == 0) {
108 brcm_usb_uninit_common(&priv->ini);
109 clk_disable(priv->usb_20_clk);
110 clk_disable(priv->usb_30_clk);
111 }
112 mutex_unlock(&priv->mutex);
113 phy->inited = false;
114 return 0;
115}
116
117static struct phy_ops brcm_usb_phy_ops = {
118 .init = brcm_usb_phy_init,
119 .exit = brcm_usb_phy_exit,
120 .owner = THIS_MODULE,
121};
122
123static struct phy *brcm_usb_phy_xlate(struct device *dev,
124 struct of_phandle_args *args)
125{
126 struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
127
128 /*
129 * values 0 and 1 are for backward compatibility with
130 * device tree nodes from older bootloaders.
131 */
132 switch (args->args[0]) {
133 case 0:
134 case PHY_TYPE_USB2:
135 if (data->phys[BRCM_USB_PHY_2_0].phy)
136 return data->phys[BRCM_USB_PHY_2_0].phy;
137 dev_warn(dev, "Error, 2.0 Phy not found\n");
138 break;
139 case 1:
140 case PHY_TYPE_USB3:
141 if (data->phys[BRCM_USB_PHY_3_0].phy)
142 return data->phys[BRCM_USB_PHY_3_0].phy;
143 dev_warn(dev, "Error, 3.0 Phy not found\n");
144 break;
145 }
146 return ERR_PTR(-ENODEV);
147}
148
149static int name_to_value(struct value_to_name_map *table, int count,
150 const char *name, int *value)
151{
152 int x;
153
154 *value = 0;
155 for (x = 0; x < count; x++) {
156 if (sysfs_streq(name, table[x].name)) {
157 *value = x;
158 return 0;
159 }
160 }
161 return -EINVAL;
162}
163
164static const char *value_to_name(struct value_to_name_map *table, int count,
165 int value)
166{
167 if (value >= count)
168 return "unknown";
169 return table[value].name;
170}
171
172static ssize_t dr_mode_show(struct device *dev,
173 struct device_attribute *attr,
174 char *buf)
175{
176 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
177
178 return sprintf(buf, "%s\n",
179 value_to_name(&brcm_dr_mode_to_name[0],
180 ARRAY_SIZE(brcm_dr_mode_to_name),
181 priv->ini.mode));
182}
183static DEVICE_ATTR_RO(dr_mode);
184
185static ssize_t dual_select_store(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t len)
188{
189 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
190 int value;
191 int res;
192
193 mutex_lock(&sysfs_lock);
194 res = name_to_value(&brcm_dual_mode_to_name[0],
195 ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value);
196 if (!res) {
197 brcm_usb_init_set_dual_select(&priv->ini, value);
198 res = len;
199 }
200 mutex_unlock(&sysfs_lock);
201 return res;
202}
203
204static ssize_t dual_select_show(struct device *dev,
205 struct device_attribute *attr,
206 char *buf)
207{
208 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
209 int value;
210
211 mutex_lock(&sysfs_lock);
212 value = brcm_usb_init_get_dual_select(&priv->ini);
213 mutex_unlock(&sysfs_lock);
214 return sprintf(buf, "%s\n",
215 value_to_name(&brcm_dual_mode_to_name[0],
216 ARRAY_SIZE(brcm_dual_mode_to_name),
217 value));
218}
219static DEVICE_ATTR_RW(dual_select);
220
221static struct attribute *brcm_usb_phy_attrs[] = {
222 &dev_attr_dr_mode.attr,
223 &dev_attr_dual_select.attr,
224 NULL
225};
226
227static const struct attribute_group brcm_usb_phy_group = {
228 .attrs = brcm_usb_phy_attrs,
229};
230
231static int brcm_usb_phy_dvr_init(struct device *dev,
232 struct brcm_usb_phy_data *priv,
233 struct device_node *dn)
234{
235 struct phy *gphy;
236 int err;
237
238 priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
239 if (IS_ERR(priv->usb_20_clk)) {
240 dev_info(dev, "Clock not found in Device Tree\n");
241 priv->usb_20_clk = NULL;
242 }
243 err = clk_prepare_enable(priv->usb_20_clk);
244 if (err)
245 return err;
246
247 if (priv->has_eohci) {
248 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
249 if (IS_ERR(gphy)) {
250 dev_err(dev, "failed to create EHCI/OHCI PHY\n");
251 return PTR_ERR(gphy);
252 }
253 priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
254 priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
255 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
256 }
257
258 if (priv->has_xhci) {
259 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
260 if (IS_ERR(gphy)) {
261 dev_err(dev, "failed to create XHCI PHY\n");
262 return PTR_ERR(gphy);
263 }
264 priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
265 priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
266 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
267
268 priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
269 if (IS_ERR(priv->usb_30_clk)) {
270 dev_info(dev,
271 "USB3.0 clock not found in Device Tree\n");
272 priv->usb_30_clk = NULL;
273 }
274 err = clk_prepare_enable(priv->usb_30_clk);
275 if (err)
276 return err;
277 }
278 return 0;
279}
280
281static int brcm_usb_phy_probe(struct platform_device *pdev)
282{
283 struct resource *res;
284 struct device *dev = &pdev->dev;
285 struct brcm_usb_phy_data *priv;
286 struct phy_provider *phy_provider;
287 struct device_node *dn = pdev->dev.of_node;
288 int err;
289 const char *mode;
290
291 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
292 if (!priv)
293 return -ENOMEM;
294 platform_set_drvdata(pdev, priv);
295
296 priv->ini.family_id = brcmstb_get_family_id();
297 priv->ini.product_id = brcmstb_get_product_id();
298 brcm_usb_set_family_map(&priv->ini);
299 dev_dbg(dev, "Best mapping table is for %s\n",
300 priv->ini.family_name);
301 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
302 if (!res) {
303 dev_err(dev, "can't get USB_CTRL base address\n");
304 return -EINVAL;
305 }
306 priv->ini.ctrl_regs = devm_ioremap_resource(dev, res);
307 if (IS_ERR(priv->ini.ctrl_regs)) {
308 dev_err(dev, "can't map CTRL register space\n");
309 return -EINVAL;
310 }
311
312 /* The XHCI EC registers are optional */
313 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
314 if (res) {
315 priv->ini.xhci_ec_regs =
316 devm_ioremap_resource(dev, res);
317 if (IS_ERR(priv->ini.xhci_ec_regs)) {
318 dev_err(dev, "can't map XHCI EC register space\n");
319 return -EINVAL;
320 }
321 }
322
323 of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
324 of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
325
326 priv->ini.mode = USB_CTLR_MODE_HOST;
327 err = of_property_read_string(dn, "dr_mode", &mode);
328 if (err == 0) {
329 name_to_value(&brcm_dr_mode_to_name[0],
330 ARRAY_SIZE(brcm_dr_mode_to_name),
331 mode, &priv->ini.mode);
332 }
333 if (of_property_read_bool(dn, "brcm,has-xhci"))
334 priv->has_xhci = true;
335 if (of_property_read_bool(dn, "brcm,has-eohci"))
336 priv->has_eohci = true;
337
338 err = brcm_usb_phy_dvr_init(dev, priv, dn);
339 if (err)
340 return err;
341
342 mutex_init(&priv->mutex);
343
344 /* make sure invert settings are correct */
345 brcm_usb_init_ipp(&priv->ini);
346
347 /*
348 * Create sysfs entries for mode.
349 * Remove "dual_select" attribute if not in dual mode
350 */
351 if (priv->ini.mode != USB_CTLR_MODE_DRD)
352 brcm_usb_phy_attrs[1] = NULL;
353 err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
354 if (err)
355 dev_warn(dev, "Error creating sysfs attributes\n");
356
357 /* start with everything off */
358 if (priv->has_xhci)
359 brcm_usb_uninit_xhci(&priv->ini);
360 if (priv->has_eohci)
361 brcm_usb_uninit_eohci(&priv->ini);
362 brcm_usb_uninit_common(&priv->ini);
363 clk_disable(priv->usb_20_clk);
364 clk_disable(priv->usb_30_clk);
365
366 phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
367
368 return PTR_ERR_OR_ZERO(phy_provider);
369}
370
371static int brcm_usb_phy_remove(struct platform_device *pdev)
372{
373 sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group);
374
375 return 0;
376}
377
378#ifdef CONFIG_PM_SLEEP
379static int brcm_usb_phy_suspend(struct device *dev)
380{
381 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
382
383 if (priv->init_count) {
384 clk_disable(priv->usb_20_clk);
385 clk_disable(priv->usb_30_clk);
386 }
387 return 0;
388}
389
390static int brcm_usb_phy_resume(struct device *dev)
391{
392 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
393
394 clk_enable(priv->usb_20_clk);
395 clk_enable(priv->usb_30_clk);
396 brcm_usb_init_ipp(&priv->ini);
397
398 /*
399 * Initialize anything that was previously initialized.
400 * Uninitialize anything that wasn't previously initialized.
401 */
402 if (priv->init_count) {
403 brcm_usb_init_common(&priv->ini);
404 if (priv->phys[BRCM_USB_PHY_2_0].inited) {
405 brcm_usb_init_eohci(&priv->ini);
406 } else if (priv->has_eohci) {
407 brcm_usb_uninit_eohci(&priv->ini);
408 clk_disable(priv->usb_20_clk);
409 }
410 if (priv->phys[BRCM_USB_PHY_3_0].inited) {
411 brcm_usb_init_xhci(&priv->ini);
412 } else if (priv->has_xhci) {
413 brcm_usb_uninit_xhci(&priv->ini);
414 clk_disable(priv->usb_30_clk);
415 }
416 } else {
417 if (priv->has_xhci)
418 brcm_usb_uninit_xhci(&priv->ini);
419 if (priv->has_eohci)
420 brcm_usb_uninit_eohci(&priv->ini);
421 brcm_usb_uninit_common(&priv->ini);
422 clk_disable(priv->usb_20_clk);
423 clk_disable(priv->usb_30_clk);
424 }
425
426 return 0;
427}
428#endif /* CONFIG_PM_SLEEP */
429
430static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
431 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
432};
433
434static const struct of_device_id brcm_usb_dt_ids[] = {
435 { .compatible = "brcm,brcmstb-usb-phy" },
436 { /* sentinel */ }
437};
438
439MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
440
441static struct platform_driver brcm_usb_driver = {
442 .probe = brcm_usb_phy_probe,
443 .remove = brcm_usb_phy_remove,
444 .driver = {
445 .name = "brcmstb-usb-phy",
446 .pm = &brcm_usb_phy_pm_ops,
447 .of_match_table = brcm_usb_dt_ids,
448 },
449};
450
451module_platform_driver(brcm_usb_driver);
452
453MODULE_ALIAS("platform:brcmstb-usb-phy");
454MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
455MODULE_DESCRIPTION("BRCM USB PHY driver");
456MODULE_LICENSE("GPL v2");