blob: f99553df8d492f25caad08729ba31798227367fa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shen70a67fd2013-09-11 18:24:49 +08002/*
3 * Driver for the Atmel USBA high speed USB device controller
4 * [Original from Linux kernel: drivers/usb/gadget/atmel_usba_udc.c]
5 *
6 * Copyright (C) 2005-2013 Atmel Corporation
7 * Bo Shen <voice.shen@atmel.com>
Bo Shen70a67fd2013-09-11 18:24:49 +08008 */
9
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Bo Shen70a67fd2013-09-11 18:24:49 +080012#include <asm/gpio.h>
13#include <asm/hardware.h>
14#include <linux/list.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Bo Shen70a67fd2013-09-11 18:24:49 +080016#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18#include <linux/usb/atmel_usba_udc.h>
19#include <malloc.h>
Bo Shen70a67fd2013-09-11 18:24:49 +080020
21#include "atmel_usba_udc.h"
22
23static int vbus_is_present(struct usba_udc *udc)
24{
25 /* No Vbus detection: Assume always present */
26 return 1;
27}
28
29static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req)
30{
31 unsigned int transaction_len;
32
33 transaction_len = req->req.length - req->req.actual;
34 req->last_transaction = 1;
35 if (transaction_len > ep->ep.maxpacket) {
36 transaction_len = ep->ep.maxpacket;
37 req->last_transaction = 0;
38 } else if (transaction_len == ep->ep.maxpacket && req->req.zero) {
39 req->last_transaction = 0;
40 }
41
42 DBG(DBG_QUEUE, "%s: submit_transaction, req %p (length %d)%s\n",
43 ep->ep.name, req, transaction_len,
44 req->last_transaction ? ", done" : "");
45
46 memcpy(ep->fifo, req->req.buf + req->req.actual, transaction_len);
47 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
48 req->req.actual += transaction_len;
49}
50
51static void submit_request(struct usba_ep *ep, struct usba_request *req)
52{
53 DBG(DBG_QUEUE, "%s: submit_request: req %p (length %d), dma: %d\n",
54 ep->ep.name, req, req->req.length, req->using_dma);
55
56 req->req.actual = 0;
57 req->submitted = 1;
58
59 next_fifo_transaction(ep, req);
Artur Rojekfbb5c152023-09-13 17:00:58 +020060 if (ep_is_control(ep))
Bo Shen70a67fd2013-09-11 18:24:49 +080061 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
Artur Rojekfbb5c152023-09-13 17:00:58 +020062 usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
Bo Shen70a67fd2013-09-11 18:24:49 +080063}
64
65static void submit_next_request(struct usba_ep *ep)
66{
67 struct usba_request *req;
68
69 if (list_empty(&ep->queue)) {
70 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY | USBA_RX_BK_RDY);
71 return;
72 }
73
74 req = list_entry(ep->queue.next, struct usba_request, queue);
75 if (!req->submitted)
76 submit_request(ep, req);
77}
78
79static void send_status(struct usba_udc *udc, struct usba_ep *ep)
80{
81 ep->state = STATUS_STAGE_IN;
82 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
83 usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
84}
85
86static void receive_data(struct usba_ep *ep)
87{
88 struct usba_udc *udc = ep->udc;
89 struct usba_request *req;
90 unsigned long status;
91 unsigned int bytecount, nr_busy;
92 int is_complete = 0;
93
94 status = usba_ep_readl(ep, STA);
95 nr_busy = USBA_BFEXT(BUSY_BANKS, status);
96
97 DBG(DBG_QUEUE, "receive data: nr_busy=%u\n", nr_busy);
98
99 while (nr_busy > 0) {
100 if (list_empty(&ep->queue)) {
101 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
102 break;
103 }
104 req = list_entry(ep->queue.next,
105 struct usba_request, queue);
106
107 bytecount = USBA_BFEXT(BYTE_COUNT, status);
108
109 if (status & USBA_SHORT_PACKET)
110 is_complete = 1;
111 if (req->req.actual + bytecount >= req->req.length) {
112 is_complete = 1;
113 bytecount = req->req.length - req->req.actual;
114 }
115
116 memcpy(req->req.buf + req->req.actual, ep->fifo, bytecount);
117 req->req.actual += bytecount;
118
119 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
120
121 if (is_complete) {
122 DBG(DBG_QUEUE, "%s: request done\n", ep->ep.name);
123 req->req.status = 0;
124 list_del_init(&req->queue);
125 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
126 spin_lock(&udc->lock);
127 req->req.complete(&ep->ep, &req->req);
128 spin_unlock(&udc->lock);
129 }
130
131 status = usba_ep_readl(ep, STA);
132 nr_busy = USBA_BFEXT(BUSY_BANKS, status);
133
134 if (is_complete && ep_is_control(ep)) {
135 send_status(udc, ep);
136 break;
137 }
138 }
139}
140
141static void
142request_complete(struct usba_ep *ep, struct usba_request *req, int status)
143{
144 if (req->req.status == -EINPROGRESS)
145 req->req.status = status;
146
147 DBG(DBG_GADGET | DBG_REQ, "%s: req %p complete: status %d, actual %u\n",
148 ep->ep.name, req, req->req.status, req->req.actual);
149
150 req->req.complete(&ep->ep, &req->req);
151}
152
153static void
154request_complete_list(struct usba_ep *ep, struct list_head *list, int status)
155{
156 struct usba_request *req, *tmp_req;
157
158 list_for_each_entry_safe(req, tmp_req, list, queue) {
159 list_del_init(&req->queue);
160 request_complete(ep, req, status);
161 }
162}
163
164static int
165usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
166{
167 struct usba_ep *ep = to_usba_ep(_ep);
168 struct usba_udc *udc = ep->udc;
Bo Shen91981552014-08-27 17:28:17 +0800169 unsigned long flags = 0, ept_cfg, maxpacket;
Bo Shen70a67fd2013-09-11 18:24:49 +0800170 unsigned int nr_trans;
171
172 DBG(DBG_GADGET, "%s: ep_enable: desc=%p\n", ep->ep.name, desc);
173
174 maxpacket = usb_endpoint_maxp(desc) & 0x7ff;
175
176 if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
177 != ep->index) ||
178 ep->index == 0 ||
179 desc->bDescriptorType != USB_DT_ENDPOINT ||
180 maxpacket == 0 ||
181 maxpacket > ep->fifo_size) {
182 DBG(DBG_ERR, "ep_enable: Invalid argument");
183 return -EINVAL;
184 }
185
186 ep->is_isoc = 0;
187 ep->is_in = 0;
188
189 if (maxpacket <= 8)
190 ept_cfg = USBA_BF(EPT_SIZE, USBA_EPT_SIZE_8);
191 else
192 /* LSB is bit 1, not 0 */
193 ept_cfg = USBA_BF(EPT_SIZE, fls(maxpacket - 1) - 3);
194
195 DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
196 ep->ep.name, ept_cfg, maxpacket);
197
198 if (usb_endpoint_dir_in(desc)) {
199 ep->is_in = 1;
200 ept_cfg |= USBA_EPT_DIR_IN;
201 }
202
203 switch (usb_endpoint_type(desc)) {
204 case USB_ENDPOINT_XFER_CONTROL:
205 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL);
206 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE);
207 break;
208 case USB_ENDPOINT_XFER_ISOC:
209 if (!ep->can_isoc) {
210 DBG(DBG_ERR, "ep_enable: %s is not isoc capable\n",
211 ep->ep.name);
212 return -EINVAL;
213 }
214
215 /*
216 * Bits 11:12 specify number of _additional_
217 * transactions per microframe.
218 */
219 nr_trans = ((usb_endpoint_maxp(desc) >> 11) & 3) + 1;
220 if (nr_trans > 3)
221 return -EINVAL;
222
223 ep->is_isoc = 1;
224 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_ISO);
225
226 /*
227 * Do triple-buffering on high-bandwidth iso endpoints.
228 */
229 if (nr_trans > 1 && ep->nr_banks == 3)
230 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_TRIPLE);
231 else
232 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
233 ept_cfg |= USBA_BF(NB_TRANS, nr_trans);
234 break;
235 case USB_ENDPOINT_XFER_BULK:
236 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK);
237 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE);
238 break;
239 case USB_ENDPOINT_XFER_INT:
240 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_INT);
241 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE);
242 break;
243 }
244
245 spin_lock_irqsave(&ep->udc->lock, flags);
246
247 ep->desc = desc;
248 ep->ep.maxpacket = maxpacket;
249
250 usba_ep_writel(ep, CFG, ept_cfg);
251 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
252
253 usba_writel(udc, INT_ENB,
254 (usba_readl(udc, INT_ENB)
255 | USBA_BF(EPT_INT, 1 << ep->index)));
256
257 spin_unlock_irqrestore(&udc->lock, flags);
258
259 DBG(DBG_HW, "EPT_CFG%d after init: %#08lx\n", ep->index,
260 (unsigned long)usba_ep_readl(ep, CFG));
261 DBG(DBG_HW, "INT_ENB after init: %#08lx\n",
262 (unsigned long)usba_readl(udc, INT_ENB));
263
264 return 0;
265}
266
267static int usba_ep_disable(struct usb_ep *_ep)
268{
269 struct usba_ep *ep = to_usba_ep(_ep);
270 struct usba_udc *udc = ep->udc;
271 LIST_HEAD(req_list);
Bo Shen91981552014-08-27 17:28:17 +0800272 unsigned long flags = 0;
Bo Shen70a67fd2013-09-11 18:24:49 +0800273
274 DBG(DBG_GADGET, "ep_disable: %s\n", ep->ep.name);
275
276 spin_lock_irqsave(&udc->lock, flags);
277
278 if (!ep->desc) {
279 spin_unlock_irqrestore(&udc->lock, flags);
280 /* REVISIT because this driver disables endpoints in
281 * reset_all_endpoints() before calling disconnect(),
282 * most gadget drivers would trigger this non-error ...
283 */
284 if (udc->gadget.speed != USB_SPEED_UNKNOWN)
285 DBG(DBG_ERR, "ep_disable: %s not enabled\n",
286 ep->ep.name);
287 return -EINVAL;
288 }
289 ep->desc = NULL;
290
291 list_splice_init(&ep->queue, &req_list);
292 usba_ep_writel(ep, CFG, 0);
293 usba_ep_writel(ep, CTL_DIS, USBA_EPT_ENABLE);
294 usba_writel(udc, INT_ENB,
295 usba_readl(udc, INT_ENB) &
296 ~USBA_BF(EPT_INT, 1 << ep->index));
297
298 request_complete_list(ep, &req_list, -ESHUTDOWN);
299
300 spin_unlock_irqrestore(&udc->lock, flags);
301
302 return 0;
303}
304
305static struct usb_request *
306usba_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
307{
308 struct usba_request *req;
309
310 DBG(DBG_GADGET, "ep_alloc_request: %p, 0x%x\n", _ep, gfp_flags);
311
Stephen Warren16a643e2014-07-01 16:59:08 -0600312 req = calloc(1, sizeof(struct usba_request));
Bo Shen70a67fd2013-09-11 18:24:49 +0800313 if (!req)
314 return NULL;
315
316 INIT_LIST_HEAD(&req->queue);
317
318 return &req->req;
319}
320
321static void
322usba_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
323{
324 struct usba_request *req = to_usba_req(_req);
325
326 DBG(DBG_GADGET, "ep_free_request: %p, %p\n", _ep, _req);
327
328 free(req);
329}
330
331static int
332usba_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
333{
334 struct usba_request *req = to_usba_req(_req);
335 struct usba_ep *ep = to_usba_ep(_ep);
336 struct usba_udc *udc = ep->udc;
Bo Shen91981552014-08-27 17:28:17 +0800337 unsigned long flags = 0;
Bo Shen70a67fd2013-09-11 18:24:49 +0800338 int ret;
339
340 DBG(DBG_GADGET | DBG_QUEUE | DBG_REQ, "%s: queue req %p, len %u\n",
341 ep->ep.name, req, _req->length);
342
343 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN ||
344 !ep->desc)
345 return -ESHUTDOWN;
346
347 req->submitted = 0;
348 req->using_dma = 0;
349 req->last_transaction = 0;
350
351 _req->status = -EINPROGRESS;
352 _req->actual = 0;
353
354 /* May have received a reset since last time we checked */
355 ret = -ESHUTDOWN;
356 spin_lock_irqsave(&udc->lock, flags);
357 if (ep->desc) {
358 list_add_tail(&req->queue, &ep->queue);
359
360 if ((!ep_is_control(ep) && ep->is_in) ||
361 (ep_is_control(ep) && (ep->state == DATA_STAGE_IN ||
362 ep->state == STATUS_STAGE_IN)))
363 usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
364 else
365 usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY);
366
367 ret = 0;
368 }
369 spin_unlock_irqrestore(&udc->lock, flags);
370
371 return ret;
372}
373
374static int usba_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
375{
376 struct usba_ep *ep = to_usba_ep(_ep);
377 struct usba_request *req = to_usba_req(_req);
378
379 DBG(DBG_GADGET | DBG_QUEUE, "ep_dequeue: %s, req %p\n",
380 ep->ep.name, req);
381
382 /*
383 * Errors should stop the queue from advancing until the
384 * completion function returns.
385 */
386 list_del_init(&req->queue);
387
388 request_complete(ep, req, -ECONNRESET);
389
390 /* Process the next request if any */
391 submit_next_request(ep);
392
393 return 0;
394}
395
396static int usba_ep_set_halt(struct usb_ep *_ep, int value)
397{
398 struct usba_ep *ep = to_usba_ep(_ep);
Bo Shen91981552014-08-27 17:28:17 +0800399 unsigned long flags = 0;
Bo Shen70a67fd2013-09-11 18:24:49 +0800400 int ret = 0;
401
402 DBG(DBG_GADGET, "endpoint %s: %s HALT\n", ep->ep.name,
403 value ? "set" : "clear");
404
405 if (!ep->desc) {
406 DBG(DBG_ERR, "Attempted to halt uninitialized ep %s\n",
407 ep->ep.name);
408 return -ENODEV;
409 }
410
411 if (ep->is_isoc) {
412 DBG(DBG_ERR, "Attempted to halt isochronous ep %s\n",
413 ep->ep.name);
414 return -ENOTTY;
415 }
416
417 spin_lock_irqsave(&udc->lock, flags);
418
419 /*
420 * We can't halt IN endpoints while there are still data to be
421 * transferred
422 */
423 if (!list_empty(&ep->queue) ||
424 ((value && ep->is_in && (usba_ep_readl(ep, STA) &
425 USBA_BF(BUSY_BANKS, -1L))))) {
426 ret = -EAGAIN;
427 } else {
428 if (value)
429 usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL);
430 else
431 usba_ep_writel(ep, CLR_STA,
432 USBA_FORCE_STALL | USBA_TOGGLE_CLR);
433 usba_ep_readl(ep, STA);
434 }
435
436 spin_unlock_irqrestore(&udc->lock, flags);
437
438 return ret;
439}
440
441static int usba_ep_fifo_status(struct usb_ep *_ep)
442{
443 struct usba_ep *ep = to_usba_ep(_ep);
444
445 return USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA));
446}
447
448static void usba_ep_fifo_flush(struct usb_ep *_ep)
449{
450 struct usba_ep *ep = to_usba_ep(_ep);
451 struct usba_udc *udc = ep->udc;
452
453 usba_writel(udc, EPT_RST, 1 << ep->index);
454}
455
456static const struct usb_ep_ops usba_ep_ops = {
457 .enable = usba_ep_enable,
458 .disable = usba_ep_disable,
459 .alloc_request = usba_ep_alloc_request,
460 .free_request = usba_ep_free_request,
461 .queue = usba_ep_queue,
462 .dequeue = usba_ep_dequeue,
463 .set_halt = usba_ep_set_halt,
464 .fifo_status = usba_ep_fifo_status,
465 .fifo_flush = usba_ep_fifo_flush,
466};
467
468static int usba_udc_get_frame(struct usb_gadget *gadget)
469{
470 struct usba_udc *udc = to_usba_udc(gadget);
471
472 return USBA_BFEXT(FRAME_NUMBER, usba_readl(udc, FNUM));
473}
474
475static int usba_udc_wakeup(struct usb_gadget *gadget)
476{
477 struct usba_udc *udc = to_usba_udc(gadget);
Bo Shen91981552014-08-27 17:28:17 +0800478 unsigned long flags = 0;
Bo Shen70a67fd2013-09-11 18:24:49 +0800479 u32 ctrl;
480 int ret = -EINVAL;
481
482 spin_lock_irqsave(&udc->lock, flags);
483 if (udc->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
484 ctrl = usba_readl(udc, CTRL);
485 usba_writel(udc, CTRL, ctrl | USBA_REMOTE_WAKE_UP);
486 ret = 0;
487 }
488 spin_unlock_irqrestore(&udc->lock, flags);
489
490 return ret;
491}
492
493static int
494usba_udc_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
495{
496 struct usba_udc *udc = to_usba_udc(gadget);
Bo Shen91981552014-08-27 17:28:17 +0800497 unsigned long flags = 0;
Bo Shen70a67fd2013-09-11 18:24:49 +0800498
499 spin_lock_irqsave(&udc->lock, flags);
500 if (is_selfpowered)
501 udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
502 else
503 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
504 spin_unlock_irqrestore(&udc->lock, flags);
505
506 return 0;
507}
508
509static const struct usb_gadget_ops usba_udc_ops = {
510 .get_frame = usba_udc_get_frame,
511 .wakeup = usba_udc_wakeup,
512 .set_selfpowered = usba_udc_set_selfpowered,
513};
514
515static struct usb_endpoint_descriptor usba_ep0_desc = {
516 .bLength = USB_DT_ENDPOINT_SIZE,
517 .bDescriptorType = USB_DT_ENDPOINT,
518 .bEndpointAddress = 0,
519 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
520 .wMaxPacketSize = cpu_to_le16(64),
521 /* FIXME: I have no idea what to put here */
522 .bInterval = 1,
523};
524
525/*
526 * Called with interrupts disabled and udc->lock held.
527 */
528static void reset_all_endpoints(struct usba_udc *udc)
529{
530 struct usba_ep *ep;
531 struct usba_request *req, *tmp_req;
532
533 usba_writel(udc, EPT_RST, ~0UL);
534
535 ep = to_usba_ep(udc->gadget.ep0);
536 list_for_each_entry_safe(req, tmp_req, &ep->queue, queue) {
537 list_del_init(&req->queue);
538 request_complete(ep, req, -ECONNRESET);
539 }
540
541 /* NOTE: normally, the next call to the gadget driver is in
542 * charge of disabling endpoints... usually disconnect().
543 * The exception would be entering a high speed test mode.
544 *
545 * FIXME remove this code ... and retest thoroughly.
546 */
547 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
548 if (ep->desc) {
549 spin_unlock(&udc->lock);
550 usba_ep_disable(&ep->ep);
551 spin_lock(&udc->lock);
552 }
553 }
554}
555
556static struct usba_ep *get_ep_by_addr(struct usba_udc *udc, u16 wIndex)
557{
558 struct usba_ep *ep;
559
560 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
561 return to_usba_ep(udc->gadget.ep0);
562
563 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
564 u8 bEndpointAddress;
565
566 if (!ep->desc)
567 continue;
568 bEndpointAddress = ep->desc->bEndpointAddress;
569 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
570 continue;
571 if ((bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
572 == (wIndex & USB_ENDPOINT_NUMBER_MASK))
573 return ep;
574 }
575
576 return NULL;
577}
578
579/* Called with interrupts disabled and udc->lock held */
580static inline void set_protocol_stall(struct usba_udc *udc, struct usba_ep *ep)
581{
582 usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL);
583 ep->state = WAIT_FOR_SETUP;
584}
585
586static inline int is_stalled(struct usba_udc *udc, struct usba_ep *ep)
587{
588 if (usba_ep_readl(ep, STA) & USBA_FORCE_STALL)
589 return 1;
590 return 0;
591}
592
593static inline void set_address(struct usba_udc *udc, unsigned int addr)
594{
595 u32 regval;
596
597 DBG(DBG_BUS, "setting address %u...\n", addr);
598 regval = usba_readl(udc, CTRL);
599 regval = USBA_BFINS(DEV_ADDR, addr, regval);
600 usba_writel(udc, CTRL, regval);
601}
602
603static int do_test_mode(struct usba_udc *udc)
604{
605 static const char test_packet_buffer[] = {
606 /* JKJKJKJK * 9 */
607 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
608 /* JJKKJJKK * 8 */
609 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
610 /* JJKKJJKK * 8 */
611 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
612 /* JJJJJJJKKKKKKK * 8 */
613 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
614 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
615 /* JJJJJJJK * 8 */
616 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
617 /* {JKKKKKKK * 10}, JK */
618 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
619 };
620 struct usba_ep *ep;
621 int test_mode;
622
623 test_mode = udc->test_mode;
624
625 /* Start from a clean slate */
626 reset_all_endpoints(udc);
627
628 switch (test_mode) {
629 case 0x0100:
630 /* Test_J */
631 usba_writel(udc, TST, USBA_TST_J_MODE);
632 DBG(DBG_ALL, "Entering Test_J mode...\n");
633 break;
634 case 0x0200:
635 /* Test_K */
636 usba_writel(udc, TST, USBA_TST_K_MODE);
637 DBG(DBG_ALL, "Entering Test_K mode...\n");
638 break;
639 case 0x0300:
640 /*
641 * Test_SE0_NAK: Force high-speed mode and set up ep0
642 * for Bulk IN transfers
643 */
644 ep = &udc->usba_ep[0];
645 usba_writel(udc, TST,
646 USBA_BF(SPEED_CFG, USBA_SPEED_CFG_FORCE_HIGH));
647 usba_ep_writel(ep, CFG,
648 USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64)
649 | USBA_EPT_DIR_IN
650 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK)
651 | USBA_BF(BK_NUMBER, 1));
652 if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) {
653 set_protocol_stall(udc, ep);
654 DBG(DBG_ALL, "Test_SE0_NAK: ep0 not mapped\n");
655 } else {
656 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
657 DBG(DBG_ALL, "Entering Test_SE0_NAK mode...\n");
658 }
659 break;
660 case 0x0400:
661 /* Test_Packet */
662 ep = &udc->usba_ep[0];
663 usba_ep_writel(ep, CFG,
664 USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64)
665 | USBA_EPT_DIR_IN
666 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK)
667 | USBA_BF(BK_NUMBER, 1));
668 if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) {
669 set_protocol_stall(udc, ep);
670 DBG(DBG_ALL, "Test_Packet: ep0 not mapped\n");
671 } else {
672 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
673 usba_writel(udc, TST, USBA_TST_PKT_MODE);
674 memcpy(ep->fifo, test_packet_buffer,
675 sizeof(test_packet_buffer));
676 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
677 DBG(DBG_ALL, "Entering Test_Packet mode...\n");
678 }
679 break;
680 default:
681 DBG(DBG_ERR, "Invalid test mode: 0x%04x\n", test_mode);
682 return -EINVAL;
683 }
684
685 return 0;
686}
687
688/* Avoid overly long expressions */
689static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest *crq)
690{
691 if (crq->wValue == cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP))
692 return true;
693 return false;
694}
695
696static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest *crq)
697{
698 if (crq->wValue == cpu_to_le16(USB_DEVICE_TEST_MODE))
699 return true;
700 return false;
701}
702
703static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq)
704{
705 if (crq->wValue == cpu_to_le16(USB_ENDPOINT_HALT))
706 return true;
707 return false;
708}
709
710static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
711 struct usb_ctrlrequest *crq)
712{
713 int retval = 0;
714
715 switch (crq->bRequest) {
716 case USB_REQ_GET_STATUS: {
717 u16 status;
718
719 if (crq->bRequestType == (USB_DIR_IN | USB_RECIP_DEVICE)) {
720 status = cpu_to_le16(udc->devstatus);
721 } else if (crq->bRequestType
722 == (USB_DIR_IN | USB_RECIP_INTERFACE)) {
723 status = cpu_to_le16(0);
724 } else if (crq->bRequestType
725 == (USB_DIR_IN | USB_RECIP_ENDPOINT)) {
726 struct usba_ep *target;
727
728 target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
729 if (!target)
730 goto stall;
731
732 status = 0;
733 if (is_stalled(udc, target))
734 status |= cpu_to_le16(1);
735 } else {
736 goto delegate;
737 }
738
739 /* Write directly to the FIFO. No queueing is done. */
740 if (crq->wLength != cpu_to_le16(sizeof(status)))
741 goto stall;
742 ep->state = DATA_STAGE_IN;
743 __raw_writew(status, ep->fifo);
744 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
745 break;
746 }
747
748 case USB_REQ_CLEAR_FEATURE: {
749 if (crq->bRequestType == USB_RECIP_DEVICE) {
750 if (feature_is_dev_remote_wakeup(crq))
751 udc->devstatus
752 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP);
753 else
754 /* Can't CLEAR_FEATURE TEST_MODE */
755 goto stall;
756 } else if (crq->bRequestType == USB_RECIP_ENDPOINT) {
757 struct usba_ep *target;
758
759 if (crq->wLength != cpu_to_le16(0) ||
760 !feature_is_ep_halt(crq))
761 goto stall;
762 target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
763 if (!target)
764 goto stall;
765
766 usba_ep_writel(target, CLR_STA, USBA_FORCE_STALL);
767 if (target->index != 0)
768 usba_ep_writel(target, CLR_STA,
769 USBA_TOGGLE_CLR);
770 } else {
771 goto delegate;
772 }
773
774 send_status(udc, ep);
775 break;
776 }
777
778 case USB_REQ_SET_FEATURE: {
779 if (crq->bRequestType == USB_RECIP_DEVICE) {
780 if (feature_is_dev_test_mode(crq)) {
781 send_status(udc, ep);
782 ep->state = STATUS_STAGE_TEST;
783 udc->test_mode = le16_to_cpu(crq->wIndex);
784 return 0;
785 } else if (feature_is_dev_remote_wakeup(crq)) {
786 udc->devstatus |= 1 << USB_DEVICE_REMOTE_WAKEUP;
787 } else {
788 goto stall;
789 }
790 } else if (crq->bRequestType == USB_RECIP_ENDPOINT) {
791 struct usba_ep *target;
792
793 if (crq->wLength != cpu_to_le16(0) ||
794 !feature_is_ep_halt(crq))
795 goto stall;
796
797 target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
798 if (!target)
799 goto stall;
800
801 usba_ep_writel(target, SET_STA, USBA_FORCE_STALL);
802 } else {
803 goto delegate;
804 }
805
806 send_status(udc, ep);
807 break;
808 }
809
810 case USB_REQ_SET_ADDRESS:
811 if (crq->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
812 goto delegate;
813
814 set_address(udc, le16_to_cpu(crq->wValue));
815 send_status(udc, ep);
816 ep->state = STATUS_STAGE_ADDR;
817 break;
818
819 default:
820delegate:
821 spin_unlock(&udc->lock);
822 retval = udc->driver->setup(&udc->gadget, crq);
823 spin_lock(&udc->lock);
824 }
825
826 return retval;
827
828stall:
829 DBG(DBG_ALL, "%s: Invalid setup request: %02x.%02x v%04x i%04x l%d\n",
830 ep->ep.name, crq->bRequestType, crq->bRequest,
831 le16_to_cpu(crq->wValue), le16_to_cpu(crq->wIndex),
832 le16_to_cpu(crq->wLength));
833 set_protocol_stall(udc, ep);
834
835 return -1;
836}
837
838static void usba_control_irq(struct usba_udc *udc, struct usba_ep *ep)
839{
840 struct usba_request *req;
841 u32 epstatus;
842 u32 epctrl;
843
844restart:
845 epstatus = usba_ep_readl(ep, STA);
846 epctrl = usba_ep_readl(ep, CTL);
847
848 DBG(DBG_INT, "%s [%d]: s/%08x c/%08x\n",
849 ep->ep.name, ep->state, epstatus, epctrl);
850
851 req = NULL;
852 if (!list_empty(&ep->queue))
853 req = list_entry(ep->queue.next,
854 struct usba_request, queue);
855
856 if ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
857 if (req->submitted)
858 next_fifo_transaction(ep, req);
859 else
860 submit_request(ep, req);
861
862 if (req->last_transaction) {
863 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
864 usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
865 }
866 goto restart;
867 }
868 if ((epstatus & epctrl) & USBA_TX_COMPLETE) {
869 usba_ep_writel(ep, CLR_STA, USBA_TX_COMPLETE);
870
871 switch (ep->state) {
872 case DATA_STAGE_IN:
873 usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY);
874 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
875 ep->state = STATUS_STAGE_OUT;
876 break;
877 case STATUS_STAGE_ADDR:
878 /* Activate our new address */
879 usba_writel(udc, CTRL, (usba_readl(udc, CTRL)
880 | USBA_FADDR_EN));
881 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
882 ep->state = WAIT_FOR_SETUP;
883 break;
884 case STATUS_STAGE_IN:
885 if (req) {
886 list_del_init(&req->queue);
887 request_complete(ep, req, 0);
Bo Shen70a67fd2013-09-11 18:24:49 +0800888 }
889 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
890 ep->state = WAIT_FOR_SETUP;
891 break;
892 case STATUS_STAGE_TEST:
893 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
894 ep->state = WAIT_FOR_SETUP;
895 if (do_test_mode(udc))
896 set_protocol_stall(udc, ep);
897 break;
898 default:
899 DBG(DBG_ALL, "%s: TXCOMP: Invalid endpoint state %d\n",
900 ep->ep.name, ep->state);
901 set_protocol_stall(udc, ep);
902 break;
903 }
904
905 goto restart;
906 }
907 if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
908 switch (ep->state) {
909 case STATUS_STAGE_OUT:
910 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
911 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
912
913 if (req) {
914 list_del_init(&req->queue);
915 request_complete(ep, req, 0);
916 }
917 ep->state = WAIT_FOR_SETUP;
918 break;
919
920 case DATA_STAGE_OUT:
921 receive_data(ep);
922 break;
923
924 default:
925 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
926 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
927 DBG(DBG_ALL, "%s: RXRDY: Invalid endpoint state %d\n",
928 ep->ep.name, ep->state);
929 set_protocol_stall(udc, ep);
930 break;
931 }
932
933 goto restart;
934 }
935 if (epstatus & USBA_RX_SETUP) {
936 union {
937 struct usb_ctrlrequest crq;
938 unsigned long data[2];
939 } crq;
940 unsigned int pkt_len;
941 int ret;
942
943 if (ep->state != WAIT_FOR_SETUP) {
944 /*
945 * Didn't expect a SETUP packet at this
946 * point. Clean up any pending requests (which
947 * may be successful).
948 */
949 int status = -EPROTO;
950
951 /*
952 * RXRDY and TXCOMP are dropped when SETUP
953 * packets arrive. Just pretend we received
954 * the status packet.
955 */
956 if (ep->state == STATUS_STAGE_OUT ||
957 ep->state == STATUS_STAGE_IN) {
958 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
959 status = 0;
960 }
961
962 if (req) {
963 list_del_init(&req->queue);
964 request_complete(ep, req, status);
965 }
966 }
967
968 pkt_len = USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA));
969 DBG(DBG_HW, "Packet length: %u\n", pkt_len);
970 if (pkt_len != sizeof(crq)) {
971 DBG(DBG_ALL, "udc: Invalid length %u (expected %zu)\n",
972 pkt_len, sizeof(crq));
973 set_protocol_stall(udc, ep);
974 return;
975 }
976
977 DBG(DBG_FIFO, "Copying ctrl request from 0x%p:\n", ep->fifo);
978 memcpy(crq.data, ep->fifo, sizeof(crq));
979
980 /* Free up one bank in the FIFO so that we can
981 * generate or receive a reply right away. */
982 usba_ep_writel(ep, CLR_STA, USBA_RX_SETUP);
983
984 if (crq.crq.bRequestType & USB_DIR_IN) {
985 /*
986 * The USB 2.0 spec states that "if wLength is
987 * zero, there is no data transfer phase."
988 * However, testusb #14 seems to actually
989 * expect a data phase even if wLength = 0...
990 */
991 ep->state = DATA_STAGE_IN;
992 } else {
993 if (crq.crq.wLength != cpu_to_le16(0))
994 ep->state = DATA_STAGE_OUT;
995 else
996 ep->state = STATUS_STAGE_IN;
997 }
998
999 ret = -1;
1000 if (ep->index == 0) {
1001 ret = handle_ep0_setup(udc, ep, &crq.crq);
1002 } else {
1003 spin_unlock(&udc->lock);
1004 ret = udc->driver->setup(&udc->gadget, &crq.crq);
1005 spin_lock(&udc->lock);
1006 }
1007
1008 DBG(DBG_BUS, "req %02x.%02x, length %d, state %d, ret %d\n",
1009 crq.crq.bRequestType, crq.crq.bRequest,
1010 le16_to_cpu(crq.crq.wLength), ep->state, ret);
1011
1012 if (ret < 0) {
1013 /* Let the host know that we failed */
1014 set_protocol_stall(udc, ep);
1015 }
1016 }
1017}
1018
1019static void usba_ep_irq(struct usba_udc *udc, struct usba_ep *ep)
1020{
1021 struct usba_request *req;
1022 u32 epstatus;
1023 u32 epctrl;
1024
1025 epstatus = usba_ep_readl(ep, STA);
1026 epctrl = usba_ep_readl(ep, CTL);
1027
1028 DBG(DBG_INT, "%s: interrupt, status: 0x%08x\n", ep->ep.name, epstatus);
1029
1030 while ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
1031 DBG(DBG_BUS, "%s: TX PK ready\n", ep->ep.name);
1032
1033 if (list_empty(&ep->queue)) {
Bo Shen70a67fd2013-09-11 18:24:49 +08001034 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
1035 return;
1036 }
1037
1038 req = list_entry(ep->queue.next, struct usba_request, queue);
1039
1040 if (req->submitted)
1041 next_fifo_transaction(ep, req);
1042 else
1043 submit_request(ep, req);
1044
1045 if (req->last_transaction) {
1046 list_del_init(&req->queue);
Bo Shen70a67fd2013-09-11 18:24:49 +08001047 request_complete(ep, req, 0);
1048 }
1049
1050 epstatus = usba_ep_readl(ep, STA);
1051 epctrl = usba_ep_readl(ep, CTL);
1052 }
1053
1054 if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
1055 DBG(DBG_BUS, "%s: RX data ready\n", ep->ep.name);
1056 receive_data(ep);
Bo Shen70a67fd2013-09-11 18:24:49 +08001057 }
1058}
1059
1060static int usba_udc_irq(struct usba_udc *udc)
1061{
1062 u32 status, ep_status;
1063
1064 spin_lock(&udc->lock);
1065
1066 status = usba_readl(udc, INT_STA);
1067 DBG(DBG_INT, "irq, status=%#08x\n", status);
1068
1069 if (status & USBA_DET_SUSPEND) {
1070 usba_writel(udc, INT_CLR, USBA_DET_SUSPEND);
1071 DBG(DBG_BUS, "Suspend detected\n");
1072 if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
1073 udc->driver && udc->driver->suspend) {
1074 spin_unlock(&udc->lock);
1075 udc->driver->suspend(&udc->gadget);
1076 spin_lock(&udc->lock);
1077 }
1078 }
1079
1080 if (status & USBA_WAKE_UP) {
1081 usba_writel(udc, INT_CLR, USBA_WAKE_UP);
1082 DBG(DBG_BUS, "Wake Up CPU detected\n");
1083 }
1084
1085 if (status & USBA_END_OF_RESUME) {
1086 usba_writel(udc, INT_CLR, USBA_END_OF_RESUME);
1087 DBG(DBG_BUS, "Resume detected\n");
1088 if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
1089 udc->driver && udc->driver->resume) {
1090 spin_unlock(&udc->lock);
1091 udc->driver->resume(&udc->gadget);
1092 spin_lock(&udc->lock);
1093 }
1094 }
1095
1096 ep_status = USBA_BFEXT(EPT_INT, status);
1097 if (ep_status) {
1098 int i;
1099
1100 for (i = 0; i < USBA_NR_ENDPOINTS; i++)
1101 if (ep_status & (1 << i)) {
1102 if (ep_is_control(&udc->usba_ep[i]))
1103 usba_control_irq(udc, &udc->usba_ep[i]);
1104 else
1105 usba_ep_irq(udc, &udc->usba_ep[i]);
1106 }
1107 }
1108
1109 if (status & USBA_END_OF_RESET) {
1110 struct usba_ep *ep0;
1111
1112 usba_writel(udc, INT_CLR, USBA_END_OF_RESET);
1113 reset_all_endpoints(udc);
1114
1115 if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
1116 udc->driver->disconnect) {
1117 udc->gadget.speed = USB_SPEED_UNKNOWN;
1118 spin_unlock(&udc->lock);
1119 udc->driver->disconnect(&udc->gadget);
1120 spin_lock(&udc->lock);
1121 }
1122
1123 if (status & USBA_HIGH_SPEED)
1124 udc->gadget.speed = USB_SPEED_HIGH;
1125 else
1126 udc->gadget.speed = USB_SPEED_FULL;
1127
1128 ep0 = &udc->usba_ep[0];
1129 ep0->desc = &usba_ep0_desc;
1130 ep0->state = WAIT_FOR_SETUP;
1131 usba_ep_writel(ep0, CFG,
1132 (USBA_BF(EPT_SIZE, EP0_EPT_SIZE)
1133 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL)
1134 | USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE)));
1135 usba_ep_writel(ep0, CTL_ENB,
1136 USBA_EPT_ENABLE | USBA_RX_SETUP);
1137 usba_writel(udc, INT_ENB,
1138 (usba_readl(udc, INT_ENB)
1139 | USBA_BF(EPT_INT, 1)
1140 | USBA_DET_SUSPEND
1141 | USBA_END_OF_RESUME));
1142
1143 /*
1144 * Unclear why we hit this irregularly, e.g. in usbtest,
1145 * but it's clearly harmless...
1146 */
1147 if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED))
1148 DBG(DBG_ALL, "ODD: EP0 configuration is invalid!\n");
1149 }
1150
1151 spin_unlock(&udc->lock);
1152
1153 return 0;
1154}
1155
1156static int atmel_usba_start(struct usba_udc *udc)
1157{
1158 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED;
1159
1160 udc->vbus_prev = 0;
1161
1162 /* If Vbus is present, enable the controller and wait for reset */
1163 if (vbus_is_present(udc) && udc->vbus_prev == 0) {
1164 usba_writel(udc, CTRL, USBA_ENABLE_MASK);
1165 usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
1166 }
1167
1168 return 0;
1169}
1170
1171static int atmel_usba_stop(struct usba_udc *udc)
1172{
1173 udc->gadget.speed = USB_SPEED_UNKNOWN;
1174 reset_all_endpoints(udc);
1175
1176 /* This will also disable the DP pullup */
1177 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1178
1179 return 0;
1180}
1181
1182static struct usba_udc controller = {
1183 .regs = (unsigned *)ATMEL_BASE_UDPHS,
1184 .fifo = (unsigned *)ATMEL_BASE_UDPHS_FIFO,
1185 .gadget = {
1186 .ops = &usba_udc_ops,
1187 .ep_list = LIST_HEAD_INIT(controller.gadget.ep_list),
1188 .speed = USB_SPEED_HIGH,
1189 .is_dualspeed = 1,
1190 .name = "atmel_usba_udc",
1191 },
1192};
1193
Marek Vasut283bbec2023-09-01 11:50:03 +02001194int dm_usb_gadget_handle_interrupts(struct udevice *dev)
Bo Shen70a67fd2013-09-11 18:24:49 +08001195{
1196 struct usba_udc *udc = &controller;
1197
1198 return usba_udc_irq(udc);
1199}
1200
Bo Shen70a67fd2013-09-11 18:24:49 +08001201int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1202{
1203 struct usba_udc *udc = &controller;
1204 int ret;
1205
1206 if (!driver || !driver->bind || !driver->setup) {
1207 printf("bad paramter\n");
1208 return -EINVAL;
1209 }
1210
1211 if (udc->driver) {
1212 printf("UDC already has a gadget driver\n");
1213 return -EBUSY;
1214 }
1215
1216 atmel_usba_start(udc);
1217
1218 udc->driver = driver;
1219
1220 ret = driver->bind(&udc->gadget);
1221 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001222 pr_err("driver->bind() returned %d\n", ret);
Bo Shen70a67fd2013-09-11 18:24:49 +08001223 udc->driver = NULL;
1224 }
1225
1226 return ret;
1227}
1228
1229int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1230{
1231 struct usba_udc *udc = &controller;
1232
Bo Shen6c7733e2013-09-24 13:19:24 +08001233 if (!driver || !driver->unbind || !driver->disconnect) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001234 pr_err("bad paramter\n");
Bo Shen70a67fd2013-09-11 18:24:49 +08001235 return -EINVAL;
1236 }
1237
Bo Shen6c7733e2013-09-24 13:19:24 +08001238 driver->disconnect(&udc->gadget);
Bo Shen70a67fd2013-09-11 18:24:49 +08001239 driver->unbind(&udc->gadget);
1240 udc->driver = NULL;
1241
1242 atmel_usba_stop(udc);
1243
1244 return 0;
1245}
1246
1247static struct usba_ep *usba_udc_pdata(struct usba_platform_data *pdata,
1248 struct usba_udc *udc)
1249{
1250 struct usba_ep *eps;
1251 int i;
1252
1253 eps = malloc(sizeof(struct usba_ep) * pdata->num_ep);
1254 if (!eps) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001255 pr_err("failed to alloc eps\n");
Bo Shen70a67fd2013-09-11 18:24:49 +08001256 return NULL;
1257 }
1258
1259 udc->gadget.ep0 = &eps[0].ep;
1260
1261 INIT_LIST_HEAD(&udc->gadget.ep_list);
1262 INIT_LIST_HEAD(&eps[0].ep.ep_list);
1263
1264 for (i = 0; i < pdata->num_ep; i++) {
1265 struct usba_ep *ep = &eps[i];
1266
1267 ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
1268 ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
1269 ep->fifo = udc->fifo + USBA_FIFO_BASE(i);
1270 ep->ep.ops = &usba_ep_ops;
1271 ep->ep.name = pdata->ep[i].name;
1272 ep->ep.maxpacket = pdata->ep[i].fifo_size;
1273 ep->fifo_size = ep->ep.maxpacket;
1274 ep->udc = udc;
1275 INIT_LIST_HEAD(&ep->queue);
1276 ep->nr_banks = pdata->ep[i].nr_banks;
1277 ep->index = pdata->ep[i].index;
1278 ep->can_dma = pdata->ep[i].can_dma;
1279 ep->can_isoc = pdata->ep[i].can_isoc;
1280 if (i)
1281 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1282 };
1283
1284 return eps;
1285}
1286
1287int usba_udc_probe(struct usba_platform_data *pdata)
1288{
1289 struct usba_udc *udc;
1290
1291 udc = &controller;
1292
1293 udc->usba_ep = usba_udc_pdata(pdata, udc);
1294
1295 return 0;
1296}