1 // SPDX-License-Identifier: GPL-2.0
3 * USB Glue for Amlogic G12A SoCs
5 * Copyright (c) 2019 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
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
14 * - Dynamic OTG switching with ID change interrupt
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/clk.h>
22 #include <linux/of_platform.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/bitfield.h>
26 #include <linux/bitops.h>
27 #include <linux/reset.h>
28 #include <linux/phy/phy.h>
29 #include <linux/usb/otg.h>
30 #include <linux/usb/role.h>
31 #include <linux/regulator/consumer.h>
33 /* USB2 Ports Control Registers */
35 #define U2P_REG_SIZE 0x20
38 #define U2P_R0_HOST_DEVICE BIT(0)
39 #define U2P_R0_POWER_OK BIT(1)
40 #define U2P_R0_HAST_MODE BIT(2)
41 #define U2P_R0_POWER_ON_RESET BIT(3)
42 #define U2P_R0_ID_PULLUP BIT(4)
43 #define U2P_R0_DRV_VBUS BIT(5)
46 #define U2P_R1_PHY_READY BIT(0)
47 #define U2P_R1_ID_DIG BIT(1)
48 #define U2P_R1_OTG_SESSION_VALID BIT(2)
49 #define U2P_R1_VBUS_VALID BIT(3)
51 /* USB Glue Control Registers */
54 #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
55 #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
56 #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
57 #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
58 #define USB_R0_U2D_ACT BIT(31)
61 #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
62 #define USB_R1_U3H_PME_ENABLE BIT(1)
63 #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(4, 2)
64 #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(9, 7)
65 #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(13, 12)
66 #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
67 #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
68 #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
69 #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
70 #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
73 #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
74 #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
77 #define USB_R3_P30_SSC_ENABLE BIT(0)
78 #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
79 #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
80 #define USB_R3_P30_REF_SSP_EN BIT(13)
83 #define USB_R4_P21_PORT_RESET_0 BIT(0)
84 #define USB_R4_P21_SLEEP_M0 BIT(1)
85 #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
86 #define USB_R4_P21_ONLY BIT(4)
89 #define USB_R5_ID_DIG_SYNC BIT(0)
90 #define USB_R5_ID_DIG_REG BIT(1)
91 #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
92 #define USB_R5_ID_DIG_EN_0 BIT(4)
93 #define USB_R5_ID_DIG_EN_1 BIT(5)
94 #define USB_R5_ID_DIG_CURR BIT(6)
95 #define USB_R5_ID_DIG_IRQ BIT(7)
96 #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
97 #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
106 static const char *phy_names
[PHY_COUNT
] = {
107 "usb2-phy0", "usb2-phy1", "usb3-phy0",
110 struct dwc3_meson_g12a
{
112 struct regmap
*regmap
;
114 struct reset_control
*reset
;
115 struct phy
*phys
[PHY_COUNT
];
116 enum usb_dr_mode otg_mode
;
117 enum phy_mode otg_phy_mode
;
118 unsigned int usb2_ports
;
119 unsigned int usb3_ports
;
120 struct regulator
*vbus
;
121 struct usb_role_switch_desc switch_desc
;
122 struct usb_role_switch
*role_switch
;
125 static void dwc3_meson_g12a_usb2_set_mode(struct dwc3_meson_g12a
*priv
,
126 int i
, enum phy_mode mode
)
128 if (mode
== PHY_MODE_USB_HOST
)
129 regmap_update_bits(priv
->regmap
, U2P_R0
+ (U2P_REG_SIZE
* i
),
133 regmap_update_bits(priv
->regmap
, U2P_R0
+ (U2P_REG_SIZE
* i
),
134 U2P_R0_HOST_DEVICE
, 0);
137 static int dwc3_meson_g12a_usb2_init(struct dwc3_meson_g12a
*priv
)
141 if (priv
->otg_mode
== USB_DR_MODE_PERIPHERAL
)
142 priv
->otg_phy_mode
= PHY_MODE_USB_DEVICE
;
144 priv
->otg_phy_mode
= PHY_MODE_USB_HOST
;
146 for (i
= 0 ; i
< USB3_HOST_PHY
; ++i
) {
150 regmap_update_bits(priv
->regmap
, U2P_R0
+ (U2P_REG_SIZE
* i
),
151 U2P_R0_POWER_ON_RESET
,
152 U2P_R0_POWER_ON_RESET
);
154 if (i
== USB2_OTG_PHY
) {
155 regmap_update_bits(priv
->regmap
,
156 U2P_R0
+ (U2P_REG_SIZE
* i
),
157 U2P_R0_ID_PULLUP
| U2P_R0_DRV_VBUS
,
158 U2P_R0_ID_PULLUP
| U2P_R0_DRV_VBUS
);
160 dwc3_meson_g12a_usb2_set_mode(priv
, i
,
163 dwc3_meson_g12a_usb2_set_mode(priv
, i
,
166 regmap_update_bits(priv
->regmap
, U2P_R0
+ (U2P_REG_SIZE
* i
),
167 U2P_R0_POWER_ON_RESET
, 0);
173 static void dwc3_meson_g12a_usb3_init(struct dwc3_meson_g12a
*priv
)
175 regmap_update_bits(priv
->regmap
, USB_R3
,
176 USB_R3_P30_SSC_RANGE_MASK
|
177 USB_R3_P30_REF_SSP_EN
,
178 USB_R3_P30_SSC_ENABLE
|
179 FIELD_PREP(USB_R3_P30_SSC_RANGE_MASK
, 2) |
180 USB_R3_P30_REF_SSP_EN
);
183 regmap_update_bits(priv
->regmap
, USB_R2
,
184 USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK
,
185 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK
, 0x15));
187 regmap_update_bits(priv
->regmap
, USB_R2
,
188 USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK
,
189 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK
, 0x20));
193 regmap_update_bits(priv
->regmap
, USB_R1
,
194 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT
,
195 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT
);
197 regmap_update_bits(priv
->regmap
, USB_R1
,
198 USB_R1_P30_PCS_TX_SWING_FULL_MASK
,
199 FIELD_PREP(USB_R1_P30_PCS_TX_SWING_FULL_MASK
, 127));
202 static void dwc3_meson_g12a_usb_otg_apply_mode(struct dwc3_meson_g12a
*priv
)
204 if (priv
->otg_phy_mode
== PHY_MODE_USB_DEVICE
) {
205 regmap_update_bits(priv
->regmap
, USB_R0
,
206 USB_R0_U2D_ACT
, USB_R0_U2D_ACT
);
207 regmap_update_bits(priv
->regmap
, USB_R0
,
208 USB_R0_U2D_SS_SCALEDOWN_MODE_MASK
, 0);
209 regmap_update_bits(priv
->regmap
, USB_R4
,
210 USB_R4_P21_SLEEP_M0
, USB_R4_P21_SLEEP_M0
);
212 regmap_update_bits(priv
->regmap
, USB_R0
,
214 regmap_update_bits(priv
->regmap
, USB_R4
,
215 USB_R4_P21_SLEEP_M0
, 0);
219 static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a
*priv
)
223 ret
= dwc3_meson_g12a_usb2_init(priv
);
227 regmap_update_bits(priv
->regmap
, USB_R1
,
228 USB_R1_U3H_FLADJ_30MHZ_REG_MASK
,
229 FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK
, 0x20));
231 regmap_update_bits(priv
->regmap
, USB_R5
,
234 regmap_update_bits(priv
->regmap
, USB_R5
,
237 regmap_update_bits(priv
->regmap
, USB_R5
,
238 USB_R5_ID_DIG_TH_MASK
,
239 FIELD_PREP(USB_R5_ID_DIG_TH_MASK
, 0xff));
241 /* If we have an actual SuperSpeed port, initialize it */
242 if (priv
->usb3_ports
)
243 dwc3_meson_g12a_usb3_init(priv
);
245 dwc3_meson_g12a_usb_otg_apply_mode(priv
);
250 static const struct regmap_config phy_meson_g12a_usb3_regmap_conf
= {
254 .max_register
= USB_R5
,
257 static int dwc3_meson_g12a_get_phys(struct dwc3_meson_g12a
*priv
)
261 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
262 priv
->phys
[i
] = devm_phy_optional_get(priv
->dev
, phy_names
[i
]);
266 if (IS_ERR(priv
->phys
[i
]))
267 return PTR_ERR(priv
->phys
[i
]);
269 if (i
== USB3_HOST_PHY
)
275 dev_info(priv
->dev
, "USB2 ports: %d\n", priv
->usb2_ports
);
276 dev_info(priv
->dev
, "USB3 ports: %d\n", priv
->usb3_ports
);
281 static enum phy_mode
dwc3_meson_g12a_get_id(struct dwc3_meson_g12a
*priv
)
285 regmap_read(priv
->regmap
, USB_R5
, ®
);
287 if (reg
& (USB_R5_ID_DIG_SYNC
| USB_R5_ID_DIG_REG
))
288 return PHY_MODE_USB_DEVICE
;
290 return PHY_MODE_USB_HOST
;
293 static int dwc3_meson_g12a_otg_mode_set(struct dwc3_meson_g12a
*priv
,
298 if (!priv
->phys
[USB2_OTG_PHY
])
301 if (mode
== PHY_MODE_USB_HOST
)
302 dev_info(priv
->dev
, "switching to Host Mode\n");
304 dev_info(priv
->dev
, "switching to Device Mode\n");
307 if (mode
== PHY_MODE_USB_DEVICE
)
308 ret
= regulator_disable(priv
->vbus
);
310 ret
= regulator_enable(priv
->vbus
);
315 priv
->otg_phy_mode
= mode
;
317 dwc3_meson_g12a_usb2_set_mode(priv
, USB2_OTG_PHY
, mode
);
319 dwc3_meson_g12a_usb_otg_apply_mode(priv
);
324 static int dwc3_meson_g12a_role_set(struct device
*dev
, enum usb_role role
)
326 struct dwc3_meson_g12a
*priv
= dev_get_drvdata(dev
);
329 if (role
== USB_ROLE_NONE
)
332 mode
= (role
== USB_ROLE_HOST
) ? PHY_MODE_USB_HOST
333 : PHY_MODE_USB_DEVICE
;
335 if (mode
== priv
->otg_phy_mode
)
338 return dwc3_meson_g12a_otg_mode_set(priv
, mode
);
341 static enum usb_role
dwc3_meson_g12a_role_get(struct device
*dev
)
343 struct dwc3_meson_g12a
*priv
= dev_get_drvdata(dev
);
345 return priv
->otg_phy_mode
== PHY_MODE_USB_HOST
?
346 USB_ROLE_HOST
: USB_ROLE_DEVICE
;
349 static irqreturn_t
dwc3_meson_g12a_irq_thread(int irq
, void *data
)
351 struct dwc3_meson_g12a
*priv
= data
;
352 enum phy_mode otg_id
;
354 otg_id
= dwc3_meson_g12a_get_id(priv
);
355 if (otg_id
!= priv
->otg_phy_mode
) {
356 if (dwc3_meson_g12a_otg_mode_set(priv
, otg_id
))
357 dev_warn(priv
->dev
, "Failed to switch OTG mode\n");
360 regmap_update_bits(priv
->regmap
, USB_R5
, USB_R5_ID_DIG_IRQ
, 0);
365 static struct device
*dwc3_meson_g12_find_child(struct device
*dev
,
366 const char *compatible
)
368 struct platform_device
*pdev
;
369 struct device_node
*np
;
371 np
= of_get_compatible_child(dev
->of_node
, compatible
);
375 pdev
= of_find_device_by_node(np
);
383 static int dwc3_meson_g12a_probe(struct platform_device
*pdev
)
385 struct dwc3_meson_g12a
*priv
;
386 struct device
*dev
= &pdev
->dev
;
387 struct device_node
*np
= dev
->of_node
;
389 struct resource
*res
;
390 enum phy_mode otg_id
;
393 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
397 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
398 base
= devm_ioremap_resource(dev
, res
);
400 return PTR_ERR(base
);
402 priv
->regmap
= devm_regmap_init_mmio(dev
, base
,
403 &phy_meson_g12a_usb3_regmap_conf
);
404 if (IS_ERR(priv
->regmap
))
405 return PTR_ERR(priv
->regmap
);
407 priv
->vbus
= devm_regulator_get_optional(dev
, "vbus");
408 if (IS_ERR(priv
->vbus
)) {
409 if (PTR_ERR(priv
->vbus
) == -EPROBE_DEFER
)
410 return PTR_ERR(priv
->vbus
);
414 priv
->clk
= devm_clk_get(dev
, NULL
);
415 if (IS_ERR(priv
->clk
))
416 return PTR_ERR(priv
->clk
);
418 ret
= clk_prepare_enable(priv
->clk
);
422 devm_add_action_or_reset(dev
,
423 (void(*)(void *))clk_disable_unprepare
,
426 platform_set_drvdata(pdev
, priv
);
429 priv
->reset
= devm_reset_control_get(dev
, NULL
);
430 if (IS_ERR(priv
->reset
)) {
431 ret
= PTR_ERR(priv
->reset
);
432 dev_err(dev
, "failed to get device reset, err=%d\n", ret
);
436 ret
= reset_control_reset(priv
->reset
);
440 ret
= dwc3_meson_g12a_get_phys(priv
);
445 ret
= regulator_enable(priv
->vbus
);
451 priv
->otg_mode
= usb_get_dr_mode(dev
);
453 if (priv
->otg_mode
== USB_DR_MODE_OTG
) {
454 /* Ack irq before registering */
455 regmap_update_bits(priv
->regmap
, USB_R5
,
456 USB_R5_ID_DIG_IRQ
, 0);
458 irq
= platform_get_irq(pdev
, 0);
459 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
460 dwc3_meson_g12a_irq_thread
,
461 IRQF_ONESHOT
, pdev
->name
, priv
);
466 dwc3_meson_g12a_usb_init(priv
);
469 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
470 ret
= phy_init(priv
->phys
[i
]);
476 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
477 ret
= phy_power_on(priv
->phys
[i
]);
482 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
484 clk_disable_unprepare(priv
->clk
);
488 /* Setup OTG mode corresponding to the ID pin */
489 if (priv
->otg_mode
== USB_DR_MODE_OTG
) {
490 otg_id
= dwc3_meson_g12a_get_id(priv
);
491 if (otg_id
!= priv
->otg_phy_mode
) {
492 if (dwc3_meson_g12a_otg_mode_set(priv
, otg_id
))
493 dev_warn(dev
, "Failed to switch OTG mode\n");
497 /* Setup role switcher */
498 priv
->switch_desc
.usb2_port
= dwc3_meson_g12_find_child(dev
,
500 priv
->switch_desc
.udc
= dwc3_meson_g12_find_child(dev
, "snps,dwc2");
501 priv
->switch_desc
.allow_userspace_control
= true;
502 priv
->switch_desc
.set
= dwc3_meson_g12a_role_set
;
503 priv
->switch_desc
.get
= dwc3_meson_g12a_role_get
;
505 priv
->role_switch
= usb_role_switch_register(dev
, &priv
->switch_desc
);
506 if (IS_ERR(priv
->role_switch
))
507 dev_warn(dev
, "Unable to register Role Switch\n");
509 pm_runtime_set_active(dev
);
510 pm_runtime_enable(dev
);
511 pm_runtime_get_sync(dev
);
516 for (i
= 0 ; i
< PHY_COUNT
; ++i
)
517 phy_power_off(priv
->phys
[i
]);
520 for (i
= 0 ; i
< PHY_COUNT
; ++i
)
521 phy_exit(priv
->phys
[i
]);
526 static int dwc3_meson_g12a_remove(struct platform_device
*pdev
)
528 struct dwc3_meson_g12a
*priv
= platform_get_drvdata(pdev
);
529 struct device
*dev
= &pdev
->dev
;
532 usb_role_switch_unregister(priv
->role_switch
);
534 of_platform_depopulate(dev
);
536 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
537 phy_power_off(priv
->phys
[i
]);
538 phy_exit(priv
->phys
[i
]);
541 pm_runtime_disable(dev
);
542 pm_runtime_put_noidle(dev
);
543 pm_runtime_set_suspended(dev
);
548 static int __maybe_unused
dwc3_meson_g12a_runtime_suspend(struct device
*dev
)
550 struct dwc3_meson_g12a
*priv
= dev_get_drvdata(dev
);
552 clk_disable(priv
->clk
);
557 static int __maybe_unused
dwc3_meson_g12a_runtime_resume(struct device
*dev
)
559 struct dwc3_meson_g12a
*priv
= dev_get_drvdata(dev
);
561 return clk_enable(priv
->clk
);
564 static int __maybe_unused
dwc3_meson_g12a_suspend(struct device
*dev
)
566 struct dwc3_meson_g12a
*priv
= dev_get_drvdata(dev
);
569 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
570 phy_power_off(priv
->phys
[i
]);
571 phy_exit(priv
->phys
[i
]);
574 reset_control_assert(priv
->reset
);
579 static int __maybe_unused
dwc3_meson_g12a_resume(struct device
*dev
)
581 struct dwc3_meson_g12a
*priv
= dev_get_drvdata(dev
);
584 reset_control_deassert(priv
->reset
);
586 dwc3_meson_g12a_usb_init(priv
);
589 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
590 ret
= phy_init(priv
->phys
[i
]);
596 for (i
= 0 ; i
< PHY_COUNT
; ++i
) {
597 ret
= phy_power_on(priv
->phys
[i
]);
605 static const struct dev_pm_ops dwc3_meson_g12a_dev_pm_ops
= {
606 SET_SYSTEM_SLEEP_PM_OPS(dwc3_meson_g12a_suspend
, dwc3_meson_g12a_resume
)
607 SET_RUNTIME_PM_OPS(dwc3_meson_g12a_runtime_suspend
,
608 dwc3_meson_g12a_runtime_resume
, NULL
)
611 static const struct of_device_id dwc3_meson_g12a_match
[] = {
612 { .compatible
= "amlogic,meson-g12a-usb-ctrl" },
615 MODULE_DEVICE_TABLE(of
, dwc3_meson_g12a_match
);
617 static struct platform_driver dwc3_meson_g12a_driver
= {
618 .probe
= dwc3_meson_g12a_probe
,
619 .remove
= dwc3_meson_g12a_remove
,
621 .name
= "dwc3-meson-g12a",
622 .of_match_table
= dwc3_meson_g12a_match
,
623 .pm
= &dwc3_meson_g12a_dev_pm_ops
,
627 module_platform_driver(dwc3_meson_g12a_driver
);
628 MODULE_LICENSE("GPL v2");
629 MODULE_DESCRIPTION("Amlogic Meson G12A USB Glue Layer");
630 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");