blob: 2f318144421cf2496f9b076408391f1b74ec8bfe [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
Patrick Delaunay75150542019-03-29 15:42:15 +0100239#if !CONFIG_IS_ENABLED(DM_USB_GADGET)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200240/*
241 Register entry point for the peripheral controller driver.
242*/
243int usb_gadget_register_driver(struct usb_gadget_driver *driver)
244{
Marek Vasut1a639ff2015-12-04 00:57:58 +0100245 struct dwc2_udc *dev = the_controller;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200246 int retval = 0;
Bo Shen60dc40c2014-08-27 17:28:18 +0800247 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200248
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200249 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200250
Patrice Chotard1d2f3d52021-02-17 10:17:27 +0100251 if (!driver || driver->speed < USB_SPEED_FULL
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200252 || !driver->bind || !driver->disconnect || !driver->setup)
253 return -EINVAL;
254 if (!dev)
255 return -ENODEV;
256 if (dev->driver)
257 return -EBUSY;
258
259 spin_lock_irqsave(&dev->lock, flags);
260 /* first hook up the driver ... */
261 dev->driver = driver;
262 spin_unlock_irqrestore(&dev->lock, flags);
263
264 if (retval) { /* TODO */
265 printf("target device_add failed, error %d\n", retval);
266 return retval;
267 }
268
269 retval = driver->bind(&dev->gadget);
270 if (retval) {
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200271 debug_cond(DEBUG_SETUP != 0,
272 "%s: bind to driver --> error %d\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200273 dev->gadget.name, retval);
274 dev->driver = 0;
275 return retval;
276 }
277
278 enable_irq(IRQ_OTG);
279
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200280 debug_cond(DEBUG_SETUP != 0,
281 "Registered gadget driver %s\n", dev->gadget.name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200282 udc_enable(dev);
283
284 return 0;
285}
286
287/*
288 * Unregister entry point for the peripheral controller driver.
289 */
290int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
291{
Marek Vasut1a639ff2015-12-04 00:57:58 +0100292 struct dwc2_udc *dev = the_controller;
Bo Shen60dc40c2014-08-27 17:28:18 +0800293 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200294
295 if (!dev)
296 return -ENODEV;
297 if (!driver || driver != dev->driver)
298 return -EINVAL;
299
300 spin_lock_irqsave(&dev->lock, flags);
301 dev->driver = 0;
302 stop_activity(dev, driver);
303 spin_unlock_irqrestore(&dev->lock, flags);
304
305 driver->unbind(&dev->gadget);
306
307 disable_irq(IRQ_OTG);
308
309 udc_disable(dev);
310 return 0;
311}
Patrick Delaunay75150542019-03-29 15:42:15 +0100312#else /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
313
314static int dwc2_gadget_start(struct usb_gadget *g,
315 struct usb_gadget_driver *driver)
316{
317 struct dwc2_udc *dev = the_controller;
318
319 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
320
Patrice Chotard1d2f3d52021-02-17 10:17:27 +0100321 if (!driver || driver->speed < USB_SPEED_FULL ||
Patrick Delaunay75150542019-03-29 15:42:15 +0100322 !driver->bind || !driver->disconnect || !driver->setup)
323 return -EINVAL;
324
325 if (!dev)
326 return -ENODEV;
327
328 if (dev->driver)
329 return -EBUSY;
330
331 /* first hook up the driver ... */
332 dev->driver = driver;
333
334 debug_cond(DEBUG_SETUP != 0,
335 "Registered gadget driver %s\n", dev->gadget.name);
336 return udc_enable(dev);
337}
338
339static int dwc2_gadget_stop(struct usb_gadget *g)
340{
341 struct dwc2_udc *dev = the_controller;
342
343 if (!dev)
344 return -ENODEV;
345
346 if (!dev->driver)
347 return -EINVAL;
348
349 dev->driver = 0;
350 stop_activity(dev, dev->driver);
351
352 udc_disable(dev);
353
354 return 0;
355}
356
357#endif /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200358
359/*
360 * done - retire a request; caller blocked irqs
361 */
Marek Vasut32f931c2015-12-04 01:51:07 +0100362static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200363{
364 unsigned int stopped = ep->stopped;
365
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000366 debug("%s: %s %p, req = %p, stopped = %d\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200367 __func__, ep->ep.name, ep, &req->req, stopped);
368
369 list_del_init(&req->queue);
370
371 if (likely(req->req.status == -EINPROGRESS))
372 req->req.status = status;
373 else
374 status = req->req.status;
375
376 if (status && status != -ESHUTDOWN) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000377 debug("complete %s req %p stat %d len %u/%u\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200378 ep->ep.name, &req->req, status,
379 req->req.actual, req->req.length);
380 }
381
382 /* don't modify queue heads during completion callback */
383 ep->stopped = 1;
384
Lukasz Majewski0dfeb002012-05-02 13:11:35 +0200385#ifdef DEBUG
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200386 printf("calling complete callback\n");
387 {
388 int i, len = req->req.length;
389
390 printf("pkt[%d] = ", req->req.length);
391 if (len > 64)
392 len = 64;
393 for (i = 0; i < len; i++) {
394 printf("%02x", ((u8 *)req->req.buf)[i]);
395 if ((i & 7) == 7)
396 printf(" ");
397 }
398 printf("\n");
399 }
400#endif
401 spin_unlock(&ep->dev->lock);
402 req->req.complete(&ep->ep, &req->req);
403 spin_lock(&ep->dev->lock);
404
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000405 debug("callback completed\n");
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200406
407 ep->stopped = stopped;
408}
409
410/*
411 * nuke - dequeue ALL requests
412 */
Marek Vasut5b309f72015-12-04 01:48:57 +0100413static void nuke(struct dwc2_ep *ep, int status)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200414{
Marek Vasut32f931c2015-12-04 01:51:07 +0100415 struct dwc2_request *req;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200416
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000417 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200418
419 /* called with irqs blocked */
420 while (!list_empty(&ep->queue)) {
Marek Vasut32f931c2015-12-04 01:51:07 +0100421 req = list_entry(ep->queue.next, struct dwc2_request, queue);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200422 done(ep, req, status);
423 }
424}
425
Marek Vasut1a639ff2015-12-04 00:57:58 +0100426static void stop_activity(struct dwc2_udc *dev,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200427 struct usb_gadget_driver *driver)
428{
429 int i;
430
431 /* don't disconnect drivers more than once */
432 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
433 driver = 0;
434 dev->gadget.speed = USB_SPEED_UNKNOWN;
435
436 /* prevent new request submissions, kill any outstanding requests */
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100437 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
Marek Vasut5b309f72015-12-04 01:48:57 +0100438 struct dwc2_ep *ep = &dev->ep[i];
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200439 ep->stopped = 1;
440 nuke(ep, -ESHUTDOWN);
441 }
442
443 /* report disconnect; the driver is already quiesced */
444 if (driver) {
445 spin_unlock(&dev->lock);
446 driver->disconnect(&dev->gadget);
447 spin_lock(&dev->lock);
448 }
449
450 /* re-init driver-visible data structures */
451 udc_reinit(dev);
452}
453
Marek Vasut1a639ff2015-12-04 00:57:58 +0100454static void reconfig_usbd(struct dwc2_udc *dev)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200455{
456 /* 2. Soft-reset OTG Core and then unreset again. */
457 int i;
458 unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
Marek Vasut80300882014-11-04 04:23:25 +0100459 uint32_t dflt_gusbcfg;
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800460 uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100461 u32 max_hw_ep;
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100462 int pdata_hw_ep;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200463
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000464 debug("Reseting OTG controller\n");
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200465
Marek Vasut80300882014-11-04 04:23:25 +0100466 dflt_gusbcfg =
467 0<<15 /* PHY Low Power Clock sel*/
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200468 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
469 |0x5<<10 /* Turnaround time*/
470 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
471 /* 1:SRP enable] H1= 1,1*/
472 |0<<7 /* Ulpi DDR sel*/
473 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
474 |0<<4 /* 0: utmi+, 1:ulpi*/
Steve Raed7198f32016-06-07 15:35:21 -0700475#ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8
476 |0<<3 /* phy i/f 0:8bit, 1:16bit*/
477#else
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200478 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
Steve Raed7198f32016-06-07 15:35:21 -0700479#endif
Marek Vasut80300882014-11-04 04:23:25 +0100480 |0x7<<0; /* HS/FS Timeout**/
481
482 if (dev->pdata->usb_gusbcfg)
483 dflt_gusbcfg = dev->pdata->usb_gusbcfg;
484
485 writel(dflt_gusbcfg, &reg->gusbcfg);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200486
487 /* 3. Put the OTG device core in the disconnected state.*/
488 uTemp = readl(&reg->dctl);
489 uTemp |= SOFT_DISCONNECT;
490 writel(uTemp, &reg->dctl);
491
492 udelay(20);
493
494 /* 4. Make the OTG device core exit from the disconnected state.*/
495 uTemp = readl(&reg->dctl);
496 uTemp = uTemp & ~SOFT_DISCONNECT;
497 writel(uTemp, &reg->dctl);
498
499 /* 5. Configure OTG Core to initial settings of device mode.*/
500 /* [][1: full speed(30Mhz) 0:high speed]*/
501 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
502
503 mdelay(1);
504
505 /* 6. Unmask the core interrupts*/
506 writel(GINTMSK_INIT, &reg->gintmsk);
507
508 /* 7. Set NAK bit of EP0, EP1, EP2*/
509 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
510 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
511
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100512 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200513 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
514 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
515 }
516
517 /* 8. Unmask EPO interrupts*/
518 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
519 | (1 << EP0_CON), &reg->daintmsk);
520
521 /* 9. Unmask device OUT EP common interrupts*/
522 writel(DOEPMSK_INIT, &reg->doepmsk);
523
524 /* 10. Unmask device IN EP common interrupts*/
525 writel(DIEPMSK_INIT, &reg->diepmsk);
526
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800527 rx_fifo_sz = RX_FIFO_SIZE;
528 np_tx_fifo_sz = NPTX_FIFO_SIZE;
529 tx_fifo_sz = PTX_FIFO_SIZE;
530
531 if (dev->pdata->rx_fifo_sz)
532 rx_fifo_sz = dev->pdata->rx_fifo_sz;
533 if (dev->pdata->np_tx_fifo_sz)
534 np_tx_fifo_sz = dev->pdata->np_tx_fifo_sz;
535 if (dev->pdata->tx_fifo_sz)
536 tx_fifo_sz = dev->pdata->tx_fifo_sz;
537
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200538 /* 11. Set Rx FIFO Size (in 32-bit words) */
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800539 writel(rx_fifo_sz, &reg->grxfsiz);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200540
541 /* 12. Set Non Periodic Tx FIFO Size */
Xu Ziyuanb729dc02016-07-14 14:52:33 +0800542 writel((np_tx_fifo_sz << 16) | rx_fifo_sz,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200543 &reg->gnptxfsiz);
544
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100545 /* retrieve the number of IN Endpoints (excluding ep0) */
546 max_hw_ep = (readl(&reg->ghwcfg4) & GHWCFG4_NUM_IN_EPS_MASK) >>
547 GHWCFG4_NUM_IN_EPS_SHIFT;
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100548 pdata_hw_ep = dev->pdata->tx_fifo_sz_nb;
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100549
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100550 /* tx_fifo_sz_nb should equal to number of IN Endpoint */
551 if (pdata_hw_ep && max_hw_ep != pdata_hw_ep)
552 pr_warn("Got %d hw endpoint but %d tx-fifo-size in array !!\n",
553 max_hw_ep, pdata_hw_ep);
554
555 for (i = 0; i < max_hw_ep; i++) {
556 if (pdata_hw_ep)
557 tx_fifo_sz = dev->pdata->tx_fifo_sz_array[i];
558
Patrick Delaunay80a0de12019-03-29 15:42:19 +0100559 writel((rx_fifo_sz + np_tx_fifo_sz + (tx_fifo_sz * i)) |
560 tx_fifo_sz << 16, &reg->dieptxf[i]);
Patrice Chotard00f3fc62019-03-29 15:42:20 +0100561 }
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200562 /* Flush the RX FIFO */
563 writel(RX_FIFO_FLUSH, &reg->grstctl);
564 while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100565 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200566
567 /* Flush all the Tx FIFO's */
568 writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
569 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
570 while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100571 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200572
573 /* 13. Clear NAK bit of EP0, EP1, EP2*/
574 /* For Slave mode*/
575 /* EP0: Control OUT */
576 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
577 &reg->out_endp[EP0_CON].doepctl);
578
579 /* 14. Initialize OTG Link Core.*/
580 writel(GAHBCFG_INIT, &reg->gahbcfg);
581}
582
Marek Vasut1a639ff2015-12-04 00:57:58 +0100583static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200584{
585 unsigned int ep_ctrl;
586 int i;
587
588 if (speed == USB_SPEED_HIGH) {
589 ep0_fifo_size = 64;
590 ep_fifo_size = 512;
591 ep_fifo_size2 = 1024;
592 dev->gadget.speed = USB_SPEED_HIGH;
593 } else {
594 ep0_fifo_size = 64;
595 ep_fifo_size = 64;
596 ep_fifo_size2 = 64;
597 dev->gadget.speed = USB_SPEED_FULL;
598 }
599
600 dev->ep[0].ep.maxpacket = ep0_fifo_size;
Marek Vasutcb9c5d02015-12-04 02:44:33 +0100601 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200602 dev->ep[i].ep.maxpacket = ep_fifo_size;
603
604 /* EP0 - Control IN (64 bytes)*/
605 ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
606 writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
607
608 /* EP0 - Control OUT (64 bytes)*/
609 ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
610 writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
611}
612
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100613static int dwc2_ep_enable(struct usb_ep *_ep,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200614 const struct usb_endpoint_descriptor *desc)
615{
Marek Vasut5b309f72015-12-04 01:48:57 +0100616 struct dwc2_ep *ep;
Marek Vasut1a639ff2015-12-04 00:57:58 +0100617 struct dwc2_udc *dev;
Bo Shen60dc40c2014-08-27 17:28:18 +0800618 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200619
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000620 debug("%s: %p\n", __func__, _ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200621
Marek Vasut5b309f72015-12-04 01:48:57 +0100622 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200623 if (!_ep || !desc || ep->desc || _ep->name == ep0name
624 || desc->bDescriptorType != USB_DT_ENDPOINT
625 || ep->bEndpointAddress != desc->bEndpointAddress
Tom Rini73424522011-12-15 08:40:51 -0700626 || ep_maxpacket(ep) <
627 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200628
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000629 debug("%s: bad ep or descriptor\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200630 return -EINVAL;
631 }
632
633 /* xfer types must match, except that interrupt ~= bulk */
634 if (ep->bmAttributes != desc->bmAttributes
635 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
636 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
637
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000638 debug("%s: %s type mismatch\n", __func__, _ep->name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200639 return -EINVAL;
640 }
641
642 /* hardware _could_ do smaller, but driver doesn't */
Frank Wang0d64b5b2016-01-27 12:39:40 -0800643 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK &&
644 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) >
Tom Rini73424522011-12-15 08:40:51 -0700645 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200646
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000647 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200648 return -ERANGE;
649 }
650
651 dev = ep->dev;
652 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
653
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000654 debug("%s: bogus device state\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200655 return -ESHUTDOWN;
656 }
657
658 ep->stopped = 0;
659 ep->desc = desc;
660 ep->pio_irqs = 0;
Tom Rini73424522011-12-15 08:40:51 -0700661 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200662
663 /* Reset halt state */
Marek Vasutfa03c812015-12-04 02:03:45 +0100664 dwc2_udc_set_nak(ep);
665 dwc2_udc_set_halt(_ep, 0);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200666
667 spin_lock_irqsave(&ep->dev->lock, flags);
Marek Vasutfa03c812015-12-04 02:03:45 +0100668 dwc2_udc_ep_activate(ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200669 spin_unlock_irqrestore(&ep->dev->lock, flags);
670
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000671 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200672 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
673 return 0;
674}
675
676/*
677 * Disable EP
678 */
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100679static int dwc2_ep_disable(struct usb_ep *_ep)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200680{
Marek Vasut5b309f72015-12-04 01:48:57 +0100681 struct dwc2_ep *ep;
Bo Shen60dc40c2014-08-27 17:28:18 +0800682 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200683
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000684 debug("%s: %p\n", __func__, _ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200685
Marek Vasut5b309f72015-12-04 01:48:57 +0100686 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200687 if (!_ep || !ep->desc) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000688 debug("%s: %s not enabled\n", __func__,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200689 _ep ? ep->ep.name : NULL);
690 return -EINVAL;
691 }
692
693 spin_lock_irqsave(&ep->dev->lock, flags);
694
695 /* Nuke all pending requests */
696 nuke(ep, -ESHUTDOWN);
697
698 ep->desc = 0;
699 ep->stopped = 1;
700
701 spin_unlock_irqrestore(&ep->dev->lock, flags);
702
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000703 debug("%s: disabled %s\n", __func__, _ep->name);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200704 return 0;
705}
706
Marek Vasut153150f2015-12-04 02:17:40 +0100707static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200708 gfp_t gfp_flags)
709{
Marek Vasut32f931c2015-12-04 01:51:07 +0100710 struct dwc2_request *req;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200711
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000712 debug("%s: %s %p\n", __func__, ep->name, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200713
Mike Frysinger1f19e3f2012-04-26 02:34:44 +0000714 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200715 if (!req)
716 return 0;
717
718 memset(req, 0, sizeof *req);
719 INIT_LIST_HEAD(&req->queue);
720
721 return &req->req;
722}
723
Marek Vasut153150f2015-12-04 02:17:40 +0100724static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200725{
Marek Vasut32f931c2015-12-04 01:51:07 +0100726 struct dwc2_request *req;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200727
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000728 debug("%s: %p\n", __func__, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200729
Marek Vasut32f931c2015-12-04 01:51:07 +0100730 req = container_of(_req, struct dwc2_request, req);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200731 WARN_ON(!list_empty(&req->queue));
732 kfree(req);
733}
734
735/* dequeue JUST ONE request */
Marek Vasut153150f2015-12-04 02:17:40 +0100736static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200737{
Marek Vasut5b309f72015-12-04 01:48:57 +0100738 struct dwc2_ep *ep;
Marek Vasut32f931c2015-12-04 01:51:07 +0100739 struct dwc2_request *req;
Bo Shen60dc40c2014-08-27 17:28:18 +0800740 unsigned long flags = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200741
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000742 debug("%s: %p\n", __func__, _ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200743
Marek Vasut5b309f72015-12-04 01:48:57 +0100744 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200745 if (!_ep || ep->ep.name == ep0name)
746 return -EINVAL;
747
748 spin_lock_irqsave(&ep->dev->lock, flags);
749
750 /* make sure it's actually queued on this endpoint */
751 list_for_each_entry(req, &ep->queue, queue) {
752 if (&req->req == _req)
753 break;
754 }
755 if (&req->req != _req) {
756 spin_unlock_irqrestore(&ep->dev->lock, flags);
757 return -EINVAL;
758 }
759
760 done(ep, req, -ECONNRESET);
761
762 spin_unlock_irqrestore(&ep->dev->lock, flags);
763 return 0;
764}
765
766/*
767 * Return bytes in EP FIFO
768 */
Marek Vasut153150f2015-12-04 02:17:40 +0100769static int dwc2_fifo_status(struct usb_ep *_ep)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200770{
771 int count = 0;
Marek Vasut5b309f72015-12-04 01:48:57 +0100772 struct dwc2_ep *ep;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200773
Marek Vasut5b309f72015-12-04 01:48:57 +0100774 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200775 if (!_ep) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000776 debug("%s: bad ep\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200777 return -ENODEV;
778 }
779
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000780 debug("%s: %d\n", __func__, ep_index(ep));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200781
782 /* LPD can't report unclaimed bytes from IN fifos */
783 if (ep_is_in(ep))
784 return -EOPNOTSUPP;
785
786 return count;
787}
788
789/*
790 * Flush EP FIFO
791 */
Marek Vasut153150f2015-12-04 02:17:40 +0100792static void dwc2_fifo_flush(struct usb_ep *_ep)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200793{
Marek Vasut5b309f72015-12-04 01:48:57 +0100794 struct dwc2_ep *ep;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200795
Marek Vasut5b309f72015-12-04 01:48:57 +0100796 ep = container_of(_ep, struct dwc2_ep, ep);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200797 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000798 debug("%s: bad ep\n", __func__);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200799 return;
800 }
801
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000802 debug("%s: %d\n", __func__, ep_index(ep));
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200803}
804
Marek Vasutfa03c812015-12-04 02:03:45 +0100805static const struct usb_gadget_ops dwc2_udc_ops = {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200806 /* current versions must always be self-powered */
Patrick Delaunay75150542019-03-29 15:42:15 +0100807#if CONFIG_IS_ENABLED(DM_USB_GADGET)
808 .udc_start = dwc2_gadget_start,
809 .udc_stop = dwc2_gadget_stop,
810#endif
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200811};
812
Marek Vasut1a639ff2015-12-04 00:57:58 +0100813static struct dwc2_udc memory = {
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200814 .usb_address = 0,
815 .gadget = {
Marek Vasutfa03c812015-12-04 02:03:45 +0100816 .ops = &dwc2_udc_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200817 .ep0 = &memory.ep[0].ep,
818 .name = driver_name,
819 },
820
821 /* control endpoint */
822 .ep[0] = {
823 .ep = {
824 .name = ep0name,
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100825 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200826 .maxpacket = EP0_FIFO_SIZE,
827 },
828 .dev = &memory,
829
830 .bEndpointAddress = 0,
831 .bmAttributes = 0,
832
833 .ep_type = ep_control,
834 },
835
836 /* first group of endpoints */
837 .ep[1] = {
838 .ep = {
839 .name = "ep1in-bulk",
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100840 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200841 .maxpacket = EP_FIFO_SIZE,
842 },
843 .dev = &memory,
844
845 .bEndpointAddress = USB_DIR_IN | 1,
846 .bmAttributes = USB_ENDPOINT_XFER_BULK,
847
848 .ep_type = ep_bulk_out,
849 .fifo_num = 1,
850 },
851
852 .ep[2] = {
853 .ep = {
854 .name = "ep2out-bulk",
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100855 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200856 .maxpacket = EP_FIFO_SIZE,
857 },
858 .dev = &memory,
859
860 .bEndpointAddress = USB_DIR_OUT | 2,
861 .bmAttributes = USB_ENDPOINT_XFER_BULK,
862
863 .ep_type = ep_bulk_in,
864 .fifo_num = 2,
865 },
866
867 .ep[3] = {
868 .ep = {
869 .name = "ep3in-int",
Marek Vasut4e7c29e2015-12-04 02:13:42 +0100870 .ops = &dwc2_ep_ops,
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200871 .maxpacket = EP_FIFO_SIZE,
872 },
873 .dev = &memory,
874
875 .bEndpointAddress = USB_DIR_IN | 3,
876 .bmAttributes = USB_ENDPOINT_XFER_INT,
877
878 .ep_type = ep_interrupt,
879 .fifo_num = 3,
880 },
881};
882
883/*
884 * probe - binds to the platform device
885 */
886
Marek Vasut01b61fa2015-12-04 02:26:33 +0100887int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200888{
Marek Vasut1a639ff2015-12-04 00:57:58 +0100889 struct dwc2_udc *dev = &memory;
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100890 int retval = 0;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200891
Anatolij Gustschine63ce4f2011-12-19 04:20:35 +0000892 debug("%s: %p\n", __func__, pdata);
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200893
894 dev->pdata = pdata;
895
Marek Vasut67ec9dc2015-12-04 01:11:45 +0100896 reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200897
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200898 dev->gadget.is_dualspeed = 1; /* Hack only*/
899 dev->gadget.is_otg = 0;
900 dev->gadget.is_a_peripheral = 0;
901 dev->gadget.b_hnp_enable = 0;
902 dev->gadget.a_hnp_support = 0;
903 dev->gadget.a_alt_hnp_support = 0;
904
905 the_controller = dev;
906
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100907 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
908 ROUND(sizeof(struct usb_ctrlrequest),
909 CONFIG_SYS_CACHELINE_SIZE));
910 if (!usb_ctrl) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900911 pr_err("No memory available for UDC!\n");
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100912 return -ENOMEM;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200913 }
Lukasz Majewski904f8f72014-02-05 10:10:44 +0100914
915 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200916
917 udc_reinit(dev);
918
919 return retval;
920}
921
Patrick Delaunay75150542019-03-29 15:42:15 +0100922int dwc2_udc_handle_interrupt(void)
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200923{
924 u32 intr_status = readl(&reg->gintsts);
925 u32 gintmsk = readl(&reg->gintmsk);
926
927 if (intr_status & gintmsk)
Marek Vasutfa03c812015-12-04 02:03:45 +0100928 return dwc2_udc_irq(1, (void *)the_controller);
Patrick Delaunay75150542019-03-29 15:42:15 +0100929
930 return 0;
931}
932
933#if !CONFIG_IS_ENABLED(DM_USB_GADGET)
934
935int usb_gadget_handle_interrupts(int index)
936{
937 return dwc2_udc_handle_interrupt();
938}
939
940#else /* CONFIG_IS_ENABLED(DM_USB_GADGET) */
941
942struct dwc2_priv_data {
943 struct clk_bulk clks;
944 struct reset_ctl_bulk resets;
developer43903f32020-05-02 11:35:14 +0200945 struct phy_bulk phys;
Patrick Delaunaye014cb62019-03-29 15:42:21 +0100946 struct udevice *usb33d_supply;
Patrick Delaunay75150542019-03-29 15:42:15 +0100947};
948
949int dm_usb_gadget_handle_interrupts(struct udevice *dev)
950{
951 return dwc2_udc_handle_interrupt();
952}
953
developer43903f32020-05-02 11:35:14 +0200954static int dwc2_phy_setup(struct udevice *dev, struct phy_bulk *phys)
Patrick Delaunay75150542019-03-29 15:42:15 +0100955{
developer43903f32020-05-02 11:35:14 +0200956 int ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100957
developer43903f32020-05-02 11:35:14 +0200958 ret = generic_phy_get_bulk(dev, phys);
959 if (ret)
960 return ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100961
developer43903f32020-05-02 11:35:14 +0200962 ret = generic_phy_init_bulk(phys);
963 if (ret)
964 return ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100965
developer43903f32020-05-02 11:35:14 +0200966 ret = generic_phy_power_on_bulk(phys);
967 if (ret)
968 generic_phy_exit_bulk(phys);
Patrick Delaunay75150542019-03-29 15:42:15 +0100969
970 return ret;
Lukasz Majewskiac3c8312011-10-27 10:36:46 +0200971}
Patrick Delaunay75150542019-03-29 15:42:15 +0100972
developer43903f32020-05-02 11:35:14 +0200973static void dwc2_phy_shutdown(struct udevice *dev, struct phy_bulk *phys)
Patrick Delaunay75150542019-03-29 15:42:15 +0100974{
developer43903f32020-05-02 11:35:14 +0200975 generic_phy_power_off_bulk(phys);
976 generic_phy_exit_bulk(phys);
Patrick Delaunay75150542019-03-29 15:42:15 +0100977}
978
Simon Glassaad29ae2020-12-03 16:55:21 -0700979static int dwc2_udc_otg_of_to_plat(struct udevice *dev)
Patrick Delaunay75150542019-03-29 15:42:15 +0100980{
Simon Glassfa20e932020-12-03 16:55:20 -0700981 struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
Patrick Delaunaye014cb62019-03-29 15:42:21 +0100982 ulong drvdata;
983 void (*set_params)(struct dwc2_plat_otg_data *data);
Patrick Delaunayed742632019-06-18 16:57:16 +0200984 int ret;
Patrick Delaunay75150542019-03-29 15:42:15 +0100985
Simon Glassa7ece582020-12-19 10:40:14 -0700986 if (usb_get_dr_mode(dev_ofnode(dev)) != USB_DR_MODE_PERIPHERAL &&
987 usb_get_dr_mode(dev_ofnode(dev)) != USB_DR_MODE_OTG) {
Patrick Delaunay75150542019-03-29 15:42:15 +0100988 dev_dbg(dev, "Invalid mode\n");
989 return -ENODEV;
990 }
991
Simon Glass71fa5b42020-12-03 16:55:18 -0700992 plat->regs_otg = dev_read_addr(dev);
Patrick Delaunay75150542019-03-29 15:42:15 +0100993
Simon Glass71fa5b42020-12-03 16:55:18 -0700994 plat->rx_fifo_sz = dev_read_u32_default(dev, "g-rx-fifo-size", 0);
995 plat->np_tx_fifo_sz = dev_read_u32_default(dev, "g-np-tx-fifo-size", 0);
Patrick Delaunayed742632019-06-18 16:57:16 +0200996
Simon Glass71fa5b42020-12-03 16:55:18 -0700997 plat->tx_fifo_sz_nb =
Patrick Delaunayed742632019-06-18 16:57:16 +0200998 dev_read_size(dev, "g-tx-fifo-size") / sizeof(u32);
Simon Glass71fa5b42020-12-03 16:55:18 -0700999 if (plat->tx_fifo_sz_nb > DWC2_MAX_HW_ENDPOINTS)
1000 plat->tx_fifo_sz_nb = DWC2_MAX_HW_ENDPOINTS;
1001 if (plat->tx_fifo_sz_nb) {
Patrick Delaunayed742632019-06-18 16:57:16 +02001002 ret = dev_read_u32_array(dev, "g-tx-fifo-size",
Simon Glass71fa5b42020-12-03 16:55:18 -07001003 plat->tx_fifo_sz_array,
1004 plat->tx_fifo_sz_nb);
Patrick Delaunayed742632019-06-18 16:57:16 +02001005 if (ret)
1006 return ret;
1007 }
Patrick Delaunay75150542019-03-29 15:42:15 +01001008
Simon Glass71fa5b42020-12-03 16:55:18 -07001009 plat->force_b_session_valid =
Patrick Delaunay7f3384d2019-03-29 15:42:24 +01001010 dev_read_bool(dev, "u-boot,force-b-session-valid");
Patrick Delaunay0c42d1d2019-03-29 15:42:17 +01001011
Simon Glass71fa5b42020-12-03 16:55:18 -07001012 plat->force_vbus_detection =
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001013 dev_read_bool(dev, "u-boot,force-vbus-detection");
1014
Simon Glass71fa5b42020-12-03 16:55:18 -07001015 /* force plat according compatible */
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001016 drvdata = dev_get_driver_data(dev);
1017 if (drvdata) {
1018 set_params = (void *)drvdata;
Simon Glass71fa5b42020-12-03 16:55:18 -07001019 set_params(plat);
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001020 }
1021
Patrick Delaunay75150542019-03-29 15:42:15 +01001022 return 0;
1023}
1024
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001025static void dwc2_set_stm32mp1_hsotg_params(struct dwc2_plat_otg_data *p)
1026{
1027 p->activate_stm_id_vb_detection = true;
1028 p->usb_gusbcfg =
1029 0 << 15 /* PHY Low Power Clock sel*/
1030 | 0x9 << 10 /* USB Turnaround time (0x9 for HS phy) */
1031 | 0 << 9 /* [0:HNP disable,1:HNP enable]*/
1032 | 0 << 8 /* [0:SRP disable 1:SRP enable]*/
1033 | 0 << 6 /* 0: high speed utmi+, 1: full speed serial*/
1034 | 0x7 << 0; /* FS timeout calibration**/
Patrick Delaunay7f3384d2019-03-29 15:42:24 +01001035
1036 if (p->force_b_session_valid)
1037 p->usb_gusbcfg |= 1 << 30; /* FDMOD: Force device mode */
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001038}
1039
Patrick Delaunay75150542019-03-29 15:42:15 +01001040static int dwc2_udc_otg_reset_init(struct udevice *dev,
1041 struct reset_ctl_bulk *resets)
1042{
1043 int ret;
1044
1045 ret = reset_get_bulk(dev, resets);
Jaehoon Chung09aee382020-10-21 21:28:41 +09001046 if (ret == -ENOTSUPP || ret == -ENOENT)
Patrick Delaunay75150542019-03-29 15:42:15 +01001047 return 0;
1048
1049 if (ret)
1050 return ret;
1051
Patrick Delaunay9e4abe32019-03-29 15:42:16 +01001052 ret = reset_assert_bulk(resets);
1053
1054 if (!ret) {
1055 udelay(2);
1056 ret = reset_deassert_bulk(resets);
1057 }
Patrick Delaunay75150542019-03-29 15:42:15 +01001058 if (ret) {
1059 reset_release_bulk(resets);
1060 return ret;
1061 }
1062
1063 return 0;
1064}
1065
1066static int dwc2_udc_otg_clk_init(struct udevice *dev,
1067 struct clk_bulk *clks)
1068{
1069 int ret;
1070
1071 ret = clk_get_bulk(dev, clks);
1072 if (ret == -ENOSYS)
1073 return 0;
1074
1075 if (ret)
1076 return ret;
1077
1078 ret = clk_enable_bulk(clks);
1079 if (ret) {
1080 clk_release_bulk(clks);
1081 return ret;
1082 }
1083
1084 return 0;
1085}
1086
1087static int dwc2_udc_otg_probe(struct udevice *dev)
1088{
Simon Glassfa20e932020-12-03 16:55:20 -07001089 struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
Patrick Delaunay75150542019-03-29 15:42:15 +01001090 struct dwc2_priv_data *priv = dev_get_priv(dev);
Patrick Delaunay0c42d1d2019-03-29 15:42:17 +01001091 struct dwc2_usbotg_reg *usbotg_reg =
Simon Glass71fa5b42020-12-03 16:55:18 -07001092 (struct dwc2_usbotg_reg *)plat->regs_otg;
Patrick Delaunay75150542019-03-29 15:42:15 +01001093 int ret;
1094
1095 ret = dwc2_udc_otg_clk_init(dev, &priv->clks);
1096 if (ret)
1097 return ret;
1098
1099 ret = dwc2_udc_otg_reset_init(dev, &priv->resets);
1100 if (ret)
1101 return ret;
1102
developer43903f32020-05-02 11:35:14 +02001103 ret = dwc2_phy_setup(dev, &priv->phys);
Patrick Delaunay75150542019-03-29 15:42:15 +01001104 if (ret)
1105 return ret;
1106
Simon Glass71fa5b42020-12-03 16:55:18 -07001107 if (plat->activate_stm_id_vb_detection) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001108 if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
Simon Glass71fa5b42020-12-03 16:55:18 -07001109 (!plat->force_b_session_valid ||
1110 plat->force_vbus_detection)) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001111 ret = device_get_supply_regulator(dev, "usb33d-supply",
1112 &priv->usb33d_supply);
1113 if (ret) {
1114 dev_err(dev, "can't get voltage level detector supply\n");
1115 return ret;
1116 }
1117 ret = regulator_set_enable(priv->usb33d_supply, true);
1118 if (ret) {
1119 dev_err(dev, "can't enable voltage level detector supply\n");
1120 return ret;
1121 }
1122 }
1123
Simon Glass71fa5b42020-12-03 16:55:18 -07001124 if (plat->force_b_session_valid &&
1125 !plat->force_vbus_detection) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001126 /* Override VBUS detection: enable then value*/
1127 setbits_le32(&usbotg_reg->gotgctl, VB_VALOEN);
1128 setbits_le32(&usbotg_reg->gotgctl, VB_VALOVAL);
1129 } else {
1130 /* Enable VBUS sensing */
1131 setbits_le32(&usbotg_reg->ggpio,
1132 GGPIO_STM32_OTG_GCCFG_VBDEN);
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001133 }
Simon Glass71fa5b42020-12-03 16:55:18 -07001134 if (plat->force_b_session_valid) {
Patrick Delaunay99df7a52020-10-15 14:49:37 +02001135 /* Override B session bits: enable then value */
1136 setbits_le32(&usbotg_reg->gotgctl, A_VALOEN | B_VALOEN);
1137 setbits_le32(&usbotg_reg->gotgctl,
1138 A_VALOVAL | B_VALOVAL);
1139 } else {
1140 /* Enable ID detection */
1141 setbits_le32(&usbotg_reg->ggpio,
1142 GGPIO_STM32_OTG_GCCFG_IDEN);
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001143 }
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001144 }
1145
Simon Glass71fa5b42020-12-03 16:55:18 -07001146 ret = dwc2_udc_probe(plat);
Patrick Delaunay75150542019-03-29 15:42:15 +01001147 if (ret)
1148 return ret;
1149
1150 the_controller->driver = 0;
1151
1152 ret = usb_add_gadget_udc((struct device *)dev, &the_controller->gadget);
1153
1154 return ret;
1155}
1156
1157static int dwc2_udc_otg_remove(struct udevice *dev)
1158{
1159 struct dwc2_priv_data *priv = dev_get_priv(dev);
1160
1161 usb_del_gadget_udc(&the_controller->gadget);
1162
1163 reset_release_bulk(&priv->resets);
1164
1165 clk_release_bulk(&priv->clks);
1166
developer43903f32020-05-02 11:35:14 +02001167 dwc2_phy_shutdown(dev, &priv->phys);
Patrick Delaunay75150542019-03-29 15:42:15 +01001168
1169 return dm_scan_fdt_dev(dev);
1170}
1171
1172static const struct udevice_id dwc2_udc_otg_ids[] = {
1173 { .compatible = "snps,dwc2" },
Marek Szyprowski2a7a7d92019-12-02 12:11:17 +01001174 { .compatible = "brcm,bcm2835-usb" },
Patrick Delaunay3d09a6a2021-02-09 11:14:46 +01001175 { .compatible = "st,stm32mp15-hsotg",
Patrick Delaunaye014cb62019-03-29 15:42:21 +01001176 .data = (ulong)dwc2_set_stm32mp1_hsotg_params },
1177 {},
Patrick Delaunay75150542019-03-29 15:42:15 +01001178};
1179
1180U_BOOT_DRIVER(dwc2_udc_otg) = {
1181 .name = "dwc2-udc-otg",
1182 .id = UCLASS_USB_GADGET_GENERIC,
1183 .of_match = dwc2_udc_otg_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001184 .of_to_plat = dwc2_udc_otg_of_to_plat,
Patrick Delaunay75150542019-03-29 15:42:15 +01001185 .probe = dwc2_udc_otg_probe,
1186 .remove = dwc2_udc_otg_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -07001187 .plat_auto = sizeof(struct dwc2_plat_otg_data),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001188 .priv_auto = sizeof(struct dwc2_priv_data),
Patrick Delaunay75150542019-03-29 15:42:15 +01001189};
Patrick Delaunaye2ad7792019-03-29 15:42:18 +01001190
1191int dwc2_udc_B_session_valid(struct udevice *dev)
1192{
Simon Glassfa20e932020-12-03 16:55:20 -07001193 struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
Patrick Delaunaye2ad7792019-03-29 15:42:18 +01001194 struct dwc2_usbotg_reg *usbotg_reg =
Simon Glass71fa5b42020-12-03 16:55:18 -07001195 (struct dwc2_usbotg_reg *)plat->regs_otg;
Patrick Delaunaye2ad7792019-03-29 15:42:18 +01001196
1197 return readl(&usbotg_reg->gotgctl) & B_SESSION_VALID;
1198}
Patrick Delaunay75150542019-03-29 15:42:15 +01001199#endif /* CONFIG_IS_ENABLED(DM_USB_GADGET) */