1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Ingenic JZ4740 "glue layer"
4 *
5 * Copyright (C) 2013, Apelete Seketeli <apelete@seketeli.net>
6 */
7
8 #include <linux/clk.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/usb/role.h>
16 #include <linux/usb/usb_phy_generic.h>
17
18 #include "musb_core.h"
19
20 struct jz4740_glue {
21 struct platform_device *pdev;
22 struct musb *musb;
23 struct clk *clk;
24 struct usb_role_switch *role_sw;
25 };
26
jz4740_musb_interrupt(int irq,void * __hci)27 static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci)
28 {
29 unsigned long flags;
30 irqreturn_t retval = IRQ_NONE, retval_dma = IRQ_NONE;
31 struct musb *musb = __hci;
32
33 if (IS_ENABLED(CONFIG_USB_INVENTRA_DMA) && musb->dma_controller)
34 retval_dma = dma_controller_irq(irq, musb->dma_controller);
35
36 spin_lock_irqsave(&musb->lock, flags);
37
38 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
39 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
40 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
41
42 /*
43 * The controller is gadget only, the state of the host mode IRQ bits is
44 * undefined. Mask them to make sure that the musb driver core will
45 * never see them set
46 */
47 musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME |
48 MUSB_INTR_RESET | MUSB_INTR_SOF;
49
50 if (musb->int_usb || musb->int_tx || musb->int_rx)
51 retval = musb_interrupt(musb);
52
53 spin_unlock_irqrestore(&musb->lock, flags);
54
55 if (retval == IRQ_HANDLED || retval_dma == IRQ_HANDLED)
56 return IRQ_HANDLED;
57
58 return IRQ_NONE;
59 }
60
61 static struct musb_fifo_cfg jz4740_musb_fifo_cfg[] = {
62 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
63 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
64 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 64, },
65 };
66
67 static const struct musb_hdrc_config jz4740_musb_config = {
68 /* Silicon does not implement USB OTG. */
69 .multipoint = 0,
70 /* Max EPs scanned, driver will decide which EP can be used. */
71 .num_eps = 4,
72 /* RAMbits needed to configure EPs from table */
73 .ram_bits = 9,
74 .fifo_cfg = jz4740_musb_fifo_cfg,
75 .fifo_cfg_size = ARRAY_SIZE(jz4740_musb_fifo_cfg),
76 };
77
jz4740_musb_role_switch_set(struct usb_role_switch * sw,enum usb_role role)78 static int jz4740_musb_role_switch_set(struct usb_role_switch *sw,
79 enum usb_role role)
80 {
81 struct jz4740_glue *glue = usb_role_switch_get_drvdata(sw);
82 struct usb_phy *phy = glue->musb->xceiv;
83
84 switch (role) {
85 case USB_ROLE_NONE:
86 atomic_notifier_call_chain(&phy->notifier, USB_EVENT_NONE, phy);
87 break;
88 case USB_ROLE_DEVICE:
89 atomic_notifier_call_chain(&phy->notifier, USB_EVENT_VBUS, phy);
90 break;
91 case USB_ROLE_HOST:
92 atomic_notifier_call_chain(&phy->notifier, USB_EVENT_ID, phy);
93 break;
94 }
95
96 return 0;
97 }
98
jz4740_musb_init(struct musb * musb)99 static int jz4740_musb_init(struct musb *musb)
100 {
101 struct device *dev = musb->controller->parent;
102 struct jz4740_glue *glue = dev_get_drvdata(dev);
103 struct usb_role_switch_desc role_sw_desc = {
104 .set = jz4740_musb_role_switch_set,
105 .driver_data = glue,
106 .fwnode = dev_fwnode(dev),
107 };
108
109 glue->musb = musb;
110
111 if (dev->of_node)
112 musb->xceiv = devm_usb_get_phy_by_phandle(dev, "phys", 0);
113 else
114 musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
115 if (IS_ERR(musb->xceiv))
116 return dev_err_probe(dev, PTR_ERR(musb->xceiv),
117 "No transceiver configured\n");
118
119 glue->role_sw = usb_role_switch_register(dev, &role_sw_desc);
120 if (IS_ERR(glue->role_sw)) {
121 dev_err(dev, "Failed to register USB role switch\n");
122 return PTR_ERR(glue->role_sw);
123 }
124
125 /*
126 * Silicon does not implement ConfigData register.
127 * Set dyn_fifo to avoid reading EP config from hardware.
128 */
129 musb->dyn_fifo = true;
130
131 musb->isr = jz4740_musb_interrupt;
132
133 return 0;
134 }
135
jz4740_musb_exit(struct musb * musb)136 static int jz4740_musb_exit(struct musb *musb)
137 {
138 struct jz4740_glue *glue = dev_get_drvdata(musb->controller->parent);
139
140 usb_role_switch_unregister(glue->role_sw);
141
142 return 0;
143 }
144
145 static const struct musb_platform_ops jz4740_musb_ops = {
146 .quirks = MUSB_DMA_INVENTRA | MUSB_INDEXED_EP,
147 .fifo_mode = 2,
148 .init = jz4740_musb_init,
149 .exit = jz4740_musb_exit,
150 #ifdef CONFIG_USB_INVENTRA_DMA
151 .dma_init = musbhs_dma_controller_create_noirq,
152 .dma_exit = musbhs_dma_controller_destroy,
153 #endif
154 };
155
156 static const struct musb_hdrc_platform_data jz4740_musb_pdata = {
157 .mode = MUSB_PERIPHERAL,
158 .config = &jz4740_musb_config,
159 .platform_ops = &jz4740_musb_ops,
160 };
161
162 static struct musb_fifo_cfg jz4770_musb_fifo_cfg[] = {
163 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
164 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
165 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
166 { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
167 { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
168 { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
169 { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
170 { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
171 { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
172 { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
173 };
174
175 static struct musb_hdrc_config jz4770_musb_config = {
176 .multipoint = 1,
177 .num_eps = 11,
178 .ram_bits = 11,
179 .fifo_cfg = jz4770_musb_fifo_cfg,
180 .fifo_cfg_size = ARRAY_SIZE(jz4770_musb_fifo_cfg),
181 };
182
183 static const struct musb_hdrc_platform_data jz4770_musb_pdata = {
184 .mode = MUSB_PERIPHERAL, /* TODO: support OTG */
185 .config = &jz4770_musb_config,
186 .platform_ops = &jz4740_musb_ops,
187 };
188
jz4740_probe(struct platform_device * pdev)189 static int jz4740_probe(struct platform_device *pdev)
190 {
191 struct device *dev = &pdev->dev;
192 const struct musb_hdrc_platform_data *pdata;
193 struct platform_device *musb;
194 struct jz4740_glue *glue;
195 struct clk *clk;
196 int ret;
197
198 glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
199 if (!glue)
200 return -ENOMEM;
201
202 pdata = of_device_get_match_data(dev);
203 if (!pdata) {
204 dev_err(dev, "missing platform data\n");
205 return -EINVAL;
206 }
207
208 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
209 if (!musb) {
210 dev_err(dev, "failed to allocate musb device\n");
211 return -ENOMEM;
212 }
213
214 clk = devm_clk_get(dev, "udc");
215 if (IS_ERR(clk)) {
216 dev_err(dev, "failed to get clock\n");
217 ret = PTR_ERR(clk);
218 goto err_platform_device_put;
219 }
220
221 ret = clk_prepare_enable(clk);
222 if (ret) {
223 dev_err(dev, "failed to enable clock\n");
224 goto err_platform_device_put;
225 }
226
227 musb->dev.parent = dev;
228 musb->dev.dma_mask = &musb->dev.coherent_dma_mask;
229 musb->dev.coherent_dma_mask = DMA_BIT_MASK(32);
230 device_set_of_node_from_dev(&musb->dev, dev);
231
232 glue->pdev = musb;
233 glue->clk = clk;
234
235 platform_set_drvdata(pdev, glue);
236
237 ret = platform_device_add_resources(musb, pdev->resource,
238 pdev->num_resources);
239 if (ret) {
240 dev_err(dev, "failed to add resources\n");
241 goto err_clk_disable;
242 }
243
244 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
245 if (ret) {
246 dev_err(dev, "failed to add platform_data\n");
247 goto err_clk_disable;
248 }
249
250 ret = platform_device_add(musb);
251 if (ret) {
252 dev_err(dev, "failed to register musb device\n");
253 goto err_clk_disable;
254 }
255
256 return 0;
257
258 err_clk_disable:
259 clk_disable_unprepare(clk);
260 err_platform_device_put:
261 platform_device_put(musb);
262 return ret;
263 }
264
jz4740_remove(struct platform_device * pdev)265 static int jz4740_remove(struct platform_device *pdev)
266 {
267 struct jz4740_glue *glue = platform_get_drvdata(pdev);
268
269 platform_device_unregister(glue->pdev);
270 clk_disable_unprepare(glue->clk);
271
272 return 0;
273 }
274
275 static const struct of_device_id jz4740_musb_of_match[] = {
276 { .compatible = "ingenic,jz4740-musb", .data = &jz4740_musb_pdata },
277 { .compatible = "ingenic,jz4770-musb", .data = &jz4770_musb_pdata },
278 { /* sentinel */ },
279 };
280 MODULE_DEVICE_TABLE(of, jz4740_musb_of_match);
281
282 static struct platform_driver jz4740_driver = {
283 .probe = jz4740_probe,
284 .remove = jz4740_remove,
285 .driver = {
286 .name = "musb-jz4740",
287 .of_match_table = jz4740_musb_of_match,
288 },
289 };
290
291 MODULE_DESCRIPTION("JZ4740 MUSB Glue Layer");
292 MODULE_AUTHOR("Apelete Seketeli <apelete@seketeli.net>");
293 MODULE_LICENSE("GPL v2");
294 module_platform_driver(jz4740_driver);
295