1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS DRD Driver.
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2019 Texas Instruments
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/iopoll.h>
17 #include <linux/usb/otg.h>
18 #include <linux/phy/phy.h>
25 * cdns3_set_mode - change mode of OTG Core
26 * @cdns: pointer to context structure
27 * @mode: selected mode from cdns_role
29 * Returns 0 on success otherwise negative errno
31 int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode)
36 case USB_DR_MODE_PERIPHERAL:
38 case USB_DR_MODE_HOST:
41 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
42 if (cdns->version == CDNS3_CONTROLLER_V1) {
43 reg = readl(&cdns->otg_v1_regs->override);
44 reg |= OVERRIDE_IDPULLUP;
45 writel(reg, &cdns->otg_v1_regs->override);
48 * Enable work around feature built into the
49 * controller to address issue with RX Sensitivity
50 * est (EL_17) for USB2 PHY. The issue only occures
51 * for 0x0002450D controller version.
53 if (cdns->phyrst_a_enable) {
54 reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
55 reg |= PHYRST_CFG_PHYRST_A_ENABLE;
56 writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
59 reg = readl(&cdns->otg_v0_regs->ctrl1);
60 reg |= OVERRIDE_IDPULLUP_V0;
61 writel(reg, &cdns->otg_v0_regs->ctrl1);
65 * Hardware specification says: "ID_VALUE must be valid within
66 * 50ms after idpullup is set to '1" so driver must wait
67 * 50ms before reading this pin.
69 usleep_range(50000, 60000);
72 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
79 int cdns3_get_id(struct cdns3 *cdns)
83 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
84 dev_dbg(cdns->dev, "OTG ID: %d", id);
89 int cdns3_get_vbus(struct cdns3 *cdns)
93 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
94 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
99 bool cdns3_is_host(struct cdns3 *cdns)
101 if (cdns->dr_mode == USB_DR_MODE_HOST)
103 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST)
109 bool cdns3_is_device(struct cdns3 *cdns)
111 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
113 else if (cdns->dr_mode == USB_DR_MODE_OTG)
114 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL)
121 * cdns3_otg_disable_irq - Disable all OTG interrupts
122 * @cdns: Pointer to controller context structure
124 static void cdns3_otg_disable_irq(struct cdns3 *cdns)
126 writel(0, &cdns->otg_regs->ien);
130 * cdns3_otg_enable_irq - enable id and sess_valid interrupts
131 * @cdns: Pointer to controller context structure
133 static void cdns3_otg_enable_irq(struct cdns3 *cdns)
135 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
136 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_regs->ien);
140 * cdns3_drd_host_on - start host.
141 * @cdns: Pointer to controller context structure.
143 * Returns 0 on success otherwise negative errno.
145 int cdns3_drd_host_on(struct cdns3 *cdns)
150 /* Enable host mode. */
151 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
152 &cdns->otg_regs->cmd);
154 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
155 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
156 val & OTGSTS_XHCI_READY, 1, 100000);
159 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
161 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
166 * cdns3_drd_host_off - stop host.
167 * @cdns: Pointer to controller context structure.
169 void cdns3_drd_host_off(struct cdns3 *cdns)
173 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
174 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
175 &cdns->otg_regs->cmd);
177 /* Waiting till H_IDLE state.*/
178 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
179 !(val & OTGSTATE_HOST_STATE_MASK),
181 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
185 * cdns3_drd_gadget_on - start gadget.
186 * @cdns: Pointer to controller context structure.
188 * Returns 0 on success otherwise negative errno
190 int cdns3_drd_gadget_on(struct cdns3 *cdns)
193 u32 reg = OTGCMD_OTG_DIS;
195 /* switch OTG core */
196 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
198 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
200 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
201 val & OTGSTS_DEV_READY,
204 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
208 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
213 * cdns3_drd_gadget_off - stop gadget.
214 * @cdns: Pointer to controller context structure.
216 void cdns3_drd_gadget_off(struct cdns3 *cdns)
221 * Driver should wait at least 10us after disabling Device
222 * before turning-off Device (DEV_BUS_DROP).
224 usleep_range(20, 30);
225 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
226 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
227 &cdns->otg_regs->cmd);
228 /* Waiting till DEV_IDLE state.*/
229 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
230 !(val & OTGSTATE_DEV_STATE_MASK),
232 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
236 * cdns3_init_otg_mode - initialize drd controller
237 * @cdns: Pointer to controller context structure
239 * Returns 0 on success otherwise negative errno
241 static int cdns3_init_otg_mode(struct cdns3 *cdns)
245 cdns3_otg_disable_irq(cdns);
246 /* clear all interrupts */
247 writel(~0, &cdns->otg_regs->ivect);
249 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG);
253 cdns3_otg_enable_irq(cdns);
259 * cdns3_drd_update_mode - initialize mode of operation
260 * @cdns: Pointer to controller context structure
262 * Returns 0 on success otherwise negative errno
264 int cdns3_drd_update_mode(struct cdns3 *cdns)
268 switch (cdns->dr_mode) {
269 case USB_DR_MODE_PERIPHERAL:
270 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
272 case USB_DR_MODE_HOST:
273 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
275 case USB_DR_MODE_OTG:
276 ret = cdns3_init_otg_mode(cdns);
279 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
287 static irqreturn_t cdns3_drd_thread_irq(int irq, void *data)
289 struct cdns3 *cdns = data;
291 cdns3_hw_role_switch(cdns);
297 * cdns3_drd_irq - interrupt handler for OTG events
299 * @irq: irq number for cdns3 core device
300 * @data: structure of cdns3
302 * Returns IRQ_HANDLED or IRQ_NONE
304 static irqreturn_t cdns3_drd_irq(int irq, void *data)
306 irqreturn_t ret = IRQ_NONE;
307 struct cdns3 *cdns = data;
310 if (cdns->dr_mode != USB_DR_MODE_OTG)
316 reg = readl(&cdns->otg_regs->ivect);
321 if (reg & OTGIEN_ID_CHANGE_INT) {
322 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
325 ret = IRQ_WAKE_THREAD;
328 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
329 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
330 cdns3_get_vbus(cdns));
332 ret = IRQ_WAKE_THREAD;
335 writel(~0, &cdns->otg_regs->ivect);
339 int cdns3_drd_init(struct cdns3 *cdns)
345 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
347 return PTR_ERR(regs);
349 /* Detection of DRD version. Controller has been released
350 * in two versions. Both are similar, but they have same changes
352 * The first register in old version is command register and it's read
353 * only, so driver should read 0 from it. On the other hand, in v1
354 * the first register contains device ID number which is not set to 0.
355 * Driver uses this fact to detect the proper version of
358 cdns->otg_v0_regs = regs;
359 if (!readl(&cdns->otg_v0_regs->cmd)) {
360 cdns->version = CDNS3_CONTROLLER_V0;
361 cdns->otg_v1_regs = NULL;
362 cdns->otg_regs = regs;
363 writel(1, &cdns->otg_v0_regs->simulate);
364 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
365 readl(&cdns->otg_v0_regs->version));
367 cdns->otg_v0_regs = NULL;
368 cdns->otg_v1_regs = regs;
369 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd;
370 cdns->version = CDNS3_CONTROLLER_V1;
371 writel(1, &cdns->otg_v1_regs->simulate);
372 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
373 readl(&cdns->otg_v1_regs->did),
374 readl(&cdns->otg_v1_regs->rid));
377 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
379 /* Update dr_mode according to STRAP configuration. */
380 cdns->dr_mode = USB_DR_MODE_OTG;
381 if (state == OTGSTS_STRAP_HOST) {
382 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
383 cdns->dr_mode = USB_DR_MODE_HOST;
384 } else if (state == OTGSTS_STRAP_GADGET) {
385 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
386 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
389 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
391 cdns3_drd_thread_irq,
393 dev_name(cdns->dev), cdns);
395 dev_err(cdns->dev, "couldn't get otg_irq\n");
399 state = readl(&cdns->otg_regs->sts);
400 if (OTGSTS_OTG_NRDY(state)) {
401 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
408 int cdns3_drd_exit(struct cdns3 *cdns)
410 cdns3_otg_disable_irq(cdns);