Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: cdnsp: cdns3 Add main part of Cadence USBSSP DRD Driver

This patch introduces the main part of Cadence USBSSP DRD driver
to Linux kernel.
To reduce the patch size a little bit, the header file gadget.h was
intentionally added as separate patch.

The Cadence USBSSP DRD Controller is a highly configurable IP Core which
can be instantiated as Dual-Role Device (DRD), Peripheral Only and
Host Only (XHCI)configurations.

The current driver has been validated with FPGA platform. We have
support for PCIe bus, which is used on FPGA prototyping.

The host side of USBSS DRD controller is compliant with XHCI.
The architecture for device side is almost the same as for host side,
and most of the XHCI specification can be used to understand how
this controller operates.

Signed-off-by: Pawel Laszczak <pawell@cadence.com>
Signed-off-by: Peter Chen <peter.chen@nxp.com>

authored by

Pawel Laszczak and committed by
Peter Chen
3d829045 e93e58d2

+6643 -31
+2
drivers/usb/Makefile
··· 13 13 obj-$(CONFIG_USB_DWC2) += dwc2/ 14 14 obj-$(CONFIG_USB_ISP1760) += isp1760/ 15 15 16 + obj-$(CONFIG_USB_CDNS_SUPPORT) += cdns3/ 16 17 obj-$(CONFIG_USB_CDNS3) += cdns3/ 18 + obj-$(CONFIG_USB_CDNSP_PCI) += cdns3/ 17 19 18 20 obj-$(CONFIG_USB_MON) += mon/ 19 21 obj-$(CONFIG_USB_MTU3) += mtu3/
+58 -10
drivers/usb/cdns3/Kconfig
··· 1 - config CDNS_USB_COMMON 2 - tristate 3 - 4 - config CDNS_USB_HOST 5 - bool 6 - 7 - config USB_CDNS3 8 - tristate "Cadence USB3 Dual-Role Controller" 1 + config USB_CDNS_SUPPORT 2 + tristate "Cadence USB Support" 9 3 depends on USB_SUPPORT && (USB || USB_GADGET) && HAS_DMA 10 4 select USB_XHCI_PLATFORM if USB_XHCI_HCD 11 5 select USB_ROLE_SWITCH 12 - select CDNS_USB_COMMON 6 + help 7 + Say Y here if your system has a Cadence USBSS or USBSSP 8 + dual-role controller. 9 + It supports: dual-role switch, Host-only, and Peripheral-only. 10 + 11 + config USB_CDNS_HOST 12 + bool 13 + 14 + if USB_CDNS_SUPPORT 15 + 16 + config USB_CDNS3 17 + tristate "Cadence USB3 Dual-Role Controller" 18 + depends on USB_CDNS_SUPPORT 13 19 help 14 20 Say Y here if your system has a Cadence USB3 dual-role controller. 15 21 It supports: dual-role switch, Host-only, and Peripheral-only. 16 22 17 23 If you choose to build this driver is a dynamically linked 18 24 as module, the module will be called cdns3.ko. 25 + endif 19 26 20 27 if USB_CDNS3 21 28 ··· 39 32 config USB_CDNS3_HOST 40 33 bool "Cadence USB3 host controller" 41 34 depends on USB=y || USB=USB_CDNS3 42 - select CDNS_USB_HOST 35 + select USB_CDNS_HOST 43 36 help 44 37 Say Y here to enable host controller functionality of the 45 38 Cadence driver. ··· 77 70 platforms that contain Cadence USB3 controller core. 78 71 79 72 For example, imx8qm and imx8qxp. 73 + 74 + endif 75 + 76 + if USB_CDNS_SUPPORT 77 + 78 + config USB_CDNSP_PCI 79 + tristate "Cadence CDNSP Dual-Role Controller" 80 + depends on USB_CDNS_SUPPORT && USB_PCI && ACPI 81 + help 82 + Say Y here if your system has a Cadence CDNSP dual-role controller. 83 + It supports: dual-role switch Host-only, and Peripheral-only. 84 + 85 + If you choose to build this driver is a dynamically linked 86 + module, the module will be called cdnsp.ko. 87 + endif 88 + 89 + if USB_CDNSP_PCI 90 + 91 + config USB_CDNSP_GADGET 92 + bool "Cadence CDNSP device controller" 93 + depends on USB_GADGET=y || USB_GADGET=USB_CDNSP_PCI 94 + help 95 + Say Y here to enable device controller functionality of the 96 + Cadence CDNSP-DEV driver. 97 + 98 + Cadence CDNSP Device Controller in device mode is 99 + very similar to XHCI controller. Therefore some algorithms 100 + used has been taken from host driver. 101 + This controller supports FF, HS, SS and SSP mode. 102 + It doesn't support LS. 103 + 104 + config USB_CDNSP_HOST 105 + bool "Cadence CDNSP host controller" 106 + depends on USB=y || USB=USB_CDNSP_PCI 107 + select USB_CDNS_HOST 108 + help 109 + Say Y here to enable host controller functionality of the 110 + Cadence driver. 111 + 112 + Host controller is compliant with XHCI so it uses 113 + standard XHCI driver. 80 114 81 115 endif
+16 -11
drivers/usb/cdns3/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 # define_trace.h needs to know how to find our header 3 - CFLAGS_trace.o := -I$(src) 3 + CFLAGS_trace.o := -I$(src) 4 4 5 - cdns-usb-common-y := core.o drd.o 6 - cdns3-y := cdns3-plat.o 5 + cdns-usb-common-y := core.o drd.o 6 + cdns3-y := cdns3-plat.o 7 7 8 - obj-$(CONFIG_USB_CDNS3) += cdns3.o 9 - obj-$(CONFIG_CDNS_USB_COMMON) += cdns-usb-common.o 8 + obj-$(CONFIG_USB_CDNS3) += cdns3.o 9 + obj-$(CONFIG_USB_CDNS_SUPPORT) += cdns-usb-common.o 10 10 11 - cdns-usb-common-$(CONFIG_CDNS_USB_HOST) += host.o 12 - cdns3-$(CONFIG_USB_CDNS3_GADGET) += gadget.o ep0.o 11 + cdns-usb-common-$(CONFIG_USB_CDNS_HOST) += host.o 12 + cdns3-$(CONFIG_USB_CDNS3_GADGET) += gadget.o ep0.o 13 13 14 14 ifneq ($(CONFIG_USB_CDNS3_GADGET),) 15 - cdns3-$(CONFIG_TRACING) += trace.o 15 + cdns3-$(CONFIG_TRACING) += trace.o 16 16 endif 17 17 18 - obj-$(CONFIG_USB_CDNS3_PCI_WRAP) += cdns3-pci-wrap.o 19 - obj-$(CONFIG_USB_CDNS3_TI) += cdns3-ti.o 20 - obj-$(CONFIG_USB_CDNS3_IMX) += cdns3-imx.o 18 + obj-$(CONFIG_USB_CDNS3_PCI_WRAP) += cdns3-pci-wrap.o 19 + obj-$(CONFIG_USB_CDNS3_TI) += cdns3-ti.o 20 + obj-$(CONFIG_USB_CDNS3_IMX) += cdns3-imx.o 21 + 22 + cdnsp-udc-pci-y := cdnsp-pci.o 23 + obj-$(CONFIG_USB_CDNSP_PCI) += cdnsp-udc-pci.o 24 + cdnsp-udc-pci-$(CONFIG_USB_CDNSP_GADGET) += cdnsp-ring.o cdnsp-gadget.o \ 25 + cdnsp-mem.o cdnsp-ep0.o
+477
drivers/usb/cdns3/cdnsp-ep0.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Cadence CDNSP DRD Driver. 4 + * 5 + * Copyright (C) 2020 Cadence. 6 + * 7 + * Author: Pawel Laszczak <pawell@cadence.com> 8 + * 9 + */ 10 + 11 + #include <linux/usb/composite.h> 12 + #include <linux/usb/gadget.h> 13 + #include <linux/list.h> 14 + 15 + #include "cdnsp-gadget.h" 16 + 17 + static void cdnsp_ep0_stall(struct cdnsp_device *pdev) 18 + { 19 + struct cdnsp_request *preq; 20 + struct cdnsp_ep *pep; 21 + 22 + pep = &pdev->eps[0]; 23 + preq = next_request(&pep->pending_list); 24 + 25 + if (pdev->three_stage_setup) { 26 + cdnsp_halt_endpoint(pdev, pep, true); 27 + 28 + if (preq) 29 + cdnsp_gadget_giveback(pep, preq, -ECONNRESET); 30 + } else { 31 + pep->ep_state |= EP0_HALTED_STATUS; 32 + 33 + if (preq) 34 + list_del(&preq->list); 35 + 36 + cdnsp_status_stage(pdev); 37 + } 38 + } 39 + 40 + static int cdnsp_ep0_delegate_req(struct cdnsp_device *pdev, 41 + struct usb_ctrlrequest *ctrl) 42 + { 43 + int ret; 44 + 45 + spin_unlock(&pdev->lock); 46 + ret = pdev->gadget_driver->setup(&pdev->gadget, ctrl); 47 + spin_lock(&pdev->lock); 48 + 49 + return ret; 50 + } 51 + 52 + static int cdnsp_ep0_set_config(struct cdnsp_device *pdev, 53 + struct usb_ctrlrequest *ctrl) 54 + { 55 + enum usb_device_state state = pdev->gadget.state; 56 + u32 cfg; 57 + int ret; 58 + 59 + cfg = le16_to_cpu(ctrl->wValue); 60 + 61 + switch (state) { 62 + case USB_STATE_ADDRESS: 63 + break; 64 + case USB_STATE_CONFIGURED: 65 + break; 66 + default: 67 + dev_err(pdev->dev, "Set Configuration - bad device state\n"); 68 + return -EINVAL; 69 + } 70 + 71 + ret = cdnsp_ep0_delegate_req(pdev, ctrl); 72 + if (ret) 73 + return ret; 74 + 75 + if (!cfg) 76 + usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS); 77 + 78 + return 0; 79 + } 80 + 81 + static int cdnsp_ep0_set_address(struct cdnsp_device *pdev, 82 + struct usb_ctrlrequest *ctrl) 83 + { 84 + enum usb_device_state state = pdev->gadget.state; 85 + struct cdnsp_slot_ctx *slot_ctx; 86 + unsigned int slot_state; 87 + int ret; 88 + u32 addr; 89 + 90 + addr = le16_to_cpu(ctrl->wValue); 91 + 92 + if (addr > 127) { 93 + dev_err(pdev->dev, "Invalid device address %d\n", addr); 94 + return -EINVAL; 95 + } 96 + 97 + slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx); 98 + 99 + if (state == USB_STATE_CONFIGURED) { 100 + dev_err(pdev->dev, "Can't Set Address from Configured State\n"); 101 + return -EINVAL; 102 + } 103 + 104 + pdev->device_address = le16_to_cpu(ctrl->wValue); 105 + 106 + slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx); 107 + slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)); 108 + if (slot_state == SLOT_STATE_ADDRESSED) 109 + cdnsp_reset_device(pdev); 110 + 111 + /*set device address*/ 112 + ret = cdnsp_setup_device(pdev, SETUP_CONTEXT_ADDRESS); 113 + if (ret) 114 + return ret; 115 + 116 + if (addr) 117 + usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS); 118 + else 119 + usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT); 120 + 121 + return 0; 122 + } 123 + 124 + int cdnsp_status_stage(struct cdnsp_device *pdev) 125 + { 126 + pdev->ep0_stage = CDNSP_STATUS_STAGE; 127 + pdev->ep0_preq.request.length = 0; 128 + 129 + return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq); 130 + } 131 + 132 + static int cdnsp_w_index_to_ep_index(__le32 wIndex) 133 + { 134 + wIndex = le32_to_cpu(wIndex); 135 + 136 + if (!(wIndex & USB_ENDPOINT_NUMBER_MASK)) 137 + return 0; 138 + 139 + return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) + 140 + (wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1; 141 + } 142 + 143 + static int cdnsp_ep0_handle_status(struct cdnsp_device *pdev, 144 + struct usb_ctrlrequest *ctrl) 145 + { 146 + struct cdnsp_ep *pep; 147 + __le16 *response; 148 + int ep_sts = 0; 149 + u16 status = 0; 150 + u32 recipient; 151 + 152 + recipient = ctrl->bRequestType & USB_RECIP_MASK; 153 + 154 + switch (recipient) { 155 + case USB_RECIP_DEVICE: 156 + status = pdev->gadget.is_selfpowered; 157 + status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP; 158 + 159 + if (pdev->gadget.speed >= USB_SPEED_SUPER) { 160 + status |= pdev->u1_allowed << USB_DEV_STAT_U1_ENABLED; 161 + status |= pdev->u2_allowed << USB_DEV_STAT_U2_ENABLED; 162 + } 163 + break; 164 + case USB_RECIP_INTERFACE: 165 + /* 166 + * Function Remote Wake Capable D0 167 + * Function Remote Wakeup D1 168 + */ 169 + return cdnsp_ep0_delegate_req(pdev, ctrl); 170 + case USB_RECIP_ENDPOINT: 171 + pep = &pdev->eps[cdnsp_w_index_to_ep_index(ctrl->wIndex)]; 172 + ep_sts = GET_EP_CTX_STATE(pep->out_ctx); 173 + 174 + /* check if endpoint is stalled */ 175 + if (ep_sts == EP_STATE_HALTED) 176 + status = BIT(USB_ENDPOINT_HALT); 177 + break; 178 + default: 179 + return -EINVAL; 180 + } 181 + 182 + response = (__le16 *)pdev->setup_buf; 183 + *response = cpu_to_le16(status); 184 + 185 + pdev->ep0_preq.request.length = sizeof(*response); 186 + pdev->ep0_preq.request.buf = pdev->setup_buf; 187 + 188 + return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq); 189 + } 190 + 191 + static void cdnsp_enter_test_mode(struct cdnsp_device *pdev) 192 + { 193 + u32 temp; 194 + 195 + temp = readl(&pdev->active_port->regs->portpmsc) & ~GENMASK(31, 28); 196 + temp |= PORT_TEST_MODE(pdev->test_mode); 197 + writel(temp, &pdev->active_port->regs->portpmsc); 198 + } 199 + 200 + static int cdnsp_ep0_handle_feature_device(struct cdnsp_device *pdev, 201 + struct usb_ctrlrequest *ctrl, 202 + int set) 203 + { 204 + enum usb_device_state state; 205 + enum usb_device_speed speed; 206 + u16 tmode; 207 + 208 + state = pdev->gadget.state; 209 + speed = pdev->gadget.speed; 210 + 211 + switch (le16_to_cpu(ctrl->wValue)) { 212 + case USB_DEVICE_REMOTE_WAKEUP: 213 + pdev->may_wakeup = !!set; 214 + break; 215 + case USB_DEVICE_U1_ENABLE: 216 + if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER) 217 + return -EINVAL; 218 + 219 + pdev->u1_allowed = !!set; 220 + break; 221 + case USB_DEVICE_U2_ENABLE: 222 + if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER) 223 + return -EINVAL; 224 + 225 + pdev->u2_allowed = !!set; 226 + break; 227 + case USB_DEVICE_LTM_ENABLE: 228 + return -EINVAL; 229 + case USB_DEVICE_TEST_MODE: 230 + if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH) 231 + return -EINVAL; 232 + 233 + tmode = le16_to_cpu(ctrl->wIndex); 234 + 235 + if (!set || (tmode & 0xff) != 0) 236 + return -EINVAL; 237 + 238 + tmode = tmode >> 8; 239 + 240 + if (tmode > USB_TEST_FORCE_ENABLE || tmode < USB_TEST_J) 241 + return -EINVAL; 242 + 243 + pdev->test_mode = tmode; 244 + 245 + /* 246 + * Test mode must be set before Status Stage but controller 247 + * will start testing sequence after Status Stage. 248 + */ 249 + cdnsp_enter_test_mode(pdev); 250 + break; 251 + default: 252 + return -EINVAL; 253 + } 254 + 255 + return 0; 256 + } 257 + 258 + static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device *pdev, 259 + struct usb_ctrlrequest *ctrl, 260 + int set) 261 + { 262 + u16 wValue, wIndex; 263 + int ret; 264 + 265 + wValue = le16_to_cpu(ctrl->wValue); 266 + wIndex = le16_to_cpu(ctrl->wIndex); 267 + 268 + switch (wValue) { 269 + case USB_INTRF_FUNC_SUSPEND: 270 + ret = cdnsp_ep0_delegate_req(pdev, ctrl); 271 + if (ret) 272 + return ret; 273 + 274 + /* 275 + * Remote wakeup is enabled when any function within a device 276 + * is enabled for function remote wakeup. 277 + */ 278 + if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) 279 + pdev->may_wakeup++; 280 + else 281 + if (pdev->may_wakeup > 0) 282 + pdev->may_wakeup--; 283 + 284 + return 0; 285 + default: 286 + return -EINVAL; 287 + } 288 + 289 + return 0; 290 + } 291 + 292 + static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device *pdev, 293 + struct usb_ctrlrequest *ctrl, 294 + int set) 295 + { 296 + struct cdnsp_ep *pep; 297 + u32 wValue; 298 + 299 + wValue = le16_to_cpu(ctrl->wValue); 300 + pep = &pdev->eps[cdnsp_w_index_to_ep_index(ctrl->wIndex)]; 301 + 302 + switch (wValue) { 303 + case USB_ENDPOINT_HALT: 304 + if (!set && (pep->ep_state & EP_WEDGE)) { 305 + /* Resets Sequence Number */ 306 + cdnsp_halt_endpoint(pdev, pep, 0); 307 + cdnsp_halt_endpoint(pdev, pep, 1); 308 + break; 309 + } 310 + 311 + return cdnsp_halt_endpoint(pdev, pep, set); 312 + default: 313 + dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue); 314 + return -EINVAL; 315 + } 316 + 317 + return 0; 318 + } 319 + 320 + static int cdnsp_ep0_handle_feature(struct cdnsp_device *pdev, 321 + struct usb_ctrlrequest *ctrl, 322 + int set) 323 + { 324 + switch (ctrl->bRequestType & USB_RECIP_MASK) { 325 + case USB_RECIP_DEVICE: 326 + return cdnsp_ep0_handle_feature_device(pdev, ctrl, set); 327 + case USB_RECIP_INTERFACE: 328 + return cdnsp_ep0_handle_feature_intf(pdev, ctrl, set); 329 + case USB_RECIP_ENDPOINT: 330 + return cdnsp_ep0_handle_feature_endpoint(pdev, ctrl, set); 331 + default: 332 + return -EINVAL; 333 + } 334 + } 335 + 336 + static int cdnsp_ep0_set_sel(struct cdnsp_device *pdev, 337 + struct usb_ctrlrequest *ctrl) 338 + { 339 + enum usb_device_state state = pdev->gadget.state; 340 + u16 wLength; 341 + 342 + if (state == USB_STATE_DEFAULT) 343 + return -EINVAL; 344 + 345 + wLength = le16_to_cpu(ctrl->wLength); 346 + 347 + if (wLength != 6) { 348 + dev_err(pdev->dev, "Set SEL should be 6 bytes, got %d\n", 349 + wLength); 350 + return -EINVAL; 351 + } 352 + 353 + /* 354 + * To handle Set SEL we need to receive 6 bytes from Host. So let's 355 + * queue a usb_request for 6 bytes. 356 + */ 357 + pdev->ep0_preq.request.length = 6; 358 + pdev->ep0_preq.request.buf = pdev->setup_buf; 359 + 360 + return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq); 361 + } 362 + 363 + static int cdnsp_ep0_set_isoch_delay(struct cdnsp_device *pdev, 364 + struct usb_ctrlrequest *ctrl) 365 + { 366 + if (le16_to_cpu(ctrl->wIndex) || le16_to_cpu(ctrl->wLength)) 367 + return -EINVAL; 368 + 369 + pdev->gadget.isoch_delay = le16_to_cpu(ctrl->wValue); 370 + 371 + return 0; 372 + } 373 + 374 + static int cdnsp_ep0_std_request(struct cdnsp_device *pdev, 375 + struct usb_ctrlrequest *ctrl) 376 + { 377 + int ret; 378 + 379 + switch (ctrl->bRequest) { 380 + case USB_REQ_GET_STATUS: 381 + ret = cdnsp_ep0_handle_status(pdev, ctrl); 382 + break; 383 + case USB_REQ_CLEAR_FEATURE: 384 + ret = cdnsp_ep0_handle_feature(pdev, ctrl, 0); 385 + break; 386 + case USB_REQ_SET_FEATURE: 387 + ret = cdnsp_ep0_handle_feature(pdev, ctrl, 1); 388 + break; 389 + case USB_REQ_SET_ADDRESS: 390 + ret = cdnsp_ep0_set_address(pdev, ctrl); 391 + break; 392 + case USB_REQ_SET_CONFIGURATION: 393 + ret = cdnsp_ep0_set_config(pdev, ctrl); 394 + break; 395 + case USB_REQ_SET_SEL: 396 + ret = cdnsp_ep0_set_sel(pdev, ctrl); 397 + break; 398 + case USB_REQ_SET_ISOCH_DELAY: 399 + ret = cdnsp_ep0_set_isoch_delay(pdev, ctrl); 400 + break; 401 + case USB_REQ_SET_INTERFACE: 402 + /* 403 + * Add request into pending list to block sending status stage 404 + * by libcomposite. 405 + */ 406 + list_add_tail(&pdev->ep0_preq.list, 407 + &pdev->ep0_preq.pep->pending_list); 408 + 409 + ret = cdnsp_ep0_delegate_req(pdev, ctrl); 410 + if (ret == -EBUSY) 411 + ret = 0; 412 + 413 + list_del(&pdev->ep0_preq.list); 414 + break; 415 + default: 416 + ret = cdnsp_ep0_delegate_req(pdev, ctrl); 417 + break; 418 + } 419 + 420 + return ret; 421 + } 422 + 423 + void cdnsp_setup_analyze(struct cdnsp_device *pdev) 424 + { 425 + struct usb_ctrlrequest *ctrl = &pdev->setup; 426 + int ret = 0; 427 + __le16 len; 428 + 429 + if (!pdev->gadget_driver) 430 + goto out; 431 + 432 + if (pdev->gadget.state == USB_STATE_NOTATTACHED) { 433 + dev_err(pdev->dev, "ERR: Setup detected in unattached state\n"); 434 + ret = -EINVAL; 435 + goto out; 436 + } 437 + 438 + /* Restore the ep0 to Stopped/Running state. */ 439 + if (pdev->eps[0].ep_state & EP_HALTED) 440 + cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0); 441 + 442 + /* 443 + * Finishing previous SETUP transfer by removing request from 444 + * list and informing upper layer 445 + */ 446 + if (!list_empty(&pdev->eps[0].pending_list)) { 447 + struct cdnsp_request *req; 448 + 449 + req = next_request(&pdev->eps[0].pending_list); 450 + cdnsp_ep_dequeue(&pdev->eps[0], req); 451 + } 452 + 453 + len = le16_to_cpu(ctrl->wLength); 454 + if (!len) { 455 + pdev->three_stage_setup = false; 456 + pdev->ep0_expect_in = false; 457 + } else { 458 + pdev->three_stage_setup = true; 459 + pdev->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); 460 + } 461 + 462 + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) 463 + ret = cdnsp_ep0_std_request(pdev, ctrl); 464 + else 465 + ret = cdnsp_ep0_delegate_req(pdev, ctrl); 466 + 467 + if (!len) 468 + pdev->ep0_stage = CDNSP_STATUS_STAGE; 469 + 470 + if (ret == USB_GADGET_DELAYED_STATUS) 471 + return; 472 + out: 473 + if (ret < 0) 474 + cdnsp_ep0_stall(pdev); 475 + else if (pdev->ep0_stage == CDNSP_STATUS_STAGE) 476 + cdnsp_status_stage(pdev); 477 + }
+1954
drivers/usb/cdns3/cdnsp-gadget.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Cadence CDNSP DRD Driver. 4 + * 5 + * Copyright (C) 2020 Cadence. 6 + * 7 + * Author: Pawel Laszczak <pawell@cadence.com> 8 + * 9 + */ 10 + 11 + #include <linux/moduleparam.h> 12 + #include <linux/dma-mapping.h> 13 + #include <linux/module.h> 14 + #include <linux/iopoll.h> 15 + #include <linux/delay.h> 16 + #include <linux/log2.h> 17 + #include <linux/slab.h> 18 + #include <linux/pci.h> 19 + #include <linux/irq.h> 20 + #include <linux/dmi.h> 21 + 22 + #include "core.h" 23 + #include "gadget-export.h" 24 + #include "drd.h" 25 + #include "cdnsp-gadget.h" 26 + 27 + unsigned int cdnsp_port_speed(unsigned int port_status) 28 + { 29 + /*Detect gadget speed based on PORTSC register*/ 30 + if (DEV_SUPERSPEEDPLUS(port_status)) 31 + return USB_SPEED_SUPER_PLUS; 32 + else if (DEV_SUPERSPEED(port_status)) 33 + return USB_SPEED_SUPER; 34 + else if (DEV_HIGHSPEED(port_status)) 35 + return USB_SPEED_HIGH; 36 + else if (DEV_FULLSPEED(port_status)) 37 + return USB_SPEED_FULL; 38 + 39 + /* If device is detached then speed will be USB_SPEED_UNKNOWN.*/ 40 + return USB_SPEED_UNKNOWN; 41 + } 42 + 43 + /* 44 + * Given a port state, this function returns a value that would result in the 45 + * port being in the same state, if the value was written to the port status 46 + * control register. 47 + * Save Read Only (RO) bits and save read/write bits where 48 + * writing a 0 clears the bit and writing a 1 sets the bit (RWS). 49 + * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect. 50 + */ 51 + u32 cdnsp_port_state_to_neutral(u32 state) 52 + { 53 + /* Save read-only status and port state. */ 54 + return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS); 55 + } 56 + 57 + /** 58 + * Find the offset of the extended capabilities with capability ID id. 59 + * @base: PCI MMIO registers base address. 60 + * @start: Address at which to start looking, (0 or HCC_PARAMS to start at 61 + * beginning of list) 62 + * @id: Extended capability ID to search for. 63 + * 64 + * Returns the offset of the next matching extended capability structure. 65 + * Some capabilities can occur several times, 66 + * e.g., the EXT_CAPS_PROTOCOL, and this provides a way to find them all. 67 + */ 68 + int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id) 69 + { 70 + u32 offset = start; 71 + u32 next; 72 + u32 val; 73 + 74 + if (!start || start == HCC_PARAMS_OFFSET) { 75 + val = readl(base + HCC_PARAMS_OFFSET); 76 + if (val == ~0) 77 + return 0; 78 + 79 + offset = HCC_EXT_CAPS(val) << 2; 80 + if (!offset) 81 + return 0; 82 + }; 83 + 84 + do { 85 + val = readl(base + offset); 86 + if (val == ~0) 87 + return 0; 88 + 89 + if (EXT_CAPS_ID(val) == id && offset != start) 90 + return offset; 91 + 92 + next = EXT_CAPS_NEXT(val); 93 + offset += next << 2; 94 + } while (next); 95 + 96 + return 0; 97 + } 98 + 99 + void cdnsp_set_link_state(struct cdnsp_device *pdev, 100 + __le32 __iomem *port_regs, 101 + u32 link_state) 102 + { 103 + u32 temp; 104 + 105 + temp = readl(port_regs); 106 + temp = cdnsp_port_state_to_neutral(temp); 107 + temp |= PORT_WKCONN_E | PORT_WKDISC_E; 108 + writel(temp, port_regs); 109 + 110 + temp &= ~PORT_PLS_MASK; 111 + temp |= PORT_LINK_STROBE | link_state; 112 + 113 + writel(temp, port_regs); 114 + } 115 + 116 + static void cdnsp_disable_port(struct cdnsp_device *pdev, 117 + __le32 __iomem *port_regs) 118 + { 119 + u32 temp = cdnsp_port_state_to_neutral(readl(port_regs)); 120 + 121 + writel(temp | PORT_PED, port_regs); 122 + } 123 + 124 + static void cdnsp_clear_port_change_bit(struct cdnsp_device *pdev, 125 + __le32 __iomem *port_regs) 126 + { 127 + u32 portsc = readl(port_regs); 128 + 129 + writel(cdnsp_port_state_to_neutral(portsc) | 130 + (portsc & PORT_CHANGE_BITS), port_regs); 131 + } 132 + 133 + static void cdnsp_set_chicken_bits_2(struct cdnsp_device *pdev, u32 bit) 134 + { 135 + __le32 __iomem *reg; 136 + void __iomem *base; 137 + u32 offset = 0; 138 + 139 + base = &pdev->cap_regs->hc_capbase; 140 + offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP); 141 + reg = base + offset + REG_CHICKEN_BITS_2_OFFSET; 142 + 143 + bit = readl(reg) | bit; 144 + writel(bit, reg); 145 + } 146 + 147 + static void cdnsp_clear_chicken_bits_2(struct cdnsp_device *pdev, u32 bit) 148 + { 149 + __le32 __iomem *reg; 150 + void __iomem *base; 151 + u32 offset = 0; 152 + 153 + base = &pdev->cap_regs->hc_capbase; 154 + offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP); 155 + reg = base + offset + REG_CHICKEN_BITS_2_OFFSET; 156 + 157 + bit = readl(reg) & ~bit; 158 + writel(bit, reg); 159 + } 160 + 161 + /* 162 + * Disable interrupts and begin the controller halting process. 163 + */ 164 + static void cdnsp_quiesce(struct cdnsp_device *pdev) 165 + { 166 + u32 halted; 167 + u32 mask; 168 + u32 cmd; 169 + 170 + mask = ~(u32)(CDNSP_IRQS); 171 + 172 + halted = readl(&pdev->op_regs->status) & STS_HALT; 173 + if (!halted) 174 + mask &= ~(CMD_R_S | CMD_DEVEN); 175 + 176 + cmd = readl(&pdev->op_regs->command); 177 + cmd &= mask; 178 + writel(cmd, &pdev->op_regs->command); 179 + } 180 + 181 + /* 182 + * Force controller into halt state. 183 + * 184 + * Disable any IRQs and clear the run/stop bit. 185 + * Controller will complete any current and actively pipelined transactions, and 186 + * should halt within 16 ms of the run/stop bit being cleared. 187 + * Read controller Halted bit in the status register to see when the 188 + * controller is finished. 189 + */ 190 + int cdnsp_halt(struct cdnsp_device *pdev) 191 + { 192 + int ret; 193 + u32 val; 194 + 195 + cdnsp_quiesce(pdev); 196 + 197 + ret = readl_poll_timeout_atomic(&pdev->op_regs->status, val, 198 + val & STS_HALT, 1, 199 + CDNSP_MAX_HALT_USEC); 200 + if (ret) { 201 + dev_err(pdev->dev, "ERROR: Device halt failed\n"); 202 + return ret; 203 + } 204 + 205 + pdev->cdnsp_state |= CDNSP_STATE_HALTED; 206 + 207 + return 0; 208 + } 209 + 210 + /* 211 + * device controller died, register read returns 0xffffffff, or command never 212 + * ends. 213 + */ 214 + void cdnsp_died(struct cdnsp_device *pdev) 215 + { 216 + dev_err(pdev->dev, "ERROR: CDNSP controller not responding\n"); 217 + pdev->cdnsp_state |= CDNSP_STATE_DYING; 218 + cdnsp_halt(pdev); 219 + } 220 + 221 + /* 222 + * Set the run bit and wait for the device to be running. 223 + */ 224 + static int cdnsp_start(struct cdnsp_device *pdev) 225 + { 226 + u32 temp; 227 + int ret; 228 + 229 + temp = readl(&pdev->op_regs->command); 230 + temp |= (CMD_R_S | CMD_DEVEN); 231 + writel(temp, &pdev->op_regs->command); 232 + 233 + pdev->cdnsp_state = 0; 234 + 235 + /* 236 + * Wait for the STS_HALT Status bit to be 0 to indicate the device is 237 + * running. 238 + */ 239 + ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp, 240 + !(temp & STS_HALT), 1, 241 + CDNSP_MAX_HALT_USEC); 242 + if (ret) { 243 + pdev->cdnsp_state = CDNSP_STATE_DYING; 244 + dev_err(pdev->dev, "ERROR: Controller run failed\n"); 245 + } 246 + 247 + return ret; 248 + } 249 + 250 + /* 251 + * Reset a halted controller. 252 + * 253 + * This resets pipelines, timers, counters, state machines, etc. 254 + * Transactions will be terminated immediately, and operational registers 255 + * will be set to their defaults. 256 + */ 257 + int cdnsp_reset(struct cdnsp_device *pdev) 258 + { 259 + u32 command; 260 + u32 temp; 261 + int ret; 262 + 263 + temp = readl(&pdev->op_regs->status); 264 + 265 + if (temp == ~(u32)0) { 266 + dev_err(pdev->dev, "Device not accessible, reset failed.\n"); 267 + return -ENODEV; 268 + } 269 + 270 + if ((temp & STS_HALT) == 0) { 271 + dev_err(pdev->dev, "Controller not halted, aborting reset.\n"); 272 + return -EINVAL; 273 + } 274 + 275 + command = readl(&pdev->op_regs->command); 276 + command |= CMD_RESET; 277 + writel(command, &pdev->op_regs->command); 278 + 279 + ret = readl_poll_timeout_atomic(&pdev->op_regs->command, temp, 280 + !(temp & CMD_RESET), 1, 281 + 10 * 1000); 282 + if (ret) { 283 + dev_err(pdev->dev, "ERROR: Controller reset failed\n"); 284 + return ret; 285 + } 286 + 287 + /* 288 + * CDNSP cannot write any doorbells or operational registers other 289 + * than status until the "Controller Not Ready" flag is cleared. 290 + */ 291 + ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp, 292 + !(temp & STS_CNR), 1, 293 + 10 * 1000); 294 + 295 + if (ret) { 296 + dev_err(pdev->dev, "ERROR: Controller not ready to work\n"); 297 + return ret; 298 + } 299 + 300 + dev_dbg(pdev->dev, "Controller ready to work"); 301 + 302 + return ret; 303 + } 304 + 305 + /* 306 + * cdnsp_get_endpoint_index - Find the index for an endpoint given its 307 + * descriptor.Use the return value to right shift 1 for the bitmask. 308 + * 309 + * Index = (epnum * 2) + direction - 1, 310 + * where direction = 0 for OUT, 1 for IN. 311 + * For control endpoints, the IN index is used (OUT index is unused), so 312 + * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) 313 + */ 314 + static unsigned int 315 + cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor *desc) 316 + { 317 + unsigned int index = (unsigned int)usb_endpoint_num(desc); 318 + 319 + if (usb_endpoint_xfer_control(desc)) 320 + return index * 2; 321 + 322 + return (index * 2) + (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; 323 + } 324 + 325 + /* 326 + * Find the flag for this endpoint (for use in the control context). Use the 327 + * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is 328 + * bit 1, etc. 329 + */ 330 + static unsigned int 331 + cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor *desc) 332 + { 333 + return 1 << (cdnsp_get_endpoint_index(desc) + 1); 334 + } 335 + 336 + int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq) 337 + { 338 + struct cdnsp_device *pdev = pep->pdev; 339 + struct usb_request *request; 340 + int ret; 341 + 342 + if (preq->epnum == 0 && !list_empty(&pep->pending_list)) 343 + return -EBUSY; 344 + 345 + request = &preq->request; 346 + request->actual = 0; 347 + request->status = -EINPROGRESS; 348 + preq->direction = pep->direction; 349 + preq->epnum = pep->number; 350 + preq->td.drbl = 0; 351 + 352 + ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->direction); 353 + if (ret) 354 + return ret; 355 + 356 + list_add_tail(&preq->list, &pep->pending_list); 357 + 358 + switch (usb_endpoint_type(pep->endpoint.desc)) { 359 + case USB_ENDPOINT_XFER_CONTROL: 360 + ret = cdnsp_queue_ctrl_tx(pdev, preq); 361 + break; 362 + case USB_ENDPOINT_XFER_BULK: 363 + case USB_ENDPOINT_XFER_INT: 364 + ret = cdnsp_queue_bulk_tx(pdev, preq); 365 + break; 366 + case USB_ENDPOINT_XFER_ISOC: 367 + ret = cdnsp_queue_isoc_tx_prepare(pdev, preq); 368 + } 369 + 370 + if (ret) 371 + goto unmap; 372 + 373 + return 0; 374 + 375 + unmap: 376 + usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request, 377 + pep->direction); 378 + list_del(&preq->list); 379 + 380 + return ret; 381 + } 382 + 383 + /* 384 + * Remove the request's TD from the endpoint ring. This may cause the 385 + * controller to stop USB transfers, potentially stopping in the middle of a 386 + * TRB buffer. The controller should pick up where it left off in the TD, 387 + * unless a Set Transfer Ring Dequeue Pointer is issued. 388 + * 389 + * The TRBs that make up the buffers for the canceled request will be "removed" 390 + * from the ring. Since the ring is a contiguous structure, they can't be 391 + * physically removed. Instead, there are two options: 392 + * 393 + * 1) If the controller is in the middle of processing the request to be 394 + * canceled, we simply move the ring's dequeue pointer past those TRBs 395 + * using the Set Transfer Ring Dequeue Pointer command. This will be 396 + * the common case, when drivers timeout on the last submitted request 397 + * and attempt to cancel. 398 + * 399 + * 2) If the controller is in the middle of a different TD, we turn the TRBs 400 + * into a series of 1-TRB transfer no-op TDs. No-ops shouldn't be chained. 401 + * The controller will need to invalidate the any TRBs it has cached after 402 + * the stop endpoint command. 403 + * 404 + * 3) The TD may have completed by the time the Stop Endpoint Command 405 + * completes, so software needs to handle that case too. 406 + * 407 + */ 408 + int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq) 409 + { 410 + struct cdnsp_device *pdev = pep->pdev; 411 + int ret; 412 + 413 + if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_RUNNING) { 414 + ret = cdnsp_cmd_stop_ep(pdev, pep); 415 + if (ret) 416 + return ret; 417 + } 418 + 419 + return cdnsp_remove_request(pdev, preq, pep); 420 + } 421 + 422 + static void cdnsp_zero_in_ctx(struct cdnsp_device *pdev) 423 + { 424 + struct cdnsp_input_control_ctx *ctrl_ctx; 425 + struct cdnsp_slot_ctx *slot_ctx; 426 + struct cdnsp_ep_ctx *ep_ctx; 427 + int i; 428 + 429 + ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx); 430 + 431 + /* 432 + * When a device's add flag and drop flag are zero, any subsequent 433 + * configure endpoint command will leave that endpoint's state 434 + * untouched. Make sure we don't leave any old state in the input 435 + * endpoint contexts. 436 + */ 437 + ctrl_ctx->drop_flags = 0; 438 + ctrl_ctx->add_flags = 0; 439 + slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx); 440 + slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); 441 + 442 + /* Endpoint 0 is always valid */ 443 + slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); 444 + for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) { 445 + ep_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, i); 446 + ep_ctx->ep_info = 0; 447 + ep_ctx->ep_info2 = 0; 448 + ep_ctx->deq = 0; 449 + ep_ctx->tx_info = 0; 450 + } 451 + } 452 + 453 + /* Issue a configure endpoint command and wait for it to finish. */ 454 + static int cdnsp_configure_endpoint(struct cdnsp_device *pdev) 455 + { 456 + int ret; 457 + 458 + cdnsp_queue_configure_endpoint(pdev, pdev->cmd.in_ctx->dma); 459 + cdnsp_ring_cmd_db(pdev); 460 + ret = cdnsp_wait_for_cmd_compl(pdev); 461 + if (ret) { 462 + dev_err(pdev->dev, 463 + "ERR: unexpected command completion code 0x%x.\n", ret); 464 + return -EINVAL; 465 + } 466 + 467 + return ret; 468 + } 469 + 470 + static void cdnsp_invalidate_ep_events(struct cdnsp_device *pdev, 471 + struct cdnsp_ep *pep) 472 + { 473 + struct cdnsp_segment *segment; 474 + union cdnsp_trb *event; 475 + u32 cycle_state; 476 + __le32 data; 477 + 478 + event = pdev->event_ring->dequeue; 479 + segment = pdev->event_ring->deq_seg; 480 + cycle_state = pdev->event_ring->cycle_state; 481 + 482 + while (1) { 483 + data = le32_to_cpu(event->trans_event.flags); 484 + 485 + /* Check the owner of the TRB. */ 486 + if ((data & TRB_CYCLE) != cycle_state) 487 + break; 488 + 489 + if (TRB_FIELD_TO_TYPE(data) == TRB_TRANSFER && 490 + TRB_TO_EP_ID(data) == (pep->idx + 1)) { 491 + data |= TRB_EVENT_INVALIDATE; 492 + event->trans_event.flags = cpu_to_le32(data); 493 + } 494 + 495 + if (cdnsp_last_trb_on_seg(segment, event)) { 496 + cycle_state ^= 1; 497 + segment = pdev->event_ring->deq_seg->next; 498 + event = segment->trbs; 499 + } else { 500 + event++; 501 + } 502 + } 503 + } 504 + 505 + int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev) 506 + { 507 + struct cdnsp_segment *event_deq_seg; 508 + union cdnsp_trb *cmd_trb; 509 + dma_addr_t cmd_deq_dma; 510 + union cdnsp_trb *event; 511 + u32 cycle_state; 512 + __le32 flags; 513 + int ret, val; 514 + u64 cmd_dma; 515 + 516 + cmd_trb = pdev->cmd.command_trb; 517 + pdev->cmd.status = 0; 518 + 519 + ret = readl_poll_timeout_atomic(&pdev->op_regs->cmd_ring, val, 520 + !CMD_RING_BUSY(val), 1, 521 + CDNSP_CMD_TIMEOUT); 522 + if (ret) { 523 + dev_err(pdev->dev, "ERR: Timeout while waiting for command\n"); 524 + pdev->cdnsp_state = CDNSP_STATE_DYING; 525 + return -ETIMEDOUT; 526 + } 527 + 528 + event = pdev->event_ring->dequeue; 529 + event_deq_seg = pdev->event_ring->deq_seg; 530 + cycle_state = pdev->event_ring->cycle_state; 531 + 532 + cmd_deq_dma = cdnsp_trb_virt_to_dma(pdev->cmd_ring->deq_seg, cmd_trb); 533 + if (!cmd_deq_dma) 534 + return -EINVAL; 535 + 536 + while (1) { 537 + flags = le32_to_cpu(event->event_cmd.flags); 538 + 539 + /* Check the owner of the TRB. */ 540 + if ((flags & TRB_CYCLE) != cycle_state) 541 + return -EINVAL; 542 + 543 + cmd_dma = le64_to_cpu(event->event_cmd.cmd_trb); 544 + 545 + /* 546 + * Check whether the completion event is for last queued 547 + * command. 548 + */ 549 + if (TRB_FIELD_TO_TYPE(flags) != TRB_COMPLETION || 550 + cmd_dma != (u64)cmd_deq_dma) { 551 + if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) { 552 + event++; 553 + continue; 554 + } 555 + 556 + if (cdnsp_last_trb_on_ring(pdev->event_ring, 557 + event_deq_seg, event)) 558 + cycle_state ^= 1; 559 + 560 + event_deq_seg = event_deq_seg->next; 561 + event = event_deq_seg->trbs; 562 + continue; 563 + } 564 + 565 + pdev->cmd.status = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)); 566 + if (pdev->cmd.status == COMP_SUCCESS) 567 + return 0; 568 + 569 + return -pdev->cmd.status; 570 + } 571 + } 572 + 573 + int cdnsp_halt_endpoint(struct cdnsp_device *pdev, 574 + struct cdnsp_ep *pep, 575 + int value) 576 + { 577 + int ret; 578 + 579 + if (value) { 580 + ret = cdnsp_cmd_stop_ep(pdev, pep); 581 + if (ret) 582 + return ret; 583 + 584 + if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_STOPPED) { 585 + cdnsp_queue_halt_endpoint(pdev, pep->idx); 586 + cdnsp_ring_cmd_db(pdev); 587 + ret = cdnsp_wait_for_cmd_compl(pdev); 588 + } 589 + 590 + pep->ep_state |= EP_HALTED; 591 + } else { 592 + /* 593 + * In device mode driver can call reset endpoint command 594 + * from any endpoint state. 595 + */ 596 + cdnsp_queue_reset_ep(pdev, pep->idx); 597 + cdnsp_ring_cmd_db(pdev); 598 + ret = cdnsp_wait_for_cmd_compl(pdev); 599 + if (ret) 600 + return ret; 601 + 602 + pep->ep_state &= ~EP_HALTED; 603 + 604 + if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE)) 605 + cdnsp_ring_doorbell_for_active_rings(pdev, pep); 606 + 607 + pep->ep_state &= ~EP_WEDGE; 608 + } 609 + 610 + return 0; 611 + } 612 + 613 + static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev, 614 + struct cdnsp_ep *pep) 615 + { 616 + struct cdnsp_input_control_ctx *ctrl_ctx; 617 + struct cdnsp_slot_ctx *slot_ctx; 618 + int ret = 0; 619 + u32 ep_sts; 620 + int i; 621 + 622 + ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx); 623 + 624 + /* Don't issue the command if there's no endpoints to update. */ 625 + if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0) 626 + return 0; 627 + 628 + ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 629 + ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG); 630 + ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG)); 631 + 632 + /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */ 633 + slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx); 634 + for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) { 635 + __le32 le32 = cpu_to_le32(BIT(i)); 636 + 637 + if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) || 638 + (ctrl_ctx->add_flags & le32) || i == 1) { 639 + slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); 640 + slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i)); 641 + break; 642 + } 643 + } 644 + 645 + ep_sts = GET_EP_CTX_STATE(pep->out_ctx); 646 + 647 + if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) && 648 + ep_sts == EP_STATE_DISABLED) || 649 + (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags)) 650 + ret = cdnsp_configure_endpoint(pdev); 651 + 652 + cdnsp_zero_in_ctx(pdev); 653 + 654 + return ret; 655 + } 656 + 657 + /* 658 + * This submits a Reset Device Command, which will set the device state to 0, 659 + * set the device address to 0, and disable all the endpoints except the default 660 + * control endpoint. The USB core should come back and call 661 + * cdnsp_setup_device(), and then re-set up the configuration. 662 + */ 663 + int cdnsp_reset_device(struct cdnsp_device *pdev) 664 + { 665 + struct cdnsp_slot_ctx *slot_ctx; 666 + int slot_state; 667 + int ret, i; 668 + 669 + slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx); 670 + slot_ctx->dev_info = 0; 671 + pdev->device_address = 0; 672 + 673 + /* If device is not setup, there is no point in resetting it. */ 674 + slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx); 675 + slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)); 676 + 677 + if (slot_state <= SLOT_STATE_DEFAULT && 678 + pdev->eps[0].ep_state & EP_HALTED) { 679 + cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0); 680 + } 681 + 682 + /* 683 + * During Reset Device command controller shall transition the 684 + * endpoint ep0 to the Running State. 685 + */ 686 + pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED); 687 + pdev->eps[0].ep_state |= EP_ENABLED; 688 + 689 + if (slot_state <= SLOT_STATE_DEFAULT) 690 + return 0; 691 + 692 + cdnsp_queue_reset_device(pdev); 693 + cdnsp_ring_cmd_db(pdev); 694 + ret = cdnsp_wait_for_cmd_compl(pdev); 695 + 696 + /* 697 + * After Reset Device command all not default endpoints 698 + * are in Disabled state. 699 + */ 700 + for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) 701 + pdev->eps[i].ep_state |= EP_STOPPED; 702 + 703 + if (ret) 704 + dev_err(pdev->dev, "Reset device failed with error code %d", 705 + ret); 706 + 707 + return ret; 708 + } 709 + 710 + /* 711 + * Sets the MaxPStreams field and the Linear Stream Array field. 712 + * Sets the dequeue pointer to the stream context array. 713 + */ 714 + static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev, 715 + struct cdnsp_ep_ctx *ep_ctx, 716 + struct cdnsp_stream_info *stream_info) 717 + { 718 + u32 max_primary_streams; 719 + 720 + /* MaxPStreams is the number of stream context array entries, not the 721 + * number we're actually using. Must be in 2^(MaxPstreams + 1) format. 722 + * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc. 723 + */ 724 + max_primary_streams = fls(stream_info->num_stream_ctxs) - 2; 725 + ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK); 726 + ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams) 727 + | EP_HAS_LSA); 728 + ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma); 729 + } 730 + 731 + /* 732 + * The drivers use this function to prepare a bulk endpoints to use streams. 733 + * 734 + * Don't allow the call to succeed if endpoint only supports one stream 735 + * (which means it doesn't support streams at all). 736 + */ 737 + int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep) 738 + { 739 + unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc); 740 + unsigned int num_stream_ctxs; 741 + int ret; 742 + 743 + if (num_streams == 0) 744 + return 0; 745 + 746 + if (num_streams > STREAM_NUM_STREAMS) 747 + return -EINVAL; 748 + 749 + /* 750 + * Add two to the number of streams requested to account for 751 + * stream 0 that is reserved for controller usage and one additional 752 + * for TASK SET FULL response. 753 + */ 754 + num_streams += 2; 755 + 756 + /* The stream context array size must be a power of two */ 757 + num_stream_ctxs = roundup_pow_of_two(num_streams); 758 + 759 + ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams); 760 + if (ret) 761 + return ret; 762 + 763 + cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info); 764 + 765 + pep->ep_state |= EP_HAS_STREAMS; 766 + pep->stream_info.td_count = 0; 767 + pep->stream_info.first_prime_det = 0; 768 + 769 + /* Subtract 1 for stream 0, which drivers can't use. */ 770 + return num_streams - 1; 771 + } 772 + 773 + int cdnsp_disable_slot(struct cdnsp_device *pdev) 774 + { 775 + int ret; 776 + 777 + cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT); 778 + cdnsp_ring_cmd_db(pdev); 779 + ret = cdnsp_wait_for_cmd_compl(pdev); 780 + 781 + pdev->slot_id = 0; 782 + pdev->active_port = NULL; 783 + 784 + memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE); 785 + memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE); 786 + 787 + return ret; 788 + } 789 + 790 + int cdnsp_enable_slot(struct cdnsp_device *pdev) 791 + { 792 + struct cdnsp_slot_ctx *slot_ctx; 793 + int slot_state; 794 + int ret; 795 + 796 + /* If device is not setup, there is no point in resetting it */ 797 + slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx); 798 + slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)); 799 + 800 + if (slot_state != SLOT_STATE_DISABLED) 801 + return 0; 802 + 803 + cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT); 804 + cdnsp_ring_cmd_db(pdev); 805 + ret = cdnsp_wait_for_cmd_compl(pdev); 806 + if (ret) 807 + return ret; 808 + 809 + pdev->slot_id = 1; 810 + 811 + return 0; 812 + } 813 + 814 + /* 815 + * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY 816 + * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS. 817 + */ 818 + int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup) 819 + { 820 + struct cdnsp_input_control_ctx *ctrl_ctx; 821 + struct cdnsp_slot_ctx *slot_ctx; 822 + int dev_state = 0; 823 + int ret; 824 + 825 + if (!pdev->slot_id) 826 + return -EINVAL; 827 + 828 + if (!pdev->active_port->port_num) 829 + return -EINVAL; 830 + 831 + slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx); 832 + dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)); 833 + 834 + if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) 835 + return 0; 836 + 837 + slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx); 838 + ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx); 839 + 840 + if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) { 841 + ret = cdnsp_setup_addressable_priv_dev(pdev); 842 + if (ret) 843 + return ret; 844 + } 845 + 846 + cdnsp_copy_ep0_dequeue_into_input_ctx(pdev); 847 + 848 + ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); 849 + ctrl_ctx->drop_flags = 0; 850 + 851 + cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup); 852 + cdnsp_ring_cmd_db(pdev); 853 + ret = cdnsp_wait_for_cmd_compl(pdev); 854 + 855 + /* Zero the input context control for later use. */ 856 + ctrl_ctx->add_flags = 0; 857 + ctrl_ctx->drop_flags = 0; 858 + 859 + return ret; 860 + } 861 + 862 + void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev, 863 + struct usb_request *req, 864 + int enable) 865 + { 866 + if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable) 867 + return; 868 + 869 + if (enable) 870 + writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE, 871 + &pdev->active_port->regs->portpmsc); 872 + else 873 + writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc); 874 + } 875 + 876 + static int cdnsp_get_frame(struct cdnsp_device *pdev) 877 + { 878 + return readl(&pdev->run_regs->microframe_index) >> 3; 879 + } 880 + 881 + static int cdnsp_gadget_ep_enable(struct usb_ep *ep, 882 + const struct usb_endpoint_descriptor *desc) 883 + { 884 + struct cdnsp_input_control_ctx *ctrl_ctx; 885 + struct cdnsp_device *pdev; 886 + struct cdnsp_ep *pep; 887 + unsigned long flags; 888 + u32 added_ctxs; 889 + int ret; 890 + 891 + if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT || 892 + !desc->wMaxPacketSize) 893 + return -EINVAL; 894 + 895 + pep = to_cdnsp_ep(ep); 896 + pdev = pep->pdev; 897 + 898 + if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED, 899 + "%s is already enabled\n", pep->name)) 900 + return 0; 901 + 902 + spin_lock_irqsave(&pdev->lock, flags); 903 + 904 + added_ctxs = cdnsp_get_endpoint_flag(desc); 905 + if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) { 906 + dev_err(pdev->dev, "ERROR: Bad endpoint number\n"); 907 + ret = -EINVAL; 908 + goto unlock; 909 + } 910 + 911 + pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0; 912 + 913 + if (pdev->gadget.speed == USB_SPEED_FULL) { 914 + if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT) 915 + pep->interval = desc->bInterval << 3; 916 + if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) 917 + pep->interval = BIT(desc->bInterval - 1) << 3; 918 + } 919 + 920 + if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) { 921 + if (pep->interval > BIT(12)) { 922 + dev_err(pdev->dev, "bInterval %d not supported\n", 923 + desc->bInterval); 924 + ret = -EINVAL; 925 + goto unlock; 926 + } 927 + cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS); 928 + } 929 + 930 + ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC); 931 + if (ret) 932 + goto unlock; 933 + 934 + ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx); 935 + ctrl_ctx->add_flags = cpu_to_le32(added_ctxs); 936 + ctrl_ctx->drop_flags = 0; 937 + 938 + ret = cdnsp_update_eps_configuration(pdev, pep); 939 + if (ret) { 940 + cdnsp_free_endpoint_rings(pdev, pep); 941 + goto unlock; 942 + } 943 + 944 + pep->ep_state |= EP_ENABLED; 945 + pep->ep_state &= ~EP_STOPPED; 946 + 947 + unlock: 948 + spin_unlock_irqrestore(&pdev->lock, flags); 949 + 950 + return ret; 951 + } 952 + 953 + static int cdnsp_gadget_ep_disable(struct usb_ep *ep) 954 + { 955 + struct cdnsp_input_control_ctx *ctrl_ctx; 956 + struct cdnsp_request *preq; 957 + struct cdnsp_device *pdev; 958 + struct cdnsp_ep *pep; 959 + unsigned long flags; 960 + u32 drop_flag; 961 + int ret = 0; 962 + 963 + if (!ep) 964 + return -EINVAL; 965 + 966 + pep = to_cdnsp_ep(ep); 967 + pdev = pep->pdev; 968 + 969 + spin_lock_irqsave(&pdev->lock, flags); 970 + 971 + if (!(pep->ep_state & EP_ENABLED)) { 972 + dev_err(pdev->dev, "%s is already disabled\n", pep->name); 973 + ret = -EINVAL; 974 + goto finish; 975 + } 976 + 977 + cdnsp_cmd_stop_ep(pdev, pep); 978 + pep->ep_state |= EP_DIS_IN_RROGRESS; 979 + cdnsp_cmd_flush_ep(pdev, pep); 980 + 981 + /* Remove all queued USB requests. */ 982 + while (!list_empty(&pep->pending_list)) { 983 + preq = next_request(&pep->pending_list); 984 + cdnsp_ep_dequeue(pep, preq); 985 + } 986 + 987 + cdnsp_invalidate_ep_events(pdev, pep); 988 + 989 + pep->ep_state &= ~EP_DIS_IN_RROGRESS; 990 + drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc); 991 + ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx); 992 + ctrl_ctx->drop_flags = cpu_to_le32(drop_flag); 993 + ctrl_ctx->add_flags = 0; 994 + 995 + cdnsp_endpoint_zero(pdev, pep); 996 + 997 + ret = cdnsp_update_eps_configuration(pdev, pep); 998 + cdnsp_free_endpoint_rings(pdev, pep); 999 + 1000 + pep->ep_state &= ~EP_ENABLED; 1001 + pep->ep_state |= EP_STOPPED; 1002 + 1003 + finish: 1004 + spin_unlock_irqrestore(&pdev->lock, flags); 1005 + 1006 + return ret; 1007 + } 1008 + 1009 + static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep, 1010 + gfp_t gfp_flags) 1011 + { 1012 + struct cdnsp_ep *pep = to_cdnsp_ep(ep); 1013 + struct cdnsp_request *preq; 1014 + 1015 + preq = kzalloc(sizeof(*preq), gfp_flags); 1016 + if (!preq) 1017 + return NULL; 1018 + 1019 + preq->epnum = pep->number; 1020 + preq->pep = pep; 1021 + 1022 + return &preq->request; 1023 + } 1024 + 1025 + static void cdnsp_gadget_ep_free_request(struct usb_ep *ep, 1026 + struct usb_request *request) 1027 + { 1028 + struct cdnsp_request *preq = to_cdnsp_request(request); 1029 + 1030 + kfree(preq); 1031 + } 1032 + 1033 + static int cdnsp_gadget_ep_queue(struct usb_ep *ep, 1034 + struct usb_request *request, 1035 + gfp_t gfp_flags) 1036 + { 1037 + struct cdnsp_request *preq; 1038 + struct cdnsp_device *pdev; 1039 + struct cdnsp_ep *pep; 1040 + unsigned long flags; 1041 + int ret; 1042 + 1043 + if (!request || !ep) 1044 + return -EINVAL; 1045 + 1046 + pep = to_cdnsp_ep(ep); 1047 + pdev = pep->pdev; 1048 + 1049 + if (!(pep->ep_state & EP_ENABLED)) { 1050 + dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n", 1051 + pep->name); 1052 + return -EINVAL; 1053 + } 1054 + 1055 + preq = to_cdnsp_request(request); 1056 + spin_lock_irqsave(&pdev->lock, flags); 1057 + ret = cdnsp_ep_enqueue(pep, preq); 1058 + spin_unlock_irqrestore(&pdev->lock, flags); 1059 + 1060 + return ret; 1061 + } 1062 + 1063 + static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep, 1064 + struct usb_request *request) 1065 + { 1066 + struct cdnsp_ep *pep = to_cdnsp_ep(ep); 1067 + struct cdnsp_device *pdev = pep->pdev; 1068 + unsigned long flags; 1069 + int ret; 1070 + 1071 + if (!pep->endpoint.desc) { 1072 + dev_err(pdev->dev, 1073 + "%s: can't dequeue to disabled endpoint\n", 1074 + pep->name); 1075 + return -ESHUTDOWN; 1076 + } 1077 + 1078 + spin_lock_irqsave(&pdev->lock, flags); 1079 + ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request)); 1080 + spin_unlock_irqrestore(&pdev->lock, flags); 1081 + 1082 + return ret; 1083 + } 1084 + 1085 + static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value) 1086 + { 1087 + struct cdnsp_ep *pep = to_cdnsp_ep(ep); 1088 + struct cdnsp_device *pdev = pep->pdev; 1089 + struct cdnsp_request *preq; 1090 + unsigned long flags = 0; 1091 + int ret; 1092 + 1093 + spin_lock_irqsave(&pdev->lock, flags); 1094 + 1095 + preq = next_request(&pep->pending_list); 1096 + if (value) { 1097 + if (preq) { 1098 + ret = -EAGAIN; 1099 + goto done; 1100 + } 1101 + } 1102 + 1103 + ret = cdnsp_halt_endpoint(pdev, pep, value); 1104 + 1105 + done: 1106 + spin_unlock_irqrestore(&pdev->lock, flags); 1107 + return ret; 1108 + } 1109 + 1110 + static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep) 1111 + { 1112 + struct cdnsp_ep *pep = to_cdnsp_ep(ep); 1113 + struct cdnsp_device *pdev = pep->pdev; 1114 + unsigned long flags = 0; 1115 + int ret; 1116 + 1117 + spin_lock_irqsave(&pdev->lock, flags); 1118 + pep->ep_state |= EP_WEDGE; 1119 + ret = cdnsp_halt_endpoint(pdev, pep, 1); 1120 + spin_unlock_irqrestore(&pdev->lock, flags); 1121 + 1122 + return ret; 1123 + } 1124 + 1125 + static const struct usb_ep_ops cdnsp_gadget_ep0_ops = { 1126 + .enable = cdnsp_gadget_ep_enable, 1127 + .disable = cdnsp_gadget_ep_disable, 1128 + .alloc_request = cdnsp_gadget_ep_alloc_request, 1129 + .free_request = cdnsp_gadget_ep_free_request, 1130 + .queue = cdnsp_gadget_ep_queue, 1131 + .dequeue = cdnsp_gadget_ep_dequeue, 1132 + .set_halt = cdnsp_gadget_ep_set_halt, 1133 + .set_wedge = cdnsp_gadget_ep_set_wedge, 1134 + }; 1135 + 1136 + static const struct usb_ep_ops cdnsp_gadget_ep_ops = { 1137 + .enable = cdnsp_gadget_ep_enable, 1138 + .disable = cdnsp_gadget_ep_disable, 1139 + .alloc_request = cdnsp_gadget_ep_alloc_request, 1140 + .free_request = cdnsp_gadget_ep_free_request, 1141 + .queue = cdnsp_gadget_ep_queue, 1142 + .dequeue = cdnsp_gadget_ep_dequeue, 1143 + .set_halt = cdnsp_gadget_ep_set_halt, 1144 + .set_wedge = cdnsp_gadget_ep_set_wedge, 1145 + }; 1146 + 1147 + void cdnsp_gadget_giveback(struct cdnsp_ep *pep, 1148 + struct cdnsp_request *preq, 1149 + int status) 1150 + { 1151 + struct cdnsp_device *pdev = pep->pdev; 1152 + 1153 + list_del(&preq->list); 1154 + 1155 + if (preq->request.status == -EINPROGRESS) 1156 + preq->request.status = status; 1157 + 1158 + usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request, 1159 + preq->direction); 1160 + 1161 + if (preq != &pdev->ep0_preq) { 1162 + spin_unlock(&pdev->lock); 1163 + usb_gadget_giveback_request(&pep->endpoint, &preq->request); 1164 + spin_lock(&pdev->lock); 1165 + } 1166 + } 1167 + 1168 + static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = { 1169 + .bLength = USB_DT_ENDPOINT_SIZE, 1170 + .bDescriptorType = USB_DT_ENDPOINT, 1171 + .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 1172 + }; 1173 + 1174 + static int cdnsp_run(struct cdnsp_device *pdev, 1175 + enum usb_device_speed speed) 1176 + { 1177 + u32 fs_speed = 0; 1178 + u64 temp_64; 1179 + u32 temp; 1180 + int ret; 1181 + 1182 + temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue); 1183 + temp_64 &= ~ERST_PTR_MASK; 1184 + temp = readl(&pdev->ir_set->irq_control); 1185 + temp &= ~IMOD_INTERVAL_MASK; 1186 + temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK); 1187 + writel(temp, &pdev->ir_set->irq_control); 1188 + 1189 + temp = readl(&pdev->port3x_regs->mode_addr); 1190 + 1191 + switch (speed) { 1192 + case USB_SPEED_SUPER_PLUS: 1193 + temp |= CFG_3XPORT_SSP_SUPPORT; 1194 + break; 1195 + case USB_SPEED_SUPER: 1196 + temp &= ~CFG_3XPORT_SSP_SUPPORT; 1197 + break; 1198 + case USB_SPEED_HIGH: 1199 + break; 1200 + case USB_SPEED_FULL: 1201 + fs_speed = PORT_REG6_FORCE_FS; 1202 + break; 1203 + default: 1204 + dev_err(pdev->dev, "invalid maximum_speed parameter %d\n", 1205 + speed); 1206 + fallthrough; 1207 + case USB_SPEED_UNKNOWN: 1208 + /* Default to superspeed. */ 1209 + speed = USB_SPEED_SUPER; 1210 + break; 1211 + } 1212 + 1213 + if (speed >= USB_SPEED_SUPER) { 1214 + writel(temp, &pdev->port3x_regs->mode_addr); 1215 + cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc, 1216 + XDEV_RXDETECT); 1217 + } else { 1218 + cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc); 1219 + } 1220 + 1221 + cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc, 1222 + XDEV_RXDETECT); 1223 + 1224 + cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 1225 + 1226 + writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6); 1227 + 1228 + ret = cdnsp_start(pdev); 1229 + if (ret) { 1230 + ret = -ENODEV; 1231 + goto err; 1232 + } 1233 + 1234 + temp = readl(&pdev->op_regs->command); 1235 + temp |= (CMD_INTE); 1236 + writel(temp, &pdev->op_regs->command); 1237 + 1238 + temp = readl(&pdev->ir_set->irq_pending); 1239 + writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending); 1240 + 1241 + return 0; 1242 + err: 1243 + cdnsp_halt(pdev); 1244 + return ret; 1245 + } 1246 + 1247 + static int cdnsp_gadget_udc_start(struct usb_gadget *g, 1248 + struct usb_gadget_driver *driver) 1249 + { 1250 + enum usb_device_speed max_speed = driver->max_speed; 1251 + struct cdnsp_device *pdev = gadget_to_cdnsp(g); 1252 + unsigned long flags; 1253 + int ret; 1254 + 1255 + spin_lock_irqsave(&pdev->lock, flags); 1256 + pdev->gadget_driver = driver; 1257 + 1258 + /* limit speed if necessary */ 1259 + max_speed = min(driver->max_speed, g->max_speed); 1260 + ret = cdnsp_run(pdev, max_speed); 1261 + 1262 + spin_unlock_irqrestore(&pdev->lock, flags); 1263 + 1264 + return ret; 1265 + } 1266 + 1267 + /* 1268 + * Update Event Ring Dequeue Pointer: 1269 + * - When all events have finished 1270 + * - To avoid "Event Ring Full Error" condition 1271 + */ 1272 + void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev, 1273 + union cdnsp_trb *event_ring_deq, 1274 + u8 clear_ehb) 1275 + { 1276 + u64 temp_64; 1277 + dma_addr_t deq; 1278 + 1279 + temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue); 1280 + 1281 + /* If necessary, update the HW's version of the event ring deq ptr. */ 1282 + if (event_ring_deq != pdev->event_ring->dequeue) { 1283 + deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg, 1284 + pdev->event_ring->dequeue); 1285 + temp_64 &= ERST_PTR_MASK; 1286 + temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK); 1287 + } 1288 + 1289 + /* Clear the event handler busy flag (RW1C). */ 1290 + if (clear_ehb) 1291 + temp_64 |= ERST_EHB; 1292 + else 1293 + temp_64 &= ~ERST_EHB; 1294 + 1295 + cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue); 1296 + } 1297 + 1298 + static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev) 1299 + { 1300 + struct cdnsp_segment *seg; 1301 + u64 val_64; 1302 + int i; 1303 + 1304 + cdnsp_initialize_ring_info(pdev->cmd_ring); 1305 + 1306 + seg = pdev->cmd_ring->first_seg; 1307 + for (i = 0; i < pdev->cmd_ring->num_segs; i++) { 1308 + memset(seg->trbs, 0, 1309 + sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1)); 1310 + seg = seg->next; 1311 + } 1312 + 1313 + /* Set the address in the Command Ring Control register. */ 1314 + val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring); 1315 + val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) | 1316 + (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) | 1317 + pdev->cmd_ring->cycle_state; 1318 + cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring); 1319 + } 1320 + 1321 + static void cdnsp_consume_all_events(struct cdnsp_device *pdev) 1322 + { 1323 + struct cdnsp_segment *event_deq_seg; 1324 + union cdnsp_trb *event_ring_deq; 1325 + union cdnsp_trb *event; 1326 + u32 cycle_bit; 1327 + 1328 + event_ring_deq = pdev->event_ring->dequeue; 1329 + event_deq_seg = pdev->event_ring->deq_seg; 1330 + event = pdev->event_ring->dequeue; 1331 + 1332 + /* Update ring dequeue pointer. */ 1333 + while (1) { 1334 + cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE); 1335 + 1336 + /* Does the controller or driver own the TRB? */ 1337 + if (cycle_bit != pdev->event_ring->cycle_state) 1338 + break; 1339 + 1340 + cdnsp_inc_deq(pdev, pdev->event_ring); 1341 + 1342 + if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) { 1343 + event++; 1344 + continue; 1345 + } 1346 + 1347 + if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg, 1348 + event)) 1349 + cycle_bit ^= 1; 1350 + 1351 + event_deq_seg = event_deq_seg->next; 1352 + event = event_deq_seg->trbs; 1353 + } 1354 + 1355 + cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1); 1356 + } 1357 + 1358 + static void cdnsp_stop(struct cdnsp_device *pdev) 1359 + { 1360 + u32 temp; 1361 + 1362 + cdnsp_cmd_flush_ep(pdev, &pdev->eps[0]); 1363 + 1364 + /* Remove internally queued request for ep0. */ 1365 + if (!list_empty(&pdev->eps[0].pending_list)) { 1366 + struct cdnsp_request *req; 1367 + 1368 + req = next_request(&pdev->eps[0].pending_list); 1369 + if (req == &pdev->ep0_preq) 1370 + cdnsp_ep_dequeue(&pdev->eps[0], req); 1371 + } 1372 + 1373 + cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc); 1374 + cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc); 1375 + cdnsp_disable_slot(pdev); 1376 + cdnsp_halt(pdev); 1377 + 1378 + temp = readl(&pdev->op_regs->status); 1379 + writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status); 1380 + temp = readl(&pdev->ir_set->irq_pending); 1381 + writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending); 1382 + 1383 + cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc); 1384 + cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc); 1385 + 1386 + /* Clear interrupt line */ 1387 + temp = readl(&pdev->ir_set->irq_pending); 1388 + temp |= IMAN_IP; 1389 + writel(temp, &pdev->ir_set->irq_pending); 1390 + 1391 + cdnsp_consume_all_events(pdev); 1392 + cdnsp_clear_cmd_ring(pdev); 1393 + } 1394 + 1395 + /* 1396 + * Stop controller. 1397 + * This function is called by the gadget core when the driver is removed. 1398 + * Disable slot, disable IRQs, and quiesce the controller. 1399 + */ 1400 + static int cdnsp_gadget_udc_stop(struct usb_gadget *g) 1401 + { 1402 + struct cdnsp_device *pdev = gadget_to_cdnsp(g); 1403 + unsigned long flags; 1404 + 1405 + spin_lock_irqsave(&pdev->lock, flags); 1406 + cdnsp_stop(pdev); 1407 + pdev->gadget_driver = NULL; 1408 + spin_unlock_irqrestore(&pdev->lock, flags); 1409 + 1410 + return 0; 1411 + } 1412 + 1413 + static int cdnsp_gadget_get_frame(struct usb_gadget *g) 1414 + { 1415 + struct cdnsp_device *pdev = gadget_to_cdnsp(g); 1416 + 1417 + return cdnsp_get_frame(pdev); 1418 + } 1419 + 1420 + static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev) 1421 + { 1422 + struct cdnsp_port_regs __iomem *port_regs; 1423 + u32 portpm, portsc; 1424 + 1425 + port_regs = pdev->active_port->regs; 1426 + portsc = readl(&port_regs->portsc) & PORT_PLS_MASK; 1427 + 1428 + /* Remote wakeup feature is not enabled by host. */ 1429 + if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) { 1430 + portpm = readl(&port_regs->portpmsc); 1431 + 1432 + if (!(portpm & PORT_RWE)) 1433 + return; 1434 + } 1435 + 1436 + if (portsc == XDEV_U3 && !pdev->may_wakeup) 1437 + return; 1438 + 1439 + cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0); 1440 + 1441 + pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING; 1442 + } 1443 + 1444 + static int cdnsp_gadget_wakeup(struct usb_gadget *g) 1445 + { 1446 + struct cdnsp_device *pdev = gadget_to_cdnsp(g); 1447 + unsigned long flags; 1448 + 1449 + spin_lock_irqsave(&pdev->lock, flags); 1450 + __cdnsp_gadget_wakeup(pdev); 1451 + spin_unlock_irqrestore(&pdev->lock, flags); 1452 + 1453 + return 0; 1454 + } 1455 + 1456 + static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g, 1457 + int is_selfpowered) 1458 + { 1459 + struct cdnsp_device *pdev = gadget_to_cdnsp(g); 1460 + unsigned long flags; 1461 + 1462 + spin_lock_irqsave(&pdev->lock, flags); 1463 + g->is_selfpowered = !!is_selfpowered; 1464 + spin_unlock_irqrestore(&pdev->lock, flags); 1465 + 1466 + return 0; 1467 + } 1468 + 1469 + static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on) 1470 + { 1471 + struct cdnsp_device *pdev = gadget_to_cdnsp(gadget); 1472 + struct cdns *cdns = dev_get_drvdata(pdev->dev); 1473 + 1474 + if (!is_on) { 1475 + cdnsp_reset_device(pdev); 1476 + cdns_clear_vbus(cdns); 1477 + } else { 1478 + cdns_set_vbus(cdns); 1479 + } 1480 + return 0; 1481 + } 1482 + 1483 + const struct usb_gadget_ops cdnsp_gadget_ops = { 1484 + .get_frame = cdnsp_gadget_get_frame, 1485 + .wakeup = cdnsp_gadget_wakeup, 1486 + .set_selfpowered = cdnsp_gadget_set_selfpowered, 1487 + .pullup = cdnsp_gadget_pullup, 1488 + .udc_start = cdnsp_gadget_udc_start, 1489 + .udc_stop = cdnsp_gadget_udc_stop, 1490 + }; 1491 + 1492 + static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev, 1493 + struct cdnsp_ep *pep) 1494 + { 1495 + void __iomem *reg = &pdev->cap_regs->hc_capbase; 1496 + int endpoints; 1497 + 1498 + reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID); 1499 + 1500 + if (!pep->direction) { 1501 + pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET); 1502 + pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET); 1503 + pep->buffering = (pep->buffering + 1) / 2; 1504 + pep->buffering_period = (pep->buffering_period + 1) / 2; 1505 + return; 1506 + } 1507 + 1508 + endpoints = HCS_ENDPOINTS(readl(&pdev->hcs_params1)) / 2; 1509 + 1510 + /* Set to XBUF_TX_TAG_MASK_0 register. */ 1511 + reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32); 1512 + /* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */ 1513 + reg += pep->number * sizeof(u32) * 2; 1514 + 1515 + pep->buffering = (readl(reg) + 1) / 2; 1516 + pep->buffering_period = pep->buffering; 1517 + } 1518 + 1519 + static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev) 1520 + { 1521 + int max_streams = HCC_MAX_PSA(pdev->hcc_params); 1522 + struct cdnsp_ep *pep; 1523 + int i; 1524 + 1525 + INIT_LIST_HEAD(&pdev->gadget.ep_list); 1526 + 1527 + if (max_streams < STREAM_LOG_STREAMS) { 1528 + dev_err(pdev->dev, "Stream size %d not supported\n", 1529 + max_streams); 1530 + return -EINVAL; 1531 + } 1532 + 1533 + max_streams = STREAM_LOG_STREAMS; 1534 + 1535 + for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) { 1536 + bool direction = !(i & 1); /* Start from OUT endpoint. */ 1537 + u8 epnum = ((i + 1) >> 1); 1538 + 1539 + if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction)) 1540 + continue; 1541 + 1542 + pep = &pdev->eps[i]; 1543 + pep->pdev = pdev; 1544 + pep->number = epnum; 1545 + pep->direction = direction; /* 0 for OUT, 1 for IN. */ 1546 + 1547 + /* 1548 + * Ep0 is bidirectional, so ep0in and ep0out are represented by 1549 + * pdev->eps[0] 1550 + */ 1551 + if (epnum == 0) { 1552 + snprintf(pep->name, sizeof(pep->name), "ep%d%s", 1553 + epnum, "BiDir"); 1554 + 1555 + pep->idx = 0; 1556 + usb_ep_set_maxpacket_limit(&pep->endpoint, 512); 1557 + pep->endpoint.maxburst = 1; 1558 + pep->endpoint.ops = &cdnsp_gadget_ep0_ops; 1559 + pep->endpoint.desc = &cdnsp_gadget_ep0_desc; 1560 + pep->endpoint.comp_desc = NULL; 1561 + pep->endpoint.caps.type_control = true; 1562 + pep->endpoint.caps.dir_in = true; 1563 + pep->endpoint.caps.dir_out = true; 1564 + 1565 + pdev->ep0_preq.epnum = pep->number; 1566 + pdev->ep0_preq.pep = pep; 1567 + pdev->gadget.ep0 = &pep->endpoint; 1568 + } else { 1569 + snprintf(pep->name, sizeof(pep->name), "ep%d%s", 1570 + epnum, (pep->direction) ? "in" : "out"); 1571 + 1572 + pep->idx = (epnum * 2 + (direction ? 1 : 0)) - 1; 1573 + usb_ep_set_maxpacket_limit(&pep->endpoint, 1024); 1574 + 1575 + pep->endpoint.max_streams = max_streams; 1576 + pep->endpoint.ops = &cdnsp_gadget_ep_ops; 1577 + list_add_tail(&pep->endpoint.ep_list, 1578 + &pdev->gadget.ep_list); 1579 + 1580 + pep->endpoint.caps.type_iso = true; 1581 + pep->endpoint.caps.type_bulk = true; 1582 + pep->endpoint.caps.type_int = true; 1583 + 1584 + pep->endpoint.caps.dir_in = direction; 1585 + pep->endpoint.caps.dir_out = !direction; 1586 + } 1587 + 1588 + pep->endpoint.name = pep->name; 1589 + pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx); 1590 + pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx); 1591 + cdnsp_get_ep_buffering(pdev, pep); 1592 + 1593 + dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: " 1594 + "CTRL: %s, INT: %s, BULK: %s, ISOC %s, " 1595 + "SupDir IN: %s, OUT: %s\n", 1596 + pep->name, 1024, 1597 + (pep->endpoint.caps.type_control) ? "yes" : "no", 1598 + (pep->endpoint.caps.type_int) ? "yes" : "no", 1599 + (pep->endpoint.caps.type_bulk) ? "yes" : "no", 1600 + (pep->endpoint.caps.type_iso) ? "yes" : "no", 1601 + (pep->endpoint.caps.dir_in) ? "yes" : "no", 1602 + (pep->endpoint.caps.dir_out) ? "yes" : "no"); 1603 + 1604 + INIT_LIST_HEAD(&pep->pending_list); 1605 + } 1606 + 1607 + return 0; 1608 + } 1609 + 1610 + static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev) 1611 + { 1612 + struct cdnsp_ep *pep; 1613 + int i; 1614 + 1615 + for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) { 1616 + pep = &pdev->eps[i]; 1617 + if (pep->number != 0 && pep->out_ctx) 1618 + list_del(&pep->endpoint.ep_list); 1619 + } 1620 + } 1621 + 1622 + void cdnsp_disconnect_gadget(struct cdnsp_device *pdev) 1623 + { 1624 + pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING; 1625 + 1626 + if (pdev->gadget_driver && pdev->gadget_driver->disconnect) { 1627 + spin_unlock(&pdev->lock); 1628 + pdev->gadget_driver->disconnect(&pdev->gadget); 1629 + spin_lock(&pdev->lock); 1630 + } 1631 + 1632 + pdev->gadget.speed = USB_SPEED_UNKNOWN; 1633 + usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED); 1634 + 1635 + pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING; 1636 + } 1637 + 1638 + void cdnsp_suspend_gadget(struct cdnsp_device *pdev) 1639 + { 1640 + if (pdev->gadget_driver && pdev->gadget_driver->suspend) { 1641 + spin_unlock(&pdev->lock); 1642 + pdev->gadget_driver->suspend(&pdev->gadget); 1643 + spin_lock(&pdev->lock); 1644 + } 1645 + } 1646 + 1647 + void cdnsp_resume_gadget(struct cdnsp_device *pdev) 1648 + { 1649 + if (pdev->gadget_driver && pdev->gadget_driver->resume) { 1650 + spin_unlock(&pdev->lock); 1651 + pdev->gadget_driver->resume(&pdev->gadget); 1652 + spin_lock(&pdev->lock); 1653 + } 1654 + } 1655 + 1656 + void cdnsp_irq_reset(struct cdnsp_device *pdev) 1657 + { 1658 + struct cdnsp_port_regs __iomem *port_regs; 1659 + 1660 + cdnsp_reset_device(pdev); 1661 + 1662 + port_regs = pdev->active_port->regs; 1663 + pdev->gadget.speed = cdnsp_port_speed(readl(port_regs)); 1664 + 1665 + spin_unlock(&pdev->lock); 1666 + usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver); 1667 + spin_lock(&pdev->lock); 1668 + 1669 + switch (pdev->gadget.speed) { 1670 + case USB_SPEED_SUPER_PLUS: 1671 + case USB_SPEED_SUPER: 1672 + cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 1673 + pdev->gadget.ep0->maxpacket = 512; 1674 + break; 1675 + case USB_SPEED_HIGH: 1676 + case USB_SPEED_FULL: 1677 + cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 1678 + pdev->gadget.ep0->maxpacket = 64; 1679 + break; 1680 + default: 1681 + /* Low speed is not supported. */ 1682 + dev_err(pdev->dev, "Unknown device speed\n"); 1683 + break; 1684 + } 1685 + 1686 + cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS); 1687 + cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY); 1688 + usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT); 1689 + } 1690 + 1691 + static void cdnsp_get_rev_cap(struct cdnsp_device *pdev) 1692 + { 1693 + void __iomem *reg = &pdev->cap_regs->hc_capbase; 1694 + struct cdnsp_rev_cap *rev_cap; 1695 + 1696 + reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP); 1697 + rev_cap = reg; 1698 + 1699 + pdev->rev_cap.ctrl_revision = readl(&rev_cap->ctrl_revision); 1700 + pdev->rev_cap.rtl_revision = readl(&rev_cap->rtl_revision); 1701 + pdev->rev_cap.ep_supported = readl(&rev_cap->ep_supported); 1702 + pdev->rev_cap.ext_cap = readl(&rev_cap->ext_cap); 1703 + pdev->rev_cap.rx_buff_size = readl(&rev_cap->rx_buff_size); 1704 + pdev->rev_cap.tx_buff_size = readl(&rev_cap->tx_buff_size); 1705 + 1706 + dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n", 1707 + pdev->rev_cap.ctrl_revision, pdev->rev_cap.rtl_revision, 1708 + pdev->rev_cap.ep_supported, pdev->rev_cap.rx_buff_size, 1709 + pdev->rev_cap.tx_buff_size); 1710 + } 1711 + 1712 + static int cdnsp_gen_setup(struct cdnsp_device *pdev) 1713 + { 1714 + int ret; 1715 + u32 reg; 1716 + 1717 + pdev->cap_regs = pdev->regs; 1718 + pdev->op_regs = pdev->regs + 1719 + HC_LENGTH(readl(&pdev->cap_regs->hc_capbase)); 1720 + pdev->run_regs = pdev->regs + 1721 + (readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK); 1722 + 1723 + /* Cache read-only capability registers */ 1724 + pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1); 1725 + pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase); 1726 + pdev->hci_version = HC_VERSION(pdev->hcc_params); 1727 + pdev->hcc_params = readl(&pdev->cap_regs->hcc_params); 1728 + 1729 + cdnsp_get_rev_cap(pdev); 1730 + 1731 + /* Make sure the Device Controller is halted. */ 1732 + ret = cdnsp_halt(pdev); 1733 + if (ret) 1734 + return ret; 1735 + 1736 + /* Reset the internal controller memory state and registers. */ 1737 + ret = cdnsp_reset(pdev); 1738 + if (ret) 1739 + return ret; 1740 + 1741 + /* 1742 + * Set dma_mask and coherent_dma_mask to 64-bits, 1743 + * if controller supports 64-bit addressing. 1744 + */ 1745 + if (HCC_64BIT_ADDR(pdev->hcc_params) && 1746 + !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) { 1747 + dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n"); 1748 + dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64)); 1749 + } else { 1750 + /* 1751 + * This is to avoid error in cases where a 32-bit USB 1752 + * controller is used on a 64-bit capable system. 1753 + */ 1754 + ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32)); 1755 + if (ret) 1756 + return ret; 1757 + 1758 + dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n"); 1759 + dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32)); 1760 + } 1761 + 1762 + spin_lock_init(&pdev->lock); 1763 + 1764 + ret = cdnsp_mem_init(pdev, GFP_KERNEL); 1765 + if (ret) 1766 + return ret; 1767 + 1768 + /* 1769 + * Software workaround for U1: after transition 1770 + * to U1 the controller starts gating clock, and in some cases, 1771 + * it causes that controller stack. 1772 + */ 1773 + reg = readl(&pdev->port3x_regs->mode_2); 1774 + reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN; 1775 + writel(reg, &pdev->port3x_regs->mode_2); 1776 + 1777 + return 0; 1778 + } 1779 + 1780 + static int __cdnsp_gadget_init(struct cdns *cdns) 1781 + { 1782 + struct cdnsp_device *pdev; 1783 + u32 max_speed; 1784 + int ret = -ENOMEM; 1785 + 1786 + cdns_drd_gadget_on(cdns); 1787 + 1788 + pdev = kzalloc(sizeof(*pdev), GFP_KERNEL); 1789 + if (!pdev) 1790 + return -ENOMEM; 1791 + 1792 + pm_runtime_get_sync(cdns->dev); 1793 + 1794 + cdns->gadget_dev = pdev; 1795 + pdev->dev = cdns->dev; 1796 + pdev->regs = cdns->dev_regs; 1797 + max_speed = usb_get_maximum_speed(cdns->dev); 1798 + 1799 + switch (max_speed) { 1800 + case USB_SPEED_FULL: 1801 + case USB_SPEED_HIGH: 1802 + case USB_SPEED_SUPER: 1803 + case USB_SPEED_SUPER_PLUS: 1804 + break; 1805 + default: 1806 + dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed); 1807 + fallthrough; 1808 + case USB_SPEED_UNKNOWN: 1809 + /* Default to SSP */ 1810 + max_speed = USB_SPEED_SUPER_PLUS; 1811 + break; 1812 + } 1813 + 1814 + pdev->gadget.ops = &cdnsp_gadget_ops; 1815 + pdev->gadget.name = "cdnsp-gadget"; 1816 + pdev->gadget.speed = USB_SPEED_UNKNOWN; 1817 + pdev->gadget.sg_supported = 1; 1818 + pdev->gadget.max_speed = USB_SPEED_SUPER_PLUS; 1819 + pdev->gadget.lpm_capable = 1; 1820 + 1821 + pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL); 1822 + if (!pdev->setup_buf) 1823 + goto free_pdev; 1824 + 1825 + /* 1826 + * Controller supports not aligned buffer but it should improve 1827 + * performance. 1828 + */ 1829 + pdev->gadget.quirk_ep_out_aligned_size = true; 1830 + 1831 + ret = cdnsp_gen_setup(pdev); 1832 + if (ret) { 1833 + dev_err(pdev->dev, "Generic initialization failed %d\n", ret); 1834 + goto free_setup; 1835 + } 1836 + 1837 + ret = cdnsp_gadget_init_endpoints(pdev); 1838 + if (ret) { 1839 + dev_err(pdev->dev, "failed to initialize endpoints\n"); 1840 + goto halt_pdev; 1841 + } 1842 + 1843 + ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget); 1844 + if (ret) { 1845 + dev_err(pdev->dev, "failed to register udc\n"); 1846 + goto free_endpoints; 1847 + } 1848 + 1849 + ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq, 1850 + cdnsp_irq_handler, 1851 + cdnsp_thread_irq_handler, IRQF_SHARED, 1852 + dev_name(pdev->dev), pdev); 1853 + if (ret) 1854 + goto del_gadget; 1855 + 1856 + return 0; 1857 + 1858 + del_gadget: 1859 + usb_del_gadget_udc(&pdev->gadget); 1860 + free_endpoints: 1861 + cdnsp_gadget_free_endpoints(pdev); 1862 + halt_pdev: 1863 + cdnsp_halt(pdev); 1864 + cdnsp_reset(pdev); 1865 + cdnsp_mem_cleanup(pdev); 1866 + free_setup: 1867 + kfree(pdev->setup_buf); 1868 + free_pdev: 1869 + kfree(pdev); 1870 + 1871 + return ret; 1872 + } 1873 + 1874 + static void cdnsp_gadget_exit(struct cdns *cdns) 1875 + { 1876 + struct cdnsp_device *pdev = cdns->gadget_dev; 1877 + 1878 + devm_free_irq(pdev->dev, cdns->dev_irq, pdev); 1879 + pm_runtime_mark_last_busy(cdns->dev); 1880 + pm_runtime_put_autosuspend(cdns->dev); 1881 + usb_del_gadget_udc(&pdev->gadget); 1882 + cdnsp_gadget_free_endpoints(pdev); 1883 + cdnsp_mem_cleanup(pdev); 1884 + kfree(pdev); 1885 + cdns->gadget_dev = NULL; 1886 + cdns_drd_gadget_off(cdns); 1887 + } 1888 + 1889 + static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup) 1890 + { 1891 + struct cdnsp_device *pdev = cdns->gadget_dev; 1892 + unsigned long flags; 1893 + 1894 + if (pdev->link_state == XDEV_U3) 1895 + return 0; 1896 + 1897 + spin_lock_irqsave(&pdev->lock, flags); 1898 + cdnsp_disconnect_gadget(pdev); 1899 + cdnsp_stop(pdev); 1900 + spin_unlock_irqrestore(&pdev->lock, flags); 1901 + 1902 + return 0; 1903 + } 1904 + 1905 + static int cdnsp_gadget_resume(struct cdns *cdns, bool hibernated) 1906 + { 1907 + struct cdnsp_device *pdev = cdns->gadget_dev; 1908 + enum usb_device_speed max_speed; 1909 + unsigned long flags; 1910 + int ret; 1911 + 1912 + if (!pdev->gadget_driver) 1913 + return 0; 1914 + 1915 + spin_lock_irqsave(&pdev->lock, flags); 1916 + max_speed = pdev->gadget_driver->max_speed; 1917 + 1918 + /* Limit speed if necessary. */ 1919 + max_speed = min(max_speed, pdev->gadget.max_speed); 1920 + 1921 + ret = cdnsp_run(pdev, max_speed); 1922 + 1923 + if (pdev->link_state == XDEV_U3) 1924 + __cdnsp_gadget_wakeup(pdev); 1925 + 1926 + spin_unlock_irqrestore(&pdev->lock, flags); 1927 + 1928 + return ret; 1929 + } 1930 + 1931 + /** 1932 + * cdnsp_gadget_init - initialize device structure 1933 + * @cdns: cdnsp instance 1934 + * 1935 + * This function initializes the gadget. 1936 + */ 1937 + int cdnsp_gadget_init(struct cdns *cdns) 1938 + { 1939 + struct cdns_role_driver *rdrv; 1940 + 1941 + rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL); 1942 + if (!rdrv) 1943 + return -ENOMEM; 1944 + 1945 + rdrv->start = __cdnsp_gadget_init; 1946 + rdrv->stop = cdnsp_gadget_exit; 1947 + rdrv->suspend = cdnsp_gadget_suspend; 1948 + rdrv->resume = cdnsp_gadget_resume; 1949 + rdrv->state = CDNS_ROLE_STATE_INACTIVE; 1950 + rdrv->name = "gadget"; 1951 + cdns->roles[USB_ROLE_DEVICE] = rdrv; 1952 + 1953 + return 0; 1954 + }
+137
drivers/usb/cdns3/cdnsp-gadget.h
··· 1460 1460 u16 test_mode; 1461 1461 }; 1462 1462 1463 + /* 1464 + * Registers should always be accessed with double word or quad word accesses. 1465 + * 1466 + * Registers with 64-bit address pointers should be written to with 1467 + * dword accesses by writing the low dword first (ptr[0]), then the high dword 1468 + * (ptr[1]) second. controller implementations that do not support 64-bit 1469 + * address pointers will ignore the high dword, and write order is irrelevant. 1470 + */ 1471 + static inline u64 cdnsp_read_64(__le64 __iomem *regs) 1472 + { 1473 + return lo_hi_readq(regs); 1474 + } 1475 + 1476 + static inline void cdnsp_write_64(const u64 val, __le64 __iomem *regs) 1477 + { 1478 + lo_hi_writeq(val, regs); 1479 + } 1480 + 1481 + /* CDNSP memory management functions. */ 1482 + void cdnsp_mem_cleanup(struct cdnsp_device *pdev); 1483 + int cdnsp_mem_init(struct cdnsp_device *pdev, gfp_t flags); 1484 + int cdnsp_setup_addressable_priv_dev(struct cdnsp_device *pdev); 1485 + void cdnsp_copy_ep0_dequeue_into_input_ctx(struct cdnsp_device *pdev); 1486 + void cdnsp_endpoint_zero(struct cdnsp_device *pdev, struct cdnsp_ep *ep); 1487 + int cdnsp_endpoint_init(struct cdnsp_device *pdev, 1488 + struct cdnsp_ep *pep, 1489 + gfp_t mem_flags); 1490 + int cdnsp_ring_expansion(struct cdnsp_device *pdev, 1491 + struct cdnsp_ring *ring, 1492 + unsigned int num_trbs, gfp_t flags); 1493 + struct cdnsp_ring *cdnsp_dma_to_transfer_ring(struct cdnsp_ep *ep, u64 address); 1494 + int cdnsp_alloc_stream_info(struct cdnsp_device *pdev, 1495 + struct cdnsp_ep *pep, 1496 + unsigned int num_stream_ctxs, 1497 + unsigned int num_streams); 1498 + int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep); 1499 + void cdnsp_free_endpoint_rings(struct cdnsp_device *pdev, struct cdnsp_ep *pep); 1500 + 1501 + /* Device controller glue. */ 1502 + int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id); 1503 + int cdnsp_halt(struct cdnsp_device *pdev); 1504 + void cdnsp_died(struct cdnsp_device *pdev); 1505 + int cdnsp_reset(struct cdnsp_device *pdev); 1506 + irqreturn_t cdnsp_irq_handler(int irq, void *priv); 1507 + int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup); 1508 + void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *usbsssp_data, 1509 + struct usb_request *req, int enable); 1510 + irqreturn_t cdnsp_thread_irq_handler(int irq, void *data); 1511 + 1512 + /* Ring, segment, TRB, and TD functions. */ 1513 + dma_addr_t cdnsp_trb_virt_to_dma(struct cdnsp_segment *seg, 1514 + union cdnsp_trb *trb); 1515 + bool cdnsp_last_trb_on_seg(struct cdnsp_segment *seg, union cdnsp_trb *trb); 1516 + bool cdnsp_last_trb_on_ring(struct cdnsp_ring *ring, 1517 + struct cdnsp_segment *seg, 1518 + union cdnsp_trb *trb); 1519 + int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev); 1520 + void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev, 1521 + union cdnsp_trb *event_ring_deq, 1522 + u8 clear_ehb); 1523 + void cdnsp_initialize_ring_info(struct cdnsp_ring *ring); 1524 + void cdnsp_ring_cmd_db(struct cdnsp_device *pdev); 1525 + void cdnsp_queue_slot_control(struct cdnsp_device *pdev, u32 trb_type); 1526 + void cdnsp_queue_address_device(struct cdnsp_device *pdev, 1527 + dma_addr_t in_ctx_ptr, 1528 + enum cdnsp_setup_dev setup); 1529 + void cdnsp_queue_stop_endpoint(struct cdnsp_device *pdev, 1530 + unsigned int ep_index); 1531 + int cdnsp_queue_ctrl_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq); 1532 + int cdnsp_queue_bulk_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq); 1533 + int cdnsp_queue_isoc_tx_prepare(struct cdnsp_device *pdev, 1534 + struct cdnsp_request *preq); 1535 + void cdnsp_queue_configure_endpoint(struct cdnsp_device *pdev, 1536 + dma_addr_t in_ctx_ptr); 1537 + void cdnsp_queue_reset_ep(struct cdnsp_device *pdev, unsigned int ep_index); 1538 + void cdnsp_queue_halt_endpoint(struct cdnsp_device *pdev, 1539 + unsigned int ep_index); 1540 + void cdnsp_queue_flush_endpoint(struct cdnsp_device *pdev, 1541 + unsigned int ep_index); 1542 + void cdnsp_force_header_wakeup(struct cdnsp_device *pdev, int intf_num); 1543 + void cdnsp_queue_reset_device(struct cdnsp_device *pdev); 1544 + void cdnsp_queue_new_dequeue_state(struct cdnsp_device *pdev, 1545 + struct cdnsp_ep *pep, 1546 + struct cdnsp_dequeue_state *deq_state); 1547 + void cdnsp_ring_doorbell_for_active_rings(struct cdnsp_device *pdev, 1548 + struct cdnsp_ep *pep); 1549 + void cdnsp_inc_deq(struct cdnsp_device *pdev, struct cdnsp_ring *ring); 1550 + void cdnsp_set_link_state(struct cdnsp_device *pdev, 1551 + __le32 __iomem *port_regs, u32 link_state); 1552 + u32 cdnsp_port_state_to_neutral(u32 state); 1553 + 1554 + /* CDNSP device controller contexts. */ 1555 + int cdnsp_enable_slot(struct cdnsp_device *pdev); 1556 + int cdnsp_disable_slot(struct cdnsp_device *pdev); 1557 + struct cdnsp_input_control_ctx 1558 + *cdnsp_get_input_control_ctx(struct cdnsp_container_ctx *ctx); 1559 + struct cdnsp_slot_ctx *cdnsp_get_slot_ctx(struct cdnsp_container_ctx *ctx); 1560 + struct cdnsp_ep_ctx *cdnsp_get_ep_ctx(struct cdnsp_container_ctx *ctx, 1561 + unsigned int ep_index); 1562 + /* CDNSP gadget interface. */ 1563 + void cdnsp_suspend_gadget(struct cdnsp_device *pdev); 1564 + void cdnsp_resume_gadget(struct cdnsp_device *pdev); 1565 + void cdnsp_disconnect_gadget(struct cdnsp_device *pdev); 1566 + void cdnsp_gadget_giveback(struct cdnsp_ep *pep, struct cdnsp_request *preq, 1567 + int status); 1568 + int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq); 1569 + int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq); 1570 + unsigned int cdnsp_port_speed(unsigned int port_status); 1571 + void cdnsp_irq_reset(struct cdnsp_device *pdev); 1572 + int cdnsp_halt_endpoint(struct cdnsp_device *pdev, 1573 + struct cdnsp_ep *pep, int value); 1574 + int cdnsp_cmd_stop_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep); 1575 + int cdnsp_cmd_flush_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep); 1576 + void cdnsp_setup_analyze(struct cdnsp_device *pdev); 1577 + int cdnsp_status_stage(struct cdnsp_device *pdev); 1578 + int cdnsp_reset_device(struct cdnsp_device *pdev); 1579 + 1580 + /** 1581 + * next_request - gets the next request on the given list 1582 + * @list: the request list to operate on 1583 + * 1584 + * Caller should take care of locking. This function return NULL or the first 1585 + * request available on list. 1586 + */ 1587 + static inline struct cdnsp_request *next_request(struct list_head *list) 1588 + { 1589 + return list_first_entry_or_null(list, struct cdnsp_request, list); 1590 + } 1591 + 1592 + #define to_cdnsp_ep(ep) (container_of(ep, struct cdnsp_ep, endpoint)) 1593 + #define gadget_to_cdnsp(g) (container_of(g, struct cdnsp_device, gadget)) 1594 + #define request_to_cdnsp_request(r) (container_of(r, struct cdnsp_request, \ 1595 + request)) 1596 + #define to_cdnsp_request(r) (container_of(r, struct cdnsp_request, request)) 1597 + int cdnsp_remove_request(struct cdnsp_device *pdev, struct cdnsp_request *preq, 1598 + struct cdnsp_ep *pep); 1599 + 1463 1600 #endif /* __LINUX_CDNSP_GADGET_H */
+1310
drivers/usb/cdns3/cdnsp-mem.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Cadence CDNSP DRD Driver. 4 + * 5 + * Copyright (C) 2020 Cadence. 6 + * 7 + * Author: Pawel Laszczak <pawell@cadence.com> 8 + * 9 + * Code based on Linux XHCI driver. 10 + * Origin: Copyright (C) 2008 Intel Corp. 11 + */ 12 + 13 + #include <linux/dma-mapping.h> 14 + #include <linux/dmapool.h> 15 + #include <linux/slab.h> 16 + #include <linux/usb.h> 17 + 18 + #include "cdnsp-gadget.h" 19 + 20 + static void cdnsp_free_stream_info(struct cdnsp_device *pdev, 21 + struct cdnsp_ep *pep); 22 + /* 23 + * Allocates a generic ring segment from the ring pool, sets the dma address, 24 + * initializes the segment to zero, and sets the private next pointer to NULL. 25 + * 26 + * "All components of all Command and Transfer TRBs shall be initialized to '0'" 27 + */ 28 + static struct cdnsp_segment *cdnsp_segment_alloc(struct cdnsp_device *pdev, 29 + unsigned int cycle_state, 30 + unsigned int max_packet, 31 + gfp_t flags) 32 + { 33 + struct cdnsp_segment *seg; 34 + dma_addr_t dma; 35 + int i; 36 + 37 + seg = kzalloc(sizeof(*seg), flags); 38 + if (!seg) 39 + return NULL; 40 + 41 + seg->trbs = dma_pool_zalloc(pdev->segment_pool, flags, &dma); 42 + if (!seg->trbs) { 43 + kfree(seg); 44 + return NULL; 45 + } 46 + 47 + if (max_packet) { 48 + seg->bounce_buf = kzalloc(max_packet, flags | GFP_DMA); 49 + if (!seg->bounce_buf) 50 + goto free_dma; 51 + } 52 + 53 + /* If the cycle state is 0, set the cycle bit to 1 for all the TRBs. */ 54 + if (cycle_state == 0) { 55 + for (i = 0; i < TRBS_PER_SEGMENT; i++) 56 + seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE); 57 + } 58 + seg->dma = dma; 59 + seg->next = NULL; 60 + 61 + return seg; 62 + 63 + free_dma: 64 + dma_pool_free(pdev->segment_pool, seg->trbs, dma); 65 + kfree(seg); 66 + 67 + return NULL; 68 + } 69 + 70 + static void cdnsp_segment_free(struct cdnsp_device *pdev, 71 + struct cdnsp_segment *seg) 72 + { 73 + if (seg->trbs) 74 + dma_pool_free(pdev->segment_pool, seg->trbs, seg->dma); 75 + 76 + kfree(seg->bounce_buf); 77 + kfree(seg); 78 + } 79 + 80 + static void cdnsp_free_segments_for_ring(struct cdnsp_device *pdev, 81 + struct cdnsp_segment *first) 82 + { 83 + struct cdnsp_segment *seg; 84 + 85 + seg = first->next; 86 + 87 + while (seg != first) { 88 + struct cdnsp_segment *next = seg->next; 89 + 90 + cdnsp_segment_free(pdev, seg); 91 + seg = next; 92 + } 93 + 94 + cdnsp_segment_free(pdev, first); 95 + } 96 + 97 + /* 98 + * Make the prev segment point to the next segment. 99 + * 100 + * Change the last TRB in the prev segment to be a Link TRB which points to the 101 + * DMA address of the next segment. The caller needs to set any Link TRB 102 + * related flags, such as End TRB, Toggle Cycle, and no snoop. 103 + */ 104 + static void cdnsp_link_segments(struct cdnsp_device *pdev, 105 + struct cdnsp_segment *prev, 106 + struct cdnsp_segment *next, 107 + enum cdnsp_ring_type type) 108 + { 109 + struct cdnsp_link_trb *link; 110 + u32 val; 111 + 112 + if (!prev || !next) 113 + return; 114 + 115 + prev->next = next; 116 + if (type != TYPE_EVENT) { 117 + link = &prev->trbs[TRBS_PER_SEGMENT - 1].link; 118 + link->segment_ptr = cpu_to_le64(next->dma); 119 + 120 + /* 121 + * Set the last TRB in the segment to have a TRB type ID 122 + * of Link TRB 123 + */ 124 + val = le32_to_cpu(link->control); 125 + val &= ~TRB_TYPE_BITMASK; 126 + val |= TRB_TYPE(TRB_LINK); 127 + link->control = cpu_to_le32(val); 128 + } 129 + } 130 + 131 + /* 132 + * Link the ring to the new segments. 133 + * Set Toggle Cycle for the new ring if needed. 134 + */ 135 + static void cdnsp_link_rings(struct cdnsp_device *pdev, 136 + struct cdnsp_ring *ring, 137 + struct cdnsp_segment *first, 138 + struct cdnsp_segment *last, 139 + unsigned int num_segs) 140 + { 141 + struct cdnsp_segment *next; 142 + 143 + if (!ring || !first || !last) 144 + return; 145 + 146 + next = ring->enq_seg->next; 147 + cdnsp_link_segments(pdev, ring->enq_seg, first, ring->type); 148 + cdnsp_link_segments(pdev, last, next, ring->type); 149 + ring->num_segs += num_segs; 150 + ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs; 151 + 152 + if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) { 153 + ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= 154 + ~cpu_to_le32(LINK_TOGGLE); 155 + last->trbs[TRBS_PER_SEGMENT - 1].link.control |= 156 + cpu_to_le32(LINK_TOGGLE); 157 + ring->last_seg = last; 158 + } 159 + } 160 + 161 + /* 162 + * We need a radix tree for mapping physical addresses of TRBs to which stream 163 + * ID they belong to. We need to do this because the device controller won't 164 + * tell us which stream ring the TRB came from. We could store the stream ID 165 + * in an event data TRB, but that doesn't help us for the cancellation case, 166 + * since the endpoint may stop before it reaches that event data TRB. 167 + * 168 + * The radix tree maps the upper portion of the TRB DMA address to a ring 169 + * segment that has the same upper portion of DMA addresses. For example, 170 + * say I have segments of size 1KB, that are always 1KB aligned. A segment may 171 + * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the 172 + * key to the stream ID is 0x43244. I can use the DMA address of the TRB to 173 + * pass the radix tree a key to get the right stream ID: 174 + * 175 + * 0x10c90fff >> 10 = 0x43243 176 + * 0x10c912c0 >> 10 = 0x43244 177 + * 0x10c91400 >> 10 = 0x43245 178 + * 179 + * Obviously, only those TRBs with DMA addresses that are within the segment 180 + * will make the radix tree return the stream ID for that ring. 181 + * 182 + * Caveats for the radix tree: 183 + * 184 + * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an 185 + * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be 186 + * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the 187 + * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit 188 + * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit 189 + * extended systems (where the DMA address can be bigger than 32-bits), 190 + * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that. 191 + */ 192 + static int cdnsp_insert_segment_mapping(struct radix_tree_root *trb_address_map, 193 + struct cdnsp_ring *ring, 194 + struct cdnsp_segment *seg, 195 + gfp_t mem_flags) 196 + { 197 + unsigned long key; 198 + int ret; 199 + 200 + key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); 201 + 202 + /* Skip any segments that were already added. */ 203 + if (radix_tree_lookup(trb_address_map, key)) 204 + return 0; 205 + 206 + ret = radix_tree_maybe_preload(mem_flags); 207 + if (ret) 208 + return ret; 209 + 210 + ret = radix_tree_insert(trb_address_map, key, ring); 211 + radix_tree_preload_end(); 212 + 213 + return ret; 214 + } 215 + 216 + static void cdnsp_remove_segment_mapping(struct radix_tree_root *trb_address_map, 217 + struct cdnsp_segment *seg) 218 + { 219 + unsigned long key; 220 + 221 + key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); 222 + if (radix_tree_lookup(trb_address_map, key)) 223 + radix_tree_delete(trb_address_map, key); 224 + } 225 + 226 + static int cdnsp_update_stream_segment_mapping(struct radix_tree_root *trb_address_map, 227 + struct cdnsp_ring *ring, 228 + struct cdnsp_segment *first_seg, 229 + struct cdnsp_segment *last_seg, 230 + gfp_t mem_flags) 231 + { 232 + struct cdnsp_segment *failed_seg; 233 + struct cdnsp_segment *seg; 234 + int ret; 235 + 236 + seg = first_seg; 237 + do { 238 + ret = cdnsp_insert_segment_mapping(trb_address_map, ring, seg, 239 + mem_flags); 240 + if (ret) 241 + goto remove_streams; 242 + if (seg == last_seg) 243 + return 0; 244 + seg = seg->next; 245 + } while (seg != first_seg); 246 + 247 + return 0; 248 + 249 + remove_streams: 250 + failed_seg = seg; 251 + seg = first_seg; 252 + do { 253 + cdnsp_remove_segment_mapping(trb_address_map, seg); 254 + if (seg == failed_seg) 255 + return ret; 256 + seg = seg->next; 257 + } while (seg != first_seg); 258 + 259 + return ret; 260 + } 261 + 262 + static void cdnsp_remove_stream_mapping(struct cdnsp_ring *ring) 263 + { 264 + struct cdnsp_segment *seg; 265 + 266 + seg = ring->first_seg; 267 + do { 268 + cdnsp_remove_segment_mapping(ring->trb_address_map, seg); 269 + seg = seg->next; 270 + } while (seg != ring->first_seg); 271 + } 272 + 273 + static int cdnsp_update_stream_mapping(struct cdnsp_ring *ring) 274 + { 275 + return cdnsp_update_stream_segment_mapping(ring->trb_address_map, ring, 276 + ring->first_seg, ring->last_seg, GFP_ATOMIC); 277 + } 278 + 279 + static void cdnsp_ring_free(struct cdnsp_device *pdev, struct cdnsp_ring *ring) 280 + { 281 + if (!ring) 282 + return; 283 + 284 + if (ring->first_seg) { 285 + if (ring->type == TYPE_STREAM) 286 + cdnsp_remove_stream_mapping(ring); 287 + 288 + cdnsp_free_segments_for_ring(pdev, ring->first_seg); 289 + } 290 + 291 + kfree(ring); 292 + } 293 + 294 + void cdnsp_initialize_ring_info(struct cdnsp_ring *ring) 295 + { 296 + ring->enqueue = ring->first_seg->trbs; 297 + ring->enq_seg = ring->first_seg; 298 + ring->dequeue = ring->enqueue; 299 + ring->deq_seg = ring->first_seg; 300 + 301 + /* 302 + * The ring is initialized to 0. The producer must write 1 to the cycle 303 + * bit to handover ownership of the TRB, so PCS = 1. The consumer must 304 + * compare CCS to the cycle bit to check ownership, so CCS = 1. 305 + * 306 + * New rings are initialized with cycle state equal to 1; if we are 307 + * handling ring expansion, set the cycle state equal to the old ring. 308 + */ 309 + ring->cycle_state = 1; 310 + 311 + /* 312 + * Each segment has a link TRB, and leave an extra TRB for SW 313 + * accounting purpose 314 + */ 315 + ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1; 316 + } 317 + 318 + /* Allocate segments and link them for a ring. */ 319 + static int cdnsp_alloc_segments_for_ring(struct cdnsp_device *pdev, 320 + struct cdnsp_segment **first, 321 + struct cdnsp_segment **last, 322 + unsigned int num_segs, 323 + unsigned int cycle_state, 324 + enum cdnsp_ring_type type, 325 + unsigned int max_packet, 326 + gfp_t flags) 327 + { 328 + struct cdnsp_segment *prev; 329 + 330 + /* Allocate first segment. */ 331 + prev = cdnsp_segment_alloc(pdev, cycle_state, max_packet, flags); 332 + if (!prev) 333 + return -ENOMEM; 334 + 335 + num_segs--; 336 + *first = prev; 337 + 338 + /* Allocate all other segments. */ 339 + while (num_segs > 0) { 340 + struct cdnsp_segment *next; 341 + 342 + next = cdnsp_segment_alloc(pdev, cycle_state, 343 + max_packet, flags); 344 + if (!next) { 345 + cdnsp_free_segments_for_ring(pdev, *first); 346 + return -ENOMEM; 347 + } 348 + 349 + cdnsp_link_segments(pdev, prev, next, type); 350 + 351 + prev = next; 352 + num_segs--; 353 + } 354 + 355 + cdnsp_link_segments(pdev, prev, *first, type); 356 + *last = prev; 357 + 358 + return 0; 359 + } 360 + 361 + /* 362 + * Create a new ring with zero or more segments. 363 + * 364 + * Link each segment together into a ring. 365 + * Set the end flag and the cycle toggle bit on the last segment. 366 + */ 367 + static struct cdnsp_ring *cdnsp_ring_alloc(struct cdnsp_device *pdev, 368 + unsigned int num_segs, 369 + enum cdnsp_ring_type type, 370 + unsigned int max_packet, 371 + gfp_t flags) 372 + { 373 + struct cdnsp_ring *ring; 374 + int ret; 375 + 376 + ring = kzalloc(sizeof *(ring), flags); 377 + if (!ring) 378 + return NULL; 379 + 380 + ring->num_segs = num_segs; 381 + ring->bounce_buf_len = max_packet; 382 + INIT_LIST_HEAD(&ring->td_list); 383 + ring->type = type; 384 + 385 + if (num_segs == 0) 386 + return ring; 387 + 388 + ret = cdnsp_alloc_segments_for_ring(pdev, &ring->first_seg, 389 + &ring->last_seg, num_segs, 390 + 1, type, max_packet, flags); 391 + if (ret) 392 + goto fail; 393 + 394 + /* Only event ring does not use link TRB. */ 395 + if (type != TYPE_EVENT) 396 + ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |= 397 + cpu_to_le32(LINK_TOGGLE); 398 + 399 + cdnsp_initialize_ring_info(ring); 400 + 401 + return ring; 402 + fail: 403 + kfree(ring); 404 + return NULL; 405 + } 406 + 407 + void cdnsp_free_endpoint_rings(struct cdnsp_device *pdev, struct cdnsp_ep *pep) 408 + { 409 + cdnsp_ring_free(pdev, pep->ring); 410 + pep->ring = NULL; 411 + cdnsp_free_stream_info(pdev, pep); 412 + } 413 + 414 + /* 415 + * Expand an existing ring. 416 + * Allocate a new ring which has same segment numbers and link the two rings. 417 + */ 418 + int cdnsp_ring_expansion(struct cdnsp_device *pdev, 419 + struct cdnsp_ring *ring, 420 + unsigned int num_trbs, 421 + gfp_t flags) 422 + { 423 + unsigned int num_segs_needed; 424 + struct cdnsp_segment *first; 425 + struct cdnsp_segment *last; 426 + unsigned int num_segs; 427 + int ret; 428 + 429 + num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) / 430 + (TRBS_PER_SEGMENT - 1); 431 + 432 + /* Allocate number of segments we needed, or double the ring size. */ 433 + num_segs = max(ring->num_segs, num_segs_needed); 434 + 435 + ret = cdnsp_alloc_segments_for_ring(pdev, &first, &last, num_segs, 436 + ring->cycle_state, ring->type, 437 + ring->bounce_buf_len, flags); 438 + if (ret) 439 + return -ENOMEM; 440 + 441 + if (ring->type == TYPE_STREAM) 442 + ret = cdnsp_update_stream_segment_mapping(ring->trb_address_map, 443 + ring, first, 444 + last, flags); 445 + 446 + if (ret) { 447 + cdnsp_free_segments_for_ring(pdev, first); 448 + 449 + return ret; 450 + } 451 + 452 + cdnsp_link_rings(pdev, ring, first, last, num_segs); 453 + 454 + return 0; 455 + } 456 + 457 + static int cdnsp_init_device_ctx(struct cdnsp_device *pdev) 458 + { 459 + int size = HCC_64BYTE_CONTEXT(pdev->hcc_params) ? 2048 : 1024; 460 + 461 + pdev->out_ctx.type = CDNSP_CTX_TYPE_DEVICE; 462 + pdev->out_ctx.size = size; 463 + pdev->out_ctx.ctx_size = CTX_SIZE(pdev->hcc_params); 464 + pdev->out_ctx.bytes = dma_pool_zalloc(pdev->device_pool, GFP_ATOMIC, 465 + &pdev->out_ctx.dma); 466 + 467 + if (!pdev->out_ctx.bytes) 468 + return -ENOMEM; 469 + 470 + pdev->in_ctx.type = CDNSP_CTX_TYPE_INPUT; 471 + pdev->in_ctx.ctx_size = pdev->out_ctx.ctx_size; 472 + pdev->in_ctx.size = size + pdev->out_ctx.ctx_size; 473 + pdev->in_ctx.bytes = dma_pool_zalloc(pdev->device_pool, GFP_ATOMIC, 474 + &pdev->in_ctx.dma); 475 + 476 + if (!pdev->in_ctx.bytes) { 477 + dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes, 478 + pdev->out_ctx.dma); 479 + return -ENOMEM; 480 + } 481 + 482 + return 0; 483 + } 484 + 485 + struct cdnsp_input_control_ctx 486 + *cdnsp_get_input_control_ctx(struct cdnsp_container_ctx *ctx) 487 + { 488 + if (ctx->type != CDNSP_CTX_TYPE_INPUT) 489 + return NULL; 490 + 491 + return (struct cdnsp_input_control_ctx *)ctx->bytes; 492 + } 493 + 494 + struct cdnsp_slot_ctx *cdnsp_get_slot_ctx(struct cdnsp_container_ctx *ctx) 495 + { 496 + if (ctx->type == CDNSP_CTX_TYPE_DEVICE) 497 + return (struct cdnsp_slot_ctx *)ctx->bytes; 498 + 499 + return (struct cdnsp_slot_ctx *)(ctx->bytes + ctx->ctx_size); 500 + } 501 + 502 + struct cdnsp_ep_ctx *cdnsp_get_ep_ctx(struct cdnsp_container_ctx *ctx, 503 + unsigned int ep_index) 504 + { 505 + /* Increment ep index by offset of start of ep ctx array. */ 506 + ep_index++; 507 + if (ctx->type == CDNSP_CTX_TYPE_INPUT) 508 + ep_index++; 509 + 510 + return (struct cdnsp_ep_ctx *)(ctx->bytes + (ep_index * ctx->ctx_size)); 511 + } 512 + 513 + static void cdnsp_free_stream_ctx(struct cdnsp_device *pdev, 514 + struct cdnsp_ep *pep) 515 + { 516 + dma_pool_free(pdev->device_pool, pep->stream_info.stream_ctx_array, 517 + pep->stream_info.ctx_array_dma); 518 + } 519 + 520 + /* The stream context array must be a power of 2. */ 521 + static struct cdnsp_stream_ctx 522 + *cdnsp_alloc_stream_ctx(struct cdnsp_device *pdev, struct cdnsp_ep *pep) 523 + { 524 + size_t size = sizeof(struct cdnsp_stream_ctx) * 525 + pep->stream_info.num_stream_ctxs; 526 + 527 + if (size > CDNSP_CTX_SIZE) 528 + return NULL; 529 + 530 + /** 531 + * Driver uses intentionally the device_pool to allocated stream 532 + * context array. Device Pool has 2048 bytes of size what gives us 533 + * 128 entries. 534 + */ 535 + return dma_pool_zalloc(pdev->device_pool, GFP_DMA32 | GFP_ATOMIC, 536 + &pep->stream_info.ctx_array_dma); 537 + } 538 + 539 + struct cdnsp_ring *cdnsp_dma_to_transfer_ring(struct cdnsp_ep *pep, u64 address) 540 + { 541 + if (pep->ep_state & EP_HAS_STREAMS) 542 + return radix_tree_lookup(&pep->stream_info.trb_address_map, 543 + address >> TRB_SEGMENT_SHIFT); 544 + 545 + return pep->ring; 546 + } 547 + 548 + /* 549 + * Change an endpoint's internal structure so it supports stream IDs. 550 + * The number of requested streams includes stream 0, which cannot be used by 551 + * driver. 552 + * 553 + * The number of stream contexts in the stream context array may be bigger than 554 + * the number of streams the driver wants to use. This is because the number of 555 + * stream context array entries must be a power of two. 556 + */ 557 + int cdnsp_alloc_stream_info(struct cdnsp_device *pdev, 558 + struct cdnsp_ep *pep, 559 + unsigned int num_stream_ctxs, 560 + unsigned int num_streams) 561 + { 562 + struct cdnsp_stream_info *stream_info; 563 + struct cdnsp_ring *cur_ring; 564 + u32 cur_stream; 565 + u64 addr; 566 + int ret; 567 + int mps; 568 + 569 + stream_info = &pep->stream_info; 570 + stream_info->num_streams = num_streams; 571 + stream_info->num_stream_ctxs = num_stream_ctxs; 572 + 573 + /* Initialize the array of virtual pointers to stream rings. */ 574 + stream_info->stream_rings = kcalloc(num_streams, 575 + sizeof(struct cdnsp_ring *), 576 + GFP_ATOMIC); 577 + if (!stream_info->stream_rings) 578 + return -ENOMEM; 579 + 580 + /* Initialize the array of DMA addresses for stream rings for the HW. */ 581 + stream_info->stream_ctx_array = cdnsp_alloc_stream_ctx(pdev, pep); 582 + if (!stream_info->stream_ctx_array) 583 + goto cleanup_stream_rings; 584 + 585 + memset(stream_info->stream_ctx_array, 0, 586 + sizeof(struct cdnsp_stream_ctx) * num_stream_ctxs); 587 + INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC); 588 + mps = usb_endpoint_maxp(pep->endpoint.desc); 589 + 590 + /* 591 + * Allocate rings for all the streams that the driver will use, 592 + * and add their segment DMA addresses to the radix tree. 593 + * Stream 0 is reserved. 594 + */ 595 + for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 596 + cur_ring = cdnsp_ring_alloc(pdev, 2, TYPE_STREAM, mps, 597 + GFP_ATOMIC); 598 + stream_info->stream_rings[cur_stream] = cur_ring; 599 + 600 + if (!cur_ring) 601 + goto cleanup_rings; 602 + 603 + cur_ring->stream_id = cur_stream; 604 + cur_ring->trb_address_map = &stream_info->trb_address_map; 605 + 606 + /* Set deq ptr, cycle bit, and stream context type. */ 607 + addr = cur_ring->first_seg->dma | SCT_FOR_CTX(SCT_PRI_TR) | 608 + cur_ring->cycle_state; 609 + 610 + stream_info->stream_ctx_array[cur_stream].stream_ring = 611 + cpu_to_le64(addr); 612 + 613 + ret = cdnsp_update_stream_mapping(cur_ring); 614 + if (ret) 615 + goto cleanup_rings; 616 + } 617 + 618 + return 0; 619 + 620 + cleanup_rings: 621 + for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 622 + cur_ring = stream_info->stream_rings[cur_stream]; 623 + if (cur_ring) { 624 + cdnsp_ring_free(pdev, cur_ring); 625 + stream_info->stream_rings[cur_stream] = NULL; 626 + } 627 + } 628 + 629 + cleanup_stream_rings: 630 + kfree(pep->stream_info.stream_rings); 631 + 632 + return -ENOMEM; 633 + } 634 + 635 + /* Frees all stream contexts associated with the endpoint. */ 636 + static void cdnsp_free_stream_info(struct cdnsp_device *pdev, 637 + struct cdnsp_ep *pep) 638 + { 639 + struct cdnsp_stream_info *stream_info = &pep->stream_info; 640 + struct cdnsp_ring *cur_ring; 641 + int cur_stream; 642 + 643 + if (!(pep->ep_state & EP_HAS_STREAMS)) 644 + return; 645 + 646 + for (cur_stream = 1; cur_stream < stream_info->num_streams; 647 + cur_stream++) { 648 + cur_ring = stream_info->stream_rings[cur_stream]; 649 + if (cur_ring) { 650 + cdnsp_ring_free(pdev, cur_ring); 651 + stream_info->stream_rings[cur_stream] = NULL; 652 + } 653 + } 654 + 655 + if (stream_info->stream_ctx_array) 656 + cdnsp_free_stream_ctx(pdev, pep); 657 + 658 + kfree(stream_info->stream_rings); 659 + pep->ep_state &= ~EP_HAS_STREAMS; 660 + } 661 + 662 + /* All the cdnsp_tds in the ring's TD list should be freed at this point.*/ 663 + static void cdnsp_free_priv_device(struct cdnsp_device *pdev) 664 + { 665 + pdev->dcbaa->dev_context_ptrs[1] = 0; 666 + 667 + cdnsp_free_endpoint_rings(pdev, &pdev->eps[0]); 668 + 669 + if (pdev->in_ctx.bytes) 670 + dma_pool_free(pdev->device_pool, pdev->in_ctx.bytes, 671 + pdev->in_ctx.dma); 672 + 673 + if (pdev->out_ctx.bytes) 674 + dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes, 675 + pdev->out_ctx.dma); 676 + 677 + pdev->in_ctx.bytes = NULL; 678 + pdev->out_ctx.bytes = NULL; 679 + } 680 + 681 + static int cdnsp_alloc_priv_device(struct cdnsp_device *pdev, gfp_t flags) 682 + { 683 + int ret = -ENOMEM; 684 + 685 + ret = cdnsp_init_device_ctx(pdev); 686 + if (ret) 687 + return ret; 688 + 689 + /* Allocate endpoint 0 ring. */ 690 + pdev->eps[0].ring = cdnsp_ring_alloc(pdev, 2, TYPE_CTRL, 0, flags); 691 + if (!pdev->eps[0].ring) 692 + goto fail; 693 + 694 + /* Point to output device context in dcbaa. */ 695 + pdev->dcbaa->dev_context_ptrs[1] = cpu_to_le64(pdev->out_ctx.dma); 696 + pdev->cmd.in_ctx = &pdev->in_ctx; 697 + 698 + return 0; 699 + fail: 700 + dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes, 701 + pdev->out_ctx.dma); 702 + dma_pool_free(pdev->device_pool, pdev->in_ctx.bytes, 703 + pdev->in_ctx.dma); 704 + 705 + return ret; 706 + } 707 + 708 + void cdnsp_copy_ep0_dequeue_into_input_ctx(struct cdnsp_device *pdev) 709 + { 710 + struct cdnsp_ep_ctx *ep0_ctx = pdev->eps[0].in_ctx; 711 + struct cdnsp_ring *ep_ring = pdev->eps[0].ring; 712 + dma_addr_t dma; 713 + 714 + dma = cdnsp_trb_virt_to_dma(ep_ring->enq_seg, ep_ring->enqueue); 715 + ep0_ctx->deq = cpu_to_le64(dma | ep_ring->cycle_state); 716 + } 717 + 718 + /* Setup an controller private device for a Set Address command. */ 719 + int cdnsp_setup_addressable_priv_dev(struct cdnsp_device *pdev) 720 + { 721 + struct cdnsp_slot_ctx *slot_ctx; 722 + struct cdnsp_ep_ctx *ep0_ctx; 723 + u32 max_packets, port; 724 + 725 + ep0_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, 0); 726 + slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx); 727 + 728 + /* Only the control endpoint is valid - one endpoint context. */ 729 + slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); 730 + 731 + switch (pdev->gadget.speed) { 732 + case USB_SPEED_SUPER_PLUS: 733 + slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP); 734 + max_packets = MAX_PACKET(512); 735 + break; 736 + case USB_SPEED_SUPER: 737 + slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS); 738 + max_packets = MAX_PACKET(512); 739 + break; 740 + case USB_SPEED_HIGH: 741 + slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS); 742 + max_packets = MAX_PACKET(64); 743 + break; 744 + case USB_SPEED_FULL: 745 + slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS); 746 + max_packets = MAX_PACKET(64); 747 + break; 748 + default: 749 + /* Speed was not set , this shouldn't happen. */ 750 + return -EINVAL; 751 + } 752 + 753 + port = DEV_PORT(pdev->active_port->port_num); 754 + slot_ctx->dev_port |= cpu_to_le32(port); 755 + slot_ctx->dev_state = (pdev->device_address & DEV_ADDR_MASK); 756 + ep0_ctx->tx_info = EP_AVG_TRB_LENGTH(0x8); 757 + ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP)); 758 + ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) | 759 + max_packets); 760 + 761 + ep0_ctx->deq = cpu_to_le64(pdev->eps[0].ring->first_seg->dma | 762 + pdev->eps[0].ring->cycle_state); 763 + 764 + return 0; 765 + } 766 + 767 + /* 768 + * Convert interval expressed as 2^(bInterval - 1) == interval into 769 + * straight exponent value 2^n == interval. 770 + */ 771 + static unsigned int cdnsp_parse_exponent_interval(struct usb_gadget *g, 772 + struct cdnsp_ep *pep) 773 + { 774 + unsigned int interval; 775 + 776 + interval = clamp_val(pep->endpoint.desc->bInterval, 1, 16) - 1; 777 + if (interval != pep->endpoint.desc->bInterval - 1) 778 + dev_warn(&g->dev, "ep %s - rounding interval to %d %sframes\n", 779 + pep->name, 1 << interval, 780 + g->speed == USB_SPEED_FULL ? "" : "micro"); 781 + 782 + /* 783 + * Full speed isoc endpoints specify interval in frames, 784 + * not microframes. We are using microframes everywhere, 785 + * so adjust accordingly. 786 + */ 787 + if (g->speed == USB_SPEED_FULL) 788 + interval += 3; /* 1 frame = 2^3 uframes */ 789 + 790 + /* Controller handles only up to 512ms (2^12). */ 791 + if (interval > 12) 792 + interval = 12; 793 + 794 + return interval; 795 + } 796 + 797 + /* 798 + * Convert bInterval expressed in microframes (in 1-255 range) to exponent of 799 + * microframes, rounded down to nearest power of 2. 800 + */ 801 + static unsigned int cdnsp_microframes_to_exponent(struct usb_gadget *g, 802 + struct cdnsp_ep *pep, 803 + unsigned int desc_interval, 804 + unsigned int min_exponent, 805 + unsigned int max_exponent) 806 + { 807 + unsigned int interval; 808 + 809 + interval = fls(desc_interval) - 1; 810 + return clamp_val(interval, min_exponent, max_exponent); 811 + } 812 + 813 + /* 814 + * Return the polling interval. 815 + * 816 + * The polling interval is expressed in "microframes". If controllers's Interval 817 + * field is set to N, it will service the endpoint every 2^(Interval)*125us. 818 + */ 819 + static unsigned int cdnsp_get_endpoint_interval(struct usb_gadget *g, 820 + struct cdnsp_ep *pep) 821 + { 822 + unsigned int interval = 0; 823 + 824 + switch (g->speed) { 825 + case USB_SPEED_HIGH: 826 + case USB_SPEED_SUPER_PLUS: 827 + case USB_SPEED_SUPER: 828 + if (usb_endpoint_xfer_int(pep->endpoint.desc) || 829 + usb_endpoint_xfer_isoc(pep->endpoint.desc)) 830 + interval = cdnsp_parse_exponent_interval(g, pep); 831 + break; 832 + case USB_SPEED_FULL: 833 + if (usb_endpoint_xfer_isoc(pep->endpoint.desc)) { 834 + interval = cdnsp_parse_exponent_interval(g, pep); 835 + } else if (usb_endpoint_xfer_int(pep->endpoint.desc)) { 836 + interval = pep->endpoint.desc->bInterval << 3; 837 + interval = cdnsp_microframes_to_exponent(g, pep, 838 + interval, 839 + 3, 10); 840 + } 841 + 842 + break; 843 + default: 844 + WARN_ON(1); 845 + } 846 + 847 + return interval; 848 + } 849 + 850 + /* 851 + * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps. 852 + * High speed endpoint descriptors can define "the number of additional 853 + * transaction opportunities per microframe", but that goes in the Max Burst 854 + * endpoint context field. 855 + */ 856 + static u32 cdnsp_get_endpoint_mult(struct usb_gadget *g, struct cdnsp_ep *pep) 857 + { 858 + if (g->speed < USB_SPEED_SUPER || 859 + !usb_endpoint_xfer_isoc(pep->endpoint.desc)) 860 + return 0; 861 + 862 + return pep->endpoint.comp_desc->bmAttributes; 863 + } 864 + 865 + static u32 cdnsp_get_endpoint_max_burst(struct usb_gadget *g, 866 + struct cdnsp_ep *pep) 867 + { 868 + /* Super speed and Plus have max burst in ep companion desc */ 869 + if (g->speed >= USB_SPEED_SUPER) 870 + return pep->endpoint.comp_desc->bMaxBurst; 871 + 872 + if (g->speed == USB_SPEED_HIGH && 873 + (usb_endpoint_xfer_isoc(pep->endpoint.desc) || 874 + usb_endpoint_xfer_int(pep->endpoint.desc))) 875 + return (usb_endpoint_maxp(pep->endpoint.desc) & 0x1800) >> 11; 876 + 877 + return 0; 878 + } 879 + 880 + static u32 cdnsp_get_endpoint_type(const struct usb_endpoint_descriptor *desc) 881 + { 882 + int in; 883 + 884 + in = usb_endpoint_dir_in(desc); 885 + 886 + switch (usb_endpoint_type(desc)) { 887 + case USB_ENDPOINT_XFER_CONTROL: 888 + return CTRL_EP; 889 + case USB_ENDPOINT_XFER_BULK: 890 + return in ? BULK_IN_EP : BULK_OUT_EP; 891 + case USB_ENDPOINT_XFER_ISOC: 892 + return in ? ISOC_IN_EP : ISOC_OUT_EP; 893 + case USB_ENDPOINT_XFER_INT: 894 + return in ? INT_IN_EP : INT_OUT_EP; 895 + } 896 + 897 + return 0; 898 + } 899 + 900 + /* 901 + * Return the maximum endpoint service interval time (ESIT) payload. 902 + * Basically, this is the maxpacket size, multiplied by the burst size 903 + * and mult size. 904 + */ 905 + static u32 cdnsp_get_max_esit_payload(struct usb_gadget *g, 906 + struct cdnsp_ep *pep) 907 + { 908 + int max_packet; 909 + int max_burst; 910 + 911 + /* Only applies for interrupt or isochronous endpoints*/ 912 + if (usb_endpoint_xfer_control(pep->endpoint.desc) || 913 + usb_endpoint_xfer_bulk(pep->endpoint.desc)) 914 + return 0; 915 + 916 + /* SuperSpeedPlus Isoc ep sending over 48k per EIST. */ 917 + if (g->speed >= USB_SPEED_SUPER_PLUS && 918 + USB_SS_SSP_ISOC_COMP(pep->endpoint.desc->bmAttributes)) 919 + return le32_to_cpu(pep->endpoint.comp_desc->wBytesPerInterval); 920 + /* SuperSpeed or SuperSpeedPlus Isoc ep with less than 48k per esit */ 921 + else if (g->speed >= USB_SPEED_SUPER) 922 + return le16_to_cpu(pep->endpoint.comp_desc->wBytesPerInterval); 923 + 924 + max_packet = usb_endpoint_maxp(pep->endpoint.desc); 925 + max_burst = usb_endpoint_maxp_mult(pep->endpoint.desc); 926 + 927 + /* A 0 in max burst means 1 transfer per ESIT */ 928 + return max_packet * max_burst; 929 + } 930 + 931 + int cdnsp_endpoint_init(struct cdnsp_device *pdev, 932 + struct cdnsp_ep *pep, 933 + gfp_t mem_flags) 934 + { 935 + enum cdnsp_ring_type ring_type; 936 + struct cdnsp_ep_ctx *ep_ctx; 937 + unsigned int err_count = 0; 938 + unsigned int avg_trb_len; 939 + unsigned int max_packet; 940 + unsigned int max_burst; 941 + unsigned int interval; 942 + u32 max_esit_payload; 943 + unsigned int mult; 944 + u32 endpoint_type; 945 + int ret; 946 + 947 + ep_ctx = pep->in_ctx; 948 + 949 + endpoint_type = cdnsp_get_endpoint_type(pep->endpoint.desc); 950 + if (!endpoint_type) 951 + return -EINVAL; 952 + 953 + ring_type = usb_endpoint_type(pep->endpoint.desc); 954 + 955 + /* 956 + * Get values to fill the endpoint context, mostly from ep descriptor. 957 + * The average TRB buffer length for bulk endpoints is unclear as we 958 + * have no clue on scatter gather list entry size. For Isoc and Int, 959 + * set it to max available. 960 + */ 961 + max_esit_payload = cdnsp_get_max_esit_payload(&pdev->gadget, pep); 962 + interval = cdnsp_get_endpoint_interval(&pdev->gadget, pep); 963 + mult = cdnsp_get_endpoint_mult(&pdev->gadget, pep); 964 + max_packet = usb_endpoint_maxp(pep->endpoint.desc); 965 + max_burst = cdnsp_get_endpoint_max_burst(&pdev->gadget, pep); 966 + avg_trb_len = max_esit_payload; 967 + 968 + /* Allow 3 retries for everything but isoc, set CErr = 3. */ 969 + if (!usb_endpoint_xfer_isoc(pep->endpoint.desc)) 970 + err_count = 3; 971 + if (usb_endpoint_xfer_bulk(pep->endpoint.desc) && 972 + pdev->gadget.speed == USB_SPEED_HIGH) 973 + max_packet = 512; 974 + /* Controller spec indicates that ctrl ep avg TRB Length should be 8. */ 975 + if (usb_endpoint_xfer_control(pep->endpoint.desc)) 976 + avg_trb_len = 8; 977 + 978 + /* Set up the endpoint ring. */ 979 + pep->ring = cdnsp_ring_alloc(pdev, 2, ring_type, max_packet, mem_flags); 980 + pep->skip = false; 981 + 982 + /* Fill the endpoint context */ 983 + ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) | 984 + EP_INTERVAL(interval) | EP_MULT(mult)); 985 + ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) | 986 + MAX_PACKET(max_packet) | MAX_BURST(max_burst) | 987 + ERROR_COUNT(err_count)); 988 + ep_ctx->deq = cpu_to_le64(pep->ring->first_seg->dma | 989 + pep->ring->cycle_state); 990 + 991 + ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) | 992 + EP_AVG_TRB_LENGTH(avg_trb_len)); 993 + 994 + if (usb_endpoint_xfer_bulk(pep->endpoint.desc) && 995 + pdev->gadget.speed > USB_SPEED_HIGH) { 996 + ret = cdnsp_alloc_streams(pdev, pep); 997 + if (ret < 0) 998 + return ret; 999 + } 1000 + 1001 + return 0; 1002 + } 1003 + 1004 + void cdnsp_endpoint_zero(struct cdnsp_device *pdev, struct cdnsp_ep *pep) 1005 + { 1006 + pep->in_ctx->ep_info = 0; 1007 + pep->in_ctx->ep_info2 = 0; 1008 + pep->in_ctx->deq = 0; 1009 + pep->in_ctx->tx_info = 0; 1010 + } 1011 + 1012 + static int cdnsp_alloc_erst(struct cdnsp_device *pdev, 1013 + struct cdnsp_ring *evt_ring, 1014 + struct cdnsp_erst *erst, 1015 + gfp_t flags) 1016 + { 1017 + struct cdnsp_erst_entry *entry; 1018 + struct cdnsp_segment *seg; 1019 + unsigned int val; 1020 + size_t size; 1021 + 1022 + size = sizeof(struct cdnsp_erst_entry) * evt_ring->num_segs; 1023 + erst->entries = dma_alloc_coherent(pdev->dev, size, 1024 + &erst->erst_dma_addr, flags); 1025 + if (!erst->entries) 1026 + return -ENOMEM; 1027 + 1028 + erst->num_entries = evt_ring->num_segs; 1029 + 1030 + seg = evt_ring->first_seg; 1031 + for (val = 0; val < evt_ring->num_segs; val++) { 1032 + entry = &erst->entries[val]; 1033 + entry->seg_addr = cpu_to_le64(seg->dma); 1034 + entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT); 1035 + entry->rsvd = 0; 1036 + seg = seg->next; 1037 + } 1038 + 1039 + return 0; 1040 + } 1041 + 1042 + static void cdnsp_free_erst(struct cdnsp_device *pdev, struct cdnsp_erst *erst) 1043 + { 1044 + size_t size = sizeof(struct cdnsp_erst_entry) * (erst->num_entries); 1045 + struct device *dev = pdev->dev; 1046 + 1047 + if (erst->entries) 1048 + dma_free_coherent(dev, size, erst->entries, 1049 + erst->erst_dma_addr); 1050 + 1051 + erst->entries = NULL; 1052 + } 1053 + 1054 + void cdnsp_mem_cleanup(struct cdnsp_device *pdev) 1055 + { 1056 + struct device *dev = pdev->dev; 1057 + 1058 + cdnsp_free_priv_device(pdev); 1059 + cdnsp_free_erst(pdev, &pdev->erst); 1060 + 1061 + if (pdev->event_ring) 1062 + cdnsp_ring_free(pdev, pdev->event_ring); 1063 + 1064 + pdev->event_ring = NULL; 1065 + 1066 + if (pdev->cmd_ring) 1067 + cdnsp_ring_free(pdev, pdev->cmd_ring); 1068 + 1069 + pdev->cmd_ring = NULL; 1070 + 1071 + dma_pool_destroy(pdev->segment_pool); 1072 + pdev->segment_pool = NULL; 1073 + dma_pool_destroy(pdev->device_pool); 1074 + pdev->device_pool = NULL; 1075 + 1076 + if (pdev->dcbaa) 1077 + dma_free_coherent(dev, sizeof(*pdev->dcbaa), 1078 + pdev->dcbaa, pdev->dcbaa->dma); 1079 + 1080 + pdev->dcbaa = NULL; 1081 + 1082 + pdev->usb2_port.exist = 0; 1083 + pdev->usb3_port.exist = 0; 1084 + pdev->usb2_port.port_num = 0; 1085 + pdev->usb3_port.port_num = 0; 1086 + pdev->active_port = NULL; 1087 + } 1088 + 1089 + static void cdnsp_set_event_deq(struct cdnsp_device *pdev) 1090 + { 1091 + dma_addr_t deq; 1092 + u64 temp; 1093 + 1094 + deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg, 1095 + pdev->event_ring->dequeue); 1096 + 1097 + /* Update controller event ring dequeue pointer */ 1098 + temp = cdnsp_read_64(&pdev->ir_set->erst_dequeue); 1099 + temp &= ERST_PTR_MASK; 1100 + 1101 + /* 1102 + * Don't clear the EHB bit (which is RW1C) because 1103 + * there might be more events to service. 1104 + */ 1105 + temp &= ~ERST_EHB; 1106 + 1107 + cdnsp_write_64(((u64)deq & (u64)~ERST_PTR_MASK) | temp, 1108 + &pdev->ir_set->erst_dequeue); 1109 + } 1110 + 1111 + static void cdnsp_add_in_port(struct cdnsp_device *pdev, 1112 + struct cdnsp_port *port, 1113 + __le32 __iomem *addr) 1114 + { 1115 + u32 temp, port_offset; 1116 + 1117 + temp = readl(addr); 1118 + port->maj_rev = CDNSP_EXT_PORT_MAJOR(temp); 1119 + port->min_rev = CDNSP_EXT_PORT_MINOR(temp); 1120 + 1121 + /* Port offset and count in the third dword.*/ 1122 + temp = readl(addr + 2); 1123 + port_offset = CDNSP_EXT_PORT_OFF(temp); 1124 + 1125 + port->port_num = port_offset; 1126 + port->exist = 1; 1127 + } 1128 + 1129 + /* 1130 + * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that 1131 + * specify what speeds each port is supposed to be. 1132 + */ 1133 + static int cdnsp_setup_port_arrays(struct cdnsp_device *pdev, gfp_t flags) 1134 + { 1135 + void __iomem *base; 1136 + u32 offset; 1137 + int i; 1138 + 1139 + base = &pdev->cap_regs->hc_capbase; 1140 + offset = cdnsp_find_next_ext_cap(base, 0, 1141 + EXT_CAP_CFG_DEV_20PORT_CAP_ID); 1142 + pdev->port20_regs = base + offset; 1143 + 1144 + offset = cdnsp_find_next_ext_cap(base, 0, D_XEC_CFG_3XPORT_CAP); 1145 + pdev->port3x_regs = base + offset; 1146 + 1147 + offset = 0; 1148 + base = &pdev->cap_regs->hc_capbase; 1149 + 1150 + /* Driver expects max 2 extended protocol capability. */ 1151 + for (i = 0; i < 2; i++) { 1152 + u32 temp; 1153 + 1154 + offset = cdnsp_find_next_ext_cap(base, offset, 1155 + EXT_CAPS_PROTOCOL); 1156 + temp = readl(base + offset); 1157 + 1158 + if (CDNSP_EXT_PORT_MAJOR(temp) == 0x03 && 1159 + !pdev->usb3_port.port_num) 1160 + cdnsp_add_in_port(pdev, &pdev->usb3_port, 1161 + base + offset); 1162 + 1163 + if (CDNSP_EXT_PORT_MAJOR(temp) == 0x02 && 1164 + !pdev->usb2_port.port_num) 1165 + cdnsp_add_in_port(pdev, &pdev->usb2_port, 1166 + base + offset); 1167 + } 1168 + 1169 + if (!pdev->usb2_port.exist || !pdev->usb3_port.exist) { 1170 + dev_err(pdev->dev, "Error: Only one port detected\n"); 1171 + return -ENODEV; 1172 + } 1173 + 1174 + pdev->usb2_port.regs = (struct cdnsp_port_regs *) 1175 + (&pdev->op_regs->port_reg_base + NUM_PORT_REGS * 1176 + (pdev->usb2_port.port_num - 1)); 1177 + 1178 + pdev->usb3_port.regs = (struct cdnsp_port_regs *) 1179 + (&pdev->op_regs->port_reg_base + NUM_PORT_REGS * 1180 + (pdev->usb3_port.port_num - 1)); 1181 + 1182 + return 0; 1183 + } 1184 + 1185 + /* 1186 + * Initialize memory for CDNSP (one-time init). 1187 + * 1188 + * Program the PAGESIZE register, initialize the device context array, create 1189 + * device contexts, set up a command ring segment, create event 1190 + * ring (one for now). 1191 + */ 1192 + int cdnsp_mem_init(struct cdnsp_device *pdev, gfp_t flags) 1193 + { 1194 + struct device *dev = pdev->dev; 1195 + int ret = -ENOMEM; 1196 + unsigned int val; 1197 + dma_addr_t dma; 1198 + u32 page_size; 1199 + u64 val_64; 1200 + 1201 + /* 1202 + * Use 4K pages, since that's common and the minimum the 1203 + * controller supports 1204 + */ 1205 + page_size = 1 << 12; 1206 + 1207 + val = readl(&pdev->op_regs->config_reg); 1208 + val |= ((val & ~MAX_DEVS) | CDNSP_DEV_MAX_SLOTS) | CONFIG_U3E; 1209 + writel(val, &pdev->op_regs->config_reg); 1210 + 1211 + /* 1212 + * Doorbell array must be physically contiguous 1213 + * and 64-byte (cache line) aligned. 1214 + */ 1215 + pdev->dcbaa = dma_alloc_coherent(dev, sizeof(*pdev->dcbaa), 1216 + &dma, GFP_KERNEL); 1217 + if (!pdev->dcbaa) 1218 + goto mem_init_fail; 1219 + 1220 + memset(pdev->dcbaa, 0, sizeof(*pdev->dcbaa)); 1221 + pdev->dcbaa->dma = dma; 1222 + 1223 + cdnsp_write_64(dma, &pdev->op_regs->dcbaa_ptr); 1224 + 1225 + /* 1226 + * Initialize the ring segment pool. The ring must be a contiguous 1227 + * structure comprised of TRBs. The TRBs must be 16 byte aligned, 1228 + * however, the command ring segment needs 64-byte aligned segments 1229 + * and our use of dma addresses in the trb_address_map radix tree needs 1230 + * TRB_SEGMENT_SIZE alignment, so driver pick the greater alignment 1231 + * need. 1232 + */ 1233 + pdev->segment_pool = dma_pool_create("CDNSP ring segments", dev, 1234 + TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, 1235 + page_size); 1236 + 1237 + pdev->device_pool = dma_pool_create("CDNSP input/output contexts", dev, 1238 + CDNSP_CTX_SIZE, 64, page_size); 1239 + 1240 + if (!pdev->segment_pool || !pdev->device_pool) 1241 + goto mem_init_fail; 1242 + 1243 + /* Set up the command ring to have one segments for now. */ 1244 + pdev->cmd_ring = cdnsp_ring_alloc(pdev, 1, TYPE_COMMAND, 0, flags); 1245 + if (!pdev->cmd_ring) 1246 + goto mem_init_fail; 1247 + 1248 + /* Set the address in the Command Ring Control register */ 1249 + val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring); 1250 + val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) | 1251 + (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) | 1252 + pdev->cmd_ring->cycle_state; 1253 + cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring); 1254 + 1255 + val = readl(&pdev->cap_regs->db_off); 1256 + val &= DBOFF_MASK; 1257 + pdev->dba = (void __iomem *)pdev->cap_regs + val; 1258 + 1259 + /* Set ir_set to interrupt register set 0 */ 1260 + pdev->ir_set = &pdev->run_regs->ir_set[0]; 1261 + 1262 + /* 1263 + * Event ring setup: Allocate a normal ring, but also setup 1264 + * the event ring segment table (ERST). 1265 + */ 1266 + pdev->event_ring = cdnsp_ring_alloc(pdev, ERST_NUM_SEGS, TYPE_EVENT, 1267 + 0, flags); 1268 + if (!pdev->event_ring) 1269 + goto mem_init_fail; 1270 + 1271 + ret = cdnsp_alloc_erst(pdev, pdev->event_ring, &pdev->erst, flags); 1272 + if (ret) 1273 + goto mem_init_fail; 1274 + 1275 + /* Set ERST count with the number of entries in the segment table. */ 1276 + val = readl(&pdev->ir_set->erst_size); 1277 + val &= ERST_SIZE_MASK; 1278 + val |= ERST_NUM_SEGS; 1279 + writel(val, &pdev->ir_set->erst_size); 1280 + 1281 + /* Set the segment table base address. */ 1282 + val_64 = cdnsp_read_64(&pdev->ir_set->erst_base); 1283 + val_64 &= ERST_PTR_MASK; 1284 + val_64 |= (pdev->erst.erst_dma_addr & (u64)~ERST_PTR_MASK); 1285 + cdnsp_write_64(val_64, &pdev->ir_set->erst_base); 1286 + 1287 + /* Set the event ring dequeue address. */ 1288 + cdnsp_set_event_deq(pdev); 1289 + 1290 + ret = cdnsp_setup_port_arrays(pdev, flags); 1291 + if (ret) 1292 + goto mem_init_fail; 1293 + 1294 + ret = cdnsp_alloc_priv_device(pdev, GFP_ATOMIC); 1295 + if (ret) { 1296 + dev_err(pdev->dev, 1297 + "Could not allocate cdnsp_device data structures\n"); 1298 + goto mem_init_fail; 1299 + } 1300 + 1301 + return 0; 1302 + 1303 + mem_init_fail: 1304 + dev_err(pdev->dev, "Couldn't initialize memory\n"); 1305 + cdnsp_halt(pdev); 1306 + cdnsp_reset(pdev); 1307 + cdnsp_mem_cleanup(pdev); 1308 + 1309 + return ret; 1310 + }
+254
drivers/usb/cdns3/cdnsp-pci.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Cadence PCI Glue driver. 4 + * 5 + * Copyright (C) 2019 Cadence. 6 + * 7 + * Author: Pawel Laszczak <pawell@cadence.com> 8 + * 9 + */ 10 + 11 + #include <linux/platform_device.h> 12 + #include <linux/dma-mapping.h> 13 + #include <linux/kernel.h> 14 + #include <linux/module.h> 15 + #include <linux/slab.h> 16 + #include <linux/pci.h> 17 + 18 + #include "core.h" 19 + #include "gadget-export.h" 20 + 21 + #define PCI_BAR_HOST 0 22 + #define PCI_BAR_OTG 0 23 + #define PCI_BAR_DEV 2 24 + 25 + #define PCI_DEV_FN_HOST_DEVICE 0 26 + #define PCI_DEV_FN_OTG 1 27 + 28 + #define PCI_DRIVER_NAME "cdns-pci-usbssp" 29 + #define PLAT_DRIVER_NAME "cdns-usbssp" 30 + 31 + #define CDNS_VENDOR_ID 0x17cd 32 + #define CDNS_DEVICE_ID 0x0100 33 + #define CDNS_DRD_IF (PCI_CLASS_SERIAL_USB << 8 | 0x80) 34 + 35 + static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev) 36 + { 37 + struct pci_dev *func; 38 + 39 + /* 40 + * Gets the second function. 41 + * It's little tricky, but this platform has two function. 42 + * The fist keeps resources for Host/Device while the second 43 + * keeps resources for DRD/OTG. 44 + */ 45 + func = pci_get_device(pdev->vendor, pdev->device, NULL); 46 + if (!func) 47 + return NULL; 48 + 49 + if (func->devfn == pdev->devfn) { 50 + func = pci_get_device(pdev->vendor, pdev->device, func); 51 + if (!func) 52 + return NULL; 53 + } 54 + 55 + return func; 56 + } 57 + 58 + static int cdnsp_pci_probe(struct pci_dev *pdev, 59 + const struct pci_device_id *id) 60 + { 61 + struct device *dev = &pdev->dev; 62 + struct pci_dev *func; 63 + struct resource *res; 64 + struct cdns *cdnsp; 65 + int ret; 66 + 67 + /* 68 + * For GADGET/HOST PCI (devfn) function number is 0, 69 + * for OTG PCI (devfn) function number is 1. 70 + */ 71 + if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE && 72 + pdev->devfn != PCI_DEV_FN_OTG)) 73 + return -EINVAL; 74 + 75 + func = cdnsp_get_second_fun(pdev); 76 + if (!func) 77 + return -EINVAL; 78 + 79 + if (func->class == PCI_CLASS_SERIAL_USB_XHCI || 80 + pdev->class == PCI_CLASS_SERIAL_USB_XHCI) { 81 + ret = -EINVAL; 82 + goto put_pci; 83 + } 84 + 85 + ret = pcim_enable_device(pdev); 86 + if (ret) { 87 + dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret); 88 + goto put_pci; 89 + } 90 + 91 + pci_set_master(pdev); 92 + if (pci_is_enabled(func)) { 93 + cdnsp = pci_get_drvdata(func); 94 + } else { 95 + cdnsp = kzalloc(sizeof(*cdnsp), GFP_KERNEL); 96 + if (!cdnsp) { 97 + ret = -ENOMEM; 98 + goto disable_pci; 99 + } 100 + } 101 + 102 + /* For GADGET device function number is 0. */ 103 + if (pdev->devfn == 0) { 104 + resource_size_t rsrc_start, rsrc_len; 105 + 106 + /* Function 0: host(BAR_0) + device(BAR_1).*/ 107 + dev_dbg(dev, "Initialize resources\n"); 108 + rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV); 109 + rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV); 110 + res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev"); 111 + if (!res) { 112 + dev_dbg(dev, "controller already in use\n"); 113 + ret = -EBUSY; 114 + goto free_cdnsp; 115 + } 116 + 117 + cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len); 118 + if (!cdnsp->dev_regs) { 119 + dev_dbg(dev, "error mapping memory\n"); 120 + ret = -EFAULT; 121 + goto free_cdnsp; 122 + } 123 + 124 + cdnsp->dev_irq = pdev->irq; 125 + dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n", 126 + &rsrc_start); 127 + 128 + res = &cdnsp->xhci_res[0]; 129 + res->start = pci_resource_start(pdev, PCI_BAR_HOST); 130 + res->end = pci_resource_end(pdev, PCI_BAR_HOST); 131 + res->name = "xhci"; 132 + res->flags = IORESOURCE_MEM; 133 + dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n", 134 + &res->start); 135 + 136 + /* Interrupt for XHCI, */ 137 + res = &cdnsp->xhci_res[1]; 138 + res->start = pdev->irq; 139 + res->name = "host"; 140 + res->flags = IORESOURCE_IRQ; 141 + } else { 142 + res = &cdnsp->otg_res; 143 + res->start = pci_resource_start(pdev, PCI_BAR_OTG); 144 + res->end = pci_resource_end(pdev, PCI_BAR_OTG); 145 + res->name = "otg"; 146 + res->flags = IORESOURCE_MEM; 147 + dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n", 148 + &res->start); 149 + 150 + /* Interrupt for OTG/DRD. */ 151 + cdnsp->otg_irq = pdev->irq; 152 + } 153 + 154 + if (pci_is_enabled(func)) { 155 + cdnsp->dev = dev; 156 + cdnsp->gadget_init = cdnsp_gadget_init; 157 + 158 + ret = cdns_init(cdnsp); 159 + if (ret) 160 + goto free_cdnsp; 161 + } 162 + 163 + pci_set_drvdata(pdev, cdnsp); 164 + 165 + device_wakeup_enable(&pdev->dev); 166 + if (pci_dev_run_wake(pdev)) 167 + pm_runtime_put_noidle(&pdev->dev); 168 + 169 + return 0; 170 + 171 + free_cdnsp: 172 + if (!pci_is_enabled(func)) 173 + kfree(cdnsp); 174 + 175 + disable_pci: 176 + pci_disable_device(pdev); 177 + 178 + put_pci: 179 + pci_dev_put(func); 180 + 181 + return ret; 182 + } 183 + 184 + static void cdnsp_pci_remove(struct pci_dev *pdev) 185 + { 186 + struct cdns *cdnsp; 187 + struct pci_dev *func; 188 + 189 + func = cdnsp_get_second_fun(pdev); 190 + cdnsp = (struct cdns *)pci_get_drvdata(pdev); 191 + 192 + if (pci_dev_run_wake(pdev)) 193 + pm_runtime_get_noresume(&pdev->dev); 194 + 195 + if (!pci_is_enabled(func)) { 196 + kfree(cdnsp); 197 + goto pci_put; 198 + } 199 + 200 + cdns_remove(cdnsp); 201 + 202 + pci_put: 203 + pci_dev_put(func); 204 + } 205 + 206 + static int __maybe_unused cdnsp_pci_suspend(struct device *dev) 207 + { 208 + struct cdns *cdns = dev_get_drvdata(dev); 209 + 210 + return cdns_suspend(cdns); 211 + } 212 + 213 + static int __maybe_unused cdnsp_pci_resume(struct device *dev) 214 + { 215 + struct cdns *cdns = dev_get_drvdata(dev); 216 + unsigned long flags; 217 + int ret; 218 + 219 + spin_lock_irqsave(&cdns->lock, flags); 220 + ret = cdns_resume(cdns, 1); 221 + spin_unlock_irqrestore(&cdns->lock, flags); 222 + 223 + return ret; 224 + } 225 + 226 + static const struct dev_pm_ops cdnsp_pci_pm_ops = { 227 + SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume) 228 + }; 229 + 230 + static const struct pci_device_id cdnsp_pci_ids[] = { 231 + { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 232 + PCI_CLASS_SERIAL_USB_DEVICE, PCI_ANY_ID }, 233 + { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 234 + CDNS_DRD_IF, PCI_ANY_ID }, 235 + { 0, } 236 + }; 237 + 238 + static struct pci_driver cdnsp_pci_driver = { 239 + .name = "cdnsp-pci", 240 + .id_table = &cdnsp_pci_ids[0], 241 + .probe = cdnsp_pci_probe, 242 + .remove = cdnsp_pci_remove, 243 + .driver = { 244 + .pm = &cdnsp_pci_pm_ops, 245 + } 246 + }; 247 + 248 + module_pci_driver(cdnsp_pci_driver); 249 + MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids); 250 + 251 + MODULE_ALIAS("pci:cdnsp"); 252 + MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>"); 253 + MODULE_LICENSE("GPL v2"); 254 + MODULE_DESCRIPTION("Cadence CDNSP PCI driver");
+2376
drivers/usb/cdns3/cdnsp-ring.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Cadence CDNSP DRD Driver. 4 + * 5 + * Copyright (C) 2020 Cadence. 6 + * 7 + * Author: Pawel Laszczak <pawell@cadence.com> 8 + * 9 + * Code based on Linux XHCI driver. 10 + * Origin: Copyright (C) 2008 Intel Corp 11 + */ 12 + 13 + /* 14 + * Ring initialization rules: 15 + * 1. Each segment is initialized to zero, except for link TRBs. 16 + * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or 17 + * Consumer Cycle State (CCS), depending on ring function. 18 + * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment. 19 + * 20 + * Ring behavior rules: 21 + * 1. A ring is empty if enqueue == dequeue. This means there will always be at 22 + * least one free TRB in the ring. This is useful if you want to turn that 23 + * into a link TRB and expand the ring. 24 + * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a 25 + * link TRB, then load the pointer with the address in the link TRB. If the 26 + * link TRB had its toggle bit set, you may need to update the ring cycle 27 + * state (see cycle bit rules). You may have to do this multiple times 28 + * until you reach a non-link TRB. 29 + * 3. A ring is full if enqueue++ (for the definition of increment above) 30 + * equals the dequeue pointer. 31 + * 32 + * Cycle bit rules: 33 + * 1. When a consumer increments a dequeue pointer and encounters a toggle bit 34 + * in a link TRB, it must toggle the ring cycle state. 35 + * 2. When a producer increments an enqueue pointer and encounters a toggle bit 36 + * in a link TRB, it must toggle the ring cycle state. 37 + * 38 + * Producer rules: 39 + * 1. Check if ring is full before you enqueue. 40 + * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing. 41 + * Update enqueue pointer between each write (which may update the ring 42 + * cycle state). 43 + * 3. Notify consumer. If SW is producer, it rings the doorbell for command 44 + * and endpoint rings. If controller is the producer for the event ring, 45 + * and it generates an interrupt according to interrupt modulation rules. 46 + * 47 + * Consumer rules: 48 + * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state, 49 + * the TRB is owned by the consumer. 50 + * 2. Update dequeue pointer (which may update the ring cycle state) and 51 + * continue processing TRBs until you reach a TRB which is not owned by you. 52 + * 3. Notify the producer. SW is the consumer for the event ring, and it 53 + * updates event ring dequeue pointer. Controller is the consumer for the 54 + * command and endpoint rings; it generates events on the event ring 55 + * for these. 56 + */ 57 + 58 + #include <linux/scatterlist.h> 59 + #include <linux/dma-mapping.h> 60 + #include <linux/delay.h> 61 + #include <linux/slab.h> 62 + #include <linux/irq.h> 63 + 64 + #include "cdnsp-gadget.h" 65 + 66 + /* 67 + * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA 68 + * address of the TRB. 69 + */ 70 + dma_addr_t cdnsp_trb_virt_to_dma(struct cdnsp_segment *seg, 71 + union cdnsp_trb *trb) 72 + { 73 + unsigned long segment_offset = trb - seg->trbs; 74 + 75 + if (trb < seg->trbs || segment_offset >= TRBS_PER_SEGMENT) 76 + return 0; 77 + 78 + return seg->dma + (segment_offset * sizeof(*trb)); 79 + } 80 + 81 + static bool cdnsp_trb_is_noop(union cdnsp_trb *trb) 82 + { 83 + return TRB_TYPE_NOOP_LE32(trb->generic.field[3]); 84 + } 85 + 86 + static bool cdnsp_trb_is_link(union cdnsp_trb *trb) 87 + { 88 + return TRB_TYPE_LINK_LE32(trb->link.control); 89 + } 90 + 91 + bool cdnsp_last_trb_on_seg(struct cdnsp_segment *seg, union cdnsp_trb *trb) 92 + { 93 + return trb == &seg->trbs[TRBS_PER_SEGMENT - 1]; 94 + } 95 + 96 + bool cdnsp_last_trb_on_ring(struct cdnsp_ring *ring, 97 + struct cdnsp_segment *seg, 98 + union cdnsp_trb *trb) 99 + { 100 + return cdnsp_last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg); 101 + } 102 + 103 + static bool cdnsp_link_trb_toggles_cycle(union cdnsp_trb *trb) 104 + { 105 + return le32_to_cpu(trb->link.control) & LINK_TOGGLE; 106 + } 107 + 108 + static void cdnsp_trb_to_noop(union cdnsp_trb *trb, u32 noop_type) 109 + { 110 + if (cdnsp_trb_is_link(trb)) { 111 + /* Unchain chained link TRBs. */ 112 + trb->link.control &= cpu_to_le32(~TRB_CHAIN); 113 + } else { 114 + trb->generic.field[0] = 0; 115 + trb->generic.field[1] = 0; 116 + trb->generic.field[2] = 0; 117 + /* Preserve only the cycle bit of this TRB. */ 118 + trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); 119 + trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type)); 120 + } 121 + } 122 + 123 + /* 124 + * Updates trb to point to the next TRB in the ring, and updates seg if the next 125 + * TRB is in a new segment. This does not skip over link TRBs, and it does not 126 + * effect the ring dequeue or enqueue pointers. 127 + */ 128 + static void cdnsp_next_trb(struct cdnsp_device *pdev, 129 + struct cdnsp_ring *ring, 130 + struct cdnsp_segment **seg, 131 + union cdnsp_trb **trb) 132 + { 133 + if (cdnsp_trb_is_link(*trb)) { 134 + *seg = (*seg)->next; 135 + *trb = ((*seg)->trbs); 136 + } else { 137 + (*trb)++; 138 + } 139 + } 140 + 141 + /* 142 + * See Cycle bit rules. SW is the consumer for the event ring only. 143 + * Don't make a ring full of link TRBs. That would be dumb and this would loop. 144 + */ 145 + void cdnsp_inc_deq(struct cdnsp_device *pdev, struct cdnsp_ring *ring) 146 + { 147 + /* event ring doesn't have link trbs, check for last trb. */ 148 + if (ring->type == TYPE_EVENT) { 149 + if (!cdnsp_last_trb_on_seg(ring->deq_seg, ring->dequeue)) { 150 + ring->dequeue++; 151 + return; 152 + } 153 + 154 + if (cdnsp_last_trb_on_ring(ring, ring->deq_seg, ring->dequeue)) 155 + ring->cycle_state ^= 1; 156 + 157 + ring->deq_seg = ring->deq_seg->next; 158 + ring->dequeue = ring->deq_seg->trbs; 159 + return; 160 + } 161 + 162 + /* All other rings have link trbs. */ 163 + if (!cdnsp_trb_is_link(ring->dequeue)) { 164 + ring->dequeue++; 165 + ring->num_trbs_free++; 166 + } 167 + while (cdnsp_trb_is_link(ring->dequeue)) { 168 + ring->deq_seg = ring->deq_seg->next; 169 + ring->dequeue = ring->deq_seg->trbs; 170 + } 171 + } 172 + 173 + /* 174 + * See Cycle bit rules. SW is the consumer for the event ring only. 175 + * Don't make a ring full of link TRBs. That would be dumb and this would loop. 176 + * 177 + * If we've just enqueued a TRB that is in the middle of a TD (meaning the 178 + * chain bit is set), then set the chain bit in all the following link TRBs. 179 + * If we've enqueued the last TRB in a TD, make sure the following link TRBs 180 + * have their chain bit cleared (so that each Link TRB is a separate TD). 181 + * 182 + * @more_trbs_coming: Will you enqueue more TRBs before ringing the doorbell. 183 + */ 184 + static void cdnsp_inc_enq(struct cdnsp_device *pdev, 185 + struct cdnsp_ring *ring, 186 + bool more_trbs_coming) 187 + { 188 + union cdnsp_trb *next; 189 + u32 chain; 190 + 191 + chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; 192 + 193 + /* If this is not event ring, there is one less usable TRB. */ 194 + if (!cdnsp_trb_is_link(ring->enqueue)) 195 + ring->num_trbs_free--; 196 + next = ++(ring->enqueue); 197 + 198 + /* Update the dequeue pointer further if that was a link TRB */ 199 + while (cdnsp_trb_is_link(next)) { 200 + /* 201 + * If the caller doesn't plan on enqueuing more TDs before 202 + * ringing the doorbell, then we don't want to give the link TRB 203 + * to the hardware just yet. We'll give the link TRB back in 204 + * cdnsp_prepare_ring() just before we enqueue the TD at the 205 + * top of the ring. 206 + */ 207 + if (!chain && !more_trbs_coming) 208 + break; 209 + 210 + next->link.control &= cpu_to_le32(~TRB_CHAIN); 211 + next->link.control |= cpu_to_le32(chain); 212 + 213 + /* Give this link TRB to the hardware */ 214 + wmb(); 215 + next->link.control ^= cpu_to_le32(TRB_CYCLE); 216 + 217 + /* Toggle the cycle bit after the last ring segment. */ 218 + if (cdnsp_link_trb_toggles_cycle(next)) 219 + ring->cycle_state ^= 1; 220 + 221 + ring->enq_seg = ring->enq_seg->next; 222 + ring->enqueue = ring->enq_seg->trbs; 223 + next = ring->enqueue; 224 + } 225 + } 226 + 227 + /* 228 + * Check to see if there's room to enqueue num_trbs on the ring and make sure 229 + * enqueue pointer will not advance into dequeue segment. 230 + */ 231 + static bool cdnsp_room_on_ring(struct cdnsp_device *pdev, 232 + struct cdnsp_ring *ring, 233 + unsigned int num_trbs) 234 + { 235 + int num_trbs_in_deq_seg; 236 + 237 + if (ring->num_trbs_free < num_trbs) 238 + return false; 239 + 240 + if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) { 241 + num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs; 242 + 243 + if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg) 244 + return false; 245 + } 246 + 247 + return true; 248 + } 249 + 250 + /* 251 + * Workaround for L1: controller has issue with resuming from L1 after 252 + * setting doorbell for endpoint during L1 state. This function forces 253 + * resume signal in such case. 254 + */ 255 + static void cdnsp_force_l0_go(struct cdnsp_device *pdev) 256 + { 257 + if (pdev->active_port == &pdev->usb2_port && pdev->gadget.lpm_capable) 258 + cdnsp_set_link_state(pdev, &pdev->active_port->regs->portsc, XDEV_U0); 259 + } 260 + 261 + /* Ring the doorbell after placing a command on the ring. */ 262 + void cdnsp_ring_cmd_db(struct cdnsp_device *pdev) 263 + { 264 + writel(DB_VALUE_CMD, &pdev->dba->cmd_db); 265 + } 266 + 267 + /* 268 + * Ring the doorbell after placing a transfer on the ring. 269 + * Returns true if doorbell was set, otherwise false. 270 + */ 271 + static bool cdnsp_ring_ep_doorbell(struct cdnsp_device *pdev, 272 + struct cdnsp_ep *pep, 273 + unsigned int stream_id) 274 + { 275 + __le32 __iomem *reg_addr = &pdev->dba->ep_db; 276 + unsigned int ep_state = pep->ep_state; 277 + unsigned int db_value; 278 + 279 + /* 280 + * Don't ring the doorbell for this endpoint if endpoint is halted or 281 + * disabled. 282 + */ 283 + if (ep_state & EP_HALTED || !(ep_state & EP_ENABLED)) 284 + return false; 285 + 286 + /* For stream capable endpoints driver can ring doorbell only twice. */ 287 + if (pep->ep_state & EP_HAS_STREAMS) { 288 + if (pep->stream_info.drbls_count >= 2) 289 + return false; 290 + 291 + pep->stream_info.drbls_count++; 292 + } 293 + 294 + pep->ep_state &= ~EP_STOPPED; 295 + 296 + if (pep->idx == 0 && pdev->ep0_stage == CDNSP_DATA_STAGE && 297 + !pdev->ep0_expect_in) 298 + db_value = DB_VALUE_EP0_OUT(pep->idx, stream_id); 299 + else 300 + db_value = DB_VALUE(pep->idx, stream_id); 301 + 302 + writel(db_value, reg_addr); 303 + 304 + cdnsp_force_l0_go(pdev); 305 + 306 + /* Doorbell was set. */ 307 + return true; 308 + } 309 + 310 + /* 311 + * Get the right ring for the given pep and stream_id. 312 + * If the endpoint supports streams, boundary check the USB request's stream ID. 313 + * If the endpoint doesn't support streams, return the singular endpoint ring. 314 + */ 315 + static struct cdnsp_ring *cdnsp_get_transfer_ring(struct cdnsp_device *pdev, 316 + struct cdnsp_ep *pep, 317 + unsigned int stream_id) 318 + { 319 + if (!(pep->ep_state & EP_HAS_STREAMS)) 320 + return pep->ring; 321 + 322 + if (stream_id == 0 || stream_id >= pep->stream_info.num_streams) { 323 + dev_err(pdev->dev, "ERR: %s ring doesn't exist for SID: %d.\n", 324 + pep->name, stream_id); 325 + return NULL; 326 + } 327 + 328 + return pep->stream_info.stream_rings[stream_id]; 329 + } 330 + 331 + static struct cdnsp_ring * 332 + cdnsp_request_to_transfer_ring(struct cdnsp_device *pdev, 333 + struct cdnsp_request *preq) 334 + { 335 + return cdnsp_get_transfer_ring(pdev, preq->pep, 336 + preq->request.stream_id); 337 + } 338 + 339 + /* Ring the doorbell for any rings with pending requests. */ 340 + void cdnsp_ring_doorbell_for_active_rings(struct cdnsp_device *pdev, 341 + struct cdnsp_ep *pep) 342 + { 343 + struct cdnsp_stream_info *stream_info; 344 + unsigned int stream_id; 345 + int ret; 346 + 347 + if (pep->ep_state & EP_DIS_IN_RROGRESS) 348 + return; 349 + 350 + /* A ring has pending Request if its TD list is not empty. */ 351 + if (!(pep->ep_state & EP_HAS_STREAMS) && pep->number) { 352 + if (pep->ring && !list_empty(&pep->ring->td_list)) 353 + cdnsp_ring_ep_doorbell(pdev, pep, 0); 354 + return; 355 + } 356 + 357 + stream_info = &pep->stream_info; 358 + 359 + for (stream_id = 1; stream_id < stream_info->num_streams; stream_id++) { 360 + struct cdnsp_td *td, *td_temp; 361 + struct cdnsp_ring *ep_ring; 362 + 363 + if (stream_info->drbls_count >= 2) 364 + return; 365 + 366 + ep_ring = cdnsp_get_transfer_ring(pdev, pep, stream_id); 367 + if (!ep_ring) 368 + continue; 369 + 370 + if (!ep_ring->stream_active || ep_ring->stream_rejected) 371 + continue; 372 + 373 + list_for_each_entry_safe(td, td_temp, &ep_ring->td_list, 374 + td_list) { 375 + if (td->drbl) 376 + continue; 377 + 378 + ret = cdnsp_ring_ep_doorbell(pdev, pep, stream_id); 379 + if (ret) 380 + td->drbl = 1; 381 + } 382 + } 383 + } 384 + 385 + /* 386 + * Get the hw dequeue pointer controller stopped on, either directly from the 387 + * endpoint context, or if streams are in use from the stream context. 388 + * The returned hw_dequeue contains the lowest four bits with cycle state 389 + * and possible stream context type. 390 + */ 391 + static u64 cdnsp_get_hw_deq(struct cdnsp_device *pdev, 392 + unsigned int ep_index, 393 + unsigned int stream_id) 394 + { 395 + struct cdnsp_stream_ctx *st_ctx; 396 + struct cdnsp_ep *pep; 397 + 398 + pep = &pdev->eps[stream_id]; 399 + 400 + if (pep->ep_state & EP_HAS_STREAMS) { 401 + st_ctx = &pep->stream_info.stream_ctx_array[stream_id]; 402 + return le64_to_cpu(st_ctx->stream_ring); 403 + } 404 + 405 + return le64_to_cpu(pep->out_ctx->deq); 406 + } 407 + 408 + /* 409 + * Move the controller endpoint ring dequeue pointer past cur_td. 410 + * Record the new state of the controller endpoint ring dequeue segment, 411 + * dequeue pointer, and new consumer cycle state in state. 412 + * Update internal representation of the ring's dequeue pointer. 413 + * 414 + * We do this in three jumps: 415 + * - First we update our new ring state to be the same as when the 416 + * controller stopped. 417 + * - Then we traverse the ring to find the segment that contains 418 + * the last TRB in the TD. We toggle the controller new cycle state 419 + * when we pass any link TRBs with the toggle cycle bit set. 420 + * - Finally we move the dequeue state one TRB further, toggling the cycle bit 421 + * if we've moved it past a link TRB with the toggle cycle bit set. 422 + */ 423 + static void cdnsp_find_new_dequeue_state(struct cdnsp_device *pdev, 424 + struct cdnsp_ep *pep, 425 + unsigned int stream_id, 426 + struct cdnsp_td *cur_td, 427 + struct cdnsp_dequeue_state *state) 428 + { 429 + bool td_last_trb_found = false; 430 + struct cdnsp_segment *new_seg; 431 + struct cdnsp_ring *ep_ring; 432 + union cdnsp_trb *new_deq; 433 + bool cycle_found = false; 434 + u64 hw_dequeue; 435 + 436 + ep_ring = cdnsp_get_transfer_ring(pdev, pep, stream_id); 437 + if (!ep_ring) 438 + return; 439 + 440 + /* 441 + * Dig out the cycle state saved by the controller during the 442 + * stop endpoint command. 443 + */ 444 + hw_dequeue = cdnsp_get_hw_deq(pdev, pep->idx, stream_id); 445 + new_seg = ep_ring->deq_seg; 446 + new_deq = ep_ring->dequeue; 447 + state->new_cycle_state = hw_dequeue & 0x1; 448 + state->stream_id = stream_id; 449 + 450 + /* 451 + * We want to find the pointer, segment and cycle state of the new trb 452 + * (the one after current TD's last_trb). We know the cycle state at 453 + * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are 454 + * found. 455 + */ 456 + do { 457 + if (!cycle_found && cdnsp_trb_virt_to_dma(new_seg, new_deq) 458 + == (dma_addr_t)(hw_dequeue & ~0xf)) { 459 + cycle_found = true; 460 + 461 + if (td_last_trb_found) 462 + break; 463 + } 464 + 465 + if (new_deq == cur_td->last_trb) 466 + td_last_trb_found = true; 467 + 468 + if (cycle_found && cdnsp_trb_is_link(new_deq) && 469 + cdnsp_link_trb_toggles_cycle(new_deq)) 470 + state->new_cycle_state ^= 0x1; 471 + 472 + cdnsp_next_trb(pdev, ep_ring, &new_seg, &new_deq); 473 + 474 + /* Search wrapped around, bail out. */ 475 + if (new_deq == pep->ring->dequeue) { 476 + dev_err(pdev->dev, 477 + "Error: Failed finding new dequeue state\n"); 478 + state->new_deq_seg = NULL; 479 + state->new_deq_ptr = NULL; 480 + return; 481 + } 482 + 483 + } while (!cycle_found || !td_last_trb_found); 484 + 485 + state->new_deq_seg = new_seg; 486 + state->new_deq_ptr = new_deq; 487 + } 488 + 489 + /* 490 + * flip_cycle means flip the cycle bit of all but the first and last TRB. 491 + * (The last TRB actually points to the ring enqueue pointer, which is not part 492 + * of this TD.) This is used to remove partially enqueued isoc TDs from a ring. 493 + */ 494 + static void cdnsp_td_to_noop(struct cdnsp_device *pdev, 495 + struct cdnsp_ring *ep_ring, 496 + struct cdnsp_td *td, 497 + bool flip_cycle) 498 + { 499 + struct cdnsp_segment *seg = td->start_seg; 500 + union cdnsp_trb *trb = td->first_trb; 501 + 502 + while (1) { 503 + cdnsp_trb_to_noop(trb, TRB_TR_NOOP); 504 + 505 + /* flip cycle if asked to */ 506 + if (flip_cycle && trb != td->first_trb && trb != td->last_trb) 507 + trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE); 508 + 509 + if (trb == td->last_trb) 510 + break; 511 + 512 + cdnsp_next_trb(pdev, ep_ring, &seg, &trb); 513 + } 514 + } 515 + 516 + /* 517 + * This TD is defined by the TRBs starting at start_trb in start_seg and ending 518 + * at end_trb, which may be in another segment. If the suspect DMA address is a 519 + * TRB in this TD, this function returns that TRB's segment. Otherwise it 520 + * returns 0. 521 + */ 522 + static struct cdnsp_segment *cdnsp_trb_in_td(struct cdnsp_device *pdev, 523 + struct cdnsp_segment *start_seg, 524 + union cdnsp_trb *start_trb, 525 + union cdnsp_trb *end_trb, 526 + dma_addr_t suspect_dma) 527 + { 528 + struct cdnsp_segment *cur_seg; 529 + union cdnsp_trb *temp_trb; 530 + dma_addr_t end_seg_dma; 531 + dma_addr_t end_trb_dma; 532 + dma_addr_t start_dma; 533 + 534 + start_dma = cdnsp_trb_virt_to_dma(start_seg, start_trb); 535 + cur_seg = start_seg; 536 + 537 + do { 538 + if (start_dma == 0) 539 + return NULL; 540 + 541 + temp_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1]; 542 + /* We may get an event for a Link TRB in the middle of a TD */ 543 + end_seg_dma = cdnsp_trb_virt_to_dma(cur_seg, temp_trb); 544 + /* If the end TRB isn't in this segment, this is set to 0 */ 545 + end_trb_dma = cdnsp_trb_virt_to_dma(cur_seg, end_trb); 546 + 547 + if (end_trb_dma > 0) { 548 + /* 549 + * The end TRB is in this segment, so suspect should 550 + * be here 551 + */ 552 + if (start_dma <= end_trb_dma) { 553 + if (suspect_dma >= start_dma && 554 + suspect_dma <= end_trb_dma) { 555 + return cur_seg; 556 + } 557 + } else { 558 + /* 559 + * Case for one segment with a 560 + * TD wrapped around to the top 561 + */ 562 + if ((suspect_dma >= start_dma && 563 + suspect_dma <= end_seg_dma) || 564 + (suspect_dma >= cur_seg->dma && 565 + suspect_dma <= end_trb_dma)) { 566 + return cur_seg; 567 + } 568 + } 569 + 570 + return NULL; 571 + } 572 + 573 + /* Might still be somewhere in this segment */ 574 + if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma) 575 + return cur_seg; 576 + 577 + cur_seg = cur_seg->next; 578 + start_dma = cdnsp_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); 579 + } while (cur_seg != start_seg); 580 + 581 + return NULL; 582 + } 583 + 584 + static void cdnsp_unmap_td_bounce_buffer(struct cdnsp_device *pdev, 585 + struct cdnsp_ring *ring, 586 + struct cdnsp_td *td) 587 + { 588 + struct cdnsp_segment *seg = td->bounce_seg; 589 + struct cdnsp_request *preq; 590 + size_t len; 591 + 592 + if (!seg) 593 + return; 594 + 595 + preq = td->preq; 596 + 597 + if (!preq->direction) { 598 + dma_unmap_single(pdev->dev, seg->bounce_dma, 599 + ring->bounce_buf_len, DMA_TO_DEVICE); 600 + return; 601 + } 602 + 603 + dma_unmap_single(pdev->dev, seg->bounce_dma, ring->bounce_buf_len, 604 + DMA_FROM_DEVICE); 605 + 606 + /* For in transfers we need to copy the data from bounce to sg */ 607 + len = sg_pcopy_from_buffer(preq->request.sg, preq->request.num_sgs, 608 + seg->bounce_buf, seg->bounce_len, 609 + seg->bounce_offs); 610 + if (len != seg->bounce_len) 611 + dev_warn(pdev->dev, "WARN Wrong bounce buffer read length: %zu != %d\n", 612 + len, seg->bounce_len); 613 + 614 + seg->bounce_len = 0; 615 + seg->bounce_offs = 0; 616 + } 617 + 618 + static int cdnsp_cmd_set_deq(struct cdnsp_device *pdev, 619 + struct cdnsp_ep *pep, 620 + struct cdnsp_dequeue_state *deq_state) 621 + { 622 + struct cdnsp_ring *ep_ring; 623 + int ret; 624 + 625 + if (!deq_state->new_deq_ptr || !deq_state->new_deq_seg) { 626 + cdnsp_ring_doorbell_for_active_rings(pdev, pep); 627 + return 0; 628 + } 629 + 630 + cdnsp_queue_new_dequeue_state(pdev, pep, deq_state); 631 + cdnsp_ring_cmd_db(pdev); 632 + ret = cdnsp_wait_for_cmd_compl(pdev); 633 + 634 + /* 635 + * Update the ring's dequeue segment and dequeue pointer 636 + * to reflect the new position. 637 + */ 638 + ep_ring = cdnsp_get_transfer_ring(pdev, pep, deq_state->stream_id); 639 + 640 + if (cdnsp_trb_is_link(ep_ring->dequeue)) { 641 + ep_ring->deq_seg = ep_ring->deq_seg->next; 642 + ep_ring->dequeue = ep_ring->deq_seg->trbs; 643 + } 644 + 645 + while (ep_ring->dequeue != deq_state->new_deq_ptr) { 646 + ep_ring->num_trbs_free++; 647 + ep_ring->dequeue++; 648 + 649 + if (cdnsp_trb_is_link(ep_ring->dequeue)) { 650 + if (ep_ring->dequeue == deq_state->new_deq_ptr) 651 + break; 652 + 653 + ep_ring->deq_seg = ep_ring->deq_seg->next; 654 + ep_ring->dequeue = ep_ring->deq_seg->trbs; 655 + } 656 + } 657 + 658 + /* 659 + * Probably there was TIMEOUT during handling Set Dequeue Pointer 660 + * command. It's critical error and controller will be stopped. 661 + */ 662 + if (ret) 663 + return -ESHUTDOWN; 664 + 665 + /* Restart any rings with pending requests */ 666 + cdnsp_ring_doorbell_for_active_rings(pdev, pep); 667 + 668 + return 0; 669 + } 670 + 671 + int cdnsp_remove_request(struct cdnsp_device *pdev, 672 + struct cdnsp_request *preq, 673 + struct cdnsp_ep *pep) 674 + { 675 + struct cdnsp_dequeue_state deq_state; 676 + struct cdnsp_td *cur_td = NULL; 677 + struct cdnsp_ring *ep_ring; 678 + struct cdnsp_segment *seg; 679 + int status = -ECONNRESET; 680 + int ret = 0; 681 + u64 hw_deq; 682 + 683 + memset(&deq_state, 0, sizeof(deq_state)); 684 + 685 + cur_td = &preq->td; 686 + ep_ring = cdnsp_request_to_transfer_ring(pdev, preq); 687 + 688 + /* 689 + * If we stopped on the TD we need to cancel, then we have to 690 + * move the controller endpoint ring dequeue pointer past 691 + * this TD. 692 + */ 693 + hw_deq = cdnsp_get_hw_deq(pdev, pep->idx, preq->request.stream_id); 694 + hw_deq &= ~0xf; 695 + 696 + seg = cdnsp_trb_in_td(pdev, cur_td->start_seg, cur_td->first_trb, 697 + cur_td->last_trb, hw_deq); 698 + 699 + if (seg && (pep->ep_state & EP_ENABLED)) 700 + cdnsp_find_new_dequeue_state(pdev, pep, preq->request.stream_id, 701 + cur_td, &deq_state); 702 + else 703 + cdnsp_td_to_noop(pdev, ep_ring, cur_td, false); 704 + 705 + /* 706 + * The event handler won't see a completion for this TD anymore, 707 + * so remove it from the endpoint ring's TD list. 708 + */ 709 + list_del_init(&cur_td->td_list); 710 + ep_ring->num_tds--; 711 + pep->stream_info.td_count--; 712 + 713 + /* 714 + * During disconnecting all endpoint will be disabled so we don't 715 + * have to worry about updating dequeue pointer. 716 + */ 717 + if (pdev->cdnsp_state & CDNSP_STATE_DISCONNECT_PENDING) { 718 + status = -ESHUTDOWN; 719 + ret = cdnsp_cmd_set_deq(pdev, pep, &deq_state); 720 + } 721 + 722 + cdnsp_unmap_td_bounce_buffer(pdev, ep_ring, cur_td); 723 + cdnsp_gadget_giveback(pep, cur_td->preq, status); 724 + 725 + return ret; 726 + } 727 + 728 + static int cdnsp_update_port_id(struct cdnsp_device *pdev, u32 port_id) 729 + { 730 + struct cdnsp_port *port = pdev->active_port; 731 + u8 old_port = 0; 732 + 733 + if (port && port->port_num == port_id) 734 + return 0; 735 + 736 + if (port) 737 + old_port = port->port_num; 738 + 739 + if (port_id == pdev->usb2_port.port_num) { 740 + port = &pdev->usb2_port; 741 + } else if (port_id == pdev->usb3_port.port_num) { 742 + port = &pdev->usb3_port; 743 + } else { 744 + dev_err(pdev->dev, "Port event with invalid port ID %d\n", 745 + port_id); 746 + return -EINVAL; 747 + } 748 + 749 + if (port_id != old_port) { 750 + cdnsp_disable_slot(pdev); 751 + pdev->active_port = port; 752 + cdnsp_enable_slot(pdev); 753 + } 754 + 755 + if (port_id == pdev->usb2_port.port_num) 756 + cdnsp_set_usb2_hardware_lpm(pdev, NULL, 1); 757 + else 758 + writel(PORT_U1_TIMEOUT(1) | PORT_U2_TIMEOUT(1), 759 + &pdev->usb3_port.regs->portpmsc); 760 + 761 + return 0; 762 + } 763 + 764 + static void cdnsp_handle_port_status(struct cdnsp_device *pdev, 765 + union cdnsp_trb *event) 766 + { 767 + struct cdnsp_port_regs __iomem *port_regs; 768 + u32 portsc, cmd_regs; 769 + bool port2 = false; 770 + u32 link_state; 771 + u32 port_id; 772 + 773 + /* Port status change events always have a successful completion code */ 774 + if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) 775 + dev_err(pdev->dev, "ERR: incorrect PSC event\n"); 776 + 777 + port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); 778 + 779 + if (cdnsp_update_port_id(pdev, port_id)) 780 + goto cleanup; 781 + 782 + port_regs = pdev->active_port->regs; 783 + 784 + if (port_id == pdev->usb2_port.port_num) 785 + port2 = true; 786 + 787 + new_event: 788 + portsc = readl(&port_regs->portsc); 789 + writel(cdnsp_port_state_to_neutral(portsc) | 790 + (portsc & PORT_CHANGE_BITS), &port_regs->portsc); 791 + 792 + pdev->gadget.speed = cdnsp_port_speed(portsc); 793 + link_state = portsc & PORT_PLS_MASK; 794 + 795 + /* Port Link State change detected. */ 796 + if ((portsc & PORT_PLC)) { 797 + if (!(pdev->cdnsp_state & CDNSP_WAKEUP_PENDING) && 798 + link_state == XDEV_RESUME) { 799 + cmd_regs = readl(&pdev->op_regs->command); 800 + if (!(cmd_regs & CMD_R_S)) 801 + goto cleanup; 802 + 803 + if (DEV_SUPERSPEED_ANY(portsc)) { 804 + cdnsp_set_link_state(pdev, &port_regs->portsc, 805 + XDEV_U0); 806 + 807 + cdnsp_resume_gadget(pdev); 808 + } 809 + } 810 + 811 + if ((pdev->cdnsp_state & CDNSP_WAKEUP_PENDING) && 812 + link_state == XDEV_U0) { 813 + pdev->cdnsp_state &= ~CDNSP_WAKEUP_PENDING; 814 + 815 + cdnsp_force_header_wakeup(pdev, 1); 816 + cdnsp_ring_cmd_db(pdev); 817 + cdnsp_wait_for_cmd_compl(pdev); 818 + } 819 + 820 + if (link_state == XDEV_U0 && pdev->link_state == XDEV_U3 && 821 + !DEV_SUPERSPEED_ANY(portsc)) 822 + cdnsp_resume_gadget(pdev); 823 + 824 + if (link_state == XDEV_U3 && pdev->link_state != XDEV_U3) 825 + cdnsp_suspend_gadget(pdev); 826 + 827 + pdev->link_state = link_state; 828 + } 829 + 830 + if (portsc & PORT_CSC) { 831 + /* Detach device. */ 832 + if (pdev->gadget.connected && !(portsc & PORT_CONNECT)) 833 + cdnsp_disconnect_gadget(pdev); 834 + 835 + /* Attach device. */ 836 + if (portsc & PORT_CONNECT) { 837 + if (!port2) 838 + cdnsp_irq_reset(pdev); 839 + 840 + usb_gadget_set_state(&pdev->gadget, USB_STATE_ATTACHED); 841 + } 842 + } 843 + 844 + /* Port reset. */ 845 + if ((portsc & (PORT_RC | PORT_WRC)) && (portsc & PORT_CONNECT)) { 846 + cdnsp_irq_reset(pdev); 847 + pdev->u1_allowed = 0; 848 + pdev->u2_allowed = 0; 849 + pdev->may_wakeup = 0; 850 + } 851 + 852 + if (portsc & PORT_CEC) 853 + dev_err(pdev->dev, "Port Over Current detected\n"); 854 + 855 + if (portsc & PORT_CEC) 856 + dev_err(pdev->dev, "Port Configure Error detected\n"); 857 + 858 + if (readl(&port_regs->portsc) & PORT_CHANGE_BITS) 859 + goto new_event; 860 + 861 + cleanup: 862 + cdnsp_inc_deq(pdev, pdev->event_ring); 863 + } 864 + 865 + static void cdnsp_td_cleanup(struct cdnsp_device *pdev, 866 + struct cdnsp_td *td, 867 + struct cdnsp_ring *ep_ring, 868 + int *status) 869 + { 870 + struct cdnsp_request *preq = td->preq; 871 + 872 + /* if a bounce buffer was used to align this td then unmap it */ 873 + cdnsp_unmap_td_bounce_buffer(pdev, ep_ring, td); 874 + 875 + /* 876 + * If the controller said we transferred more data than the buffer 877 + * length, Play it safe and say we didn't transfer anything. 878 + */ 879 + if (preq->request.actual > preq->request.length) { 880 + preq->request.actual = 0; 881 + *status = 0; 882 + } 883 + 884 + list_del_init(&td->td_list); 885 + ep_ring->num_tds--; 886 + preq->pep->stream_info.td_count--; 887 + 888 + cdnsp_gadget_giveback(preq->pep, preq, *status); 889 + } 890 + 891 + static void cdnsp_finish_td(struct cdnsp_device *pdev, 892 + struct cdnsp_td *td, 893 + struct cdnsp_transfer_event *event, 894 + struct cdnsp_ep *ep, 895 + int *status) 896 + { 897 + struct cdnsp_ring *ep_ring; 898 + u32 trb_comp_code; 899 + 900 + ep_ring = cdnsp_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); 901 + trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 902 + 903 + if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID || 904 + trb_comp_code == COMP_STOPPED || 905 + trb_comp_code == COMP_STOPPED_SHORT_PACKET) { 906 + /* 907 + * The Endpoint Stop Command completion will take care of any 908 + * stopped TDs. A stopped TD may be restarted, so don't update 909 + * the ring dequeue pointer or take this TD off any lists yet. 910 + */ 911 + return; 912 + } 913 + 914 + /* Update ring dequeue pointer */ 915 + while (ep_ring->dequeue != td->last_trb) 916 + cdnsp_inc_deq(pdev, ep_ring); 917 + 918 + cdnsp_inc_deq(pdev, ep_ring); 919 + 920 + cdnsp_td_cleanup(pdev, td, ep_ring, status); 921 + } 922 + 923 + /* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */ 924 + static int cdnsp_sum_trb_lengths(struct cdnsp_device *pdev, 925 + struct cdnsp_ring *ring, 926 + union cdnsp_trb *stop_trb) 927 + { 928 + struct cdnsp_segment *seg = ring->deq_seg; 929 + union cdnsp_trb *trb = ring->dequeue; 930 + u32 sum; 931 + 932 + for (sum = 0; trb != stop_trb; cdnsp_next_trb(pdev, ring, &seg, &trb)) { 933 + if (!cdnsp_trb_is_noop(trb) && !cdnsp_trb_is_link(trb)) 934 + sum += TRB_LEN(le32_to_cpu(trb->generic.field[2])); 935 + } 936 + return sum; 937 + } 938 + 939 + static int cdnsp_giveback_first_trb(struct cdnsp_device *pdev, 940 + struct cdnsp_ep *pep, 941 + unsigned int stream_id, 942 + int start_cycle, 943 + struct cdnsp_generic_trb *start_trb) 944 + { 945 + /* 946 + * Pass all the TRBs to the hardware at once and make sure this write 947 + * isn't reordered. 948 + */ 949 + wmb(); 950 + 951 + if (start_cycle) 952 + start_trb->field[3] |= cpu_to_le32(start_cycle); 953 + else 954 + start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); 955 + 956 + if ((pep->ep_state & EP_HAS_STREAMS) && 957 + !pep->stream_info.first_prime_det) 958 + return 0; 959 + 960 + return cdnsp_ring_ep_doorbell(pdev, pep, stream_id); 961 + } 962 + 963 + /* 964 + * Process control tds, update USB request status and actual_length. 965 + */ 966 + static void cdnsp_process_ctrl_td(struct cdnsp_device *pdev, 967 + struct cdnsp_td *td, 968 + union cdnsp_trb *event_trb, 969 + struct cdnsp_transfer_event *event, 970 + struct cdnsp_ep *pep, 971 + int *status) 972 + { 973 + struct cdnsp_ring *ep_ring; 974 + u32 remaining; 975 + u32 trb_type; 976 + 977 + trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event_trb->generic.field[3])); 978 + ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer)); 979 + remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 980 + 981 + /* 982 + * if on data stage then update the actual_length of the USB 983 + * request and flag it as set, so it won't be overwritten in the event 984 + * for the last TRB. 985 + */ 986 + if (trb_type == TRB_DATA) { 987 + td->request_length_set = true; 988 + td->preq->request.actual = td->preq->request.length - remaining; 989 + } 990 + 991 + /* at status stage */ 992 + if (!td->request_length_set) 993 + td->preq->request.actual = td->preq->request.length; 994 + 995 + if (pdev->ep0_stage == CDNSP_DATA_STAGE && pep->number == 0 && 996 + pdev->three_stage_setup) { 997 + td = list_entry(ep_ring->td_list.next, struct cdnsp_td, 998 + td_list); 999 + pdev->ep0_stage = CDNSP_STATUS_STAGE; 1000 + 1001 + cdnsp_giveback_first_trb(pdev, pep, 0, ep_ring->cycle_state, 1002 + &td->last_trb->generic); 1003 + return; 1004 + } 1005 + 1006 + cdnsp_finish_td(pdev, td, event, pep, status); 1007 + } 1008 + 1009 + /* 1010 + * Process isochronous tds, update usb request status and actual_length. 1011 + */ 1012 + static void cdnsp_process_isoc_td(struct cdnsp_device *pdev, 1013 + struct cdnsp_td *td, 1014 + union cdnsp_trb *ep_trb, 1015 + struct cdnsp_transfer_event *event, 1016 + struct cdnsp_ep *pep, 1017 + int status) 1018 + { 1019 + struct cdnsp_request *preq = td->preq; 1020 + u32 remaining, requested, ep_trb_len; 1021 + bool sum_trbs_for_length = false; 1022 + struct cdnsp_ring *ep_ring; 1023 + u32 trb_comp_code; 1024 + u32 td_length; 1025 + 1026 + ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer)); 1027 + trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 1028 + remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 1029 + ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); 1030 + 1031 + requested = preq->request.length; 1032 + 1033 + /* handle completion code */ 1034 + switch (trb_comp_code) { 1035 + case COMP_SUCCESS: 1036 + preq->request.status = 0; 1037 + break; 1038 + case COMP_SHORT_PACKET: 1039 + preq->request.status = 0; 1040 + sum_trbs_for_length = true; 1041 + break; 1042 + case COMP_ISOCH_BUFFER_OVERRUN: 1043 + case COMP_BABBLE_DETECTED_ERROR: 1044 + preq->request.status = -EOVERFLOW; 1045 + break; 1046 + case COMP_STOPPED: 1047 + sum_trbs_for_length = true; 1048 + break; 1049 + case COMP_STOPPED_SHORT_PACKET: 1050 + /* field normally containing residue now contains transferred */ 1051 + preq->request.status = 0; 1052 + requested = remaining; 1053 + break; 1054 + case COMP_STOPPED_LENGTH_INVALID: 1055 + requested = 0; 1056 + remaining = 0; 1057 + break; 1058 + default: 1059 + sum_trbs_for_length = true; 1060 + preq->request.status = -1; 1061 + break; 1062 + } 1063 + 1064 + if (sum_trbs_for_length) { 1065 + td_length = cdnsp_sum_trb_lengths(pdev, ep_ring, ep_trb); 1066 + td_length += ep_trb_len - remaining; 1067 + } else { 1068 + td_length = requested; 1069 + } 1070 + 1071 + td->preq->request.actual += td_length; 1072 + 1073 + cdnsp_finish_td(pdev, td, event, pep, &status); 1074 + } 1075 + 1076 + static void cdnsp_skip_isoc_td(struct cdnsp_device *pdev, 1077 + struct cdnsp_td *td, 1078 + struct cdnsp_transfer_event *event, 1079 + struct cdnsp_ep *pep, 1080 + int status) 1081 + { 1082 + struct cdnsp_ring *ep_ring; 1083 + 1084 + ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer)); 1085 + td->preq->request.status = -EXDEV; 1086 + td->preq->request.actual = 0; 1087 + 1088 + /* Update ring dequeue pointer */ 1089 + while (ep_ring->dequeue != td->last_trb) 1090 + cdnsp_inc_deq(pdev, ep_ring); 1091 + 1092 + cdnsp_inc_deq(pdev, ep_ring); 1093 + 1094 + cdnsp_td_cleanup(pdev, td, ep_ring, &status); 1095 + } 1096 + 1097 + /* 1098 + * Process bulk and interrupt tds, update usb request status and actual_length. 1099 + */ 1100 + static void cdnsp_process_bulk_intr_td(struct cdnsp_device *pdev, 1101 + struct cdnsp_td *td, 1102 + union cdnsp_trb *ep_trb, 1103 + struct cdnsp_transfer_event *event, 1104 + struct cdnsp_ep *ep, 1105 + int *status) 1106 + { 1107 + u32 remaining, requested, ep_trb_len; 1108 + struct cdnsp_ring *ep_ring; 1109 + u32 trb_comp_code; 1110 + 1111 + ep_ring = cdnsp_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); 1112 + trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 1113 + remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 1114 + ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); 1115 + requested = td->preq->request.length; 1116 + 1117 + switch (trb_comp_code) { 1118 + case COMP_SUCCESS: 1119 + case COMP_SHORT_PACKET: 1120 + *status = 0; 1121 + break; 1122 + case COMP_STOPPED_SHORT_PACKET: 1123 + td->preq->request.actual = remaining; 1124 + goto finish_td; 1125 + case COMP_STOPPED_LENGTH_INVALID: 1126 + /* Stopped on ep trb with invalid length, exclude it. */ 1127 + ep_trb_len = 0; 1128 + remaining = 0; 1129 + break; 1130 + } 1131 + 1132 + if (ep_trb == td->last_trb) 1133 + ep_trb_len = requested - remaining; 1134 + else 1135 + ep_trb_len = cdnsp_sum_trb_lengths(pdev, ep_ring, ep_trb) + 1136 + ep_trb_len - remaining; 1137 + td->preq->request.actual = ep_trb_len; 1138 + 1139 + finish_td: 1140 + ep->stream_info.drbls_count--; 1141 + 1142 + cdnsp_finish_td(pdev, td, event, ep, status); 1143 + } 1144 + 1145 + static void cdnsp_handle_tx_nrdy(struct cdnsp_device *pdev, 1146 + struct cdnsp_transfer_event *event) 1147 + { 1148 + struct cdnsp_generic_trb *generic; 1149 + struct cdnsp_ring *ep_ring; 1150 + struct cdnsp_ep *pep; 1151 + int cur_stream; 1152 + int ep_index; 1153 + int host_sid; 1154 + int dev_sid; 1155 + 1156 + generic = (struct cdnsp_generic_trb *)event; 1157 + ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; 1158 + dev_sid = TRB_TO_DEV_STREAM(le32_to_cpu(generic->field[0])); 1159 + host_sid = TRB_TO_HOST_STREAM(le32_to_cpu(generic->field[2])); 1160 + 1161 + pep = &pdev->eps[ep_index]; 1162 + 1163 + if (!(pep->ep_state & EP_HAS_STREAMS)) 1164 + return; 1165 + 1166 + if (host_sid == STREAM_PRIME_ACK) { 1167 + pep->stream_info.first_prime_det = 1; 1168 + for (cur_stream = 1; cur_stream < pep->stream_info.num_streams; 1169 + cur_stream++) { 1170 + ep_ring = pep->stream_info.stream_rings[cur_stream]; 1171 + ep_ring->stream_active = 1; 1172 + ep_ring->stream_rejected = 0; 1173 + } 1174 + } 1175 + 1176 + if (host_sid == STREAM_REJECTED) { 1177 + struct cdnsp_td *td, *td_temp; 1178 + 1179 + pep->stream_info.drbls_count--; 1180 + ep_ring = pep->stream_info.stream_rings[dev_sid]; 1181 + ep_ring->stream_active = 0; 1182 + ep_ring->stream_rejected = 1; 1183 + 1184 + list_for_each_entry_safe(td, td_temp, &ep_ring->td_list, 1185 + td_list) { 1186 + td->drbl = 0; 1187 + } 1188 + } 1189 + 1190 + cdnsp_ring_doorbell_for_active_rings(pdev, pep); 1191 + } 1192 + 1193 + /* 1194 + * If this function returns an error condition, it means it got a Transfer 1195 + * event with a corrupted TRB DMA address or endpoint is disabled. 1196 + */ 1197 + static int cdnsp_handle_tx_event(struct cdnsp_device *pdev, 1198 + struct cdnsp_transfer_event *event) 1199 + { 1200 + const struct usb_endpoint_descriptor *desc; 1201 + bool handling_skipped_tds = false; 1202 + struct cdnsp_segment *ep_seg; 1203 + struct cdnsp_ring *ep_ring; 1204 + int status = -EINPROGRESS; 1205 + union cdnsp_trb *ep_trb; 1206 + dma_addr_t ep_trb_dma; 1207 + struct cdnsp_ep *pep; 1208 + struct cdnsp_td *td; 1209 + u32 trb_comp_code; 1210 + int invalidate; 1211 + int ep_index; 1212 + 1213 + invalidate = le32_to_cpu(event->flags) & TRB_EVENT_INVALIDATE; 1214 + ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; 1215 + trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 1216 + ep_trb_dma = le64_to_cpu(event->buffer); 1217 + 1218 + pep = &pdev->eps[ep_index]; 1219 + ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer)); 1220 + 1221 + /* 1222 + * If device is disconnect then all requests will be dequeued 1223 + * by upper layers as part of disconnect sequence. 1224 + * We don't want handle such event to avoid racing. 1225 + */ 1226 + if (invalidate || !pdev->gadget.connected) 1227 + goto cleanup; 1228 + 1229 + if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_DISABLED) 1230 + goto err_out; 1231 + 1232 + /* Some transfer events don't always point to a trb*/ 1233 + if (!ep_ring) { 1234 + switch (trb_comp_code) { 1235 + case COMP_INVALID_STREAM_TYPE_ERROR: 1236 + case COMP_INVALID_STREAM_ID_ERROR: 1237 + case COMP_RING_UNDERRUN: 1238 + case COMP_RING_OVERRUN: 1239 + goto cleanup; 1240 + default: 1241 + dev_err(pdev->dev, "ERROR: %s event for unknown ring\n", 1242 + pep->name); 1243 + goto err_out; 1244 + } 1245 + } 1246 + 1247 + /* Look for some error cases that need special treatment. */ 1248 + switch (trb_comp_code) { 1249 + case COMP_BABBLE_DETECTED_ERROR: 1250 + status = -EOVERFLOW; 1251 + break; 1252 + case COMP_RING_UNDERRUN: 1253 + case COMP_RING_OVERRUN: 1254 + /* 1255 + * When the Isoch ring is empty, the controller will generate 1256 + * a Ring Overrun Event for IN Isoch endpoint or Ring 1257 + * Underrun Event for OUT Isoch endpoint. 1258 + */ 1259 + goto cleanup; 1260 + case COMP_MISSED_SERVICE_ERROR: 1261 + /* 1262 + * When encounter missed service error, one or more isoc tds 1263 + * may be missed by controller. 1264 + * Set skip flag of the ep_ring; Complete the missed tds as 1265 + * short transfer when process the ep_ring next time. 1266 + */ 1267 + pep->skip = true; 1268 + break; 1269 + } 1270 + 1271 + do { 1272 + /* 1273 + * This TRB should be in the TD at the head of this ring's TD 1274 + * list. 1275 + */ 1276 + if (list_empty(&ep_ring->td_list)) { 1277 + if (pep->skip) 1278 + pep->skip = false; 1279 + 1280 + goto cleanup; 1281 + } 1282 + 1283 + td = list_entry(ep_ring->td_list.next, struct cdnsp_td, 1284 + td_list); 1285 + 1286 + /* Is this a TRB in the currently executing TD? */ 1287 + ep_seg = cdnsp_trb_in_td(pdev, ep_ring->deq_seg, 1288 + ep_ring->dequeue, td->last_trb, 1289 + ep_trb_dma); 1290 + 1291 + /* 1292 + * Skip the Force Stopped Event. The event_trb(ep_trb_dma) 1293 + * of FSE is not in the current TD pointed by ep_ring->dequeue 1294 + * because that the hardware dequeue pointer still at the 1295 + * previous TRB of the current TD. The previous TRB maybe a 1296 + * Link TD or the last TRB of the previous TD. The command 1297 + * completion handle will take care the rest. 1298 + */ 1299 + if (!ep_seg && (trb_comp_code == COMP_STOPPED || 1300 + trb_comp_code == COMP_STOPPED_LENGTH_INVALID)) { 1301 + pep->skip = false; 1302 + goto cleanup; 1303 + } 1304 + 1305 + desc = td->preq->pep->endpoint.desc; 1306 + if (!ep_seg) { 1307 + if (!pep->skip || !usb_endpoint_xfer_isoc(desc)) { 1308 + /* Something is busted, give up! */ 1309 + dev_err(pdev->dev, 1310 + "ERROR Transfer event TRB DMA ptr not " 1311 + "part of current TD ep_index %d " 1312 + "comp_code %u\n", ep_index, 1313 + trb_comp_code); 1314 + return -EINVAL; 1315 + } 1316 + 1317 + cdnsp_skip_isoc_td(pdev, td, event, pep, status); 1318 + goto cleanup; 1319 + } 1320 + 1321 + if (trb_comp_code == COMP_SHORT_PACKET) 1322 + ep_ring->last_td_was_short = true; 1323 + else 1324 + ep_ring->last_td_was_short = false; 1325 + 1326 + if (pep->skip) { 1327 + pep->skip = false; 1328 + cdnsp_skip_isoc_td(pdev, td, event, pep, status); 1329 + goto cleanup; 1330 + } 1331 + 1332 + ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) 1333 + / sizeof(*ep_trb)]; 1334 + 1335 + if (cdnsp_trb_is_noop(ep_trb)) 1336 + goto cleanup; 1337 + 1338 + if (usb_endpoint_xfer_control(desc)) 1339 + cdnsp_process_ctrl_td(pdev, td, ep_trb, event, pep, 1340 + &status); 1341 + else if (usb_endpoint_xfer_isoc(desc)) 1342 + cdnsp_process_isoc_td(pdev, td, ep_trb, event, pep, 1343 + status); 1344 + else 1345 + cdnsp_process_bulk_intr_td(pdev, td, ep_trb, event, pep, 1346 + &status); 1347 + cleanup: 1348 + handling_skipped_tds = pep->skip; 1349 + 1350 + /* 1351 + * Do not update event ring dequeue pointer if we're in a loop 1352 + * processing missed tds. 1353 + */ 1354 + if (!handling_skipped_tds) 1355 + cdnsp_inc_deq(pdev, pdev->event_ring); 1356 + 1357 + /* 1358 + * If ep->skip is set, it means there are missed tds on the 1359 + * endpoint ring need to take care of. 1360 + * Process them as short transfer until reach the td pointed by 1361 + * the event. 1362 + */ 1363 + } while (handling_skipped_tds); 1364 + return 0; 1365 + 1366 + err_out: 1367 + dev_err(pdev->dev, "@%016llx %08x %08x %08x %08x\n", 1368 + (unsigned long long) 1369 + cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg, 1370 + pdev->event_ring->dequeue), 1371 + lower_32_bits(le64_to_cpu(event->buffer)), 1372 + upper_32_bits(le64_to_cpu(event->buffer)), 1373 + le32_to_cpu(event->transfer_len), 1374 + le32_to_cpu(event->flags)); 1375 + return -EINVAL; 1376 + } 1377 + 1378 + /* 1379 + * This function handles all events on the event ring. 1380 + * Returns true for "possibly more events to process" (caller should call 1381 + * again), otherwise false if done. 1382 + */ 1383 + static bool cdnsp_handle_event(struct cdnsp_device *pdev) 1384 + { 1385 + unsigned int comp_code; 1386 + union cdnsp_trb *event; 1387 + bool update_ptrs = true; 1388 + __le32 cycle_bit; 1389 + int ret = 0; 1390 + u32 flags; 1391 + 1392 + event = pdev->event_ring->dequeue; 1393 + flags = le32_to_cpu(event->event_cmd.flags); 1394 + cycle_bit = (flags & TRB_CYCLE); 1395 + 1396 + /* Does the controller or driver own the TRB? */ 1397 + if (cycle_bit != pdev->event_ring->cycle_state) 1398 + return false; 1399 + 1400 + /* 1401 + * Barrier between reading the TRB_CYCLE (valid) flag above and any 1402 + * reads of the event's flags/data below. 1403 + */ 1404 + rmb(); 1405 + 1406 + switch (flags & TRB_TYPE_BITMASK) { 1407 + case TRB_TYPE(TRB_COMPLETION): 1408 + /* 1409 + * Command can't be handled in interrupt context so just 1410 + * increment command ring dequeue pointer. 1411 + */ 1412 + cdnsp_inc_deq(pdev, pdev->cmd_ring); 1413 + break; 1414 + case TRB_TYPE(TRB_PORT_STATUS): 1415 + cdnsp_handle_port_status(pdev, event); 1416 + update_ptrs = false; 1417 + break; 1418 + case TRB_TYPE(TRB_TRANSFER): 1419 + ret = cdnsp_handle_tx_event(pdev, &event->trans_event); 1420 + if (ret >= 0) 1421 + update_ptrs = false; 1422 + break; 1423 + case TRB_TYPE(TRB_SETUP): 1424 + pdev->ep0_stage = CDNSP_SETUP_STAGE; 1425 + pdev->setup_id = TRB_SETUPID_TO_TYPE(flags); 1426 + pdev->setup_speed = TRB_SETUP_SPEEDID(flags); 1427 + pdev->setup = *((struct usb_ctrlrequest *) 1428 + &event->trans_event.buffer); 1429 + 1430 + cdnsp_setup_analyze(pdev); 1431 + break; 1432 + case TRB_TYPE(TRB_ENDPOINT_NRDY): 1433 + cdnsp_handle_tx_nrdy(pdev, &event->trans_event); 1434 + break; 1435 + case TRB_TYPE(TRB_HC_EVENT): { 1436 + comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2])); 1437 + 1438 + switch (comp_code) { 1439 + case COMP_EVENT_RING_FULL_ERROR: 1440 + dev_err(pdev->dev, "Event Ring Full\n"); 1441 + break; 1442 + default: 1443 + dev_err(pdev->dev, "Controller error code 0x%02x\n", 1444 + comp_code); 1445 + } 1446 + 1447 + break; 1448 + } 1449 + case TRB_TYPE(TRB_MFINDEX_WRAP): 1450 + case TRB_TYPE(TRB_DRB_OVERFLOW): 1451 + break; 1452 + default: 1453 + dev_warn(pdev->dev, "ERROR unknown event type %ld\n", 1454 + TRB_FIELD_TO_TYPE(flags)); 1455 + } 1456 + 1457 + if (update_ptrs) 1458 + /* Update SW event ring dequeue pointer. */ 1459 + cdnsp_inc_deq(pdev, pdev->event_ring); 1460 + 1461 + /* 1462 + * Caller will call us again to check if there are more items 1463 + * on the event ring. 1464 + */ 1465 + return true; 1466 + } 1467 + 1468 + irqreturn_t cdnsp_thread_irq_handler(int irq, void *data) 1469 + { 1470 + struct cdnsp_device *pdev = (struct cdnsp_device *)data; 1471 + union cdnsp_trb *event_ring_deq; 1472 + int counter = 0; 1473 + 1474 + spin_lock(&pdev->lock); 1475 + 1476 + if (pdev->cdnsp_state & (CDNSP_STATE_HALTED | CDNSP_STATE_DYING)) { 1477 + cdnsp_died(pdev); 1478 + spin_unlock(&pdev->lock); 1479 + return IRQ_HANDLED; 1480 + } 1481 + 1482 + event_ring_deq = pdev->event_ring->dequeue; 1483 + 1484 + while (cdnsp_handle_event(pdev)) { 1485 + if (++counter >= TRBS_PER_EV_DEQ_UPDATE) { 1486 + cdnsp_update_erst_dequeue(pdev, event_ring_deq, 0); 1487 + event_ring_deq = pdev->event_ring->dequeue; 1488 + counter = 0; 1489 + } 1490 + } 1491 + 1492 + cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1); 1493 + 1494 + spin_unlock(&pdev->lock); 1495 + 1496 + return IRQ_HANDLED; 1497 + } 1498 + 1499 + irqreturn_t cdnsp_irq_handler(int irq, void *priv) 1500 + { 1501 + struct cdnsp_device *pdev = (struct cdnsp_device *)priv; 1502 + u32 irq_pending; 1503 + u32 status; 1504 + 1505 + status = readl(&pdev->op_regs->status); 1506 + 1507 + if (status == ~(u32)0) { 1508 + cdnsp_died(pdev); 1509 + return IRQ_HANDLED; 1510 + } 1511 + 1512 + if (!(status & STS_EINT)) 1513 + return IRQ_NONE; 1514 + 1515 + writel(status | STS_EINT, &pdev->op_regs->status); 1516 + irq_pending = readl(&pdev->ir_set->irq_pending); 1517 + irq_pending |= IMAN_IP; 1518 + writel(irq_pending, &pdev->ir_set->irq_pending); 1519 + 1520 + if (status & STS_FATAL) { 1521 + cdnsp_died(pdev); 1522 + return IRQ_HANDLED; 1523 + } 1524 + 1525 + return IRQ_WAKE_THREAD; 1526 + } 1527 + 1528 + /* 1529 + * Generic function for queuing a TRB on a ring. 1530 + * The caller must have checked to make sure there's room on the ring. 1531 + * 1532 + * @more_trbs_coming: Will you enqueue more TRBs before setting doorbell? 1533 + */ 1534 + static void cdnsp_queue_trb(struct cdnsp_device *pdev, struct cdnsp_ring *ring, 1535 + bool more_trbs_coming, u32 field1, u32 field2, 1536 + u32 field3, u32 field4) 1537 + { 1538 + struct cdnsp_generic_trb *trb; 1539 + 1540 + trb = &ring->enqueue->generic; 1541 + 1542 + trb->field[0] = cpu_to_le32(field1); 1543 + trb->field[1] = cpu_to_le32(field2); 1544 + trb->field[2] = cpu_to_le32(field3); 1545 + trb->field[3] = cpu_to_le32(field4); 1546 + 1547 + cdnsp_inc_enq(pdev, ring, more_trbs_coming); 1548 + } 1549 + 1550 + /* 1551 + * Does various checks on the endpoint ring, and makes it ready to 1552 + * queue num_trbs. 1553 + */ 1554 + static int cdnsp_prepare_ring(struct cdnsp_device *pdev, 1555 + struct cdnsp_ring *ep_ring, 1556 + u32 ep_state, unsigned 1557 + int num_trbs, 1558 + gfp_t mem_flags) 1559 + { 1560 + unsigned int num_trbs_needed; 1561 + 1562 + /* Make sure the endpoint has been added to controller schedule. */ 1563 + switch (ep_state) { 1564 + case EP_STATE_STOPPED: 1565 + case EP_STATE_RUNNING: 1566 + case EP_STATE_HALTED: 1567 + break; 1568 + default: 1569 + dev_err(pdev->dev, "ERROR: incorrect endpoint state\n"); 1570 + return -EINVAL; 1571 + } 1572 + 1573 + while (1) { 1574 + if (cdnsp_room_on_ring(pdev, ep_ring, num_trbs)) 1575 + break; 1576 + 1577 + num_trbs_needed = num_trbs - ep_ring->num_trbs_free; 1578 + if (cdnsp_ring_expansion(pdev, ep_ring, num_trbs_needed, 1579 + mem_flags)) { 1580 + dev_err(pdev->dev, "Ring expansion failed\n"); 1581 + return -ENOMEM; 1582 + } 1583 + } 1584 + 1585 + while (cdnsp_trb_is_link(ep_ring->enqueue)) { 1586 + ep_ring->enqueue->link.control |= cpu_to_le32(TRB_CHAIN); 1587 + /* The cycle bit must be set as the last operation. */ 1588 + wmb(); 1589 + ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE); 1590 + 1591 + /* Toggle the cycle bit after the last ring segment. */ 1592 + if (cdnsp_link_trb_toggles_cycle(ep_ring->enqueue)) 1593 + ep_ring->cycle_state ^= 1; 1594 + ep_ring->enq_seg = ep_ring->enq_seg->next; 1595 + ep_ring->enqueue = ep_ring->enq_seg->trbs; 1596 + } 1597 + return 0; 1598 + } 1599 + 1600 + static int cdnsp_prepare_transfer(struct cdnsp_device *pdev, 1601 + struct cdnsp_request *preq, 1602 + unsigned int num_trbs) 1603 + { 1604 + struct cdnsp_ring *ep_ring; 1605 + int ret; 1606 + 1607 + ep_ring = cdnsp_get_transfer_ring(pdev, preq->pep, 1608 + preq->request.stream_id); 1609 + if (!ep_ring) 1610 + return -EINVAL; 1611 + 1612 + ret = cdnsp_prepare_ring(pdev, ep_ring, 1613 + GET_EP_CTX_STATE(preq->pep->out_ctx), 1614 + num_trbs, GFP_ATOMIC); 1615 + if (ret) 1616 + return ret; 1617 + 1618 + INIT_LIST_HEAD(&preq->td.td_list); 1619 + preq->td.preq = preq; 1620 + 1621 + /* Add this TD to the tail of the endpoint ring's TD list. */ 1622 + list_add_tail(&preq->td.td_list, &ep_ring->td_list); 1623 + ep_ring->num_tds++; 1624 + preq->pep->stream_info.td_count++; 1625 + 1626 + preq->td.start_seg = ep_ring->enq_seg; 1627 + preq->td.first_trb = ep_ring->enqueue; 1628 + 1629 + return 0; 1630 + } 1631 + 1632 + static unsigned int cdnsp_count_trbs(u64 addr, u64 len) 1633 + { 1634 + unsigned int num_trbs; 1635 + 1636 + num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)), 1637 + TRB_MAX_BUFF_SIZE); 1638 + if (num_trbs == 0) 1639 + num_trbs++; 1640 + 1641 + return num_trbs; 1642 + } 1643 + 1644 + static unsigned int count_trbs_needed(struct cdnsp_request *preq) 1645 + { 1646 + return cdnsp_count_trbs(preq->request.dma, preq->request.length); 1647 + } 1648 + 1649 + static unsigned int count_sg_trbs_needed(struct cdnsp_request *preq) 1650 + { 1651 + unsigned int i, len, full_len, num_trbs = 0; 1652 + struct scatterlist *sg; 1653 + 1654 + full_len = preq->request.length; 1655 + 1656 + for_each_sg(preq->request.sg, sg, preq->request.num_sgs, i) { 1657 + len = sg_dma_len(sg); 1658 + num_trbs += cdnsp_count_trbs(sg_dma_address(sg), len); 1659 + len = min(len, full_len); 1660 + full_len -= len; 1661 + if (full_len == 0) 1662 + break; 1663 + } 1664 + 1665 + return num_trbs; 1666 + } 1667 + 1668 + static unsigned int count_isoc_trbs_needed(struct cdnsp_request *preq) 1669 + { 1670 + return cdnsp_count_trbs(preq->request.dma, preq->request.length); 1671 + } 1672 + 1673 + static void cdnsp_check_trb_math(struct cdnsp_request *preq, int running_total) 1674 + { 1675 + if (running_total != preq->request.length) 1676 + dev_err(preq->pep->pdev->dev, 1677 + "%s - Miscalculated tx length, " 1678 + "queued %#x, asked for %#x (%d)\n", 1679 + preq->pep->name, running_total, 1680 + preq->request.length, preq->request.actual); 1681 + } 1682 + 1683 + /* 1684 + * TD size is the number of max packet sized packets remaining in the TD 1685 + * (*not* including this TRB). 1686 + * 1687 + * Total TD packet count = total_packet_count = 1688 + * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize) 1689 + * 1690 + * Packets transferred up to and including this TRB = packets_transferred = 1691 + * rounddown(total bytes transferred including this TRB / wMaxPacketSize) 1692 + * 1693 + * TD size = total_packet_count - packets_transferred 1694 + * 1695 + * It must fit in bits 21:17, so it can't be bigger than 31. 1696 + * This is taken care of in the TRB_TD_SIZE() macro 1697 + * 1698 + * The last TRB in a TD must have the TD size set to zero. 1699 + */ 1700 + static u32 cdnsp_td_remainder(struct cdnsp_device *pdev, 1701 + int transferred, 1702 + int trb_buff_len, 1703 + unsigned int td_total_len, 1704 + struct cdnsp_request *preq, 1705 + bool more_trbs_coming) 1706 + { 1707 + u32 maxp, total_packet_count; 1708 + 1709 + /* One TRB with a zero-length data packet. */ 1710 + if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) || 1711 + trb_buff_len == td_total_len) 1712 + return 0; 1713 + 1714 + maxp = usb_endpoint_maxp(preq->pep->endpoint.desc); 1715 + total_packet_count = DIV_ROUND_UP(td_total_len, maxp); 1716 + 1717 + /* Queuing functions don't count the current TRB into transferred. */ 1718 + return (total_packet_count - ((transferred + trb_buff_len) / maxp)); 1719 + } 1720 + 1721 + static int cdnsp_align_td(struct cdnsp_device *pdev, 1722 + struct cdnsp_request *preq, u32 enqd_len, 1723 + u32 *trb_buff_len, struct cdnsp_segment *seg) 1724 + { 1725 + struct device *dev = pdev->dev; 1726 + unsigned int unalign; 1727 + unsigned int max_pkt; 1728 + u32 new_buff_len; 1729 + 1730 + max_pkt = usb_endpoint_maxp(preq->pep->endpoint.desc); 1731 + unalign = (enqd_len + *trb_buff_len) % max_pkt; 1732 + 1733 + /* We got lucky, last normal TRB data on segment is packet aligned. */ 1734 + if (unalign == 0) 1735 + return 0; 1736 + 1737 + /* Is the last nornal TRB alignable by splitting it. */ 1738 + if (*trb_buff_len > unalign) { 1739 + *trb_buff_len -= unalign; 1740 + return 0; 1741 + } 1742 + 1743 + /* 1744 + * We want enqd_len + trb_buff_len to sum up to a number aligned to 1745 + * number which is divisible by the endpoint's wMaxPacketSize. IOW: 1746 + * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0. 1747 + */ 1748 + new_buff_len = max_pkt - (enqd_len % max_pkt); 1749 + 1750 + if (new_buff_len > (preq->request.length - enqd_len)) 1751 + new_buff_len = (preq->request.length - enqd_len); 1752 + 1753 + /* Create a max max_pkt sized bounce buffer pointed to by last trb. */ 1754 + if (preq->direction) { 1755 + sg_pcopy_to_buffer(preq->request.sg, 1756 + preq->request.num_mapped_sgs, 1757 + seg->bounce_buf, new_buff_len, enqd_len); 1758 + seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 1759 + max_pkt, DMA_TO_DEVICE); 1760 + } else { 1761 + seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 1762 + max_pkt, DMA_FROM_DEVICE); 1763 + } 1764 + 1765 + if (dma_mapping_error(dev, seg->bounce_dma)) { 1766 + /* Try without aligning.*/ 1767 + dev_warn(pdev->dev, 1768 + "Failed mapping bounce buffer, not aligning\n"); 1769 + return 0; 1770 + } 1771 + 1772 + *trb_buff_len = new_buff_len; 1773 + seg->bounce_len = new_buff_len; 1774 + seg->bounce_offs = enqd_len; 1775 + 1776 + /* 1777 + * Bounce buffer successful aligned and seg->bounce_dma will be used 1778 + * in transfer TRB as new transfer buffer address. 1779 + */ 1780 + return 1; 1781 + } 1782 + 1783 + int cdnsp_queue_bulk_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq) 1784 + { 1785 + unsigned int enqd_len, block_len, trb_buff_len, full_len; 1786 + unsigned int start_cycle, num_sgs = 0; 1787 + struct cdnsp_generic_trb *start_trb; 1788 + u32 field, length_field, remainder; 1789 + struct scatterlist *sg = NULL; 1790 + bool more_trbs_coming = true; 1791 + bool need_zero_pkt = false; 1792 + bool zero_len_trb = false; 1793 + struct cdnsp_ring *ring; 1794 + bool first_trb = true; 1795 + unsigned int num_trbs; 1796 + struct cdnsp_ep *pep; 1797 + u64 addr, send_addr; 1798 + int sent_len, ret; 1799 + 1800 + ring = cdnsp_request_to_transfer_ring(pdev, preq); 1801 + if (!ring) 1802 + return -EINVAL; 1803 + 1804 + full_len = preq->request.length; 1805 + 1806 + if (preq->request.num_sgs) { 1807 + num_sgs = preq->request.num_sgs; 1808 + sg = preq->request.sg; 1809 + addr = (u64)sg_dma_address(sg); 1810 + block_len = sg_dma_len(sg); 1811 + num_trbs = count_sg_trbs_needed(preq); 1812 + } else { 1813 + num_trbs = count_trbs_needed(preq); 1814 + addr = (u64)preq->request.dma; 1815 + block_len = full_len; 1816 + } 1817 + 1818 + pep = preq->pep; 1819 + 1820 + /* Deal with request.zero - need one more td/trb. */ 1821 + if (preq->request.zero && preq->request.length && 1822 + IS_ALIGNED(full_len, usb_endpoint_maxp(pep->endpoint.desc))) { 1823 + need_zero_pkt = true; 1824 + num_trbs++; 1825 + } 1826 + 1827 + ret = cdnsp_prepare_transfer(pdev, preq, num_trbs); 1828 + if (ret) 1829 + return ret; 1830 + 1831 + /* 1832 + * Don't give the first TRB to the hardware (by toggling the cycle bit) 1833 + * until we've finished creating all the other TRBs. The ring's cycle 1834 + * state may change as we enqueue the other TRBs, so save it too. 1835 + */ 1836 + start_trb = &ring->enqueue->generic; 1837 + start_cycle = ring->cycle_state; 1838 + send_addr = addr; 1839 + 1840 + /* Queue the TRBs, even if they are zero-length */ 1841 + for (enqd_len = 0; zero_len_trb || first_trb || enqd_len < full_len; 1842 + enqd_len += trb_buff_len) { 1843 + field = TRB_TYPE(TRB_NORMAL); 1844 + 1845 + /* TRB buffer should not cross 64KB boundaries */ 1846 + trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 1847 + trb_buff_len = min(trb_buff_len, block_len); 1848 + if (enqd_len + trb_buff_len > full_len) 1849 + trb_buff_len = full_len - enqd_len; 1850 + 1851 + /* Don't change the cycle bit of the first TRB until later */ 1852 + if (first_trb) { 1853 + first_trb = false; 1854 + if (start_cycle == 0) 1855 + field |= TRB_CYCLE; 1856 + } else { 1857 + field |= ring->cycle_state; 1858 + } 1859 + 1860 + /* 1861 + * Chain all the TRBs together; clear the chain bit in the last 1862 + * TRB to indicate it's the last TRB in the chain. 1863 + */ 1864 + if (enqd_len + trb_buff_len < full_len || need_zero_pkt) { 1865 + field |= TRB_CHAIN; 1866 + if (cdnsp_trb_is_link(ring->enqueue + 1)) { 1867 + if (cdnsp_align_td(pdev, preq, enqd_len, 1868 + &trb_buff_len, 1869 + ring->enq_seg)) { 1870 + send_addr = ring->enq_seg->bounce_dma; 1871 + /* Assuming TD won't span 2 segs */ 1872 + preq->td.bounce_seg = ring->enq_seg; 1873 + } 1874 + } 1875 + } 1876 + 1877 + if (enqd_len + trb_buff_len >= full_len) { 1878 + if (need_zero_pkt && zero_len_trb) { 1879 + zero_len_trb = true; 1880 + } else { 1881 + field &= ~TRB_CHAIN; 1882 + field |= TRB_IOC; 1883 + more_trbs_coming = false; 1884 + need_zero_pkt = false; 1885 + preq->td.last_trb = ring->enqueue; 1886 + } 1887 + } 1888 + 1889 + /* Only set interrupt on short packet for OUT endpoints. */ 1890 + if (!preq->direction) 1891 + field |= TRB_ISP; 1892 + 1893 + /* Set the TRB length, TD size, and interrupter fields. */ 1894 + remainder = cdnsp_td_remainder(pdev, enqd_len, trb_buff_len, 1895 + full_len, preq, 1896 + more_trbs_coming); 1897 + 1898 + length_field = TRB_LEN(trb_buff_len) | TRB_TD_SIZE(remainder) | 1899 + TRB_INTR_TARGET(0); 1900 + 1901 + cdnsp_queue_trb(pdev, ring, more_trbs_coming | need_zero_pkt, 1902 + lower_32_bits(send_addr), 1903 + upper_32_bits(send_addr), 1904 + length_field, 1905 + field); 1906 + 1907 + addr += trb_buff_len; 1908 + sent_len = trb_buff_len; 1909 + while (sg && sent_len >= block_len) { 1910 + /* New sg entry */ 1911 + --num_sgs; 1912 + sent_len -= block_len; 1913 + if (num_sgs != 0) { 1914 + sg = sg_next(sg); 1915 + block_len = sg_dma_len(sg); 1916 + addr = (u64)sg_dma_address(sg); 1917 + addr += sent_len; 1918 + } 1919 + } 1920 + block_len -= sent_len; 1921 + send_addr = addr; 1922 + } 1923 + 1924 + cdnsp_check_trb_math(preq, enqd_len); 1925 + ret = cdnsp_giveback_first_trb(pdev, pep, preq->request.stream_id, 1926 + start_cycle, start_trb); 1927 + 1928 + if (ret) 1929 + preq->td.drbl = 1; 1930 + 1931 + return 0; 1932 + } 1933 + 1934 + int cdnsp_queue_ctrl_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq) 1935 + { 1936 + u32 field, length_field, remainder; 1937 + struct cdnsp_ep *pep = preq->pep; 1938 + struct cdnsp_ring *ep_ring; 1939 + int num_trbs; 1940 + int ret; 1941 + 1942 + ep_ring = cdnsp_request_to_transfer_ring(pdev, preq); 1943 + if (!ep_ring) 1944 + return -EINVAL; 1945 + 1946 + /* 1 TRB for data, 1 for status */ 1947 + num_trbs = (pdev->three_stage_setup) ? 2 : 1; 1948 + 1949 + ret = cdnsp_prepare_transfer(pdev, preq, num_trbs); 1950 + if (ret) 1951 + return ret; 1952 + 1953 + /* If there's data, queue data TRBs */ 1954 + if (pdev->ep0_expect_in) 1955 + field = TRB_TYPE(TRB_DATA) | TRB_IOC; 1956 + else 1957 + field = TRB_ISP | TRB_TYPE(TRB_DATA) | TRB_IOC; 1958 + 1959 + if (preq->request.length > 0) { 1960 + remainder = cdnsp_td_remainder(pdev, 0, preq->request.length, 1961 + preq->request.length, preq, 1); 1962 + 1963 + length_field = TRB_LEN(preq->request.length) | 1964 + TRB_TD_SIZE(remainder) | TRB_INTR_TARGET(0); 1965 + 1966 + if (pdev->ep0_expect_in) 1967 + field |= TRB_DIR_IN; 1968 + 1969 + cdnsp_queue_trb(pdev, ep_ring, true, 1970 + lower_32_bits(preq->request.dma), 1971 + upper_32_bits(preq->request.dma), length_field, 1972 + field | ep_ring->cycle_state | 1973 + TRB_SETUPID(pdev->setup_id) | 1974 + pdev->setup_speed); 1975 + 1976 + pdev->ep0_stage = CDNSP_DATA_STAGE; 1977 + } 1978 + 1979 + /* Save the DMA address of the last TRB in the TD. */ 1980 + preq->td.last_trb = ep_ring->enqueue; 1981 + 1982 + /* Queue status TRB. */ 1983 + if (preq->request.length == 0) 1984 + field = ep_ring->cycle_state; 1985 + else 1986 + field = (ep_ring->cycle_state ^ 1); 1987 + 1988 + if (preq->request.length > 0 && pdev->ep0_expect_in) 1989 + field |= TRB_DIR_IN; 1990 + 1991 + if (pep->ep_state & EP0_HALTED_STATUS) { 1992 + pep->ep_state &= ~EP0_HALTED_STATUS; 1993 + field |= TRB_SETUPSTAT(TRB_SETUPSTAT_STALL); 1994 + } else { 1995 + field |= TRB_SETUPSTAT(TRB_SETUPSTAT_ACK); 1996 + } 1997 + 1998 + cdnsp_queue_trb(pdev, ep_ring, false, 0, 0, TRB_INTR_TARGET(0), 1999 + field | TRB_IOC | TRB_SETUPID(pdev->setup_id) | 2000 + TRB_TYPE(TRB_STATUS) | pdev->setup_speed); 2001 + 2002 + cdnsp_ring_ep_doorbell(pdev, pep, preq->request.stream_id); 2003 + 2004 + return 0; 2005 + } 2006 + 2007 + int cdnsp_cmd_stop_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep) 2008 + { 2009 + u32 ep_state = GET_EP_CTX_STATE(pep->out_ctx); 2010 + int ret = 0; 2011 + 2012 + if (ep_state == EP_STATE_STOPPED || ep_state == EP_STATE_DISABLED) 2013 + goto ep_stopped; 2014 + 2015 + cdnsp_queue_stop_endpoint(pdev, pep->idx); 2016 + cdnsp_ring_cmd_db(pdev); 2017 + ret = cdnsp_wait_for_cmd_compl(pdev); 2018 + 2019 + ep_stopped: 2020 + pep->ep_state |= EP_STOPPED; 2021 + return ret; 2022 + } 2023 + 2024 + int cdnsp_cmd_flush_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep) 2025 + { 2026 + int ret; 2027 + 2028 + cdnsp_queue_flush_endpoint(pdev, pep->idx); 2029 + cdnsp_ring_cmd_db(pdev); 2030 + ret = cdnsp_wait_for_cmd_compl(pdev); 2031 + 2032 + return ret; 2033 + } 2034 + 2035 + /* 2036 + * The transfer burst count field of the isochronous TRB defines the number of 2037 + * bursts that are required to move all packets in this TD. Only SuperSpeed 2038 + * devices can burst up to bMaxBurst number of packets per service interval. 2039 + * This field is zero based, meaning a value of zero in the field means one 2040 + * burst. Basically, for everything but SuperSpeed devices, this field will be 2041 + * zero. 2042 + */ 2043 + static unsigned int cdnsp_get_burst_count(struct cdnsp_device *pdev, 2044 + struct cdnsp_request *preq, 2045 + unsigned int total_packet_count) 2046 + { 2047 + unsigned int max_burst; 2048 + 2049 + if (pdev->gadget.speed < USB_SPEED_SUPER) 2050 + return 0; 2051 + 2052 + max_burst = preq->pep->endpoint.comp_desc->bMaxBurst; 2053 + return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; 2054 + } 2055 + 2056 + /* 2057 + * Returns the number of packets in the last "burst" of packets. This field is 2058 + * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so 2059 + * the last burst packet count is equal to the total number of packets in the 2060 + * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst 2061 + * must contain (bMaxBurst + 1) number of packets, but the last burst can 2062 + * contain 1 to (bMaxBurst + 1) packets. 2063 + */ 2064 + static unsigned int 2065 + cdnsp_get_last_burst_packet_count(struct cdnsp_device *pdev, 2066 + struct cdnsp_request *preq, 2067 + unsigned int total_packet_count) 2068 + { 2069 + unsigned int max_burst; 2070 + unsigned int residue; 2071 + 2072 + if (pdev->gadget.speed >= USB_SPEED_SUPER) { 2073 + /* bMaxBurst is zero based: 0 means 1 packet per burst. */ 2074 + max_burst = preq->pep->endpoint.comp_desc->bMaxBurst; 2075 + residue = total_packet_count % (max_burst + 1); 2076 + 2077 + /* 2078 + * If residue is zero, the last burst contains (max_burst + 1) 2079 + * number of packets, but the TLBPC field is zero-based. 2080 + */ 2081 + if (residue == 0) 2082 + return max_burst; 2083 + 2084 + return residue - 1; 2085 + } 2086 + if (total_packet_count == 0) 2087 + return 0; 2088 + 2089 + return total_packet_count - 1; 2090 + } 2091 + 2092 + /* Queue function isoc transfer */ 2093 + static int cdnsp_queue_isoc_tx(struct cdnsp_device *pdev, 2094 + struct cdnsp_request *preq) 2095 + { 2096 + int trb_buff_len, td_len, td_remain_len, ret; 2097 + unsigned int burst_count, last_burst_pkt; 2098 + unsigned int total_pkt_count, max_pkt; 2099 + struct cdnsp_generic_trb *start_trb; 2100 + bool more_trbs_coming = true; 2101 + struct cdnsp_ring *ep_ring; 2102 + int running_total = 0; 2103 + u32 field, length_field; 2104 + int start_cycle; 2105 + int trbs_per_td; 2106 + u64 addr; 2107 + int i; 2108 + 2109 + ep_ring = preq->pep->ring; 2110 + start_trb = &ep_ring->enqueue->generic; 2111 + start_cycle = ep_ring->cycle_state; 2112 + td_len = preq->request.length; 2113 + addr = (u64)preq->request.dma; 2114 + td_remain_len = td_len; 2115 + 2116 + max_pkt = usb_endpoint_maxp(preq->pep->endpoint.desc); 2117 + total_pkt_count = DIV_ROUND_UP(td_len, max_pkt); 2118 + 2119 + /* A zero-length transfer still involves at least one packet. */ 2120 + if (total_pkt_count == 0) 2121 + total_pkt_count++; 2122 + 2123 + burst_count = cdnsp_get_burst_count(pdev, preq, total_pkt_count); 2124 + last_burst_pkt = cdnsp_get_last_burst_packet_count(pdev, preq, 2125 + total_pkt_count); 2126 + trbs_per_td = count_isoc_trbs_needed(preq); 2127 + 2128 + ret = cdnsp_prepare_transfer(pdev, preq, trbs_per_td); 2129 + if (ret) 2130 + goto cleanup; 2131 + 2132 + /* 2133 + * Set isoc specific data for the first TRB in a TD. 2134 + * Prevent HW from getting the TRBs by keeping the cycle state 2135 + * inverted in the first TDs isoc TRB. 2136 + */ 2137 + field = TRB_TYPE(TRB_ISOC) | TRB_TLBPC(last_burst_pkt) | 2138 + !start_cycle | TRB_SIA | TRB_TBC(burst_count); 2139 + 2140 + /* Fill the rest of the TRB fields, and remaining normal TRBs. */ 2141 + for (i = 0; i < trbs_per_td; i++) { 2142 + u32 remainder; 2143 + 2144 + /* Calculate TRB length. */ 2145 + trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 2146 + if (trb_buff_len > td_remain_len) 2147 + trb_buff_len = td_remain_len; 2148 + 2149 + /* Set the TRB length, TD size, & interrupter fields. */ 2150 + remainder = cdnsp_td_remainder(pdev, running_total, 2151 + trb_buff_len, td_len, preq, 2152 + more_trbs_coming); 2153 + 2154 + length_field = TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0); 2155 + 2156 + /* Only first TRB is isoc, overwrite otherwise. */ 2157 + if (i) { 2158 + field = TRB_TYPE(TRB_NORMAL) | ep_ring->cycle_state; 2159 + length_field |= TRB_TD_SIZE(remainder); 2160 + } else { 2161 + length_field |= TRB_TD_SIZE_TBC(burst_count); 2162 + } 2163 + 2164 + /* Only set interrupt on short packet for OUT EPs. */ 2165 + if (usb_endpoint_dir_out(preq->pep->endpoint.desc)) 2166 + field |= TRB_ISP; 2167 + 2168 + /* Set the chain bit for all except the last TRB. */ 2169 + if (i < trbs_per_td - 1) { 2170 + more_trbs_coming = true; 2171 + field |= TRB_CHAIN; 2172 + } else { 2173 + more_trbs_coming = false; 2174 + preq->td.last_trb = ep_ring->enqueue; 2175 + field |= TRB_IOC; 2176 + } 2177 + 2178 + cdnsp_queue_trb(pdev, ep_ring, more_trbs_coming, 2179 + lower_32_bits(addr), upper_32_bits(addr), 2180 + length_field, field); 2181 + 2182 + running_total += trb_buff_len; 2183 + addr += trb_buff_len; 2184 + td_remain_len -= trb_buff_len; 2185 + } 2186 + 2187 + /* Check TD length */ 2188 + if (running_total != td_len) { 2189 + dev_err(pdev->dev, "ISOC TD length unmatch\n"); 2190 + ret = -EINVAL; 2191 + goto cleanup; 2192 + } 2193 + 2194 + cdnsp_giveback_first_trb(pdev, preq->pep, preq->request.stream_id, 2195 + start_cycle, start_trb); 2196 + 2197 + return 0; 2198 + 2199 + cleanup: 2200 + /* Clean up a partially enqueued isoc transfer. */ 2201 + list_del_init(&preq->td.td_list); 2202 + ep_ring->num_tds--; 2203 + 2204 + /* 2205 + * Use the first TD as a temporary variable to turn the TDs we've 2206 + * queued into No-ops with a software-owned cycle bit. 2207 + * That way the hardware won't accidentally start executing bogus TDs 2208 + * when we partially overwrite them. 2209 + * td->first_trb and td->start_seg are already set. 2210 + */ 2211 + preq->td.last_trb = ep_ring->enqueue; 2212 + /* Every TRB except the first & last will have its cycle bit flipped. */ 2213 + cdnsp_td_to_noop(pdev, ep_ring, &preq->td, true); 2214 + 2215 + /* Reset the ring enqueue back to the first TRB and its cycle bit. */ 2216 + ep_ring->enqueue = preq->td.first_trb; 2217 + ep_ring->enq_seg = preq->td.start_seg; 2218 + ep_ring->cycle_state = start_cycle; 2219 + return ret; 2220 + } 2221 + 2222 + int cdnsp_queue_isoc_tx_prepare(struct cdnsp_device *pdev, 2223 + struct cdnsp_request *preq) 2224 + { 2225 + struct cdnsp_ring *ep_ring; 2226 + u32 ep_state; 2227 + int num_trbs; 2228 + int ret; 2229 + 2230 + ep_ring = preq->pep->ring; 2231 + ep_state = GET_EP_CTX_STATE(preq->pep->out_ctx); 2232 + num_trbs = count_isoc_trbs_needed(preq); 2233 + 2234 + /* 2235 + * Check the ring to guarantee there is enough room for the whole 2236 + * request. Do not insert any td of the USB Request to the ring if the 2237 + * check failed. 2238 + */ 2239 + ret = cdnsp_prepare_ring(pdev, ep_ring, ep_state, num_trbs, GFP_ATOMIC); 2240 + if (ret) 2241 + return ret; 2242 + 2243 + return cdnsp_queue_isoc_tx(pdev, preq); 2244 + } 2245 + 2246 + /**** Command Ring Operations ****/ 2247 + /* 2248 + * Generic function for queuing a command TRB on the command ring. 2249 + * Driver queue only one command to ring in the moment. 2250 + */ 2251 + static void cdnsp_queue_command(struct cdnsp_device *pdev, 2252 + u32 field1, 2253 + u32 field2, 2254 + u32 field3, 2255 + u32 field4) 2256 + { 2257 + cdnsp_prepare_ring(pdev, pdev->cmd_ring, EP_STATE_RUNNING, 1, 2258 + GFP_ATOMIC); 2259 + 2260 + pdev->cmd.command_trb = pdev->cmd_ring->enqueue; 2261 + 2262 + cdnsp_queue_trb(pdev, pdev->cmd_ring, false, field1, field2, 2263 + field3, field4 | pdev->cmd_ring->cycle_state); 2264 + } 2265 + 2266 + /* Queue a slot enable or disable request on the command ring */ 2267 + void cdnsp_queue_slot_control(struct cdnsp_device *pdev, u32 trb_type) 2268 + { 2269 + cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(trb_type) | 2270 + SLOT_ID_FOR_TRB(pdev->slot_id)); 2271 + } 2272 + 2273 + /* Queue an address device command TRB */ 2274 + void cdnsp_queue_address_device(struct cdnsp_device *pdev, 2275 + dma_addr_t in_ctx_ptr, 2276 + enum cdnsp_setup_dev setup) 2277 + { 2278 + cdnsp_queue_command(pdev, lower_32_bits(in_ctx_ptr), 2279 + upper_32_bits(in_ctx_ptr), 0, 2280 + TRB_TYPE(TRB_ADDR_DEV) | 2281 + SLOT_ID_FOR_TRB(pdev->slot_id) | 2282 + (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0)); 2283 + } 2284 + 2285 + /* Queue a reset device command TRB */ 2286 + void cdnsp_queue_reset_device(struct cdnsp_device *pdev) 2287 + { 2288 + cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_RESET_DEV) | 2289 + SLOT_ID_FOR_TRB(pdev->slot_id)); 2290 + } 2291 + 2292 + /* Queue a configure endpoint command TRB */ 2293 + void cdnsp_queue_configure_endpoint(struct cdnsp_device *pdev, 2294 + dma_addr_t in_ctx_ptr) 2295 + { 2296 + cdnsp_queue_command(pdev, lower_32_bits(in_ctx_ptr), 2297 + upper_32_bits(in_ctx_ptr), 0, 2298 + TRB_TYPE(TRB_CONFIG_EP) | 2299 + SLOT_ID_FOR_TRB(pdev->slot_id)); 2300 + } 2301 + 2302 + /* 2303 + * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop 2304 + * activity on an endpoint that is about to be suspended. 2305 + */ 2306 + void cdnsp_queue_stop_endpoint(struct cdnsp_device *pdev, unsigned int ep_index) 2307 + { 2308 + cdnsp_queue_command(pdev, 0, 0, 0, SLOT_ID_FOR_TRB(pdev->slot_id) | 2309 + EP_ID_FOR_TRB(ep_index) | TRB_TYPE(TRB_STOP_RING)); 2310 + } 2311 + 2312 + /* Set Transfer Ring Dequeue Pointer command. */ 2313 + void cdnsp_queue_new_dequeue_state(struct cdnsp_device *pdev, 2314 + struct cdnsp_ep *pep, 2315 + struct cdnsp_dequeue_state *deq_state) 2316 + { 2317 + u32 trb_stream_id = STREAM_ID_FOR_TRB(deq_state->stream_id); 2318 + u32 trb_slot_id = SLOT_ID_FOR_TRB(pdev->slot_id); 2319 + u32 type = TRB_TYPE(TRB_SET_DEQ); 2320 + u32 trb_sct = 0; 2321 + dma_addr_t addr; 2322 + 2323 + addr = cdnsp_trb_virt_to_dma(deq_state->new_deq_seg, 2324 + deq_state->new_deq_ptr); 2325 + 2326 + if (deq_state->stream_id) 2327 + trb_sct = SCT_FOR_TRB(SCT_PRI_TR); 2328 + 2329 + cdnsp_queue_command(pdev, lower_32_bits(addr) | trb_sct | 2330 + deq_state->new_cycle_state, upper_32_bits(addr), 2331 + trb_stream_id, trb_slot_id | 2332 + EP_ID_FOR_TRB(pep->idx) | type); 2333 + } 2334 + 2335 + void cdnsp_queue_reset_ep(struct cdnsp_device *pdev, unsigned int ep_index) 2336 + { 2337 + return cdnsp_queue_command(pdev, 0, 0, 0, 2338 + SLOT_ID_FOR_TRB(pdev->slot_id) | 2339 + EP_ID_FOR_TRB(ep_index) | 2340 + TRB_TYPE(TRB_RESET_EP)); 2341 + } 2342 + 2343 + /* 2344 + * Queue a halt endpoint request on the command ring. 2345 + */ 2346 + void cdnsp_queue_halt_endpoint(struct cdnsp_device *pdev, unsigned int ep_index) 2347 + { 2348 + cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_HALT_ENDPOINT) | 2349 + SLOT_ID_FOR_TRB(pdev->slot_id) | 2350 + EP_ID_FOR_TRB(ep_index)); 2351 + } 2352 + 2353 + /* 2354 + * Queue a flush endpoint request on the command ring. 2355 + */ 2356 + void cdnsp_queue_flush_endpoint(struct cdnsp_device *pdev, 2357 + unsigned int ep_index) 2358 + { 2359 + cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_FLUSH_ENDPOINT) | 2360 + SLOT_ID_FOR_TRB(pdev->slot_id) | 2361 + EP_ID_FOR_TRB(ep_index)); 2362 + } 2363 + 2364 + void cdnsp_force_header_wakeup(struct cdnsp_device *pdev, int intf_num) 2365 + { 2366 + u32 lo, mid; 2367 + 2368 + lo = TRB_FH_TO_PACKET_TYPE(TRB_FH_TR_PACKET) | 2369 + TRB_FH_TO_DEVICE_ADDRESS(pdev->device_address); 2370 + mid = TRB_FH_TR_PACKET_DEV_NOT | 2371 + TRB_FH_TO_NOT_TYPE(TRB_FH_TR_PACKET_FUNCTION_WAKE) | 2372 + TRB_FH_TO_INTERFACE(intf_num); 2373 + 2374 + cdnsp_queue_command(pdev, lo, mid, 0, 2375 + TRB_TYPE(TRB_FORCE_HEADER) | SET_PORT_ID(2)); 2376 + }
+9 -2
drivers/usb/cdns3/core.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* 3 - * Cadence USBSS DRD Driver. 3 + * Cadence USBSS and USBSSP DRD Driver. 4 4 * 5 5 * Copyright (C) 2018-2019 Cadence. 6 6 * Copyright (C) 2017-2018 NXP ··· 136 136 dr_mode = best_dr_mode; 137 137 138 138 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) { 139 - ret = cdns_host_init(cdns); 139 + if ((cdns->version == CDNSP_CONTROLLER_V2 && 140 + IS_ENABLED(CONFIG_USB_CDNSP_HOST)) || 141 + (cdns->version < CDNSP_CONTROLLER_V2 && 142 + IS_ENABLED(CONFIG_USB_CDNS3_HOST))) 143 + ret = cdns_host_init(cdns); 144 + else 145 + ret = -ENXIO; 146 + 140 147 if (ret) { 141 148 dev_err(dev, "Host initialization failed with %d\n", 142 149 ret);
+1 -1
drivers/usb/cdns3/core.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 /* 3 - * Cadence USBSS DRD Header File. 3 + * Cadence USBSS and USBSSP DRD Header File. 4 4 * 5 5 * Copyright (C) 2017-2018 NXP 6 6 * Copyright (C) 2018-2019 Cadence.
+28 -1
drivers/usb/cdns3/drd.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* 3 - * Cadence USBSS DRD Driver. 3 + * Cadence USBSS and USBSSP DRD Driver. 4 4 * 5 5 * Copyright (C) 2018-2020 Cadence. 6 6 * Copyright (C) 2019 Texas Instruments ··· 102 102 103 103 return vbus; 104 104 } 105 + 106 + void cdns_clear_vbus(struct cdns *cdns) 107 + { 108 + u32 reg; 109 + 110 + if (cdns->version != CDNSP_CONTROLLER_V2) 111 + return; 112 + 113 + reg = readl(&cdns->otg_cdnsp_regs->override); 114 + reg |= OVERRIDE_SESS_VLD_SEL; 115 + writel(reg, &cdns->otg_cdnsp_regs->override); 116 + } 117 + EXPORT_SYMBOL_GPL(cdns_clear_vbus); 118 + 119 + void cdns_set_vbus(struct cdns *cdns) 120 + { 121 + u32 reg; 122 + 123 + if (cdns->version != CDNSP_CONTROLLER_V2) 124 + return; 125 + 126 + reg = readl(&cdns->otg_cdnsp_regs->override); 127 + reg &= ~OVERRIDE_SESS_VLD_SEL; 128 + writel(reg, &cdns->otg_cdnsp_regs->override); 129 + } 130 + EXPORT_SYMBOL_GPL(cdns_set_vbus); 105 131 106 132 bool cdns_is_host(struct cdns *cdns) 107 133 { ··· 475 449 int cdns_drd_exit(struct cdns *cdns) 476 450 { 477 451 cdns_otg_disable_irq(cdns); 452 + 478 453 return 0; 479 454 }
+2
drivers/usb/cdns3/drd.h
··· 206 206 bool cdns_is_device(struct cdns *cdns); 207 207 int cdns_get_id(struct cdns *cdns); 208 208 int cdns_get_vbus(struct cdns *cdns); 209 + void cdns_clear_vbus(struct cdns *cdns); 210 + void cdns_set_vbus(struct cdns *cdns); 209 211 int cdns_drd_init(struct cdns *cdns); 210 212 int cdns_drd_exit(struct cdns *cdns); 211 213 int cdns_drd_update_mode(struct cdns *cdns);
+15 -3
drivers/usb/cdns3/gadget-export.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 /* 3 - * Cadence USBSS DRD Driver - Gadget Export APIs. 3 + * Cadence USBSS and USBSSP DRD Driver - Gadget Export APIs. 4 4 * 5 5 * Copyright (C) 2017 NXP 6 6 * Copyright (C) 2017-2018 NXP ··· 10 10 #ifndef __LINUX_CDNS3_GADGET_EXPORT 11 11 #define __LINUX_CDNS3_GADGET_EXPORT 12 12 13 - #ifdef CONFIG_USB_CDNS3_GADGET 13 + #if IS_ENABLED(CONFIG_USB_CDNSP_GADGET) 14 + 15 + int cdnsp_gadget_init(struct cdns *cdns); 16 + #else 17 + 18 + static inline int cdnsp_gadget_init(struct cdns *cdns) 19 + { 20 + return -ENXIO; 21 + } 22 + 23 + #endif /* CONFIG_USB_CDNSP_GADGET */ 24 + 25 + #if IS_ENABLED(CONFIG_USB_CDNS3_GADGET) 14 26 15 27 int cdns3_gadget_init(struct cdns *cdns); 16 28 #else ··· 32 20 return -ENXIO; 33 21 } 34 22 35 - #endif 23 + #endif /* CONFIG_USB_CDNS3_GADGET */ 36 24 37 25 #endif /* __LINUX_CDNS3_GADGET_EXPORT */
+4 -3
drivers/usb/cdns3/host-export.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 /* 3 - * Cadence USBSS DRD Driver - Host Export APIs 3 + * Cadence USBSS and USBSSP DRD Driver - Host Export APIs 4 4 * 5 5 * Copyright (C) 2017-2018 NXP 6 6 * ··· 9 9 #ifndef __LINUX_CDNS3_HOST_EXPORT 10 10 #define __LINUX_CDNS3_HOST_EXPORT 11 11 12 + #if IS_ENABLED(CONFIG_USB_CDNS_HOST) 13 + 12 14 struct usb_hcd; 13 - #ifdef CONFIG_USB_CDNS3_HOST 14 15 15 16 int cdns_host_init(struct cdns *cdns); 16 17 int xhci_cdns3_suspend_quirk(struct usb_hcd *hcd); ··· 29 28 return 0; 30 29 } 31 30 32 - #endif /* CONFIG_USB_CDNS3_HOST */ 31 + #endif /* USB_CDNS_HOST */ 33 32 34 33 #endif /* __LINUX_CDNS3_HOST_EXPORT */