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