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