1 // SPDX-License-Identifier: GPL-2.0
3 * USB Glue for Amlogic G12A SoCs
5 * Copyright (c) 2019 BayLibre, SAS
10 * The USB is organized with a glue around the DWC3 Controller IP as :
11 * - Control registers for each USB2 Ports
12 * - Control registers for the USB PHY layer
13 * - SuperSpeed PHY can be enabled only if port is used
16 * - Add dynamic OTG switching with ID change interrupt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.h>
24 #include <linux/of_platform.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regmap.h>
27 #include <linux/bitfield.h>
28 #include <linux/bitops.h>
29 #include <linux/reset.h>
30 #include <linux/phy/phy.h>
31 #include <linux/usb/otg.h>
32 #include <linux/usb/role.h>
33 #include <linux/regulator/consumer.h>
35 /* USB2 Ports Control Registers */
37 #define U2P_REG_SIZE 0x20
40 #define U2P_R0_HOST_DEVICE BIT(0)
41 #define U2P_R0_POWER_OK BIT(1)
42 #define U2P_R0_HAST_MODE BIT(2)
43 #define U2P_R0_POWER_ON_RESET BIT(3)
44 #define U2P_R0_ID_PULLUP BIT(4)
45 #define U2P_R0_DRV_VBUS BIT(5)
48 #define U2P_R1_PHY_READY BIT(0)
49 #define U2P_R1_ID_DIG BIT(1)
50 #define U2P_R1_OTG_SESSION_VALID BIT(2)
51 #define U2P_R1_VBUS_VALID BIT(3)
53 /* USB Glue Control Registers */
56 #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
57 #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
58 #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
59 #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
60 #define USB_R0_U2D_ACT BIT(31)
63 #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
64 #define USB_R1_U3H_PME_ENABLE BIT(1)
65 #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(4, 2)
66 #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(9, 7)
67 #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(13, 12)
68 #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
69 #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
70 #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
71 #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
72 #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
75 #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
76 #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
79 #define USB_R3_P30_SSC_ENABLE BIT(0)
80 #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
81 #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
82 #define USB_R3_P30_REF_SSP_EN BIT(13)
85 #define USB_R4_P21_PORT_RESET_0 BIT(0)
86 #define USB_R4_P21_SLEEP_M0 BIT(1)
87 #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
88 #define USB_R4_P21_ONLY BIT(4)
91 #define USB_R5_ID_DIG_SYNC BIT(0)
92 #define USB_R5_ID_DIG_REG BIT(1)
93 #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
94 #define USB_R5_ID_DIG_EN_0 BIT(4)
95 #define USB_R5_ID_DIG_EN_1 BIT(5)
96 #define USB_R5_ID_DIG_CURR BIT(6)
97 #define USB_R5_ID_DIG_IRQ BIT(7)
98 #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
99 #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
108 static const char *phy_names[PHY_COUNT] = {
109 "usb2-phy0", "usb2-phy1", "usb3-phy0",
112 struct dwc3_meson_g12a {
114 struct regmap *regmap;
116 struct reset_control *reset;
117 struct phy *phys[PHY_COUNT];
118 enum usb_dr_mode otg_mode;
119 enum phy_mode otg_phy_mode;
120 unsigned int usb2_ports;
121 unsigned int usb3_ports;
122 struct regulator *vbus;
123 struct usb_role_switch_desc switch_desc;
124 struct usb_role_switch *role_switch;
127 static void dwc3_meson_g12a_usb2_set_mode(struct dwc3_meson_g12a *priv,
128 int i, enum phy_mode mode)
130 if (mode == PHY_MODE_USB_HOST)
131 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
135 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
136 U2P_R0_HOST_DEVICE, 0);
139 static int dwc3_meson_g12a_usb2_init(struct dwc3_meson_g12a *priv)
143 if (priv->otg_mode == USB_DR_MODE_PERIPHERAL)
144 priv->otg_phy_mode = PHY_MODE_USB_DEVICE;
146 priv->otg_phy_mode = PHY_MODE_USB_HOST;
148 for (i = 0 ; i < USB3_HOST_PHY ; ++i) {
152 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
153 U2P_R0_POWER_ON_RESET,
154 U2P_R0_POWER_ON_RESET);
156 if (i == USB2_OTG_PHY) {
157 regmap_update_bits(priv->regmap,
158 U2P_R0 + (U2P_REG_SIZE * i),
159 U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS,
160 U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS);
162 dwc3_meson_g12a_usb2_set_mode(priv, i,
165 dwc3_meson_g12a_usb2_set_mode(priv, i,
168 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
169 U2P_R0_POWER_ON_RESET, 0);
175 static void dwc3_meson_g12a_usb3_init(struct dwc3_meson_g12a *priv)
177 regmap_update_bits(priv->regmap, USB_R3,
178 USB_R3_P30_SSC_RANGE_MASK |
179 USB_R3_P30_REF_SSP_EN,
180 USB_R3_P30_SSC_ENABLE |
181 FIELD_PREP(USB_R3_P30_SSC_RANGE_MASK, 2) |
182 USB_R3_P30_REF_SSP_EN);
185 regmap_update_bits(priv->regmap, USB_R2,
186 USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK,
187 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK, 0x15));
189 regmap_update_bits(priv->regmap, USB_R2,
190 USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK,
191 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK, 0x20));
195 regmap_update_bits(priv->regmap, USB_R1,
196 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT,
197 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT);
199 regmap_update_bits(priv->regmap, USB_R1,
200 USB_R1_P30_PCS_TX_SWING_FULL_MASK,
201 FIELD_PREP(USB_R1_P30_PCS_TX_SWING_FULL_MASK, 127));
204 static void dwc3_meson_g12a_usb_otg_apply_mode(struct dwc3_meson_g12a *priv)
206 if (priv->otg_phy_mode == PHY_MODE_USB_DEVICE) {
207 regmap_update_bits(priv->regmap, USB_R0,
208 USB_R0_U2D_ACT, USB_R0_U2D_ACT);
209 regmap_update_bits(priv->regmap, USB_R0,
210 USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0);
211 regmap_update_bits(priv->regmap, USB_R4,
212 USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0);
214 regmap_update_bits(priv->regmap, USB_R0,
216 regmap_update_bits(priv->regmap, USB_R4,
217 USB_R4_P21_SLEEP_M0, 0);
221 static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv)
225 ret = dwc3_meson_g12a_usb2_init(priv);
229 regmap_update_bits(priv->regmap, USB_R1,
230 USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
231 FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
233 regmap_update_bits(priv->regmap, USB_R5,
236 regmap_update_bits(priv->regmap, USB_R5,
239 regmap_update_bits(priv->regmap, USB_R5,
240 USB_R5_ID_DIG_TH_MASK,
241 FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
243 /* If we have an actual SuperSpeed port, initialize it */
244 if (priv->usb3_ports)
245 dwc3_meson_g12a_usb3_init(priv);
247 dwc3_meson_g12a_usb_otg_apply_mode(priv);
252 static const struct regmap_config phy_meson_g12a_usb3_regmap_conf = {
256 .max_register = USB_R5,
259 static int dwc3_meson_g12a_get_phys(struct dwc3_meson_g12a *priv)
263 for (i = 0 ; i < PHY_COUNT ; ++i) {
264 priv->phys[i] = devm_phy_optional_get(priv->dev, phy_names[i]);
268 if (IS_ERR(priv->phys[i]))
269 return PTR_ERR(priv->phys[i]);
271 if (i == USB3_HOST_PHY)
277 dev_info(priv->dev, "USB2 ports: %d\n", priv->usb2_ports);
278 dev_info(priv->dev, "USB3 ports: %d\n", priv->usb3_ports);
283 static enum phy_mode dwc3_meson_g12a_get_id(struct dwc3_meson_g12a *priv)
287 regmap_read(priv->regmap, USB_R5, ®);
289 if (reg & (USB_R5_ID_DIG_SYNC | USB_R5_ID_DIG_REG))
290 return PHY_MODE_USB_DEVICE;
292 return PHY_MODE_USB_HOST;
295 static int dwc3_meson_g12a_otg_mode_set(struct dwc3_meson_g12a *priv,
300 if (!priv->phys[USB2_OTG_PHY])
303 if (mode == PHY_MODE_USB_HOST)
304 dev_info(priv->dev, "switching to Host Mode\n");
306 dev_info(priv->dev, "switching to Device Mode\n");
309 if (mode == PHY_MODE_USB_DEVICE)
310 ret = regulator_disable(priv->vbus);
312 ret = regulator_enable(priv->vbus);
317 priv->otg_phy_mode = mode;
319 dwc3_meson_g12a_usb2_set_mode(priv, USB2_OTG_PHY, mode);
321 dwc3_meson_g12a_usb_otg_apply_mode(priv);
326 static int dwc3_meson_g12a_role_set(struct device *dev, enum usb_role role)
328 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
331 if (role == USB_ROLE_NONE)
334 mode = (role == USB_ROLE_HOST) ? PHY_MODE_USB_HOST
335 : PHY_MODE_USB_DEVICE;
337 if (mode == priv->otg_phy_mode)
340 return dwc3_meson_g12a_otg_mode_set(priv, mode);
343 static enum usb_role dwc3_meson_g12a_role_get(struct device *dev)
345 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
347 return priv->otg_phy_mode == PHY_MODE_USB_HOST ?
348 USB_ROLE_HOST : USB_ROLE_DEVICE;
351 static struct device *dwc3_meson_g12_find_child(struct device *dev,
352 const char *compatible)
354 struct platform_device *pdev;
355 struct device_node *np;
357 np = of_get_compatible_child(dev->of_node, compatible);
361 pdev = of_find_device_by_node(np);
369 static int dwc3_meson_g12a_probe(struct platform_device *pdev)
371 struct dwc3_meson_g12a *priv;
372 struct device *dev = &pdev->dev;
373 struct device_node *np = dev->of_node;
375 struct resource *res;
376 enum phy_mode otg_id;
379 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 base = devm_ioremap_resource(dev, res);
386 return PTR_ERR(base);
388 priv->regmap = devm_regmap_init_mmio(dev, base,
389 &phy_meson_g12a_usb3_regmap_conf);
390 if (IS_ERR(priv->regmap))
391 return PTR_ERR(priv->regmap);
393 priv->vbus = devm_regulator_get_optional(dev, "vbus");
394 if (IS_ERR(priv->vbus)) {
395 if (PTR_ERR(priv->vbus) == -EPROBE_DEFER)
396 return PTR_ERR(priv->vbus);
400 priv->clk = devm_clk_get(dev, NULL);
401 if (IS_ERR(priv->clk))
402 return PTR_ERR(priv->clk);
404 ret = clk_prepare_enable(priv->clk);
408 devm_add_action_or_reset(dev,
409 (void(*)(void *))clk_disable_unprepare,
412 platform_set_drvdata(pdev, priv);
415 priv->reset = devm_reset_control_get(dev, NULL);
416 if (IS_ERR(priv->reset)) {
417 ret = PTR_ERR(priv->reset);
418 dev_err(dev, "failed to get device reset, err=%d\n", ret);
422 ret = reset_control_reset(priv->reset);
426 ret = dwc3_meson_g12a_get_phys(priv);
431 ret = regulator_enable(priv->vbus);
437 priv->otg_mode = usb_get_dr_mode(dev);
439 dwc3_meson_g12a_usb_init(priv);
442 for (i = 0 ; i < PHY_COUNT ; ++i) {
443 ret = phy_init(priv->phys[i]);
449 for (i = 0 ; i < PHY_COUNT ; ++i) {
450 ret = phy_power_on(priv->phys[i]);
455 ret = of_platform_populate(np, NULL, NULL, dev);
457 clk_disable_unprepare(priv->clk);
461 /* Setup OTG mode corresponding to the ID pin */
462 if (priv->otg_mode == USB_DR_MODE_OTG) {
463 /* TOFIX Handle ID mode toggling via IRQ */
464 otg_id = dwc3_meson_g12a_get_id(priv);
465 if (otg_id != priv->otg_phy_mode) {
466 if (dwc3_meson_g12a_otg_mode_set(priv, otg_id))
467 dev_warn(dev, "Failed to switch OTG mode\n");
471 /* Setup role switcher */
472 priv->switch_desc.usb2_port = dwc3_meson_g12_find_child(dev,
474 priv->switch_desc.udc = dwc3_meson_g12_find_child(dev, "snps,dwc2");
475 priv->switch_desc.allow_userspace_control = true;
476 priv->switch_desc.set = dwc3_meson_g12a_role_set;
477 priv->switch_desc.get = dwc3_meson_g12a_role_get;
479 priv->role_switch = usb_role_switch_register(dev, &priv->switch_desc);
480 if (IS_ERR(priv->role_switch))
481 dev_warn(dev, "Unable to register Role Switch\n");
483 pm_runtime_set_active(dev);
484 pm_runtime_enable(dev);
485 pm_runtime_get_sync(dev);
490 for (i = 0 ; i < PHY_COUNT ; ++i)
491 phy_power_off(priv->phys[i]);
494 for (i = 0 ; i < PHY_COUNT ; ++i)
495 phy_exit(priv->phys[i]);
500 static int dwc3_meson_g12a_remove(struct platform_device *pdev)
502 struct dwc3_meson_g12a *priv = platform_get_drvdata(pdev);
503 struct device *dev = &pdev->dev;
506 usb_role_switch_unregister(priv->role_switch);
508 of_platform_depopulate(dev);
510 for (i = 0 ; i < PHY_COUNT ; ++i) {
511 phy_power_off(priv->phys[i]);
512 phy_exit(priv->phys[i]);
515 pm_runtime_disable(dev);
516 pm_runtime_put_noidle(dev);
517 pm_runtime_set_suspended(dev);
522 static int __maybe_unused dwc3_meson_g12a_runtime_suspend(struct device *dev)
524 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
526 clk_disable(priv->clk);
531 static int __maybe_unused dwc3_meson_g12a_runtime_resume(struct device *dev)
533 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
535 return clk_enable(priv->clk);
538 static int __maybe_unused dwc3_meson_g12a_suspend(struct device *dev)
540 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
543 for (i = 0 ; i < PHY_COUNT ; ++i) {
544 phy_power_off(priv->phys[i]);
545 phy_exit(priv->phys[i]);
548 reset_control_assert(priv->reset);
553 static int __maybe_unused dwc3_meson_g12a_resume(struct device *dev)
555 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
558 reset_control_deassert(priv->reset);
560 dwc3_meson_g12a_usb_init(priv);
563 for (i = 0 ; i < PHY_COUNT ; ++i) {
564 ret = phy_init(priv->phys[i]);
570 for (i = 0 ; i < PHY_COUNT ; ++i) {
571 ret = phy_power_on(priv->phys[i]);
579 static const struct dev_pm_ops dwc3_meson_g12a_dev_pm_ops = {
580 SET_SYSTEM_SLEEP_PM_OPS(dwc3_meson_g12a_suspend, dwc3_meson_g12a_resume)
581 SET_RUNTIME_PM_OPS(dwc3_meson_g12a_runtime_suspend,
582 dwc3_meson_g12a_runtime_resume, NULL)
585 static const struct of_device_id dwc3_meson_g12a_match[] = {
586 { .compatible = "amlogic,meson-g12a-usb-ctrl" },
589 MODULE_DEVICE_TABLE(of, dwc3_meson_g12a_match);
591 static struct platform_driver dwc3_meson_g12a_driver = {
592 .probe = dwc3_meson_g12a_probe,
593 .remove = dwc3_meson_g12a_remove,
595 .name = "dwc3-meson-g12a",
596 .of_match_table = dwc3_meson_g12a_match,
597 .pm = &dwc3_meson_g12a_dev_pm_ops,
601 module_platform_driver(dwc3_meson_g12a_driver);
602 MODULE_LICENSE("GPL v2");
603 MODULE_DESCRIPTION("Amlogic Meson G12A USB Glue Layer");