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