1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2012 Freescale Semiconductor, Inc.
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
10 #include <linux/delay.h>
12 #include "ci_hdrc_imx.h"
14 #define MX25_USB_PHY_CTRL_OFFSET 0x08
15 #define MX25_BM_EXTERNAL_VBUS_DIVIDER BIT(23)
17 #define MX25_EHCI_INTERFACE_SINGLE_UNI (2 << 0)
18 #define MX25_EHCI_INTERFACE_DIFF_UNI (0 << 0)
19 #define MX25_EHCI_INTERFACE_MASK (0xf)
21 #define MX25_OTG_SIC_SHIFT 29
22 #define MX25_OTG_SIC_MASK (0x3 << MX25_OTG_SIC_SHIFT)
23 #define MX25_OTG_PM_BIT BIT(24)
24 #define MX25_OTG_PP_BIT BIT(11)
25 #define MX25_OTG_OCPOL_BIT BIT(3)
27 #define MX25_H1_SIC_SHIFT 21
28 #define MX25_H1_SIC_MASK (0x3 << MX25_H1_SIC_SHIFT)
29 #define MX25_H1_PP_BIT BIT(18)
30 #define MX25_H1_PM_BIT BIT(16)
31 #define MX25_H1_IPPUE_UP_BIT BIT(7)
32 #define MX25_H1_IPPUE_DOWN_BIT BIT(6)
33 #define MX25_H1_TLL_BIT BIT(5)
34 #define MX25_H1_USBTE_BIT BIT(4)
35 #define MX25_H1_OCPOL_BIT BIT(2)
37 #define MX27_H1_PM_BIT BIT(8)
38 #define MX27_H2_PM_BIT BIT(16)
39 #define MX27_OTG_PM_BIT BIT(24)
41 #define MX53_USB_OTG_PHY_CTRL_0_OFFSET 0x08
42 #define MX53_USB_OTG_PHY_CTRL_1_OFFSET 0x0c
43 #define MX53_USB_CTRL_1_OFFSET 0x10
44 #define MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_MASK (0x11 << 2)
45 #define MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_ULPI BIT(2)
46 #define MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_MASK (0x11 << 6)
47 #define MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_ULPI BIT(6)
48 #define MX53_USB_UH2_CTRL_OFFSET 0x14
49 #define MX53_USB_UH3_CTRL_OFFSET 0x18
50 #define MX53_USB_CLKONOFF_CTRL_OFFSET 0x24
51 #define MX53_USB_CLKONOFF_CTRL_H2_INT60CKOFF BIT(21)
52 #define MX53_USB_CLKONOFF_CTRL_H3_INT60CKOFF BIT(22)
53 #define MX53_BM_OVER_CUR_DIS_H1 BIT(5)
54 #define MX53_BM_OVER_CUR_DIS_OTG BIT(8)
55 #define MX53_BM_OVER_CUR_DIS_UHx BIT(30)
56 #define MX53_USB_CTRL_1_UH2_ULPI_EN BIT(26)
57 #define MX53_USB_CTRL_1_UH3_ULPI_EN BIT(27)
58 #define MX53_USB_UHx_CTRL_WAKE_UP_EN BIT(7)
59 #define MX53_USB_UHx_CTRL_ULPI_INT_EN BIT(8)
60 #define MX53_USB_PHYCTRL1_PLLDIV_MASK 0x3
61 #define MX53_USB_PLL_DIV_24_MHZ 0x01
63 #define MX6_BM_NON_BURST_SETTING BIT(1)
64 #define MX6_BM_OVER_CUR_DIS BIT(7)
65 #define MX6_BM_OVER_CUR_POLARITY BIT(8)
66 #define MX6_BM_WAKEUP_ENABLE BIT(10)
67 #define MX6_BM_ID_WAKEUP BIT(16)
68 #define MX6_BM_VBUS_WAKEUP BIT(17)
69 #define MX6SX_BM_DPDM_WAKEUP_EN BIT(29)
70 #define MX6_BM_WAKEUP_INTR BIT(31)
71 #define MX6_USB_OTG1_PHY_CTRL 0x18
72 /* For imx6dql, it is host-only controller, for later imx6, it is otg's */
73 #define MX6_USB_OTG2_PHY_CTRL 0x1c
74 #define MX6SX_USB_VBUS_WAKEUP_SOURCE(v) (v << 8)
75 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_VBUS MX6SX_USB_VBUS_WAKEUP_SOURCE(0)
76 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_AVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(1)
77 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(2)
78 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_SESS_END MX6SX_USB_VBUS_WAKEUP_SOURCE(3)
80 #define VF610_OVER_CUR_DIS BIT(7)
82 #define MX7D_USBNC_USB_CTRL2 0x4
83 #define MX7D_USB_VBUS_WAKEUP_SOURCE_MASK 0x3
84 #define MX7D_USB_VBUS_WAKEUP_SOURCE(v) (v << 0)
85 #define MX7D_USB_VBUS_WAKEUP_SOURCE_VBUS MX7D_USB_VBUS_WAKEUP_SOURCE(0)
86 #define MX7D_USB_VBUS_WAKEUP_SOURCE_AVALID MX7D_USB_VBUS_WAKEUP_SOURCE(1)
87 #define MX7D_USB_VBUS_WAKEUP_SOURCE_BVALID MX7D_USB_VBUS_WAKEUP_SOURCE(2)
88 #define MX7D_USB_VBUS_WAKEUP_SOURCE_SESS_END MX7D_USB_VBUS_WAKEUP_SOURCE(3)
91 /* It's called once when probe a usb device */
92 int (*init
)(struct imx_usbmisc_data
*data
);
93 /* It's called once after adding a usb device */
94 int (*post
)(struct imx_usbmisc_data
*data
);
95 /* It's called when we need to enable/disable usb wakeup */
96 int (*set_wakeup
)(struct imx_usbmisc_data
*data
, bool enabled
);
102 const struct usbmisc_ops
*ops
;
105 static inline bool is_imx53_usbmisc(struct imx_usbmisc_data
*data
);
107 static int usbmisc_imx25_init(struct imx_usbmisc_data
*data
)
109 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
116 spin_lock_irqsave(&usbmisc
->lock
, flags
);
117 switch (data
->index
) {
119 val
= readl(usbmisc
->base
);
120 val
&= ~(MX25_OTG_SIC_MASK
| MX25_OTG_PP_BIT
);
121 val
|= (MX25_EHCI_INTERFACE_DIFF_UNI
& MX25_EHCI_INTERFACE_MASK
) << MX25_OTG_SIC_SHIFT
;
122 val
|= (MX25_OTG_PM_BIT
| MX25_OTG_OCPOL_BIT
);
123 writel(val
, usbmisc
->base
);
126 val
= readl(usbmisc
->base
);
127 val
&= ~(MX25_H1_SIC_MASK
| MX25_H1_PP_BIT
| MX25_H1_IPPUE_UP_BIT
);
128 val
|= (MX25_EHCI_INTERFACE_SINGLE_UNI
& MX25_EHCI_INTERFACE_MASK
) << MX25_H1_SIC_SHIFT
;
129 val
|= (MX25_H1_PM_BIT
| MX25_H1_OCPOL_BIT
| MX25_H1_TLL_BIT
|
130 MX25_H1_USBTE_BIT
| MX25_H1_IPPUE_DOWN_BIT
);
132 writel(val
, usbmisc
->base
);
136 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
141 static int usbmisc_imx25_post(struct imx_usbmisc_data
*data
)
143 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
152 spin_lock_irqsave(&usbmisc
->lock
, flags
);
153 reg
= usbmisc
->base
+ MX25_USB_PHY_CTRL_OFFSET
;
155 writel(val
| MX25_BM_EXTERNAL_VBUS_DIVIDER
, reg
);
156 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
157 usleep_range(5000, 10000); /* needed to stabilize voltage */
163 static int usbmisc_imx27_init(struct imx_usbmisc_data
*data
)
165 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
169 switch (data
->index
) {
171 val
= MX27_OTG_PM_BIT
;
174 val
= MX27_H1_PM_BIT
;
177 val
= MX27_H2_PM_BIT
;
183 spin_lock_irqsave(&usbmisc
->lock
, flags
);
184 if (data
->disable_oc
)
185 val
= readl(usbmisc
->base
) | val
;
187 val
= readl(usbmisc
->base
) & ~val
;
188 writel(val
, usbmisc
->base
);
189 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
194 static int usbmisc_imx53_init(struct imx_usbmisc_data
*data
)
196 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
197 void __iomem
*reg
= NULL
;
204 /* Select a 24 MHz reference clock for the PHY */
205 val
= readl(usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_1_OFFSET
);
206 val
&= ~MX53_USB_PHYCTRL1_PLLDIV_MASK
;
207 val
|= MX53_USB_PLL_DIV_24_MHZ
;
208 writel(val
, usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_1_OFFSET
);
210 spin_lock_irqsave(&usbmisc
->lock
, flags
);
212 switch (data
->index
) {
214 if (data
->disable_oc
) {
215 reg
= usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_0_OFFSET
;
216 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_OTG
;
221 if (data
->disable_oc
) {
222 reg
= usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_0_OFFSET
;
223 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_H1
;
229 /* set USBH2 into ULPI-mode. */
230 reg
= usbmisc
->base
+ MX53_USB_CTRL_1_OFFSET
;
231 val
= readl(reg
) | MX53_USB_CTRL_1_UH2_ULPI_EN
;
232 /* select ULPI clock */
233 val
&= ~MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_MASK
;
234 val
|= MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_ULPI
;
236 /* Set interrupt wake up enable */
237 reg
= usbmisc
->base
+ MX53_USB_UH2_CTRL_OFFSET
;
238 val
= readl(reg
) | MX53_USB_UHx_CTRL_WAKE_UP_EN
239 | MX53_USB_UHx_CTRL_ULPI_INT_EN
;
241 if (is_imx53_usbmisc(data
)) {
242 /* Disable internal 60Mhz clock */
243 reg
= usbmisc
->base
+
244 MX53_USB_CLKONOFF_CTRL_OFFSET
;
246 MX53_USB_CLKONOFF_CTRL_H2_INT60CKOFF
;
251 if (data
->disable_oc
) {
252 reg
= usbmisc
->base
+ MX53_USB_UH2_CTRL_OFFSET
;
253 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_UHx
;
259 /* set USBH3 into ULPI-mode. */
260 reg
= usbmisc
->base
+ MX53_USB_CTRL_1_OFFSET
;
261 val
= readl(reg
) | MX53_USB_CTRL_1_UH3_ULPI_EN
;
262 /* select ULPI clock */
263 val
&= ~MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_MASK
;
264 val
|= MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_ULPI
;
266 /* Set interrupt wake up enable */
267 reg
= usbmisc
->base
+ MX53_USB_UH3_CTRL_OFFSET
;
268 val
= readl(reg
) | MX53_USB_UHx_CTRL_WAKE_UP_EN
269 | MX53_USB_UHx_CTRL_ULPI_INT_EN
;
272 if (is_imx53_usbmisc(data
)) {
273 /* Disable internal 60Mhz clock */
274 reg
= usbmisc
->base
+
275 MX53_USB_CLKONOFF_CTRL_OFFSET
;
277 MX53_USB_CLKONOFF_CTRL_H3_INT60CKOFF
;
281 if (data
->disable_oc
) {
282 reg
= usbmisc
->base
+ MX53_USB_UH3_CTRL_OFFSET
;
283 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_UHx
;
289 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
294 static int usbmisc_imx6q_set_wakeup
295 (struct imx_usbmisc_data
*data
, bool enabled
)
297 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
300 u32 wakeup_setting
= (MX6_BM_WAKEUP_ENABLE
|
301 MX6_BM_VBUS_WAKEUP
| MX6_BM_ID_WAKEUP
);
307 spin_lock_irqsave(&usbmisc
->lock
, flags
);
308 val
= readl(usbmisc
->base
+ data
->index
* 4);
310 val
|= wakeup_setting
;
311 writel(val
, usbmisc
->base
+ data
->index
* 4);
313 if (val
& MX6_BM_WAKEUP_INTR
)
314 pr_debug("wakeup int at ci_hdrc.%d\n", data
->index
);
315 val
&= ~wakeup_setting
;
316 writel(val
, usbmisc
->base
+ data
->index
* 4);
318 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
323 static int usbmisc_imx6q_init(struct imx_usbmisc_data
*data
)
325 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
332 spin_lock_irqsave(&usbmisc
->lock
, flags
);
334 reg
= readl(usbmisc
->base
+ data
->index
* 4);
335 if (data
->disable_oc
) {
336 reg
|= MX6_BM_OVER_CUR_DIS
;
337 } else if (data
->oc_polarity
== 1) {
339 reg
&= ~(MX6_BM_OVER_CUR_DIS
| MX6_BM_OVER_CUR_POLARITY
);
341 writel(reg
, usbmisc
->base
+ data
->index
* 4);
343 /* SoC non-burst setting */
344 reg
= readl(usbmisc
->base
+ data
->index
* 4);
345 writel(reg
| MX6_BM_NON_BURST_SETTING
,
346 usbmisc
->base
+ data
->index
* 4);
348 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
350 usbmisc_imx6q_set_wakeup(data
, false);
355 static int usbmisc_imx6sx_init(struct imx_usbmisc_data
*data
)
357 void __iomem
*reg
= NULL
;
359 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
362 usbmisc_imx6q_init(data
);
364 if (data
->index
== 0 || data
->index
== 1) {
365 reg
= usbmisc
->base
+ MX6_USB_OTG1_PHY_CTRL
+ data
->index
* 4;
366 spin_lock_irqsave(&usbmisc
->lock
, flags
);
367 /* Set vbus wakeup source as bvalid */
369 writel(val
| MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID
, reg
);
371 * Disable dp/dm wakeup in device mode when vbus is
374 val
= readl(usbmisc
->base
+ data
->index
* 4);
375 writel(val
& ~MX6SX_BM_DPDM_WAKEUP_EN
,
376 usbmisc
->base
+ data
->index
* 4);
377 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
383 static int usbmisc_vf610_init(struct imx_usbmisc_data
*data
)
385 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
389 * Vybrid only has one misc register set, but in two different
390 * areas. These is reflected in two instances of this driver.
392 if (data
->index
>= 1)
395 if (data
->disable_oc
) {
396 reg
= readl(usbmisc
->base
);
397 writel(reg
| VF610_OVER_CUR_DIS
, usbmisc
->base
);
403 static int usbmisc_imx7d_set_wakeup
404 (struct imx_usbmisc_data
*data
, bool enabled
)
406 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
409 u32 wakeup_setting
= (MX6_BM_WAKEUP_ENABLE
|
410 MX6_BM_VBUS_WAKEUP
| MX6_BM_ID_WAKEUP
);
412 spin_lock_irqsave(&usbmisc
->lock
, flags
);
413 val
= readl(usbmisc
->base
);
415 writel(val
| wakeup_setting
, usbmisc
->base
);
417 if (val
& MX6_BM_WAKEUP_INTR
)
418 dev_dbg(data
->dev
, "wakeup int\n");
419 writel(val
& ~wakeup_setting
, usbmisc
->base
);
421 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
426 static int usbmisc_imx7d_init(struct imx_usbmisc_data
*data
)
428 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
432 if (data
->index
>= 1)
435 spin_lock_irqsave(&usbmisc
->lock
, flags
);
436 reg
= readl(usbmisc
->base
);
437 if (data
->disable_oc
) {
438 reg
|= MX6_BM_OVER_CUR_DIS
;
439 } else if (data
->oc_polarity
== 1) {
441 reg
&= ~(MX6_BM_OVER_CUR_DIS
| MX6_BM_OVER_CUR_POLARITY
);
443 writel(reg
, usbmisc
->base
);
445 reg
= readl(usbmisc
->base
+ MX7D_USBNC_USB_CTRL2
);
446 reg
&= ~MX7D_USB_VBUS_WAKEUP_SOURCE_MASK
;
447 writel(reg
| MX7D_USB_VBUS_WAKEUP_SOURCE_BVALID
,
448 usbmisc
->base
+ MX7D_USBNC_USB_CTRL2
);
449 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
451 usbmisc_imx7d_set_wakeup(data
, false);
456 static const struct usbmisc_ops imx25_usbmisc_ops
= {
457 .init
= usbmisc_imx25_init
,
458 .post
= usbmisc_imx25_post
,
461 static const struct usbmisc_ops imx27_usbmisc_ops
= {
462 .init
= usbmisc_imx27_init
,
465 static const struct usbmisc_ops imx51_usbmisc_ops
= {
466 .init
= usbmisc_imx53_init
,
469 static const struct usbmisc_ops imx53_usbmisc_ops
= {
470 .init
= usbmisc_imx53_init
,
473 static const struct usbmisc_ops imx6q_usbmisc_ops
= {
474 .set_wakeup
= usbmisc_imx6q_set_wakeup
,
475 .init
= usbmisc_imx6q_init
,
478 static const struct usbmisc_ops vf610_usbmisc_ops
= {
479 .init
= usbmisc_vf610_init
,
482 static const struct usbmisc_ops imx6sx_usbmisc_ops
= {
483 .set_wakeup
= usbmisc_imx6q_set_wakeup
,
484 .init
= usbmisc_imx6sx_init
,
487 static const struct usbmisc_ops imx7d_usbmisc_ops
= {
488 .init
= usbmisc_imx7d_init
,
489 .set_wakeup
= usbmisc_imx7d_set_wakeup
,
492 static inline bool is_imx53_usbmisc(struct imx_usbmisc_data
*data
)
494 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
496 return usbmisc
->ops
== &imx53_usbmisc_ops
;
499 int imx_usbmisc_init(struct imx_usbmisc_data
*data
)
501 struct imx_usbmisc
*usbmisc
;
506 usbmisc
= dev_get_drvdata(data
->dev
);
507 if (!usbmisc
->ops
->init
)
509 return usbmisc
->ops
->init(data
);
511 EXPORT_SYMBOL_GPL(imx_usbmisc_init
);
513 int imx_usbmisc_init_post(struct imx_usbmisc_data
*data
)
515 struct imx_usbmisc
*usbmisc
;
520 usbmisc
= dev_get_drvdata(data
->dev
);
521 if (!usbmisc
->ops
->post
)
523 return usbmisc
->ops
->post(data
);
525 EXPORT_SYMBOL_GPL(imx_usbmisc_init_post
);
527 int imx_usbmisc_set_wakeup(struct imx_usbmisc_data
*data
, bool enabled
)
529 struct imx_usbmisc
*usbmisc
;
534 usbmisc
= dev_get_drvdata(data
->dev
);
535 if (!usbmisc
->ops
->set_wakeup
)
537 return usbmisc
->ops
->set_wakeup(data
, enabled
);
539 EXPORT_SYMBOL_GPL(imx_usbmisc_set_wakeup
);
541 static const struct of_device_id usbmisc_imx_dt_ids
[] = {
543 .compatible
= "fsl,imx25-usbmisc",
544 .data
= &imx25_usbmisc_ops
,
547 .compatible
= "fsl,imx35-usbmisc",
548 .data
= &imx25_usbmisc_ops
,
551 .compatible
= "fsl,imx27-usbmisc",
552 .data
= &imx27_usbmisc_ops
,
555 .compatible
= "fsl,imx51-usbmisc",
556 .data
= &imx51_usbmisc_ops
,
559 .compatible
= "fsl,imx53-usbmisc",
560 .data
= &imx53_usbmisc_ops
,
563 .compatible
= "fsl,imx6q-usbmisc",
564 .data
= &imx6q_usbmisc_ops
,
567 .compatible
= "fsl,vf610-usbmisc",
568 .data
= &vf610_usbmisc_ops
,
571 .compatible
= "fsl,imx6sx-usbmisc",
572 .data
= &imx6sx_usbmisc_ops
,
575 .compatible
= "fsl,imx6ul-usbmisc",
576 .data
= &imx6sx_usbmisc_ops
,
579 .compatible
= "fsl,imx7d-usbmisc",
580 .data
= &imx7d_usbmisc_ops
,
584 MODULE_DEVICE_TABLE(of
, usbmisc_imx_dt_ids
);
586 static int usbmisc_imx_probe(struct platform_device
*pdev
)
588 struct resource
*res
;
589 struct imx_usbmisc
*data
;
590 const struct of_device_id
*of_id
;
592 of_id
= of_match_device(usbmisc_imx_dt_ids
, &pdev
->dev
);
596 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
600 spin_lock_init(&data
->lock
);
602 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
603 data
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
604 if (IS_ERR(data
->base
))
605 return PTR_ERR(data
->base
);
607 data
->ops
= (const struct usbmisc_ops
*)of_id
->data
;
608 platform_set_drvdata(pdev
, data
);
613 static int usbmisc_imx_remove(struct platform_device
*pdev
)
618 static struct platform_driver usbmisc_imx_driver
= {
619 .probe
= usbmisc_imx_probe
,
620 .remove
= usbmisc_imx_remove
,
622 .name
= "usbmisc_imx",
623 .of_match_table
= usbmisc_imx_dt_ids
,
627 module_platform_driver(usbmisc_imx_driver
);
629 MODULE_ALIAS("platform:usbmisc-imx");
630 MODULE_LICENSE("GPL v2");
631 MODULE_DESCRIPTION("driver for imx usb non-core registers");
632 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");