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
);
154 spin_lock_irqsave(&usbmisc
->lock
, flags
);
155 reg
= usbmisc
->base
+ MX25_USB_PHY_CTRL_OFFSET
;
159 val
|= MX25_BM_EXTERNAL_VBUS_DIVIDER
;
161 val
&= ~MX25_BM_EXTERNAL_VBUS_DIVIDER
;
164 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
165 usleep_range(5000, 10000); /* needed to stabilize voltage */
170 static int usbmisc_imx27_init(struct imx_usbmisc_data
*data
)
172 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
176 switch (data
->index
) {
178 val
= MX27_OTG_PM_BIT
;
181 val
= MX27_H1_PM_BIT
;
184 val
= MX27_H2_PM_BIT
;
190 spin_lock_irqsave(&usbmisc
->lock
, flags
);
191 if (data
->disable_oc
)
192 val
= readl(usbmisc
->base
) | val
;
194 val
= readl(usbmisc
->base
) & ~val
;
195 writel(val
, usbmisc
->base
);
196 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
201 static int usbmisc_imx53_init(struct imx_usbmisc_data
*data
)
203 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
204 void __iomem
*reg
= NULL
;
211 /* Select a 24 MHz reference clock for the PHY */
212 val
= readl(usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_1_OFFSET
);
213 val
&= ~MX53_USB_PHYCTRL1_PLLDIV_MASK
;
214 val
|= MX53_USB_PLL_DIV_24_MHZ
;
215 writel(val
, usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_1_OFFSET
);
217 spin_lock_irqsave(&usbmisc
->lock
, flags
);
219 switch (data
->index
) {
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_OTG
;
228 if (data
->disable_oc
) {
229 reg
= usbmisc
->base
+ MX53_USB_OTG_PHY_CTRL_0_OFFSET
;
230 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_H1
;
236 /* set USBH2 into ULPI-mode. */
237 reg
= usbmisc
->base
+ MX53_USB_CTRL_1_OFFSET
;
238 val
= readl(reg
) | MX53_USB_CTRL_1_UH2_ULPI_EN
;
239 /* select ULPI clock */
240 val
&= ~MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_MASK
;
241 val
|= MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_ULPI
;
243 /* Set interrupt wake up enable */
244 reg
= usbmisc
->base
+ MX53_USB_UH2_CTRL_OFFSET
;
245 val
= readl(reg
) | MX53_USB_UHx_CTRL_WAKE_UP_EN
246 | MX53_USB_UHx_CTRL_ULPI_INT_EN
;
248 if (is_imx53_usbmisc(data
)) {
249 /* Disable internal 60Mhz clock */
250 reg
= usbmisc
->base
+
251 MX53_USB_CLKONOFF_CTRL_OFFSET
;
253 MX53_USB_CLKONOFF_CTRL_H2_INT60CKOFF
;
258 if (data
->disable_oc
) {
259 reg
= usbmisc
->base
+ MX53_USB_UH2_CTRL_OFFSET
;
260 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_UHx
;
266 /* set USBH3 into ULPI-mode. */
267 reg
= usbmisc
->base
+ MX53_USB_CTRL_1_OFFSET
;
268 val
= readl(reg
) | MX53_USB_CTRL_1_UH3_ULPI_EN
;
269 /* select ULPI clock */
270 val
&= ~MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_MASK
;
271 val
|= MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_ULPI
;
273 /* Set interrupt wake up enable */
274 reg
= usbmisc
->base
+ MX53_USB_UH3_CTRL_OFFSET
;
275 val
= readl(reg
) | MX53_USB_UHx_CTRL_WAKE_UP_EN
276 | MX53_USB_UHx_CTRL_ULPI_INT_EN
;
279 if (is_imx53_usbmisc(data
)) {
280 /* Disable internal 60Mhz clock */
281 reg
= usbmisc
->base
+
282 MX53_USB_CLKONOFF_CTRL_OFFSET
;
284 MX53_USB_CLKONOFF_CTRL_H3_INT60CKOFF
;
288 if (data
->disable_oc
) {
289 reg
= usbmisc
->base
+ MX53_USB_UH3_CTRL_OFFSET
;
290 val
= readl(reg
) | MX53_BM_OVER_CUR_DIS_UHx
;
296 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
301 static int usbmisc_imx6q_set_wakeup
302 (struct imx_usbmisc_data
*data
, bool enabled
)
304 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
307 u32 wakeup_setting
= (MX6_BM_WAKEUP_ENABLE
|
308 MX6_BM_VBUS_WAKEUP
| MX6_BM_ID_WAKEUP
);
314 spin_lock_irqsave(&usbmisc
->lock
, flags
);
315 val
= readl(usbmisc
->base
+ data
->index
* 4);
317 val
|= wakeup_setting
;
319 if (val
& MX6_BM_WAKEUP_INTR
)
320 pr_debug("wakeup int at ci_hdrc.%d\n", data
->index
);
321 val
&= ~wakeup_setting
;
323 writel(val
, usbmisc
->base
+ data
->index
* 4);
324 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
329 static int usbmisc_imx6q_init(struct imx_usbmisc_data
*data
)
331 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
338 spin_lock_irqsave(&usbmisc
->lock
, flags
);
340 reg
= readl(usbmisc
->base
+ data
->index
* 4);
341 if (data
->disable_oc
) {
342 reg
|= MX6_BM_OVER_CUR_DIS
;
343 } else if (data
->oc_polarity
== 1) {
345 reg
&= ~(MX6_BM_OVER_CUR_DIS
| MX6_BM_OVER_CUR_POLARITY
);
347 reg
&= ~(MX6_BM_OVER_CUR_DIS
);
349 writel(reg
, usbmisc
->base
+ data
->index
* 4);
351 /* SoC non-burst setting */
352 reg
= readl(usbmisc
->base
+ data
->index
* 4);
353 writel(reg
| MX6_BM_NON_BURST_SETTING
,
354 usbmisc
->base
+ data
->index
* 4);
356 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
358 usbmisc_imx6q_set_wakeup(data
, false);
363 static int usbmisc_imx6sx_init(struct imx_usbmisc_data
*data
)
365 void __iomem
*reg
= NULL
;
367 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
370 usbmisc_imx6q_init(data
);
372 if (data
->index
== 0 || data
->index
== 1) {
373 reg
= usbmisc
->base
+ MX6_USB_OTG1_PHY_CTRL
+ data
->index
* 4;
374 spin_lock_irqsave(&usbmisc
->lock
, flags
);
375 /* Set vbus wakeup source as bvalid */
377 writel(val
| MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID
, reg
);
379 * Disable dp/dm wakeup in device mode when vbus is
382 val
= readl(usbmisc
->base
+ data
->index
* 4);
383 writel(val
& ~MX6SX_BM_DPDM_WAKEUP_EN
,
384 usbmisc
->base
+ data
->index
* 4);
385 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
391 static int usbmisc_vf610_init(struct imx_usbmisc_data
*data
)
393 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
397 * Vybrid only has one misc register set, but in two different
398 * areas. These is reflected in two instances of this driver.
400 if (data
->index
>= 1)
403 if (data
->disable_oc
) {
404 reg
= readl(usbmisc
->base
);
405 writel(reg
| VF610_OVER_CUR_DIS
, usbmisc
->base
);
411 static int usbmisc_imx7d_set_wakeup
412 (struct imx_usbmisc_data
*data
, bool enabled
)
414 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
417 u32 wakeup_setting
= (MX6_BM_WAKEUP_ENABLE
|
418 MX6_BM_VBUS_WAKEUP
| MX6_BM_ID_WAKEUP
);
420 spin_lock_irqsave(&usbmisc
->lock
, flags
);
421 val
= readl(usbmisc
->base
);
423 writel(val
| wakeup_setting
, usbmisc
->base
);
425 if (val
& MX6_BM_WAKEUP_INTR
)
426 dev_dbg(data
->dev
, "wakeup int\n");
427 writel(val
& ~wakeup_setting
, usbmisc
->base
);
429 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
434 static int usbmisc_imx7d_init(struct imx_usbmisc_data
*data
)
436 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
440 if (data
->index
>= 1)
443 spin_lock_irqsave(&usbmisc
->lock
, flags
);
444 reg
= readl(usbmisc
->base
);
445 if (data
->disable_oc
) {
446 reg
|= MX6_BM_OVER_CUR_DIS
;
447 } else if (data
->oc_polarity
== 1) {
449 reg
&= ~(MX6_BM_OVER_CUR_DIS
| MX6_BM_OVER_CUR_POLARITY
);
451 writel(reg
, usbmisc
->base
);
453 reg
= readl(usbmisc
->base
+ MX7D_USBNC_USB_CTRL2
);
454 reg
&= ~MX7D_USB_VBUS_WAKEUP_SOURCE_MASK
;
455 writel(reg
| MX7D_USB_VBUS_WAKEUP_SOURCE_BVALID
,
456 usbmisc
->base
+ MX7D_USBNC_USB_CTRL2
);
457 spin_unlock_irqrestore(&usbmisc
->lock
, flags
);
459 usbmisc_imx7d_set_wakeup(data
, false);
464 static const struct usbmisc_ops imx25_usbmisc_ops
= {
465 .init
= usbmisc_imx25_init
,
466 .post
= usbmisc_imx25_post
,
469 static const struct usbmisc_ops imx27_usbmisc_ops
= {
470 .init
= usbmisc_imx27_init
,
473 static const struct usbmisc_ops imx51_usbmisc_ops
= {
474 .init
= usbmisc_imx53_init
,
477 static const struct usbmisc_ops imx53_usbmisc_ops
= {
478 .init
= usbmisc_imx53_init
,
481 static const struct usbmisc_ops imx6q_usbmisc_ops
= {
482 .set_wakeup
= usbmisc_imx6q_set_wakeup
,
483 .init
= usbmisc_imx6q_init
,
486 static const struct usbmisc_ops vf610_usbmisc_ops
= {
487 .init
= usbmisc_vf610_init
,
490 static const struct usbmisc_ops imx6sx_usbmisc_ops
= {
491 .set_wakeup
= usbmisc_imx6q_set_wakeup
,
492 .init
= usbmisc_imx6sx_init
,
495 static const struct usbmisc_ops imx7d_usbmisc_ops
= {
496 .init
= usbmisc_imx7d_init
,
497 .set_wakeup
= usbmisc_imx7d_set_wakeup
,
500 static inline bool is_imx53_usbmisc(struct imx_usbmisc_data
*data
)
502 struct imx_usbmisc
*usbmisc
= dev_get_drvdata(data
->dev
);
504 return usbmisc
->ops
== &imx53_usbmisc_ops
;
507 int imx_usbmisc_init(struct imx_usbmisc_data
*data
)
509 struct imx_usbmisc
*usbmisc
;
514 usbmisc
= dev_get_drvdata(data
->dev
);
515 if (!usbmisc
->ops
->init
)
517 return usbmisc
->ops
->init(data
);
519 EXPORT_SYMBOL_GPL(imx_usbmisc_init
);
521 int imx_usbmisc_init_post(struct imx_usbmisc_data
*data
)
523 struct imx_usbmisc
*usbmisc
;
528 usbmisc
= dev_get_drvdata(data
->dev
);
529 if (!usbmisc
->ops
->post
)
531 return usbmisc
->ops
->post(data
);
533 EXPORT_SYMBOL_GPL(imx_usbmisc_init_post
);
535 int imx_usbmisc_set_wakeup(struct imx_usbmisc_data
*data
, bool enabled
)
537 struct imx_usbmisc
*usbmisc
;
542 usbmisc
= dev_get_drvdata(data
->dev
);
543 if (!usbmisc
->ops
->set_wakeup
)
545 return usbmisc
->ops
->set_wakeup(data
, enabled
);
547 EXPORT_SYMBOL_GPL(imx_usbmisc_set_wakeup
);
549 static const struct of_device_id usbmisc_imx_dt_ids
[] = {
551 .compatible
= "fsl,imx25-usbmisc",
552 .data
= &imx25_usbmisc_ops
,
555 .compatible
= "fsl,imx35-usbmisc",
556 .data
= &imx25_usbmisc_ops
,
559 .compatible
= "fsl,imx27-usbmisc",
560 .data
= &imx27_usbmisc_ops
,
563 .compatible
= "fsl,imx51-usbmisc",
564 .data
= &imx51_usbmisc_ops
,
567 .compatible
= "fsl,imx53-usbmisc",
568 .data
= &imx53_usbmisc_ops
,
571 .compatible
= "fsl,imx6q-usbmisc",
572 .data
= &imx6q_usbmisc_ops
,
575 .compatible
= "fsl,vf610-usbmisc",
576 .data
= &vf610_usbmisc_ops
,
579 .compatible
= "fsl,imx6sx-usbmisc",
580 .data
= &imx6sx_usbmisc_ops
,
583 .compatible
= "fsl,imx6ul-usbmisc",
584 .data
= &imx6sx_usbmisc_ops
,
587 .compatible
= "fsl,imx7d-usbmisc",
588 .data
= &imx7d_usbmisc_ops
,
592 MODULE_DEVICE_TABLE(of
, usbmisc_imx_dt_ids
);
594 static int usbmisc_imx_probe(struct platform_device
*pdev
)
596 struct resource
*res
;
597 struct imx_usbmisc
*data
;
598 const struct of_device_id
*of_id
;
600 of_id
= of_match_device(usbmisc_imx_dt_ids
, &pdev
->dev
);
604 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
608 spin_lock_init(&data
->lock
);
610 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
611 data
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
612 if (IS_ERR(data
->base
))
613 return PTR_ERR(data
->base
);
615 data
->ops
= (const struct usbmisc_ops
*)of_id
->data
;
616 platform_set_drvdata(pdev
, data
);
621 static int usbmisc_imx_remove(struct platform_device
*pdev
)
626 static struct platform_driver usbmisc_imx_driver
= {
627 .probe
= usbmisc_imx_probe
,
628 .remove
= usbmisc_imx_remove
,
630 .name
= "usbmisc_imx",
631 .of_match_table
= usbmisc_imx_dt_ids
,
635 module_platform_driver(usbmisc_imx_driver
);
637 MODULE_ALIAS("platform:usbmisc-imx");
638 MODULE_LICENSE("GPL v2");
639 MODULE_DESCRIPTION("driver for imx usb non-core registers");
640 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");