Lines Matching +full:otg +full:- +full:switch
1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018-2020 Cadence.
16 #include <linux/usb/otg.h>
22 * cdns_set_mode - change mode of OTG Core
33 switch (mode) { in cdns_set_mode()
39 dev_dbg(cdns->dev, "Set controller to OTG mode\n"); in cdns_set_mode()
41 if (cdns->version == CDNSP_CONTROLLER_V2) in cdns_set_mode()
42 override_reg = &cdns->otg_cdnsp_regs->override; in cdns_set_mode()
43 else if (cdns->version == CDNS3_CONTROLLER_V1) in cdns_set_mode()
44 override_reg = &cdns->otg_v1_regs->override; in cdns_set_mode()
46 override_reg = &cdns->otg_v0_regs->ctrl1; in cdns_set_mode()
50 if (cdns->version != CDNS3_CONTROLLER_V0) in cdns_set_mode()
57 if (cdns->version == CDNS3_CONTROLLER_V1) { in cdns_set_mode()
64 if (cdns->phyrst_a_enable) { in cdns_set_mode()
65 reg = readl(&cdns->otg_v1_regs->phyrst_cfg); in cdns_set_mode()
67 writel(reg, &cdns->otg_v1_regs->phyrst_cfg); in cdns_set_mode()
79 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode); in cdns_set_mode()
80 return -EINVAL; in cdns_set_mode()
90 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE; in cdns_get_id()
91 dev_dbg(cdns->dev, "OTG ID: %d", id); in cdns_get_id()
100 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID); in cdns_get_vbus()
101 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus); in cdns_get_vbus()
110 if (cdns->version != CDNSP_CONTROLLER_V2) in cdns_clear_vbus()
113 reg = readl(&cdns->otg_cdnsp_regs->override); in cdns_clear_vbus()
115 writel(reg, &cdns->otg_cdnsp_regs->override); in cdns_clear_vbus()
123 if (cdns->version != CDNSP_CONTROLLER_V2) in cdns_set_vbus()
126 reg = readl(&cdns->otg_cdnsp_regs->override); in cdns_set_vbus()
128 writel(reg, &cdns->otg_cdnsp_regs->override); in cdns_set_vbus()
134 if (cdns->dr_mode == USB_DR_MODE_HOST) in cdns_is_host()
144 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) in cdns_is_device()
146 else if (cdns->dr_mode == USB_DR_MODE_OTG) in cdns_is_device()
154 * cdns_otg_disable_irq - Disable all OTG interrupts
159 if (cdns->version) in cdns_otg_disable_irq()
160 writel(0, &cdns->otg_irq_regs->ien); in cdns_otg_disable_irq()
164 * cdns_otg_enable_irq - enable id and sess_valid interrupts
170 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien); in cdns_otg_enable_irq()
174 * cdns_drd_host_on - start host.
186 &cdns->otg_regs->cmd); in cdns_drd_host_on()
188 if (cdns->version == CDNSP_CONTROLLER_V2) in cdns_drd_host_on()
193 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n"); in cdns_drd_host_on()
194 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val, in cdns_drd_host_on()
198 dev_err(cdns->dev, "timeout waiting for xhci_ready\n"); in cdns_drd_host_on()
200 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_HOST); in cdns_drd_host_on()
201 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST); in cdns_drd_host_on()
206 * cdns_drd_host_off - stop host.
215 &cdns->otg_regs->cmd); in cdns_drd_host_off()
218 readl_poll_timeout_atomic(&cdns->otg_regs->state, val, in cdns_drd_host_off()
221 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID); in cdns_drd_host_off()
222 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID); in cdns_drd_host_off()
226 * cdns_drd_gadget_on - start gadget.
237 /* switch OTG core */ in cdns_drd_gadget_on()
238 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd); in cdns_drd_gadget_on()
240 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n"); in cdns_drd_gadget_on()
242 if (cdns->version == CDNSP_CONTROLLER_V2) in cdns_drd_gadget_on()
247 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val, in cdns_drd_gadget_on()
250 dev_err(cdns->dev, "timeout waiting for dev_ready\n"); in cdns_drd_gadget_on()
254 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_DEVICE); in cdns_drd_gadget_on()
255 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE); in cdns_drd_gadget_on()
261 * cdns_drd_gadget_off - stop gadget.
270 * before turning-off Device (DEV_BUS_DROP). in cdns_drd_gadget_off()
275 &cdns->otg_regs->cmd); in cdns_drd_gadget_off()
277 readl_poll_timeout_atomic(&cdns->otg_regs->state, val, in cdns_drd_gadget_off()
280 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID); in cdns_drd_gadget_off()
281 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID); in cdns_drd_gadget_off()
286 * cdns_init_otg_mode - initialize drd controller
297 writel(~0, &cdns->otg_irq_regs->ivect); in cdns_init_otg_mode()
309 * cdns_drd_update_mode - initialize mode of operation
318 switch (cdns->dr_mode) { in cdns_drd_update_mode()
329 dev_err(cdns->dev, "Unsupported mode of operation %d\n", in cdns_drd_update_mode()
330 cdns->dr_mode); in cdns_drd_update_mode()
331 return -EINVAL; in cdns_drd_update_mode()
347 * cdns_drd_irq - interrupt handler for OTG events
360 if (cdns->dr_mode != USB_DR_MODE_OTG) in cdns_drd_irq()
363 if (cdns->in_lpm) in cdns_drd_irq()
366 reg = readl(&cdns->otg_irq_regs->ivect); in cdns_drd_irq()
372 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n", in cdns_drd_irq()
379 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n", in cdns_drd_irq()
385 writel(~0, &cdns->otg_irq_regs->ivect); in cdns_drd_irq()
395 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res); in cdns_drd_init()
408 cdns->otg_v0_regs = regs; in cdns_drd_init()
409 if (!readl(&cdns->otg_v0_regs->cmd)) { in cdns_drd_init()
410 cdns->version = CDNS3_CONTROLLER_V0; in cdns_drd_init()
411 cdns->otg_v1_regs = NULL; in cdns_drd_init()
412 cdns->otg_cdnsp_regs = NULL; in cdns_drd_init()
413 cdns->otg_regs = regs; in cdns_drd_init()
414 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *) in cdns_drd_init()
415 &cdns->otg_v0_regs->ien; in cdns_drd_init()
416 writel(1, &cdns->otg_v0_regs->simulate); in cdns_drd_init()
417 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n", in cdns_drd_init()
418 readl(&cdns->otg_v0_regs->version)); in cdns_drd_init()
420 cdns->otg_v0_regs = NULL; in cdns_drd_init()
421 cdns->otg_v1_regs = regs; in cdns_drd_init()
422 cdns->otg_cdnsp_regs = regs; in cdns_drd_init()
424 cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd; in cdns_drd_init()
426 state = readl(&cdns->otg_cdnsp_regs->did); in cdns_drd_init()
429 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *) in cdns_drd_init()
430 &cdns->otg_cdnsp_regs->ien; in cdns_drd_init()
431 cdns->version = CDNSP_CONTROLLER_V2; in cdns_drd_init()
433 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *) in cdns_drd_init()
434 &cdns->otg_v1_regs->ien; in cdns_drd_init()
435 writel(1, &cdns->otg_v1_regs->simulate); in cdns_drd_init()
437 if (cdns->pdata && in cdns_drd_init()
438 (cdns->pdata->quirks & CDNS3_DRD_SUSPEND_RESIDENCY_ENABLE)) { in cdns_drd_init()
439 reg = readl(&cdns->otg_v1_regs->susp_ctrl); in cdns_drd_init()
441 writel(reg, &cdns->otg_v1_regs->susp_ctrl); in cdns_drd_init()
444 cdns->version = CDNS3_CONTROLLER_V1; in cdns_drd_init()
446 dev_err(cdns->dev, "not supported DID=0x%08x\n", state); in cdns_drd_init()
447 return -EINVAL; in cdns_drd_init()
450 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n", in cdns_drd_init()
451 readl(&cdns->otg_v1_regs->did), in cdns_drd_init()
452 readl(&cdns->otg_v1_regs->rid)); in cdns_drd_init()
455 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts)); in cdns_drd_init()
458 cdns->dr_mode = USB_DR_MODE_OTG; in cdns_drd_init()
460 if ((cdns->version == CDNSP_CONTROLLER_V2 && in cdns_drd_init()
462 (cdns->version != CDNSP_CONTROLLER_V2 && in cdns_drd_init()
464 dev_dbg(cdns->dev, "Controller strapped to HOST\n"); in cdns_drd_init()
465 cdns->dr_mode = USB_DR_MODE_HOST; in cdns_drd_init()
466 } else if ((cdns->version == CDNSP_CONTROLLER_V2 && in cdns_drd_init()
468 (cdns->version != CDNSP_CONTROLLER_V2 && in cdns_drd_init()
470 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n"); in cdns_drd_init()
471 cdns->dr_mode = USB_DR_MODE_PERIPHERAL; in cdns_drd_init()
474 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq, in cdns_drd_init()
478 dev_name(cdns->dev), cdns); in cdns_drd_init()
480 dev_err(cdns->dev, "couldn't get otg_irq\n"); in cdns_drd_init()
484 state = readl(&cdns->otg_regs->sts); in cdns_drd_init()
486 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n"); in cdns_drd_init()
487 return -ENODEV; in cdns_drd_init()
503 if (cdns->version == CDNS3_CONTROLLER_V0) { in cdns_power_is_lost()
504 if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0))) in cdns_power_is_lost()
507 if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0))) in cdns_power_is_lost()