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