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