blob: 2bf7ed8d60468f0c179eef8ba61e0046a7269e85 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskiac3c8312011-10-27 10:36:46 +02002/*
Marek Vasutf8995db2015-12-04 02:34:46 +01003 * drivers/usb/gadget/dwc2_udc_otg.c
Marek Vasut4a7629a2015-12-04 02:55:37 +01004 * Designware DWC2 on-chip full/high speed USB OTG 2.0 device controllers
Lukasz Majewskiac3c8312011-10-27 10:36:46 +02005 *
6 * Copyright (C) 2008 for Samsung Electronics
7 *
8 * BSP Support for Samsung's UDC driver
9 * available at:
10 * git://git.kernel.org/pub/scm/linux/kernel/git/kki_ap/linux-2.6-samsung.git
11 *
12 * State machine bugfixes:
13 * Marek Szyprowski <m.szyprowski@samsung.com>
14 *
15 * Ported to u-boot:
16 * Marek Szyprowski <m.szyprowski@samsung.com>
17 * Lukasz Majewski <l.majewski@samsumg.com>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020018 */
Lukasz Majewski8fb78fd2012-05-02 13:11:38 +020019#undef DEBUG
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020020#include <common.h>
Patrick Delaunay75150542019-03-29 15:42:15 +010021#include <clk.h>
22#include <dm.h>
23#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <log.h>
Patrick Delaunay75150542019-03-29 15:42:15 +010025#include <malloc.h>
26#include <reset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070027#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070028#include <dm/devres.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060029#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Patrick Delaunay75150542019-03-29 15:42:15 +010031
Masahiro Yamada56a931c2016-09-21 11:28:55 +090032#include <linux/errno.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020033#include <linux/list.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020034
35#include <linux/usb/ch9.h>
Patrick Delaunay75150542019-03-29 15:42:15 +010036#include <linux/usb/otg.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020037#include <linux/usb/gadget.h>
38
Marek Szyprowski2a7a7d92019-12-02 12:11:17 +010039#include <phys2bus.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020040#include <asm/byteorder.h>
Tom Rini73424522011-12-15 08:40:51 -070041#include <asm/unaligned.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020042#include <asm/io.h>
43
44#include <asm/mach-types.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020045
Patrick Delaunay75150542019-03-29 15:42:15 +010046#include <power/regulator.h>
47
Marek Vasut4811c662015-12-04 02:32:22 +010048#include "dwc2_udc_otg_regs.h"
49#include "dwc2_udc_otg_priv.h"
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020050
51/***********************************************************/
52
53#define OTG_DMA_MODE 1
54
Lukasz Majewski0dfeb002012-05-02 13:11:35 +020055#define DEBUG_SETUP 0
56#define DEBUG_EP0 0
57#define DEBUG_ISR 0
58#define DEBUG_OUT_EP 0
59#define DEBUG_IN_EP 0
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020060
Marek Vasutf1be9cb2015-12-04 02:51:20 +010061#include <usb/dwc2_udc.h>
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020062
63#define EP0_CON 0
64#define EP_MASK 0xF
65
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020066static char *state_names[] = {
67 "WAIT_FOR_SETUP",
68 "DATA_STATE_XMIT",
69 "DATA_STATE_NEED_ZLP",
70 "WAIT_FOR_OUT_STATUS",
71 "DATA_STATE_RECV",
72 "WAIT_FOR_COMPLETE",
73 "WAIT_FOR_OUT_COMPLETE",
74 "WAIT_FOR_IN_COMPLETE",
75 "WAIT_FOR_NULL_COMPLETE",
76};
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020077
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020078#define DRIVER_VERSION "15 March 2009"
79
Marek Vasut1a639ff2015-12-04 00:57:58 +010080struct dwc2_udc *the_controller;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020081
Marek Vasutdbdd58f2015-12-04 02:28:40 +010082static const char driver_name[] = "dwc2-udc";
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020083static const char ep0name[] = "ep0-control";
84
85/* Max packet size*/
86static unsigned int ep0_fifo_size = 64;
87static unsigned int ep_fifo_size = 512;
88static unsigned int ep_fifo_size2 = 1024;
89static int reset_available = 1;
90
91static struct usb_ctrlrequest *usb_ctrl;
92static dma_addr_t usb_ctrl_dma_addr;
93
94/*
95 Local declarations.
96*/
Marek Vasut4e7c29e2015-12-04 02:13:42 +010097static int dwc2_ep_enable(struct usb_ep *ep,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +020098 const struct usb_endpoint_descriptor *);
Marek Vasut4e7c29e2015-12-04 02:13:42 +010099static int dwc2_ep_disable(struct usb_ep *ep);
Marek Vasut153150f2015-12-04 02:17:40 +0100100static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200101 gfp_t gfp_flags);
Marek Vasut153150f2015-12-04 02:17:40 +0100102static void dwc2_free_request(struct usb_ep *ep, struct usb_request *);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200103
Marek Vasut153150f2015-12-04 02:17:40 +0100104static int dwc2_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
105static int dwc2_dequeue(struct usb_ep *ep, struct usb_request *);
106static int dwc2_fifo_status(struct usb_ep *ep);
107static void dwc2_fifo_flush(struct usb_ep *ep);
Marek Vasutfe92a512015-12-04 01:59:12 +0100108static void dwc2_ep0_read(struct dwc2_udc *dev);
109static void dwc2_ep0_kick(struct dwc2_udc *dev, struct dwc2_ep *ep);
Marek Vasut153150f2015-12-04 02:17:40 +0100110static void dwc2_handle_ep0(struct dwc2_udc *dev);
Marek Vasutfe92a512015-12-04 01:59:12 +0100111static int dwc2_ep0_write(struct dwc2_udc *dev);
Marek Vasut32f931c2015-12-04 01:51:07 +0100112static int write_fifo_ep0(struct dwc2_ep *ep, struct dwc2_request *req);
113static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status);
Marek Vasut1a639ff2015-12-04 00:57:58 +0100114static void stop_activity(struct dwc2_udc *dev,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200115 struct usb_gadget_driver *driver);
Marek Vasut1a639ff2015-12-04 00:57:58 +0100116static int udc_enable(struct dwc2_udc *dev);
117static void udc_set_address(struct dwc2_udc *dev, unsigned char address);
118static void reconfig_usbd(struct dwc2_udc *dev);
119static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed);
Marek Vasut5b309f72015-12-04 01:48:57 +0100120static void nuke(struct dwc2_ep *ep, int status);
Marek Vasutfa03c812015-12-04 02:03:45 +0100121static int dwc2_udc_set_halt(struct usb_ep *_ep, int value);
122static void dwc2_udc_set_nak(struct dwc2_ep *ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200123
Lukasz Majewski8fb78fd2012-05-02 13:11:38 +0200124void set_udc_gadget_private_data(void *p)
125{
126 debug_cond(DEBUG_SETUP != 0,
127 "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
128 the_controller, p);
129 the_controller->gadget.dev.device_data = p;
130}
131
132void *get_udc_gadget_private_data(struct usb_gadget *gadget)
133{
134 return gadget->dev.device_data;
135}
136
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100137static struct usb_ep_ops dwc2_ep_ops = {
138 .enable = dwc2_ep_enable,
139 .disable = dwc2_ep_disable,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200140
Marek Vasut153150f2015-12-04 02:17:40 +0100141 .alloc_request = dwc2_alloc_request,
142 .free_request = dwc2_free_request,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200143
Marek Vasut153150f2015-12-04 02:17:40 +0100144 .queue = dwc2_queue,
145 .dequeue = dwc2_dequeue,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200146
Marek Vasutfa03c812015-12-04 02:03:45 +0100147 .set_halt = dwc2_udc_set_halt,
Marek Vasut153150f2015-12-04 02:17:40 +0100148 .fifo_status = dwc2_fifo_status,
149 .fifo_flush = dwc2_fifo_flush,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200150};
151
152#define create_proc_files() do {} while (0)
153#define remove_proc_files() do {} while (0)
154
155/***********************************************************/
156
Marek Vasut67ec9dc2015-12-04 01:11:45 +0100157struct dwc2_usbotg_reg *reg;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200158
Lukasz Majewskief731022014-08-25 11:07:29 +0200159bool dfu_usb_get_reset(void)
160{
161 return !!(readl(&reg->gintsts) & INT_RESET);
162}
163
Marek Vasut1a639ff2015-12-04 00:57:58 +0100164__weak void otg_phy_init(struct dwc2_udc *dev) {}
165__weak void otg_phy_off(struct dwc2_udc *dev) {}
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200166
167/***********************************************************/
168
Marek Vasutf8995db2015-12-04 02:34:46 +0100169#include "dwc2_udc_otg_xfer_dma.c"
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200170
171/*
172 * udc_disable - disable USB device controller
173 */
Marek Vasut1a639ff2015-12-04 00:57:58 +0100174static void udc_disable(struct dwc2_udc *dev)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200175{
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200176 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200177
178 udc_set_address(dev, 0);
179
180 dev->ep0state = WAIT_FOR_SETUP;
181 dev->gadget.speed = USB_SPEED_UNKNOWN;
182 dev->usb_address = 0;
183
184 otg_phy_off(dev);
185}
186
187/*
188 * udc_reinit - initialize software state
189 */
Marek Vasut1a639ff2015-12-04 00:57:58 +0100190static void udc_reinit(struct dwc2_udc *dev)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200191{
192 unsigned int i;
193
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200194 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200195
196 /* device/ep0 records init */
197 INIT_LIST_HEAD(&dev->gadget.ep_list);
198 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
199 dev->ep0state = WAIT_FOR_SETUP;
200
201 /* basic endpoint records init */
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100202 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
Marek Vasut5b309f72015-12-04 01:48:57 +0100203 struct dwc2_ep *ep = &dev->ep[i];
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200204
205 if (i != 0)
206 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
207
208 ep->desc = 0;
209 ep->stopped = 0;
210 INIT_LIST_HEAD(&ep->queue);
211 ep->pio_irqs = 0;
212 }
213
214 /* the rest was statically initialized, and is read-only */
215}
216
217#define BYTES2MAXP(x) (x / 8)
218#define MAXP2BYTES(x) (x * 8)
219
220/* until it's enabled, this UDC should be completely invisible
221 * to any USB host.
222 */
Marek Vasut1a639ff2015-12-04 00:57:58 +0100223static int udc_enable(struct dwc2_udc *dev)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200224{
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200225 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200226
227 otg_phy_init(dev);
Marek Vasut80300882014-11-04 04:23:25 +0100228 reconfig_usbd(dev);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200229
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200230 debug_cond(DEBUG_SETUP != 0,
Marek Vasutdbdd58f2015-12-04 02:28:40 +0100231 "DWC2 USB 2.0 OTG Controller Core Initialized : 0x%x\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200232 readl(&reg->gintmsk));
233
234 dev->gadget.speed = USB_SPEED_UNKNOWN;
235
236 return 0;
237}
238
Mattijs Korpershoek01f35912023-01-11 09:19:27 +0100239static int dwc2_gadget_pullup(struct usb_gadget *g, int is_on)
240{
241 clrsetbits_le32(&reg->dctl, SOFT_DISCONNECT,
242 is_on ? 0 : SOFT_DISCONNECT);
243
244 return 0;
245}
246
Patrick Delaunay75150542019-03-29 15:42:15 +0100247#if !CONFIG_IS_ENABLED(DM_USB_GADGET)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200248/*
249 Register entry point for the peripheral controller driver.
250*/
251int usb_gadget_register_driver(struct usb_gadget_driver *driver)
252{
Marek Vasut1a639ff2015-12-04 00:57:58 +0100253 struct dwc2_udc *dev = the_controller;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200254 int retval = 0;
Bo Shen60dc40c2014-08-27 17:28:18 +0800255 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200256
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200257 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200258
Patrice Chotard1d2f3d52021-02-17 10:17:27 +0100259 if (!driver || driver->speed < USB_SPEED_FULL
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200260 || !driver->bind || !driver->disconnect || !driver->setup)
261 return -EINVAL;
262 if (!dev)
263 return -ENODEV;
264 if (dev->driver)
265 return -EBUSY;
266
267 spin_lock_irqsave(&dev->lock, flags);
268 /* first hook up the driver ... */
269 dev->driver = driver;
270 spin_unlock_irqrestore(&dev->lock, flags);
271
272 if (retval) { /* TODO */
273 printf("target device_add failed, error %d\n", retval);
274 return retval;
275 }
276
277 retval = driver->bind(&dev->gadget);
278 if (retval) {
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200279 debug_cond(DEBUG_SETUP != 0,
280 "%s: bind to driver --> error %d\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200281 dev->gadget.name, retval);
282 dev->driver = 0;
283 return retval;
284 }
285
286 enable_irq(IRQ_OTG);
287
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200288 debug_cond(DEBUG_SETUP != 0,
289 "Registered gadget driver %s\n", dev->gadget.name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200290 udc_enable(dev);
291
292 return 0;
293}
294
295/*
296 * Unregister entry point for the peripheral controller driver.
297 */
298int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
299{
Marek Vasut1a639ff2015-12-04 00:57:58 +0100300 struct dwc2_udc *dev = the_controller;
Bo Shen60dc40c2014-08-27 17:28:18 +0800301 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200302
303 if (!dev)
304 return -ENODEV;
305 if (!driver || driver != dev->driver)
306 return -EINVAL;
307
308 spin_lock_irqsave(&dev->lock, flags);
309 dev->driver = 0;
310 stop_activity(dev, driver);
311 spin_unlock_irqrestore(&dev->lock, flags);
312
313 driver->unbind(&dev->gadget);
314
315 disable_irq(IRQ_OTG);
316
317 udc_disable(dev);
318 return 0;
319}
Patrick Delaunay75150542019-03-29 15:42:15 +0100320#else /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
321
322static int dwc2_gadget_start(struct usb_gadget *g,
323 struct usb_gadget_driver *driver)
324{
325 struct dwc2_udc *dev = the_controller;
326
327 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
328
Patrice Chotard1d2f3d52021-02-17 10:17:27 +0100329 if (!driver || driver->speed < USB_SPEED_FULL ||
Patrick Delaunay75150542019-03-29 15:42:15 +0100330 !driver->bind || !driver->disconnect || !driver->setup)
331 return -EINVAL;
332
333 if (!dev)
334 return -ENODEV;
335
336 if (dev->driver)
337 return -EBUSY;
338
339 /* first hook up the driver ... */
340 dev->driver = driver;
341
342 debug_cond(DEBUG_SETUP != 0,
343 "Registered gadget driver %s\n", dev->gadget.name);
344 return udc_enable(dev);
345}
346
347static int dwc2_gadget_stop(struct usb_gadget *g)
348{
349 struct dwc2_udc *dev = the_controller;
350
351 if (!dev)
352 return -ENODEV;
353
354 if (!dev->driver)
355 return -EINVAL;
356
357 dev->driver = 0;
358 stop_activity(dev, dev->driver);
359
360 udc_disable(dev);
361
362 return 0;
363}
364
365#endif /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200366
367/*
368 * done - retire a request; caller blocked irqs
369 */
Marek Vasut32f931c2015-12-04 01:51:07 +0100370static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200371{
372 unsigned int stopped = ep->stopped;
373
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000374 debug("%s: %s %p, req = %p, stopped = %d\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200375 __func__, ep->ep.name, ep, &req->req, stopped);
376
377 list_del_init(&req->queue);
378
379 if (likely(req->req.status == -EINPROGRESS))
380 req->req.status = status;
381 else
382 status = req->req.status;
383
384 if (status && status != -ESHUTDOWN) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000385 debug("complete %s req %p stat %d len %u/%u\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200386 ep->ep.name, &req->req, status,
387 req->req.actual, req->req.length);
388 }
389
390 /* don't modify queue heads during completion callback */
391 ep->stopped = 1;
392
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200393#ifdef DEBUG
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200394 printf("calling complete callback\n");
395 {
396 int i, len = req->req.length;
397
398 printf("pkt[%d] = ", req->req.length);
399 if (len > 64)
400 len = 64;
401 for (i = 0; i < len; i++) {
402 printf("%02x", ((u8 *)req->req.buf)[i]);
403 if ((i & 7) == 7)
404 printf(" ");
405 }
406 printf("\n");
407 }
408#endif
409 spin_unlock(&ep->dev->lock);
410 req->req.complete(&ep->ep, &req->req);
411 spin_lock(&ep->dev->lock);
412
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000413 debug("callback completed\n");
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200414
415 ep->stopped = stopped;
416}
417
418/*
419 * nuke - dequeue ALL requests
420 */
Marek Vasut5b309f72015-12-04 01:48:57 +0100421static void nuke(struct dwc2_ep *ep, int status)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200422{
Marek Vasut32f931c2015-12-04 01:51:07 +0100423 struct dwc2_request *req;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200424
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000425 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200426
427 /* called with irqs blocked */
428 while (!list_empty(&ep->queue)) {
Marek Vasut32f931c2015-12-04 01:51:07 +0100429 req = list_entry(ep->queue.next, struct dwc2_request, queue);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200430 done(ep, req, status);
431 }
432}
433
Marek Vasut1a639ff2015-12-04 00:57:58 +0100434static void stop_activity(struct dwc2_udc *dev,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200435 struct usb_gadget_driver *driver)
436{
437 int i;
438
439 /* don't disconnect drivers more than once */
440 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
441 driver = 0;
442 dev->gadget.speed = USB_SPEED_UNKNOWN;
443
444 /* prevent new request submissions, kill any outstanding requests */
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100445 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
Marek Vasut5b309f72015-12-04 01:48:57 +0100446 struct dwc2_ep *ep = &dev->ep[i];
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200447 ep->stopped = 1;
448 nuke(ep, -ESHUTDOWN);
449 }
450
451 /* report disconnect; the driver is already quiesced */
452 if (driver) {
453 spin_unlock(&dev->lock);
454 driver->disconnect(&dev->gadget);
455 spin_lock(&dev->lock);
456 }
457
458 /* re-init driver-visible data structures */
459 udc_reinit(dev);
460}
461
Marek Vasut1a639ff2015-12-04 00:57:58 +0100462static void reconfig_usbd(struct dwc2_udc *dev)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200463{
464 /* 2. Soft-reset OTG Core and then unreset again. */
465 int i;
466 unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
Marek Vasut80300882014-11-04 04:23:25 +0100467 uint32_t dflt_gusbcfg;
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800468 uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100469 u32 max_hw_ep;
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100470 int pdata_hw_ep;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200471
Vagrant Cascadian86f49232021-12-21 13:07:02 -0800472 debug("Resetting OTG controller\n");
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200473
Marek Vasut80300882014-11-04 04:23:25 +0100474 dflt_gusbcfg =
475 0<<15 /* PHY Low Power Clock sel*/
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200476 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
477 |0x5<<10 /* Turnaround time*/
478 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
479 /* 1:SRP enable] H1= 1,1*/
480 |0<<7 /* Ulpi DDR sel*/
481 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
482 |0<<4 /* 0: utmi+, 1:ulpi*/
Steve Raed7198f32016-06-07 15:35:21 -0700483#ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8
484 |0<<3 /* phy i/f 0:8bit, 1:16bit*/
485#else
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200486 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
Steve Raed7198f32016-06-07 15:35:21 -0700487#endif
Marek Vasut80300882014-11-04 04:23:25 +0100488 |0x7<<0; /* HS/FS Timeout**/
489
490 if (dev->pdata->usb_gusbcfg)
491 dflt_gusbcfg = dev->pdata->usb_gusbcfg;
492
493 writel(dflt_gusbcfg, &reg->gusbcfg);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200494
495 /* 3. Put the OTG device core in the disconnected state.*/
496 uTemp = readl(&reg->dctl);
497 uTemp |= SOFT_DISCONNECT;
498 writel(uTemp, &reg->dctl);
499
500 udelay(20);
501
502 /* 4. Make the OTG device core exit from the disconnected state.*/
503 uTemp = readl(&reg->dctl);
504 uTemp = uTemp & ~SOFT_DISCONNECT;
505 writel(uTemp, &reg->dctl);
506
507 /* 5. Configure OTG Core to initial settings of device mode.*/
508 /* [][1: full speed(30Mhz) 0:high speed]*/
509 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
510
511 mdelay(1);
512
513 /* 6. Unmask the core interrupts*/
514 writel(GINTMSK_INIT, &reg->gintmsk);
515
516 /* 7. Set NAK bit of EP0, EP1, EP2*/
517 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
518 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
519
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100520 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200521 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
522 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
523 }
524
525 /* 8. Unmask EPO interrupts*/
526 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
527 | (1 << EP0_CON), &reg->daintmsk);
528
529 /* 9. Unmask device OUT EP common interrupts*/
530 writel(DOEPMSK_INIT, &reg->doepmsk);
531
532 /* 10. Unmask device IN EP common interrupts*/
533 writel(DIEPMSK_INIT, &reg->diepmsk);
534
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800535 rx_fifo_sz = RX_FIFO_SIZE;
536 np_tx_fifo_sz = NPTX_FIFO_SIZE;
537 tx_fifo_sz = PTX_FIFO_SIZE;
538
539 if (dev->pdata->rx_fifo_sz)
540 rx_fifo_sz = dev->pdata->rx_fifo_sz;
541 if (dev->pdata->np_tx_fifo_sz)
542 np_tx_fifo_sz = dev->pdata->np_tx_fifo_sz;
543 if (dev->pdata->tx_fifo_sz)
544 tx_fifo_sz = dev->pdata->tx_fifo_sz;
545
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200546 /* 11. Set Rx FIFO Size (in 32-bit words) */
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800547 writel(rx_fifo_sz, &reg->grxfsiz);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200548
549 /* 12. Set Non Periodic Tx FIFO Size */
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800550 writel((np_tx_fifo_sz << 16) | rx_fifo_sz,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200551 &reg->gnptxfsiz);
552
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100553 /* retrieve the number of IN Endpoints (excluding ep0) */
554 max_hw_ep = (readl(&reg->ghwcfg4) & GHWCFG4_NUM_IN_EPS_MASK) >>
555 GHWCFG4_NUM_IN_EPS_SHIFT;
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100556 pdata_hw_ep = dev->pdata->tx_fifo_sz_nb;
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100557
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100558 /* tx_fifo_sz_nb should equal to number of IN Endpoint */
559 if (pdata_hw_ep && max_hw_ep != pdata_hw_ep)
560 pr_warn("Got %d hw endpoint but %d tx-fifo-size in array !!\n",
561 max_hw_ep, pdata_hw_ep);
562
563 for (i = 0; i < max_hw_ep; i++) {
564 if (pdata_hw_ep)
565 tx_fifo_sz = dev->pdata->tx_fifo_sz_array[i];
566
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100567 writel((rx_fifo_sz + np_tx_fifo_sz + (tx_fifo_sz * i)) |
568 tx_fifo_sz << 16, &reg->dieptxf[i]);
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100569 }
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200570 /* Flush the RX FIFO */
571 writel(RX_FIFO_FLUSH, &reg->grstctl);
572 while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100573 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200574
575 /* Flush all the Tx FIFO's */
576 writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
577 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
578 while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100579 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200580
581 /* 13. Clear NAK bit of EP0, EP1, EP2*/
582 /* For Slave mode*/
583 /* EP0: Control OUT */
584 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
585 &reg->out_endp[EP0_CON].doepctl);
586
587 /* 14. Initialize OTG Link Core.*/
588 writel(GAHBCFG_INIT, &reg->gahbcfg);
589}
590
Marek Vasut1a639ff2015-12-04 00:57:58 +0100591static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200592{
593 unsigned int ep_ctrl;
594 int i;
595
596 if (speed == USB_SPEED_HIGH) {
597 ep0_fifo_size = 64;
598 ep_fifo_size = 512;
599 ep_fifo_size2 = 1024;
600 dev->gadget.speed = USB_SPEED_HIGH;
601 } else {
602 ep0_fifo_size = 64;
603 ep_fifo_size = 64;
604 ep_fifo_size2 = 64;
605 dev->gadget.speed = USB_SPEED_FULL;
606 }
607
608 dev->ep[0].ep.maxpacket = ep0_fifo_size;
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100609 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200610 dev->ep[i].ep.maxpacket = ep_fifo_size;
611
612 /* EP0 - Control IN (64 bytes)*/
613 ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
614 writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
615
616 /* EP0 - Control OUT (64 bytes)*/
617 ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
618 writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
619}
620
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100621static int dwc2_ep_enable(struct usb_ep *_ep,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200622 const struct usb_endpoint_descriptor *desc)
623{
Marek Vasut5b309f72015-12-04 01:48:57 +0100624 struct dwc2_ep *ep;
Marek Vasut1a639ff2015-12-04 00:57:58 +0100625 struct dwc2_udc *dev;
Bo Shen60dc40c2014-08-27 17:28:18 +0800626 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200627
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000628 debug("%s: %p\n", __func__, _ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200629
Marek Vasut5b309f72015-12-04 01:48:57 +0100630 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200631 if (!_ep || !desc || ep->desc || _ep->name == ep0name
632 || desc->bDescriptorType != USB_DT_ENDPOINT
633 || ep->bEndpointAddress != desc->bEndpointAddress
Tom Rini73424522011-12-15 08:40:51 -0700634 || ep_maxpacket(ep) <
635 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200636
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000637 debug("%s: bad ep or descriptor\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200638 return -EINVAL;
639 }
640
641 /* xfer types must match, except that interrupt ~= bulk */
642 if (ep->bmAttributes != desc->bmAttributes
643 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
644 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
645
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000646 debug("%s: %s type mismatch\n", __func__, _ep->name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200647 return -EINVAL;
648 }
649
650 /* hardware _could_ do smaller, but driver doesn't */
Frank Wang0d64b5b2016-01-27 12:39:40 -0800651 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK &&
652 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) >
Tom Rini73424522011-12-15 08:40:51 -0700653 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200654
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000655 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200656 return -ERANGE;
657 }
658
659 dev = ep->dev;
660 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
661
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000662 debug("%s: bogus device state\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200663 return -ESHUTDOWN;
664 }
665
Gary Bisson82832592022-01-06 16:02:08 +0100666 _ep->desc = desc;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200667 ep->stopped = 0;
668 ep->desc = desc;
669 ep->pio_irqs = 0;
Tom Rini73424522011-12-15 08:40:51 -0700670 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200671
672 /* Reset halt state */
Marek Vasutfa03c812015-12-04 02:03:45 +0100673 dwc2_udc_set_nak(ep);
674 dwc2_udc_set_halt(_ep, 0);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200675
676 spin_lock_irqsave(&ep->dev->lock, flags);
Marek Vasutfa03c812015-12-04 02:03:45 +0100677 dwc2_udc_ep_activate(ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200678 spin_unlock_irqrestore(&ep->dev->lock, flags);
679
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000680 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200681 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
682 return 0;
683}
684
685/*
686 * Disable EP
687 */
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100688static int dwc2_ep_disable(struct usb_ep *_ep)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200689{
Marek Vasut5b309f72015-12-04 01:48:57 +0100690 struct dwc2_ep *ep;
Bo Shen60dc40c2014-08-27 17:28:18 +0800691 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200692
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000693 debug("%s: %p\n", __func__, _ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200694
Marek Vasut5b309f72015-12-04 01:48:57 +0100695 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200696 if (!_ep || !ep->desc) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000697 debug("%s: %s not enabled\n", __func__,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200698 _ep ? ep->ep.name : NULL);
699 return -EINVAL;
700 }
701
702 spin_lock_irqsave(&ep->dev->lock, flags);
703
704 /* Nuke all pending requests */
705 nuke(ep, -ESHUTDOWN);
706
Gary Bisson82832592022-01-06 16:02:08 +0100707 _ep->desc = NULL;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200708 ep->desc = 0;
709 ep->stopped = 1;
710
711 spin_unlock_irqrestore(&ep->dev->lock, flags);
712
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000713 debug("%s: disabled %s\n", __func__, _ep->name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200714 return 0;
715}
716
Marek Vasut153150f2015-12-04 02:17:40 +0100717static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200718 gfp_t gfp_flags)
719{
Marek Vasut32f931c2015-12-04 01:51:07 +0100720 struct dwc2_request *req;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200721
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000722 debug("%s: %s %p\n", __func__, ep->name, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200723
Mike Frysinger1f19e3f2012-04-26 02:34:44 +0000724 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200725 if (!req)
726 return 0;
727
728 memset(req, 0, sizeof *req);
729 INIT_LIST_HEAD(&req->queue);
730
731 return &req->req;
732}
733
Marek Vasut153150f2015-12-04 02:17:40 +0100734static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200735{
Marek Vasut32f931c2015-12-04 01:51:07 +0100736 struct dwc2_request *req;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200737
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000738 debug("%s: %p\n", __func__, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200739
Marek Vasut32f931c2015-12-04 01:51:07 +0100740 req = container_of(_req, struct dwc2_request, req);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200741 WARN_ON(!list_empty(&req->queue));
742 kfree(req);
743}
744
745/* dequeue JUST ONE request */
Marek Vasut153150f2015-12-04 02:17:40 +0100746static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200747{
Marek Vasut5b309f72015-12-04 01:48:57 +0100748 struct dwc2_ep *ep;
Marek Vasut32f931c2015-12-04 01:51:07 +0100749 struct dwc2_request *req;
Bo Shen60dc40c2014-08-27 17:28:18 +0800750 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200751
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000752 debug("%s: %p\n", __func__, _ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200753
Marek Vasut5b309f72015-12-04 01:48:57 +0100754 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200755 if (!_ep || ep->ep.name == ep0name)
756 return -EINVAL;
757
758 spin_lock_irqsave(&ep->dev->lock, flags);
759
760 /* make sure it's actually queued on this endpoint */
761 list_for_each_entry(req, &ep->queue, queue) {
762 if (&req->req == _req)
763 break;
764 }
765 if (&req->req != _req) {
766 spin_unlock_irqrestore(&ep->dev->lock, flags);
767 return -EINVAL;
768 }
769
770 done(ep, req, -ECONNRESET);
771
772 spin_unlock_irqrestore(&ep->dev->lock, flags);
773 return 0;
774}
775
776/*
777 * Return bytes in EP FIFO
778 */
Marek Vasut153150f2015-12-04 02:17:40 +0100779static int dwc2_fifo_status(struct usb_ep *_ep)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200780{
781 int count = 0;
Marek Vasut5b309f72015-12-04 01:48:57 +0100782 struct dwc2_ep *ep;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200783
Marek Vasut5b309f72015-12-04 01:48:57 +0100784 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200785 if (!_ep) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000786 debug("%s: bad ep\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200787 return -ENODEV;
788 }
789
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000790 debug("%s: %d\n", __func__, ep_index(ep));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200791
792 /* LPD can't report unclaimed bytes from IN fifos */
793 if (ep_is_in(ep))
794 return -EOPNOTSUPP;
795
796 return count;
797}
798
799/*
800 * Flush EP FIFO
801 */
Marek Vasut153150f2015-12-04 02:17:40 +0100802static void dwc2_fifo_flush(struct usb_ep *_ep)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200803{
Marek Vasut5b309f72015-12-04 01:48:57 +0100804 struct dwc2_ep *ep;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200805
Marek Vasut5b309f72015-12-04 01:48:57 +0100806 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200807 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000808 debug("%s: bad ep\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200809 return;
810 }
811
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000812 debug("%s: %d\n", __func__, ep_index(ep));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200813}
814
Marek Vasutfa03c812015-12-04 02:03:45 +0100815static const struct usb_gadget_ops dwc2_udc_ops = {
Mattijs Korpershoek01f35912023-01-11 09:19:27 +0100816 .pullup = dwc2_gadget_pullup,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200817 /* current versions must always be self-powered */
Patrick Delaunay75150542019-03-29 15:42:15 +0100818#if CONFIG_IS_ENABLED(DM_USB_GADGET)
819 .udc_start = dwc2_gadget_start,
820 .udc_stop = dwc2_gadget_stop,
821#endif
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200822};
823
Marek Vasut1a639ff2015-12-04 00:57:58 +0100824static struct dwc2_udc memory = {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200825 .usb_address = 0,
826 .gadget = {
Marek Vasutfa03c812015-12-04 02:03:45 +0100827 .ops = &dwc2_udc_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200828 .ep0 = &memory.ep[0].ep,
829 .name = driver_name,
830 },
831
832 /* control endpoint */
833 .ep[0] = {
834 .ep = {
835 .name = ep0name,
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100836 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200837 .maxpacket = EP0_FIFO_SIZE,
838 },
839 .dev = &memory,
840
841 .bEndpointAddress = 0,
842 .bmAttributes = 0,
843
844 .ep_type = ep_control,
845 },
846
847 /* first group of endpoints */
848 .ep[1] = {
849 .ep = {
850 .name = "ep1in-bulk",
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100851 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200852 .maxpacket = EP_FIFO_SIZE,
853 },
854 .dev = &memory,
855
856 .bEndpointAddress = USB_DIR_IN | 1,
857 .bmAttributes = USB_ENDPOINT_XFER_BULK,
858
859 .ep_type = ep_bulk_out,
860 .fifo_num = 1,
861 },
862
863 .ep[2] = {
864 .ep = {
865 .name = "ep2out-bulk",
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100866 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200867 .maxpacket = EP_FIFO_SIZE,
868 },
869 .dev = &memory,
870
871 .bEndpointAddress = USB_DIR_OUT | 2,
872 .bmAttributes = USB_ENDPOINT_XFER_BULK,
873
874 .ep_type = ep_bulk_in,
875 .fifo_num = 2,
876 },
877
878 .ep[3] = {
879 .ep = {
880 .name = "ep3in-int",
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100881 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200882 .maxpacket = EP_FIFO_SIZE,
883 },
884 .dev = &memory,
885
886 .bEndpointAddress = USB_DIR_IN | 3,
887 .bmAttributes = USB_ENDPOINT_XFER_INT,
888
889 .ep_type = ep_interrupt,
890 .fifo_num = 3,
891 },
892};
893
894/*
895 * probe - binds to the platform device
896 */
897
Marek Vasut01b61fa2015-12-04 02:26:33 +0100898int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200899{
Marek Vasut1a639ff2015-12-04 00:57:58 +0100900 struct dwc2_udc *dev = &memory;
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100901 int retval = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200902
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000903 debug("%s: %p\n", __func__, pdata);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200904
905 dev->pdata = pdata;
906
Marek Vasut67ec9dc2015-12-04 01:11:45 +0100907 reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200908
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200909 dev->gadget.is_dualspeed = 1; /* Hack only*/
910 dev->gadget.is_otg = 0;
911 dev->gadget.is_a_peripheral = 0;
912 dev->gadget.b_hnp_enable = 0;
913 dev->gadget.a_hnp_support = 0;
914 dev->gadget.a_alt_hnp_support = 0;
915
916 the_controller = dev;
917
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100918 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
919 ROUND(sizeof(struct usb_ctrlrequest),
920 CONFIG_SYS_CACHELINE_SIZE));
921 if (!usb_ctrl) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900922 pr_err("No memory available for UDC!\n");
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100923 return -ENOMEM;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200924 }
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100925
926 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200927
928 udc_reinit(dev);
929
930 return retval;
931}
932
Patrick Delaunay75150542019-03-29 15:42:15 +0100933int dwc2_udc_handle_interrupt(void)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200934{
935 u32 intr_status = readl(&reg->gintsts);
936 u32 gintmsk = readl(&reg->gintmsk);
937
938 if (intr_status & gintmsk)
Marek Vasutfa03c812015-12-04 02:03:45 +0100939 return dwc2_udc_irq(1, (void *)the_controller);
Patrick Delaunay75150542019-03-29 15:42:15 +0100940
941 return 0;
942}
943
944#if !CONFIG_IS_ENABLED(DM_USB_GADGET)
945
946int usb_gadget_handle_interrupts(int index)
947{
948 return dwc2_udc_handle_interrupt();
949}
950
951#else /* CONFIG_IS_ENABLED(DM_USB_GADGET) */
952
953struct dwc2_priv_data {
954 struct clk_bulk clks;
955 struct reset_ctl_bulk resets;
developer43903f32020-05-02 11:35:14 +0200956 struct phy_bulk phys;
Patrick Delaunaye014cb62019-03-29 15:42:21 +0100957 struct udevice *usb33d_supply;
Patrick Delaunay75150542019-03-29 15:42:15 +0100958};
959
960int dm_usb_gadget_handle_interrupts(struct udevice *dev)
961{
962 return dwc2_udc_handle_interrupt();
963}
964
developer43903f32020-05-02 11:35:14 +0200965static int dwc2_phy_setup(struct udevice *dev, struct phy_bulk *phys)
Patrick Delaunay75150542019-03-29 15:42:15 +0100966{
developer43903f32020-05-02 11:35:14 +0200967 int ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100968
developer43903f32020-05-02 11:35:14 +0200969 ret = generic_phy_get_bulk(dev, phys);
970 if (ret)
971 return ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100972
developer43903f32020-05-02 11:35:14 +0200973 ret = generic_phy_init_bulk(phys);
974 if (ret)
975 return ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100976
developer43903f32020-05-02 11:35:14 +0200977 ret = generic_phy_power_on_bulk(phys);
978 if (ret)
979 generic_phy_exit_bulk(phys);
Patrick Delaunay75150542019-03-29 15:42:15 +0100980
981 return ret;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200982}
Patrick Delaunay75150542019-03-29 15:42:15 +0100983
developer43903f32020-05-02 11:35:14 +0200984static void dwc2_phy_shutdown(struct udevice *dev, struct phy_bulk *phys)
Patrick Delaunay75150542019-03-29 15:42:15 +0100985{
developer43903f32020-05-02 11:35:14 +0200986 generic_phy_power_off_bulk(phys);
987 generic_phy_exit_bulk(phys);
Patrick Delaunay75150542019-03-29 15:42:15 +0100988}
989
Simon Glassaad29ae2020-12-03 16:55:21 -0700990static int dwc2_udc_otg_of_to_plat(struct udevice *dev)
Patrick Delaunay75150542019-03-29 15:42:15 +0100991{
Simon Glassfa20e932020-12-03 16:55:20 -0700992 struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
Patrick Delaunaye014cb62019-03-29 15:42:21 +0100993 ulong drvdata;
994 void (*set_params)(struct dwc2_plat_otg_data *data);
Patrick Delaunayed742632019-06-18 16:57:16 +0200995 int ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100996
Simon Glassa7ece582020-12-19 10:40:14 -0700997 if (usb_get_dr_mode(dev_ofnode(dev)) != USB_DR_MODE_PERIPHERAL &&
998 usb_get_dr_mode(dev_ofnode(dev)) != USB_DR_MODE_OTG) {
Patrick Delaunay75150542019-03-29 15:42:15 +0100999 dev_dbg(dev, "Invalid mode\n");
1000 return -ENODEV;
1001 }
1002
Simon Glass71fa5b42020-12-03 16:55:18 -07001003 plat->regs_otg = dev_read_addr(dev);
Patrick Delaunay75150542019-03-29 15:42:15 +01001004
Simon Glass71fa5b42020-12-03 16:55:18 -07001005 plat->rx_fifo_sz = dev_read_u32_default(dev, "g-rx-fifo-size", 0);
1006 plat->np_tx_fifo_sz = dev_read_u32_default(dev, "g-np-tx-fifo-size", 0);
Patrick Delaunayed742632019-06-18 16:57:16 +02001007
Wolfgang Grandegger46238dd2022-03-14 09:32:53 +01001008 ret = dev_read_size(dev, "g-tx-fifo-size");
1009 if (ret > 0)
1010 plat->tx_fifo_sz_nb = ret / sizeof(u32);
Simon Glass71fa5b42020-12-03 16:55:18 -07001011 if (plat->tx_fifo_sz_nb > DWC2_MAX_HW_ENDPOINTS)
1012 plat->tx_fifo_sz_nb = DWC2_MAX_HW_ENDPOINTS;
1013 if (plat->tx_fifo_sz_nb) {
Patrick Delaunayed742632019-06-18 16:57:16 +02001014 ret = dev_read_u32_array(dev, "g-tx-fifo-size",
Simon Glass71fa5b42020-12-03 16:55:18 -07001015 plat->tx_fifo_sz_array,
1016 plat->tx_fifo_sz_nb);
Patrick Delaunayed742632019-06-18 16:57:16 +02001017 if (ret)
1018 return ret;
1019 }
Patrick Delaunay75150542019-03-29 15:42:15 +01001020
Simon Glass71fa5b42020-12-03 16:55:18 -07001021 plat->force_b_session_valid =
Patrick Delaunay7f3384d2019-03-29 15:42:24 +01001022 dev_read_bool(dev, "u-boot,force-b-session-valid");
Patrick Delaunay0c42d1d2019-03-29 15:42:17 +01001023
Simon Glass71fa5b42020-12-03 16:55:18 -07001024 plat->force_vbus_detection =
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001025 dev_read_bool(dev, "u-boot,force-vbus-detection");
1026
Simon Glass71fa5b42020-12-03 16:55:18 -07001027 /* force plat according compatible */
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001028 drvdata = dev_get_driver_data(dev);
1029 if (drvdata) {
1030 set_params = (void *)drvdata;
Simon Glass71fa5b42020-12-03 16:55:18 -07001031 set_params(plat);
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001032 }
1033
Patrick Delaunay75150542019-03-29 15:42:15 +01001034 return 0;
1035}
1036
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001037static void dwc2_set_stm32mp1_hsotg_params(struct dwc2_plat_otg_data *p)
1038{
1039 p->activate_stm_id_vb_detection = true;
1040 p->usb_gusbcfg =
1041 0 << 15 /* PHY Low Power Clock sel*/
1042 | 0x9 << 10 /* USB Turnaround time (0x9 for HS phy) */
1043 | 0 << 9 /* [0:HNP disable,1:HNP enable]*/
1044 | 0 << 8 /* [0:SRP disable 1:SRP enable]*/
1045 | 0 << 6 /* 0: high speed utmi+, 1: full speed serial*/
1046 | 0x7 << 0; /* FS timeout calibration**/
Patrick Delaunay7f3384d2019-03-29 15:42:24 +01001047
1048 if (p->force_b_session_valid)
1049 p->usb_gusbcfg |= 1 << 30; /* FDMOD: Force device mode */
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001050}
1051
Patrick Delaunay75150542019-03-29 15:42:15 +01001052static int dwc2_udc_otg_reset_init(struct udevice *dev,
1053 struct reset_ctl_bulk *resets)
1054{
1055 int ret;
1056
1057 ret = reset_get_bulk(dev, resets);
Jaehoon Chung09aee382020-10-21 21:28:41 +09001058 if (ret == -ENOTSUPP || ret == -ENOENT)
Patrick Delaunay75150542019-03-29 15:42:15 +01001059 return 0;
1060
1061 if (ret)
1062 return ret;
1063
Patrick Delaunay9e4abe32019-03-29 15:42:16 +01001064 ret = reset_assert_bulk(resets);
1065
1066 if (!ret) {
1067 udelay(2);
1068 ret = reset_deassert_bulk(resets);
1069 }
Patrick Delaunay75150542019-03-29 15:42:15 +01001070 if (ret) {
1071 reset_release_bulk(resets);
1072 return ret;
1073 }
1074
1075 return 0;
1076}
1077
1078static int dwc2_udc_otg_clk_init(struct udevice *dev,
1079 struct clk_bulk *clks)
1080{
1081 int ret;
1082
1083 ret = clk_get_bulk(dev, clks);
1084 if (ret == -ENOSYS)
1085 return 0;
1086
1087 if (ret)
1088 return ret;
1089
1090 ret = clk_enable_bulk(clks);
1091 if (ret) {
1092 clk_release_bulk(clks);
1093 return ret;
1094 }
1095
1096 return 0;
1097}
1098
1099static int dwc2_udc_otg_probe(struct udevice *dev)
1100{
Simon Glassfa20e932020-12-03 16:55:20 -07001101 struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
Patrick Delaunay75150542019-03-29 15:42:15 +01001102 struct dwc2_priv_data *priv = dev_get_priv(dev);
Patrick Delaunay0c42d1d2019-03-29 15:42:17 +01001103 struct dwc2_usbotg_reg *usbotg_reg =
Simon Glass71fa5b42020-12-03 16:55:18 -07001104 (struct dwc2_usbotg_reg *)plat->regs_otg;
Patrick Delaunay75150542019-03-29 15:42:15 +01001105 int ret;
1106
1107 ret = dwc2_udc_otg_clk_init(dev, &priv->clks);
1108 if (ret)
1109 return ret;
1110
1111 ret = dwc2_udc_otg_reset_init(dev, &priv->resets);
1112 if (ret)
1113 return ret;
1114
developer43903f32020-05-02 11:35:14 +02001115 ret = dwc2_phy_setup(dev, &priv->phys);
Patrick Delaunay75150542019-03-29 15:42:15 +01001116 if (ret)
1117 return ret;
1118
Simon Glass71fa5b42020-12-03 16:55:18 -07001119 if (plat->activate_stm_id_vb_detection) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001120 if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
Simon Glass71fa5b42020-12-03 16:55:18 -07001121 (!plat->force_b_session_valid ||
1122 plat->force_vbus_detection)) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001123 ret = device_get_supply_regulator(dev, "usb33d-supply",
1124 &priv->usb33d_supply);
1125 if (ret) {
1126 dev_err(dev, "can't get voltage level detector supply\n");
1127 return ret;
1128 }
1129 ret = regulator_set_enable(priv->usb33d_supply, true);
1130 if (ret) {
1131 dev_err(dev, "can't enable voltage level detector supply\n");
1132 return ret;
1133 }
1134 }
1135
Simon Glass71fa5b42020-12-03 16:55:18 -07001136 if (plat->force_b_session_valid &&
1137 !plat->force_vbus_detection) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001138 /* Override VBUS detection: enable then value*/
1139 setbits_le32(&usbotg_reg->gotgctl, VB_VALOEN);
1140 setbits_le32(&usbotg_reg->gotgctl, VB_VALOVAL);
1141 } else {
1142 /* Enable VBUS sensing */
1143 setbits_le32(&usbotg_reg->ggpio,
1144 GGPIO_STM32_OTG_GCCFG_VBDEN);
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001145 }
Simon Glass71fa5b42020-12-03 16:55:18 -07001146 if (plat->force_b_session_valid) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001147 /* Override B session bits: enable then value */
1148 setbits_le32(&usbotg_reg->gotgctl, A_VALOEN | B_VALOEN);
1149 setbits_le32(&usbotg_reg->gotgctl,
1150 A_VALOVAL | B_VALOVAL);
1151 } else {
1152 /* Enable ID detection */
1153 setbits_le32(&usbotg_reg->ggpio,
1154 GGPIO_STM32_OTG_GCCFG_IDEN);
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001155 }
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001156 }
1157
Simon Glass71fa5b42020-12-03 16:55:18 -07001158 ret = dwc2_udc_probe(plat);
Patrick Delaunay75150542019-03-29 15:42:15 +01001159 if (ret)
1160 return ret;
1161
1162 the_controller->driver = 0;
1163
1164 ret = usb_add_gadget_udc((struct device *)dev, &the_controller->gadget);
1165
1166 return ret;
1167}
1168
1169static int dwc2_udc_otg_remove(struct udevice *dev)
1170{
1171 struct dwc2_priv_data *priv = dev_get_priv(dev);
1172
1173 usb_del_gadget_udc(&the_controller->gadget);
1174
1175 reset_release_bulk(&priv->resets);
1176
1177 clk_release_bulk(&priv->clks);
1178
developer43903f32020-05-02 11:35:14 +02001179 dwc2_phy_shutdown(dev, &priv->phys);
Patrick Delaunay75150542019-03-29 15:42:15 +01001180
1181 return dm_scan_fdt_dev(dev);
1182}
1183
1184static const struct udevice_id dwc2_udc_otg_ids[] = {
1185 { .compatible = "snps,dwc2" },
Marek Szyprowski2a7a7d92019-12-02 12:11:17 +01001186 { .compatible = "brcm,bcm2835-usb" },
Patrick Delaunay3d09a6a2021-02-09 11:14:46 +01001187 { .compatible = "st,stm32mp15-hsotg",
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001188 .data = (ulong)dwc2_set_stm32mp1_hsotg_params },
1189 {},
Patrick Delaunay75150542019-03-29 15:42:15 +01001190};
1191
1192U_BOOT_DRIVER(dwc2_udc_otg) = {
1193 .name = "dwc2-udc-otg",
1194 .id = UCLASS_USB_GADGET_GENERIC,
1195 .of_match = dwc2_udc_otg_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001196 .of_to_plat = dwc2_udc_otg_of_to_plat,
Patrick Delaunay75150542019-03-29 15:42:15 +01001197 .probe = dwc2_udc_otg_probe,
1198 .remove = dwc2_udc_otg_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -07001199 .plat_auto = sizeof(struct dwc2_plat_otg_data),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001200 .priv_auto = sizeof(struct dwc2_priv_data),
Patrick Delaunay75150542019-03-29 15:42:15 +01001201};
Patrick Delaunaye2ad7792019-03-29 15:42:18 +01001202
1203int dwc2_udc_B_session_valid(struct udevice *dev)
1204{
Simon Glassfa20e932020-12-03 16:55:20 -07001205 struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
Patrick Delaunaye2ad7792019-03-29 15:42:18 +01001206 struct dwc2_usbotg_reg *usbotg_reg =
Simon Glass71fa5b42020-12-03 16:55:18 -07001207 (struct dwc2_usbotg_reg *)plat->regs_otg;
Patrick Delaunaye2ad7792019-03-29 15:42:18 +01001208
1209 return readl(&usbotg_reg->gotgctl) & B_SESSION_VALID;
1210}
Patrick Delaunay75150542019-03-29 15:42:15 +01001211#endif /* CONFIG_IS_ENABLED(DM_USB_GADGET) */