1eb81955bSIlya Yanok #include <common.h>
224b852a7SSimon Glass #include <console.h>
37a342785SHeiko Schocher #include <watchdog.h>
4246e3b87SHans de Goede #ifdef CONFIG_ARCH_SUNXI
52aacc423SHans de Goede #include <asm/arch/usb_phy.h>
6246e3b87SHans de Goede #endif
71221ce45SMasahiro Yamada #include <linux/errno.h>
8eb81955bSIlya Yanok #include <linux/usb/ch9.h>
9eb81955bSIlya Yanok #include <linux/usb/gadget.h>
10eb81955bSIlya Yanok
11eb81955bSIlya Yanok #include <usb.h>
12eb81955bSIlya Yanok #include "linux-compat.h"
13eb81955bSIlya Yanok #include "usb-compat.h"
14eb81955bSIlya Yanok #include "musb_core.h"
15eb81955bSIlya Yanok #include "musb_host.h"
16eb81955bSIlya Yanok #include "musb_gadget.h"
17fc85d39eSHans de Goede #include "musb_uboot.h"
18eb81955bSIlya Yanok
1995de1e2fSPaul Kocialkowski #ifdef CONFIG_USB_MUSB_HOST
20904f2a83SHans de Goede struct int_queue {
21904f2a83SHans de Goede struct usb_host_endpoint hep;
22904f2a83SHans de Goede struct urb urb;
23904f2a83SHans de Goede };
24904f2a83SHans de Goede
253739bf7eSSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
26fc85d39eSHans de Goede struct musb_host_data musb_host;
2709e7ea47SHans de Goede #endif
28eb81955bSIlya Yanok
musb_host_complete_urb(struct urb * urb)29eb81955bSIlya Yanok static void musb_host_complete_urb(struct urb *urb)
30eb81955bSIlya Yanok {
31eb81955bSIlya Yanok urb->dev->status &= ~USB_ST_NOT_PROC;
32eb81955bSIlya Yanok urb->dev->act_len = urb->actual_length;
33eb81955bSIlya Yanok }
34eb81955bSIlya Yanok
construct_urb(struct urb * urb,struct usb_host_endpoint * hep,struct usb_device * dev,int endpoint_type,unsigned long pipe,void * buffer,int len,struct devrequest * setup,int interval)35accf04c2SHans de Goede static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
36accf04c2SHans de Goede struct usb_device *dev, int endpoint_type,
37eb81955bSIlya Yanok unsigned long pipe, void *buffer, int len,
38eb81955bSIlya Yanok struct devrequest *setup, int interval)
39eb81955bSIlya Yanok {
40eb81955bSIlya Yanok int epnum = usb_pipeendpoint(pipe);
41eb81955bSIlya Yanok int is_in = usb_pipein(pipe);
42eb81955bSIlya Yanok
43accf04c2SHans de Goede memset(urb, 0, sizeof(struct urb));
44accf04c2SHans de Goede memset(hep, 0, sizeof(struct usb_host_endpoint));
45accf04c2SHans de Goede INIT_LIST_HEAD(&hep->urb_list);
46accf04c2SHans de Goede INIT_LIST_HEAD(&urb->urb_list);
47accf04c2SHans de Goede urb->ep = hep;
48accf04c2SHans de Goede urb->complete = musb_host_complete_urb;
49accf04c2SHans de Goede urb->status = -EINPROGRESS;
50accf04c2SHans de Goede urb->dev = dev;
51accf04c2SHans de Goede urb->pipe = pipe;
52accf04c2SHans de Goede urb->transfer_buffer = buffer;
53accf04c2SHans de Goede urb->transfer_dma = (unsigned long)buffer;
54accf04c2SHans de Goede urb->transfer_buffer_length = len;
55accf04c2SHans de Goede urb->setup_packet = (unsigned char *)setup;
56eb81955bSIlya Yanok
57accf04c2SHans de Goede urb->ep->desc.wMaxPacketSize =
58eb81955bSIlya Yanok __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
59eb81955bSIlya Yanok dev->epmaxpacketout[epnum]);
60accf04c2SHans de Goede urb->ep->desc.bmAttributes = endpoint_type;
61accf04c2SHans de Goede urb->ep->desc.bEndpointAddress =
62eb81955bSIlya Yanok (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
63accf04c2SHans de Goede urb->ep->desc.bInterval = interval;
64eb81955bSIlya Yanok }
65eb81955bSIlya Yanok
submit_urb(struct usb_hcd * hcd,struct urb * urb)66eb81955bSIlya Yanok static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
67eb81955bSIlya Yanok {
68eb81955bSIlya Yanok struct musb *host = hcd->hcd_priv;
69eb81955bSIlya Yanok int ret;
70dc9a3912SHans de Goede unsigned long timeout;
71eb81955bSIlya Yanok
72eb81955bSIlya Yanok ret = musb_urb_enqueue(hcd, urb, 0);
73eb81955bSIlya Yanok if (ret < 0) {
74eb81955bSIlya Yanok printf("Failed to enqueue URB to controller\n");
75eb81955bSIlya Yanok return ret;
76eb81955bSIlya Yanok }
77eb81955bSIlya Yanok
78dc9a3912SHans de Goede timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
79eb81955bSIlya Yanok do {
80eb81955bSIlya Yanok if (ctrlc())
81eb81955bSIlya Yanok return -EIO;
82eb81955bSIlya Yanok host->isr(0, host);
83e8672e3fSHans de Goede } while (urb->status == -EINPROGRESS &&
84dc9a3912SHans de Goede get_timer(0) < timeout);
85eb81955bSIlya Yanok
86b918a0c6SHans de Goede if (urb->status == -EINPROGRESS)
87b918a0c6SHans de Goede musb_urb_dequeue(hcd, urb, -ETIME);
88b918a0c6SHans de Goede
89eb81955bSIlya Yanok return urb->status;
90eb81955bSIlya Yanok }
91eb81955bSIlya Yanok
_musb_submit_control_msg(struct musb_host_data * host,struct usb_device * dev,unsigned long pipe,void * buffer,int len,struct devrequest * setup)92fc85d39eSHans de Goede static int _musb_submit_control_msg(struct musb_host_data *host,
93fc85d39eSHans de Goede struct usb_device *dev, unsigned long pipe,
94eb81955bSIlya Yanok void *buffer, int len, struct devrequest *setup)
95eb81955bSIlya Yanok {
96fc85d39eSHans de Goede construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
97fc85d39eSHans de Goede pipe, buffer, len, setup, 0);
98eb81955bSIlya Yanok
99eb81955bSIlya Yanok /* Fix speed for non hub-attached devices */
100e740ca3cSHans de Goede if (!usb_dev_get_parent(dev))
101fc85d39eSHans de Goede dev->speed = host->host_speed;
102eb81955bSIlya Yanok
103fc85d39eSHans de Goede return submit_urb(&host->hcd, &host->urb);
104eb81955bSIlya Yanok }
105eb81955bSIlya Yanok
_musb_submit_bulk_msg(struct musb_host_data * host,struct usb_device * dev,unsigned long pipe,void * buffer,int len)106fc85d39eSHans de Goede static int _musb_submit_bulk_msg(struct musb_host_data *host,
107fc85d39eSHans de Goede struct usb_device *dev, unsigned long pipe, void *buffer, int len)
108eb81955bSIlya Yanok {
109fc85d39eSHans de Goede construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
110fc85d39eSHans de Goede pipe, buffer, len, NULL, 0);
111fc85d39eSHans de Goede return submit_urb(&host->hcd, &host->urb);
112eb81955bSIlya Yanok }
113eb81955bSIlya Yanok
_musb_submit_int_msg(struct musb_host_data * host,struct usb_device * dev,unsigned long pipe,void * buffer,int len,int interval,bool nonblock)114fc85d39eSHans de Goede static int _musb_submit_int_msg(struct musb_host_data *host,
115fc85d39eSHans de Goede struct usb_device *dev, unsigned long pipe,
116*92937b1fSMichal Suchanek void *buffer, int len, int interval, bool nonblock)
117eb81955bSIlya Yanok {
118fc85d39eSHans de Goede construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
119eb81955bSIlya Yanok buffer, len, NULL, interval);
120fc85d39eSHans de Goede return submit_urb(&host->hcd, &host->urb);
121eb81955bSIlya Yanok }
122eb81955bSIlya Yanok
_musb_create_int_queue(struct musb_host_data * host,struct usb_device * dev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)123fc85d39eSHans de Goede static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
124fc85d39eSHans de Goede struct usb_device *dev, unsigned long pipe, int queuesize,
125fc85d39eSHans de Goede int elementsize, void *buffer, int interval)
126904f2a83SHans de Goede {
127904f2a83SHans de Goede struct int_queue *queue;
128904f2a83SHans de Goede int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
129904f2a83SHans de Goede
130904f2a83SHans de Goede if (queuesize != 1) {
131904f2a83SHans de Goede printf("ERROR musb int-queues only support queuesize 1\n");
132904f2a83SHans de Goede return NULL;
133904f2a83SHans de Goede }
134904f2a83SHans de Goede
135904f2a83SHans de Goede if (dev->int_pending & (1 << index)) {
136904f2a83SHans de Goede printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
137904f2a83SHans de Goede return NULL;
138904f2a83SHans de Goede }
139904f2a83SHans de Goede
140904f2a83SHans de Goede queue = malloc(sizeof(*queue));
141904f2a83SHans de Goede if (!queue)
142904f2a83SHans de Goede return NULL;
143904f2a83SHans de Goede
144904f2a83SHans de Goede construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
145904f2a83SHans de Goede pipe, buffer, elementsize, NULL, interval);
146904f2a83SHans de Goede
147fc85d39eSHans de Goede ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
148904f2a83SHans de Goede if (ret < 0) {
149904f2a83SHans de Goede printf("Failed to enqueue URB to controller\n");
150904f2a83SHans de Goede free(queue);
151904f2a83SHans de Goede return NULL;
152904f2a83SHans de Goede }
153904f2a83SHans de Goede
154904f2a83SHans de Goede dev->int_pending |= 1 << index;
155904f2a83SHans de Goede return queue;
156904f2a83SHans de Goede }
157904f2a83SHans de Goede
_musb_destroy_int_queue(struct musb_host_data * host,struct usb_device * dev,struct int_queue * queue)158fc85d39eSHans de Goede static int _musb_destroy_int_queue(struct musb_host_data *host,
159fc85d39eSHans de Goede struct usb_device *dev, struct int_queue *queue)
160904f2a83SHans de Goede {
161904f2a83SHans de Goede int index = usb_pipein(queue->urb.pipe) * 16 +
162904f2a83SHans de Goede usb_pipeendpoint(queue->urb.pipe);
163904f2a83SHans de Goede
164904f2a83SHans de Goede if (queue->urb.status == -EINPROGRESS)
165fc85d39eSHans de Goede musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
166904f2a83SHans de Goede
167904f2a83SHans de Goede dev->int_pending &= ~(1 << index);
168904f2a83SHans de Goede free(queue);
169904f2a83SHans de Goede return 0;
170904f2a83SHans de Goede }
171904f2a83SHans de Goede
_musb_poll_int_queue(struct musb_host_data * host,struct usb_device * dev,struct int_queue * queue)172fc85d39eSHans de Goede static void *_musb_poll_int_queue(struct musb_host_data *host,
173fc85d39eSHans de Goede struct usb_device *dev, struct int_queue *queue)
174904f2a83SHans de Goede {
175904f2a83SHans de Goede if (queue->urb.status != -EINPROGRESS)
176904f2a83SHans de Goede return NULL; /* URB has already completed in a prev. poll */
177904f2a83SHans de Goede
178fc85d39eSHans de Goede host->host->isr(0, host->host);
179904f2a83SHans de Goede
180904f2a83SHans de Goede if (queue->urb.status != -EINPROGRESS)
181904f2a83SHans de Goede return queue->urb.transfer_buffer; /* Done */
182904f2a83SHans de Goede
183904f2a83SHans de Goede return NULL; /* URB still pending */
184904f2a83SHans de Goede }
185904f2a83SHans de Goede
_musb_reset_root_port(struct musb_host_data * host,struct usb_device * dev)186fc85d39eSHans de Goede static int _musb_reset_root_port(struct musb_host_data *host,
187fc85d39eSHans de Goede struct usb_device *dev)
18890cdc103SHans de Goede {
189fc85d39eSHans de Goede void *mbase = host->host->mregs;
19090cdc103SHans de Goede u8 power;
19190cdc103SHans de Goede
19290cdc103SHans de Goede power = musb_readb(mbase, MUSB_POWER);
19390cdc103SHans de Goede power &= 0xf0;
19490cdc103SHans de Goede musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
19590cdc103SHans de Goede mdelay(50);
196246e3b87SHans de Goede #ifdef CONFIG_ARCH_SUNXI
197246e3b87SHans de Goede /*
198246e3b87SHans de Goede * sunxi phy has a bug and it will wrongly detect high speed squelch
199246e3b87SHans de Goede * when clearing reset on low-speed devices, temporary disable
200246e3b87SHans de Goede * squelch detection to work around this.
201246e3b87SHans de Goede */
2027b798658SHans de Goede sunxi_usb_phy_enable_squelch_detect(0, 0);
203246e3b87SHans de Goede #endif
20490cdc103SHans de Goede power = musb_readb(mbase, MUSB_POWER);
20590cdc103SHans de Goede musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
206246e3b87SHans de Goede #ifdef CONFIG_ARCH_SUNXI
2077b798658SHans de Goede sunxi_usb_phy_enable_squelch_detect(0, 1);
208246e3b87SHans de Goede #endif
209fc85d39eSHans de Goede host->host->isr(0, host->host);
210fc85d39eSHans de Goede host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
21190cdc103SHans de Goede USB_SPEED_HIGH :
21290cdc103SHans de Goede (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
21390cdc103SHans de Goede USB_SPEED_FULL : USB_SPEED_LOW;
214fc85d39eSHans de Goede mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
215de31213fSSimon Glass
216de31213fSSimon Glass return 0;
21790cdc103SHans de Goede }
21890cdc103SHans de Goede
musb_lowlevel_init(struct musb_host_data * host)219fc85d39eSHans de Goede int musb_lowlevel_init(struct musb_host_data *host)
220eb81955bSIlya Yanok {
221eb81955bSIlya Yanok void *mbase;
222dc9a3912SHans de Goede /* USB spec says it may take up to 1 second for a device to connect */
223dc9a3912SHans de Goede unsigned long timeout = get_timer(0) + 1000;
22415837236SHans de Goede int ret;
225eb81955bSIlya Yanok
226fc85d39eSHans de Goede if (!host->host) {
227eb81955bSIlya Yanok printf("MUSB host is not registered\n");
228eb81955bSIlya Yanok return -ENODEV;
229eb81955bSIlya Yanok }
230eb81955bSIlya Yanok
231fc85d39eSHans de Goede ret = musb_start(host->host);
23215837236SHans de Goede if (ret)
23315837236SHans de Goede return ret;
23415837236SHans de Goede
235fc85d39eSHans de Goede mbase = host->host->mregs;
236eb81955bSIlya Yanok do {
237eb81955bSIlya Yanok if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
238eb81955bSIlya Yanok break;
239dc9a3912SHans de Goede } while (get_timer(0) < timeout);
240bf313230SHans de Goede if (get_timer(0) >= timeout) {
241bf313230SHans de Goede musb_stop(host->host);
242eb81955bSIlya Yanok return -ENODEV;
243bf313230SHans de Goede }
244eb81955bSIlya Yanok
245fc85d39eSHans de Goede _musb_reset_root_port(host, NULL);
246fc85d39eSHans de Goede host->host->is_active = 1;
247fc85d39eSHans de Goede host->hcd.hcd_priv = host->host;
248eb81955bSIlya Yanok
249eb81955bSIlya Yanok return 0;
250eb81955bSIlya Yanok }
251eb81955bSIlya Yanok
2523739bf7eSSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
usb_lowlevel_stop(int index)253eb81955bSIlya Yanok int usb_lowlevel_stop(int index)
254eb81955bSIlya Yanok {
255fc85d39eSHans de Goede if (!musb_host.host) {
256eb81955bSIlya Yanok printf("MUSB host is not registered\n");
257eb81955bSIlya Yanok return -ENODEV;
258eb81955bSIlya Yanok }
259eb81955bSIlya Yanok
260fc85d39eSHans de Goede musb_stop(musb_host.host);
261eb81955bSIlya Yanok return 0;
262eb81955bSIlya Yanok }
2631398252aSHans de Goede
submit_bulk_msg(struct usb_device * dev,unsigned long pipe,void * buffer,int length)2641398252aSHans de Goede int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
2651398252aSHans de Goede void *buffer, int length)
2661398252aSHans de Goede {
267fc85d39eSHans de Goede return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
2681398252aSHans de Goede }
2691398252aSHans de Goede
submit_control_msg(struct usb_device * dev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)2701398252aSHans de Goede int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2711398252aSHans de Goede void *buffer, int length, struct devrequest *setup)
2721398252aSHans de Goede {
273fc85d39eSHans de Goede return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
2741398252aSHans de Goede }
2751398252aSHans de Goede
submit_int_msg(struct usb_device * dev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)2761398252aSHans de Goede int submit_int_msg(struct usb_device *dev, unsigned long pipe,
277*92937b1fSMichal Suchanek void *buffer, int length, int interval, bool nonblock)
2781398252aSHans de Goede {
279*92937b1fSMichal Suchanek return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length,
280*92937b1fSMichal Suchanek interval, nonblock);
2811398252aSHans de Goede }
2821398252aSHans de Goede
create_int_queue(struct usb_device * dev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)2831398252aSHans de Goede struct int_queue *create_int_queue(struct usb_device *dev,
2841398252aSHans de Goede unsigned long pipe, int queuesize, int elementsize,
2851398252aSHans de Goede void *buffer, int interval)
2861398252aSHans de Goede {
287fc85d39eSHans de Goede return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
2881398252aSHans de Goede buffer, interval);
2891398252aSHans de Goede }
2901398252aSHans de Goede
poll_int_queue(struct usb_device * dev,struct int_queue * queue)2911398252aSHans de Goede void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
2921398252aSHans de Goede {
293fc85d39eSHans de Goede return _musb_poll_int_queue(&musb_host, dev, queue);
2941398252aSHans de Goede }
2951398252aSHans de Goede
destroy_int_queue(struct usb_device * dev,struct int_queue * queue)2961398252aSHans de Goede int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
2971398252aSHans de Goede {
298fc85d39eSHans de Goede return _musb_destroy_int_queue(&musb_host, dev, queue);
2991398252aSHans de Goede }
3001398252aSHans de Goede
usb_reset_root_port(struct usb_device * dev)3011398252aSHans de Goede int usb_reset_root_port(struct usb_device *dev)
3021398252aSHans de Goede {
303fc85d39eSHans de Goede return _musb_reset_root_port(&musb_host, dev);
3041398252aSHans de Goede }
3051398252aSHans de Goede
usb_lowlevel_init(int index,enum usb_init_type init,void ** controller)3061398252aSHans de Goede int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
3071398252aSHans de Goede {
308fc85d39eSHans de Goede return musb_lowlevel_init(&musb_host);
3091398252aSHans de Goede }
3103739bf7eSSven Schwermer #endif /* !CONFIG_IS_ENABLED(DM_USB) */
31109e7ea47SHans de Goede
3123739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
musb_submit_control_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)31309e7ea47SHans de Goede static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev,
31409e7ea47SHans de Goede unsigned long pipe, void *buffer, int length,
31509e7ea47SHans de Goede struct devrequest *setup)
31609e7ea47SHans de Goede {
31709e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
31809e7ea47SHans de Goede return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
31909e7ea47SHans de Goede }
32009e7ea47SHans de Goede
musb_submit_bulk_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length)32109e7ea47SHans de Goede static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
32209e7ea47SHans de Goede unsigned long pipe, void *buffer, int length)
32309e7ea47SHans de Goede {
32409e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
32509e7ea47SHans de Goede return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
32609e7ea47SHans de Goede }
32709e7ea47SHans de Goede
musb_submit_int_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)32809e7ea47SHans de Goede static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
32909e7ea47SHans de Goede unsigned long pipe, void *buffer, int length,
330*92937b1fSMichal Suchanek int interval, bool nonblock)
33109e7ea47SHans de Goede {
33209e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
333*92937b1fSMichal Suchanek return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval,
334*92937b1fSMichal Suchanek nonblock);
33509e7ea47SHans de Goede }
33609e7ea47SHans de Goede
musb_create_int_queue(struct udevice * dev,struct usb_device * udev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)33709e7ea47SHans de Goede static struct int_queue *musb_create_int_queue(struct udevice *dev,
33809e7ea47SHans de Goede struct usb_device *udev, unsigned long pipe, int queuesize,
33909e7ea47SHans de Goede int elementsize, void *buffer, int interval)
34009e7ea47SHans de Goede {
34109e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
34209e7ea47SHans de Goede return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
34309e7ea47SHans de Goede buffer, interval);
34409e7ea47SHans de Goede }
34509e7ea47SHans de Goede
musb_poll_int_queue(struct udevice * dev,struct usb_device * udev,struct int_queue * queue)34609e7ea47SHans de Goede static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
34709e7ea47SHans de Goede struct int_queue *queue)
34809e7ea47SHans de Goede {
34909e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
35009e7ea47SHans de Goede return _musb_poll_int_queue(host, udev, queue);
35109e7ea47SHans de Goede }
35209e7ea47SHans de Goede
musb_destroy_int_queue(struct udevice * dev,struct usb_device * udev,struct int_queue * queue)35309e7ea47SHans de Goede static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
35409e7ea47SHans de Goede struct int_queue *queue)
35509e7ea47SHans de Goede {
35609e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
35709e7ea47SHans de Goede return _musb_destroy_int_queue(host, udev, queue);
35809e7ea47SHans de Goede }
35909e7ea47SHans de Goede
musb_reset_root_port(struct udevice * dev,struct usb_device * udev)36009e7ea47SHans de Goede static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
36109e7ea47SHans de Goede {
36209e7ea47SHans de Goede struct musb_host_data *host = dev_get_priv(dev);
36309e7ea47SHans de Goede return _musb_reset_root_port(host, udev);
36409e7ea47SHans de Goede }
36509e7ea47SHans de Goede
36609e7ea47SHans de Goede struct dm_usb_ops musb_usb_ops = {
36709e7ea47SHans de Goede .control = musb_submit_control_msg,
36809e7ea47SHans de Goede .bulk = musb_submit_bulk_msg,
36909e7ea47SHans de Goede .interrupt = musb_submit_int_msg,
37009e7ea47SHans de Goede .create_int_queue = musb_create_int_queue,
37109e7ea47SHans de Goede .poll_int_queue = musb_poll_int_queue,
37209e7ea47SHans de Goede .destroy_int_queue = musb_destroy_int_queue,
37309e7ea47SHans de Goede .reset_root_port = musb_reset_root_port,
37409e7ea47SHans de Goede };
3753739bf7eSSven Schwermer #endif /* CONFIG_IS_ENABLED(DM_USB) */
37695de1e2fSPaul Kocialkowski #endif /* CONFIG_USB_MUSB_HOST */
377eb81955bSIlya Yanok
37895de1e2fSPaul Kocialkowski #ifdef CONFIG_USB_MUSB_GADGET
379eb81955bSIlya Yanok static struct musb *gadget;
380eb81955bSIlya Yanok
usb_gadget_handle_interrupts(int index)3812d48aa69SKishon Vijay Abraham I int usb_gadget_handle_interrupts(int index)
382eb81955bSIlya Yanok {
3837a342785SHeiko Schocher WATCHDOG_RESET();
384eb81955bSIlya Yanok if (!gadget || !gadget->isr)
385eb81955bSIlya Yanok return -EINVAL;
386eb81955bSIlya Yanok
387eb81955bSIlya Yanok return gadget->isr(0, gadget);
388eb81955bSIlya Yanok }
389eb81955bSIlya Yanok
usb_gadget_register_driver(struct usb_gadget_driver * driver)390eb81955bSIlya Yanok int usb_gadget_register_driver(struct usb_gadget_driver *driver)
391eb81955bSIlya Yanok {
392eb81955bSIlya Yanok int ret;
393eb81955bSIlya Yanok
39476b09b85SBin Liu if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
395eb81955bSIlya Yanok !driver->setup) {
396eb81955bSIlya Yanok printf("bad parameter.\n");
397eb81955bSIlya Yanok return -EINVAL;
398eb81955bSIlya Yanok }
399eb81955bSIlya Yanok
400eb81955bSIlya Yanok if (!gadget) {
401eb81955bSIlya Yanok printf("Controller uninitialized\n");
402eb81955bSIlya Yanok return -ENXIO;
403eb81955bSIlya Yanok }
404eb81955bSIlya Yanok
405eb81955bSIlya Yanok ret = musb_gadget_start(&gadget->g, driver);
406eb81955bSIlya Yanok if (ret < 0) {
407eb81955bSIlya Yanok printf("gadget_start failed with %d\n", ret);
408eb81955bSIlya Yanok return ret;
409eb81955bSIlya Yanok }
410eb81955bSIlya Yanok
411eb81955bSIlya Yanok ret = driver->bind(&gadget->g);
412eb81955bSIlya Yanok if (ret < 0) {
413eb81955bSIlya Yanok printf("bind failed with %d\n", ret);
414eb81955bSIlya Yanok return ret;
415eb81955bSIlya Yanok }
416eb81955bSIlya Yanok
417eb81955bSIlya Yanok return 0;
418eb81955bSIlya Yanok }
419eb81955bSIlya Yanok
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)420eb81955bSIlya Yanok int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
421eb81955bSIlya Yanok {
422078d7302SRob Herring if (driver->disconnect)
423078d7302SRob Herring driver->disconnect(&gadget->g);
424078d7302SRob Herring if (driver->unbind)
425078d7302SRob Herring driver->unbind(&gadget->g);
426eb81955bSIlya Yanok return 0;
427eb81955bSIlya Yanok }
42895de1e2fSPaul Kocialkowski #endif /* CONFIG_USB_MUSB_GADGET */
429eb81955bSIlya Yanok
musb_register(struct musb_hdrc_platform_data * plat,void * bdata,void * ctl_regs)430eb81955bSIlya Yanok int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
431eb81955bSIlya Yanok void *ctl_regs)
432eb81955bSIlya Yanok {
433eb81955bSIlya Yanok struct musb **musbp;
434eb81955bSIlya Yanok
435eb81955bSIlya Yanok switch (plat->mode) {
4363739bf7eSSven Schwermer #if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
437eb81955bSIlya Yanok case MUSB_HOST:
438fc85d39eSHans de Goede musbp = &musb_host.host;
439eb81955bSIlya Yanok break;
440eb81955bSIlya Yanok #endif
44195de1e2fSPaul Kocialkowski #ifdef CONFIG_USB_MUSB_GADGET
442eb81955bSIlya Yanok case MUSB_PERIPHERAL:
443eb81955bSIlya Yanok musbp = &gadget;
444eb81955bSIlya Yanok break;
445eb81955bSIlya Yanok #endif
446eb81955bSIlya Yanok default:
447eb81955bSIlya Yanok return -EINVAL;
448eb81955bSIlya Yanok }
449eb81955bSIlya Yanok
450eb81955bSIlya Yanok *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
4512511b2edSHeinrich Schuchardt if (!*musbp) {
452eb81955bSIlya Yanok printf("Failed to init the controller\n");
453eb81955bSIlya Yanok return -EIO;
454eb81955bSIlya Yanok }
455eb81955bSIlya Yanok
456eb81955bSIlya Yanok return 0;
457eb81955bSIlya Yanok }
458