musb-new: Properly remove a transfer from the schedule on timeout
[platform/kernel/u-boot.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 static struct musb *host;
16 static struct usb_hcd hcd;
17 static enum usb_device_speed host_speed;
18
19 static 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
25 static struct usb_host_endpoint hep;
26 static struct urb urb;
27
28 static struct urb *construct_urb(struct usb_device *dev, int endpoint_type,
29                                 unsigned long pipe, void *buffer, int len,
30                                 struct devrequest *setup, int interval)
31 {
32         int epnum = usb_pipeendpoint(pipe);
33         int is_in = usb_pipein(pipe);
34
35         memset(&urb, 0, sizeof(struct urb));
36         memset(&hep, 0, sizeof(struct usb_host_endpoint));
37         INIT_LIST_HEAD(&hep.urb_list);
38         INIT_LIST_HEAD(&urb.urb_list);
39         urb.ep = &hep;
40         urb.complete = musb_host_complete_urb;
41         urb.status = -EINPROGRESS;
42         urb.dev = dev;
43         urb.pipe = pipe;
44         urb.transfer_buffer = buffer;
45         urb.transfer_dma = (unsigned long)buffer;
46         urb.transfer_buffer_length = len;
47         urb.setup_packet = (unsigned char *)setup;
48
49         urb.ep->desc.wMaxPacketSize =
50                 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
51                                 dev->epmaxpacketout[epnum]);
52         urb.ep->desc.bmAttributes = endpoint_type;
53         urb.ep->desc.bEndpointAddress =
54                 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
55         urb.ep->desc.bInterval = interval;
56
57         return &urb;
58 }
59
60 static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
61 {
62         struct musb *host = hcd->hcd_priv;
63         int ret;
64         unsigned long timeout;
65
66         ret = musb_urb_enqueue(hcd, urb, 0);
67         if (ret < 0) {
68                 printf("Failed to enqueue URB to controller\n");
69                 return ret;
70         }
71
72         timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
73         do {
74                 if (ctrlc())
75                         return -EIO;
76                 host->isr(0, host);
77         } while (urb->status == -EINPROGRESS &&
78                  get_timer(0) < timeout);
79
80         if (urb->status == -EINPROGRESS)
81                 musb_urb_dequeue(hcd, urb, -ETIME);
82
83         return urb->status;
84 }
85
86 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
87                         void *buffer, int len, struct devrequest *setup)
88 {
89         struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_CONTROL, pipe,
90                                         buffer, len, setup, 0);
91
92         /* Fix speed for non hub-attached devices */
93         if (!dev->parent)
94                 dev->speed = host_speed;
95
96         return submit_urb(&hcd, urb);
97 }
98
99
100 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
101                                         void *buffer, int len)
102 {
103         struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_BULK, pipe,
104                                         buffer, len, NULL, 0);
105         return submit_urb(&hcd, urb);
106 }
107
108 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
109                                 void *buffer, int len, int interval)
110 {
111         struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_INT, pipe,
112                                         buffer, len, NULL, interval);
113         return submit_urb(&hcd, urb);
114 }
115
116 void usb_reset_root_port(void)
117 {
118         void *mbase = host->mregs;
119         u8 power;
120
121         power = musb_readb(mbase, MUSB_POWER);
122         power &= 0xf0;
123         musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
124         mdelay(50);
125         power = musb_readb(mbase, MUSB_POWER);
126         musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
127         host->isr(0, host);
128         host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
129                         USB_SPEED_HIGH :
130                         (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
131                         USB_SPEED_FULL : USB_SPEED_LOW;
132         mdelay((host_speed == USB_SPEED_LOW) ? 200 : 50);
133 }
134
135 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
136 {
137         void *mbase;
138         /* USB spec says it may take up to 1 second for a device to connect */
139         unsigned long timeout = get_timer(0) + 1000;
140
141         if (!host) {
142                 printf("MUSB host is not registered\n");
143                 return -ENODEV;
144         }
145
146         musb_start(host);
147         mbase = host->mregs;
148         do {
149                 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
150                         break;
151         } while (get_timer(0) < timeout);
152         if (get_timer(0) >= timeout)
153                 return -ENODEV;
154
155         usb_reset_root_port();
156         host->is_active = 1;
157         hcd.hcd_priv = host;
158
159         return 0;
160 }
161
162 int usb_lowlevel_stop(int index)
163 {
164         if (!host) {
165                 printf("MUSB host is not registered\n");
166                 return -ENODEV;
167         }
168
169         musb_stop(host);
170         return 0;
171 }
172 #endif /* CONFIG_MUSB_HOST */
173
174 #ifdef CONFIG_MUSB_GADGET
175 static struct musb *gadget;
176
177 int usb_gadget_handle_interrupts(void)
178 {
179         WATCHDOG_RESET();
180         if (!gadget || !gadget->isr)
181                 return -EINVAL;
182
183         return gadget->isr(0, gadget);
184 }
185
186 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
187 {
188         int ret;
189
190         if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
191             !driver->setup) {
192                 printf("bad parameter.\n");
193                 return -EINVAL;
194         }
195
196         if (!gadget) {
197                 printf("Controller uninitialized\n");
198                 return -ENXIO;
199         }
200
201         ret = musb_gadget_start(&gadget->g, driver);
202         if (ret < 0) {
203                 printf("gadget_start failed with %d\n", ret);
204                 return ret;
205         }
206
207         ret = driver->bind(&gadget->g);
208         if (ret < 0) {
209                 printf("bind failed with %d\n", ret);
210                 return ret;
211         }
212
213         return 0;
214 }
215
216 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
217 {
218         if (driver->disconnect)
219                 driver->disconnect(&gadget->g);
220         if (driver->unbind)
221                 driver->unbind(&gadget->g);
222         return 0;
223 }
224 #endif /* CONFIG_MUSB_GADGET */
225
226 int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
227                         void *ctl_regs)
228 {
229         struct musb **musbp;
230
231         switch (plat->mode) {
232 #ifdef CONFIG_MUSB_HOST
233         case MUSB_HOST:
234                 musbp = &host;
235                 break;
236 #endif
237 #ifdef CONFIG_MUSB_GADGET
238         case MUSB_PERIPHERAL:
239                 musbp = &gadget;
240                 break;
241 #endif
242         default:
243                 return -EINVAL;
244         }
245
246         *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
247         if (!musbp) {
248                 printf("Failed to init the controller\n");
249                 return -EIO;
250         }
251
252         return 0;
253 }