musb: Add musb_host_data struct to hold global data
[platform/kernel/u-boot.git] / drivers / usb / musb-new / musb_uboot.c
1 #include <common.h>
2 #include <watchdog.h>
3 #ifdef CONFIG_ARCH_SUNXI
4 #include <asm/arch/usb_phy.h>
5 #endif
6 #include <asm/errno.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/usb/gadget.h>
9
10 #include <usb.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"
16 #include "musb_uboot.h"
17
18 #ifdef CONFIG_MUSB_HOST
19 struct int_queue {
20         struct usb_host_endpoint hep;
21         struct urb urb;
22 };
23
24 struct musb_host_data musb_host;
25
26 static void musb_host_complete_urb(struct urb *urb)
27 {
28         urb->dev->status &= ~USB_ST_NOT_PROC;
29         urb->dev->act_len = urb->actual_length;
30 }
31
32 static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
33                           struct usb_device *dev, int endpoint_type,
34                           unsigned long pipe, void *buffer, int len,
35                           struct devrequest *setup, int interval)
36 {
37         int epnum = usb_pipeendpoint(pipe);
38         int is_in = usb_pipein(pipe);
39
40         memset(urb, 0, sizeof(struct urb));
41         memset(hep, 0, sizeof(struct usb_host_endpoint));
42         INIT_LIST_HEAD(&hep->urb_list);
43         INIT_LIST_HEAD(&urb->urb_list);
44         urb->ep = hep;
45         urb->complete = musb_host_complete_urb;
46         urb->status = -EINPROGRESS;
47         urb->dev = dev;
48         urb->pipe = pipe;
49         urb->transfer_buffer = buffer;
50         urb->transfer_dma = (unsigned long)buffer;
51         urb->transfer_buffer_length = len;
52         urb->setup_packet = (unsigned char *)setup;
53
54         urb->ep->desc.wMaxPacketSize =
55                 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
56                                 dev->epmaxpacketout[epnum]);
57         urb->ep->desc.bmAttributes = endpoint_type;
58         urb->ep->desc.bEndpointAddress =
59                 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
60         urb->ep->desc.bInterval = interval;
61 }
62
63 static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
64 {
65         struct musb *host = hcd->hcd_priv;
66         int ret;
67         unsigned long timeout;
68
69         ret = musb_urb_enqueue(hcd, urb, 0);
70         if (ret < 0) {
71                 printf("Failed to enqueue URB to controller\n");
72                 return ret;
73         }
74
75         timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
76         do {
77                 if (ctrlc())
78                         return -EIO;
79                 host->isr(0, host);
80         } while (urb->status == -EINPROGRESS &&
81                  get_timer(0) < timeout);
82
83         if (urb->status == -EINPROGRESS)
84                 musb_urb_dequeue(hcd, urb, -ETIME);
85
86         return urb->status;
87 }
88
89 static int _musb_submit_control_msg(struct musb_host_data *host,
90         struct usb_device *dev, unsigned long pipe,
91         void *buffer, int len, struct devrequest *setup)
92 {
93         construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
94                       pipe, buffer, len, setup, 0);
95
96         /* Fix speed for non hub-attached devices */
97         if (!usb_dev_get_parent(dev))
98                 dev->speed = host->host_speed;
99
100         return submit_urb(&host->hcd, &host->urb);
101 }
102
103 static int _musb_submit_bulk_msg(struct musb_host_data *host,
104         struct usb_device *dev, unsigned long pipe, void *buffer, int len)
105 {
106         construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
107                       pipe, buffer, len, NULL, 0);
108         return submit_urb(&host->hcd, &host->urb);
109 }
110
111 static int _musb_submit_int_msg(struct musb_host_data *host,
112         struct usb_device *dev, unsigned long pipe,
113         void *buffer, int len, int interval)
114 {
115         construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
116                       buffer, len, NULL, interval);
117         return submit_urb(&host->hcd, &host->urb);
118 }
119
120 static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
121         struct usb_device *dev, unsigned long pipe, int queuesize,
122         int elementsize, void *buffer, int interval)
123 {
124         struct int_queue *queue;
125         int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
126
127         if (queuesize != 1) {
128                 printf("ERROR musb int-queues only support queuesize 1\n");
129                 return NULL;
130         }
131
132         if (dev->int_pending & (1 << index)) {
133                 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
134                 return NULL;
135         }
136
137         queue = malloc(sizeof(*queue));
138         if (!queue)
139                 return NULL;
140
141         construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
142                       pipe, buffer, elementsize, NULL, interval);
143
144         ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
145         if (ret < 0) {
146                 printf("Failed to enqueue URB to controller\n");
147                 free(queue);
148                 return NULL;
149         }
150
151         dev->int_pending |= 1 << index;
152         return queue;
153 }
154
155 static int _musb_destroy_int_queue(struct musb_host_data *host,
156         struct usb_device *dev, struct int_queue *queue)
157 {
158         int index = usb_pipein(queue->urb.pipe) * 16 + 
159                     usb_pipeendpoint(queue->urb.pipe);
160
161         if (queue->urb.status == -EINPROGRESS)
162                 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
163
164         dev->int_pending &= ~(1 << index);
165         free(queue);
166         return 0;
167 }
168
169 static void *_musb_poll_int_queue(struct musb_host_data *host,
170         struct usb_device *dev, struct int_queue *queue)
171 {
172         if (queue->urb.status != -EINPROGRESS)
173                 return NULL; /* URB has already completed in a prev. poll */
174
175         host->host->isr(0, host->host);
176
177         if (queue->urb.status != -EINPROGRESS)
178                 return queue->urb.transfer_buffer; /* Done */
179
180         return NULL; /* URB still pending */
181 }
182
183 static int _musb_reset_root_port(struct musb_host_data *host,
184         struct usb_device *dev)
185 {
186         void *mbase = host->host->mregs;
187         u8 power;
188
189         power = musb_readb(mbase, MUSB_POWER);
190         power &= 0xf0;
191         musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
192         mdelay(50);
193 #ifdef CONFIG_ARCH_SUNXI
194         /*
195          * sunxi phy has a bug and it will wrongly detect high speed squelch
196          * when clearing reset on low-speed devices, temporary disable
197          * squelch detection to work around this.
198          */
199         sunxi_usb_phy_enable_squelch_detect(0, 0);
200 #endif
201         power = musb_readb(mbase, MUSB_POWER);
202         musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
203 #ifdef CONFIG_ARCH_SUNXI
204         sunxi_usb_phy_enable_squelch_detect(0, 1);
205 #endif
206         host->host->isr(0, host->host);
207         host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
208                         USB_SPEED_HIGH :
209                         (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
210                         USB_SPEED_FULL : USB_SPEED_LOW;
211         mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
212
213         return 0;
214 }
215
216 int musb_lowlevel_init(struct musb_host_data *host)
217 {
218         void *mbase;
219         /* USB spec says it may take up to 1 second for a device to connect */
220         unsigned long timeout = get_timer(0) + 1000;
221         int ret;
222
223         if (!host->host) {
224                 printf("MUSB host is not registered\n");
225                 return -ENODEV;
226         }
227
228         ret = musb_start(host->host);
229         if (ret)
230                 return ret;
231
232         mbase = host->host->mregs;
233         do {
234                 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
235                         break;
236         } while (get_timer(0) < timeout);
237         if (get_timer(0) >= timeout)
238                 return -ENODEV;
239
240         _musb_reset_root_port(host, NULL);
241         host->host->is_active = 1;
242         host->hcd.hcd_priv = host->host;
243
244         return 0;
245 }
246
247 int usb_lowlevel_stop(int index)
248 {
249         if (!musb_host.host) {
250                 printf("MUSB host is not registered\n");
251                 return -ENODEV;
252         }
253
254         musb_stop(musb_host.host);
255         return 0;
256 }
257
258 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
259                             void *buffer, int length)
260 {
261         return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
262 }
263
264 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
265                        void *buffer, int length, struct devrequest *setup)
266 {
267         return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
268 }
269
270 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
271                    void *buffer, int length, int interval)
272 {
273         return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, interval);
274 }
275
276 struct int_queue *create_int_queue(struct usb_device *dev,
277                 unsigned long pipe, int queuesize, int elementsize,
278                 void *buffer, int interval)
279 {
280         return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
281                                       buffer, interval);
282 }
283
284 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
285 {
286         return _musb_poll_int_queue(&musb_host, dev, queue);
287 }
288
289 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
290 {
291         return _musb_destroy_int_queue(&musb_host, dev, queue);
292 }
293
294 int usb_reset_root_port(struct usb_device *dev)
295 {
296         return _musb_reset_root_port(&musb_host, dev);
297 }
298
299 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
300 {
301         return musb_lowlevel_init(&musb_host);
302 }
303 #endif /* CONFIG_MUSB_HOST */
304
305 #ifdef CONFIG_MUSB_GADGET
306 static struct musb *gadget;
307
308 int usb_gadget_handle_interrupts(int index)
309 {
310         WATCHDOG_RESET();
311         if (!gadget || !gadget->isr)
312                 return -EINVAL;
313
314         return gadget->isr(0, gadget);
315 }
316
317 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
318 {
319         int ret;
320
321         if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
322             !driver->setup) {
323                 printf("bad parameter.\n");
324                 return -EINVAL;
325         }
326
327         if (!gadget) {
328                 printf("Controller uninitialized\n");
329                 return -ENXIO;
330         }
331
332         ret = musb_gadget_start(&gadget->g, driver);
333         if (ret < 0) {
334                 printf("gadget_start failed with %d\n", ret);
335                 return ret;
336         }
337
338         ret = driver->bind(&gadget->g);
339         if (ret < 0) {
340                 printf("bind failed with %d\n", ret);
341                 return ret;
342         }
343
344         return 0;
345 }
346
347 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
348 {
349         if (driver->disconnect)
350                 driver->disconnect(&gadget->g);
351         if (driver->unbind)
352                 driver->unbind(&gadget->g);
353         return 0;
354 }
355 #endif /* CONFIG_MUSB_GADGET */
356
357 int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
358                         void *ctl_regs)
359 {
360         struct musb **musbp;
361
362         switch (plat->mode) {
363 #ifdef CONFIG_MUSB_HOST
364         case MUSB_HOST:
365                 musbp = &musb_host.host;
366                 break;
367 #endif
368 #ifdef CONFIG_MUSB_GADGET
369         case MUSB_PERIPHERAL:
370                 musbp = &gadget;
371                 break;
372 #endif
373         default:
374                 return -EINVAL;
375         }
376
377         *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
378         if (!musbp) {
379                 printf("Failed to init the controller\n");
380                 return -EIO;
381         }
382
383         return 0;
384 }