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