Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / usb / cdns3 / drd.c
blob38ccd29e4cdef1e0dda34fc13cb4bb774d9642ca
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence USBSS DRD Driver.
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2019 Texas Instruments
8 * Author: Pawel Laszczak <pawell@cadence.com>
9 * Roger Quadros <rogerq@ti.com>
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>
20 #include "gadget.h"
21 #include "drd.h"
22 #include "core.h"
24 /**
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)
33 u32 reg;
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 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);
58 } else {
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);
70 break;
71 default:
72 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
73 return -EINVAL;
76 return 0;
79 int cdns3_get_id(struct cdns3 *cdns)
81 int id;
83 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
84 dev_dbg(cdns->dev, "OTG ID: %d", id);
86 return id;
89 int cdns3_get_vbus(struct cdns3 *cdns)
91 int vbus;
93 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
94 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
96 return vbus;
99 bool cdns3_is_host(struct cdns3 *cdns)
101 if (cdns->dr_mode == USB_DR_MODE_HOST)
102 return true;
103 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST)
104 return true;
106 return false;
109 bool cdns3_is_device(struct cdns3 *cdns)
111 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
112 return true;
113 else if (cdns->dr_mode == USB_DR_MODE_OTG)
114 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL)
115 return true;
117 return false;
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)
147 u32 val;
148 int ret;
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);
158 if (ret)
159 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
161 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
162 return ret;
166 * cdns3_drd_host_off - stop host.
167 * @cdns: Pointer to controller context structure.
169 void cdns3_drd_host_off(struct cdns3 *cdns)
171 u32 val;
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),
180 1, 2000000);
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)
192 int ret, val;
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,
202 1, 100000);
203 if (ret) {
204 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
205 return ret;
208 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
209 return 0;
213 * cdns3_drd_gadget_off - stop gadget.
214 * @cdns: Pointer to controller context structure.
216 void cdns3_drd_gadget_off(struct cdns3 *cdns)
218 u32 val;
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),
231 1, 2000000);
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)
243 int ret;
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);
250 if (ret)
251 return ret;
253 cdns3_otg_enable_irq(cdns);
255 return 0;
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)
266 int ret;
268 switch (cdns->dr_mode) {
269 case USB_DR_MODE_PERIPHERAL:
270 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
271 break;
272 case USB_DR_MODE_HOST:
273 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
274 break;
275 case USB_DR_MODE_OTG:
276 ret = cdns3_init_otg_mode(cdns);
277 break;
278 default:
279 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
280 cdns->dr_mode);
281 return -EINVAL;
284 return ret;
287 static irqreturn_t cdns3_drd_thread_irq(int irq, void *data)
289 struct cdns3 *cdns = data;
291 cdns3_hw_role_switch(cdns);
293 return IRQ_HANDLED;
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;
308 u32 reg;
310 if (cdns->dr_mode != USB_DR_MODE_OTG)
311 return IRQ_NONE;
313 if (cdns->in_lpm)
314 return ret;
316 reg = readl(&cdns->otg_regs->ivect);
318 if (!reg)
319 return IRQ_NONE;
321 if (reg & OTGIEN_ID_CHANGE_INT) {
322 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
323 cdns3_get_id(cdns));
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);
336 return ret;
339 int cdns3_drd_init(struct cdns3 *cdns)
341 void __iomem *regs;
342 u32 state;
343 int ret;
345 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
346 if (IS_ERR(regs))
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
351 * in register maps.
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
356 * controller.
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));
366 } else {
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,
390 cdns3_drd_irq,
391 cdns3_drd_thread_irq,
392 IRQF_SHARED,
393 dev_name(cdns->dev), cdns);
394 if (ret) {
395 dev_err(cdns->dev, "couldn't get otg_irq\n");
396 return ret;
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");
402 return -ENODEV;
405 return 0;
408 int cdns3_drd_exit(struct cdns3 *cdns)
410 cdns3_otg_disable_irq(cdns);
411 return 0;