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