1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 MediaTek Inc.
6 * Min Guo <min.guo@mediatek.com>
7 * Yonglong Wu <yonglong.wu@mediatek.com>
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/usb/role.h>
16 #include <linux/usb/usb_phy_generic.h>
17 #include "musb_core.h"
20 #define USB_L1INTS 0x00a0
21 #define USB_L1INTM 0x00a4
22 #define MTK_MUSB_TXFUNCADDR 0x0480
24 /* MediaTek controller toggle enable and status reg */
25 #define MUSB_RXTOG 0x80
26 #define MUSB_RXTOGEN 0x82
27 #define MUSB_TXTOG 0x84
28 #define MUSB_TXTOGEN 0x86
29 #define MTK_TOGGLE_EN GENMASK(15, 0)
31 #define TX_INT_STATUS BIT(0)
32 #define RX_INT_STATUS BIT(1)
33 #define USBCOM_INT_STATUS BIT(2)
34 #define DMA_INT_STATUS BIT(3)
36 #define DMA_INTR_STATUS_MSK GENMASK(7, 0)
37 #define DMA_INTR_UNMASK_SET_MSK GENMASK(31, 24)
42 struct platform_device
*musb_pdev
;
43 struct platform_device
*usb_phy
;
45 struct usb_phy
*xceiv
;
46 enum phy_mode phy_mode
;
51 struct usb_role_switch
*role_sw
;
54 static int mtk_musb_clks_get(struct mtk_glue
*glue
)
56 struct device
*dev
= glue
->dev
;
58 glue
->main
= devm_clk_get(dev
, "main");
59 if (IS_ERR(glue
->main
)) {
60 dev_err(dev
, "fail to get main clock\n");
61 return PTR_ERR(glue
->main
);
64 glue
->mcu
= devm_clk_get(dev
, "mcu");
65 if (IS_ERR(glue
->mcu
)) {
66 dev_err(dev
, "fail to get mcu clock\n");
67 return PTR_ERR(glue
->mcu
);
70 glue
->univpll
= devm_clk_get(dev
, "univpll");
71 if (IS_ERR(glue
->univpll
)) {
72 dev_err(dev
, "fail to get univpll clock\n");
73 return PTR_ERR(glue
->univpll
);
79 static int mtk_musb_clks_enable(struct mtk_glue
*glue
)
83 ret
= clk_prepare_enable(glue
->main
);
85 dev_err(glue
->dev
, "failed to enable main clock\n");
89 ret
= clk_prepare_enable(glue
->mcu
);
91 dev_err(glue
->dev
, "failed to enable mcu clock\n");
95 ret
= clk_prepare_enable(glue
->univpll
);
97 dev_err(glue
->dev
, "failed to enable univpll clock\n");
104 clk_disable_unprepare(glue
->mcu
);
106 clk_disable_unprepare(glue
->main
);
111 static void mtk_musb_clks_disable(struct mtk_glue
*glue
)
113 clk_disable_unprepare(glue
->univpll
);
114 clk_disable_unprepare(glue
->mcu
);
115 clk_disable_unprepare(glue
->main
);
118 static int mtk_otg_switch_set(struct mtk_glue
*glue
, enum usb_role role
)
120 struct musb
*musb
= glue
->musb
;
121 u8 devctl
= readb(musb
->mregs
+ MUSB_DEVCTL
);
122 enum usb_role new_role
;
124 if (role
== glue
->role
)
129 musb
->xceiv
->otg
->state
= OTG_STATE_A_WAIT_VRISE
;
130 glue
->phy_mode
= PHY_MODE_USB_HOST
;
131 new_role
= USB_ROLE_HOST
;
132 if (glue
->role
== USB_ROLE_NONE
)
133 phy_power_on(glue
->phy
);
135 devctl
|= MUSB_DEVCTL_SESSION
;
136 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
139 case USB_ROLE_DEVICE
:
140 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
141 glue
->phy_mode
= PHY_MODE_USB_DEVICE
;
142 new_role
= USB_ROLE_DEVICE
;
143 devctl
&= ~MUSB_DEVCTL_SESSION
;
144 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
145 if (glue
->role
== USB_ROLE_NONE
)
146 phy_power_on(glue
->phy
);
151 glue
->phy_mode
= PHY_MODE_USB_OTG
;
152 new_role
= USB_ROLE_NONE
;
153 devctl
&= ~MUSB_DEVCTL_SESSION
;
154 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
155 if (glue
->role
!= USB_ROLE_NONE
)
156 phy_power_off(glue
->phy
);
160 dev_err(glue
->dev
, "Invalid State\n");
164 glue
->role
= new_role
;
165 phy_set_mode(glue
->phy
, glue
->phy_mode
);
170 static int musb_usb_role_sx_set(struct usb_role_switch
*sw
, enum usb_role role
)
172 return mtk_otg_switch_set(usb_role_switch_get_drvdata(sw
), role
);
175 static enum usb_role
musb_usb_role_sx_get(struct usb_role_switch
*sw
)
177 struct mtk_glue
*glue
= usb_role_switch_get_drvdata(sw
);
182 static int mtk_otg_switch_init(struct mtk_glue
*glue
)
184 struct usb_role_switch_desc role_sx_desc
= { 0 };
186 role_sx_desc
.set
= musb_usb_role_sx_set
;
187 role_sx_desc
.get
= musb_usb_role_sx_get
;
188 role_sx_desc
.fwnode
= dev_fwnode(glue
->dev
);
189 role_sx_desc
.driver_data
= glue
;
190 glue
->role_sw
= usb_role_switch_register(glue
->dev
, &role_sx_desc
);
192 return PTR_ERR_OR_ZERO(glue
->role_sw
);
195 static void mtk_otg_switch_exit(struct mtk_glue
*glue
)
197 return usb_role_switch_unregister(glue
->role_sw
);
200 static irqreturn_t
generic_interrupt(int irq
, void *__hci
)
203 irqreturn_t retval
= IRQ_NONE
;
204 struct musb
*musb
= __hci
;
206 spin_lock_irqsave(&musb
->lock
, flags
);
207 musb
->int_usb
= musb_clearb(musb
->mregs
, MUSB_INTRUSB
);
208 musb
->int_rx
= musb_clearw(musb
->mregs
, MUSB_INTRRX
);
209 musb
->int_tx
= musb_clearw(musb
->mregs
, MUSB_INTRTX
);
211 if ((musb
->int_usb
& MUSB_INTR_RESET
) && !is_host_active(musb
)) {
212 /* ep0 FADDR must be 0 when (re)entering peripheral mode */
213 musb_ep_select(musb
->mregs
, 0);
214 musb_writeb(musb
->mregs
, MUSB_FADDR
, 0);
217 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
)
218 retval
= musb_interrupt(musb
);
220 spin_unlock_irqrestore(&musb
->lock
, flags
);
225 static irqreturn_t
mtk_musb_interrupt(int irq
, void *dev_id
)
227 irqreturn_t retval
= IRQ_NONE
;
228 struct musb
*musb
= (struct musb
*)dev_id
;
231 l1_ints
= musb_readl(musb
->mregs
, USB_L1INTS
) &
232 musb_readl(musb
->mregs
, USB_L1INTM
);
234 if (l1_ints
& (TX_INT_STATUS
| RX_INT_STATUS
| USBCOM_INT_STATUS
))
235 retval
= generic_interrupt(irq
, musb
);
237 #if defined(CONFIG_USB_INVENTRA_DMA)
238 if (l1_ints
& DMA_INT_STATUS
)
239 retval
= dma_controller_irq(irq
, musb
->dma_controller
);
244 static u32
mtk_musb_busctl_offset(u8 epnum
, u16 offset
)
246 return MTK_MUSB_TXFUNCADDR
+ offset
+ 8 * epnum
;
249 static u8
mtk_musb_clearb(void __iomem
*addr
, unsigned int offset
)
254 data
= musb_readb(addr
, offset
);
255 musb_writeb(addr
, offset
, data
);
259 static u16
mtk_musb_clearw(void __iomem
*addr
, unsigned int offset
)
264 data
= musb_readw(addr
, offset
);
265 musb_writew(addr
, offset
, data
);
269 static int mtk_musb_set_mode(struct musb
*musb
, u8 mode
)
271 struct device
*dev
= musb
->controller
;
272 struct mtk_glue
*glue
= dev_get_drvdata(dev
->parent
);
273 enum phy_mode new_mode
;
274 enum usb_role new_role
;
278 new_mode
= PHY_MODE_USB_HOST
;
279 new_role
= USB_ROLE_HOST
;
281 case MUSB_PERIPHERAL
:
282 new_mode
= PHY_MODE_USB_DEVICE
;
283 new_role
= USB_ROLE_DEVICE
;
286 new_mode
= PHY_MODE_USB_OTG
;
287 new_role
= USB_ROLE_NONE
;
290 dev_err(glue
->dev
, "Invalid mode request\n");
294 if (glue
->phy_mode
== new_mode
)
297 if (musb
->port_mode
!= MUSB_OTG
) {
298 dev_err(glue
->dev
, "Does not support changing modes\n");
302 mtk_otg_switch_set(glue
, new_role
);
306 static int mtk_musb_init(struct musb
*musb
)
308 struct device
*dev
= musb
->controller
;
309 struct mtk_glue
*glue
= dev_get_drvdata(dev
->parent
);
313 musb
->phy
= glue
->phy
;
314 musb
->xceiv
= glue
->xceiv
;
315 musb
->is_host
= false;
316 musb
->isr
= mtk_musb_interrupt
;
318 /* Set TX/RX toggle enable */
319 musb_writew(musb
->mregs
, MUSB_TXTOGEN
, MTK_TOGGLE_EN
);
320 musb_writew(musb
->mregs
, MUSB_RXTOGEN
, MTK_TOGGLE_EN
);
322 if (musb
->port_mode
== MUSB_OTG
) {
323 ret
= mtk_otg_switch_init(glue
);
328 ret
= phy_init(glue
->phy
);
332 ret
= phy_power_on(glue
->phy
);
334 goto err_phy_power_on
;
336 phy_set_mode(glue
->phy
, glue
->phy_mode
);
338 #if defined(CONFIG_USB_INVENTRA_DMA)
339 musb_writel(musb
->mregs
, MUSB_HSDMA_INTR
,
340 DMA_INTR_STATUS_MSK
| DMA_INTR_UNMASK_SET_MSK
);
342 musb_writel(musb
->mregs
, USB_L1INTM
, TX_INT_STATUS
| RX_INT_STATUS
|
343 USBCOM_INT_STATUS
| DMA_INT_STATUS
);
349 mtk_otg_switch_exit(glue
);
353 static u16
mtk_musb_get_toggle(struct musb_qh
*qh
, int is_out
)
355 struct musb
*musb
= qh
->hw_ep
->musb
;
356 u8 epnum
= qh
->hw_ep
->epnum
;
359 toggle
= musb_readw(musb
->mregs
, is_out
? MUSB_TXTOG
: MUSB_RXTOG
);
360 return toggle
& (1 << epnum
);
363 static u16
mtk_musb_set_toggle(struct musb_qh
*qh
, int is_out
, struct urb
*urb
)
365 struct musb
*musb
= qh
->hw_ep
->musb
;
366 u8 epnum
= qh
->hw_ep
->epnum
;
369 toggle
= usb_gettoggle(urb
->dev
, qh
->epnum
, is_out
);
372 value
= musb_readw(musb
->mregs
, MUSB_TXTOG
);
373 value
|= toggle
<< epnum
;
374 musb_writew(musb
->mregs
, MUSB_TXTOG
, value
);
376 value
= musb_readw(musb
->mregs
, MUSB_RXTOG
);
377 value
|= toggle
<< epnum
;
378 musb_writew(musb
->mregs
, MUSB_RXTOG
, value
);
384 static int mtk_musb_exit(struct musb
*musb
)
386 struct device
*dev
= musb
->controller
;
387 struct mtk_glue
*glue
= dev_get_drvdata(dev
->parent
);
389 mtk_otg_switch_exit(glue
);
390 phy_power_off(glue
->phy
);
392 mtk_musb_clks_disable(glue
);
394 pm_runtime_put_sync(dev
);
395 pm_runtime_disable(dev
);
399 static const struct musb_platform_ops mtk_musb_ops
= {
400 .quirks
= MUSB_DMA_INVENTRA
,
401 .init
= mtk_musb_init
,
402 .get_toggle
= mtk_musb_get_toggle
,
403 .set_toggle
= mtk_musb_set_toggle
,
404 .exit
= mtk_musb_exit
,
405 #ifdef CONFIG_USB_INVENTRA_DMA
406 .dma_init
= musbhs_dma_controller_create_noirq
,
407 .dma_exit
= musbhs_dma_controller_destroy
,
409 .clearb
= mtk_musb_clearb
,
410 .clearw
= mtk_musb_clearw
,
411 .busctl_offset
= mtk_musb_busctl_offset
,
412 .set_mode
= mtk_musb_set_mode
,
415 #define MTK_MUSB_MAX_EP_NUM 8
416 #define MTK_MUSB_RAM_BITS 11
418 static struct musb_fifo_cfg mtk_musb_mode_cfg
[] = {
419 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, },
420 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, },
421 { .hw_ep_num
= 2, .style
= FIFO_TX
, .maxpacket
= 512, },
422 { .hw_ep_num
= 2, .style
= FIFO_RX
, .maxpacket
= 512, },
423 { .hw_ep_num
= 3, .style
= FIFO_TX
, .maxpacket
= 512, },
424 { .hw_ep_num
= 3, .style
= FIFO_RX
, .maxpacket
= 512, },
425 { .hw_ep_num
= 4, .style
= FIFO_TX
, .maxpacket
= 512, },
426 { .hw_ep_num
= 4, .style
= FIFO_RX
, .maxpacket
= 512, },
427 { .hw_ep_num
= 5, .style
= FIFO_TX
, .maxpacket
= 512, },
428 { .hw_ep_num
= 5, .style
= FIFO_RX
, .maxpacket
= 512, },
429 { .hw_ep_num
= 6, .style
= FIFO_TX
, .maxpacket
= 1024, },
430 { .hw_ep_num
= 6, .style
= FIFO_RX
, .maxpacket
= 1024, },
431 { .hw_ep_num
= 7, .style
= FIFO_TX
, .maxpacket
= 512, },
432 { .hw_ep_num
= 7, .style
= FIFO_RX
, .maxpacket
= 64, },
435 static const struct musb_hdrc_config mtk_musb_hdrc_config
= {
436 .fifo_cfg
= mtk_musb_mode_cfg
,
437 .fifo_cfg_size
= ARRAY_SIZE(mtk_musb_mode_cfg
),
440 .num_eps
= MTK_MUSB_MAX_EP_NUM
,
441 .ram_bits
= MTK_MUSB_RAM_BITS
,
444 static const struct platform_device_info mtk_dev_info
= {
446 .id
= PLATFORM_DEVID_AUTO
,
447 .dma_mask
= DMA_BIT_MASK(32),
450 static int mtk_musb_probe(struct platform_device
*pdev
)
452 struct musb_hdrc_platform_data
*pdata
;
453 struct mtk_glue
*glue
;
454 struct platform_device_info pinfo
;
455 struct device
*dev
= &pdev
->dev
;
456 struct device_node
*np
= dev
->of_node
;
459 glue
= devm_kzalloc(dev
, sizeof(*glue
), GFP_KERNEL
);
464 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
468 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
470 dev_err(dev
, "failed to create child devices at %p\n", np
);
474 ret
= mtk_musb_clks_get(glue
);
478 pdata
->config
= &mtk_musb_hdrc_config
;
479 pdata
->platform_ops
= &mtk_musb_ops
;
480 pdata
->mode
= usb_get_dr_mode(dev
);
482 if (IS_ENABLED(CONFIG_USB_MUSB_HOST
))
483 pdata
->mode
= USB_DR_MODE_HOST
;
484 else if (IS_ENABLED(CONFIG_USB_MUSB_GADGET
))
485 pdata
->mode
= USB_DR_MODE_PERIPHERAL
;
487 switch (pdata
->mode
) {
488 case USB_DR_MODE_HOST
:
489 glue
->phy_mode
= PHY_MODE_USB_HOST
;
490 glue
->role
= USB_ROLE_HOST
;
492 case USB_DR_MODE_PERIPHERAL
:
493 glue
->phy_mode
= PHY_MODE_USB_DEVICE
;
494 glue
->role
= USB_ROLE_DEVICE
;
496 case USB_DR_MODE_OTG
:
497 glue
->phy_mode
= PHY_MODE_USB_OTG
;
498 glue
->role
= USB_ROLE_NONE
;
501 dev_err(&pdev
->dev
, "Error 'dr_mode' property\n");
505 glue
->phy
= devm_of_phy_get_by_index(dev
, np
, 0);
506 if (IS_ERR(glue
->phy
)) {
507 dev_err(dev
, "fail to getting phy %ld\n",
509 return PTR_ERR(glue
->phy
);
512 glue
->usb_phy
= usb_phy_generic_register();
513 if (IS_ERR(glue
->usb_phy
)) {
514 dev_err(dev
, "fail to registering usb-phy %ld\n",
515 PTR_ERR(glue
->usb_phy
));
516 return PTR_ERR(glue
->usb_phy
);
519 glue
->xceiv
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
520 if (IS_ERR(glue
->xceiv
)) {
521 dev_err(dev
, "fail to getting usb-phy %d\n", ret
);
522 ret
= PTR_ERR(glue
->xceiv
);
523 goto err_unregister_usb_phy
;
526 platform_set_drvdata(pdev
, glue
);
527 pm_runtime_enable(dev
);
528 pm_runtime_get_sync(dev
);
530 ret
= mtk_musb_clks_enable(glue
);
534 pinfo
= mtk_dev_info
;
536 pinfo
.res
= pdev
->resource
;
537 pinfo
.num_res
= pdev
->num_resources
;
539 pinfo
.size_data
= sizeof(*pdata
);
541 glue
->musb_pdev
= platform_device_register_full(&pinfo
);
542 if (IS_ERR(glue
->musb_pdev
)) {
543 ret
= PTR_ERR(glue
->musb_pdev
);
544 dev_err(dev
, "failed to register musb device: %d\n", ret
);
545 goto err_device_register
;
551 mtk_musb_clks_disable(glue
);
553 pm_runtime_put_sync(dev
);
554 pm_runtime_disable(dev
);
555 err_unregister_usb_phy
:
556 usb_phy_generic_unregister(glue
->usb_phy
);
560 static int mtk_musb_remove(struct platform_device
*pdev
)
562 struct mtk_glue
*glue
= platform_get_drvdata(pdev
);
563 struct platform_device
*usb_phy
= glue
->usb_phy
;
565 platform_device_unregister(glue
->musb_pdev
);
566 usb_phy_generic_unregister(usb_phy
);
572 static const struct of_device_id mtk_musb_match
[] = {
573 {.compatible
= "mediatek,mtk-musb",},
576 MODULE_DEVICE_TABLE(of
, mtk_musb_match
);
579 static struct platform_driver mtk_musb_driver
= {
580 .probe
= mtk_musb_probe
,
581 .remove
= mtk_musb_remove
,
584 .of_match_table
= of_match_ptr(mtk_musb_match
),
588 module_platform_driver(mtk_musb_driver
);
590 MODULE_DESCRIPTION("MediaTek MUSB Glue Layer");
591 MODULE_AUTHOR("Min Guo <min.guo@mediatek.com>");
592 MODULE_LICENSE("GPL v2");