1 // SPDX-License-Identifier: GPL-2.0+
3 * Allwinner sun4i MUSB Glue Layer
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
11 #include <linux/clk.h>
12 #include <linux/err.h>
13 #include <linux/extcon.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/phy/phy-sun4i-usb.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
22 #include <linux/soc/sunxi/sunxi_sram.h>
23 #include <linux/usb/musb.h>
24 #include <linux/usb/of.h>
25 #include <linux/usb/usb_phy_generic.h>
26 #include <linux/workqueue.h>
27 #include "musb_core.h"
30 * Register offsets, note sunxi musb has a different layout then most
31 * musb implementations, we translate the layout in musb_readb & friends.
33 #define SUNXI_MUSB_POWER 0x0040
34 #define SUNXI_MUSB_DEVCTL 0x0041
35 #define SUNXI_MUSB_INDEX 0x0042
36 #define SUNXI_MUSB_VEND0 0x0043
37 #define SUNXI_MUSB_INTRTX 0x0044
38 #define SUNXI_MUSB_INTRRX 0x0046
39 #define SUNXI_MUSB_INTRTXE 0x0048
40 #define SUNXI_MUSB_INTRRXE 0x004a
41 #define SUNXI_MUSB_INTRUSB 0x004c
42 #define SUNXI_MUSB_INTRUSBE 0x0050
43 #define SUNXI_MUSB_FRAME 0x0054
44 #define SUNXI_MUSB_TXFIFOSZ 0x0090
45 #define SUNXI_MUSB_TXFIFOADD 0x0092
46 #define SUNXI_MUSB_RXFIFOSZ 0x0094
47 #define SUNXI_MUSB_RXFIFOADD 0x0096
48 #define SUNXI_MUSB_FADDR 0x0098
49 #define SUNXI_MUSB_TXFUNCADDR 0x0098
50 #define SUNXI_MUSB_TXHUBADDR 0x009a
51 #define SUNXI_MUSB_TXHUBPORT 0x009b
52 #define SUNXI_MUSB_RXFUNCADDR 0x009c
53 #define SUNXI_MUSB_RXHUBADDR 0x009e
54 #define SUNXI_MUSB_RXHUBPORT 0x009f
55 #define SUNXI_MUSB_CONFIGDATA 0x00c0
58 #define SUNXI_MUSB_VEND0_PIO_MODE 0
61 #define SUNXI_MUSB_FL_ENABLED 0
62 #define SUNXI_MUSB_FL_HOSTMODE 1
63 #define SUNXI_MUSB_FL_HOSTMODE_PEND 2
64 #define SUNXI_MUSB_FL_VBUS_ON 3
65 #define SUNXI_MUSB_FL_PHY_ON 4
66 #define SUNXI_MUSB_FL_HAS_SRAM 5
67 #define SUNXI_MUSB_FL_HAS_RESET 6
68 #define SUNXI_MUSB_FL_NO_CONFIGDATA 7
69 #define SUNXI_MUSB_FL_PHY_MODE_PEND 8
71 struct sunxi_musb_cfg {
72 const struct musb_hdrc_config *hdrc_config;
78 /* Our read/write methods need access and do not get passed in a musb ref :| */
79 static struct musb *sunxi_musb;
84 struct platform_device *musb_pdev;
86 struct reset_control *rst;
88 struct platform_device *usb_phy;
89 struct usb_phy *xceiv;
90 enum phy_mode phy_mode;
92 struct work_struct work;
93 struct extcon_dev *extcon;
94 struct notifier_block host_nb;
97 /* phy_power_on / off may sleep, so we use a workqueue */
98 static void sunxi_musb_work(struct work_struct *work)
100 struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
101 bool vbus_on, phy_on;
103 if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
106 if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
107 struct musb *musb = glue->musb;
111 spin_lock_irqsave(&musb->lock, flags);
113 devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
114 if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
115 set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
116 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
118 devctl |= MUSB_DEVCTL_SESSION;
120 clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
121 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
123 devctl &= ~MUSB_DEVCTL_SESSION;
125 writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
127 spin_unlock_irqrestore(&musb->lock, flags);
130 vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
131 phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
133 if (phy_on != vbus_on) {
135 phy_power_on(glue->phy);
136 set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
138 phy_power_off(glue->phy);
139 clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
143 if (test_and_clear_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags))
144 phy_set_mode(glue->phy, glue->phy_mode);
147 static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
149 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
152 set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
153 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
155 clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
158 schedule_work(&glue->work);
161 static void sunxi_musb_pre_root_reset_end(struct musb *musb)
163 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
165 sun4i_usb_phy_set_squelch_detect(glue->phy, false);
168 static void sunxi_musb_post_root_reset_end(struct musb *musb)
170 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
172 sun4i_usb_phy_set_squelch_detect(glue->phy, true);
175 static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
177 struct musb *musb = __hci;
180 spin_lock_irqsave(&musb->lock, flags);
182 musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
184 writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
186 if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
187 /* ep0 FADDR must be 0 when (re)entering peripheral mode */
188 musb_ep_select(musb->mregs, 0);
189 musb_writeb(musb->mregs, MUSB_FADDR, 0);
192 musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
194 writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
196 musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
198 writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
200 musb_interrupt(musb);
202 spin_unlock_irqrestore(&musb->lock, flags);
207 static int sunxi_musb_host_notifier(struct notifier_block *nb,
208 unsigned long event, void *ptr)
210 struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
213 set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
215 clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
217 set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
218 schedule_work(&glue->work);
223 static int sunxi_musb_init(struct musb *musb)
225 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
229 musb->phy = glue->phy;
230 musb->xceiv = glue->xceiv;
232 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags)) {
233 ret = sunxi_sram_claim(musb->controller->parent);
238 ret = clk_prepare_enable(glue->clk);
240 goto error_sram_release;
242 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
243 ret = reset_control_deassert(glue->rst);
245 goto error_clk_disable;
248 writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
250 /* Register notifier before calling phy_init() */
251 ret = devm_extcon_register_notifier(glue->dev, glue->extcon,
252 EXTCON_USB_HOST, &glue->host_nb);
254 goto error_reset_assert;
256 ret = phy_init(glue->phy);
258 goto error_reset_assert;
260 musb->isr = sunxi_musb_interrupt;
262 /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
263 pm_runtime_get(musb->controller);
268 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
269 reset_control_assert(glue->rst);
271 clk_disable_unprepare(glue->clk);
273 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
274 sunxi_sram_release(musb->controller->parent);
278 static int sunxi_musb_exit(struct musb *musb)
280 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
282 pm_runtime_put(musb->controller);
284 cancel_work_sync(&glue->work);
285 if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
286 phy_power_off(glue->phy);
290 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
291 reset_control_assert(glue->rst);
293 clk_disable_unprepare(glue->clk);
294 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
295 sunxi_sram_release(musb->controller->parent);
297 devm_usb_put_phy(glue->dev, glue->xceiv);
302 static void sunxi_musb_enable(struct musb *musb)
304 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
308 /* musb_core does not call us in a balanced manner */
309 if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
312 schedule_work(&glue->work);
315 static void sunxi_musb_disable(struct musb *musb)
317 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
319 clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
322 static struct dma_controller *
323 sunxi_musb_dma_controller_create(struct musb *musb, void __iomem *base)
328 static void sunxi_musb_dma_controller_destroy(struct dma_controller *c)
332 static int sunxi_musb_set_mode(struct musb *musb, u8 mode)
334 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
335 enum phy_mode new_mode;
339 new_mode = PHY_MODE_USB_HOST;
341 case MUSB_PERIPHERAL:
342 new_mode = PHY_MODE_USB_DEVICE;
345 new_mode = PHY_MODE_USB_OTG;
348 dev_err(musb->controller->parent,
349 "Error requested mode not supported by this kernel\n");
353 if (glue->phy_mode == new_mode)
356 if (musb->port_mode != MUSB_OTG) {
357 dev_err(musb->controller->parent,
358 "Error changing modes is only supported in dual role mode\n");
362 if (musb->port1_status & USB_PORT_STAT_ENABLE)
363 musb_root_disconnect(musb);
366 * phy_set_mode may sleep, and we're called with a spinlock held,
367 * so let sunxi_musb_work deal with it.
369 glue->phy_mode = new_mode;
370 set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
371 schedule_work(&glue->work);
376 static int sunxi_musb_recover(struct musb *musb)
378 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
381 * Schedule a phy_set_mode with the current glue->phy_mode value,
382 * this will force end the current session.
384 set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
385 schedule_work(&glue->work);
391 * sunxi musb register layout
392 * 0x00 - 0x17 fifo regs, 1 long per fifo
393 * 0x40 - 0x57 generic control regs (power - frame)
394 * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
395 * 0x90 - 0x97 fifo control regs (indexed)
396 * 0x98 - 0x9f multipoint / busctl regs (indexed)
397 * 0xc0 configdata reg
400 static u32 sunxi_musb_fifo_offset(u8 epnum)
405 static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
407 WARN_ONCE(offset != 0,
408 "sunxi_musb_ep_offset called with non 0 offset\n");
410 return 0x80; /* indexed, so ignore epnum */
413 static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
415 return SUNXI_MUSB_TXFUNCADDR + offset;
418 static u8 sunxi_musb_readb(void __iomem *addr, u32 offset)
420 struct sunxi_glue *glue;
422 if (addr == sunxi_musb->mregs) {
423 /* generic control or fifo control reg access */
426 return readb(addr + SUNXI_MUSB_FADDR);
428 return readb(addr + SUNXI_MUSB_POWER);
430 return readb(addr + SUNXI_MUSB_INTRUSB);
432 return readb(addr + SUNXI_MUSB_INTRUSBE);
434 return readb(addr + SUNXI_MUSB_INDEX);
436 return 0; /* No testmode on sunxi */
438 return readb(addr + SUNXI_MUSB_DEVCTL);
440 return readb(addr + SUNXI_MUSB_TXFIFOSZ);
442 return readb(addr + SUNXI_MUSB_RXFIFOSZ);
443 case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
444 glue = dev_get_drvdata(sunxi_musb->controller->parent);
445 /* A33 saves a reg, and we get to hardcode this */
446 if (test_bit(SUNXI_MUSB_FL_NO_CONFIGDATA,
450 return readb(addr + SUNXI_MUSB_CONFIGDATA);
451 case MUSB_ULPI_BUSCONTROL:
452 dev_warn(sunxi_musb->controller->parent,
453 "sunxi-musb does not have ULPI bus control register\n");
455 /* Offset for these is fixed by sunxi_musb_busctl_offset() */
456 case SUNXI_MUSB_TXFUNCADDR:
457 case SUNXI_MUSB_TXHUBADDR:
458 case SUNXI_MUSB_TXHUBPORT:
459 case SUNXI_MUSB_RXFUNCADDR:
460 case SUNXI_MUSB_RXHUBADDR:
461 case SUNXI_MUSB_RXHUBPORT:
462 /* multipoint / busctl reg access */
463 return readb(addr + offset);
465 dev_err(sunxi_musb->controller->parent,
466 "Error unknown readb offset %u\n", offset);
469 } else if (addr == (sunxi_musb->mregs + 0x80)) {
470 /* ep control reg access */
471 /* sunxi has a 2 byte hole before the txtype register */
472 if (offset >= MUSB_TXTYPE)
474 return readb(addr + offset);
477 dev_err(sunxi_musb->controller->parent,
478 "Error unknown readb at 0x%x bytes offset\n",
479 (int)(addr - sunxi_musb->mregs));
483 static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
485 if (addr == sunxi_musb->mregs) {
486 /* generic control or fifo control reg access */
489 return writeb(data, addr + SUNXI_MUSB_FADDR);
491 return writeb(data, addr + SUNXI_MUSB_POWER);
493 return writeb(data, addr + SUNXI_MUSB_INTRUSB);
495 return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
497 return writeb(data, addr + SUNXI_MUSB_INDEX);
500 dev_warn(sunxi_musb->controller->parent,
501 "sunxi-musb does not have testmode\n");
504 return writeb(data, addr + SUNXI_MUSB_DEVCTL);
506 return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
508 return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
509 case MUSB_ULPI_BUSCONTROL:
510 dev_warn(sunxi_musb->controller->parent,
511 "sunxi-musb does not have ULPI bus control register\n");
513 /* Offset for these is fixed by sunxi_musb_busctl_offset() */
514 case SUNXI_MUSB_TXFUNCADDR:
515 case SUNXI_MUSB_TXHUBADDR:
516 case SUNXI_MUSB_TXHUBPORT:
517 case SUNXI_MUSB_RXFUNCADDR:
518 case SUNXI_MUSB_RXHUBADDR:
519 case SUNXI_MUSB_RXHUBPORT:
520 /* multipoint / busctl reg access */
521 return writeb(data, addr + offset);
523 dev_err(sunxi_musb->controller->parent,
524 "Error unknown writeb offset %u\n", offset);
527 } else if (addr == (sunxi_musb->mregs + 0x80)) {
528 /* ep control reg access */
529 if (offset >= MUSB_TXTYPE)
531 return writeb(data, addr + offset);
534 dev_err(sunxi_musb->controller->parent,
535 "Error unknown writeb at 0x%x bytes offset\n",
536 (int)(addr - sunxi_musb->mregs));
539 static u16 sunxi_musb_readw(void __iomem *addr, u32 offset)
541 if (addr == sunxi_musb->mregs) {
542 /* generic control or fifo control reg access */
545 return readw(addr + SUNXI_MUSB_INTRTX);
547 return readw(addr + SUNXI_MUSB_INTRRX);
549 return readw(addr + SUNXI_MUSB_INTRTXE);
551 return readw(addr + SUNXI_MUSB_INTRRXE);
553 return readw(addr + SUNXI_MUSB_FRAME);
555 return readw(addr + SUNXI_MUSB_TXFIFOADD);
557 return readw(addr + SUNXI_MUSB_RXFIFOADD);
559 return 0; /* sunxi musb version is not known */
561 dev_err(sunxi_musb->controller->parent,
562 "Error unknown readw offset %u\n", offset);
565 } else if (addr == (sunxi_musb->mregs + 0x80)) {
566 /* ep control reg access */
567 return readw(addr + offset);
570 dev_err(sunxi_musb->controller->parent,
571 "Error unknown readw at 0x%x bytes offset\n",
572 (int)(addr - sunxi_musb->mregs));
576 static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
578 if (addr == sunxi_musb->mregs) {
579 /* generic control or fifo control reg access */
582 return writew(data, addr + SUNXI_MUSB_INTRTX);
584 return writew(data, addr + SUNXI_MUSB_INTRRX);
586 return writew(data, addr + SUNXI_MUSB_INTRTXE);
588 return writew(data, addr + SUNXI_MUSB_INTRRXE);
590 return writew(data, addr + SUNXI_MUSB_FRAME);
592 return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
594 return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
596 dev_err(sunxi_musb->controller->parent,
597 "Error unknown writew offset %u\n", offset);
600 } else if (addr == (sunxi_musb->mregs + 0x80)) {
601 /* ep control reg access */
602 return writew(data, addr + offset);
605 dev_err(sunxi_musb->controller->parent,
606 "Error unknown writew at 0x%x bytes offset\n",
607 (int)(addr - sunxi_musb->mregs));
610 static const struct musb_platform_ops sunxi_musb_ops = {
611 .quirks = MUSB_INDEXED_EP,
612 .init = sunxi_musb_init,
613 .exit = sunxi_musb_exit,
614 .enable = sunxi_musb_enable,
615 .disable = sunxi_musb_disable,
616 .fifo_offset = sunxi_musb_fifo_offset,
617 .ep_offset = sunxi_musb_ep_offset,
618 .busctl_offset = sunxi_musb_busctl_offset,
619 .readb = sunxi_musb_readb,
620 .writeb = sunxi_musb_writeb,
621 .readw = sunxi_musb_readw,
622 .writew = sunxi_musb_writew,
623 .dma_init = sunxi_musb_dma_controller_create,
624 .dma_exit = sunxi_musb_dma_controller_destroy,
625 .set_mode = sunxi_musb_set_mode,
626 .recover = sunxi_musb_recover,
627 .set_vbus = sunxi_musb_set_vbus,
628 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
629 .post_root_reset_end = sunxi_musb_post_root_reset_end,
632 #define SUNXI_MUSB_RAM_BITS 11
634 /* Allwinner OTG supports up to 5 endpoints */
635 static struct musb_fifo_cfg sunxi_musb_mode_cfg_5eps[] = {
636 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
637 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
638 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
639 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
640 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
641 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
642 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
643 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
644 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
645 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
648 /* H3/V3s OTG supports only 4 endpoints */
649 static struct musb_fifo_cfg sunxi_musb_mode_cfg_4eps[] = {
650 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
651 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
652 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
653 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
654 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
655 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
656 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
657 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
660 static const struct musb_hdrc_config sunxi_musb_hdrc_config_5eps = {
661 .fifo_cfg = sunxi_musb_mode_cfg_5eps,
662 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_5eps),
665 /* Two FIFOs per endpoint, plus ep_0. */
666 .num_eps = (ARRAY_SIZE(sunxi_musb_mode_cfg_5eps) / 2) + 1,
667 .ram_bits = SUNXI_MUSB_RAM_BITS,
670 static const struct musb_hdrc_config sunxi_musb_hdrc_config_4eps = {
671 .fifo_cfg = sunxi_musb_mode_cfg_4eps,
672 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_4eps),
675 /* Two FIFOs per endpoint, plus ep_0. */
676 .num_eps = (ARRAY_SIZE(sunxi_musb_mode_cfg_4eps) / 2) + 1,
677 .ram_bits = SUNXI_MUSB_RAM_BITS,
680 static int sunxi_musb_probe(struct platform_device *pdev)
682 struct musb_hdrc_platform_data pdata;
683 struct platform_device_info pinfo;
684 struct sunxi_glue *glue;
685 struct device_node *np = pdev->dev.of_node;
686 const struct sunxi_musb_cfg *cfg;
690 dev_err(&pdev->dev, "Error no device tree node found\n");
694 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
698 memset(&pdata, 0, sizeof(pdata));
699 switch (usb_get_dr_mode(&pdev->dev)) {
700 #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
701 case USB_DR_MODE_HOST:
702 pdata.mode = MUSB_HOST;
703 glue->phy_mode = PHY_MODE_USB_HOST;
706 #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
707 case USB_DR_MODE_PERIPHERAL:
708 pdata.mode = MUSB_PERIPHERAL;
709 glue->phy_mode = PHY_MODE_USB_DEVICE;
712 #ifdef CONFIG_USB_MUSB_DUAL_ROLE
713 case USB_DR_MODE_OTG:
714 pdata.mode = MUSB_OTG;
715 glue->phy_mode = PHY_MODE_USB_OTG;
719 dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
722 pdata.platform_ops = &sunxi_musb_ops;
724 cfg = of_device_get_match_data(&pdev->dev);
728 pdata.config = cfg->hdrc_config;
730 glue->dev = &pdev->dev;
731 INIT_WORK(&glue->work, sunxi_musb_work);
732 glue->host_nb.notifier_call = sunxi_musb_host_notifier;
735 set_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags);
738 set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
740 if (cfg->no_configdata)
741 set_bit(SUNXI_MUSB_FL_NO_CONFIGDATA, &glue->flags);
743 glue->clk = devm_clk_get(&pdev->dev, NULL);
744 if (IS_ERR(glue->clk)) {
745 dev_err(&pdev->dev, "Error getting clock: %ld\n",
747 return PTR_ERR(glue->clk);
750 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
751 glue->rst = devm_reset_control_get(&pdev->dev, NULL);
752 if (IS_ERR(glue->rst))
753 return dev_err_probe(&pdev->dev, PTR_ERR(glue->rst),
754 "Error getting reset\n");
757 glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
758 if (IS_ERR(glue->extcon))
759 return dev_err_probe(&pdev->dev, PTR_ERR(glue->extcon),
760 "Invalid or missing extcon\n");
762 glue->phy = devm_phy_get(&pdev->dev, "usb");
763 if (IS_ERR(glue->phy))
764 return dev_err_probe(&pdev->dev, PTR_ERR(glue->phy),
765 "Error getting phy\n");
767 glue->usb_phy = usb_phy_generic_register();
768 if (IS_ERR(glue->usb_phy)) {
769 dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
770 PTR_ERR(glue->usb_phy));
771 return PTR_ERR(glue->usb_phy);
774 glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
775 if (IS_ERR(glue->xceiv)) {
776 ret = PTR_ERR(glue->xceiv);
777 dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
778 goto err_unregister_usb_phy;
781 platform_set_drvdata(pdev, glue);
783 memset(&pinfo, 0, sizeof(pinfo));
784 pinfo.name = "musb-hdrc";
785 pinfo.id = PLATFORM_DEVID_AUTO;
786 pinfo.parent = &pdev->dev;
787 pinfo.fwnode = of_fwnode_handle(pdev->dev.of_node);
788 pinfo.of_node_reused = true;
789 pinfo.res = pdev->resource;
790 pinfo.num_res = pdev->num_resources;
792 pinfo.size_data = sizeof(pdata);
794 glue->musb_pdev = platform_device_register_full(&pinfo);
795 if (IS_ERR(glue->musb_pdev)) {
796 ret = PTR_ERR(glue->musb_pdev);
797 dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
798 goto err_unregister_usb_phy;
803 err_unregister_usb_phy:
804 usb_phy_generic_unregister(glue->usb_phy);
808 static void sunxi_musb_remove(struct platform_device *pdev)
810 struct sunxi_glue *glue = platform_get_drvdata(pdev);
811 struct platform_device *usb_phy = glue->usb_phy;
813 platform_device_unregister(glue->musb_pdev);
814 usb_phy_generic_unregister(usb_phy);
817 static const struct sunxi_musb_cfg sun4i_a10_musb_cfg = {
818 .hdrc_config = &sunxi_musb_hdrc_config_5eps,
822 static const struct sunxi_musb_cfg sun6i_a31_musb_cfg = {
823 .hdrc_config = &sunxi_musb_hdrc_config_5eps,
827 static const struct sunxi_musb_cfg sun8i_a33_musb_cfg = {
828 .hdrc_config = &sunxi_musb_hdrc_config_5eps,
830 .no_configdata = true,
833 static const struct sunxi_musb_cfg sun8i_h3_musb_cfg = {
834 .hdrc_config = &sunxi_musb_hdrc_config_4eps,
836 .no_configdata = true,
839 static const struct sunxi_musb_cfg suniv_f1c100s_musb_cfg = {
840 .hdrc_config = &sunxi_musb_hdrc_config_5eps,
843 .no_configdata = true,
846 static const struct of_device_id sunxi_musb_match[] = {
847 { .compatible = "allwinner,sun4i-a10-musb",
848 .data = &sun4i_a10_musb_cfg, },
849 { .compatible = "allwinner,sun6i-a31-musb",
850 .data = &sun6i_a31_musb_cfg, },
851 { .compatible = "allwinner,sun8i-a33-musb",
852 .data = &sun8i_a33_musb_cfg, },
853 { .compatible = "allwinner,sun8i-h3-musb",
854 .data = &sun8i_h3_musb_cfg, },
855 { .compatible = "allwinner,suniv-f1c100s-musb",
856 .data = &suniv_f1c100s_musb_cfg, },
859 MODULE_DEVICE_TABLE(of, sunxi_musb_match);
861 static struct platform_driver sunxi_musb_driver = {
862 .probe = sunxi_musb_probe,
863 .remove_new = sunxi_musb_remove,
865 .name = "musb-sunxi",
866 .of_match_table = sunxi_musb_match,
869 module_platform_driver(sunxi_musb_driver);
871 MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
873 MODULE_LICENSE("GPL v2");