Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 2 | /* |
Marek Vasut | f8995db | 2015-12-04 02:34:46 +0100 | [diff] [blame] | 3 | * drivers/usb/gadget/dwc2_udc_otg.c |
Marek Vasut | 4a7629a | 2015-12-04 02:55:37 +0100 | [diff] [blame] | 4 | * Designware DWC2 on-chip full/high speed USB OTG 2.0 device controllers |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 5 | * |
| 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 Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 18 | */ |
Lukasz Majewski | 8fb78fd | 2012-05-02 13:11:38 +0200 | [diff] [blame] | 19 | #undef DEBUG |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 20 | #include <common.h> |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 21 | #include <clk.h> |
| 22 | #include <dm.h> |
| 23 | #include <generic-phy.h> |
| 24 | #include <malloc.h> |
| 25 | #include <reset.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 26 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 27 | #include <dm/devres.h> |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 28 | |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 29 | #include <linux/errno.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 30 | #include <linux/list.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 31 | |
| 32 | #include <linux/usb/ch9.h> |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 33 | #include <linux/usb/otg.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 34 | #include <linux/usb/gadget.h> |
| 35 | |
Marek Szyprowski | 2a7a7d9 | 2019-12-02 12:11:17 +0100 | [diff] [blame] | 36 | #include <phys2bus.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 37 | #include <asm/byteorder.h> |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 39 | #include <asm/io.h> |
| 40 | |
| 41 | #include <asm/mach-types.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 42 | |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 43 | #include <power/regulator.h> |
| 44 | |
Marek Vasut | 4811c66 | 2015-12-04 02:32:22 +0100 | [diff] [blame] | 45 | #include "dwc2_udc_otg_regs.h" |
| 46 | #include "dwc2_udc_otg_priv.h" |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 47 | |
| 48 | /***********************************************************/ |
| 49 | |
| 50 | #define OTG_DMA_MODE 1 |
| 51 | |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 52 | #define DEBUG_SETUP 0 |
| 53 | #define DEBUG_EP0 0 |
| 54 | #define DEBUG_ISR 0 |
| 55 | #define DEBUG_OUT_EP 0 |
| 56 | #define DEBUG_IN_EP 0 |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 57 | |
Marek Vasut | f1be9cb | 2015-12-04 02:51:20 +0100 | [diff] [blame] | 58 | #include <usb/dwc2_udc.h> |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 59 | |
| 60 | #define EP0_CON 0 |
| 61 | #define EP_MASK 0xF |
| 62 | |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 63 | static char *state_names[] = { |
| 64 | "WAIT_FOR_SETUP", |
| 65 | "DATA_STATE_XMIT", |
| 66 | "DATA_STATE_NEED_ZLP", |
| 67 | "WAIT_FOR_OUT_STATUS", |
| 68 | "DATA_STATE_RECV", |
| 69 | "WAIT_FOR_COMPLETE", |
| 70 | "WAIT_FOR_OUT_COMPLETE", |
| 71 | "WAIT_FOR_IN_COMPLETE", |
| 72 | "WAIT_FOR_NULL_COMPLETE", |
| 73 | }; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 74 | |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 75 | #define DRIVER_VERSION "15 March 2009" |
| 76 | |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 77 | struct dwc2_udc *the_controller; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 78 | |
Marek Vasut | dbdd58f | 2015-12-04 02:28:40 +0100 | [diff] [blame] | 79 | static const char driver_name[] = "dwc2-udc"; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 80 | static const char ep0name[] = "ep0-control"; |
| 81 | |
| 82 | /* Max packet size*/ |
| 83 | static unsigned int ep0_fifo_size = 64; |
| 84 | static unsigned int ep_fifo_size = 512; |
| 85 | static unsigned int ep_fifo_size2 = 1024; |
| 86 | static int reset_available = 1; |
| 87 | |
| 88 | static struct usb_ctrlrequest *usb_ctrl; |
| 89 | static dma_addr_t usb_ctrl_dma_addr; |
| 90 | |
| 91 | /* |
| 92 | Local declarations. |
| 93 | */ |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 94 | static int dwc2_ep_enable(struct usb_ep *ep, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 95 | const struct usb_endpoint_descriptor *); |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 96 | static int dwc2_ep_disable(struct usb_ep *ep); |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 97 | static struct usb_request *dwc2_alloc_request(struct usb_ep *ep, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 98 | gfp_t gfp_flags); |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 99 | static void dwc2_free_request(struct usb_ep *ep, struct usb_request *); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 100 | |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 101 | static int dwc2_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags); |
| 102 | static int dwc2_dequeue(struct usb_ep *ep, struct usb_request *); |
| 103 | static int dwc2_fifo_status(struct usb_ep *ep); |
| 104 | static void dwc2_fifo_flush(struct usb_ep *ep); |
Marek Vasut | fe92a51 | 2015-12-04 01:59:12 +0100 | [diff] [blame] | 105 | static void dwc2_ep0_read(struct dwc2_udc *dev); |
| 106 | static void dwc2_ep0_kick(struct dwc2_udc *dev, struct dwc2_ep *ep); |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 107 | static void dwc2_handle_ep0(struct dwc2_udc *dev); |
Marek Vasut | fe92a51 | 2015-12-04 01:59:12 +0100 | [diff] [blame] | 108 | static int dwc2_ep0_write(struct dwc2_udc *dev); |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 109 | static int write_fifo_ep0(struct dwc2_ep *ep, struct dwc2_request *req); |
| 110 | static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status); |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 111 | static void stop_activity(struct dwc2_udc *dev, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 112 | struct usb_gadget_driver *driver); |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 113 | static int udc_enable(struct dwc2_udc *dev); |
| 114 | static void udc_set_address(struct dwc2_udc *dev, unsigned char address); |
| 115 | static void reconfig_usbd(struct dwc2_udc *dev); |
| 116 | static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed); |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 117 | static void nuke(struct dwc2_ep *ep, int status); |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 118 | static int dwc2_udc_set_halt(struct usb_ep *_ep, int value); |
| 119 | static void dwc2_udc_set_nak(struct dwc2_ep *ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 120 | |
Lukasz Majewski | 8fb78fd | 2012-05-02 13:11:38 +0200 | [diff] [blame] | 121 | void set_udc_gadget_private_data(void *p) |
| 122 | { |
| 123 | debug_cond(DEBUG_SETUP != 0, |
| 124 | "%s: the_controller: 0x%p, p: 0x%p\n", __func__, |
| 125 | the_controller, p); |
| 126 | the_controller->gadget.dev.device_data = p; |
| 127 | } |
| 128 | |
| 129 | void *get_udc_gadget_private_data(struct usb_gadget *gadget) |
| 130 | { |
| 131 | return gadget->dev.device_data; |
| 132 | } |
| 133 | |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 134 | static struct usb_ep_ops dwc2_ep_ops = { |
| 135 | .enable = dwc2_ep_enable, |
| 136 | .disable = dwc2_ep_disable, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 137 | |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 138 | .alloc_request = dwc2_alloc_request, |
| 139 | .free_request = dwc2_free_request, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 140 | |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 141 | .queue = dwc2_queue, |
| 142 | .dequeue = dwc2_dequeue, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 143 | |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 144 | .set_halt = dwc2_udc_set_halt, |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 145 | .fifo_status = dwc2_fifo_status, |
| 146 | .fifo_flush = dwc2_fifo_flush, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | #define create_proc_files() do {} while (0) |
| 150 | #define remove_proc_files() do {} while (0) |
| 151 | |
| 152 | /***********************************************************/ |
| 153 | |
Marek Vasut | 67ec9dc | 2015-12-04 01:11:45 +0100 | [diff] [blame] | 154 | struct dwc2_usbotg_reg *reg; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 155 | |
Lukasz Majewski | ef73102 | 2014-08-25 11:07:29 +0200 | [diff] [blame] | 156 | bool dfu_usb_get_reset(void) |
| 157 | { |
| 158 | return !!(readl(®->gintsts) & INT_RESET); |
| 159 | } |
| 160 | |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 161 | __weak void otg_phy_init(struct dwc2_udc *dev) {} |
| 162 | __weak void otg_phy_off(struct dwc2_udc *dev) {} |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 163 | |
| 164 | /***********************************************************/ |
| 165 | |
Marek Vasut | f8995db | 2015-12-04 02:34:46 +0100 | [diff] [blame] | 166 | #include "dwc2_udc_otg_xfer_dma.c" |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * udc_disable - disable USB device controller |
| 170 | */ |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 171 | static void udc_disable(struct dwc2_udc *dev) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 172 | { |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 173 | debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 174 | |
| 175 | udc_set_address(dev, 0); |
| 176 | |
| 177 | dev->ep0state = WAIT_FOR_SETUP; |
| 178 | dev->gadget.speed = USB_SPEED_UNKNOWN; |
| 179 | dev->usb_address = 0; |
| 180 | |
| 181 | otg_phy_off(dev); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * udc_reinit - initialize software state |
| 186 | */ |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 187 | static void udc_reinit(struct dwc2_udc *dev) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 188 | { |
| 189 | unsigned int i; |
| 190 | |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 191 | debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 192 | |
| 193 | /* device/ep0 records init */ |
| 194 | INIT_LIST_HEAD(&dev->gadget.ep_list); |
| 195 | INIT_LIST_HEAD(&dev->gadget.ep0->ep_list); |
| 196 | dev->ep0state = WAIT_FOR_SETUP; |
| 197 | |
| 198 | /* basic endpoint records init */ |
Marek Vasut | cb9c5d0 | 2015-12-04 02:44:33 +0100 | [diff] [blame] | 199 | for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) { |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 200 | struct dwc2_ep *ep = &dev->ep[i]; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 201 | |
| 202 | if (i != 0) |
| 203 | list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list); |
| 204 | |
| 205 | ep->desc = 0; |
| 206 | ep->stopped = 0; |
| 207 | INIT_LIST_HEAD(&ep->queue); |
| 208 | ep->pio_irqs = 0; |
| 209 | } |
| 210 | |
| 211 | /* the rest was statically initialized, and is read-only */ |
| 212 | } |
| 213 | |
| 214 | #define BYTES2MAXP(x) (x / 8) |
| 215 | #define MAXP2BYTES(x) (x * 8) |
| 216 | |
| 217 | /* until it's enabled, this UDC should be completely invisible |
| 218 | * to any USB host. |
| 219 | */ |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 220 | static int udc_enable(struct dwc2_udc *dev) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 221 | { |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 222 | debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 223 | |
| 224 | otg_phy_init(dev); |
Marek Vasut | 8030088 | 2014-11-04 04:23:25 +0100 | [diff] [blame] | 225 | reconfig_usbd(dev); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 226 | |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 227 | debug_cond(DEBUG_SETUP != 0, |
Marek Vasut | dbdd58f | 2015-12-04 02:28:40 +0100 | [diff] [blame] | 228 | "DWC2 USB 2.0 OTG Controller Core Initialized : 0x%x\n", |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 229 | readl(®->gintmsk)); |
| 230 | |
| 231 | dev->gadget.speed = USB_SPEED_UNKNOWN; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 236 | #if !CONFIG_IS_ENABLED(DM_USB_GADGET) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 237 | /* |
| 238 | Register entry point for the peripheral controller driver. |
| 239 | */ |
| 240 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 241 | { |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 242 | struct dwc2_udc *dev = the_controller; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 243 | int retval = 0; |
Bo Shen | 60dc40c | 2014-08-27 17:28:18 +0800 | [diff] [blame] | 244 | unsigned long flags = 0; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 245 | |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 246 | debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name"); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 247 | |
| 248 | if (!driver |
| 249 | || (driver->speed != USB_SPEED_FULL |
| 250 | && driver->speed != USB_SPEED_HIGH) |
| 251 | || !driver->bind || !driver->disconnect || !driver->setup) |
| 252 | return -EINVAL; |
| 253 | if (!dev) |
| 254 | return -ENODEV; |
| 255 | if (dev->driver) |
| 256 | return -EBUSY; |
| 257 | |
| 258 | spin_lock_irqsave(&dev->lock, flags); |
| 259 | /* first hook up the driver ... */ |
| 260 | dev->driver = driver; |
| 261 | spin_unlock_irqrestore(&dev->lock, flags); |
| 262 | |
| 263 | if (retval) { /* TODO */ |
| 264 | printf("target device_add failed, error %d\n", retval); |
| 265 | return retval; |
| 266 | } |
| 267 | |
| 268 | retval = driver->bind(&dev->gadget); |
| 269 | if (retval) { |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 270 | debug_cond(DEBUG_SETUP != 0, |
| 271 | "%s: bind to driver --> error %d\n", |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 272 | dev->gadget.name, retval); |
| 273 | dev->driver = 0; |
| 274 | return retval; |
| 275 | } |
| 276 | |
| 277 | enable_irq(IRQ_OTG); |
| 278 | |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 279 | debug_cond(DEBUG_SETUP != 0, |
| 280 | "Registered gadget driver %s\n", dev->gadget.name); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 281 | udc_enable(dev); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Unregister entry point for the peripheral controller driver. |
| 288 | */ |
| 289 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 290 | { |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 291 | struct dwc2_udc *dev = the_controller; |
Bo Shen | 60dc40c | 2014-08-27 17:28:18 +0800 | [diff] [blame] | 292 | unsigned long flags = 0; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 293 | |
| 294 | if (!dev) |
| 295 | return -ENODEV; |
| 296 | if (!driver || driver != dev->driver) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | spin_lock_irqsave(&dev->lock, flags); |
| 300 | dev->driver = 0; |
| 301 | stop_activity(dev, driver); |
| 302 | spin_unlock_irqrestore(&dev->lock, flags); |
| 303 | |
| 304 | driver->unbind(&dev->gadget); |
| 305 | |
| 306 | disable_irq(IRQ_OTG); |
| 307 | |
| 308 | udc_disable(dev); |
| 309 | return 0; |
| 310 | } |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 311 | #else /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */ |
| 312 | |
| 313 | static int dwc2_gadget_start(struct usb_gadget *g, |
| 314 | struct usb_gadget_driver *driver) |
| 315 | { |
| 316 | struct dwc2_udc *dev = the_controller; |
| 317 | |
| 318 | debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name"); |
| 319 | |
| 320 | if (!driver || |
| 321 | (driver->speed != USB_SPEED_FULL && |
| 322 | driver->speed != USB_SPEED_HIGH) || |
| 323 | !driver->bind || !driver->disconnect || !driver->setup) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | if (!dev) |
| 327 | return -ENODEV; |
| 328 | |
| 329 | if (dev->driver) |
| 330 | return -EBUSY; |
| 331 | |
| 332 | /* first hook up the driver ... */ |
| 333 | dev->driver = driver; |
| 334 | |
| 335 | debug_cond(DEBUG_SETUP != 0, |
| 336 | "Registered gadget driver %s\n", dev->gadget.name); |
| 337 | return udc_enable(dev); |
| 338 | } |
| 339 | |
| 340 | static int dwc2_gadget_stop(struct usb_gadget *g) |
| 341 | { |
| 342 | struct dwc2_udc *dev = the_controller; |
| 343 | |
| 344 | if (!dev) |
| 345 | return -ENODEV; |
| 346 | |
| 347 | if (!dev->driver) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | dev->driver = 0; |
| 351 | stop_activity(dev, dev->driver); |
| 352 | |
| 353 | udc_disable(dev); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | #endif /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */ |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * done - retire a request; caller blocked irqs |
| 362 | */ |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 363 | static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 364 | { |
| 365 | unsigned int stopped = ep->stopped; |
| 366 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 367 | debug("%s: %s %p, req = %p, stopped = %d\n", |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 368 | __func__, ep->ep.name, ep, &req->req, stopped); |
| 369 | |
| 370 | list_del_init(&req->queue); |
| 371 | |
| 372 | if (likely(req->req.status == -EINPROGRESS)) |
| 373 | req->req.status = status; |
| 374 | else |
| 375 | status = req->req.status; |
| 376 | |
| 377 | if (status && status != -ESHUTDOWN) { |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 378 | debug("complete %s req %p stat %d len %u/%u\n", |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 379 | ep->ep.name, &req->req, status, |
| 380 | req->req.actual, req->req.length); |
| 381 | } |
| 382 | |
| 383 | /* don't modify queue heads during completion callback */ |
| 384 | ep->stopped = 1; |
| 385 | |
Lukasz Majewski | 0dfeb00 | 2012-05-02 13:11:35 +0200 | [diff] [blame] | 386 | #ifdef DEBUG |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 387 | printf("calling complete callback\n"); |
| 388 | { |
| 389 | int i, len = req->req.length; |
| 390 | |
| 391 | printf("pkt[%d] = ", req->req.length); |
| 392 | if (len > 64) |
| 393 | len = 64; |
| 394 | for (i = 0; i < len; i++) { |
| 395 | printf("%02x", ((u8 *)req->req.buf)[i]); |
| 396 | if ((i & 7) == 7) |
| 397 | printf(" "); |
| 398 | } |
| 399 | printf("\n"); |
| 400 | } |
| 401 | #endif |
| 402 | spin_unlock(&ep->dev->lock); |
| 403 | req->req.complete(&ep->ep, &req->req); |
| 404 | spin_lock(&ep->dev->lock); |
| 405 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 406 | debug("callback completed\n"); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 407 | |
| 408 | ep->stopped = stopped; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * nuke - dequeue ALL requests |
| 413 | */ |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 414 | static void nuke(struct dwc2_ep *ep, int status) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 415 | { |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 416 | struct dwc2_request *req; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 417 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 418 | debug("%s: %s %p\n", __func__, ep->ep.name, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 419 | |
| 420 | /* called with irqs blocked */ |
| 421 | while (!list_empty(&ep->queue)) { |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 422 | req = list_entry(ep->queue.next, struct dwc2_request, queue); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 423 | done(ep, req, status); |
| 424 | } |
| 425 | } |
| 426 | |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 427 | static void stop_activity(struct dwc2_udc *dev, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 428 | struct usb_gadget_driver *driver) |
| 429 | { |
| 430 | int i; |
| 431 | |
| 432 | /* don't disconnect drivers more than once */ |
| 433 | if (dev->gadget.speed == USB_SPEED_UNKNOWN) |
| 434 | driver = 0; |
| 435 | dev->gadget.speed = USB_SPEED_UNKNOWN; |
| 436 | |
| 437 | /* prevent new request submissions, kill any outstanding requests */ |
Marek Vasut | cb9c5d0 | 2015-12-04 02:44:33 +0100 | [diff] [blame] | 438 | for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) { |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 439 | struct dwc2_ep *ep = &dev->ep[i]; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 440 | ep->stopped = 1; |
| 441 | nuke(ep, -ESHUTDOWN); |
| 442 | } |
| 443 | |
| 444 | /* report disconnect; the driver is already quiesced */ |
| 445 | if (driver) { |
| 446 | spin_unlock(&dev->lock); |
| 447 | driver->disconnect(&dev->gadget); |
| 448 | spin_lock(&dev->lock); |
| 449 | } |
| 450 | |
| 451 | /* re-init driver-visible data structures */ |
| 452 | udc_reinit(dev); |
| 453 | } |
| 454 | |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 455 | static void reconfig_usbd(struct dwc2_udc *dev) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 456 | { |
| 457 | /* 2. Soft-reset OTG Core and then unreset again. */ |
| 458 | int i; |
| 459 | unsigned int uTemp = writel(CORE_SOFT_RESET, ®->grstctl); |
Marek Vasut | 8030088 | 2014-11-04 04:23:25 +0100 | [diff] [blame] | 460 | uint32_t dflt_gusbcfg; |
Xu Ziyuan | b729dc0 | 2016-07-14 14:52:33 +0800 | [diff] [blame] | 461 | uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz; |
Patrick Delaunay | 80a0de1 | 2019-03-29 15:42:19 +0100 | [diff] [blame] | 462 | u32 max_hw_ep; |
Patrice Chotard | 00f3fc6 | 2019-03-29 15:42:20 +0100 | [diff] [blame] | 463 | int pdata_hw_ep; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 464 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 465 | debug("Reseting OTG controller\n"); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 466 | |
Marek Vasut | 8030088 | 2014-11-04 04:23:25 +0100 | [diff] [blame] | 467 | dflt_gusbcfg = |
| 468 | 0<<15 /* PHY Low Power Clock sel*/ |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 469 | |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/ |
| 470 | |0x5<<10 /* Turnaround time*/ |
| 471 | |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/ |
| 472 | /* 1:SRP enable] H1= 1,1*/ |
| 473 | |0<<7 /* Ulpi DDR sel*/ |
| 474 | |0<<6 /* 0: high speed utmi+, 1: full speed serial*/ |
| 475 | |0<<4 /* 0: utmi+, 1:ulpi*/ |
Steve Rae | d7198f3 | 2016-06-07 15:35:21 -0700 | [diff] [blame] | 476 | #ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8 |
| 477 | |0<<3 /* phy i/f 0:8bit, 1:16bit*/ |
| 478 | #else |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 479 | |1<<3 /* phy i/f 0:8bit, 1:16bit*/ |
Steve Rae | d7198f3 | 2016-06-07 15:35:21 -0700 | [diff] [blame] | 480 | #endif |
Marek Vasut | 8030088 | 2014-11-04 04:23:25 +0100 | [diff] [blame] | 481 | |0x7<<0; /* HS/FS Timeout**/ |
| 482 | |
| 483 | if (dev->pdata->usb_gusbcfg) |
| 484 | dflt_gusbcfg = dev->pdata->usb_gusbcfg; |
| 485 | |
| 486 | writel(dflt_gusbcfg, ®->gusbcfg); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 487 | |
| 488 | /* 3. Put the OTG device core in the disconnected state.*/ |
| 489 | uTemp = readl(®->dctl); |
| 490 | uTemp |= SOFT_DISCONNECT; |
| 491 | writel(uTemp, ®->dctl); |
| 492 | |
| 493 | udelay(20); |
| 494 | |
| 495 | /* 4. Make the OTG device core exit from the disconnected state.*/ |
| 496 | uTemp = readl(®->dctl); |
| 497 | uTemp = uTemp & ~SOFT_DISCONNECT; |
| 498 | writel(uTemp, ®->dctl); |
| 499 | |
| 500 | /* 5. Configure OTG Core to initial settings of device mode.*/ |
| 501 | /* [][1: full speed(30Mhz) 0:high speed]*/ |
| 502 | writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, ®->dcfg); |
| 503 | |
| 504 | mdelay(1); |
| 505 | |
| 506 | /* 6. Unmask the core interrupts*/ |
| 507 | writel(GINTMSK_INIT, ®->gintmsk); |
| 508 | |
| 509 | /* 7. Set NAK bit of EP0, EP1, EP2*/ |
| 510 | writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[EP0_CON].doepctl); |
| 511 | writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[EP0_CON].diepctl); |
| 512 | |
Marek Vasut | cb9c5d0 | 2015-12-04 02:44:33 +0100 | [diff] [blame] | 513 | for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) { |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 514 | writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[i].doepctl); |
| 515 | writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[i].diepctl); |
| 516 | } |
| 517 | |
| 518 | /* 8. Unmask EPO interrupts*/ |
| 519 | writel(((1 << EP0_CON) << DAINT_OUT_BIT) |
| 520 | | (1 << EP0_CON), ®->daintmsk); |
| 521 | |
| 522 | /* 9. Unmask device OUT EP common interrupts*/ |
| 523 | writel(DOEPMSK_INIT, ®->doepmsk); |
| 524 | |
| 525 | /* 10. Unmask device IN EP common interrupts*/ |
| 526 | writel(DIEPMSK_INIT, ®->diepmsk); |
| 527 | |
Xu Ziyuan | b729dc0 | 2016-07-14 14:52:33 +0800 | [diff] [blame] | 528 | rx_fifo_sz = RX_FIFO_SIZE; |
| 529 | np_tx_fifo_sz = NPTX_FIFO_SIZE; |
| 530 | tx_fifo_sz = PTX_FIFO_SIZE; |
| 531 | |
| 532 | if (dev->pdata->rx_fifo_sz) |
| 533 | rx_fifo_sz = dev->pdata->rx_fifo_sz; |
| 534 | if (dev->pdata->np_tx_fifo_sz) |
| 535 | np_tx_fifo_sz = dev->pdata->np_tx_fifo_sz; |
| 536 | if (dev->pdata->tx_fifo_sz) |
| 537 | tx_fifo_sz = dev->pdata->tx_fifo_sz; |
| 538 | |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 539 | /* 11. Set Rx FIFO Size (in 32-bit words) */ |
Xu Ziyuan | b729dc0 | 2016-07-14 14:52:33 +0800 | [diff] [blame] | 540 | writel(rx_fifo_sz, ®->grxfsiz); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 541 | |
| 542 | /* 12. Set Non Periodic Tx FIFO Size */ |
Xu Ziyuan | b729dc0 | 2016-07-14 14:52:33 +0800 | [diff] [blame] | 543 | writel((np_tx_fifo_sz << 16) | rx_fifo_sz, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 544 | ®->gnptxfsiz); |
| 545 | |
Patrick Delaunay | 80a0de1 | 2019-03-29 15:42:19 +0100 | [diff] [blame] | 546 | /* retrieve the number of IN Endpoints (excluding ep0) */ |
| 547 | max_hw_ep = (readl(®->ghwcfg4) & GHWCFG4_NUM_IN_EPS_MASK) >> |
| 548 | GHWCFG4_NUM_IN_EPS_SHIFT; |
Patrice Chotard | 00f3fc6 | 2019-03-29 15:42:20 +0100 | [diff] [blame] | 549 | pdata_hw_ep = dev->pdata->tx_fifo_sz_nb; |
Patrick Delaunay | 80a0de1 | 2019-03-29 15:42:19 +0100 | [diff] [blame] | 550 | |
Patrice Chotard | 00f3fc6 | 2019-03-29 15:42:20 +0100 | [diff] [blame] | 551 | /* tx_fifo_sz_nb should equal to number of IN Endpoint */ |
| 552 | if (pdata_hw_ep && max_hw_ep != pdata_hw_ep) |
| 553 | pr_warn("Got %d hw endpoint but %d tx-fifo-size in array !!\n", |
| 554 | max_hw_ep, pdata_hw_ep); |
| 555 | |
| 556 | for (i = 0; i < max_hw_ep; i++) { |
| 557 | if (pdata_hw_ep) |
| 558 | tx_fifo_sz = dev->pdata->tx_fifo_sz_array[i]; |
| 559 | |
Patrick Delaunay | 80a0de1 | 2019-03-29 15:42:19 +0100 | [diff] [blame] | 560 | writel((rx_fifo_sz + np_tx_fifo_sz + (tx_fifo_sz * i)) | |
| 561 | tx_fifo_sz << 16, ®->dieptxf[i]); |
Patrice Chotard | 00f3fc6 | 2019-03-29 15:42:20 +0100 | [diff] [blame] | 562 | } |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 563 | /* Flush the RX FIFO */ |
| 564 | writel(RX_FIFO_FLUSH, ®->grstctl); |
| 565 | while (readl(®->grstctl) & RX_FIFO_FLUSH) |
Marek Vasut | cb9c5d0 | 2015-12-04 02:44:33 +0100 | [diff] [blame] | 566 | debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 567 | |
| 568 | /* Flush all the Tx FIFO's */ |
| 569 | writel(TX_FIFO_FLUSH_ALL, ®->grstctl); |
| 570 | writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, ®->grstctl); |
| 571 | while (readl(®->grstctl) & TX_FIFO_FLUSH) |
Marek Vasut | cb9c5d0 | 2015-12-04 02:44:33 +0100 | [diff] [blame] | 572 | debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 573 | |
| 574 | /* 13. Clear NAK bit of EP0, EP1, EP2*/ |
| 575 | /* For Slave mode*/ |
| 576 | /* EP0: Control OUT */ |
| 577 | writel(DEPCTL_EPDIS | DEPCTL_CNAK, |
| 578 | ®->out_endp[EP0_CON].doepctl); |
| 579 | |
| 580 | /* 14. Initialize OTG Link Core.*/ |
| 581 | writel(GAHBCFG_INIT, ®->gahbcfg); |
| 582 | } |
| 583 | |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 584 | static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 585 | { |
| 586 | unsigned int ep_ctrl; |
| 587 | int i; |
| 588 | |
| 589 | if (speed == USB_SPEED_HIGH) { |
| 590 | ep0_fifo_size = 64; |
| 591 | ep_fifo_size = 512; |
| 592 | ep_fifo_size2 = 1024; |
| 593 | dev->gadget.speed = USB_SPEED_HIGH; |
| 594 | } else { |
| 595 | ep0_fifo_size = 64; |
| 596 | ep_fifo_size = 64; |
| 597 | ep_fifo_size2 = 64; |
| 598 | dev->gadget.speed = USB_SPEED_FULL; |
| 599 | } |
| 600 | |
| 601 | dev->ep[0].ep.maxpacket = ep0_fifo_size; |
Marek Vasut | cb9c5d0 | 2015-12-04 02:44:33 +0100 | [diff] [blame] | 602 | for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 603 | dev->ep[i].ep.maxpacket = ep_fifo_size; |
| 604 | |
| 605 | /* EP0 - Control IN (64 bytes)*/ |
| 606 | ep_ctrl = readl(®->in_endp[EP0_CON].diepctl); |
| 607 | writel(ep_ctrl|(0<<0), ®->in_endp[EP0_CON].diepctl); |
| 608 | |
| 609 | /* EP0 - Control OUT (64 bytes)*/ |
| 610 | ep_ctrl = readl(®->out_endp[EP0_CON].doepctl); |
| 611 | writel(ep_ctrl|(0<<0), ®->out_endp[EP0_CON].doepctl); |
| 612 | } |
| 613 | |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 614 | static int dwc2_ep_enable(struct usb_ep *_ep, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 615 | const struct usb_endpoint_descriptor *desc) |
| 616 | { |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 617 | struct dwc2_ep *ep; |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 618 | struct dwc2_udc *dev; |
Bo Shen | 60dc40c | 2014-08-27 17:28:18 +0800 | [diff] [blame] | 619 | unsigned long flags = 0; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 620 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 621 | debug("%s: %p\n", __func__, _ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 622 | |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 623 | ep = container_of(_ep, struct dwc2_ep, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 624 | if (!_ep || !desc || ep->desc || _ep->name == ep0name |
| 625 | || desc->bDescriptorType != USB_DT_ENDPOINT |
| 626 | || ep->bEndpointAddress != desc->bEndpointAddress |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 627 | || ep_maxpacket(ep) < |
| 628 | le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) { |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 629 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 630 | debug("%s: bad ep or descriptor\n", __func__); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 631 | return -EINVAL; |
| 632 | } |
| 633 | |
| 634 | /* xfer types must match, except that interrupt ~= bulk */ |
| 635 | if (ep->bmAttributes != desc->bmAttributes |
| 636 | && ep->bmAttributes != USB_ENDPOINT_XFER_BULK |
| 637 | && desc->bmAttributes != USB_ENDPOINT_XFER_INT) { |
| 638 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 639 | debug("%s: %s type mismatch\n", __func__, _ep->name); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | /* hardware _could_ do smaller, but driver doesn't */ |
Frank Wang | 0d64b5b | 2016-01-27 12:39:40 -0800 | [diff] [blame] | 644 | if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK && |
| 645 | le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) > |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 646 | ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) { |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 647 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 648 | debug("%s: bad %s maxpacket\n", __func__, _ep->name); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 649 | return -ERANGE; |
| 650 | } |
| 651 | |
| 652 | dev = ep->dev; |
| 653 | if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) { |
| 654 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 655 | debug("%s: bogus device state\n", __func__); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 656 | return -ESHUTDOWN; |
| 657 | } |
| 658 | |
| 659 | ep->stopped = 0; |
| 660 | ep->desc = desc; |
| 661 | ep->pio_irqs = 0; |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 662 | ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 663 | |
| 664 | /* Reset halt state */ |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 665 | dwc2_udc_set_nak(ep); |
| 666 | dwc2_udc_set_halt(_ep, 0); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 667 | |
| 668 | spin_lock_irqsave(&ep->dev->lock, flags); |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 669 | dwc2_udc_ep_activate(ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 670 | spin_unlock_irqrestore(&ep->dev->lock, flags); |
| 671 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 672 | debug("%s: enabled %s, stopped = %d, maxpacket = %d\n", |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 673 | __func__, _ep->name, ep->stopped, ep->ep.maxpacket); |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Disable EP |
| 679 | */ |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 680 | static int dwc2_ep_disable(struct usb_ep *_ep) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 681 | { |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 682 | struct dwc2_ep *ep; |
Bo Shen | 60dc40c | 2014-08-27 17:28:18 +0800 | [diff] [blame] | 683 | unsigned long flags = 0; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 684 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 685 | debug("%s: %p\n", __func__, _ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 686 | |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 687 | ep = container_of(_ep, struct dwc2_ep, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 688 | if (!_ep || !ep->desc) { |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 689 | debug("%s: %s not enabled\n", __func__, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 690 | _ep ? ep->ep.name : NULL); |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | |
| 694 | spin_lock_irqsave(&ep->dev->lock, flags); |
| 695 | |
| 696 | /* Nuke all pending requests */ |
| 697 | nuke(ep, -ESHUTDOWN); |
| 698 | |
| 699 | ep->desc = 0; |
| 700 | ep->stopped = 1; |
| 701 | |
| 702 | spin_unlock_irqrestore(&ep->dev->lock, flags); |
| 703 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 704 | debug("%s: disabled %s\n", __func__, _ep->name); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 708 | static struct usb_request *dwc2_alloc_request(struct usb_ep *ep, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 709 | gfp_t gfp_flags) |
| 710 | { |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 711 | struct dwc2_request *req; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 712 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 713 | debug("%s: %s %p\n", __func__, ep->name, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 714 | |
Mike Frysinger | 1f19e3f | 2012-04-26 02:34:44 +0000 | [diff] [blame] | 715 | req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req)); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 716 | if (!req) |
| 717 | return 0; |
| 718 | |
| 719 | memset(req, 0, sizeof *req); |
| 720 | INIT_LIST_HEAD(&req->queue); |
| 721 | |
| 722 | return &req->req; |
| 723 | } |
| 724 | |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 725 | static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 726 | { |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 727 | struct dwc2_request *req; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 728 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 729 | debug("%s: %p\n", __func__, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 730 | |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 731 | req = container_of(_req, struct dwc2_request, req); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 732 | WARN_ON(!list_empty(&req->queue)); |
| 733 | kfree(req); |
| 734 | } |
| 735 | |
| 736 | /* dequeue JUST ONE request */ |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 737 | static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 738 | { |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 739 | struct dwc2_ep *ep; |
Marek Vasut | 32f931c | 2015-12-04 01:51:07 +0100 | [diff] [blame] | 740 | struct dwc2_request *req; |
Bo Shen | 60dc40c | 2014-08-27 17:28:18 +0800 | [diff] [blame] | 741 | unsigned long flags = 0; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 742 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 743 | debug("%s: %p\n", __func__, _ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 744 | |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 745 | ep = container_of(_ep, struct dwc2_ep, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 746 | if (!_ep || ep->ep.name == ep0name) |
| 747 | return -EINVAL; |
| 748 | |
| 749 | spin_lock_irqsave(&ep->dev->lock, flags); |
| 750 | |
| 751 | /* make sure it's actually queued on this endpoint */ |
| 752 | list_for_each_entry(req, &ep->queue, queue) { |
| 753 | if (&req->req == _req) |
| 754 | break; |
| 755 | } |
| 756 | if (&req->req != _req) { |
| 757 | spin_unlock_irqrestore(&ep->dev->lock, flags); |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | |
| 761 | done(ep, req, -ECONNRESET); |
| 762 | |
| 763 | spin_unlock_irqrestore(&ep->dev->lock, flags); |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Return bytes in EP FIFO |
| 769 | */ |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 770 | static int dwc2_fifo_status(struct usb_ep *_ep) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 771 | { |
| 772 | int count = 0; |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 773 | struct dwc2_ep *ep; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 774 | |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 775 | ep = container_of(_ep, struct dwc2_ep, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 776 | if (!_ep) { |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 777 | debug("%s: bad ep\n", __func__); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 778 | return -ENODEV; |
| 779 | } |
| 780 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 781 | debug("%s: %d\n", __func__, ep_index(ep)); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 782 | |
| 783 | /* LPD can't report unclaimed bytes from IN fifos */ |
| 784 | if (ep_is_in(ep)) |
| 785 | return -EOPNOTSUPP; |
| 786 | |
| 787 | return count; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Flush EP FIFO |
| 792 | */ |
Marek Vasut | 153150f | 2015-12-04 02:17:40 +0100 | [diff] [blame] | 793 | static void dwc2_fifo_flush(struct usb_ep *_ep) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 794 | { |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 795 | struct dwc2_ep *ep; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 796 | |
Marek Vasut | 5b309f7 | 2015-12-04 01:48:57 +0100 | [diff] [blame] | 797 | ep = container_of(_ep, struct dwc2_ep, ep); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 798 | if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) { |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 799 | debug("%s: bad ep\n", __func__); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 803 | debug("%s: %d\n", __func__, ep_index(ep)); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 806 | static const struct usb_gadget_ops dwc2_udc_ops = { |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 807 | /* current versions must always be self-powered */ |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 808 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
| 809 | .udc_start = dwc2_gadget_start, |
| 810 | .udc_stop = dwc2_gadget_stop, |
| 811 | #endif |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 812 | }; |
| 813 | |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 814 | static struct dwc2_udc memory = { |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 815 | .usb_address = 0, |
| 816 | .gadget = { |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 817 | .ops = &dwc2_udc_ops, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 818 | .ep0 = &memory.ep[0].ep, |
| 819 | .name = driver_name, |
| 820 | }, |
| 821 | |
| 822 | /* control endpoint */ |
| 823 | .ep[0] = { |
| 824 | .ep = { |
| 825 | .name = ep0name, |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 826 | .ops = &dwc2_ep_ops, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 827 | .maxpacket = EP0_FIFO_SIZE, |
| 828 | }, |
| 829 | .dev = &memory, |
| 830 | |
| 831 | .bEndpointAddress = 0, |
| 832 | .bmAttributes = 0, |
| 833 | |
| 834 | .ep_type = ep_control, |
| 835 | }, |
| 836 | |
| 837 | /* first group of endpoints */ |
| 838 | .ep[1] = { |
| 839 | .ep = { |
| 840 | .name = "ep1in-bulk", |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 841 | .ops = &dwc2_ep_ops, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 842 | .maxpacket = EP_FIFO_SIZE, |
| 843 | }, |
| 844 | .dev = &memory, |
| 845 | |
| 846 | .bEndpointAddress = USB_DIR_IN | 1, |
| 847 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 848 | |
| 849 | .ep_type = ep_bulk_out, |
| 850 | .fifo_num = 1, |
| 851 | }, |
| 852 | |
| 853 | .ep[2] = { |
| 854 | .ep = { |
| 855 | .name = "ep2out-bulk", |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 856 | .ops = &dwc2_ep_ops, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 857 | .maxpacket = EP_FIFO_SIZE, |
| 858 | }, |
| 859 | .dev = &memory, |
| 860 | |
| 861 | .bEndpointAddress = USB_DIR_OUT | 2, |
| 862 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 863 | |
| 864 | .ep_type = ep_bulk_in, |
| 865 | .fifo_num = 2, |
| 866 | }, |
| 867 | |
| 868 | .ep[3] = { |
| 869 | .ep = { |
| 870 | .name = "ep3in-int", |
Marek Vasut | 4e7c29e | 2015-12-04 02:13:42 +0100 | [diff] [blame] | 871 | .ops = &dwc2_ep_ops, |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 872 | .maxpacket = EP_FIFO_SIZE, |
| 873 | }, |
| 874 | .dev = &memory, |
| 875 | |
| 876 | .bEndpointAddress = USB_DIR_IN | 3, |
| 877 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 878 | |
| 879 | .ep_type = ep_interrupt, |
| 880 | .fifo_num = 3, |
| 881 | }, |
| 882 | }; |
| 883 | |
| 884 | /* |
| 885 | * probe - binds to the platform device |
| 886 | */ |
| 887 | |
Marek Vasut | 01b61fa | 2015-12-04 02:26:33 +0100 | [diff] [blame] | 888 | int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 889 | { |
Marek Vasut | 1a639ff | 2015-12-04 00:57:58 +0100 | [diff] [blame] | 890 | struct dwc2_udc *dev = &memory; |
Lukasz Majewski | 904f8f7 | 2014-02-05 10:10:44 +0100 | [diff] [blame] | 891 | int retval = 0; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 892 | |
Anatolij Gustschin | e63ce4f | 2011-12-19 04:20:35 +0000 | [diff] [blame] | 893 | debug("%s: %p\n", __func__, pdata); |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 894 | |
| 895 | dev->pdata = pdata; |
| 896 | |
Marek Vasut | 67ec9dc | 2015-12-04 01:11:45 +0100 | [diff] [blame] | 897 | reg = (struct dwc2_usbotg_reg *)pdata->regs_otg; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 898 | |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 899 | dev->gadget.is_dualspeed = 1; /* Hack only*/ |
| 900 | dev->gadget.is_otg = 0; |
| 901 | dev->gadget.is_a_peripheral = 0; |
| 902 | dev->gadget.b_hnp_enable = 0; |
| 903 | dev->gadget.a_hnp_support = 0; |
| 904 | dev->gadget.a_alt_hnp_support = 0; |
| 905 | |
| 906 | the_controller = dev; |
| 907 | |
Lukasz Majewski | 904f8f7 | 2014-02-05 10:10:44 +0100 | [diff] [blame] | 908 | usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE, |
| 909 | ROUND(sizeof(struct usb_ctrlrequest), |
| 910 | CONFIG_SYS_CACHELINE_SIZE)); |
| 911 | if (!usb_ctrl) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 912 | pr_err("No memory available for UDC!\n"); |
Lukasz Majewski | 904f8f7 | 2014-02-05 10:10:44 +0100 | [diff] [blame] | 913 | return -ENOMEM; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 914 | } |
Lukasz Majewski | 904f8f7 | 2014-02-05 10:10:44 +0100 | [diff] [blame] | 915 | |
| 916 | usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 917 | |
| 918 | udc_reinit(dev); |
| 919 | |
| 920 | return retval; |
| 921 | } |
| 922 | |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 923 | int dwc2_udc_handle_interrupt(void) |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 924 | { |
| 925 | u32 intr_status = readl(®->gintsts); |
| 926 | u32 gintmsk = readl(®->gintmsk); |
| 927 | |
| 928 | if (intr_status & gintmsk) |
Marek Vasut | fa03c81 | 2015-12-04 02:03:45 +0100 | [diff] [blame] | 929 | return dwc2_udc_irq(1, (void *)the_controller); |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | #if !CONFIG_IS_ENABLED(DM_USB_GADGET) |
| 935 | |
| 936 | int usb_gadget_handle_interrupts(int index) |
| 937 | { |
| 938 | return dwc2_udc_handle_interrupt(); |
| 939 | } |
| 940 | |
| 941 | #else /* CONFIG_IS_ENABLED(DM_USB_GADGET) */ |
| 942 | |
| 943 | struct dwc2_priv_data { |
| 944 | struct clk_bulk clks; |
| 945 | struct reset_ctl_bulk resets; |
| 946 | struct phy *phys; |
| 947 | int num_phys; |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 948 | struct udevice *usb33d_supply; |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 949 | }; |
| 950 | |
| 951 | int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
| 952 | { |
| 953 | return dwc2_udc_handle_interrupt(); |
| 954 | } |
| 955 | |
| 956 | int dwc2_phy_setup(struct udevice *dev, struct phy **array, int *num_phys) |
| 957 | { |
| 958 | int i, ret, count; |
| 959 | struct phy *usb_phys; |
| 960 | |
| 961 | /* Return if no phy declared */ |
| 962 | if (!dev_read_prop(dev, "phys", NULL)) |
| 963 | return 0; |
| 964 | |
| 965 | count = dev_count_phandle_with_args(dev, "phys", "#phy-cells"); |
| 966 | if (count <= 0) |
| 967 | return count; |
| 968 | |
| 969 | usb_phys = devm_kcalloc(dev, count, sizeof(struct phy), |
| 970 | GFP_KERNEL); |
| 971 | if (!usb_phys) |
| 972 | return -ENOMEM; |
| 973 | |
| 974 | for (i = 0; i < count; i++) { |
| 975 | ret = generic_phy_get_by_index(dev, i, &usb_phys[i]); |
| 976 | if (ret && ret != -ENOENT) { |
| 977 | dev_err(dev, "Failed to get USB PHY%d for %s\n", |
| 978 | i, dev->name); |
| 979 | return ret; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | for (i = 0; i < count; i++) { |
| 984 | ret = generic_phy_init(&usb_phys[i]); |
| 985 | if (ret) { |
| 986 | dev_err(dev, "Can't init USB PHY%d for %s\n", |
| 987 | i, dev->name); |
| 988 | goto phys_init_err; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | for (i = 0; i < count; i++) { |
| 993 | ret = generic_phy_power_on(&usb_phys[i]); |
| 994 | if (ret) { |
| 995 | dev_err(dev, "Can't power USB PHY%d for %s\n", |
| 996 | i, dev->name); |
| 997 | goto phys_poweron_err; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | *array = usb_phys; |
| 1002 | *num_phys = count; |
| 1003 | |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 1004 | return 0; |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1005 | |
| 1006 | phys_poweron_err: |
| 1007 | for (i = count - 1; i >= 0; i--) |
| 1008 | generic_phy_power_off(&usb_phys[i]); |
| 1009 | |
| 1010 | for (i = 0; i < count; i++) |
| 1011 | generic_phy_exit(&usb_phys[i]); |
| 1012 | |
| 1013 | return ret; |
| 1014 | |
| 1015 | phys_init_err: |
| 1016 | for (; i >= 0; i--) |
| 1017 | generic_phy_exit(&usb_phys[i]); |
| 1018 | |
| 1019 | return ret; |
Lukasz Majewski | ac3c831 | 2011-10-27 10:36:46 +0200 | [diff] [blame] | 1020 | } |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1021 | |
| 1022 | void dwc2_phy_shutdown(struct udevice *dev, struct phy *usb_phys, int num_phys) |
| 1023 | { |
| 1024 | int i, ret; |
| 1025 | |
| 1026 | for (i = 0; i < num_phys; i++) { |
| 1027 | if (!generic_phy_valid(&usb_phys[i])) |
| 1028 | continue; |
| 1029 | |
| 1030 | ret = generic_phy_power_off(&usb_phys[i]); |
| 1031 | ret |= generic_phy_exit(&usb_phys[i]); |
| 1032 | if (ret) { |
| 1033 | dev_err(dev, "Can't shutdown USB PHY%d for %s\n", |
| 1034 | i, dev->name); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | static int dwc2_udc_otg_ofdata_to_platdata(struct udevice *dev) |
| 1040 | { |
| 1041 | struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev); |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 1042 | ulong drvdata; |
| 1043 | void (*set_params)(struct dwc2_plat_otg_data *data); |
Patrick Delaunay | ed74263 | 2019-06-18 16:57:16 +0200 | [diff] [blame] | 1044 | int ret; |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1045 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame^] | 1046 | if (usb_get_dr_mode(dev->node) != USB_DR_MODE_PERIPHERAL && |
| 1047 | usb_get_dr_mode(dev->node) != USB_DR_MODE_OTG) { |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1048 | dev_dbg(dev, "Invalid mode\n"); |
| 1049 | return -ENODEV; |
| 1050 | } |
| 1051 | |
| 1052 | platdata->regs_otg = dev_read_addr(dev); |
| 1053 | |
| 1054 | platdata->rx_fifo_sz = dev_read_u32_default(dev, "g-rx-fifo-size", 0); |
| 1055 | platdata->np_tx_fifo_sz = dev_read_u32_default(dev, |
| 1056 | "g-np-tx-fifo-size", 0); |
Patrick Delaunay | ed74263 | 2019-06-18 16:57:16 +0200 | [diff] [blame] | 1057 | |
| 1058 | platdata->tx_fifo_sz_nb = |
| 1059 | dev_read_size(dev, "g-tx-fifo-size") / sizeof(u32); |
| 1060 | if (platdata->tx_fifo_sz_nb > DWC2_MAX_HW_ENDPOINTS) |
| 1061 | platdata->tx_fifo_sz_nb = DWC2_MAX_HW_ENDPOINTS; |
| 1062 | if (platdata->tx_fifo_sz_nb) { |
| 1063 | ret = dev_read_u32_array(dev, "g-tx-fifo-size", |
| 1064 | platdata->tx_fifo_sz_array, |
| 1065 | platdata->tx_fifo_sz_nb); |
| 1066 | if (ret) |
| 1067 | return ret; |
| 1068 | } |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1069 | |
Patrick Delaunay | 0c42d1d | 2019-03-29 15:42:17 +0100 | [diff] [blame] | 1070 | platdata->force_b_session_valid = |
Patrick Delaunay | 7f3384d | 2019-03-29 15:42:24 +0100 | [diff] [blame] | 1071 | dev_read_bool(dev, "u-boot,force-b-session-valid"); |
Patrick Delaunay | 0c42d1d | 2019-03-29 15:42:17 +0100 | [diff] [blame] | 1072 | |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 1073 | /* force platdata according compatible */ |
| 1074 | drvdata = dev_get_driver_data(dev); |
| 1075 | if (drvdata) { |
| 1076 | set_params = (void *)drvdata; |
| 1077 | set_params(platdata); |
| 1078 | } |
| 1079 | |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 1083 | static void dwc2_set_stm32mp1_hsotg_params(struct dwc2_plat_otg_data *p) |
| 1084 | { |
| 1085 | p->activate_stm_id_vb_detection = true; |
| 1086 | p->usb_gusbcfg = |
| 1087 | 0 << 15 /* PHY Low Power Clock sel*/ |
| 1088 | | 0x9 << 10 /* USB Turnaround time (0x9 for HS phy) */ |
| 1089 | | 0 << 9 /* [0:HNP disable,1:HNP enable]*/ |
| 1090 | | 0 << 8 /* [0:SRP disable 1:SRP enable]*/ |
| 1091 | | 0 << 6 /* 0: high speed utmi+, 1: full speed serial*/ |
| 1092 | | 0x7 << 0; /* FS timeout calibration**/ |
Patrick Delaunay | 7f3384d | 2019-03-29 15:42:24 +0100 | [diff] [blame] | 1093 | |
| 1094 | if (p->force_b_session_valid) |
| 1095 | p->usb_gusbcfg |= 1 << 30; /* FDMOD: Force device mode */ |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1098 | static int dwc2_udc_otg_reset_init(struct udevice *dev, |
| 1099 | struct reset_ctl_bulk *resets) |
| 1100 | { |
| 1101 | int ret; |
| 1102 | |
| 1103 | ret = reset_get_bulk(dev, resets); |
| 1104 | if (ret == -ENOTSUPP) |
| 1105 | return 0; |
| 1106 | |
| 1107 | if (ret) |
| 1108 | return ret; |
| 1109 | |
Patrick Delaunay | 9e4abe3 | 2019-03-29 15:42:16 +0100 | [diff] [blame] | 1110 | ret = reset_assert_bulk(resets); |
| 1111 | |
| 1112 | if (!ret) { |
| 1113 | udelay(2); |
| 1114 | ret = reset_deassert_bulk(resets); |
| 1115 | } |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1116 | if (ret) { |
| 1117 | reset_release_bulk(resets); |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | static int dwc2_udc_otg_clk_init(struct udevice *dev, |
| 1125 | struct clk_bulk *clks) |
| 1126 | { |
| 1127 | int ret; |
| 1128 | |
| 1129 | ret = clk_get_bulk(dev, clks); |
| 1130 | if (ret == -ENOSYS) |
| 1131 | return 0; |
| 1132 | |
| 1133 | if (ret) |
| 1134 | return ret; |
| 1135 | |
| 1136 | ret = clk_enable_bulk(clks); |
| 1137 | if (ret) { |
| 1138 | clk_release_bulk(clks); |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static int dwc2_udc_otg_probe(struct udevice *dev) |
| 1146 | { |
| 1147 | struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev); |
| 1148 | struct dwc2_priv_data *priv = dev_get_priv(dev); |
Patrick Delaunay | 0c42d1d | 2019-03-29 15:42:17 +0100 | [diff] [blame] | 1149 | struct dwc2_usbotg_reg *usbotg_reg = |
| 1150 | (struct dwc2_usbotg_reg *)platdata->regs_otg; |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1151 | int ret; |
| 1152 | |
| 1153 | ret = dwc2_udc_otg_clk_init(dev, &priv->clks); |
| 1154 | if (ret) |
| 1155 | return ret; |
| 1156 | |
| 1157 | ret = dwc2_udc_otg_reset_init(dev, &priv->resets); |
| 1158 | if (ret) |
| 1159 | return ret; |
| 1160 | |
| 1161 | ret = dwc2_phy_setup(dev, &priv->phys, &priv->num_phys); |
| 1162 | if (ret) |
| 1163 | return ret; |
| 1164 | |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 1165 | if (CONFIG_IS_ENABLED(DM_REGULATOR) && |
| 1166 | platdata->activate_stm_id_vb_detection && |
| 1167 | !platdata->force_b_session_valid) { |
| 1168 | ret = device_get_supply_regulator(dev, "usb33d-supply", |
| 1169 | &priv->usb33d_supply); |
| 1170 | if (ret) { |
| 1171 | dev_err(dev, "can't get voltage level detector supply\n"); |
| 1172 | return ret; |
| 1173 | } |
| 1174 | ret = regulator_set_enable(priv->usb33d_supply, true); |
| 1175 | if (ret) { |
| 1176 | dev_err(dev, "can't enable voltage level detector supply\n"); |
| 1177 | return ret; |
| 1178 | } |
| 1179 | /* Enable vbus sensing */ |
| 1180 | setbits_le32(&usbotg_reg->ggpio, |
| 1181 | GGPIO_STM32_OTG_GCCFG_VBDEN | |
| 1182 | GGPIO_STM32_OTG_GCCFG_IDEN); |
| 1183 | } |
| 1184 | |
Patrick Delaunay | 0c42d1d | 2019-03-29 15:42:17 +0100 | [diff] [blame] | 1185 | if (platdata->force_b_session_valid) |
| 1186 | /* Override B session bits : value and enable */ |
Patrick Delaunay | 7f3384d | 2019-03-29 15:42:24 +0100 | [diff] [blame] | 1187 | setbits_le32(&usbotg_reg->gotgctl, |
| 1188 | A_VALOEN | A_VALOVAL | B_VALOEN | B_VALOVAL); |
Patrick Delaunay | 0c42d1d | 2019-03-29 15:42:17 +0100 | [diff] [blame] | 1189 | |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1190 | ret = dwc2_udc_probe(platdata); |
| 1191 | if (ret) |
| 1192 | return ret; |
| 1193 | |
| 1194 | the_controller->driver = 0; |
| 1195 | |
| 1196 | ret = usb_add_gadget_udc((struct device *)dev, &the_controller->gadget); |
| 1197 | |
| 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | static int dwc2_udc_otg_remove(struct udevice *dev) |
| 1202 | { |
| 1203 | struct dwc2_priv_data *priv = dev_get_priv(dev); |
| 1204 | |
| 1205 | usb_del_gadget_udc(&the_controller->gadget); |
| 1206 | |
| 1207 | reset_release_bulk(&priv->resets); |
| 1208 | |
| 1209 | clk_release_bulk(&priv->clks); |
| 1210 | |
| 1211 | dwc2_phy_shutdown(dev, priv->phys, priv->num_phys); |
| 1212 | |
| 1213 | return dm_scan_fdt_dev(dev); |
| 1214 | } |
| 1215 | |
| 1216 | static const struct udevice_id dwc2_udc_otg_ids[] = { |
| 1217 | { .compatible = "snps,dwc2" }, |
Marek Szyprowski | 2a7a7d9 | 2019-12-02 12:11:17 +0100 | [diff] [blame] | 1218 | { .compatible = "brcm,bcm2835-usb" }, |
Patrick Delaunay | e014cb6 | 2019-03-29 15:42:21 +0100 | [diff] [blame] | 1219 | { .compatible = "st,stm32mp1-hsotg", |
| 1220 | .data = (ulong)dwc2_set_stm32mp1_hsotg_params }, |
| 1221 | {}, |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1222 | }; |
| 1223 | |
| 1224 | U_BOOT_DRIVER(dwc2_udc_otg) = { |
| 1225 | .name = "dwc2-udc-otg", |
| 1226 | .id = UCLASS_USB_GADGET_GENERIC, |
| 1227 | .of_match = dwc2_udc_otg_ids, |
| 1228 | .ofdata_to_platdata = dwc2_udc_otg_ofdata_to_platdata, |
| 1229 | .probe = dwc2_udc_otg_probe, |
| 1230 | .remove = dwc2_udc_otg_remove, |
| 1231 | .platdata_auto_alloc_size = sizeof(struct dwc2_plat_otg_data), |
| 1232 | .priv_auto_alloc_size = sizeof(struct dwc2_priv_data), |
| 1233 | }; |
Patrick Delaunay | e2ad779 | 2019-03-29 15:42:18 +0100 | [diff] [blame] | 1234 | |
| 1235 | int dwc2_udc_B_session_valid(struct udevice *dev) |
| 1236 | { |
| 1237 | struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev); |
| 1238 | struct dwc2_usbotg_reg *usbotg_reg = |
| 1239 | (struct dwc2_usbotg_reg *)platdata->regs_otg; |
| 1240 | |
| 1241 | return readl(&usbotg_reg->gotgctl) & B_SESSION_VALID; |
| 1242 | } |
Patrick Delaunay | 7515054 | 2019-03-29 15:42:15 +0100 | [diff] [blame] | 1243 | #endif /* CONFIG_IS_ENABLED(DM_USB_GADGET) */ |