Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 5 | * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved. |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 6 | * |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following |
| 14 | * disclaimer in the documentation and/or other materials provided |
| 15 | * with the distribution. |
| 16 | * * Neither the name of The Linux Foundation nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 19 | * |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 20 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <string.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <debug.h> |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 36 | #include <platform.h> |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 37 | #include <platform/iomap.h> |
| 38 | #include <platform/irqs.h> |
| 39 | #include <platform/interrupts.h> |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 40 | #include <platform/timer.h> |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 41 | #include <kernel/thread.h> |
| 42 | #include <reg.h> |
| 43 | |
| 44 | #include <dev/udc.h> |
| 45 | |
| 46 | #include "hsusb.h" |
| 47 | |
| 48 | /* common code - factor out into a shared file */ |
| 49 | |
| 50 | struct udc_descriptor { |
| 51 | struct udc_descriptor *next; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 52 | unsigned short tag; /* ((TYPE << 8) | NUM) */ |
| 53 | unsigned short len; /* total length */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 54 | unsigned char data[0]; |
| 55 | }; |
| 56 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 57 | struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num, |
| 58 | unsigned len) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 59 | { |
| 60 | struct udc_descriptor *desc; |
| 61 | if ((len > 255) || (len < 2) || (num > 255) || (type > 255)) |
| 62 | return 0; |
| 63 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 64 | if (!(desc = malloc(sizeof(struct udc_descriptor) + len))) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | |
| 67 | desc->next = 0; |
| 68 | desc->tag = (type << 8) | num; |
| 69 | desc->len = len; |
| 70 | desc->data[0] = len; |
| 71 | desc->data[1] = type; |
| 72 | |
| 73 | return desc; |
| 74 | } |
| 75 | |
| 76 | static struct udc_descriptor *desc_list = 0; |
| 77 | static unsigned next_string_id = 1; |
| 78 | |
| 79 | void udc_descriptor_register(struct udc_descriptor *desc) |
| 80 | { |
| 81 | desc->next = desc_list; |
| 82 | desc_list = desc; |
| 83 | } |
| 84 | |
| 85 | unsigned udc_string_desc_alloc(const char *str) |
| 86 | { |
| 87 | unsigned len; |
| 88 | struct udc_descriptor *desc; |
| 89 | unsigned char *data; |
| 90 | |
| 91 | if (next_string_id > 255) |
| 92 | return 0; |
| 93 | |
| 94 | if (!str) |
| 95 | return 0; |
| 96 | |
| 97 | len = strlen(str); |
| 98 | desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2); |
| 99 | if (!desc) |
| 100 | return 0; |
| 101 | next_string_id++; |
| 102 | |
| 103 | /* expand ascii string to utf16 */ |
| 104 | data = desc->data + 2; |
| 105 | while (len-- > 0) { |
| 106 | *data++ = *str++; |
| 107 | *data++ = 0; |
| 108 | } |
| 109 | |
| 110 | udc_descriptor_register(desc); |
| 111 | return desc->tag & 0xff; |
| 112 | } |
| 113 | |
| 114 | /* end of common code */ |
| 115 | |
Ajay Dudani | 7d60552 | 2010-10-01 19:52:37 -0700 | [diff] [blame] | 116 | __WEAK void hsusb_clock_init(void) |
| 117 | { |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 118 | return; |
Ajay Dudani | 7d60552 | 2010-10-01 19:52:37 -0700 | [diff] [blame] | 119 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 120 | |
| 121 | #if 1 |
| 122 | #define DBG(x...) do {} while(0) |
| 123 | #else |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 124 | #define DBG(x...) dprintf(ALWAYS, x) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 125 | #endif |
| 126 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 127 | #define usb_status(a,b) |
| 128 | |
| 129 | struct usb_request { |
| 130 | struct udc_request req; |
| 131 | struct ept_queue_item *item; |
| 132 | }; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 133 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 134 | struct udc_endpoint { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 135 | struct udc_endpoint *next; |
| 136 | unsigned bit; |
| 137 | struct ept_queue_head *head; |
| 138 | struct usb_request *req; |
| 139 | unsigned char num; |
| 140 | unsigned char in; |
| 141 | unsigned short maxpkt; |
| 142 | }; |
| 143 | |
| 144 | struct udc_endpoint *ept_list = 0; |
| 145 | struct ept_queue_head *epts = 0; |
| 146 | |
| 147 | static int usb_online = 0; |
| 148 | static int usb_highspeed = 0; |
| 149 | |
| 150 | static struct udc_device *the_device; |
| 151 | static struct udc_gadget *the_gadget; |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 152 | static unsigned test_mode = 0; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 153 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 154 | struct udc_endpoint *_udc_endpoint_alloc(unsigned num, unsigned in, |
| 155 | unsigned max_pkt) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 156 | { |
| 157 | struct udc_endpoint *ept; |
| 158 | unsigned cfg; |
| 159 | |
| 160 | ept = malloc(sizeof(*ept)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 161 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 162 | ept->maxpkt = max_pkt; |
| 163 | ept->num = num; |
| 164 | ept->in = !!in; |
| 165 | ept->req = 0; |
| 166 | |
| 167 | cfg = CONFIG_MAX_PKT(max_pkt) | CONFIG_ZLT; |
| 168 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 169 | if (ept->in) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 170 | ept->bit = EPT_TX(ept->num); |
| 171 | } else { |
| 172 | ept->bit = EPT_RX(ept->num); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 173 | if (num == 0) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 174 | cfg |= CONFIG_IOS; |
| 175 | } |
| 176 | |
| 177 | ept->head = epts + (num * 2) + (ept->in); |
| 178 | ept->head->config = cfg; |
| 179 | |
| 180 | ept->next = ept_list; |
| 181 | ept_list = ept; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 182 | |
| 183 | DBG("ept%d %s @%p/%p max=%d bit=%x\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 184 | num, in ? "in" : "out", ept, ept->head, max_pkt, ept->bit); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 185 | |
| 186 | return ept; |
| 187 | } |
| 188 | |
| 189 | static unsigned ept_alloc_table = EPT_TX(0) | EPT_RX(0); |
| 190 | |
| 191 | struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt) |
| 192 | { |
| 193 | struct udc_endpoint *ept; |
| 194 | unsigned n; |
| 195 | unsigned in; |
| 196 | |
| 197 | if (type == UDC_TYPE_BULK_IN) { |
| 198 | in = 1; |
| 199 | } else if (type == UDC_TYPE_BULK_OUT) { |
| 200 | in = 0; |
| 201 | } else { |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | for (n = 1; n < 16; n++) { |
| 206 | unsigned bit = in ? EPT_TX(n) : EPT_RX(n); |
| 207 | if (ept_alloc_table & bit) |
| 208 | continue; |
| 209 | ept = _udc_endpoint_alloc(n, in, maxpkt); |
| 210 | if (ept) |
| 211 | ept_alloc_table |= bit; |
| 212 | return ept; |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | void udc_endpoint_free(struct udc_endpoint *ept) |
| 218 | { |
| 219 | /* todo */ |
| 220 | } |
| 221 | |
| 222 | static void endpoint_enable(struct udc_endpoint *ept, unsigned yes) |
| 223 | { |
| 224 | unsigned n = readl(USB_ENDPTCTRL(ept->num)); |
| 225 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 226 | if (yes) { |
| 227 | if (ept->in) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 228 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 229 | } else { |
| 230 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 231 | } |
| 232 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 233 | if (ept->num != 0) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 234 | /* XXX should be more dynamic... */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 235 | if (usb_highspeed) { |
| 236 | ept->head->config = |
| 237 | CONFIG_MAX_PKT(512) | CONFIG_ZLT; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 238 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 239 | ept->head->config = |
| 240 | CONFIG_MAX_PKT(64) | CONFIG_ZLT; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
| 244 | writel(n, USB_ENDPTCTRL(ept->num)); |
| 245 | } |
| 246 | |
| 247 | struct udc_request *udc_request_alloc(void) |
| 248 | { |
| 249 | struct usb_request *req; |
| 250 | req = malloc(sizeof(*req)); |
| 251 | req->req.buf = 0; |
| 252 | req->req.length = 0; |
| 253 | req->item = memalign(32, 32); |
| 254 | return &req->req; |
| 255 | } |
| 256 | |
| 257 | void udc_request_free(struct udc_request *req) |
| 258 | { |
| 259 | free(req); |
| 260 | } |
| 261 | |
| 262 | int udc_request_queue(struct udc_endpoint *ept, struct udc_request *_req) |
| 263 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 264 | struct usb_request *req = (struct usb_request *)_req; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 265 | struct ept_queue_item *item = req->item; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 266 | unsigned phys = (unsigned)req->req.buf; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 267 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 268 | item->next = TERMINATE; |
| 269 | item->info = INFO_BYTES(req->req.length) | INFO_IOC | INFO_ACTIVE; |
| 270 | item->page0 = phys; |
| 271 | item->page1 = (phys & 0xfffff000) + 0x1000; |
Shashank Mittal | 1cc65b0 | 2011-12-20 15:30:17 -0800 | [diff] [blame] | 272 | item->page2 = (phys & 0xfffff000) + 0x2000; |
| 273 | item->page3 = (phys & 0xfffff000) + 0x3000; |
| 274 | item->page4 = (phys & 0xfffff000) + 0x4000; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 275 | |
| 276 | enter_critical_section(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 277 | ept->head->next = (unsigned)item; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 278 | ept->head->info = 0; |
| 279 | ept->req = req; |
| 280 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 281 | arch_clean_invalidate_cache_range((addr_t) ept, |
| 282 | sizeof(struct udc_endpoint)); |
| 283 | arch_clean_invalidate_cache_range((addr_t) ept->head, |
| 284 | sizeof(struct ept_queue_head)); |
| 285 | arch_clean_invalidate_cache_range((addr_t) ept->req, |
| 286 | sizeof(struct usb_request)); |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 287 | arch_clean_invalidate_cache_range((addr_t) VA(req->req.buf), |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 288 | req->req.length); |
| 289 | arch_clean_invalidate_cache_range((addr_t) ept->req->item, |
| 290 | sizeof(struct ept_queue_item)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 291 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 292 | DBG("ept%d %s queue req=%p\n", ept->num, ept->in ? "in" : "out", req); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 293 | |
| 294 | writel(ept->bit, USB_ENDPTPRIME); |
| 295 | exit_critical_section(); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static void handle_ept_complete(struct udc_endpoint *ept) |
| 300 | { |
| 301 | struct ept_queue_item *item; |
| 302 | unsigned actual; |
| 303 | int status; |
| 304 | struct usb_request *req; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 305 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 306 | DBG("ept%d %s complete req=%p\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 307 | ept->num, ept->in ? "in" : "out", ept->req); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 308 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 309 | arch_clean_invalidate_cache_range((addr_t) ept, |
| 310 | sizeof(struct udc_endpoint)); |
| 311 | arch_clean_invalidate_cache_range((addr_t) ept->req, |
| 312 | sizeof(struct usb_request)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 313 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 314 | req = ept->req; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 315 | if (req) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 316 | ept->req = 0; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 317 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 318 | item = req->item; |
| 319 | |
| 320 | /* For some reason we are getting the notification for |
| 321 | * transfer completion before the active bit has cleared. |
| 322 | * HACK: wait for the ACTIVE bit to clear: |
| 323 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 324 | do { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 325 | /* Must clean/invalidate cached item data before checking |
| 326 | * the status every time. |
| 327 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 328 | arch_clean_invalidate_cache_range((addr_t) item, |
| 329 | sizeof(struct |
| 330 | ept_queue_item)); |
| 331 | } |
| 332 | while (readl(&(item->info)) & INFO_ACTIVE); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 333 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 334 | arch_clean_invalidate_cache_range(VA((addr_t) req->req.buf), |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 335 | req->req.length); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 336 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 337 | if (item->info & 0xff) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 338 | actual = 0; |
| 339 | status = -1; |
| 340 | dprintf(INFO, "EP%d/%s FAIL nfo=%x pg0=%x\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 341 | ept->num, ept->in ? "in" : "out", item->info, |
| 342 | item->page0); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 343 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 344 | actual = |
| 345 | req->req.length - ((item->info >> 16) & 0x7fff); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 346 | status = 0; |
| 347 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 348 | if (req->req.complete) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 349 | req->req.complete(&req->req, actual, status); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static const char *reqname(unsigned r) |
| 354 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 355 | switch (r) { |
| 356 | case GET_STATUS: |
| 357 | return "GET_STATUS"; |
| 358 | case CLEAR_FEATURE: |
| 359 | return "CLEAR_FEATURE"; |
| 360 | case SET_FEATURE: |
| 361 | return "SET_FEATURE"; |
| 362 | case SET_ADDRESS: |
| 363 | return "SET_ADDRESS"; |
| 364 | case GET_DESCRIPTOR: |
| 365 | return "GET_DESCRIPTOR"; |
| 366 | case SET_DESCRIPTOR: |
| 367 | return "SET_DESCRIPTOR"; |
| 368 | case GET_CONFIGURATION: |
| 369 | return "GET_CONFIGURATION"; |
| 370 | case SET_CONFIGURATION: |
| 371 | return "SET_CONFIGURATION"; |
| 372 | case GET_INTERFACE: |
| 373 | return "GET_INTERFACE"; |
| 374 | case SET_INTERFACE: |
| 375 | return "SET_INTERFACE"; |
| 376 | default: |
| 377 | return "*UNKNOWN*"; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | static struct udc_endpoint *ep0in, *ep0out; |
| 382 | static struct udc_request *ep0req; |
| 383 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 384 | static void |
| 385 | ep0_setup_ack_complete(struct udc_endpoint *ep, struct usb_request *req) |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 386 | { |
| 387 | uint32_t mode; |
| 388 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 389 | if (!test_mode) |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 390 | return; |
| 391 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 392 | switch (test_mode) { |
| 393 | case TEST_PACKET: |
| 394 | dprintf(INFO, "Entering test mode for TST_PKT\n"); |
| 395 | mode = readl(USB_PORTSC) & (~PORTSC_PTC); |
| 396 | writel(mode | PORTSC_PTC_TST_PKT, USB_PORTSC); |
| 397 | break; |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 398 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 399 | case TEST_SE0_NAK: |
| 400 | dprintf(INFO, "Entering test mode for SE0-NAK\n"); |
| 401 | mode = readl(USB_PORTSC) & (~PORTSC_PTC); |
| 402 | writel(mode | PORTSC_PTC_SE0_NAK, USB_PORTSC); |
| 403 | break; |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | } |
| 407 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 408 | static void setup_ack(void) |
| 409 | { |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 410 | ep0req->complete = ep0_setup_ack_complete; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 411 | ep0req->length = 0; |
| 412 | udc_request_queue(ep0in, ep0req); |
| 413 | } |
| 414 | |
| 415 | static void ep0in_complete(struct udc_request *req, unsigned actual, int status) |
| 416 | { |
| 417 | DBG("ep0in_complete %p %d %d\n", req, actual, status); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 418 | if (status == 0) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 419 | req->length = 0; |
| 420 | req->complete = 0; |
| 421 | udc_request_queue(ep0out, req); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void setup_tx(void *buf, unsigned len) |
| 426 | { |
| 427 | DBG("setup_tx %p %d\n", buf, len); |
| 428 | memcpy(ep0req->buf, buf, len); |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 429 | ep0req->buf = PA((addr_t)ep0req->buf); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 430 | ep0req->complete = ep0in_complete; |
| 431 | ep0req->length = len; |
| 432 | udc_request_queue(ep0in, ep0req); |
| 433 | } |
| 434 | |
| 435 | static unsigned char usb_config_value = 0; |
| 436 | |
| 437 | #define SETUP(type,request) (((type) << 8) | (request)) |
| 438 | |
| 439 | static void handle_setup(struct udc_endpoint *ept) |
| 440 | { |
| 441 | struct setup_packet s; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 442 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 443 | arch_clean_invalidate_cache_range((addr_t) ept->head->setup_data, |
| 444 | sizeof(struct ept_queue_head)); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 445 | memcpy(&s, ept->head->setup_data, sizeof(s)); |
| 446 | writel(ept->bit, USB_ENDPTSETUPSTAT); |
| 447 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 448 | DBG("handle_setup type=0x%02x req=0x%02x val=%d idx=%d len=%d (%s)\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 449 | s.type, s.request, s.value, s.index, s.length, reqname(s.request)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 450 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 451 | switch (SETUP(s.type, s.request)) { |
| 452 | case SETUP(DEVICE_READ, GET_STATUS): |
| 453 | { |
| 454 | unsigned zero = 0; |
| 455 | if (s.length == 2) { |
| 456 | setup_tx(&zero, 2); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 457 | return; |
| 458 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 459 | break; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 460 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 461 | case SETUP(DEVICE_READ, GET_DESCRIPTOR): |
| 462 | { |
| 463 | struct udc_descriptor *desc; |
| 464 | /* usb_highspeed? */ |
| 465 | for (desc = desc_list; desc; desc = desc->next) { |
| 466 | if (desc->tag == s.value) { |
| 467 | unsigned len = desc->len; |
| 468 | if (len > s.length) |
| 469 | len = s.length; |
| 470 | setup_tx(desc->data, len); |
| 471 | return; |
| 472 | } |
| 473 | } |
| 474 | break; |
| 475 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 476 | case SETUP(DEVICE_READ, GET_CONFIGURATION): |
| 477 | /* disabling this causes data transaction failures on OSX. Why? */ |
| 478 | if ((s.value == 0) && (s.index == 0) && (s.length == 1)) { |
| 479 | setup_tx(&usb_config_value, 1); |
| 480 | return; |
| 481 | } |
| 482 | break; |
| 483 | case SETUP(DEVICE_WRITE, SET_CONFIGURATION): |
| 484 | if (s.value == 1) { |
| 485 | struct udc_endpoint *ept; |
| 486 | /* enable endpoints */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 487 | for (ept = ept_list; ept; ept = ept->next) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 488 | if (ept->num == 0) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 489 | continue; |
| 490 | endpoint_enable(ept, s.value); |
| 491 | } |
| 492 | usb_config_value = 1; |
| 493 | the_gadget->notify(the_gadget, UDC_EVENT_ONLINE); |
| 494 | } else { |
| 495 | writel(0, USB_ENDPTCTRL(1)); |
| 496 | usb_config_value = 0; |
| 497 | the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE); |
| 498 | } |
| 499 | setup_ack(); |
| 500 | usb_online = s.value ? 1 : 0; |
| 501 | usb_status(s.value ? 1 : 0, usb_highspeed); |
| 502 | return; |
| 503 | case SETUP(DEVICE_WRITE, SET_ADDRESS): |
| 504 | /* write address delayed (will take effect |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 505 | ** after the next IN txn) |
| 506 | */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 507 | writel((s.value << 25) | (1 << 24), USB_DEVICEADDR); |
| 508 | setup_ack(); |
| 509 | return; |
| 510 | case SETUP(INTERFACE_WRITE, SET_INTERFACE): |
| 511 | /* if we ack this everything hangs */ |
| 512 | /* per spec, STALL is valid if there is not alt func */ |
| 513 | goto stall; |
Subbaraman Narayanamurthy | d8b7afc | 2011-06-30 15:42:41 -0700 | [diff] [blame] | 514 | case SETUP(DEVICE_WRITE, SET_FEATURE): |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 515 | test_mode = s.index; |
Subbaraman Narayanamurthy | d8b7afc | 2011-06-30 15:42:41 -0700 | [diff] [blame] | 516 | setup_ack(); |
Subbaraman Narayanamurthy | d8b7afc | 2011-06-30 15:42:41 -0700 | [diff] [blame] | 517 | return; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 518 | case SETUP(ENDPOINT_WRITE, CLEAR_FEATURE): |
| 519 | { |
| 520 | struct udc_endpoint *ept; |
| 521 | unsigned num = s.index & 15; |
| 522 | unsigned in = !!(s.index & 0x80); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 523 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 524 | if ((s.value == 0) && (s.length == 0)) { |
| 525 | DBG("clr feat %d %d\n", num, in); |
| 526 | for (ept = ept_list; ept; ept = ept->next) { |
| 527 | if ((ept->num == num) |
| 528 | && (ept->in == in)) { |
| 529 | endpoint_enable(ept, 1); |
| 530 | setup_ack(); |
| 531 | return; |
| 532 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 535 | break; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 536 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | dprintf(INFO, "STALL %s %d %d %d %d %d\n", |
| 540 | reqname(s.request), |
| 541 | s.type, s.request, s.value, s.index, s.length); |
| 542 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 543 | stall: |
| 544 | writel((1 << 16) | (1 << 0), USB_ENDPTCTRL(ept->num)); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | unsigned ulpi_read(unsigned reg) |
| 548 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 549 | /* initiate read operation */ |
| 550 | writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg), USB_ULPI_VIEWPORT); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 551 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 552 | /* wait for completion */ |
| 553 | while (readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 554 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 555 | return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT)); |
| 556 | } |
| 557 | |
| 558 | void ulpi_write(unsigned val, unsigned reg) |
| 559 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 560 | /* initiate write operation */ |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 561 | writel(ULPI_RUN | ULPI_WRITE | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 562 | ULPI_ADDR(reg) | ULPI_DATA(val), USB_ULPI_VIEWPORT); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 563 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 564 | /* wait for completion */ |
| 565 | while (readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 566 | } |
Ajay Dudani | 5a1e330 | 2009-12-05 13:19:17 -0800 | [diff] [blame] | 567 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 568 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 569 | int udc_init(struct udc_device *dev) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 570 | { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 571 | DBG("udc_init():\n"); |
| 572 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 573 | hsusb_clock_init(); |
| 574 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 575 | /* Do any target specific intialization like GPIO settings, |
| 576 | * LDO, PHY configuration etc. needed before USB port can be used. |
| 577 | */ |
| 578 | target_usb_init(); |
| 579 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 580 | /* RESET */ |
Deepa Dinamani | 0687ecd | 2012-08-10 16:00:26 -0700 | [diff] [blame] | 581 | writel(0x00080000, USB_USBCMD); |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 582 | |
| 583 | thread_sleep(20); |
Subbaraman Narayanamurthy | b0111a1 | 2011-02-03 14:24:16 -0800 | [diff] [blame] | 584 | |
Deepa Dinamani | 0687ecd | 2012-08-10 16:00:26 -0700 | [diff] [blame] | 585 | while((readl(USB_USBCMD)&2)); |
| 586 | |
| 587 | /* select ULPI phy */ |
| 588 | writel(0x80000000, USB_PORTSC); |
| 589 | |
| 590 | /* USB_OTG_HS_AHB_BURST */ |
| 591 | writel(0x0, USB_SBUSCFG); |
| 592 | |
| 593 | /* USB_OTG_HS_AHB_MODE: HPROT_MODE */ |
| 594 | /* Bus access related config. */ |
| 595 | writel(0x08, USB_AHB_MODE); |
| 596 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 597 | epts = memalign(4096, 4096); |
| 598 | |
| 599 | dprintf(INFO, "USB init ept @ %p\n", epts); |
| 600 | memset(epts, 0, 32 * sizeof(struct ept_queue_head)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 601 | arch_clean_invalidate_cache_range((addr_t) epts, |
| 602 | 32 * sizeof(struct ept_queue_head)); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 603 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame^] | 604 | writel((unsigned)PA((addr_t)epts), USB_ENDPOINTLISTADDR); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 605 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 606 | /* select DEVICE mode */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 607 | writel(0x02, USB_USBMODE); |
| 608 | |
| 609 | writel(0xffffffff, USB_ENDPTFLUSH); |
| 610 | thread_sleep(20); |
| 611 | |
| 612 | ep0out = _udc_endpoint_alloc(0, 0, 64); |
| 613 | ep0in = _udc_endpoint_alloc(0, 1, 64); |
| 614 | ep0req = udc_request_alloc(); |
| 615 | ep0req->buf = malloc(4096); |
| 616 | |
| 617 | { |
| 618 | /* create and register a language table descriptor */ |
| 619 | /* language 0x0409 is US English */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 620 | struct udc_descriptor *desc = |
| 621 | udc_descriptor_alloc(TYPE_STRING, 0, 4); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 622 | desc->data[2] = 0x09; |
| 623 | desc->data[3] = 0x04; |
| 624 | udc_descriptor_register(desc); |
| 625 | } |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 626 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 627 | the_device = dev; |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | enum handler_return udc_interrupt(void *arg) |
| 632 | { |
| 633 | struct udc_endpoint *ept; |
| 634 | unsigned ret = INT_NO_RESCHEDULE; |
| 635 | unsigned n = readl(USB_USBSTS); |
| 636 | writel(n, USB_USBSTS); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 637 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 638 | DBG("\nudc_interrupt(): status = 0x%x\n", n); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 639 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 640 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 641 | |
| 642 | if (n == 0) { |
| 643 | DBG("n = 0\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 644 | return ret; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 645 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 646 | |
| 647 | if (n & STS_URI) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 648 | DBG("STS_URI\n"); |
| 649 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 650 | writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE); |
| 651 | writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT); |
| 652 | writel(0xffffffff, USB_ENDPTFLUSH); |
| 653 | writel(0, USB_ENDPTCTRL(1)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 654 | dprintf(INFO, "-- reset --\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 655 | usb_online = 0; |
| 656 | usb_config_value = 0; |
| 657 | the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE); |
| 658 | |
| 659 | /* error out any pending reqs */ |
| 660 | for (ept = ept_list; ept; ept = ept->next) { |
| 661 | /* ensure that ept_complete considers |
| 662 | * this to be an error state |
| 663 | */ |
| 664 | if (ept->req) { |
| 665 | ept->req->item->info = INFO_HALTED; |
| 666 | handle_ept_complete(ept); |
| 667 | } |
| 668 | } |
| 669 | usb_status(0, usb_highspeed); |
| 670 | } |
| 671 | if (n & STS_SLI) { |
Ajay Dudani | 35f686f | 2011-07-22 13:12:09 -0700 | [diff] [blame] | 672 | DBG("-- suspend --\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 673 | } |
| 674 | if (n & STS_PCI) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 675 | dprintf(INFO, "-- portchange --\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 676 | unsigned spd = (readl(USB_PORTSC) >> 26) & 3; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 677 | if (spd == 2) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 678 | usb_highspeed = 1; |
| 679 | } else { |
| 680 | usb_highspeed = 0; |
| 681 | } |
| 682 | } |
| 683 | if (n & STS_UEI) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 684 | DBG("STS_UEI\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 685 | dprintf(INFO, "<UEI %x>\n", readl(USB_ENDPTCOMPLETE)); |
| 686 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 687 | if ((n & STS_UI) || (n & STS_UEI)) { |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 688 | |
| 689 | if (n & STS_UEI) |
| 690 | DBG("ERROR "); |
| 691 | if (n & STS_UI) |
| 692 | DBG("USB "); |
| 693 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 694 | n = readl(USB_ENDPTSETUPSTAT); |
| 695 | if (n & EPT_RX(0)) { |
| 696 | handle_setup(ep0out); |
| 697 | ret = INT_RESCHEDULE; |
| 698 | } |
| 699 | |
| 700 | n = readl(USB_ENDPTCOMPLETE); |
| 701 | if (n != 0) { |
| 702 | writel(n, USB_ENDPTCOMPLETE); |
| 703 | } |
| 704 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 705 | for (ept = ept_list; ept; ept = ept->next) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 706 | if (n & ept->bit) { |
| 707 | handle_ept_complete(ept); |
| 708 | ret = INT_RESCHEDULE; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | int udc_register_gadget(struct udc_gadget *gadget) |
| 716 | { |
| 717 | if (the_gadget) { |
| 718 | dprintf(CRITICAL, "only one gadget supported\n"); |
| 719 | return -1; |
| 720 | } |
| 721 | the_gadget = gadget; |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data) |
| 726 | { |
| 727 | data[0] = 7; |
| 728 | data[1] = TYPE_ENDPOINT; |
| 729 | data[2] = ept->num | (ept->in ? 0x80 : 0x00); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 730 | data[3] = 0x02; /* bulk -- the only kind we support */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 731 | data[4] = ept->maxpkt; |
| 732 | data[5] = ept->maxpkt >> 8; |
| 733 | data[6] = ept->in ? 0x00 : 0x01; |
| 734 | } |
| 735 | |
| 736 | static unsigned udc_ifc_desc_size(struct udc_gadget *g) |
| 737 | { |
| 738 | return 9 + g->ifc_endpoints * 7; |
| 739 | } |
| 740 | |
| 741 | static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data) |
| 742 | { |
| 743 | unsigned n; |
| 744 | |
| 745 | data[0] = 0x09; |
| 746 | data[1] = TYPE_INTERFACE; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 747 | data[2] = 0x00; /* ifc number */ |
| 748 | data[3] = 0x00; /* alt number */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 749 | data[4] = g->ifc_endpoints; |
| 750 | data[5] = g->ifc_class; |
| 751 | data[6] = g->ifc_subclass; |
| 752 | data[7] = g->ifc_protocol; |
| 753 | data[8] = udc_string_desc_alloc(g->ifc_string); |
| 754 | |
| 755 | data += 9; |
| 756 | for (n = 0; n < g->ifc_endpoints; n++) { |
| 757 | udc_ept_desc_fill(g->ept[n], data); |
| 758 | data += 7; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | int udc_start(void) |
| 763 | { |
| 764 | struct udc_descriptor *desc; |
| 765 | unsigned char *data; |
| 766 | unsigned size; |
| 767 | |
Chandan Uddaraju | 40b227d | 2010-08-03 19:25:41 -0700 | [diff] [blame] | 768 | dprintf(ALWAYS, "udc_start()\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 769 | |
| 770 | if (!the_device) { |
| 771 | dprintf(CRITICAL, "udc cannot start before init\n"); |
| 772 | return -1; |
| 773 | } |
| 774 | if (!the_gadget) { |
| 775 | dprintf(CRITICAL, "udc has no gadget registered\n"); |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | /* create our device descriptor */ |
| 780 | desc = udc_descriptor_alloc(TYPE_DEVICE, 0, 18); |
| 781 | data = desc->data; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 782 | data[2] = 0x00; /* usb spec minor rev */ |
| 783 | data[3] = 0x02; /* usb spec major rev */ |
| 784 | data[4] = 0x00; /* class */ |
| 785 | data[5] = 0x00; /* subclass */ |
| 786 | data[6] = 0x00; /* protocol */ |
| 787 | data[7] = 0x40; /* max packet size on ept 0 */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 788 | memcpy(data + 8, &the_device->vendor_id, sizeof(short)); |
| 789 | memcpy(data + 10, &the_device->product_id, sizeof(short)); |
| 790 | memcpy(data + 12, &the_device->version_id, sizeof(short)); |
| 791 | data[14] = udc_string_desc_alloc(the_device->manufacturer); |
| 792 | data[15] = udc_string_desc_alloc(the_device->product); |
| 793 | data[16] = udc_string_desc_alloc(the_device->serialno); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 794 | data[17] = 1; /* number of configurations */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 795 | udc_descriptor_register(desc); |
| 796 | |
| 797 | /* create our configuration descriptor */ |
| 798 | size = 9 + udc_ifc_desc_size(the_gadget); |
| 799 | desc = udc_descriptor_alloc(TYPE_CONFIGURATION, 0, size); |
| 800 | data = desc->data; |
| 801 | data[0] = 0x09; |
| 802 | data[2] = size; |
| 803 | data[3] = size >> 8; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 804 | data[4] = 0x01; /* number of interfaces */ |
| 805 | data[5] = 0x01; /* configuration value */ |
| 806 | data[6] = 0x00; /* configuration string */ |
| 807 | data[7] = 0x80; /* attributes */ |
| 808 | data[8] = 0x80; /* max power (250ma) -- todo fix this */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 809 | udc_ifc_desc_fill(the_gadget, data + 9); |
| 810 | udc_descriptor_register(desc); |
| 811 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 812 | register_int_handler(INT_USB_HS, udc_interrupt, (void *)0); |
Amol Jadi | ca4f4c9 | 2011-01-13 20:19:34 -0800 | [diff] [blame] | 813 | writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR); |
| 814 | unmask_interrupt(INT_USB_HS); |
| 815 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 816 | /* go to RUN mode (D+ pullup enable) */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 817 | writel(0x00080001, USB_USBCMD); |
Amol Jadi | ca4f4c9 | 2011-01-13 20:19:34 -0800 | [diff] [blame] | 818 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | int udc_stop(void) |
| 823 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 824 | writel(0, USB_USBINTR); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 825 | mask_interrupt(INT_USB_HS); |
| 826 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 827 | /* disable pullup */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 828 | writel(0x00080000, USB_USBCMD); |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame] | 829 | |
| 830 | target_usb_stop(); |
| 831 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 832 | thread_sleep(10); |
| 833 | |
| 834 | return 0; |
| 835 | } |