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