1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Cadence USBSS DRD Driver. 4 * 5 * Copyright (C) 2018-2020 Cadence. 6 * Copyright (C) 2019 Texas Instruments 7 * 8 * Author: Pawel Laszczak <pawell@cadence.com> 9 * Roger Quadros <rogerq@ti.com> 10 * 11 */ 12#include <linux/kernel.h> 13#include <linux/interrupt.h> 14#include <linux/delay.h> 15#include <linux/iopoll.h> 16#include <linux/usb/otg.h> 17#include <linux/phy/phy.h> 18 19#include "gadget.h" 20#include "drd.h" 21#include "core.h" 22 23/** 24 * cdns3_set_mode - change mode of OTG Core 25 * @cdns: pointer to context structure 26 * @mode: selected mode from cdns_role 27 * 28 * Returns 0 on success otherwise negative errno 29 */ 30static int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode) 31{ 32 u32 __iomem *override_reg; 33 u32 reg; 34 35 switch (mode) { 36 case USB_DR_MODE_PERIPHERAL: 37 break; 38 case USB_DR_MODE_HOST: 39 break; 40 case USB_DR_MODE_OTG: 41 dev_dbg(cdns->dev, "Set controller to OTG mode\n"); 42 43 if (cdns->version == CDNSP_CONTROLLER_V2) 44 override_reg = &cdns->otg_cdnsp_regs->override; 45 else if (cdns->version == CDNS3_CONTROLLER_V1) 46 override_reg = &cdns->otg_v1_regs->override; 47 else 48 override_reg = &cdns->otg_v0_regs->ctrl1; 49 50 reg = readl(override_reg); 51 52 if (cdns->version != CDNS3_CONTROLLER_V0) 53 reg |= OVERRIDE_IDPULLUP; 54 else 55 reg |= OVERRIDE_IDPULLUP_V0; 56 57 writel(reg, override_reg); 58 59 if (cdns->version == CDNS3_CONTROLLER_V1) { 60 /* 61 * Enable work around feature built into the 62 * controller to address issue with RX Sensitivity 63 * est (EL_17) for USB2 PHY. The issue only occures 64 * for 0x0002450D controller version. 65 */ 66 if (cdns->phyrst_a_enable) { 67 reg = readl(&cdns->otg_v1_regs->phyrst_cfg); 68 reg |= PHYRST_CFG_PHYRST_A_ENABLE; 69 writel(reg, &cdns->otg_v1_regs->phyrst_cfg); 70 } 71 } 72 73 /* 74 * Hardware specification says: "ID_VALUE must be valid within 75 * 50ms after idpullup is set to '1" so driver must wait 76 * 50ms before reading this pin. 77 */ 78 usleep_range(50000, 60000); 79 break; 80 default: 81 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode); 82 return -EINVAL; 83 } 84 85 return 0; 86} 87 88int cdns3_get_id(struct cdns3 *cdns) 89{ 90 int id; 91 92 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE; 93 dev_dbg(cdns->dev, "OTG ID: %d", id); 94 95 return id; 96} 97 98int cdns3_get_vbus(struct cdns3 *cdns) 99{ 100 int vbus; 101 102 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID); 103 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus); 104 105 return vbus; 106} 107 108bool cdns3_is_host(struct cdns3 *cdns) 109{ 110 if (cdns->dr_mode == USB_DR_MODE_HOST) 111 return true; 112 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST) 113 return true; 114 115 return false; 116} 117 118bool cdns3_is_device(struct cdns3 *cdns) 119{ 120 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) 121 return true; 122 else if (cdns->dr_mode == USB_DR_MODE_OTG) 123 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL) 124 return true; 125 126 return false; 127} 128 129/** 130 * cdns3_otg_disable_irq - Disable all OTG interrupts 131 * @cdns: Pointer to controller context structure 132 */ 133static void cdns3_otg_disable_irq(struct cdns3 *cdns) 134{ 135 writel(0, &cdns->otg_irq_regs->ien); 136} 137 138/** 139 * cdns3_otg_enable_irq - enable id and sess_valid interrupts 140 * @cdns: Pointer to controller context structure 141 */ 142static void cdns3_otg_enable_irq(struct cdns3 *cdns) 143{ 144 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT | 145 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien); 146} 147 148/** 149 * cdns3_drd_host_on - start host. 150 * @cdns: Pointer to controller context structure. 151 * 152 * Returns 0 on success otherwise negative errno. 153 */ 154int cdns3_drd_host_on(struct cdns3 *cdns) 155{ 156 u32 val, ready_bit; 157 int ret; 158 159 /* Enable host mode. */ 160 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS, 161 &cdns->otg_regs->cmd); 162 163 if (cdns->version == CDNSP_CONTROLLER_V2) 164 ready_bit = OTGSTS_CDNSP_XHCI_READY; 165 else 166 ready_bit = OTGSTS_CDNS3_XHCI_READY; 167 168 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n"); 169 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val, 170 val & ready_bit, 1, 100000); 171 172 if (ret) 173 dev_err(cdns->dev, "timeout waiting for xhci_ready\n"); 174 175 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST); 176 return ret; 177} 178 179/** 180 * cdns3_drd_host_off - stop host. 181 * @cdns: Pointer to controller context structure. 182 */ 183void cdns3_drd_host_off(struct cdns3 *cdns) 184{ 185 u32 val; 186 187 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP | 188 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF, 189 &cdns->otg_regs->cmd); 190 191 /* Waiting till H_IDLE state.*/ 192 readl_poll_timeout_atomic(&cdns->otg_regs->state, val, 193 !(val & OTGSTATE_HOST_STATE_MASK), 194 1, 2000000); 195 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID); 196} 197 198/** 199 * cdns3_drd_gadget_on - start gadget. 200 * @cdns: Pointer to controller context structure. 201 * 202 * Returns 0 on success otherwise negative errno 203 */ 204int cdns3_drd_gadget_on(struct cdns3 *cdns) 205{ 206 u32 reg = OTGCMD_OTG_DIS; 207 u32 ready_bit; 208 int ret, val; 209 210 /* switch OTG core */ 211 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd); 212 213 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n"); 214 215 if (cdns->version == CDNSP_CONTROLLER_V2) 216 ready_bit = OTGSTS_CDNSP_DEV_READY; 217 else 218 ready_bit = OTGSTS_CDNS3_DEV_READY; 219 220 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val, 221 val & ready_bit, 1, 100000); 222 if (ret) { 223 dev_err(cdns->dev, "timeout waiting for dev_ready\n"); 224 return ret; 225 } 226 227 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE); 228 return 0; 229} 230 231/** 232 * cdns3_drd_gadget_off - stop gadget. 233 * @cdns: Pointer to controller context structure. 234 */ 235void cdns3_drd_gadget_off(struct cdns3 *cdns) 236{ 237 u32 val; 238 239 /* 240 * Driver should wait at least 10us after disabling Device 241 * before turning-off Device (DEV_BUS_DROP). 242 */ 243 usleep_range(20, 30); 244 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP | 245 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF, 246 &cdns->otg_regs->cmd); 247 /* Waiting till DEV_IDLE state.*/ 248 readl_poll_timeout_atomic(&cdns->otg_regs->state, val, 249 !(val & OTGSTATE_DEV_STATE_MASK), 250 1, 2000000); 251 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID); 252} 253 254/** 255 * cdns3_init_otg_mode - initialize drd controller 256 * @cdns: Pointer to controller context structure 257 * 258 * Returns 0 on success otherwise negative errno 259 */ 260static int cdns3_init_otg_mode(struct cdns3 *cdns) 261{ 262 int ret; 263 264 cdns3_otg_disable_irq(cdns); 265 /* clear all interrupts */ 266 writel(~0, &cdns->otg_irq_regs->ivect); 267 268 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG); 269 if (ret) 270 return ret; 271 272 cdns3_otg_enable_irq(cdns); 273 274 return 0; 275} 276 277/** 278 * cdns3_drd_update_mode - initialize mode of operation 279 * @cdns: Pointer to controller context structure 280 * 281 * Returns 0 on success otherwise negative errno 282 */ 283int cdns3_drd_update_mode(struct cdns3 *cdns) 284{ 285 int ret; 286 287 switch (cdns->dr_mode) { 288 case USB_DR_MODE_PERIPHERAL: 289 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL); 290 break; 291 case USB_DR_MODE_HOST: 292 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST); 293 break; 294 case USB_DR_MODE_OTG: 295 ret = cdns3_init_otg_mode(cdns); 296 break; 297 default: 298 dev_err(cdns->dev, "Unsupported mode of operation %d\n", 299 cdns->dr_mode); 300 return -EINVAL; 301 } 302 303 return ret; 304} 305 306static irqreturn_t cdns3_drd_thread_irq(int irq, void *data) 307{ 308 struct cdns3 *cdns = data; 309 310 cdns3_hw_role_switch(cdns); 311 312 return IRQ_HANDLED; 313} 314 315/** 316 * cdns3_drd_irq - interrupt handler for OTG events 317 * 318 * @irq: irq number for cdns3 core device 319 * @data: structure of cdns3 320 * 321 * Returns IRQ_HANDLED or IRQ_NONE 322 */ 323static irqreturn_t cdns3_drd_irq(int irq, void *data) 324{ 325 irqreturn_t ret = IRQ_NONE; 326 struct cdns3 *cdns = data; 327 u32 reg; 328 329 if (cdns->dr_mode != USB_DR_MODE_OTG) 330 return IRQ_NONE; 331 332 if (cdns->in_lpm) 333 return ret; 334 335 reg = readl(&cdns->otg_irq_regs->ivect); 336 337 if (!reg) 338 return IRQ_NONE; 339 340 if (reg & OTGIEN_ID_CHANGE_INT) { 341 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n", 342 cdns3_get_id(cdns)); 343 344 ret = IRQ_WAKE_THREAD; 345 } 346 347 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) { 348 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n", 349 cdns3_get_vbus(cdns)); 350 351 ret = IRQ_WAKE_THREAD; 352 } 353 354 writel(~0, &cdns->otg_irq_regs->ivect); 355 return ret; 356} 357 358int cdns3_drd_init(struct cdns3 *cdns) 359{ 360 void __iomem *regs; 361 u32 state; 362 int ret; 363 364 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res); 365 if (IS_ERR(regs)) 366 return PTR_ERR(regs); 367 368 /* Detection of DRD version. Controller has been released 369 * in three versions. All are very similar and are software compatible, 370 * but they have same changes in register maps. 371 * The first register in oldest version is command register and it's 372 * read only. Driver should read 0 from it. On the other hand, in v1 373 * and v2 the first register contains device ID number which is not 374 * set to 0. Driver uses this fact to detect the proper version of 375 * controller. 376 */ 377 cdns->otg_v0_regs = regs; 378 if (!readl(&cdns->otg_v0_regs->cmd)) { 379 cdns->version = CDNS3_CONTROLLER_V0; 380 cdns->otg_v1_regs = NULL; 381 cdns->otg_cdnsp_regs = NULL; 382 cdns->otg_regs = regs; 383 cdns->otg_irq_regs = (struct cdns3_otg_irq_regs *) 384 &cdns->otg_v0_regs->ien; 385 writel(1, &cdns->otg_v0_regs->simulate); 386 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n", 387 readl(&cdns->otg_v0_regs->version)); 388 } else { 389 cdns->otg_v0_regs = NULL; 390 cdns->otg_v1_regs = regs; 391 cdns->otg_cdnsp_regs = regs; 392 393 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd; 394 395 if (cdns->otg_cdnsp_regs->did == OTG_CDNSP_DID) { 396 cdns->otg_irq_regs = (struct cdns3_otg_irq_regs *) 397 &cdns->otg_cdnsp_regs->ien; 398 cdns->version = CDNSP_CONTROLLER_V2; 399 } else { 400 cdns->otg_irq_regs = (struct cdns3_otg_irq_regs *) 401 &cdns->otg_v1_regs->ien; 402 writel(1, &cdns->otg_v1_regs->simulate); 403 cdns->version = CDNS3_CONTROLLER_V1; 404 } 405 406 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n", 407 readl(&cdns->otg_v1_regs->did), 408 readl(&cdns->otg_v1_regs->rid)); 409 } 410 411 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts)); 412 413 /* Update dr_mode according to STRAP configuration. */ 414 cdns->dr_mode = USB_DR_MODE_OTG; 415 416 if ((cdns->version == CDNSP_CONTROLLER_V2 && 417 state == OTGSTS_CDNSP_STRAP_HOST) || 418 (cdns->version != CDNSP_CONTROLLER_V2 && 419 state == OTGSTS_STRAP_HOST)) { 420 dev_dbg(cdns->dev, "Controller strapped to HOST\n"); 421 cdns->dr_mode = USB_DR_MODE_HOST; 422 } else if ((cdns->version == CDNSP_CONTROLLER_V2 && 423 state == OTGSTS_CDNSP_STRAP_GADGET) || 424 (cdns->version != CDNSP_CONTROLLER_V2 && 425 state == OTGSTS_STRAP_GADGET)) { 426 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n"); 427 cdns->dr_mode = USB_DR_MODE_PERIPHERAL; 428 } 429 430 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq, 431 cdns3_drd_irq, 432 cdns3_drd_thread_irq, 433 IRQF_SHARED, 434 dev_name(cdns->dev), cdns); 435 if (ret) { 436 dev_err(cdns->dev, "couldn't get otg_irq\n"); 437 return ret; 438 } 439 440 state = readl(&cdns->otg_regs->sts); 441 if (OTGSTS_OTG_NRDY(state)) { 442 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n"); 443 return -ENODEV; 444 } 445 446 return 0; 447} 448 449int cdns3_drd_exit(struct cdns3 *cdns) 450{ 451 cdns3_otg_disable_irq(cdns); 452 return 0; 453} 454