blob: 4246aaa43ebd271312c3ccebd8c02d2cff8bcbfe [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Allwinner sun4i MUSB Glue Layer
4 *
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 *
7 * Based on code from
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 */
10
11#include <linux/clk.h>
12#include <linux/err.h>
13#include <linux/extcon.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/phy/phy-sun4i-usb.h>
19#include <linux/platform_device.h>
20#include <linux/reset.h>
21#include <linux/soc/sunxi/sunxi_sram.h>
22#include <linux/usb/musb.h>
23#include <linux/usb/of.h>
24#include <linux/usb/usb_phy_generic.h>
25#include <linux/workqueue.h>
26#include "musb_core.h"
27
28/*
29 * Register offsets, note sunxi musb has a different layout then most
30 * musb implementations, we translate the layout in musb_readb & friends.
31 */
32#define SUNXI_MUSB_POWER 0x0040
33#define SUNXI_MUSB_DEVCTL 0x0041
34#define SUNXI_MUSB_INDEX 0x0042
35#define SUNXI_MUSB_VEND0 0x0043
36#define SUNXI_MUSB_INTRTX 0x0044
37#define SUNXI_MUSB_INTRRX 0x0046
38#define SUNXI_MUSB_INTRTXE 0x0048
39#define SUNXI_MUSB_INTRRXE 0x004a
40#define SUNXI_MUSB_INTRUSB 0x004c
41#define SUNXI_MUSB_INTRUSBE 0x0050
42#define SUNXI_MUSB_FRAME 0x0054
43#define SUNXI_MUSB_TXFIFOSZ 0x0090
44#define SUNXI_MUSB_TXFIFOADD 0x0092
45#define SUNXI_MUSB_RXFIFOSZ 0x0094
46#define SUNXI_MUSB_RXFIFOADD 0x0096
47#define SUNXI_MUSB_FADDR 0x0098
48#define SUNXI_MUSB_TXFUNCADDR 0x0098
49#define SUNXI_MUSB_TXHUBADDR 0x009a
50#define SUNXI_MUSB_TXHUBPORT 0x009b
51#define SUNXI_MUSB_RXFUNCADDR 0x009c
52#define SUNXI_MUSB_RXHUBADDR 0x009e
53#define SUNXI_MUSB_RXHUBPORT 0x009f
54#define SUNXI_MUSB_CONFIGDATA 0x00c0
55
56/* VEND0 bits */
57#define SUNXI_MUSB_VEND0_PIO_MODE 0
58
59/* flags */
60#define SUNXI_MUSB_FL_ENABLED 0
61#define SUNXI_MUSB_FL_HOSTMODE 1
62#define SUNXI_MUSB_FL_HOSTMODE_PEND 2
63#define SUNXI_MUSB_FL_VBUS_ON 3
64#define SUNXI_MUSB_FL_PHY_ON 4
65#define SUNXI_MUSB_FL_HAS_SRAM 5
66#define SUNXI_MUSB_FL_HAS_RESET 6
67#define SUNXI_MUSB_FL_NO_CONFIGDATA 7
68#define SUNXI_MUSB_FL_PHY_MODE_PEND 8
69
70/* Our read/write methods need access and do not get passed in a musb ref :| */
71static struct musb *sunxi_musb;
72
73struct sunxi_glue {
74 struct device *dev;
75 struct musb *musb;
76 struct platform_device *musb_pdev;
77 struct clk *clk;
78 struct reset_control *rst;
79 struct phy *phy;
80 struct platform_device *usb_phy;
81 struct usb_phy *xceiv;
82 enum phy_mode phy_mode;
83 unsigned long flags;
84 struct work_struct work;
85 struct extcon_dev *extcon;
86 struct notifier_block host_nb;
87};
88
89/* phy_power_on / off may sleep, so we use a workqueue */
90static void sunxi_musb_work(struct work_struct *work)
91{
92 struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
93 bool vbus_on, phy_on;
94
95 if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
96 return;
97
98 if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
99 struct musb *musb = glue->musb;
100 unsigned long flags;
101 u8 devctl;
102
103 spin_lock_irqsave(&musb->lock, flags);
104
105 devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
106 if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
107 set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
108 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
109 MUSB_HST_MODE(musb);
110 devctl |= MUSB_DEVCTL_SESSION;
111 } else {
112 clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
113 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
114 MUSB_DEV_MODE(musb);
115 devctl &= ~MUSB_DEVCTL_SESSION;
116 }
117 writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
118
119 spin_unlock_irqrestore(&musb->lock, flags);
120 }
121
122 vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
123 phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
124
125 if (phy_on != vbus_on) {
126 if (vbus_on) {
127 phy_power_on(glue->phy);
128 set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
129 } else {
130 phy_power_off(glue->phy);
131 clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
132 }
133 }
134
135 if (test_and_clear_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags))
136 phy_set_mode(glue->phy, glue->phy_mode);
137}
138
139static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
140{
141 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
142
143 if (is_on) {
144 set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
145 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
146 } else {
147 clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
148 }
149
150 schedule_work(&glue->work);
151}
152
153static void sunxi_musb_pre_root_reset_end(struct musb *musb)
154{
155 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
156
157 sun4i_usb_phy_set_squelch_detect(glue->phy, false);
158}
159
160static void sunxi_musb_post_root_reset_end(struct musb *musb)
161{
162 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
163
164 sun4i_usb_phy_set_squelch_detect(glue->phy, true);
165}
166
167static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
168{
169 struct musb *musb = __hci;
170 unsigned long flags;
171
172 spin_lock_irqsave(&musb->lock, flags);
173
174 musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
175 if (musb->int_usb)
176 writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
177
178 if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
179 /* ep0 FADDR must be 0 when (re)entering peripheral mode */
180 musb_ep_select(musb->mregs, 0);
181 musb_writeb(musb->mregs, MUSB_FADDR, 0);
182 }
183
184 musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
185 if (musb->int_tx)
186 writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
187
188 musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
189 if (musb->int_rx)
190 writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
191
192 musb_interrupt(musb);
193
194 spin_unlock_irqrestore(&musb->lock, flags);
195
196 return IRQ_HANDLED;
197}
198
199static int sunxi_musb_host_notifier(struct notifier_block *nb,
200 unsigned long event, void *ptr)
201{
202 struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
203
204 if (event)
205 set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
206 else
207 clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
208
209 set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
210 schedule_work(&glue->work);
211
212 return NOTIFY_DONE;
213}
214
215static int sunxi_musb_init(struct musb *musb)
216{
217 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
218 int ret;
219
220 sunxi_musb = musb;
221 musb->phy = glue->phy;
222 musb->xceiv = glue->xceiv;
223
224 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags)) {
225 ret = sunxi_sram_claim(musb->controller->parent);
226 if (ret)
227 return ret;
228 }
229
230 ret = clk_prepare_enable(glue->clk);
231 if (ret)
232 goto error_sram_release;
233
234 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
235 ret = reset_control_deassert(glue->rst);
236 if (ret)
237 goto error_clk_disable;
238 }
239
240 writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
241
242 /* Register notifier before calling phy_init() */
243 ret = devm_extcon_register_notifier(glue->dev, glue->extcon,
244 EXTCON_USB_HOST, &glue->host_nb);
245 if (ret)
246 goto error_reset_assert;
247
248 ret = phy_init(glue->phy);
249 if (ret)
250 goto error_reset_assert;
251
252 musb->isr = sunxi_musb_interrupt;
253
254 /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
255 pm_runtime_get(musb->controller);
256
257 return 0;
258
259error_reset_assert:
260 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
261 reset_control_assert(glue->rst);
262error_clk_disable:
263 clk_disable_unprepare(glue->clk);
264error_sram_release:
265 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
266 sunxi_sram_release(musb->controller->parent);
267 return ret;
268}
269
270static int sunxi_musb_exit(struct musb *musb)
271{
272 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
273
274 pm_runtime_put(musb->controller);
275
276 cancel_work_sync(&glue->work);
277 if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
278 phy_power_off(glue->phy);
279
280 phy_exit(glue->phy);
281
282 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
283 reset_control_assert(glue->rst);
284
285 clk_disable_unprepare(glue->clk);
286 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
287 sunxi_sram_release(musb->controller->parent);
288
289 return 0;
290}
291
292static void sunxi_musb_enable(struct musb *musb)
293{
294 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
295
296 glue->musb = musb;
297
298 /* musb_core does not call us in a balanced manner */
299 if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
300 return;
301
302 schedule_work(&glue->work);
303}
304
305static void sunxi_musb_disable(struct musb *musb)
306{
307 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
308
309 clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
310}
311
312static struct dma_controller *
313sunxi_musb_dma_controller_create(struct musb *musb, void __iomem *base)
314{
315 return NULL;
316}
317
318static void sunxi_musb_dma_controller_destroy(struct dma_controller *c)
319{
320}
321
322static int sunxi_musb_set_mode(struct musb *musb, u8 mode)
323{
324 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
325 enum phy_mode new_mode;
326
327 switch (mode) {
328 case MUSB_HOST:
329 new_mode = PHY_MODE_USB_HOST;
330 break;
331 case MUSB_PERIPHERAL:
332 new_mode = PHY_MODE_USB_DEVICE;
333 break;
334 case MUSB_OTG:
335 new_mode = PHY_MODE_USB_OTG;
336 break;
337 default:
338 dev_err(musb->controller->parent,
339 "Error requested mode not supported by this kernel\n");
340 return -EINVAL;
341 }
342
343 if (glue->phy_mode == new_mode)
344 return 0;
345
346 if (musb->port_mode != MUSB_OTG) {
347 dev_err(musb->controller->parent,
348 "Error changing modes is only supported in dual role mode\n");
349 return -EINVAL;
350 }
351
352 if (musb->port1_status & USB_PORT_STAT_ENABLE)
353 musb_root_disconnect(musb);
354
355 /*
356 * phy_set_mode may sleep, and we're called with a spinlock held,
357 * so let sunxi_musb_work deal with it.
358 */
359 glue->phy_mode = new_mode;
360 set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
361 schedule_work(&glue->work);
362
363 return 0;
364}
365
366static int sunxi_musb_recover(struct musb *musb)
367{
368 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
369
370 /*
371 * Schedule a phy_set_mode with the current glue->phy_mode value,
372 * this will force end the current session.
373 */
374 set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
375 schedule_work(&glue->work);
376
377 return 0;
378}
379
380/*
381 * sunxi musb register layout
382 * 0x00 - 0x17 fifo regs, 1 long per fifo
383 * 0x40 - 0x57 generic control regs (power - frame)
384 * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
385 * 0x90 - 0x97 fifo control regs (indexed)
386 * 0x98 - 0x9f multipoint / busctl regs (indexed)
387 * 0xc0 configdata reg
388 */
389
390static u32 sunxi_musb_fifo_offset(u8 epnum)
391{
392 return (epnum * 4);
393}
394
395static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
396{
397 WARN_ONCE(offset != 0,
398 "sunxi_musb_ep_offset called with non 0 offset\n");
399
400 return 0x80; /* indexed, so ignore epnum */
401}
402
403static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
404{
405 return SUNXI_MUSB_TXFUNCADDR + offset;
406}
407
408static u8 sunxi_musb_readb(void __iomem *addr, u32 offset)
409{
410 struct sunxi_glue *glue;
411
412 if (addr == sunxi_musb->mregs) {
413 /* generic control or fifo control reg access */
414 switch (offset) {
415 case MUSB_FADDR:
416 return readb(addr + SUNXI_MUSB_FADDR);
417 case MUSB_POWER:
418 return readb(addr + SUNXI_MUSB_POWER);
419 case MUSB_INTRUSB:
420 return readb(addr + SUNXI_MUSB_INTRUSB);
421 case MUSB_INTRUSBE:
422 return readb(addr + SUNXI_MUSB_INTRUSBE);
423 case MUSB_INDEX:
424 return readb(addr + SUNXI_MUSB_INDEX);
425 case MUSB_TESTMODE:
426 return 0; /* No testmode on sunxi */
427 case MUSB_DEVCTL:
428 return readb(addr + SUNXI_MUSB_DEVCTL);
429 case MUSB_TXFIFOSZ:
430 return readb(addr + SUNXI_MUSB_TXFIFOSZ);
431 case MUSB_RXFIFOSZ:
432 return readb(addr + SUNXI_MUSB_RXFIFOSZ);
433 case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
434 glue = dev_get_drvdata(sunxi_musb->controller->parent);
435 /* A33 saves a reg, and we get to hardcode this */
436 if (test_bit(SUNXI_MUSB_FL_NO_CONFIGDATA,
437 &glue->flags))
438 return 0xde;
439
440 return readb(addr + SUNXI_MUSB_CONFIGDATA);
441 /* Offset for these is fixed by sunxi_musb_busctl_offset() */
442 case SUNXI_MUSB_TXFUNCADDR:
443 case SUNXI_MUSB_TXHUBADDR:
444 case SUNXI_MUSB_TXHUBPORT:
445 case SUNXI_MUSB_RXFUNCADDR:
446 case SUNXI_MUSB_RXHUBADDR:
447 case SUNXI_MUSB_RXHUBPORT:
448 /* multipoint / busctl reg access */
449 return readb(addr + offset);
450 default:
451 dev_err(sunxi_musb->controller->parent,
452 "Error unknown readb offset %u\n", offset);
453 return 0;
454 }
455 } else if (addr == (sunxi_musb->mregs + 0x80)) {
456 /* ep control reg access */
457 /* sunxi has a 2 byte hole before the txtype register */
458 if (offset >= MUSB_TXTYPE)
459 offset += 2;
460 return readb(addr + offset);
461 }
462
463 dev_err(sunxi_musb->controller->parent,
464 "Error unknown readb at 0x%x bytes offset\n",
465 (int)(addr - sunxi_musb->mregs));
466 return 0;
467}
468
469static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
470{
471 if (addr == sunxi_musb->mregs) {
472 /* generic control or fifo control reg access */
473 switch (offset) {
474 case MUSB_FADDR:
475 return writeb(data, addr + SUNXI_MUSB_FADDR);
476 case MUSB_POWER:
477 return writeb(data, addr + SUNXI_MUSB_POWER);
478 case MUSB_INTRUSB:
479 return writeb(data, addr + SUNXI_MUSB_INTRUSB);
480 case MUSB_INTRUSBE:
481 return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
482 case MUSB_INDEX:
483 return writeb(data, addr + SUNXI_MUSB_INDEX);
484 case MUSB_TESTMODE:
485 if (data)
486 dev_warn(sunxi_musb->controller->parent,
487 "sunxi-musb does not have testmode\n");
488 return;
489 case MUSB_DEVCTL:
490 return writeb(data, addr + SUNXI_MUSB_DEVCTL);
491 case MUSB_TXFIFOSZ:
492 return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
493 case MUSB_RXFIFOSZ:
494 return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
495 /* Offset for these is fixed by sunxi_musb_busctl_offset() */
496 case SUNXI_MUSB_TXFUNCADDR:
497 case SUNXI_MUSB_TXHUBADDR:
498 case SUNXI_MUSB_TXHUBPORT:
499 case SUNXI_MUSB_RXFUNCADDR:
500 case SUNXI_MUSB_RXHUBADDR:
501 case SUNXI_MUSB_RXHUBPORT:
502 /* multipoint / busctl reg access */
503 return writeb(data, addr + offset);
504 default:
505 dev_err(sunxi_musb->controller->parent,
506 "Error unknown writeb offset %u\n", offset);
507 return;
508 }
509 } else if (addr == (sunxi_musb->mregs + 0x80)) {
510 /* ep control reg access */
511 if (offset >= MUSB_TXTYPE)
512 offset += 2;
513 return writeb(data, addr + offset);
514 }
515
516 dev_err(sunxi_musb->controller->parent,
517 "Error unknown writeb at 0x%x bytes offset\n",
518 (int)(addr - sunxi_musb->mregs));
519}
520
521static u16 sunxi_musb_readw(void __iomem *addr, u32 offset)
522{
523 if (addr == sunxi_musb->mregs) {
524 /* generic control or fifo control reg access */
525 switch (offset) {
526 case MUSB_INTRTX:
527 return readw(addr + SUNXI_MUSB_INTRTX);
528 case MUSB_INTRRX:
529 return readw(addr + SUNXI_MUSB_INTRRX);
530 case MUSB_INTRTXE:
531 return readw(addr + SUNXI_MUSB_INTRTXE);
532 case MUSB_INTRRXE:
533 return readw(addr + SUNXI_MUSB_INTRRXE);
534 case MUSB_FRAME:
535 return readw(addr + SUNXI_MUSB_FRAME);
536 case MUSB_TXFIFOADD:
537 return readw(addr + SUNXI_MUSB_TXFIFOADD);
538 case MUSB_RXFIFOADD:
539 return readw(addr + SUNXI_MUSB_RXFIFOADD);
540 case MUSB_HWVERS:
541 return 0; /* sunxi musb version is not known */
542 default:
543 dev_err(sunxi_musb->controller->parent,
544 "Error unknown readw offset %u\n", offset);
545 return 0;
546 }
547 } else if (addr == (sunxi_musb->mregs + 0x80)) {
548 /* ep control reg access */
549 return readw(addr + offset);
550 }
551
552 dev_err(sunxi_musb->controller->parent,
553 "Error unknown readw at 0x%x bytes offset\n",
554 (int)(addr - sunxi_musb->mregs));
555 return 0;
556}
557
558static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
559{
560 if (addr == sunxi_musb->mregs) {
561 /* generic control or fifo control reg access */
562 switch (offset) {
563 case MUSB_INTRTX:
564 return writew(data, addr + SUNXI_MUSB_INTRTX);
565 case MUSB_INTRRX:
566 return writew(data, addr + SUNXI_MUSB_INTRRX);
567 case MUSB_INTRTXE:
568 return writew(data, addr + SUNXI_MUSB_INTRTXE);
569 case MUSB_INTRRXE:
570 return writew(data, addr + SUNXI_MUSB_INTRRXE);
571 case MUSB_FRAME:
572 return writew(data, addr + SUNXI_MUSB_FRAME);
573 case MUSB_TXFIFOADD:
574 return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
575 case MUSB_RXFIFOADD:
576 return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
577 default:
578 dev_err(sunxi_musb->controller->parent,
579 "Error unknown writew offset %u\n", offset);
580 return;
581 }
582 } else if (addr == (sunxi_musb->mregs + 0x80)) {
583 /* ep control reg access */
584 return writew(data, addr + offset);
585 }
586
587 dev_err(sunxi_musb->controller->parent,
588 "Error unknown writew at 0x%x bytes offset\n",
589 (int)(addr - sunxi_musb->mregs));
590}
591
592static const struct musb_platform_ops sunxi_musb_ops = {
593 .quirks = MUSB_INDEXED_EP,
594 .init = sunxi_musb_init,
595 .exit = sunxi_musb_exit,
596 .enable = sunxi_musb_enable,
597 .disable = sunxi_musb_disable,
598 .fifo_offset = sunxi_musb_fifo_offset,
599 .ep_offset = sunxi_musb_ep_offset,
600 .busctl_offset = sunxi_musb_busctl_offset,
601 .readb = sunxi_musb_readb,
602 .writeb = sunxi_musb_writeb,
603 .readw = sunxi_musb_readw,
604 .writew = sunxi_musb_writew,
605 .dma_init = sunxi_musb_dma_controller_create,
606 .dma_exit = sunxi_musb_dma_controller_destroy,
607 .set_mode = sunxi_musb_set_mode,
608 .recover = sunxi_musb_recover,
609 .set_vbus = sunxi_musb_set_vbus,
610 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
611 .post_root_reset_end = sunxi_musb_post_root_reset_end,
612};
613
614/* Allwinner OTG supports up to 5 endpoints */
615#define SUNXI_MUSB_MAX_EP_NUM 6
616#define SUNXI_MUSB_RAM_BITS 11
617
618static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
619 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
620 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
621 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
622 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
623 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
624 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
625 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
626 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
627 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
628 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
629};
630
631/* H3/V3s OTG supports only 4 endpoints */
632#define SUNXI_MUSB_MAX_EP_NUM_H3 5
633
634static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
635 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
636 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
637 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
638 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
639 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
640 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
641 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
642 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
643};
644
645static const struct musb_hdrc_config sunxi_musb_hdrc_config = {
646 .fifo_cfg = sunxi_musb_mode_cfg,
647 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
648 .multipoint = true,
649 .dyn_fifo = true,
650 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
651 .ram_bits = SUNXI_MUSB_RAM_BITS,
652};
653
654static struct musb_hdrc_config sunxi_musb_hdrc_config_h3 = {
655 .fifo_cfg = sunxi_musb_mode_cfg_h3,
656 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
657 .multipoint = true,
658 .dyn_fifo = true,
659 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
660 .ram_bits = SUNXI_MUSB_RAM_BITS,
661};
662
663
664static int sunxi_musb_probe(struct platform_device *pdev)
665{
666 struct musb_hdrc_platform_data pdata;
667 struct platform_device_info pinfo;
668 struct sunxi_glue *glue;
669 struct device_node *np = pdev->dev.of_node;
670 int ret;
671
672 if (!np) {
673 dev_err(&pdev->dev, "Error no device tree node found\n");
674 return -EINVAL;
675 }
676
677 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
678 if (!glue)
679 return -ENOMEM;
680
681 memset(&pdata, 0, sizeof(pdata));
682 switch (usb_get_dr_mode(&pdev->dev)) {
683#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
684 case USB_DR_MODE_HOST:
685 pdata.mode = MUSB_HOST;
686 glue->phy_mode = PHY_MODE_USB_HOST;
687 break;
688#endif
689#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
690 case USB_DR_MODE_PERIPHERAL:
691 pdata.mode = MUSB_PERIPHERAL;
692 glue->phy_mode = PHY_MODE_USB_DEVICE;
693 break;
694#endif
695#ifdef CONFIG_USB_MUSB_DUAL_ROLE
696 case USB_DR_MODE_OTG:
697 pdata.mode = MUSB_OTG;
698 glue->phy_mode = PHY_MODE_USB_OTG;
699 break;
700#endif
701 default:
702 dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
703 return -EINVAL;
704 }
705 pdata.platform_ops = &sunxi_musb_ops;
706 if (!of_device_is_compatible(np, "allwinner,sun8i-h3-musb"))
707 pdata.config = &sunxi_musb_hdrc_config;
708 else
709 pdata.config = &sunxi_musb_hdrc_config_h3;
710
711 glue->dev = &pdev->dev;
712 INIT_WORK(&glue->work, sunxi_musb_work);
713 glue->host_nb.notifier_call = sunxi_musb_host_notifier;
714
715 if (of_device_is_compatible(np, "allwinner,sun4i-a10-musb"))
716 set_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags);
717
718 if (of_device_is_compatible(np, "allwinner,sun6i-a31-musb"))
719 set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
720
721 if (of_device_is_compatible(np, "allwinner,sun8i-a33-musb") ||
722 of_device_is_compatible(np, "allwinner,sun8i-h3-musb")) {
723 set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
724 set_bit(SUNXI_MUSB_FL_NO_CONFIGDATA, &glue->flags);
725 }
726
727 glue->clk = devm_clk_get(&pdev->dev, NULL);
728 if (IS_ERR(glue->clk)) {
729 dev_err(&pdev->dev, "Error getting clock: %ld\n",
730 PTR_ERR(glue->clk));
731 return PTR_ERR(glue->clk);
732 }
733
734 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
735 glue->rst = devm_reset_control_get(&pdev->dev, NULL);
736 if (IS_ERR(glue->rst)) {
737 if (PTR_ERR(glue->rst) == -EPROBE_DEFER)
738 return -EPROBE_DEFER;
739 dev_err(&pdev->dev, "Error getting reset %ld\n",
740 PTR_ERR(glue->rst));
741 return PTR_ERR(glue->rst);
742 }
743 }
744
745 glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
746 if (IS_ERR(glue->extcon)) {
747 if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
748 return -EPROBE_DEFER;
749 dev_err(&pdev->dev, "Invalid or missing extcon\n");
750 return PTR_ERR(glue->extcon);
751 }
752
753 glue->phy = devm_phy_get(&pdev->dev, "usb");
754 if (IS_ERR(glue->phy)) {
755 if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
756 return -EPROBE_DEFER;
757 dev_err(&pdev->dev, "Error getting phy %ld\n",
758 PTR_ERR(glue->phy));
759 return PTR_ERR(glue->phy);
760 }
761
762 glue->usb_phy = usb_phy_generic_register();
763 if (IS_ERR(glue->usb_phy)) {
764 dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
765 PTR_ERR(glue->usb_phy));
766 return PTR_ERR(glue->usb_phy);
767 }
768
769 glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
770 if (IS_ERR(glue->xceiv)) {
771 ret = PTR_ERR(glue->xceiv);
772 dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
773 goto err_unregister_usb_phy;
774 }
775
776 platform_set_drvdata(pdev, glue);
777
778 memset(&pinfo, 0, sizeof(pinfo));
779 pinfo.name = "musb-hdrc";
780 pinfo.id = PLATFORM_DEVID_AUTO;
781 pinfo.parent = &pdev->dev;
782 pinfo.res = pdev->resource;
783 pinfo.num_res = pdev->num_resources;
784 pinfo.data = &pdata;
785 pinfo.size_data = sizeof(pdata);
786
787 glue->musb_pdev = platform_device_register_full(&pinfo);
788 if (IS_ERR(glue->musb_pdev)) {
789 ret = PTR_ERR(glue->musb_pdev);
790 dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
791 goto err_unregister_usb_phy;
792 }
793
794 return 0;
795
796err_unregister_usb_phy:
797 usb_phy_generic_unregister(glue->usb_phy);
798 return ret;
799}
800
801static int sunxi_musb_remove(struct platform_device *pdev)
802{
803 struct sunxi_glue *glue = platform_get_drvdata(pdev);
804 struct platform_device *usb_phy = glue->usb_phy;
805
806 platform_device_unregister(glue->musb_pdev);
807 usb_phy_generic_unregister(usb_phy);
808
809 return 0;
810}
811
812static const struct of_device_id sunxi_musb_match[] = {
813 { .compatible = "allwinner,sun4i-a10-musb", },
814 { .compatible = "allwinner,sun6i-a31-musb", },
815 { .compatible = "allwinner,sun8i-a33-musb", },
816 { .compatible = "allwinner,sun8i-h3-musb", },
817 {}
818};
819MODULE_DEVICE_TABLE(of, sunxi_musb_match);
820
821static struct platform_driver sunxi_musb_driver = {
822 .probe = sunxi_musb_probe,
823 .remove = sunxi_musb_remove,
824 .driver = {
825 .name = "musb-sunxi",
826 .of_match_table = sunxi_musb_match,
827 },
828};
829module_platform_driver(sunxi_musb_driver);
830
831MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
832MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
833MODULE_LICENSE("GPL v2");