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 musb_usb_role_sx_set(struct device
*dev
, enum usb_role role
)
120 struct mtk_glue
*glue
= dev_get_drvdata(dev
);
121 struct musb
*musb
= glue
->musb
;
122 u8 devctl
= readb(musb
->mregs
+ MUSB_DEVCTL
);
123 enum usb_role new_role
;
125 if (role
== glue
->role
)
130 musb
->xceiv
->otg
->state
= OTG_STATE_A_WAIT_VRISE
;
131 glue
->phy_mode
= PHY_MODE_USB_HOST
;
132 new_role
= USB_ROLE_HOST
;
133 if (glue
->role
== USB_ROLE_NONE
)
134 phy_power_on(glue
->phy
);
136 devctl
|= MUSB_DEVCTL_SESSION
;
137 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
140 case USB_ROLE_DEVICE
:
141 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
142 glue
->phy_mode
= PHY_MODE_USB_DEVICE
;
143 new_role
= USB_ROLE_DEVICE
;
144 devctl
&= ~MUSB_DEVCTL_SESSION
;
145 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
146 if (glue
->role
== USB_ROLE_NONE
)
147 phy_power_on(glue
->phy
);
152 glue
->phy_mode
= PHY_MODE_USB_OTG
;
153 new_role
= USB_ROLE_NONE
;
154 devctl
&= ~MUSB_DEVCTL_SESSION
;
155 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
156 if (glue
->role
!= USB_ROLE_NONE
)
157 phy_power_off(glue
->phy
);
161 dev_err(glue
->dev
, "Invalid State\n");
165 glue
->role
= new_role
;
166 phy_set_mode(glue
->phy
, glue
->phy_mode
);
171 static enum usb_role
musb_usb_role_sx_get(struct device
*dev
)
173 struct mtk_glue
*glue
= dev_get_drvdata(dev
);
178 static int mtk_otg_switch_init(struct mtk_glue
*glue
)
180 struct usb_role_switch_desc role_sx_desc
= { 0 };
182 role_sx_desc
.set
= musb_usb_role_sx_set
;
183 role_sx_desc
.get
= musb_usb_role_sx_get
;
184 role_sx_desc
.fwnode
= dev_fwnode(glue
->dev
);
185 glue
->role_sw
= usb_role_switch_register(glue
->dev
, &role_sx_desc
);
187 return PTR_ERR_OR_ZERO(glue
->role_sw
);
190 static void mtk_otg_switch_exit(struct mtk_glue
*glue
)
192 return usb_role_switch_unregister(glue
->role_sw
);
195 static irqreturn_t
generic_interrupt(int irq
, void *__hci
)
198 irqreturn_t retval
= IRQ_NONE
;
199 struct musb
*musb
= __hci
;
201 spin_lock_irqsave(&musb
->lock
, flags
);
202 musb
->int_usb
= musb_clearb(musb
->mregs
, MUSB_INTRUSB
);
203 musb
->int_rx
= musb_clearw(musb
->mregs
, MUSB_INTRRX
);
204 musb
->int_tx
= musb_clearw(musb
->mregs
, MUSB_INTRTX
);
206 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
)
207 retval
= musb_interrupt(musb
);
209 spin_unlock_irqrestore(&musb
->lock
, flags
);
214 static irqreturn_t
mtk_musb_interrupt(int irq
, void *dev_id
)
216 irqreturn_t retval
= IRQ_NONE
;
217 struct musb
*musb
= (struct musb
*)dev_id
;
220 l1_ints
= musb_readl(musb
->mregs
, USB_L1INTS
) &
221 musb_readl(musb
->mregs
, USB_L1INTM
);
223 if (l1_ints
& (TX_INT_STATUS
| RX_INT_STATUS
| USBCOM_INT_STATUS
))
224 retval
= generic_interrupt(irq
, musb
);
226 #if defined(CONFIG_USB_INVENTRA_DMA)
227 if (l1_ints
& DMA_INT_STATUS
)
228 retval
= dma_controller_irq(irq
, musb
->dma_controller
);
233 static u32
mtk_musb_busctl_offset(u8 epnum
, u16 offset
)
235 return MTK_MUSB_TXFUNCADDR
+ offset
+ 8 * epnum
;
238 static u8
mtk_musb_clearb(void __iomem
*addr
, unsigned int offset
)
243 data
= musb_readb(addr
, offset
);
244 musb_writeb(addr
, offset
, data
);
248 static u16
mtk_musb_clearw(void __iomem
*addr
, unsigned int offset
)
253 data
= musb_readw(addr
, offset
);
254 musb_writew(addr
, offset
, data
);
258 static int mtk_musb_set_mode(struct musb
*musb
, u8 mode
)
260 struct device
*dev
= musb
->controller
;
261 struct mtk_glue
*glue
= dev_get_drvdata(dev
->parent
);
262 enum phy_mode new_mode
;
263 enum usb_role new_role
;
267 new_mode
= PHY_MODE_USB_HOST
;
268 new_role
= USB_ROLE_HOST
;
270 case MUSB_PERIPHERAL
:
271 new_mode
= PHY_MODE_USB_DEVICE
;
272 new_role
= USB_ROLE_DEVICE
;
275 new_mode
= PHY_MODE_USB_OTG
;
276 new_role
= USB_ROLE_NONE
;
279 dev_err(glue
->dev
, "Invalid mode request\n");
283 if (glue
->phy_mode
== new_mode
)
286 if (musb
->port_mode
!= MUSB_OTG
) {
287 dev_err(glue
->dev
, "Does not support changing modes\n");
291 glue
->role
= new_role
;
292 musb_usb_role_sx_set(dev
, glue
->role
);
296 static int mtk_musb_init(struct musb
*musb
)
298 struct device
*dev
= musb
->controller
;
299 struct mtk_glue
*glue
= dev_get_drvdata(dev
->parent
);
303 musb
->phy
= glue
->phy
;
304 musb
->xceiv
= glue
->xceiv
;
305 musb
->is_host
= false;
306 musb
->isr
= mtk_musb_interrupt
;
308 /* Set TX/RX toggle enable */
309 musb_writew(musb
->mregs
, MUSB_TXTOGEN
, MTK_TOGGLE_EN
);
310 musb_writew(musb
->mregs
, MUSB_RXTOGEN
, MTK_TOGGLE_EN
);
312 if (musb
->port_mode
== MUSB_OTG
) {
313 ret
= mtk_otg_switch_init(glue
);
318 ret
= phy_init(glue
->phy
);
322 ret
= phy_power_on(glue
->phy
);
324 goto err_phy_power_on
;
326 phy_set_mode(glue
->phy
, glue
->phy_mode
);
328 #if defined(CONFIG_USB_INVENTRA_DMA)
329 musb_writel(musb
->mregs
, MUSB_HSDMA_INTR
,
330 DMA_INTR_STATUS_MSK
| DMA_INTR_UNMASK_SET_MSK
);
332 musb_writel(musb
->mregs
, USB_L1INTM
, TX_INT_STATUS
| RX_INT_STATUS
|
333 USBCOM_INT_STATUS
| DMA_INT_STATUS
);
339 mtk_otg_switch_exit(glue
);
343 static u16
mtk_musb_get_toggle(struct musb_qh
*qh
, int is_out
)
345 struct musb
*musb
= qh
->hw_ep
->musb
;
346 u8 epnum
= qh
->hw_ep
->epnum
;
349 toggle
= musb_readw(musb
->mregs
, is_out
? MUSB_TXTOG
: MUSB_RXTOG
);
350 return toggle
& (1 << epnum
);
353 static u16
mtk_musb_set_toggle(struct musb_qh
*qh
, int is_out
, struct urb
*urb
)
355 struct musb
*musb
= qh
->hw_ep
->musb
;
356 u8 epnum
= qh
->hw_ep
->epnum
;
359 toggle
= usb_gettoggle(urb
->dev
, qh
->epnum
, is_out
);
362 value
= musb_readw(musb
->mregs
, MUSB_TXTOG
);
363 value
|= toggle
<< epnum
;
364 musb_writew(musb
->mregs
, MUSB_TXTOG
, value
);
366 value
= musb_readw(musb
->mregs
, MUSB_RXTOG
);
367 value
|= toggle
<< epnum
;
368 musb_writew(musb
->mregs
, MUSB_RXTOG
, value
);
374 static int mtk_musb_exit(struct musb
*musb
)
376 struct device
*dev
= musb
->controller
;
377 struct mtk_glue
*glue
= dev_get_drvdata(dev
->parent
);
379 mtk_otg_switch_exit(glue
);
380 phy_power_off(glue
->phy
);
382 mtk_musb_clks_disable(glue
);
384 pm_runtime_put_sync(dev
);
385 pm_runtime_disable(dev
);
389 static const struct musb_platform_ops mtk_musb_ops
= {
390 .quirks
= MUSB_DMA_INVENTRA
,
391 .init
= mtk_musb_init
,
392 .get_toggle
= mtk_musb_get_toggle
,
393 .set_toggle
= mtk_musb_set_toggle
,
394 .exit
= mtk_musb_exit
,
395 #ifdef CONFIG_USB_INVENTRA_DMA
396 .dma_init
= musbhs_dma_controller_create_noirq
,
397 .dma_exit
= musbhs_dma_controller_destroy
,
399 .clearb
= mtk_musb_clearb
,
400 .clearw
= mtk_musb_clearw
,
401 .busctl_offset
= mtk_musb_busctl_offset
,
402 .set_mode
= mtk_musb_set_mode
,
405 #define MTK_MUSB_MAX_EP_NUM 8
406 #define MTK_MUSB_RAM_BITS 11
408 static struct musb_fifo_cfg mtk_musb_mode_cfg
[] = {
409 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, },
410 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, },
411 { .hw_ep_num
= 2, .style
= FIFO_TX
, .maxpacket
= 512, },
412 { .hw_ep_num
= 2, .style
= FIFO_RX
, .maxpacket
= 512, },
413 { .hw_ep_num
= 3, .style
= FIFO_TX
, .maxpacket
= 512, },
414 { .hw_ep_num
= 3, .style
= FIFO_RX
, .maxpacket
= 512, },
415 { .hw_ep_num
= 4, .style
= FIFO_TX
, .maxpacket
= 512, },
416 { .hw_ep_num
= 4, .style
= FIFO_RX
, .maxpacket
= 512, },
417 { .hw_ep_num
= 5, .style
= FIFO_TX
, .maxpacket
= 512, },
418 { .hw_ep_num
= 5, .style
= FIFO_RX
, .maxpacket
= 512, },
419 { .hw_ep_num
= 6, .style
= FIFO_TX
, .maxpacket
= 1024, },
420 { .hw_ep_num
= 6, .style
= FIFO_RX
, .maxpacket
= 1024, },
421 { .hw_ep_num
= 7, .style
= FIFO_TX
, .maxpacket
= 512, },
422 { .hw_ep_num
= 7, .style
= FIFO_RX
, .maxpacket
= 64, },
425 static const struct musb_hdrc_config mtk_musb_hdrc_config
= {
426 .fifo_cfg
= mtk_musb_mode_cfg
,
427 .fifo_cfg_size
= ARRAY_SIZE(mtk_musb_mode_cfg
),
430 .num_eps
= MTK_MUSB_MAX_EP_NUM
,
431 .ram_bits
= MTK_MUSB_RAM_BITS
,
434 static const struct platform_device_info mtk_dev_info
= {
436 .id
= PLATFORM_DEVID_AUTO
,
437 .dma_mask
= DMA_BIT_MASK(32),
440 static int mtk_musb_probe(struct platform_device
*pdev
)
442 struct musb_hdrc_platform_data
*pdata
;
443 struct mtk_glue
*glue
;
444 struct platform_device_info pinfo
;
445 struct device
*dev
= &pdev
->dev
;
446 struct device_node
*np
= dev
->of_node
;
449 glue
= devm_kzalloc(dev
, sizeof(*glue
), GFP_KERNEL
);
454 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
458 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
460 dev_err(dev
, "failed to create child devices at %p\n", np
);
464 ret
= mtk_musb_clks_get(glue
);
468 pdata
->config
= &mtk_musb_hdrc_config
;
469 pdata
->platform_ops
= &mtk_musb_ops
;
470 pdata
->mode
= usb_get_dr_mode(dev
);
472 if (IS_ENABLED(CONFIG_USB_MUSB_HOST
))
473 pdata
->mode
= USB_DR_MODE_HOST
;
474 else if (IS_ENABLED(CONFIG_USB_MUSB_GADGET
))
475 pdata
->mode
= USB_DR_MODE_PERIPHERAL
;
477 switch (pdata
->mode
) {
478 case USB_DR_MODE_HOST
:
479 glue
->phy_mode
= PHY_MODE_USB_HOST
;
480 glue
->role
= USB_ROLE_HOST
;
482 case USB_DR_MODE_PERIPHERAL
:
483 glue
->phy_mode
= PHY_MODE_USB_DEVICE
;
484 glue
->role
= USB_ROLE_DEVICE
;
486 case USB_DR_MODE_OTG
:
487 glue
->phy_mode
= PHY_MODE_USB_OTG
;
488 glue
->role
= USB_ROLE_NONE
;
491 dev_err(&pdev
->dev
, "Error 'dr_mode' property\n");
495 glue
->phy
= devm_of_phy_get_by_index(dev
, np
, 0);
496 if (IS_ERR(glue
->phy
)) {
497 dev_err(dev
, "fail to getting phy %ld\n",
499 return PTR_ERR(glue
->phy
);
502 glue
->usb_phy
= usb_phy_generic_register();
503 if (IS_ERR(glue
->usb_phy
)) {
504 dev_err(dev
, "fail to registering usb-phy %ld\n",
505 PTR_ERR(glue
->usb_phy
));
506 return PTR_ERR(glue
->usb_phy
);
509 glue
->xceiv
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
510 if (IS_ERR(glue
->xceiv
)) {
511 dev_err(dev
, "fail to getting usb-phy %d\n", ret
);
512 ret
= PTR_ERR(glue
->xceiv
);
513 goto err_unregister_usb_phy
;
516 platform_set_drvdata(pdev
, glue
);
517 pm_runtime_enable(dev
);
518 pm_runtime_get_sync(dev
);
520 ret
= mtk_musb_clks_enable(glue
);
524 pinfo
= mtk_dev_info
;
526 pinfo
.res
= pdev
->resource
;
527 pinfo
.num_res
= pdev
->num_resources
;
529 pinfo
.size_data
= sizeof(*pdata
);
531 glue
->musb_pdev
= platform_device_register_full(&pinfo
);
532 if (IS_ERR(glue
->musb_pdev
)) {
533 ret
= PTR_ERR(glue
->musb_pdev
);
534 dev_err(dev
, "failed to register musb device: %d\n", ret
);
535 goto err_device_register
;
541 mtk_musb_clks_disable(glue
);
543 pm_runtime_put_sync(dev
);
544 pm_runtime_disable(dev
);
545 err_unregister_usb_phy
:
546 usb_phy_generic_unregister(glue
->usb_phy
);
550 static int mtk_musb_remove(struct platform_device
*pdev
)
552 struct mtk_glue
*glue
= platform_get_drvdata(pdev
);
553 struct platform_device
*usb_phy
= glue
->usb_phy
;
555 platform_device_unregister(glue
->musb_pdev
);
556 usb_phy_generic_unregister(usb_phy
);
562 static const struct of_device_id mtk_musb_match
[] = {
563 {.compatible
= "mediatek,mtk-musb",},
566 MODULE_DEVICE_TABLE(of
, mtk_musb_match
);
569 static struct platform_driver mtk_musb_driver
= {
570 .probe
= mtk_musb_probe
,
571 .remove
= mtk_musb_remove
,
574 .of_match_table
= of_match_ptr(mtk_musb_match
),
578 module_platform_driver(mtk_musb_driver
);
580 MODULE_DESCRIPTION("MediaTek MUSB Glue Layer");
581 MODULE_AUTHOR("Min Guo <min.guo@mediatek.com>");
582 MODULE_LICENSE("GPL v2");