1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS and USBSSP DRD Driver.
5 * Copyright (C) 2018-2020 Cadence.
6 * Copyright (C) 2019 Texas Instruments
8 * Author: Pawel Laszczak <pawell@cadence.com>
9 * Roger Quadros <rogerq@ti.com>
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>
22 * cdns_set_mode - change mode of OTG Core
23 * @cdns: pointer to context structure
24 * @mode: selected mode from cdns_role
26 * Returns 0 on success otherwise negative errno
28 static int cdns_set_mode(struct cdns *cdns, enum usb_dr_mode mode)
30 void __iomem *override_reg;
34 case USB_DR_MODE_PERIPHERAL:
36 case USB_DR_MODE_HOST:
39 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
41 if (cdns->version == CDNSP_CONTROLLER_V2)
42 override_reg = &cdns->otg_cdnsp_regs->override;
43 else if (cdns->version == CDNS3_CONTROLLER_V1)
44 override_reg = &cdns->otg_v1_regs->override;
46 override_reg = &cdns->otg_v0_regs->ctrl1;
48 reg = readl(override_reg);
50 if (cdns->version != CDNS3_CONTROLLER_V0)
51 reg |= OVERRIDE_IDPULLUP;
53 reg |= OVERRIDE_IDPULLUP_V0;
55 writel(reg, override_reg);
57 if (cdns->version == CDNS3_CONTROLLER_V1) {
59 * Enable work around feature built into the
60 * controller to address issue with RX Sensitivity
61 * est (EL_17) for USB2 PHY. The issue only occures
62 * for 0x0002450D controller version.
64 if (cdns->phyrst_a_enable) {
65 reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
66 reg |= PHYRST_CFG_PHYRST_A_ENABLE;
67 writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
72 * Hardware specification says: "ID_VALUE must be valid within
73 * 50ms after idpullup is set to '1" so driver must wait
74 * 50ms before reading this pin.
76 usleep_range(50000, 60000);
79 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
86 int cdns_get_id(struct cdns *cdns)
90 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
91 dev_dbg(cdns->dev, "OTG ID: %d", id);
96 int cdns_get_vbus(struct cdns *cdns)
100 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
101 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
106 void cdns_clear_vbus(struct cdns *cdns)
110 if (cdns->version != CDNSP_CONTROLLER_V2)
113 reg = readl(&cdns->otg_cdnsp_regs->override);
114 reg |= OVERRIDE_SESS_VLD_SEL;
115 writel(reg, &cdns->otg_cdnsp_regs->override);
117 EXPORT_SYMBOL_GPL(cdns_clear_vbus);
119 void cdns_set_vbus(struct cdns *cdns)
123 if (cdns->version != CDNSP_CONTROLLER_V2)
126 reg = readl(&cdns->otg_cdnsp_regs->override);
127 reg &= ~OVERRIDE_SESS_VLD_SEL;
128 writel(reg, &cdns->otg_cdnsp_regs->override);
130 EXPORT_SYMBOL_GPL(cdns_set_vbus);
132 bool cdns_is_host(struct cdns *cdns)
134 if (cdns->dr_mode == USB_DR_MODE_HOST)
136 else if (cdns_get_id(cdns) == CDNS3_ID_HOST)
142 bool cdns_is_device(struct cdns *cdns)
144 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
146 else if (cdns->dr_mode == USB_DR_MODE_OTG)
147 if (cdns_get_id(cdns) == CDNS3_ID_PERIPHERAL)
154 * cdns_otg_disable_irq - Disable all OTG interrupts
155 * @cdns: Pointer to controller context structure
157 static void cdns_otg_disable_irq(struct cdns *cdns)
160 writel(0, &cdns->otg_irq_regs->ien);
164 * cdns_otg_enable_irq - enable id and sess_valid interrupts
165 * @cdns: Pointer to controller context structure
167 static void cdns_otg_enable_irq(struct cdns *cdns)
169 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
170 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien);
174 * cdns_drd_host_on - start host.
175 * @cdns: Pointer to controller context structure.
177 * Returns 0 on success otherwise negative errno.
179 int cdns_drd_host_on(struct cdns *cdns)
184 /* Enable host mode. */
185 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
186 &cdns->otg_regs->cmd);
188 if (cdns->version == CDNSP_CONTROLLER_V2)
189 ready_bit = OTGSTS_CDNSP_XHCI_READY;
191 ready_bit = OTGSTS_CDNS3_XHCI_READY;
193 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
194 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
195 val & ready_bit, 1, 100000);
198 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
200 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_HOST);
201 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
206 * cdns_drd_host_off - stop host.
207 * @cdns: Pointer to controller context structure.
209 void cdns_drd_host_off(struct cdns *cdns)
213 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
214 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
215 &cdns->otg_regs->cmd);
217 /* Waiting till H_IDLE state.*/
218 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
219 !(val & OTGSTATE_HOST_STATE_MASK),
221 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
222 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
226 * cdns_drd_gadget_on - start gadget.
227 * @cdns: Pointer to controller context structure.
229 * Returns 0 on success otherwise negative errno
231 int cdns_drd_gadget_on(struct cdns *cdns)
233 u32 reg = OTGCMD_OTG_DIS;
237 /* switch OTG core */
238 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
240 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
242 if (cdns->version == CDNSP_CONTROLLER_V2)
243 ready_bit = OTGSTS_CDNSP_DEV_READY;
245 ready_bit = OTGSTS_CDNS3_DEV_READY;
247 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
248 val & ready_bit, 1, 100000);
250 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
254 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_DEVICE);
255 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
258 EXPORT_SYMBOL_GPL(cdns_drd_gadget_on);
261 * cdns_drd_gadget_off - stop gadget.
262 * @cdns: Pointer to controller context structure.
264 void cdns_drd_gadget_off(struct cdns *cdns)
269 * Driver should wait at least 10us after disabling Device
270 * before turning-off Device (DEV_BUS_DROP).
272 usleep_range(20, 30);
273 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
274 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
275 &cdns->otg_regs->cmd);
276 /* Waiting till DEV_IDLE state.*/
277 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
278 !(val & OTGSTATE_DEV_STATE_MASK),
280 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
281 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
283 EXPORT_SYMBOL_GPL(cdns_drd_gadget_off);
286 * cdns_init_otg_mode - initialize drd controller
287 * @cdns: Pointer to controller context structure
289 * Returns 0 on success otherwise negative errno
291 static int cdns_init_otg_mode(struct cdns *cdns)
295 cdns_otg_disable_irq(cdns);
296 /* clear all interrupts */
297 writel(~0, &cdns->otg_irq_regs->ivect);
299 ret = cdns_set_mode(cdns, USB_DR_MODE_OTG);
303 cdns_otg_enable_irq(cdns);
309 * cdns_drd_update_mode - initialize mode of operation
310 * @cdns: Pointer to controller context structure
312 * Returns 0 on success otherwise negative errno
314 int cdns_drd_update_mode(struct cdns *cdns)
318 switch (cdns->dr_mode) {
319 case USB_DR_MODE_PERIPHERAL:
320 ret = cdns_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
322 case USB_DR_MODE_HOST:
323 ret = cdns_set_mode(cdns, USB_DR_MODE_HOST);
325 case USB_DR_MODE_OTG:
326 ret = cdns_init_otg_mode(cdns);
329 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
337 static irqreturn_t cdns_drd_thread_irq(int irq, void *data)
339 struct cdns *cdns = data;
341 cdns_hw_role_switch(cdns);
347 * cdns_drd_irq - interrupt handler for OTG events
349 * @irq: irq number for cdns core device
350 * @data: structure of cdns
352 * Returns IRQ_HANDLED or IRQ_NONE
354 static irqreturn_t cdns_drd_irq(int irq, void *data)
356 irqreturn_t ret = IRQ_NONE;
357 struct cdns *cdns = data;
360 if (cdns->dr_mode != USB_DR_MODE_OTG)
366 reg = readl(&cdns->otg_irq_regs->ivect);
371 if (reg & OTGIEN_ID_CHANGE_INT) {
372 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
375 ret = IRQ_WAKE_THREAD;
378 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
379 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
380 cdns_get_vbus(cdns));
382 ret = IRQ_WAKE_THREAD;
385 writel(~0, &cdns->otg_irq_regs->ivect);
389 int cdns_drd_init(struct cdns *cdns)
395 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
397 return PTR_ERR(regs);
399 /* Detection of DRD version. Controller has been released
400 * in three versions. All are very similar and are software compatible,
401 * but they have same changes in register maps.
402 * The first register in oldest version is command register and it's
403 * read only. Driver should read 0 from it. On the other hand, in v1
404 * and v2 the first register contains device ID number which is not
405 * set to 0. Driver uses this fact to detect the proper version of
408 cdns->otg_v0_regs = regs;
409 if (!readl(&cdns->otg_v0_regs->cmd)) {
410 cdns->version = CDNS3_CONTROLLER_V0;
411 cdns->otg_v1_regs = NULL;
412 cdns->otg_cdnsp_regs = NULL;
413 cdns->otg_regs = regs;
414 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
415 &cdns->otg_v0_regs->ien;
416 writel(1, &cdns->otg_v0_regs->simulate);
417 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
418 readl(&cdns->otg_v0_regs->version));
420 cdns->otg_v0_regs = NULL;
421 cdns->otg_v1_regs = regs;
422 cdns->otg_cdnsp_regs = regs;
424 cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd;
426 state = readl(&cdns->otg_cdnsp_regs->did);
428 if (OTG_CDNSP_CHECK_DID(state)) {
429 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
430 &cdns->otg_cdnsp_regs->ien;
431 cdns->version = CDNSP_CONTROLLER_V2;
432 } else if (OTG_CDNS3_CHECK_DID(state)) {
433 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
434 &cdns->otg_v1_regs->ien;
435 writel(1, &cdns->otg_v1_regs->simulate);
436 cdns->version = CDNS3_CONTROLLER_V1;
438 dev_err(cdns->dev, "not supporte DID=0x%08x\n", state);
442 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
443 readl(&cdns->otg_v1_regs->did),
444 readl(&cdns->otg_v1_regs->rid));
447 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
449 /* Update dr_mode according to STRAP configuration. */
450 cdns->dr_mode = USB_DR_MODE_OTG;
452 if ((cdns->version == CDNSP_CONTROLLER_V2 &&
453 state == OTGSTS_CDNSP_STRAP_HOST) ||
454 (cdns->version != CDNSP_CONTROLLER_V2 &&
455 state == OTGSTS_STRAP_HOST)) {
456 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
457 cdns->dr_mode = USB_DR_MODE_HOST;
458 } else if ((cdns->version == CDNSP_CONTROLLER_V2 &&
459 state == OTGSTS_CDNSP_STRAP_GADGET) ||
460 (cdns->version != CDNSP_CONTROLLER_V2 &&
461 state == OTGSTS_STRAP_GADGET)) {
462 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
463 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
466 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
470 dev_name(cdns->dev), cdns);
472 dev_err(cdns->dev, "couldn't get otg_irq\n");
476 state = readl(&cdns->otg_regs->sts);
477 if (OTGSTS_OTG_NRDY(state)) {
478 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
485 int cdns_drd_exit(struct cdns *cdns)
487 cdns_otg_disable_irq(cdns);
492 /* Indicate the cdns3 core was power lost before */
493 bool cdns_power_is_lost(struct cdns *cdns)
495 if (cdns->version == CDNS3_CONTROLLER_V0) {
496 if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
499 if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
504 EXPORT_SYMBOL_GPL(cdns_power_is_lost);