blob: 3f9a98c9070a9faf8a6d55256179fe39c7bd333c [file] [log] [blame]
Ilya Yanokeb819552012-11-06 13:48:21 +00001#include <common.h>
Heiko Schocher7a342782013-06-04 11:10:01 +02002#include <watchdog.h>
Ilya Yanokeb819552012-11-06 13:48:21 +00003#include <asm/errno.h>
4#include <linux/usb/ch9.h>
5#include <linux/usb/gadget.h>
6
Ilya Yanokeb819552012-11-06 13:48:21 +00007#include <usb.h>
8#include "linux-compat.h"
9#include "usb-compat.h"
10#include "musb_core.h"
11#include "musb_host.h"
12#include "musb_gadget.h"
13
14#ifdef CONFIG_MUSB_HOST
15static struct musb *host;
16static struct usb_hcd hcd;
17static enum usb_device_speed host_speed;
18
19static void musb_host_complete_urb(struct urb *urb)
20{
21 urb->dev->status &= ~USB_ST_NOT_PROC;
22 urb->dev->act_len = urb->actual_length;
23}
24
25static struct usb_host_endpoint hep;
26static struct urb urb;
27
Hans de Goedeaccf04c2015-01-11 20:34:53 +010028static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
29 struct usb_device *dev, int endpoint_type,
30 unsigned long pipe, void *buffer, int len,
31 struct devrequest *setup, int interval)
Ilya Yanokeb819552012-11-06 13:48:21 +000032{
33 int epnum = usb_pipeendpoint(pipe);
34 int is_in = usb_pipein(pipe);
35
Hans de Goedeaccf04c2015-01-11 20:34:53 +010036 memset(urb, 0, sizeof(struct urb));
37 memset(hep, 0, sizeof(struct usb_host_endpoint));
38 INIT_LIST_HEAD(&hep->urb_list);
39 INIT_LIST_HEAD(&urb->urb_list);
40 urb->ep = hep;
41 urb->complete = musb_host_complete_urb;
42 urb->status = -EINPROGRESS;
43 urb->dev = dev;
44 urb->pipe = pipe;
45 urb->transfer_buffer = buffer;
46 urb->transfer_dma = (unsigned long)buffer;
47 urb->transfer_buffer_length = len;
48 urb->setup_packet = (unsigned char *)setup;
Ilya Yanokeb819552012-11-06 13:48:21 +000049
Hans de Goedeaccf04c2015-01-11 20:34:53 +010050 urb->ep->desc.wMaxPacketSize =
Ilya Yanokeb819552012-11-06 13:48:21 +000051 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
52 dev->epmaxpacketout[epnum]);
Hans de Goedeaccf04c2015-01-11 20:34:53 +010053 urb->ep->desc.bmAttributes = endpoint_type;
54 urb->ep->desc.bEndpointAddress =
Ilya Yanokeb819552012-11-06 13:48:21 +000055 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
Hans de Goedeaccf04c2015-01-11 20:34:53 +010056 urb->ep->desc.bInterval = interval;
Ilya Yanokeb819552012-11-06 13:48:21 +000057}
58
Ilya Yanokeb819552012-11-06 13:48:21 +000059static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
60{
61 struct musb *host = hcd->hcd_priv;
62 int ret;
Hans de Goededc9a3912015-01-11 20:34:49 +010063 unsigned long timeout;
Ilya Yanokeb819552012-11-06 13:48:21 +000064
65 ret = musb_urb_enqueue(hcd, urb, 0);
66 if (ret < 0) {
67 printf("Failed to enqueue URB to controller\n");
68 return ret;
69 }
70
Hans de Goededc9a3912015-01-11 20:34:49 +010071 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
Ilya Yanokeb819552012-11-06 13:48:21 +000072 do {
73 if (ctrlc())
74 return -EIO;
75 host->isr(0, host);
Hans de Goedee8672e32015-01-11 20:34:50 +010076 } while (urb->status == -EINPROGRESS &&
Hans de Goededc9a3912015-01-11 20:34:49 +010077 get_timer(0) < timeout);
Ilya Yanokeb819552012-11-06 13:48:21 +000078
Hans de Goedeb918a0c2015-01-11 20:34:52 +010079 if (urb->status == -EINPROGRESS)
80 musb_urb_dequeue(hcd, urb, -ETIME);
81
Ilya Yanokeb819552012-11-06 13:48:21 +000082 return urb->status;
83}
84
85int submit_control_msg(struct usb_device *dev, unsigned long pipe,
86 void *buffer, int len, struct devrequest *setup)
87{
Hans de Goedeaccf04c2015-01-11 20:34:53 +010088 construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_CONTROL, pipe,
89 buffer, len, setup, 0);
Ilya Yanokeb819552012-11-06 13:48:21 +000090
91 /* Fix speed for non hub-attached devices */
92 if (!dev->parent)
93 dev->speed = host_speed;
94
Hans de Goedeaccf04c2015-01-11 20:34:53 +010095 return submit_urb(&hcd, &urb);
Ilya Yanokeb819552012-11-06 13:48:21 +000096}
97
98
99int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
100 void *buffer, int len)
101{
Hans de Goedeaccf04c2015-01-11 20:34:53 +0100102 construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_BULK, pipe,
103 buffer, len, NULL, 0);
104 return submit_urb(&hcd, &urb);
Ilya Yanokeb819552012-11-06 13:48:21 +0000105}
106
107int submit_int_msg(struct usb_device *dev, unsigned long pipe,
108 void *buffer, int len, int interval)
109{
Hans de Goedeaccf04c2015-01-11 20:34:53 +0100110 construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_INT, pipe,
111 buffer, len, NULL, interval);
112 return submit_urb(&hcd, &urb);
Ilya Yanokeb819552012-11-06 13:48:21 +0000113}
114
Hans de Goede90cdc102015-01-11 20:34:51 +0100115void usb_reset_root_port(void)
116{
117 void *mbase = host->mregs;
118 u8 power;
119
120 power = musb_readb(mbase, MUSB_POWER);
121 power &= 0xf0;
122 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
123 mdelay(50);
124 power = musb_readb(mbase, MUSB_POWER);
125 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
126 host->isr(0, host);
127 host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
128 USB_SPEED_HIGH :
129 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
130 USB_SPEED_FULL : USB_SPEED_LOW;
131 mdelay((host_speed == USB_SPEED_LOW) ? 200 : 50);
132}
133
Troy Kisky06d513e2013-10-10 15:27:56 -0700134int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Ilya Yanokeb819552012-11-06 13:48:21 +0000135{
Ilya Yanokeb819552012-11-06 13:48:21 +0000136 void *mbase;
Hans de Goededc9a3912015-01-11 20:34:49 +0100137 /* USB spec says it may take up to 1 second for a device to connect */
138 unsigned long timeout = get_timer(0) + 1000;
Ilya Yanokeb819552012-11-06 13:48:21 +0000139
140 if (!host) {
141 printf("MUSB host is not registered\n");
142 return -ENODEV;
143 }
144
145 musb_start(host);
146 mbase = host->mregs;
147 do {
148 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
149 break;
Hans de Goededc9a3912015-01-11 20:34:49 +0100150 } while (get_timer(0) < timeout);
151 if (get_timer(0) >= timeout)
Ilya Yanokeb819552012-11-06 13:48:21 +0000152 return -ENODEV;
153
Hans de Goede90cdc102015-01-11 20:34:51 +0100154 usb_reset_root_port();
Ilya Yanokeb819552012-11-06 13:48:21 +0000155 host->is_active = 1;
156 hcd.hcd_priv = host;
157
158 return 0;
159}
160
161int usb_lowlevel_stop(int index)
162{
163 if (!host) {
164 printf("MUSB host is not registered\n");
165 return -ENODEV;
166 }
167
168 musb_stop(host);
169 return 0;
170}
171#endif /* CONFIG_MUSB_HOST */
172
173#ifdef CONFIG_MUSB_GADGET
174static struct musb *gadget;
175
176int usb_gadget_handle_interrupts(void)
177{
Heiko Schocher7a342782013-06-04 11:10:01 +0200178 WATCHDOG_RESET();
Ilya Yanokeb819552012-11-06 13:48:21 +0000179 if (!gadget || !gadget->isr)
180 return -EINVAL;
181
182 return gadget->isr(0, gadget);
183}
184
185int usb_gadget_register_driver(struct usb_gadget_driver *driver)
186{
187 int ret;
188
Bin Liu76b09b82013-03-21 05:27:49 +0000189 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
Ilya Yanokeb819552012-11-06 13:48:21 +0000190 !driver->setup) {
191 printf("bad parameter.\n");
192 return -EINVAL;
193 }
194
195 if (!gadget) {
196 printf("Controller uninitialized\n");
197 return -ENXIO;
198 }
199
200 ret = musb_gadget_start(&gadget->g, driver);
201 if (ret < 0) {
202 printf("gadget_start failed with %d\n", ret);
203 return ret;
204 }
205
206 ret = driver->bind(&gadget->g);
207 if (ret < 0) {
208 printf("bind failed with %d\n", ret);
209 return ret;
210 }
211
212 return 0;
213}
214
215int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
216{
Rob Herring078d7302014-04-18 08:54:30 -0500217 if (driver->disconnect)
218 driver->disconnect(&gadget->g);
219 if (driver->unbind)
220 driver->unbind(&gadget->g);
Ilya Yanokeb819552012-11-06 13:48:21 +0000221 return 0;
222}
223#endif /* CONFIG_MUSB_GADGET */
224
225int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
226 void *ctl_regs)
227{
228 struct musb **musbp;
229
230 switch (plat->mode) {
231#ifdef CONFIG_MUSB_HOST
232 case MUSB_HOST:
233 musbp = &host;
234 break;
235#endif
236#ifdef CONFIG_MUSB_GADGET
237 case MUSB_PERIPHERAL:
238 musbp = &gadget;
239 break;
240#endif
241 default:
242 return -EINVAL;
243 }
244
245 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
246 if (!musbp) {
247 printf("Failed to init the controller\n");
248 return -EIO;
249 }
250
251 return 0;
252}