3 #ifdef CONFIG_ARCH_SUNXI
4 #include <asm/arch/usbc.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/usb/gadget.h>
11 #include "linux-compat.h"
12 #include "usb-compat.h"
13 #include "musb_core.h"
14 #include "musb_host.h"
15 #include "musb_gadget.h"
17 #ifdef CONFIG_MUSB_HOST
19 struct usb_host_endpoint hep
;
23 static struct musb
*host
;
24 static struct usb_hcd hcd
;
25 static enum usb_device_speed host_speed
;
27 static void musb_host_complete_urb(struct urb
*urb
)
29 urb
->dev
->status
&= ~USB_ST_NOT_PROC
;
30 urb
->dev
->act_len
= urb
->actual_length
;
33 static struct usb_host_endpoint hep
;
34 static struct urb urb
;
36 static void construct_urb(struct urb
*urb
, struct usb_host_endpoint
*hep
,
37 struct usb_device
*dev
, int endpoint_type
,
38 unsigned long pipe
, void *buffer
, int len
,
39 struct devrequest
*setup
, int interval
)
41 int epnum
= usb_pipeendpoint(pipe
);
42 int is_in
= usb_pipein(pipe
);
44 memset(urb
, 0, sizeof(struct urb
));
45 memset(hep
, 0, sizeof(struct usb_host_endpoint
));
46 INIT_LIST_HEAD(&hep
->urb_list
);
47 INIT_LIST_HEAD(&urb
->urb_list
);
49 urb
->complete
= musb_host_complete_urb
;
50 urb
->status
= -EINPROGRESS
;
53 urb
->transfer_buffer
= buffer
;
54 urb
->transfer_dma
= (unsigned long)buffer
;
55 urb
->transfer_buffer_length
= len
;
56 urb
->setup_packet
= (unsigned char *)setup
;
58 urb
->ep
->desc
.wMaxPacketSize
=
59 __cpu_to_le16(is_in
? dev
->epmaxpacketin
[epnum
] :
60 dev
->epmaxpacketout
[epnum
]);
61 urb
->ep
->desc
.bmAttributes
= endpoint_type
;
62 urb
->ep
->desc
.bEndpointAddress
=
63 (is_in
? USB_DIR_IN
: USB_DIR_OUT
) | epnum
;
64 urb
->ep
->desc
.bInterval
= interval
;
67 static int submit_urb(struct usb_hcd
*hcd
, struct urb
*urb
)
69 struct musb
*host
= hcd
->hcd_priv
;
71 unsigned long timeout
;
73 ret
= musb_urb_enqueue(hcd
, urb
, 0);
75 printf("Failed to enqueue URB to controller\n");
79 timeout
= get_timer(0) + USB_TIMEOUT_MS(urb
->pipe
);
84 } while (urb
->status
== -EINPROGRESS
&&
85 get_timer(0) < timeout
);
87 if (urb
->status
== -EINPROGRESS
)
88 musb_urb_dequeue(hcd
, urb
, -ETIME
);
93 int submit_control_msg(struct usb_device
*dev
, unsigned long pipe
,
94 void *buffer
, int len
, struct devrequest
*setup
)
96 construct_urb(&urb
, &hep
, dev
, USB_ENDPOINT_XFER_CONTROL
, pipe
,
97 buffer
, len
, setup
, 0);
99 /* Fix speed for non hub-attached devices */
101 dev
->speed
= host_speed
;
103 return submit_urb(&hcd
, &urb
);
107 int submit_bulk_msg(struct usb_device
*dev
, unsigned long pipe
,
108 void *buffer
, int len
)
110 construct_urb(&urb
, &hep
, dev
, USB_ENDPOINT_XFER_BULK
, pipe
,
111 buffer
, len
, NULL
, 0);
112 return submit_urb(&hcd
, &urb
);
115 int submit_int_msg(struct usb_device
*dev
, unsigned long pipe
,
116 void *buffer
, int len
, int interval
)
118 construct_urb(&urb
, &hep
, dev
, USB_ENDPOINT_XFER_INT
, pipe
,
119 buffer
, len
, NULL
, interval
);
120 return submit_urb(&hcd
, &urb
);
123 struct int_queue
*create_int_queue(struct usb_device
*dev
, unsigned long pipe
,
124 int queuesize
, int elementsize
, void *buffer
, int interval
)
126 struct int_queue
*queue
;
127 int ret
, index
= usb_pipein(pipe
) * 16 + usb_pipeendpoint(pipe
);
129 if (queuesize
!= 1) {
130 printf("ERROR musb int-queues only support queuesize 1\n");
134 if (dev
->int_pending
& (1 << index
)) {
135 printf("ERROR int-urb is already pending on pipe %lx\n", pipe
);
139 queue
= malloc(sizeof(*queue
));
143 construct_urb(&queue
->urb
, &queue
->hep
, dev
, USB_ENDPOINT_XFER_INT
,
144 pipe
, buffer
, elementsize
, NULL
, interval
);
146 ret
= musb_urb_enqueue(&hcd
, &queue
->urb
, 0);
148 printf("Failed to enqueue URB to controller\n");
153 dev
->int_pending
|= 1 << index
;
157 int destroy_int_queue(struct usb_device
*dev
, struct int_queue
*queue
)
159 int index
= usb_pipein(queue
->urb
.pipe
) * 16 +
160 usb_pipeendpoint(queue
->urb
.pipe
);
162 if (queue
->urb
.status
== -EINPROGRESS
)
163 musb_urb_dequeue(&hcd
, &queue
->urb
, -ETIME
);
165 dev
->int_pending
&= ~(1 << index
);
170 void *poll_int_queue(struct usb_device
*dev
, struct int_queue
*queue
)
172 if (queue
->urb
.status
!= -EINPROGRESS
)
173 return NULL
; /* URB has already completed in a prev. poll */
177 if (queue
->urb
.status
!= -EINPROGRESS
)
178 return queue
->urb
.transfer_buffer
; /* Done */
180 return NULL
; /* URB still pending */
183 int usb_reset_root_port(void)
185 void *mbase
= host
->mregs
;
188 power
= musb_readb(mbase
, MUSB_POWER
);
190 musb_writeb(mbase
, MUSB_POWER
, MUSB_POWER_RESET
| power
);
192 #ifdef CONFIG_ARCH_SUNXI
194 * sunxi phy has a bug and it will wrongly detect high speed squelch
195 * when clearing reset on low-speed devices, temporary disable
196 * squelch detection to work around this.
198 sunxi_usbc_enable_squelch_detect(0, 0);
200 power
= musb_readb(mbase
, MUSB_POWER
);
201 musb_writeb(mbase
, MUSB_POWER
, ~MUSB_POWER_RESET
& power
);
202 #ifdef CONFIG_ARCH_SUNXI
203 sunxi_usbc_enable_squelch_detect(0, 1);
206 host_speed
= (musb_readb(mbase
, MUSB_POWER
) & MUSB_POWER_HSMODE
) ?
208 (musb_readb(mbase
, MUSB_DEVCTL
) & MUSB_DEVCTL_FSDEV
) ?
209 USB_SPEED_FULL
: USB_SPEED_LOW
;
210 mdelay((host_speed
== USB_SPEED_LOW
) ? 200 : 50);
215 int usb_lowlevel_init(int index
, enum usb_init_type init
, void **controller
)
218 /* USB spec says it may take up to 1 second for a device to connect */
219 unsigned long timeout
= get_timer(0) + 1000;
222 printf("MUSB host is not registered\n");
229 if (musb_readb(mbase
, MUSB_DEVCTL
) & MUSB_DEVCTL_HM
)
231 } while (get_timer(0) < timeout
);
232 if (get_timer(0) >= timeout
)
235 usb_reset_root_port();
242 int usb_lowlevel_stop(int index
)
245 printf("MUSB host is not registered\n");
252 #endif /* CONFIG_MUSB_HOST */
254 #ifdef CONFIG_MUSB_GADGET
255 static struct musb
*gadget
;
257 int usb_gadget_handle_interrupts(int index
)
260 if (!gadget
|| !gadget
->isr
)
263 return gadget
->isr(0, gadget
);
266 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
270 if (!driver
|| driver
->speed
< USB_SPEED_FULL
|| !driver
->bind
||
272 printf("bad parameter.\n");
277 printf("Controller uninitialized\n");
281 ret
= musb_gadget_start(&gadget
->g
, driver
);
283 printf("gadget_start failed with %d\n", ret
);
287 ret
= driver
->bind(&gadget
->g
);
289 printf("bind failed with %d\n", ret
);
296 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
298 if (driver
->disconnect
)
299 driver
->disconnect(&gadget
->g
);
301 driver
->unbind(&gadget
->g
);
304 #endif /* CONFIG_MUSB_GADGET */
306 int musb_register(struct musb_hdrc_platform_data
*plat
, void *bdata
,
311 switch (plat
->mode
) {
312 #ifdef CONFIG_MUSB_HOST
317 #ifdef CONFIG_MUSB_GADGET
318 case MUSB_PERIPHERAL
:
326 *musbp
= musb_init_controller(plat
, (struct device
*)bdata
, ctl_regs
);
328 printf("Failed to init the controller\n");