1 // SPDX-License-Identifier: GPL-2.0+
3 * Ingenic JZ4740 "glue layer"
9 #include <linux/dma-mapping.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/usb/usb_phy_generic.h>
16 #include "musb_core.h"
20 struct platform_device *musb;
24 static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci)
27 irqreturn_t retval = IRQ_NONE;
28 struct musb *musb = __hci;
30 spin_lock_irqsave(&musb->lock, flags);
32 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
33 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
34 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
37 * The controller is gadget only, the state of the host mode IRQ bits is
38 * undefined. Mask them to make sure that the musb driver core will
41 musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME |
42 MUSB_INTR_RESET | MUSB_INTR_SOF;
44 if (musb->int_usb || musb->int_tx || musb->int_rx)
45 retval = musb_interrupt(musb);
47 spin_unlock_irqrestore(&musb->lock, flags);
52 static struct musb_fifo_cfg jz4740_musb_fifo_cfg[] = {
53 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
54 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
55 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 64, },
58 static const struct musb_hdrc_config jz4740_musb_config = {
59 /* Silicon does not implement USB OTG. */
61 /* Max EPs scanned, driver will decide which EP can be used. */
63 /* RAMbits needed to configure EPs from table */
65 .fifo_cfg = jz4740_musb_fifo_cfg,
66 .fifo_cfg_size = ARRAY_SIZE(jz4740_musb_fifo_cfg),
69 static struct musb_hdrc_platform_data jz4740_musb_platform_data = {
70 .mode = MUSB_PERIPHERAL,
71 .config = &jz4740_musb_config,
74 static int jz4740_musb_init(struct musb *musb)
76 usb_phy_generic_register();
77 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
78 if (IS_ERR(musb->xceiv)) {
79 pr_err("HS UDC: no transceiver configured\n");
80 return PTR_ERR(musb->xceiv);
83 /* Silicon does not implement ConfigData register.
84 * Set dyn_fifo to avoid reading EP config from hardware.
86 musb->dyn_fifo = true;
88 musb->isr = jz4740_musb_interrupt;
93 static int jz4740_musb_exit(struct musb *musb)
95 usb_put_phy(musb->xceiv);
101 * DMA has not been confirmed to work with CONFIG_USB_INVENTRA_DMA,
102 * so let's not set up the dma function pointers yet.
104 static const struct musb_platform_ops jz4740_musb_ops = {
105 .quirks = MUSB_DMA_INVENTRA | MUSB_INDEXED_EP,
107 .init = jz4740_musb_init,
108 .exit = jz4740_musb_exit,
111 static int jz4740_probe(struct platform_device *pdev)
113 struct musb_hdrc_platform_data *pdata = &jz4740_musb_platform_data;
114 struct platform_device *musb;
115 struct jz4740_glue *glue;
119 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
123 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
125 dev_err(&pdev->dev, "failed to allocate musb device\n");
129 clk = devm_clk_get(&pdev->dev, "udc");
131 dev_err(&pdev->dev, "failed to get clock\n");
133 goto err_platform_device_put;
136 ret = clk_prepare_enable(clk);
138 dev_err(&pdev->dev, "failed to enable clock\n");
139 goto err_platform_device_put;
142 musb->dev.parent = &pdev->dev;
144 glue->dev = &pdev->dev;
148 pdata->platform_ops = &jz4740_musb_ops;
150 platform_set_drvdata(pdev, glue);
152 ret = platform_device_add_resources(musb, pdev->resource,
153 pdev->num_resources);
155 dev_err(&pdev->dev, "failed to add resources\n");
156 goto err_clk_disable;
159 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
161 dev_err(&pdev->dev, "failed to add platform_data\n");
162 goto err_clk_disable;
165 ret = platform_device_add(musb);
167 dev_err(&pdev->dev, "failed to register musb device\n");
168 goto err_clk_disable;
174 clk_disable_unprepare(clk);
175 err_platform_device_put:
176 platform_device_put(musb);
180 static int jz4740_remove(struct platform_device *pdev)
182 struct jz4740_glue *glue = platform_get_drvdata(pdev);
184 platform_device_unregister(glue->musb);
185 usb_phy_generic_unregister(pdev);
186 clk_disable_unprepare(glue->clk);
191 static struct platform_driver jz4740_driver = {
192 .probe = jz4740_probe,
193 .remove = jz4740_remove,
195 .name = "musb-jz4740",
199 MODULE_DESCRIPTION("JZ4740 MUSB Glue Layer");
201 MODULE_LICENSE("GPL v2");
202 module_platform_driver(jz4740_driver);