blob: 7a6563f83fd1d5e42c3d5f7d3a0eb2fd774c3ba6 [file] [log] [blame]
Lei Wena91ee2a2011-10-05 08:11:40 -07001/*
2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lei Wena91ee2a2011-10-05 08:11:40 -07006 *
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 Kiskyf1573942013-10-10 15:28:03 -070016#include <asm/byteorder.h>
17#include <asm/errno.h>
Lei Wena91ee2a2011-10-05 08:11:40 -070018#include <asm/io.h>
Troy Kisky256a3172013-10-10 15:28:00 -070019#include <asm/unaligned.h>
Lei Wena91ee2a2011-10-05 08:11:40 -070020#include <linux/types.h>
Troy Kiskyf1573942013-10-10 15:28:03 -070021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
Marek Vasut9ba89972014-02-06 02:43:45 +010023#include <usb/ci_udc.h>
Troy Kiskyf1573942013-10-10 15:28:03 -070024#include "../host/ehci.h"
Marek Vasut9ba89972014-02-06 02:43:45 +010025#include "ci_udc.h"
Lei Wena91ee2a2011-10-05 08:11:40 -070026
Marek Vasut9d64ac92013-07-10 03:16:38 +020027/*
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 Wena91ee2a2011-10-05 08:11:40 -070037#ifndef DEBUG
38#define DBG(x...) do {} while (0)
39#else
40#define DBG(x...) printf(x)
41static 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
Stephen Warrenb3dc4222014-05-29 14:53:01 -060059static struct usb_endpoint_descriptor ep0_desc = {
Lei Wena91ee2a2011-10-05 08:11:40 -070060 .bLength = sizeof(struct usb_endpoint_descriptor),
61 .bDescriptorType = USB_DT_ENDPOINT,
Lei Wena91ee2a2011-10-05 08:11:40 -070062 .bEndpointAddress = USB_DIR_IN,
63 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
64};
65
Marek Vasut9ba89972014-02-06 02:43:45 +010066static int ci_pullup(struct usb_gadget *gadget, int is_on);
67static int ci_ep_enable(struct usb_ep *ep,
Lei Wena91ee2a2011-10-05 08:11:40 -070068 const struct usb_endpoint_descriptor *desc);
Marek Vasut9ba89972014-02-06 02:43:45 +010069static int ci_ep_disable(struct usb_ep *ep);
70static int ci_ep_queue(struct usb_ep *ep,
Lei Wena91ee2a2011-10-05 08:11:40 -070071 struct usb_request *req, gfp_t gfp_flags);
72static struct usb_request *
Marek Vasut9ba89972014-02-06 02:43:45 +010073ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
74static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
Lei Wena91ee2a2011-10-05 08:11:40 -070075
Marek Vasut9ba89972014-02-06 02:43:45 +010076static struct usb_gadget_ops ci_udc_ops = {
77 .pullup = ci_pullup,
Lei Wena91ee2a2011-10-05 08:11:40 -070078};
79
Marek Vasut9ba89972014-02-06 02:43:45 +010080static struct usb_ep_ops ci_ep_ops = {
81 .enable = ci_ep_enable,
82 .disable = ci_ep_disable,
83 .queue = ci_ep_queue,
84 .alloc_request = ci_ep_alloc_request,
85 .free_request = ci_ep_free_request,
Lei Wena91ee2a2011-10-05 08:11:40 -070086};
87
Marek Vasute1263412013-07-10 03:16:30 +020088/* Init values for USB endpoints. */
Marek Vasut9ba89972014-02-06 02:43:45 +010089static const struct usb_ep ci_ep_init[2] = {
Marek Vasute1263412013-07-10 03:16:30 +020090 [0] = { /* EP 0 */
91 .maxpacket = 64,
92 .name = "ep0",
Marek Vasut9ba89972014-02-06 02:43:45 +010093 .ops = &ci_ep_ops,
Marek Vasute1263412013-07-10 03:16:30 +020094 },
95 [1] = { /* EP 1..n */
96 .maxpacket = 512,
97 .name = "ep-",
Marek Vasut9ba89972014-02-06 02:43:45 +010098 .ops = &ci_ep_ops,
Marek Vasute1263412013-07-10 03:16:30 +020099 },
100};
101
Marek Vasut9ba89972014-02-06 02:43:45 +0100102static struct ci_drv controller = {
Marek Vasutbe0b0e72013-07-10 03:16:35 +0200103 .gadget = {
Marek Vasut9ba89972014-02-06 02:43:45 +0100104 .name = "ci_udc",
105 .ops = &ci_udc_ops,
Troy Kisky4b4df472013-09-25 18:41:09 -0700106 .is_dualspeed = 1,
Lei Wena91ee2a2011-10-05 08:11:40 -0700107 },
108};
109
Marek Vasut9212f892013-07-10 03:16:39 +0200110/**
Marek Vasut9ba89972014-02-06 02:43:45 +0100111 * ci_get_qh() - return queue head for endpoint
Marek Vasut9212f892013-07-10 03:16:39 +0200112 * @ep_num: Endpoint number
113 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
114 *
115 * This function returns the QH associated with particular endpoint
116 * and it's direction.
117 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100118static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in)
Marek Vasut9212f892013-07-10 03:16:39 +0200119{
120 return &controller.epts[(ep_num * 2) + dir_in];
121}
122
Marek Vasuta6f6b082013-07-10 03:16:41 +0200123/**
Marek Vasut9ba89972014-02-06 02:43:45 +0100124 * ci_get_qtd() - return queue item for endpoint
Marek Vasuta6f6b082013-07-10 03:16:41 +0200125 * @ep_num: Endpoint number
126 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
127 *
128 * This function returns the QH associated with particular endpoint
129 * and it's direction.
130 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100131static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in)
Marek Vasuta6f6b082013-07-10 03:16:41 +0200132{
133 return controller.items[(ep_num * 2) + dir_in];
134}
135
Marek Vasut58465342013-07-10 03:16:42 +0200136/**
Marek Vasut9ba89972014-02-06 02:43:45 +0100137 * ci_flush_qh - flush cache over queue head
Marek Vasut58465342013-07-10 03:16:42 +0200138 * @ep_num: Endpoint number
139 *
140 * This function flushes cache over QH for particular endpoint.
141 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100142static void ci_flush_qh(int ep_num)
Marek Vasut58465342013-07-10 03:16:42 +0200143{
Marek Vasut9ba89972014-02-06 02:43:45 +0100144 struct ept_queue_head *head = ci_get_qh(ep_num, 0);
Marek Vasut58465342013-07-10 03:16:42 +0200145 const uint32_t start = (uint32_t)head;
146 const uint32_t end = start + 2 * sizeof(*head);
147
148 flush_dcache_range(start, end);
149}
150
151/**
Marek Vasut9ba89972014-02-06 02:43:45 +0100152 * ci_invalidate_qh - invalidate cache over queue head
Marek Vasut58465342013-07-10 03:16:42 +0200153 * @ep_num: Endpoint number
154 *
155 * This function invalidates cache over QH for particular endpoint.
156 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100157static void ci_invalidate_qh(int ep_num)
Marek Vasut58465342013-07-10 03:16:42 +0200158{
Marek Vasut9ba89972014-02-06 02:43:45 +0100159 struct ept_queue_head *head = ci_get_qh(ep_num, 0);
Marek Vasut58465342013-07-10 03:16:42 +0200160 uint32_t start = (uint32_t)head;
161 uint32_t end = start + 2 * sizeof(*head);
162
163 invalidate_dcache_range(start, end);
164}
165
166/**
Marek Vasut9ba89972014-02-06 02:43:45 +0100167 * ci_flush_qtd - flush cache over queue item
Marek Vasut58465342013-07-10 03:16:42 +0200168 * @ep_num: Endpoint number
169 *
170 * This function flushes cache over qTD pair for particular endpoint.
171 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100172static void ci_flush_qtd(int ep_num)
Marek Vasut58465342013-07-10 03:16:42 +0200173{
Marek Vasut9ba89972014-02-06 02:43:45 +0100174 struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
Marek Vasut58465342013-07-10 03:16:42 +0200175 const uint32_t start = (uint32_t)item;
176 const uint32_t end_raw = start + 2 * sizeof(*item);
177 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
178
179 flush_dcache_range(start, end);
180}
181
182/**
Marek Vasut9ba89972014-02-06 02:43:45 +0100183 * ci_invalidate_qtd - invalidate cache over queue item
Marek Vasut58465342013-07-10 03:16:42 +0200184 * @ep_num: Endpoint number
185 *
186 * This function invalidates cache over qTD pair for particular endpoint.
187 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100188static void ci_invalidate_qtd(int ep_num)
Marek Vasut58465342013-07-10 03:16:42 +0200189{
Marek Vasut9ba89972014-02-06 02:43:45 +0100190 struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
Marek Vasut58465342013-07-10 03:16:42 +0200191 const uint32_t start = (uint32_t)item;
192 const uint32_t end_raw = start + 2 * sizeof(*item);
193 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
194
195 invalidate_dcache_range(start, end);
196}
197
Lei Wena91ee2a2011-10-05 08:11:40 -0700198static struct usb_request *
Marek Vasut9ba89972014-02-06 02:43:45 +0100199ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
Lei Wena91ee2a2011-10-05 08:11:40 -0700200{
Stephen Warren9ac8b542014-05-29 14:53:02 -0600201 struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
202 int num;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600203 struct ci_req *ci_req;
204
Stephen Warren9ac8b542014-05-29 14:53:02 -0600205 num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
206 if (num == 0 && controller.ep0_req)
207 return &controller.ep0_req->req;
208
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600209 ci_req = memalign(ARCH_DMA_MINALIGN, sizeof(*ci_req));
210 if (!ci_req)
211 return NULL;
212
213 INIT_LIST_HEAD(&ci_req->queue);
214 ci_req->b_buf = 0;
215
Stephen Warren9ac8b542014-05-29 14:53:02 -0600216 if (num == 0)
217 controller.ep0_req = ci_req;
218
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600219 return &ci_req->req;
Lei Wena91ee2a2011-10-05 08:11:40 -0700220}
221
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600222static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
Lei Wena91ee2a2011-10-05 08:11:40 -0700223{
Stephen Warren10fecbd2014-06-10 11:02:36 -0600224 struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
225 struct ci_req *ci_req = container_of(req, struct ci_req, req);
226 int num;
227
228 num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
229 if (num == 0)
230 controller.ep0_req = 0;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600231
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600232 if (ci_req->b_buf)
233 free(ci_req->b_buf);
234 free(ci_req);
Lei Wena91ee2a2011-10-05 08:11:40 -0700235}
236
Troy Kisky256a3172013-10-10 15:28:00 -0700237static void ep_enable(int num, int in, int maxpacket)
Lei Wena91ee2a2011-10-05 08:11:40 -0700238{
Marek Vasut9ba89972014-02-06 02:43:45 +0100239 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700240 unsigned n;
Lei Wena91ee2a2011-10-05 08:11:40 -0700241
242 n = readl(&udc->epctrl[num]);
243 if (in)
244 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
245 else
246 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
247
Marek Vasut58465342013-07-10 03:16:42 +0200248 if (num != 0) {
Marek Vasut9ba89972014-02-06 02:43:45 +0100249 struct ept_queue_head *head = ci_get_qh(num, in);
Troy Kisky06bb97a2013-10-10 15:28:02 -0700250
Troy Kisky256a3172013-10-10 15:28:00 -0700251 head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
Marek Vasut9ba89972014-02-06 02:43:45 +0100252 ci_flush_qh(num);
Marek Vasut58465342013-07-10 03:16:42 +0200253 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700254 writel(n, &udc->epctrl[num]);
255}
256
Marek Vasut9ba89972014-02-06 02:43:45 +0100257static int ci_ep_enable(struct usb_ep *ep,
Lei Wena91ee2a2011-10-05 08:11:40 -0700258 const struct usb_endpoint_descriptor *desc)
259{
Marek Vasut9ba89972014-02-06 02:43:45 +0100260 struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
Lei Wena91ee2a2011-10-05 08:11:40 -0700261 int num, in;
262 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
263 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasut9ba89972014-02-06 02:43:45 +0100264 ci_ep->desc = desc;
Troy Kisky256a3172013-10-10 15:28:00 -0700265
266 if (num) {
267 int max = get_unaligned_le16(&desc->wMaxPacketSize);
268
269 if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
270 max = 64;
271 if (ep->maxpacket != max) {
272 DBG("%s: from %d to %d\n", __func__,
273 ep->maxpacket, max);
274 ep->maxpacket = max;
275 }
276 }
277 ep_enable(num, in, ep->maxpacket);
278 DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
Lei Wena91ee2a2011-10-05 08:11:40 -0700279 return 0;
280}
281
Marek Vasut9ba89972014-02-06 02:43:45 +0100282static int ci_ep_disable(struct usb_ep *ep)
Lei Wena91ee2a2011-10-05 08:11:40 -0700283{
Marek Vasut9ba89972014-02-06 02:43:45 +0100284 struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
Troy Kisky35494072013-09-25 18:41:15 -0700285
Marek Vasut9ba89972014-02-06 02:43:45 +0100286 ci_ep->desc = NULL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700287 return 0;
288}
289
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600290static int ci_bounce(struct ci_req *ci_req, int in)
Marek Vasute0fc5822013-07-10 03:16:43 +0200291{
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600292 struct usb_request *req = &ci_req->req;
293 uint32_t addr = (uint32_t)req->buf;
294 uint32_t hwaddr;
295 uint32_t aligned_used_len;
Marek Vasute0fc5822013-07-10 03:16:43 +0200296
297 /* Input buffer address is not aligned. */
298 if (addr & (ARCH_DMA_MINALIGN - 1))
299 goto align;
300
301 /* Input buffer length is not aligned. */
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600302 if (req->length & (ARCH_DMA_MINALIGN - 1))
Marek Vasute0fc5822013-07-10 03:16:43 +0200303 goto align;
304
305 /* The buffer is well aligned, only flush cache. */
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600306 ci_req->hw_len = req->length;
307 ci_req->hw_buf = req->buf;
Marek Vasute0fc5822013-07-10 03:16:43 +0200308 goto flush;
309
310align:
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600311 if (ci_req->b_buf && req->length > ci_req->b_len) {
312 free(ci_req->b_buf);
313 ci_req->b_buf = 0;
314 }
315 if (!ci_req->b_buf) {
316 ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
317 ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
318 if (!ci_req->b_buf)
Marek Vasute0fc5822013-07-10 03:16:43 +0200319 return -ENOMEM;
320 }
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600321 ci_req->hw_len = ci_req->b_len;
322 ci_req->hw_buf = ci_req->b_buf;
323
Troy Kisky1567b882013-10-10 15:28:01 -0700324 if (in)
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600325 memcpy(ci_req->hw_buf, req->buf, req->length);
Marek Vasute0fc5822013-07-10 03:16:43 +0200326
327flush:
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600328 hwaddr = (uint32_t)ci_req->hw_buf;
329 aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
330 flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
Marek Vasute0fc5822013-07-10 03:16:43 +0200331
332 return 0;
333}
334
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600335static void ci_debounce(struct ci_req *ci_req, int in)
Marek Vasute0fc5822013-07-10 03:16:43 +0200336{
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600337 struct usb_request *req = &ci_req->req;
338 uint32_t addr = (uint32_t)req->buf;
339 uint32_t hwaddr = (uint32_t)ci_req->hw_buf;
340 uint32_t aligned_used_len;
Marek Vasute0fc5822013-07-10 03:16:43 +0200341
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600342 if (in)
343 return;
344
345 aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
346 invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
Marek Vasute0fc5822013-07-10 03:16:43 +0200347
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600348 if (addr == hwaddr)
349 return; /* not a bounce */
Marek Vasute0fc5822013-07-10 03:16:43 +0200350
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600351 memcpy(req->buf, ci_req->hw_buf, req->actual);
Marek Vasute0fc5822013-07-10 03:16:43 +0200352}
353
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600354static void ci_ep_submit_next_request(struct ci_ep *ci_ep)
Lei Wena91ee2a2011-10-05 08:11:40 -0700355{
Marek Vasut9ba89972014-02-06 02:43:45 +0100356 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700357 struct ept_queue_item *item;
358 struct ept_queue_head *head;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600359 int bit, num, len, in;
360 struct ci_req *ci_req;
361
362 ci_ep->req_primed = true;
363
Marek Vasut9ba89972014-02-06 02:43:45 +0100364 num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
365 in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
366 item = ci_get_qtd(num, in);
367 head = ci_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700368
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600369 ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
370 len = ci_req->req.length;
Marek Vasute0fc5822013-07-10 03:16:43 +0200371
Lei Wena91ee2a2011-10-05 08:11:40 -0700372 item->next = TERMINATE;
373 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600374 item->page0 = (uint32_t)ci_req->hw_buf;
375 item->page1 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x1000;
376 item->page2 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x2000;
377 item->page3 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x3000;
378 item->page4 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x4000;
Marek Vasut9ba89972014-02-06 02:43:45 +0100379 ci_flush_qtd(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700380
381 head->next = (unsigned) item;
382 head->info = 0;
383
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600384 DBG("ept%d %s queue len %x, req %p, buffer %p\n",
385 num, in ? "in" : "out", len, ci_req, ci_req->hw_buf);
Marek Vasut9ba89972014-02-06 02:43:45 +0100386 ci_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700387
388 if (in)
389 bit = EPT_TX(num);
390 else
391 bit = EPT_RX(num);
392
Lei Wena91ee2a2011-10-05 08:11:40 -0700393 writel(bit, &udc->epprime);
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600394}
395
396static int ci_ep_queue(struct usb_ep *ep,
397 struct usb_request *req, gfp_t gfp_flags)
398{
399 struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
400 struct ci_req *ci_req = container_of(req, struct ci_req, req);
401 int in, ret;
402 int __maybe_unused num;
403
404 num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
405 in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
406
Stephen Warren62237cf2014-05-29 14:53:00 -0600407 if (!num && ci_ep->req_primed) {
408 /*
409 * The flipping of ep0 between IN and OUT relies on
410 * ci_ep_queue consuming the current IN/OUT setting
411 * immediately. If this is deferred to a later point when the
412 * req is pulled out of ci_req->queue, then the IN/OUT setting
413 * may have been changed since the req was queued, and state
414 * will get out of sync. This condition doesn't occur today,
415 * but could if bugs were introduced later, and this error
416 * check will save a lot of debugging time.
417 */
418 printf("%s: ep0 transaction already in progress\n", __func__);
419 return -EPROTO;
420 }
421
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600422 ret = ci_bounce(ci_req, in);
423 if (ret)
424 return ret;
425
426 DBG("ept%d %s pre-queue req %p, buffer %p\n",
427 num, in ? "in" : "out", ci_req, ci_req->hw_buf);
428 list_add_tail(&ci_req->queue, &ci_ep->queue);
429
430 if (!ci_ep->req_primed)
431 ci_ep_submit_next_request(ci_ep);
Lei Wena91ee2a2011-10-05 08:11:40 -0700432
433 return 0;
434}
435
Stephen Warrenaa449fa2014-05-29 14:53:03 -0600436static void flip_ep0_direction(void)
437{
438 if (ep0_desc.bEndpointAddress == USB_DIR_IN) {
439 DBG("%s: Flipping ep0 ot OUT\n", __func__);
440 ep0_desc.bEndpointAddress = 0;
441 } else {
442 DBG("%s: Flipping ep0 ot IN\n", __func__);
443 ep0_desc.bEndpointAddress = USB_DIR_IN;
444 }
445}
446
Marek Vasut9ba89972014-02-06 02:43:45 +0100447static void handle_ep_complete(struct ci_ep *ep)
Lei Wena91ee2a2011-10-05 08:11:40 -0700448{
449 struct ept_queue_item *item;
450 int num, in, len;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600451 struct ci_req *ci_req;
452
Lei Wena91ee2a2011-10-05 08:11:40 -0700453 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
454 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasut9ba89972014-02-06 02:43:45 +0100455 item = ci_get_qtd(num, in);
456 ci_invalidate_qtd(num);
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +0200457
Stephen Warren8c02b612014-05-13 10:51:54 -0600458 len = (item->info >> 16) & 0x7fff;
Lei Wena91ee2a2011-10-05 08:11:40 -0700459 if (item->info & 0xff)
Troy Kisky3703c452013-09-25 18:41:08 -0700460 printf("EP%d/%s FAIL info=%x pg0=%x\n",
461 num, in ? "in" : "out", item->info, item->page0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700462
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600463 ci_req = list_first_entry(&ep->queue, struct ci_req, queue);
464 list_del_init(&ci_req->queue);
465 ep->req_primed = false;
466
467 if (!list_empty(&ep->queue))
468 ci_ep_submit_next_request(ep);
469
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600470 ci_req->req.actual = ci_req->req.length - len;
471 ci_debounce(ci_req, in);
Troy Kisky1567b882013-10-10 15:28:01 -0700472
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600473 DBG("ept%d %s req %p, complete %x\n",
474 num, in ? "in" : "out", ci_req, len);
Stephen Warrenaa449fa2014-05-29 14:53:03 -0600475 if (num != 0 || controller.ep0_data_phase)
476 ci_req->req.complete(&ep->ep, &ci_req->req);
477 if (num == 0 && controller.ep0_data_phase) {
478 /*
479 * Data Stage is complete, so flip ep0 dir for Status Stage,
480 * which always transfers a packet in the opposite direction.
481 */
482 DBG("%s: flip ep0 dir for Status Stage\n", __func__);
483 flip_ep0_direction();
484 controller.ep0_data_phase = false;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600485 ci_req->req.length = 0;
486 usb_ep_queue(&ep->ep, &ci_req->req, 0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700487 }
488}
489
490#define SETUP(type, request) (((type) << 8) | (request))
491
492static void handle_setup(void)
493{
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600494 struct ci_ep *ci_ep = &controller.ep[0];
495 struct ci_req *ci_req;
496 struct usb_request *req;
Marek Vasut9ba89972014-02-06 02:43:45 +0100497 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700498 struct ept_queue_head *head;
499 struct usb_ctrlrequest r;
500 int status = 0;
501 int num, in, _num, _in, i;
502 char *buf;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600503
Stephen Warren9ac8b542014-05-29 14:53:02 -0600504 ci_req = controller.ep0_req;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600505 req = &ci_req->req;
Marek Vasut9ba89972014-02-06 02:43:45 +0100506 head = ci_get_qh(0, 0); /* EP0 OUT */
Lei Wena91ee2a2011-10-05 08:11:40 -0700507
Marek Vasut9ba89972014-02-06 02:43:45 +0100508 ci_invalidate_qh(0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700509 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
Stephen Warrene13a7042014-04-24 17:52:39 -0600510#ifdef CONFIG_CI_UDC_HAS_HOSTPC
511 writel(EPT_RX(0), &udc->epsetupstat);
512#else
Lei Wena91ee2a2011-10-05 08:11:40 -0700513 writel(EPT_RX(0), &udc->epstat);
Stephen Warrene13a7042014-04-24 17:52:39 -0600514#endif
Stephen Warrenaa449fa2014-05-29 14:53:03 -0600515 DBG("handle setup %s, %x, %x index %x value %x length %x\n",
516 reqname(r.bRequest), r.bRequestType, r.bRequest, r.wIndex,
517 r.wValue, r.wLength);
518
519 /* Set EP0 dir for Data Stage based on Setup Stage data */
520 if (r.bRequestType & USB_DIR_IN) {
521 DBG("%s: Set ep0 to IN for Data Stage\n", __func__);
522 ep0_desc.bEndpointAddress = USB_DIR_IN;
523 } else {
524 DBG("%s: Set ep0 to OUT for Data Stage\n", __func__);
525 ep0_desc.bEndpointAddress = 0;
526 }
527 if (r.wLength) {
528 controller.ep0_data_phase = true;
529 } else {
530 /* 0 length -> no Data Stage. Flip dir for Status Stage */
531 DBG("%s: 0 length: flip ep0 dir for Status Stage\n", __func__);
532 flip_ep0_direction();
533 controller.ep0_data_phase = false;
534 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700535
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600536 list_del_init(&ci_req->queue);
537 ci_ep->req_primed = false;
538
Lei Wena91ee2a2011-10-05 08:11:40 -0700539 switch (SETUP(r.bRequestType, r.bRequest)) {
540 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
541 _num = r.wIndex & 15;
542 _in = !!(r.wIndex & 0x80);
543
544 if ((r.wValue == 0) && (r.wLength == 0)) {
545 req->length = 0;
546 for (i = 0; i < NUM_ENDPOINTS; i++) {
Marek Vasut9ba89972014-02-06 02:43:45 +0100547 struct ci_ep *ep = &controller.ep[i];
Troy Kisky256a3172013-10-10 15:28:00 -0700548
549 if (!ep->desc)
Lei Wena91ee2a2011-10-05 08:11:40 -0700550 continue;
Troy Kisky256a3172013-10-10 15:28:00 -0700551 num = ep->desc->bEndpointAddress
Marek Vasut98377322013-07-10 03:16:29 +0200552 & USB_ENDPOINT_NUMBER_MASK;
Troy Kisky256a3172013-10-10 15:28:00 -0700553 in = (ep->desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700554 & USB_DIR_IN) != 0;
555 if ((num == _num) && (in == _in)) {
Troy Kisky256a3172013-10-10 15:28:00 -0700556 ep_enable(num, in, ep->ep.maxpacket);
Lei Wena91ee2a2011-10-05 08:11:40 -0700557 usb_ep_queue(controller.gadget.ep0,
558 req, 0);
559 break;
560 }
561 }
562 }
563 return;
564
565 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
566 /*
567 * write address delayed (will take effect
568 * after the next IN txn)
569 */
570 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
571 req->length = 0;
572 usb_ep_queue(controller.gadget.ep0, req, 0);
573 return;
574
575 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
576 req->length = 2;
577 buf = (char *)req->buf;
578 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
579 buf[1] = 0;
580 usb_ep_queue(controller.gadget.ep0, req, 0);
581 return;
582 }
583 /* pass request up to the gadget driver */
584 if (controller.driver)
585 status = controller.driver->setup(&controller.gadget, &r);
586 else
587 status = -ENODEV;
588
589 if (!status)
590 return;
591 DBG("STALL reqname %s type %x value %x, index %x\n",
592 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
593 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
594}
595
596static void stop_activity(void)
597{
598 int i, num, in;
599 struct ept_queue_head *head;
Marek Vasut9ba89972014-02-06 02:43:45 +0100600 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700601 writel(readl(&udc->epcomp), &udc->epcomp);
Stephen Warrene13a7042014-04-24 17:52:39 -0600602#ifdef CONFIG_CI_UDC_HAS_HOSTPC
603 writel(readl(&udc->epsetupstat), &udc->epsetupstat);
604#endif
Lei Wena91ee2a2011-10-05 08:11:40 -0700605 writel(readl(&udc->epstat), &udc->epstat);
606 writel(0xffffffff, &udc->epflush);
607
608 /* error out any pending reqs */
609 for (i = 0; i < NUM_ENDPOINTS; i++) {
610 if (i != 0)
611 writel(0, &udc->epctrl[i]);
Marek Vasut98377322013-07-10 03:16:29 +0200612 if (controller.ep[i].desc) {
613 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700614 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200615 in = (controller.ep[i].desc->bEndpointAddress
616 & USB_DIR_IN) != 0;
Marek Vasut9ba89972014-02-06 02:43:45 +0100617 head = ci_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700618 head->info = INFO_ACTIVE;
Marek Vasut9ba89972014-02-06 02:43:45 +0100619 ci_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700620 }
621 }
622}
623
624void udc_irq(void)
625{
Marek Vasut9ba89972014-02-06 02:43:45 +0100626 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700627 unsigned n = readl(&udc->usbsts);
628 writel(n, &udc->usbsts);
629 int bit, i, num, in;
630
631 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
632 if (n == 0)
633 return;
634
635 if (n & STS_URI) {
636 DBG("-- reset --\n");
637 stop_activity();
638 }
639 if (n & STS_SLI)
640 DBG("-- suspend --\n");
641
642 if (n & STS_PCI) {
Troy Kisky256a3172013-10-10 15:28:00 -0700643 int max = 64;
644 int speed = USB_SPEED_FULL;
645
Stephen Warrene13a7042014-04-24 17:52:39 -0600646#ifdef CONFIG_CI_UDC_HAS_HOSTPC
647 bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
648#else
Lei Wena91ee2a2011-10-05 08:11:40 -0700649 bit = (readl(&udc->portsc) >> 26) & 3;
Stephen Warrene13a7042014-04-24 17:52:39 -0600650#endif
Troy Kisky256a3172013-10-10 15:28:00 -0700651 DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
Lei Wena91ee2a2011-10-05 08:11:40 -0700652 if (bit == 2) {
Troy Kisky256a3172013-10-10 15:28:00 -0700653 speed = USB_SPEED_HIGH;
654 max = 512;
655 }
656 controller.gadget.speed = speed;
657 for (i = 1; i < NUM_ENDPOINTS; i++) {
658 if (controller.ep[i].ep.maxpacket > max)
659 controller.ep[i].ep.maxpacket = max;
Lei Wena91ee2a2011-10-05 08:11:40 -0700660 }
661 }
662
663 if (n & STS_UEI)
664 printf("<UEI %x>\n", readl(&udc->epcomp));
665
666 if ((n & STS_UI) || (n & STS_UEI)) {
Stephen Warrene13a7042014-04-24 17:52:39 -0600667#ifdef CONFIG_CI_UDC_HAS_HOSTPC
668 n = readl(&udc->epsetupstat);
669#else
Lei Wena91ee2a2011-10-05 08:11:40 -0700670 n = readl(&udc->epstat);
Stephen Warrene13a7042014-04-24 17:52:39 -0600671#endif
Lei Wena91ee2a2011-10-05 08:11:40 -0700672 if (n & EPT_RX(0))
673 handle_setup();
674
675 n = readl(&udc->epcomp);
676 if (n != 0)
677 writel(n, &udc->epcomp);
678
679 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200680 if (controller.ep[i].desc) {
681 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700682 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200683 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700684 & USB_DIR_IN) != 0;
685 bit = (in) ? EPT_TX(num) : EPT_RX(num);
686 if (n & bit)
Marek Vasut98377322013-07-10 03:16:29 +0200687 handle_ep_complete(&controller.ep[i]);
Lei Wena91ee2a2011-10-05 08:11:40 -0700688 }
689 }
690 }
691}
692
693int usb_gadget_handle_interrupts(void)
694{
695 u32 value;
Marek Vasut9ba89972014-02-06 02:43:45 +0100696 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700697
698 value = readl(&udc->usbsts);
699 if (value)
700 udc_irq();
701
702 return value;
703}
704
Stephen Warren6e9aa0d2014-06-10 11:02:35 -0600705void udc_disconnect(void)
706{
707 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
708 /* disable pullup */
709 stop_activity();
710 writel(USBCMD_FS2, &udc->usbcmd);
711 udelay(800);
712 if (controller.driver)
713 controller.driver->disconnect(&controller.gadget);
714}
715
Marek Vasut9ba89972014-02-06 02:43:45 +0100716static int ci_pullup(struct usb_gadget *gadget, int is_on)
Lei Wena91ee2a2011-10-05 08:11:40 -0700717{
Marek Vasut9ba89972014-02-06 02:43:45 +0100718 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700719 if (is_on) {
720 /* RESET */
721 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
722 udelay(200);
723
Marek Vasut9212f892013-07-10 03:16:39 +0200724 writel((unsigned)controller.epts, &udc->epinitaddr);
Lei Wena91ee2a2011-10-05 08:11:40 -0700725
726 /* select DEVICE mode */
727 writel(USBMODE_DEVICE, &udc->usbmode);
728
729 writel(0xffffffff, &udc->epflush);
730
731 /* Turn on the USB connection by enabling the pullup resistor */
732 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
733 } else {
Stephen Warren6e9aa0d2014-06-10 11:02:35 -0600734 udc_disconnect();
Lei Wena91ee2a2011-10-05 08:11:40 -0700735 }
736
737 return 0;
738}
739
Marek Vasut9ba89972014-02-06 02:43:45 +0100740static int ci_udc_probe(void)
Lei Wena91ee2a2011-10-05 08:11:40 -0700741{
742 struct ept_queue_head *head;
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200743 uint8_t *imem;
Lei Wena91ee2a2011-10-05 08:11:40 -0700744 int i;
Marek Vasut456707d2013-07-10 03:16:37 +0200745
Marek Vasut806d76c2013-07-10 03:16:34 +0200746 const int num = 2 * NUM_ENDPOINTS;
Lei Wena91ee2a2011-10-05 08:11:40 -0700747
Marek Vasut456707d2013-07-10 03:16:37 +0200748 const int eplist_min_align = 4096;
749 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
750 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
751 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
752
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200753 const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
754 const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
755 const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
756 const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
757
Marek Vasut456707d2013-07-10 03:16:37 +0200758 /* The QH list must be aligned to 4096 bytes. */
759 controller.epts = memalign(eplist_align, eplist_sz);
760 if (!controller.epts)
761 return -ENOMEM;
762 memset(controller.epts, 0, eplist_sz);
763
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200764 /*
765 * Each qTD item must be 32-byte aligned, each qTD touple must be
766 * cacheline aligned. There are two qTD items for each endpoint and
767 * only one of them is used for the endpoint at time, so we can group
768 * them together.
769 */
770 controller.items_mem = memalign(ilist_align, ilist_sz);
771 if (!controller.items_mem) {
772 free(controller.epts);
773 return -ENOMEM;
774 }
Troy Kisky0dee8ce2013-09-25 18:41:14 -0700775 memset(controller.items_mem, 0, ilist_sz);
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200776
Lei Wena91ee2a2011-10-05 08:11:40 -0700777 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
778 /*
Marek Vasut456707d2013-07-10 03:16:37 +0200779 * Configure QH for each endpoint. The structure of the QH list
780 * is such that each two subsequent fields, N and N+1 where N is
781 * even, in the QH list represent QH for one endpoint. The Nth
782 * entry represents OUT configuration and the N+1th entry does
783 * represent IN configuration of the endpoint.
Lei Wena91ee2a2011-10-05 08:11:40 -0700784 */
Marek Vasut393004e2013-07-10 03:16:36 +0200785 head = controller.epts + i;
Lei Wena91ee2a2011-10-05 08:11:40 -0700786 if (i < 2)
787 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
788 | CONFIG_ZLT | CONFIG_IOS;
789 else
790 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
791 | CONFIG_ZLT;
792 head->next = TERMINATE;
793 head->info = 0;
794
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200795 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
796 if (i & 1)
797 imem += sizeof(struct ept_queue_item);
798
799 controller.items[i] = (struct ept_queue_item *)imem;
Marek Vasut58465342013-07-10 03:16:42 +0200800
801 if (i & 1) {
Marek Vasut9ba89972014-02-06 02:43:45 +0100802 ci_flush_qh(i - 1);
803 ci_flush_qtd(i - 1);
Marek Vasut58465342013-07-10 03:16:42 +0200804 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700805 }
806
807 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200808
809 /* Init EP 0 */
Marek Vasut9ba89972014-02-06 02:43:45 +0100810 memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
Stephen Warrenb3dc4222014-05-29 14:53:01 -0600811 controller.ep[0].desc = &ep0_desc;
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600812 INIT_LIST_HEAD(&controller.ep[0].queue);
813 controller.ep[0].req_primed = false;
Marek Vasute1263412013-07-10 03:16:30 +0200814 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wena91ee2a2011-10-05 08:11:40 -0700815 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200816
817 /* Init EP 1..n */
818 for (i = 1; i < NUM_ENDPOINTS; i++) {
Marek Vasut9ba89972014-02-06 02:43:45 +0100819 memcpy(&controller.ep[i].ep, &ci_ep_init[1],
820 sizeof(*ci_ep_init));
Stephen Warrenaa5e8642014-05-05 17:48:11 -0600821 INIT_LIST_HEAD(&controller.ep[i].queue);
822 controller.ep[i].req_primed = false;
Marek Vasute1263412013-07-10 03:16:30 +0200823 list_add_tail(&controller.ep[i].ep.ep_list,
824 &controller.gadget.ep_list);
Lei Wena91ee2a2011-10-05 08:11:40 -0700825 }
Marek Vasute1263412013-07-10 03:16:30 +0200826
Stephen Warren9ac8b542014-05-29 14:53:02 -0600827 ci_ep_alloc_request(&controller.ep[0].ep, 0);
828 if (!controller.ep0_req) {
829 free(controller.epts);
830 return -ENOMEM;
831 }
832
Lei Wena91ee2a2011-10-05 08:11:40 -0700833 return 0;
834}
835
836int usb_gadget_register_driver(struct usb_gadget_driver *driver)
837{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200838 int ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700839
Marek Vasutfe67fe72013-07-10 03:16:33 +0200840 if (!driver)
841 return -EINVAL;
842 if (!driver->bind || !driver->setup || !driver->disconnect)
843 return -EINVAL;
844 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
Lei Wena91ee2a2011-10-05 08:11:40 -0700845 return -EINVAL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700846
Troy Kisky8f9c49d2013-10-10 15:27:56 -0700847 ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200848 if (ret)
849 return ret;
850
Marek Vasut9ba89972014-02-06 02:43:45 +0100851 ret = ci_udc_probe();
Stephen Warren0381a7272014-04-24 17:52:38 -0600852#if defined(CONFIG_USB_EHCI_MX6) || defined(CONFIG_USB_EHCI_MXS)
853 /*
854 * FIXME: usb_lowlevel_init()->ehci_hcd_init() should be doing all
855 * HW-specific initialization, e.g. ULPI-vs-UTMI PHY selection
856 */
Marek Vasut1f4b4912013-07-10 03:16:32 +0200857 if (!ret) {
Stephen Warren0381a7272014-04-24 17:52:38 -0600858 struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200859
Lei Wena91ee2a2011-10-05 08:11:40 -0700860 /* select ULPI phy */
861 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
862 }
Stephen Warren0381a7272014-04-24 17:52:38 -0600863#endif
Marek Vasut1f4b4912013-07-10 03:16:32 +0200864
865 ret = driver->bind(&controller.gadget);
866 if (ret) {
867 DBG("driver->bind() returned %d\n", ret);
868 return ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700869 }
870 controller.driver = driver;
871
872 return 0;
873}
874
875int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
876{
877 return 0;
878}