Vignesh Raghavendra | b1a4928 | 2019-10-01 17:26:33 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Cadence USBSS DRD Driver - gadget side. |
| 4 | * |
| 5 | * Copyright (C) 2018 Cadence Design Systems. |
| 6 | * Copyright (C) 2017-2018 NXP |
| 7 | * |
| 8 | * Authors: Pawel Jez <pjez@cadence.com>, |
| 9 | * Pawel Laszczak <pawell@cadence.com> |
| 10 | * Peter Chen <peter.chen@nxp.com> |
| 11 | */ |
| 12 | |
Vignesh Raghavendra | 42360c4 | 2019-12-05 13:29:25 +0530 | [diff] [blame] | 13 | #include <cpu_func.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Vignesh Raghavendra | b1a4928 | 2019-10-01 17:26:33 +0530 | [diff] [blame] | 18 | #include <linux/usb/composite.h> |
| 19 | #include <linux/iopoll.h> |
| 20 | |
| 21 | #include "gadget.h" |
| 22 | #include "trace.h" |
| 23 | |
| 24 | #define readl_poll_timeout_atomic readl_poll_timeout |
| 25 | #define usleep_range(a, b) udelay((b)) |
| 26 | |
| 27 | static struct usb_endpoint_descriptor cdns3_gadget_ep0_desc = { |
| 28 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 29 | .bDescriptorType = USB_DT_ENDPOINT, |
| 30 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 31 | }; |
| 32 | |
| 33 | /** |
| 34 | * cdns3_ep0_run_transfer - Do transfer on default endpoint hardware |
| 35 | * @priv_dev: extended gadget object |
| 36 | * @dma_addr: physical address where data is/will be stored |
| 37 | * @length: data length |
| 38 | * @erdy: set it to 1 when ERDY packet should be sent - |
| 39 | * exit from flow control state |
| 40 | */ |
| 41 | static void cdns3_ep0_run_transfer(struct cdns3_device *priv_dev, |
| 42 | dma_addr_t dma_addr, |
| 43 | unsigned int length, int erdy, int zlp) |
| 44 | { |
| 45 | struct cdns3_usb_regs __iomem *regs = priv_dev->regs; |
| 46 | struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; |
| 47 | |
| 48 | priv_ep->trb_pool[0].buffer = TRB_BUFFER(dma_addr); |
| 49 | priv_ep->trb_pool[0].length = TRB_LEN(length); |
| 50 | |
| 51 | if (zlp) { |
| 52 | priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_TYPE(TRB_NORMAL); |
| 53 | priv_ep->trb_pool[1].buffer = TRB_BUFFER(dma_addr); |
| 54 | priv_ep->trb_pool[1].length = TRB_LEN(0); |
| 55 | priv_ep->trb_pool[1].control = TRB_CYCLE | TRB_IOC | |
| 56 | TRB_TYPE(TRB_NORMAL); |
| 57 | } else { |
| 58 | priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_IOC | |
| 59 | TRB_TYPE(TRB_NORMAL); |
| 60 | priv_ep->trb_pool[1].control = 0; |
| 61 | } |
| 62 | |
| 63 | /* Flush both TRBs */ |
| 64 | flush_dcache_range((unsigned long)priv_ep->trb_pool, |
| 65 | (unsigned long)priv_ep->trb_pool + |
| 66 | ROUND(sizeof(struct cdns3_trb) * 2, |
| 67 | CONFIG_SYS_CACHELINE_SIZE)); |
| 68 | |
| 69 | trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool); |
| 70 | |
| 71 | cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir); |
| 72 | |
| 73 | writel(EP_STS_TRBERR, ®s->ep_sts); |
| 74 | writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), ®s->ep_traddr); |
| 75 | trace_cdns3_doorbell_ep0(priv_dev->ep0_data_dir ? "ep0in" : "ep0out", |
| 76 | readl(®s->ep_traddr)); |
| 77 | |
| 78 | /* TRB should be prepared before starting transfer. */ |
| 79 | writel(EP_CMD_DRDY, ®s->ep_cmd); |
| 80 | |
| 81 | /* Resume controller before arming transfer. */ |
| 82 | __cdns3_gadget_wakeup(priv_dev); |
| 83 | |
| 84 | if (erdy) |
| 85 | writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * cdns3_ep0_delegate_req - Returns status of handling setup packet |
| 90 | * Setup is handled by gadget driver |
| 91 | * @priv_dev: extended gadget object |
| 92 | * @ctrl_req: pointer to received setup packet |
| 93 | * |
| 94 | * Returns zero on success or negative value on failure |
| 95 | */ |
| 96 | static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev, |
| 97 | struct usb_ctrlrequest *ctrl_req) |
| 98 | { |
| 99 | int ret; |
| 100 | |
| 101 | spin_unlock(&priv_dev->lock); |
| 102 | priv_dev->setup_pending = 1; |
| 103 | ret = priv_dev->gadget_driver->setup(&priv_dev->gadget, ctrl_req); |
| 104 | priv_dev->setup_pending = 0; |
| 105 | spin_lock(&priv_dev->lock); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev) |
| 110 | { |
| 111 | priv_dev->ep0_data_dir = 0; |
| 112 | priv_dev->ep0_stage = CDNS3_SETUP_STAGE; |
| 113 | cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, |
| 114 | sizeof(struct usb_ctrlrequest), 0, 0); |
| 115 | } |
| 116 | |
| 117 | static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev, |
| 118 | u8 send_stall, u8 send_erdy) |
| 119 | { |
| 120 | struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; |
| 121 | struct usb_request *request; |
| 122 | |
| 123 | request = cdns3_next_request(&priv_ep->pending_req_list); |
| 124 | if (request) |
| 125 | list_del_init(&request->list); |
| 126 | |
| 127 | if (send_stall) { |
| 128 | trace_cdns3_halt(priv_ep, send_stall, 0); |
| 129 | /* set_stall on ep0 */ |
| 130 | cdns3_select_ep(priv_dev, 0x00); |
| 131 | writel(EP_CMD_SSTALL, &priv_dev->regs->ep_cmd); |
| 132 | } else { |
| 133 | cdns3_prepare_setup_packet(priv_dev); |
| 134 | } |
| 135 | |
| 136 | priv_dev->ep0_stage = CDNS3_SETUP_STAGE; |
| 137 | writel((send_erdy ? EP_CMD_ERDY : 0) | EP_CMD_REQ_CMPL, |
| 138 | &priv_dev->regs->ep_cmd); |
| 139 | |
| 140 | cdns3_allow_enable_l1(priv_dev, 1); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * cdns3_req_ep0_set_configuration - Handling of SET_CONFIG standard USB request |
| 145 | * @priv_dev: extended gadget object |
| 146 | * @ctrl_req: pointer to received setup packet |
| 147 | * |
| 148 | * Returns 0 if success, USB_GADGET_DELAYED_STATUS on deferred status stage, |
| 149 | * error code on error |
| 150 | */ |
| 151 | static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev, |
| 152 | struct usb_ctrlrequest *ctrl_req) |
| 153 | { |
| 154 | enum usb_device_state device_state = priv_dev->gadget.state; |
| 155 | struct cdns3_endpoint *priv_ep; |
| 156 | u32 config = le16_to_cpu(ctrl_req->wValue); |
| 157 | int result = 0; |
| 158 | int i; |
| 159 | |
| 160 | switch (device_state) { |
| 161 | case USB_STATE_ADDRESS: |
| 162 | /* Configure non-control EPs */ |
| 163 | for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) { |
| 164 | priv_ep = priv_dev->eps[i]; |
| 165 | if (!priv_ep) |
| 166 | continue; |
| 167 | |
| 168 | if (priv_ep->flags & EP_CLAIMED) |
| 169 | cdns3_ep_config(priv_ep); |
| 170 | } |
| 171 | |
| 172 | result = cdns3_ep0_delegate_req(priv_dev, ctrl_req); |
| 173 | |
| 174 | if (result) |
| 175 | return result; |
| 176 | |
| 177 | if (config) { |
| 178 | cdns3_set_hw_configuration(priv_dev); |
| 179 | } else { |
| 180 | cdns3_hw_reset_eps_config(priv_dev); |
| 181 | usb_gadget_set_state(&priv_dev->gadget, |
| 182 | USB_STATE_ADDRESS); |
| 183 | } |
| 184 | break; |
| 185 | case USB_STATE_CONFIGURED: |
| 186 | result = cdns3_ep0_delegate_req(priv_dev, ctrl_req); |
| 187 | |
| 188 | if (!config && !result) { |
| 189 | cdns3_hw_reset_eps_config(priv_dev); |
| 190 | usb_gadget_set_state(&priv_dev->gadget, |
| 191 | USB_STATE_ADDRESS); |
| 192 | } |
| 193 | break; |
| 194 | default: |
| 195 | result = -EINVAL; |
| 196 | } |
| 197 | |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * cdns3_req_ep0_set_address - Handling of SET_ADDRESS standard USB request |
| 203 | * @priv_dev: extended gadget object |
| 204 | * @ctrl_req: pointer to received setup packet |
| 205 | * |
| 206 | * Returns 0 if success, error code on error |
| 207 | */ |
| 208 | static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev, |
| 209 | struct usb_ctrlrequest *ctrl_req) |
| 210 | { |
| 211 | enum usb_device_state device_state = priv_dev->gadget.state; |
| 212 | u32 reg; |
| 213 | u32 addr; |
| 214 | |
| 215 | addr = le16_to_cpu(ctrl_req->wValue); |
| 216 | |
| 217 | if (addr > USB_DEVICE_MAX_ADDRESS) { |
| 218 | dev_err(priv_dev->dev, |
| 219 | "Device address (%d) cannot be greater than %d\n", |
| 220 | addr, USB_DEVICE_MAX_ADDRESS); |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | |
| 224 | if (device_state == USB_STATE_CONFIGURED) { |
| 225 | dev_err(priv_dev->dev, |
| 226 | "can't set_address from configured state\n"); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | reg = readl(&priv_dev->regs->usb_cmd); |
| 231 | |
| 232 | writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR, |
| 233 | &priv_dev->regs->usb_cmd); |
| 234 | |
| 235 | usb_gadget_set_state(&priv_dev->gadget, |
| 236 | (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT)); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * cdns3_req_ep0_get_status - Handling of GET_STATUS standard USB request |
| 243 | * @priv_dev: extended gadget object |
| 244 | * @ctrl_req: pointer to received setup packet |
| 245 | * |
| 246 | * Returns 0 if success, error code on error |
| 247 | */ |
| 248 | static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev, |
| 249 | struct usb_ctrlrequest *ctrl) |
| 250 | { |
| 251 | __le16 *response_pkt; |
| 252 | u16 usb_status = 0; |
| 253 | u32 recip; |
| 254 | |
| 255 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 256 | |
| 257 | switch (recip) { |
| 258 | case USB_RECIP_DEVICE: |
| 259 | /* self powered */ |
| 260 | if (priv_dev->is_selfpowered) |
| 261 | usb_status = BIT(USB_DEVICE_SELF_POWERED); |
| 262 | |
| 263 | if (priv_dev->wake_up_flag) |
| 264 | usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP); |
| 265 | |
| 266 | if (priv_dev->gadget.speed != USB_SPEED_SUPER) |
| 267 | break; |
| 268 | |
| 269 | if (priv_dev->u1_allowed) |
| 270 | usb_status |= BIT(USB_DEV_STAT_U1_ENABLED); |
| 271 | |
| 272 | if (priv_dev->u2_allowed) |
| 273 | usb_status |= BIT(USB_DEV_STAT_U2_ENABLED); |
| 274 | |
| 275 | break; |
| 276 | case USB_RECIP_INTERFACE: |
| 277 | return cdns3_ep0_delegate_req(priv_dev, ctrl); |
| 278 | case USB_RECIP_ENDPOINT: |
| 279 | /* check if endpoint is stalled */ |
| 280 | cdns3_select_ep(priv_dev, ctrl->wIndex); |
| 281 | if (EP_STS_STALL(readl(&priv_dev->regs->ep_sts))) |
| 282 | usb_status = BIT(USB_ENDPOINT_HALT); |
| 283 | break; |
| 284 | default: |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | response_pkt = (__le16 *)priv_dev->setup_buf; |
| 289 | *response_pkt = cpu_to_le16(usb_status); |
| 290 | |
| 291 | /* Flush setup response */ |
| 292 | flush_dcache_range((unsigned long)priv_dev->setup_buf, |
| 293 | (unsigned long)priv_dev->setup_buf + |
| 294 | ROUND(sizeof(struct usb_ctrlrequest), |
| 295 | CONFIG_SYS_CACHELINE_SIZE)); |
| 296 | |
| 297 | cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, |
| 298 | sizeof(*response_pkt), 1, 0); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev, |
| 303 | struct usb_ctrlrequest *ctrl, |
| 304 | int set) |
| 305 | { |
| 306 | enum usb_device_state state; |
| 307 | enum usb_device_speed speed; |
| 308 | int ret = 0; |
| 309 | u16 tmode; |
| 310 | |
| 311 | state = priv_dev->gadget.state; |
| 312 | speed = priv_dev->gadget.speed; |
| 313 | |
| 314 | switch (ctrl->wValue) { |
| 315 | case USB_DEVICE_REMOTE_WAKEUP: |
| 316 | priv_dev->wake_up_flag = !!set; |
| 317 | break; |
| 318 | case USB_DEVICE_U1_ENABLE: |
| 319 | if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | priv_dev->u1_allowed = !!set; |
| 323 | break; |
| 324 | case USB_DEVICE_U2_ENABLE: |
| 325 | if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | priv_dev->u2_allowed = !!set; |
| 329 | break; |
| 330 | case USB_DEVICE_LTM_ENABLE: |
| 331 | ret = -EINVAL; |
| 332 | break; |
| 333 | case USB_DEVICE_TEST_MODE: |
| 334 | if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH) |
| 335 | return -EINVAL; |
| 336 | |
| 337 | tmode = le16_to_cpu(ctrl->wIndex); |
| 338 | |
| 339 | if (!set || (tmode & 0xff) != 0) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | switch (tmode >> 8) { |
| 343 | case TEST_J: |
| 344 | case TEST_K: |
| 345 | case TEST_SE0_NAK: |
| 346 | case TEST_PACKET: |
| 347 | cdns3_ep0_complete_setup(priv_dev, 0, 1); |
| 348 | /** |
| 349 | * Little delay to give the controller some time |
| 350 | * for sending status stage. |
| 351 | * This time should be less then 3ms. |
| 352 | */ |
| 353 | usleep_range(1000, 2000); |
| 354 | cdns3_set_register_bit(&priv_dev->regs->usb_cmd, |
| 355 | USB_CMD_STMODE | |
| 356 | USB_STS_TMODE_SEL(tmode - 1)); |
| 357 | break; |
| 358 | default: |
| 359 | ret = -EINVAL; |
| 360 | } |
| 361 | break; |
| 362 | default: |
| 363 | ret = -EINVAL; |
| 364 | } |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev, |
| 370 | struct usb_ctrlrequest *ctrl, |
| 371 | int set) |
| 372 | { |
| 373 | u32 wValue; |
| 374 | int ret = 0; |
| 375 | |
| 376 | wValue = le16_to_cpu(ctrl->wValue); |
| 377 | |
| 378 | switch (wValue) { |
| 379 | case USB_INTRF_FUNC_SUSPEND: |
| 380 | break; |
| 381 | default: |
| 382 | ret = -EINVAL; |
| 383 | } |
| 384 | |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev, |
| 389 | struct usb_ctrlrequest *ctrl, |
| 390 | int set) |
| 391 | { |
| 392 | struct cdns3_endpoint *priv_ep; |
| 393 | int ret = 0; |
| 394 | u8 index; |
| 395 | |
| 396 | if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | if (!(ctrl->wIndex & ~USB_DIR_IN)) |
| 400 | return 0; |
| 401 | |
| 402 | index = cdns3_ep_addr_to_index(ctrl->wIndex); |
| 403 | priv_ep = priv_dev->eps[index]; |
| 404 | |
| 405 | cdns3_select_ep(priv_dev, ctrl->wIndex); |
| 406 | |
| 407 | if (set) |
| 408 | __cdns3_gadget_ep_set_halt(priv_ep); |
| 409 | else if (!(priv_ep->flags & EP_WEDGE)) |
| 410 | ret = __cdns3_gadget_ep_clear_halt(priv_ep); |
| 411 | |
| 412 | cdns3_select_ep(priv_dev, 0x00); |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * cdns3_req_ep0_handle_feature - |
| 419 | * Handling of GET/SET_FEATURE standard USB request |
| 420 | * |
| 421 | * @priv_dev: extended gadget object |
| 422 | * @ctrl_req: pointer to received setup packet |
| 423 | * @set: must be set to 1 for SET_FEATURE request |
| 424 | * |
| 425 | * Returns 0 if success, error code on error |
| 426 | */ |
| 427 | static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev, |
| 428 | struct usb_ctrlrequest *ctrl, |
| 429 | int set) |
| 430 | { |
| 431 | int ret = 0; |
| 432 | u32 recip; |
| 433 | |
| 434 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 435 | |
| 436 | switch (recip) { |
| 437 | case USB_RECIP_DEVICE: |
| 438 | ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set); |
| 439 | break; |
| 440 | case USB_RECIP_INTERFACE: |
| 441 | ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set); |
| 442 | break; |
| 443 | case USB_RECIP_ENDPOINT: |
| 444 | ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set); |
| 445 | break; |
| 446 | default: |
| 447 | return -EINVAL; |
| 448 | } |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * cdns3_req_ep0_set_sel - Handling of SET_SEL standard USB request |
| 455 | * @priv_dev: extended gadget object |
| 456 | * @ctrl_req: pointer to received setup packet |
| 457 | * |
| 458 | * Returns 0 if success, error code on error |
| 459 | */ |
| 460 | static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev, |
| 461 | struct usb_ctrlrequest *ctrl_req) |
| 462 | { |
| 463 | if (priv_dev->gadget.state < USB_STATE_ADDRESS) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | if (ctrl_req->wLength != 6) { |
| 467 | dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n", |
| 468 | ctrl_req->wLength); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
| 472 | cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * cdns3_req_ep0_set_isoch_delay - |
| 478 | * Handling of GET_ISOCH_DELAY standard USB request |
| 479 | * @priv_dev: extended gadget object |
| 480 | * @ctrl_req: pointer to received setup packet |
| 481 | * |
| 482 | * Returns 0 if success, error code on error |
| 483 | */ |
| 484 | static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev, |
| 485 | struct usb_ctrlrequest *ctrl_req) |
| 486 | { |
| 487 | if (ctrl_req->wIndex || ctrl_req->wLength) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | priv_dev->isoch_delay = ctrl_req->wValue; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * cdns3_ep0_standard_request - Handling standard USB requests |
| 497 | * @priv_dev: extended gadget object |
| 498 | * @ctrl_req: pointer to received setup packet |
| 499 | * |
| 500 | * Returns 0 if success, error code on error |
| 501 | */ |
| 502 | static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev, |
| 503 | struct usb_ctrlrequest *ctrl_req) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | switch (ctrl_req->bRequest) { |
| 508 | case USB_REQ_SET_ADDRESS: |
| 509 | ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req); |
| 510 | break; |
| 511 | case USB_REQ_SET_CONFIGURATION: |
| 512 | ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req); |
| 513 | break; |
| 514 | case USB_REQ_GET_STATUS: |
| 515 | ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req); |
| 516 | break; |
| 517 | case USB_REQ_CLEAR_FEATURE: |
| 518 | ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0); |
| 519 | break; |
| 520 | case USB_REQ_SET_FEATURE: |
| 521 | ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1); |
| 522 | break; |
| 523 | case USB_REQ_SET_SEL: |
| 524 | ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req); |
| 525 | break; |
| 526 | case USB_REQ_SET_ISOCH_DELAY: |
| 527 | ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req); |
| 528 | break; |
| 529 | default: |
| 530 | ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req); |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | static void __pending_setup_status_handler(struct cdns3_device *priv_dev) |
| 538 | { |
| 539 | struct usb_request *request = priv_dev->pending_status_request; |
| 540 | |
| 541 | if (priv_dev->status_completion_no_call && request && |
| 542 | request->complete) { |
| 543 | request->complete(&priv_dev->eps[0]->endpoint, request); |
| 544 | priv_dev->status_completion_no_call = 0; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void cdns3_pending_setup_status_handler(struct work_struct *work) |
| 549 | { |
| 550 | struct cdns3_device *priv_dev = container_of(work, struct cdns3_device, |
| 551 | pending_status_wq); |
| 552 | unsigned long flags; |
| 553 | |
| 554 | spin_lock_irqsave(&priv_dev->lock, flags); |
| 555 | __pending_setup_status_handler(priv_dev); |
| 556 | spin_unlock_irqrestore(&priv_dev->lock, flags); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * cdns3_ep0_setup_phase - Handling setup USB requests |
| 561 | * @priv_dev: extended gadget object |
| 562 | */ |
| 563 | static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev) |
| 564 | { |
| 565 | struct usb_ctrlrequest *ctrl = priv_dev->setup_buf; |
| 566 | struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; |
| 567 | int result; |
| 568 | |
Vignesh Raghavendra | 6e0c11a | 2020-01-27 17:55:54 +0530 | [diff] [blame] | 569 | /* Invalidate Setup Packet received */ |
| 570 | invalidate_dcache_range(priv_dev->setup_dma, |
| 571 | priv_dev->setup_dma + ARCH_DMA_MINALIGN); |
| 572 | |
Vignesh Raghavendra | b1a4928 | 2019-10-01 17:26:33 +0530 | [diff] [blame] | 573 | priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN; |
| 574 | |
| 575 | trace_cdns3_ctrl_req(ctrl); |
| 576 | |
| 577 | if (!list_empty(&priv_ep->pending_req_list)) { |
| 578 | struct usb_request *request; |
| 579 | |
| 580 | request = cdns3_next_request(&priv_ep->pending_req_list); |
| 581 | priv_ep->dir = priv_dev->ep0_data_dir; |
| 582 | cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), |
| 583 | -ECONNRESET); |
| 584 | } |
| 585 | |
| 586 | if (le16_to_cpu(ctrl->wLength)) |
| 587 | priv_dev->ep0_stage = CDNS3_DATA_STAGE; |
| 588 | else |
| 589 | priv_dev->ep0_stage = CDNS3_STATUS_STAGE; |
| 590 | |
| 591 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
| 592 | result = cdns3_ep0_standard_request(priv_dev, ctrl); |
| 593 | else |
| 594 | result = cdns3_ep0_delegate_req(priv_dev, ctrl); |
| 595 | |
| 596 | if (result == USB_GADGET_DELAYED_STATUS) |
| 597 | return; |
| 598 | |
| 599 | if (result < 0) |
| 600 | cdns3_ep0_complete_setup(priv_dev, 1, 1); |
| 601 | else if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) |
| 602 | cdns3_ep0_complete_setup(priv_dev, 0, 1); |
| 603 | } |
| 604 | |
| 605 | static void cdns3_transfer_completed(struct cdns3_device *priv_dev) |
| 606 | { |
| 607 | struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; |
| 608 | |
| 609 | if (!list_empty(&priv_ep->pending_req_list)) { |
| 610 | struct usb_request *request; |
| 611 | |
| 612 | trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool); |
| 613 | request = cdns3_next_request(&priv_ep->pending_req_list); |
| 614 | |
| 615 | /* Invalidate TRB before accessing it */ |
| 616 | invalidate_dcache_range((unsigned long)priv_ep->trb_pool, |
| 617 | (unsigned long)priv_ep->trb_pool + |
| 618 | ROUND(sizeof(struct cdns3_trb), |
| 619 | CONFIG_SYS_CACHELINE_SIZE)); |
| 620 | |
| 621 | request->actual = |
| 622 | TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length)); |
| 623 | |
| 624 | priv_ep->dir = priv_dev->ep0_data_dir; |
| 625 | cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0); |
| 626 | } |
| 627 | |
| 628 | cdns3_ep0_complete_setup(priv_dev, 0, 0); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * cdns3_check_new_setup - Check if controller receive new SETUP packet. |
| 633 | * @priv_dev: extended gadget object |
| 634 | * |
| 635 | * The SETUP packet can be kept in on-chip memory or in system memory. |
| 636 | */ |
| 637 | static bool cdns3_check_new_setup(struct cdns3_device *priv_dev) |
| 638 | { |
| 639 | u32 ep_sts_reg; |
| 640 | |
| 641 | cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT); |
| 642 | ep_sts_reg = readl(&priv_dev->regs->ep_sts); |
| 643 | |
| 644 | return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT)); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * cdns3_check_ep0_interrupt_proceed - Processes interrupt related to endpoint 0 |
| 649 | * @priv_dev: extended gadget object |
| 650 | * @dir: USB_DIR_IN for IN direction, USB_DIR_OUT for OUT direction |
| 651 | */ |
| 652 | void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir) |
| 653 | { |
| 654 | u32 ep_sts_reg; |
| 655 | |
| 656 | cdns3_select_ep(priv_dev, dir); |
| 657 | |
| 658 | ep_sts_reg = readl(&priv_dev->regs->ep_sts); |
| 659 | writel(ep_sts_reg, &priv_dev->regs->ep_sts); |
| 660 | |
| 661 | trace_cdns3_ep0_irq(priv_dev, ep_sts_reg); |
| 662 | |
| 663 | __pending_setup_status_handler(priv_dev); |
| 664 | |
| 665 | if (ep_sts_reg & EP_STS_SETUP) |
| 666 | priv_dev->wait_for_setup = 1; |
| 667 | |
| 668 | if (priv_dev->wait_for_setup && ep_sts_reg & EP_STS_IOC) { |
| 669 | priv_dev->wait_for_setup = 0; |
| 670 | cdns3_allow_enable_l1(priv_dev, 0); |
| 671 | cdns3_ep0_setup_phase(priv_dev); |
| 672 | } else if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) { |
| 673 | priv_dev->ep0_data_dir = dir; |
| 674 | cdns3_transfer_completed(priv_dev); |
| 675 | } |
| 676 | |
| 677 | if (ep_sts_reg & EP_STS_DESCMIS) { |
| 678 | if (dir == 0 && !priv_dev->setup_pending) |
| 679 | cdns3_prepare_setup_packet(priv_dev); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * cdns3_gadget_ep0_enable |
| 685 | * Function shouldn't be called by gadget driver, |
| 686 | * endpoint 0 is allways active |
| 687 | */ |
| 688 | static int cdns3_gadget_ep0_enable(struct usb_ep *ep, |
| 689 | const struct usb_endpoint_descriptor *desc) |
| 690 | { |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * cdns3_gadget_ep0_disable |
| 696 | * Function shouldn't be called by gadget driver, |
| 697 | * endpoint 0 is allways active |
| 698 | */ |
| 699 | static int cdns3_gadget_ep0_disable(struct usb_ep *ep) |
| 700 | { |
| 701 | return -EINVAL; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * cdns3_gadget_ep0_set_halt |
| 706 | * @ep: pointer to endpoint zero object |
| 707 | * @value: 1 for set stall, 0 for clear stall |
| 708 | * |
| 709 | * Returns 0 |
| 710 | */ |
| 711 | static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
| 712 | { |
| 713 | /* TODO */ |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * cdns3_gadget_ep0_queue Transfer data on endpoint zero |
| 719 | * @ep: pointer to endpoint zero object |
| 720 | * @request: pointer to request object |
| 721 | * @gfp_flags: gfp flags |
| 722 | * |
| 723 | * Returns 0 on success, error code elsewhere |
| 724 | */ |
| 725 | static int cdns3_gadget_ep0_queue(struct usb_ep *ep, |
| 726 | struct usb_request *request, |
| 727 | gfp_t gfp_flags) |
| 728 | { |
| 729 | struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); |
| 730 | struct cdns3_device *priv_dev = priv_ep->cdns3_dev; |
| 731 | unsigned long flags; |
| 732 | int erdy_sent = 0; |
| 733 | int ret = 0; |
| 734 | u8 zlp = 0; |
| 735 | |
| 736 | trace_cdns3_ep0_queue(priv_dev, request); |
| 737 | |
| 738 | /* cancel the request if controller receive new SETUP packet. */ |
| 739 | if (cdns3_check_new_setup(priv_dev)) |
| 740 | return -ECONNRESET; |
| 741 | |
| 742 | /* send STATUS stage. Should be called only for SET_CONFIGURATION */ |
| 743 | if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) { |
| 744 | spin_lock_irqsave(&priv_dev->lock, flags); |
| 745 | cdns3_select_ep(priv_dev, 0x00); |
| 746 | |
| 747 | erdy_sent = !priv_dev->hw_configured_flag; |
| 748 | cdns3_set_hw_configuration(priv_dev); |
| 749 | |
| 750 | if (!erdy_sent) |
| 751 | cdns3_ep0_complete_setup(priv_dev, 0, 1); |
| 752 | |
| 753 | cdns3_allow_enable_l1(priv_dev, 1); |
| 754 | |
| 755 | request->actual = 0; |
| 756 | priv_dev->status_completion_no_call = true; |
| 757 | priv_dev->pending_status_request = request; |
| 758 | spin_unlock_irqrestore(&priv_dev->lock, flags); |
| 759 | |
| 760 | /* |
| 761 | * Since there is no completion interrupt for status stage, |
| 762 | * it needs to call ->completion in software after |
| 763 | * ep0_queue is back. |
| 764 | */ |
| 765 | #ifndef __UBOOT__ |
| 766 | queue_work(system_freezable_wq, &priv_dev->pending_status_wq); |
| 767 | #else |
| 768 | __pending_setup_status_handler(priv_dev); |
| 769 | #endif |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | spin_lock_irqsave(&priv_dev->lock, flags); |
| 774 | if (!list_empty(&priv_ep->pending_req_list)) { |
| 775 | dev_err(priv_dev->dev, |
| 776 | "can't handle multiple requests for ep0\n"); |
| 777 | spin_unlock_irqrestore(&priv_dev->lock, flags); |
| 778 | return -EBUSY; |
| 779 | } |
| 780 | |
| 781 | ret = usb_gadget_map_request(&priv_dev->gadget, request, |
| 782 | priv_dev->ep0_data_dir); |
| 783 | if (ret) { |
| 784 | spin_unlock_irqrestore(&priv_dev->lock, flags); |
| 785 | dev_err(priv_dev->dev, "failed to map request\n"); |
| 786 | return -EINVAL; |
| 787 | } |
| 788 | |
| 789 | request->status = -EINPROGRESS; |
| 790 | list_add_tail(&request->list, &priv_ep->pending_req_list); |
| 791 | |
| 792 | if (request->zero && request->length && |
| 793 | (request->length % ep->maxpacket == 0)) |
| 794 | zlp = 1; |
| 795 | |
| 796 | cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp); |
| 797 | |
| 798 | spin_unlock_irqrestore(&priv_dev->lock, flags); |
| 799 | |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint |
| 805 | * @ep: endpoint object |
| 806 | * |
| 807 | * Returns 0 |
| 808 | */ |
| 809 | int cdns3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 810 | { |
| 811 | struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); |
| 812 | |
| 813 | dev_dbg(priv_dev->dev, "Wedge for %s\n", ep->name); |
| 814 | cdns3_gadget_ep_set_halt(ep, 1); |
| 815 | priv_ep->flags |= EP_WEDGE; |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | const struct usb_ep_ops cdns3_gadget_ep0_ops = { |
| 821 | .enable = cdns3_gadget_ep0_enable, |
| 822 | .disable = cdns3_gadget_ep0_disable, |
| 823 | .alloc_request = cdns3_gadget_ep_alloc_request, |
| 824 | .free_request = cdns3_gadget_ep_free_request, |
| 825 | .queue = cdns3_gadget_ep0_queue, |
| 826 | .dequeue = cdns3_gadget_ep_dequeue, |
| 827 | .set_halt = cdns3_gadget_ep0_set_halt, |
| 828 | .set_wedge = cdns3_gadget_ep_set_wedge, |
| 829 | }; |
| 830 | |
| 831 | /** |
| 832 | * cdns3_ep0_config - Configures default endpoint |
| 833 | * @priv_dev: extended gadget object |
| 834 | * |
| 835 | * Functions sets parameters: maximal packet size and enables interrupts |
| 836 | */ |
| 837 | void cdns3_ep0_config(struct cdns3_device *priv_dev) |
| 838 | { |
| 839 | struct cdns3_usb_regs __iomem *regs; |
| 840 | struct cdns3_endpoint *priv_ep; |
| 841 | u32 max_packet_size = 64; |
| 842 | |
| 843 | regs = priv_dev->regs; |
| 844 | |
| 845 | if (priv_dev->gadget.speed == USB_SPEED_SUPER) |
| 846 | max_packet_size = 512; |
| 847 | |
| 848 | priv_ep = priv_dev->eps[0]; |
| 849 | |
| 850 | if (!list_empty(&priv_ep->pending_req_list)) { |
| 851 | struct usb_request *request; |
| 852 | |
| 853 | request = cdns3_next_request(&priv_ep->pending_req_list); |
| 854 | list_del_init(&request->list); |
| 855 | } |
| 856 | |
| 857 | priv_dev->u1_allowed = 0; |
| 858 | priv_dev->u2_allowed = 0; |
| 859 | |
| 860 | priv_dev->gadget.ep0->maxpacket = max_packet_size; |
| 861 | cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size); |
| 862 | |
| 863 | /* init ep out */ |
| 864 | cdns3_select_ep(priv_dev, USB_DIR_OUT); |
| 865 | |
| 866 | if (priv_dev->dev_ver >= DEV_VER_V3) { |
| 867 | cdns3_set_register_bit(&priv_dev->regs->dtrans, |
| 868 | BIT(0) | BIT(16)); |
| 869 | cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb, |
| 870 | BIT(0) | BIT(16)); |
| 871 | } |
| 872 | |
| 873 | writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size), |
| 874 | ®s->ep_cfg); |
| 875 | |
| 876 | writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN, |
| 877 | ®s->ep_sts_en); |
| 878 | |
| 879 | /* init ep in */ |
| 880 | cdns3_select_ep(priv_dev, USB_DIR_IN); |
| 881 | |
| 882 | writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size), |
| 883 | ®s->ep_cfg); |
| 884 | |
| 885 | writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, ®s->ep_sts_en); |
| 886 | |
| 887 | cdns3_set_register_bit(®s->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS); |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * cdns3_init_ep0 Initializes software endpoint 0 of gadget |
| 892 | * @priv_dev: extended gadget object |
| 893 | * @ep_priv: extended endpoint object |
| 894 | * |
| 895 | * Returns 0 on success else error code. |
| 896 | */ |
| 897 | int cdns3_init_ep0(struct cdns3_device *priv_dev, |
| 898 | struct cdns3_endpoint *priv_ep) |
| 899 | { |
| 900 | sprintf(priv_ep->name, "ep0"); |
| 901 | |
| 902 | /* fill linux fields */ |
| 903 | priv_ep->endpoint.ops = &cdns3_gadget_ep0_ops; |
| 904 | priv_ep->endpoint.maxburst = 1; |
| 905 | usb_ep_set_maxpacket_limit(&priv_ep->endpoint, |
| 906 | CDNS3_EP0_MAX_PACKET_LIMIT); |
| 907 | #ifndef __UBOOT__ |
| 908 | priv_ep->endpoint.address = 0; |
| 909 | #endif |
| 910 | priv_ep->endpoint.caps.type_control = 1; |
| 911 | priv_ep->endpoint.caps.dir_in = 1; |
| 912 | priv_ep->endpoint.caps.dir_out = 1; |
| 913 | priv_ep->endpoint.name = priv_ep->name; |
| 914 | priv_ep->endpoint.desc = &cdns3_gadget_ep0_desc; |
| 915 | priv_dev->gadget.ep0 = &priv_ep->endpoint; |
| 916 | priv_ep->type = USB_ENDPOINT_XFER_CONTROL; |
| 917 | |
| 918 | return cdns3_allocate_trb_pool(priv_ep); |
| 919 | } |