Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, Marvell Semiconductor Inc. |
| 3 | * Lei Wen <leiwen@marvell.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 6 | * |
| 7 | * Back ported to the 8xx platform (from the 8260 platform) by |
| 8 | * Murray.Jensen@cmst.csiro.au, 27-Jan-01. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <config.h> |
| 14 | #include <net.h> |
| 15 | #include <malloc.h> |
Troy Kisky | f157394 | 2013-10-10 15:28:03 -0700 | [diff] [blame] | 16 | #include <asm/byteorder.h> |
| 17 | #include <asm/errno.h> |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 19 | #include <asm/unaligned.h> |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 20 | #include <linux/types.h> |
Troy Kisky | f157394 | 2013-10-10 15:28:03 -0700 | [diff] [blame] | 21 | #include <linux/usb/ch9.h> |
| 22 | #include <linux/usb/gadget.h> |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 23 | #include <usb/ci_udc.h> |
Troy Kisky | f157394 | 2013-10-10 15:28:03 -0700 | [diff] [blame] | 24 | #include "../host/ehci.h" |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 25 | #include "ci_udc.h" |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 26 | |
Marek Vasut | 9d64ac9 | 2013-07-10 03:16:38 +0200 | [diff] [blame] | 27 | /* |
| 28 | * Check if the system has too long cachelines. If the cachelines are |
| 29 | * longer then 128b, the driver will not be able flush/invalidate data |
| 30 | * cache over separate QH entries. We use 128b because one QH entry is |
| 31 | * 64b long and there are always two QH list entries for each endpoint. |
| 32 | */ |
| 33 | #if ARCH_DMA_MINALIGN > 128 |
| 34 | #error This driver can not work on systems with caches longer than 128b |
| 35 | #endif |
| 36 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 37 | #ifndef DEBUG |
| 38 | #define DBG(x...) do {} while (0) |
| 39 | #else |
| 40 | #define DBG(x...) printf(x) |
| 41 | static const char *reqname(unsigned r) |
| 42 | { |
| 43 | switch (r) { |
| 44 | case USB_REQ_GET_STATUS: return "GET_STATUS"; |
| 45 | case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE"; |
| 46 | case USB_REQ_SET_FEATURE: return "SET_FEATURE"; |
| 47 | case USB_REQ_SET_ADDRESS: return "SET_ADDRESS"; |
| 48 | case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR"; |
| 49 | case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR"; |
| 50 | case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION"; |
| 51 | case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION"; |
| 52 | case USB_REQ_GET_INTERFACE: return "GET_INTERFACE"; |
| 53 | case USB_REQ_SET_INTERFACE: return "SET_INTERFACE"; |
| 54 | default: return "*UNKNOWN*"; |
| 55 | } |
| 56 | } |
| 57 | #endif |
| 58 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 59 | static struct usb_endpoint_descriptor ep0_out_desc = { |
| 60 | .bLength = sizeof(struct usb_endpoint_descriptor), |
| 61 | .bDescriptorType = USB_DT_ENDPOINT, |
| 62 | .bEndpointAddress = 0, |
| 63 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 64 | }; |
| 65 | |
| 66 | static struct usb_endpoint_descriptor ep0_in_desc = { |
| 67 | .bLength = sizeof(struct usb_endpoint_descriptor), |
| 68 | .bDescriptorType = USB_DT_ENDPOINT, |
| 69 | .bEndpointAddress = USB_DIR_IN, |
| 70 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 71 | }; |
| 72 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 73 | static int ci_pullup(struct usb_gadget *gadget, int is_on); |
| 74 | static int ci_ep_enable(struct usb_ep *ep, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 75 | const struct usb_endpoint_descriptor *desc); |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 76 | static int ci_ep_disable(struct usb_ep *ep); |
| 77 | static int ci_ep_queue(struct usb_ep *ep, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 78 | struct usb_request *req, gfp_t gfp_flags); |
| 79 | static struct usb_request * |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 80 | ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags); |
| 81 | static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 82 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 83 | static struct usb_gadget_ops ci_udc_ops = { |
| 84 | .pullup = ci_pullup, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 87 | static struct usb_ep_ops ci_ep_ops = { |
| 88 | .enable = ci_ep_enable, |
| 89 | .disable = ci_ep_disable, |
| 90 | .queue = ci_ep_queue, |
| 91 | .alloc_request = ci_ep_alloc_request, |
| 92 | .free_request = ci_ep_free_request, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 95 | /* Init values for USB endpoints. */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 96 | static const struct usb_ep ci_ep_init[2] = { |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 97 | [0] = { /* EP 0 */ |
| 98 | .maxpacket = 64, |
| 99 | .name = "ep0", |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 100 | .ops = &ci_ep_ops, |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 101 | }, |
| 102 | [1] = { /* EP 1..n */ |
| 103 | .maxpacket = 512, |
| 104 | .name = "ep-", |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 105 | .ops = &ci_ep_ops, |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 106 | }, |
| 107 | }; |
| 108 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 109 | static struct ci_drv controller = { |
Marek Vasut | be0b0e7 | 2013-07-10 03:16:35 +0200 | [diff] [blame] | 110 | .gadget = { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 111 | .name = "ci_udc", |
| 112 | .ops = &ci_udc_ops, |
Troy Kisky | 4b4df47 | 2013-09-25 18:41:09 -0700 | [diff] [blame] | 113 | .is_dualspeed = 1, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 114 | }, |
| 115 | }; |
| 116 | |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 117 | /** |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 118 | * ci_get_qh() - return queue head for endpoint |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 119 | * @ep_num: Endpoint number |
| 120 | * @dir_in: Direction of the endpoint (IN = 1, OUT = 0) |
| 121 | * |
| 122 | * This function returns the QH associated with particular endpoint |
| 123 | * and it's direction. |
| 124 | */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 125 | static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in) |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 126 | { |
| 127 | return &controller.epts[(ep_num * 2) + dir_in]; |
| 128 | } |
| 129 | |
Marek Vasut | a6f6b08 | 2013-07-10 03:16:41 +0200 | [diff] [blame] | 130 | /** |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 131 | * ci_get_qtd() - return queue item for endpoint |
Marek Vasut | a6f6b08 | 2013-07-10 03:16:41 +0200 | [diff] [blame] | 132 | * @ep_num: Endpoint number |
| 133 | * @dir_in: Direction of the endpoint (IN = 1, OUT = 0) |
| 134 | * |
| 135 | * This function returns the QH associated with particular endpoint |
| 136 | * and it's direction. |
| 137 | */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 138 | static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in) |
Marek Vasut | a6f6b08 | 2013-07-10 03:16:41 +0200 | [diff] [blame] | 139 | { |
| 140 | return controller.items[(ep_num * 2) + dir_in]; |
| 141 | } |
| 142 | |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 143 | /** |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 144 | * ci_flush_qh - flush cache over queue head |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 145 | * @ep_num: Endpoint number |
| 146 | * |
| 147 | * This function flushes cache over QH for particular endpoint. |
| 148 | */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 149 | static void ci_flush_qh(int ep_num) |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 150 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 151 | struct ept_queue_head *head = ci_get_qh(ep_num, 0); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 152 | const uint32_t start = (uint32_t)head; |
| 153 | const uint32_t end = start + 2 * sizeof(*head); |
| 154 | |
| 155 | flush_dcache_range(start, end); |
| 156 | } |
| 157 | |
| 158 | /** |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 159 | * ci_invalidate_qh - invalidate cache over queue head |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 160 | * @ep_num: Endpoint number |
| 161 | * |
| 162 | * This function invalidates cache over QH for particular endpoint. |
| 163 | */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 164 | static void ci_invalidate_qh(int ep_num) |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 165 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 166 | struct ept_queue_head *head = ci_get_qh(ep_num, 0); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 167 | uint32_t start = (uint32_t)head; |
| 168 | uint32_t end = start + 2 * sizeof(*head); |
| 169 | |
| 170 | invalidate_dcache_range(start, end); |
| 171 | } |
| 172 | |
| 173 | /** |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 174 | * ci_flush_qtd - flush cache over queue item |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 175 | * @ep_num: Endpoint number |
| 176 | * |
| 177 | * This function flushes cache over qTD pair for particular endpoint. |
| 178 | */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 179 | static void ci_flush_qtd(int ep_num) |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 180 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 181 | struct ept_queue_item *item = ci_get_qtd(ep_num, 0); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 182 | const uint32_t start = (uint32_t)item; |
| 183 | const uint32_t end_raw = start + 2 * sizeof(*item); |
| 184 | const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN); |
| 185 | |
| 186 | flush_dcache_range(start, end); |
| 187 | } |
| 188 | |
| 189 | /** |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 190 | * ci_invalidate_qtd - invalidate cache over queue item |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 191 | * @ep_num: Endpoint number |
| 192 | * |
| 193 | * This function invalidates cache over qTD pair for particular endpoint. |
| 194 | */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 195 | static void ci_invalidate_qtd(int ep_num) |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 196 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 197 | struct ept_queue_item *item = ci_get_qtd(ep_num, 0); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 198 | const uint32_t start = (uint32_t)item; |
| 199 | const uint32_t end_raw = start + 2 * sizeof(*item); |
| 200 | const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN); |
| 201 | |
| 202 | invalidate_dcache_range(start, end); |
| 203 | } |
| 204 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 205 | static struct usb_request * |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 206 | ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 207 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 208 | struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep); |
| 209 | return &ci_ep->req; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 212 | static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 213 | { |
| 214 | return; |
| 215 | } |
| 216 | |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 217 | static void ep_enable(int num, int in, int maxpacket) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 218 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 219 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 220 | unsigned n; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 221 | |
| 222 | n = readl(&udc->epctrl[num]); |
| 223 | if (in) |
| 224 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 225 | else |
| 226 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 227 | |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 228 | if (num != 0) { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 229 | struct ept_queue_head *head = ci_get_qh(num, in); |
Troy Kisky | 06bb97a | 2013-10-10 15:28:02 -0700 | [diff] [blame] | 230 | |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 231 | head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 232 | ci_flush_qh(num); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 233 | } |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 234 | writel(n, &udc->epctrl[num]); |
| 235 | } |
| 236 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 237 | static int ci_ep_enable(struct usb_ep *ep, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 238 | const struct usb_endpoint_descriptor *desc) |
| 239 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 240 | struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 241 | int num, in; |
| 242 | num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 243 | in = (desc->bEndpointAddress & USB_DIR_IN) != 0; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 244 | ci_ep->desc = desc; |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 245 | |
| 246 | if (num) { |
| 247 | int max = get_unaligned_le16(&desc->wMaxPacketSize); |
| 248 | |
| 249 | if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL)) |
| 250 | max = 64; |
| 251 | if (ep->maxpacket != max) { |
| 252 | DBG("%s: from %d to %d\n", __func__, |
| 253 | ep->maxpacket, max); |
| 254 | ep->maxpacket = max; |
| 255 | } |
| 256 | } |
| 257 | ep_enable(num, in, ep->maxpacket); |
| 258 | DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 262 | static int ci_ep_disable(struct usb_ep *ep) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 263 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 264 | struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep); |
Troy Kisky | 3549407 | 2013-09-25 18:41:15 -0700 | [diff] [blame] | 265 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 266 | ci_ep->desc = NULL; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 270 | static int ci_bounce(struct ci_ep *ep, int in) |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 271 | { |
| 272 | uint32_t addr = (uint32_t)ep->req.buf; |
| 273 | uint32_t ba; |
| 274 | |
| 275 | /* Input buffer address is not aligned. */ |
| 276 | if (addr & (ARCH_DMA_MINALIGN - 1)) |
| 277 | goto align; |
| 278 | |
| 279 | /* Input buffer length is not aligned. */ |
| 280 | if (ep->req.length & (ARCH_DMA_MINALIGN - 1)) |
| 281 | goto align; |
| 282 | |
| 283 | /* The buffer is well aligned, only flush cache. */ |
| 284 | ep->b_len = ep->req.length; |
| 285 | ep->b_buf = ep->req.buf; |
| 286 | goto flush; |
| 287 | |
| 288 | align: |
| 289 | /* Use internal buffer for small payloads. */ |
| 290 | if (ep->req.length <= 64) { |
| 291 | ep->b_len = 64; |
| 292 | ep->b_buf = ep->b_fast; |
| 293 | } else { |
| 294 | ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN); |
| 295 | ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len); |
| 296 | if (!ep->b_buf) |
| 297 | return -ENOMEM; |
| 298 | } |
Troy Kisky | 1567b88 | 2013-10-10 15:28:01 -0700 | [diff] [blame] | 299 | if (in) |
| 300 | memcpy(ep->b_buf, ep->req.buf, ep->req.length); |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 301 | |
| 302 | flush: |
| 303 | ba = (uint32_t)ep->b_buf; |
| 304 | flush_dcache_range(ba, ba + ep->b_len); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 309 | static void ci_debounce(struct ci_ep *ep, int in) |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 310 | { |
| 311 | uint32_t addr = (uint32_t)ep->req.buf; |
| 312 | uint32_t ba = (uint32_t)ep->b_buf; |
| 313 | |
Troy Kisky | 1567b88 | 2013-10-10 15:28:01 -0700 | [diff] [blame] | 314 | if (in) { |
| 315 | if (addr == ba) |
| 316 | return; /* not a bounce */ |
| 317 | goto free; |
| 318 | } |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 319 | invalidate_dcache_range(ba, ba + ep->b_len); |
| 320 | |
Troy Kisky | 1567b88 | 2013-10-10 15:28:01 -0700 | [diff] [blame] | 321 | if (addr == ba) |
| 322 | return; /* not a bounce */ |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 323 | |
Stephen Warren | d06444f | 2014-04-24 17:52:37 -0600 | [diff] [blame] | 324 | memcpy(ep->req.buf, ep->b_buf, ep->req.actual); |
Troy Kisky | 1567b88 | 2013-10-10 15:28:01 -0700 | [diff] [blame] | 325 | free: |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 326 | /* Large payloads use allocated buffer, free it. */ |
Troy Kisky | 1567b88 | 2013-10-10 15:28:01 -0700 | [diff] [blame] | 327 | if (ep->b_buf != ep->b_fast) |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 328 | free(ep->b_buf); |
| 329 | } |
| 330 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 331 | static int ci_ep_queue(struct usb_ep *ep, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 332 | struct usb_request *req, gfp_t gfp_flags) |
| 333 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 334 | struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep); |
| 335 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 336 | struct ept_queue_item *item; |
| 337 | struct ept_queue_head *head; |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 338 | int bit, num, len, in, ret; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 339 | num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 340 | in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 341 | item = ci_get_qtd(num, in); |
| 342 | head = ci_get_qh(num, in); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 343 | len = req->length; |
| 344 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 345 | ret = ci_bounce(ci_ep, in); |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 346 | if (ret) |
| 347 | return ret; |
| 348 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 349 | item->next = TERMINATE; |
| 350 | item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 351 | item->page0 = (uint32_t)ci_ep->b_buf; |
| 352 | item->page1 = ((uint32_t)ci_ep->b_buf & 0xfffff000) + 0x1000; |
Stephen Warren | de40a1e | 2014-04-24 17:52:36 -0600 | [diff] [blame] | 353 | item->page2 = ((uint32_t)ci_ep->b_buf & 0xfffff000) + 0x2000; |
| 354 | item->page3 = ((uint32_t)ci_ep->b_buf & 0xfffff000) + 0x3000; |
| 355 | item->page4 = ((uint32_t)ci_ep->b_buf & 0xfffff000) + 0x4000; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 356 | ci_flush_qtd(num); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 357 | |
| 358 | head->next = (unsigned) item; |
| 359 | head->info = 0; |
| 360 | |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 361 | DBG("ept%d %s queue len %x, buffer %p\n", |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 362 | num, in ? "in" : "out", len, ci_ep->b_buf); |
| 363 | ci_flush_qh(num); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 364 | |
| 365 | if (in) |
| 366 | bit = EPT_TX(num); |
| 367 | else |
| 368 | bit = EPT_RX(num); |
| 369 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 370 | writel(bit, &udc->epprime); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 375 | static void handle_ep_complete(struct ci_ep *ep) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 376 | { |
| 377 | struct ept_queue_item *item; |
| 378 | int num, in, len; |
| 379 | num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 380 | in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 381 | if (num == 0) |
| 382 | ep->desc = &ep0_out_desc; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 383 | item = ci_get_qtd(num, in); |
| 384 | ci_invalidate_qtd(num); |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 385 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 386 | if (item->info & 0xff) |
Troy Kisky | 3703c45 | 2013-09-25 18:41:08 -0700 | [diff] [blame] | 387 | printf("EP%d/%s FAIL info=%x pg0=%x\n", |
| 388 | num, in ? "in" : "out", item->info, item->page0); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 389 | |
| 390 | len = (item->info >> 16) & 0x7fff; |
Stephen Warren | d06444f | 2014-04-24 17:52:37 -0600 | [diff] [blame] | 391 | ep->req.actual = ep->req.length - len; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 392 | ci_debounce(ep, in); |
Troy Kisky | 1567b88 | 2013-10-10 15:28:01 -0700 | [diff] [blame] | 393 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 394 | DBG("ept%d %s complete %x\n", |
| 395 | num, in ? "in" : "out", len); |
| 396 | ep->req.complete(&ep->ep, &ep->req); |
| 397 | if (num == 0) { |
| 398 | ep->req.length = 0; |
| 399 | usb_ep_queue(&ep->ep, &ep->req, 0); |
| 400 | ep->desc = &ep0_in_desc; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | #define SETUP(type, request) (((type) << 8) | (request)) |
| 405 | |
| 406 | static void handle_setup(void) |
| 407 | { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 408 | struct usb_request *req = &controller.ep[0].req; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 409 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 410 | struct ept_queue_head *head; |
| 411 | struct usb_ctrlrequest r; |
| 412 | int status = 0; |
| 413 | int num, in, _num, _in, i; |
| 414 | char *buf; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 415 | head = ci_get_qh(0, 0); /* EP0 OUT */ |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 416 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 417 | ci_invalidate_qh(0); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 418 | memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest)); |
| 419 | writel(EPT_RX(0), &udc->epstat); |
| 420 | DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest), |
| 421 | r.bRequestType, r.bRequest, r.wIndex, r.wValue); |
| 422 | |
| 423 | switch (SETUP(r.bRequestType, r.bRequest)) { |
| 424 | case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE): |
| 425 | _num = r.wIndex & 15; |
| 426 | _in = !!(r.wIndex & 0x80); |
| 427 | |
| 428 | if ((r.wValue == 0) && (r.wLength == 0)) { |
| 429 | req->length = 0; |
| 430 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 431 | struct ci_ep *ep = &controller.ep[i]; |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 432 | |
| 433 | if (!ep->desc) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 434 | continue; |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 435 | num = ep->desc->bEndpointAddress |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 436 | & USB_ENDPOINT_NUMBER_MASK; |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 437 | in = (ep->desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 438 | & USB_DIR_IN) != 0; |
| 439 | if ((num == _num) && (in == _in)) { |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 440 | ep_enable(num, in, ep->ep.maxpacket); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 441 | usb_ep_queue(controller.gadget.ep0, |
| 442 | req, 0); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | return; |
| 448 | |
| 449 | case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS): |
| 450 | /* |
| 451 | * write address delayed (will take effect |
| 452 | * after the next IN txn) |
| 453 | */ |
| 454 | writel((r.wValue << 25) | (1 << 24), &udc->devaddr); |
| 455 | req->length = 0; |
| 456 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 457 | return; |
| 458 | |
| 459 | case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS): |
| 460 | req->length = 2; |
| 461 | buf = (char *)req->buf; |
| 462 | buf[0] = 1 << USB_DEVICE_SELF_POWERED; |
| 463 | buf[1] = 0; |
| 464 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 465 | return; |
| 466 | } |
| 467 | /* pass request up to the gadget driver */ |
| 468 | if (controller.driver) |
| 469 | status = controller.driver->setup(&controller.gadget, &r); |
| 470 | else |
| 471 | status = -ENODEV; |
| 472 | |
| 473 | if (!status) |
| 474 | return; |
| 475 | DBG("STALL reqname %s type %x value %x, index %x\n", |
| 476 | reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex); |
| 477 | writel((1<<16) | (1 << 0), &udc->epctrl[0]); |
| 478 | } |
| 479 | |
| 480 | static void stop_activity(void) |
| 481 | { |
| 482 | int i, num, in; |
| 483 | struct ept_queue_head *head; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 484 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 485 | writel(readl(&udc->epcomp), &udc->epcomp); |
| 486 | writel(readl(&udc->epstat), &udc->epstat); |
| 487 | writel(0xffffffff, &udc->epflush); |
| 488 | |
| 489 | /* error out any pending reqs */ |
| 490 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
| 491 | if (i != 0) |
| 492 | writel(0, &udc->epctrl[i]); |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 493 | if (controller.ep[i].desc) { |
| 494 | num = controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 495 | & USB_ENDPOINT_NUMBER_MASK; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 496 | in = (controller.ep[i].desc->bEndpointAddress |
| 497 | & USB_DIR_IN) != 0; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 498 | head = ci_get_qh(num, in); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 499 | head->info = INFO_ACTIVE; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 500 | ci_flush_qh(num); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | void udc_irq(void) |
| 506 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 507 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 508 | unsigned n = readl(&udc->usbsts); |
| 509 | writel(n, &udc->usbsts); |
| 510 | int bit, i, num, in; |
| 511 | |
| 512 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
| 513 | if (n == 0) |
| 514 | return; |
| 515 | |
| 516 | if (n & STS_URI) { |
| 517 | DBG("-- reset --\n"); |
| 518 | stop_activity(); |
| 519 | } |
| 520 | if (n & STS_SLI) |
| 521 | DBG("-- suspend --\n"); |
| 522 | |
| 523 | if (n & STS_PCI) { |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 524 | int max = 64; |
| 525 | int speed = USB_SPEED_FULL; |
| 526 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 527 | bit = (readl(&udc->portsc) >> 26) & 3; |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 528 | DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full"); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 529 | if (bit == 2) { |
Troy Kisky | 256a317 | 2013-10-10 15:28:00 -0700 | [diff] [blame] | 530 | speed = USB_SPEED_HIGH; |
| 531 | max = 512; |
| 532 | } |
| 533 | controller.gadget.speed = speed; |
| 534 | for (i = 1; i < NUM_ENDPOINTS; i++) { |
| 535 | if (controller.ep[i].ep.maxpacket > max) |
| 536 | controller.ep[i].ep.maxpacket = max; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | if (n & STS_UEI) |
| 541 | printf("<UEI %x>\n", readl(&udc->epcomp)); |
| 542 | |
| 543 | if ((n & STS_UI) || (n & STS_UEI)) { |
| 544 | n = readl(&udc->epstat); |
| 545 | if (n & EPT_RX(0)) |
| 546 | handle_setup(); |
| 547 | |
| 548 | n = readl(&udc->epcomp); |
| 549 | if (n != 0) |
| 550 | writel(n, &udc->epcomp); |
| 551 | |
| 552 | for (i = 0; i < NUM_ENDPOINTS && n; i++) { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 553 | if (controller.ep[i].desc) { |
| 554 | num = controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 555 | & USB_ENDPOINT_NUMBER_MASK; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 556 | in = (controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 557 | & USB_DIR_IN) != 0; |
| 558 | bit = (in) ? EPT_TX(num) : EPT_RX(num); |
| 559 | if (n & bit) |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 560 | handle_ep_complete(&controller.ep[i]); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | int usb_gadget_handle_interrupts(void) |
| 567 | { |
| 568 | u32 value; |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 569 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 570 | |
| 571 | value = readl(&udc->usbsts); |
| 572 | if (value) |
| 573 | udc_irq(); |
| 574 | |
| 575 | return value; |
| 576 | } |
| 577 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 578 | static int ci_pullup(struct usb_gadget *gadget, int is_on) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 579 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 580 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 581 | if (is_on) { |
| 582 | /* RESET */ |
| 583 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd); |
| 584 | udelay(200); |
| 585 | |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 586 | writel((unsigned)controller.epts, &udc->epinitaddr); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 587 | |
| 588 | /* select DEVICE mode */ |
| 589 | writel(USBMODE_DEVICE, &udc->usbmode); |
| 590 | |
| 591 | writel(0xffffffff, &udc->epflush); |
| 592 | |
| 593 | /* Turn on the USB connection by enabling the pullup resistor */ |
| 594 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd); |
| 595 | } else { |
| 596 | stop_activity(); |
| 597 | writel(USBCMD_FS2, &udc->usbcmd); |
| 598 | udelay(800); |
| 599 | if (controller.driver) |
| 600 | controller.driver->disconnect(gadget); |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | void udc_disconnect(void) |
| 607 | { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 608 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 609 | /* disable pullup */ |
| 610 | stop_activity(); |
| 611 | writel(USBCMD_FS2, &udc->usbcmd); |
| 612 | udelay(800); |
| 613 | if (controller.driver) |
| 614 | controller.driver->disconnect(&controller.gadget); |
| 615 | } |
| 616 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 617 | static int ci_udc_probe(void) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 618 | { |
| 619 | struct ept_queue_head *head; |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 620 | uint8_t *imem; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 621 | int i; |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 622 | |
Marek Vasut | 806d76c | 2013-07-10 03:16:34 +0200 | [diff] [blame] | 623 | const int num = 2 * NUM_ENDPOINTS; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 624 | |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 625 | const int eplist_min_align = 4096; |
| 626 | const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN); |
| 627 | const int eplist_raw_sz = num * sizeof(struct ept_queue_head); |
| 628 | const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN); |
| 629 | |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 630 | const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32); |
| 631 | const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item); |
| 632 | const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN); |
| 633 | const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz; |
| 634 | |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 635 | /* The QH list must be aligned to 4096 bytes. */ |
| 636 | controller.epts = memalign(eplist_align, eplist_sz); |
| 637 | if (!controller.epts) |
| 638 | return -ENOMEM; |
| 639 | memset(controller.epts, 0, eplist_sz); |
| 640 | |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Each qTD item must be 32-byte aligned, each qTD touple must be |
| 643 | * cacheline aligned. There are two qTD items for each endpoint and |
| 644 | * only one of them is used for the endpoint at time, so we can group |
| 645 | * them together. |
| 646 | */ |
| 647 | controller.items_mem = memalign(ilist_align, ilist_sz); |
| 648 | if (!controller.items_mem) { |
| 649 | free(controller.epts); |
| 650 | return -ENOMEM; |
| 651 | } |
Troy Kisky | 0dee8ce | 2013-09-25 18:41:14 -0700 | [diff] [blame] | 652 | memset(controller.items_mem, 0, ilist_sz); |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 653 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 654 | for (i = 0; i < 2 * NUM_ENDPOINTS; i++) { |
| 655 | /* |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 656 | * Configure QH for each endpoint. The structure of the QH list |
| 657 | * is such that each two subsequent fields, N and N+1 where N is |
| 658 | * even, in the QH list represent QH for one endpoint. The Nth |
| 659 | * entry represents OUT configuration and the N+1th entry does |
| 660 | * represent IN configuration of the endpoint. |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 661 | */ |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 662 | head = controller.epts + i; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 663 | if (i < 2) |
| 664 | head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE) |
| 665 | | CONFIG_ZLT | CONFIG_IOS; |
| 666 | else |
| 667 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) |
| 668 | | CONFIG_ZLT; |
| 669 | head->next = TERMINATE; |
| 670 | head->info = 0; |
| 671 | |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 672 | imem = controller.items_mem + ((i >> 1) * ilist_ent_sz); |
| 673 | if (i & 1) |
| 674 | imem += sizeof(struct ept_queue_item); |
| 675 | |
| 676 | controller.items[i] = (struct ept_queue_item *)imem; |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 677 | |
| 678 | if (i & 1) { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 679 | ci_flush_qh(i - 1); |
| 680 | ci_flush_qtd(i - 1); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 681 | } |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | INIT_LIST_HEAD(&controller.gadget.ep_list); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 685 | |
| 686 | /* Init EP 0 */ |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 687 | memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init)); |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 688 | controller.ep[0].desc = &ep0_in_desc; |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 689 | controller.gadget.ep0 = &controller.ep[0].ep; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 690 | INIT_LIST_HEAD(&controller.gadget.ep0->ep_list); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 691 | |
| 692 | /* Init EP 1..n */ |
| 693 | for (i = 1; i < NUM_ENDPOINTS; i++) { |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 694 | memcpy(&controller.ep[i].ep, &ci_ep_init[1], |
| 695 | sizeof(*ci_ep_init)); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 696 | list_add_tail(&controller.ep[i].ep.ep_list, |
| 697 | &controller.gadget.ep_list); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 698 | } |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 699 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 704 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 705 | int ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 706 | |
Marek Vasut | fe67fe7 | 2013-07-10 03:16:33 +0200 | [diff] [blame] | 707 | if (!driver) |
| 708 | return -EINVAL; |
| 709 | if (!driver->bind || !driver->setup || !driver->disconnect) |
| 710 | return -EINVAL; |
| 711 | if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 712 | return -EINVAL; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 713 | |
Troy Kisky | 8f9c49d | 2013-10-10 15:27:56 -0700 | [diff] [blame] | 714 | ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl); |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 715 | if (ret) |
| 716 | return ret; |
| 717 | |
Marek Vasut | 9ba8997 | 2014-02-06 02:43:45 +0100 | [diff] [blame] | 718 | ret = ci_udc_probe(); |
Stephen Warren | 0381a727 | 2014-04-24 17:52:38 -0600 | [diff] [blame^] | 719 | #if defined(CONFIG_USB_EHCI_MX6) || defined(CONFIG_USB_EHCI_MXS) |
| 720 | /* |
| 721 | * FIXME: usb_lowlevel_init()->ehci_hcd_init() should be doing all |
| 722 | * HW-specific initialization, e.g. ULPI-vs-UTMI PHY selection |
| 723 | */ |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 724 | if (!ret) { |
Stephen Warren | 0381a727 | 2014-04-24 17:52:38 -0600 | [diff] [blame^] | 725 | struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 726 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 727 | /* select ULPI phy */ |
| 728 | writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc); |
| 729 | } |
Stephen Warren | 0381a727 | 2014-04-24 17:52:38 -0600 | [diff] [blame^] | 730 | #endif |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 731 | |
| 732 | ret = driver->bind(&controller.gadget); |
| 733 | if (ret) { |
| 734 | DBG("driver->bind() returned %d\n", ret); |
| 735 | return ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 736 | } |
| 737 | controller.driver = driver; |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 743 | { |
| 744 | return 0; |
| 745 | } |