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