1 // SPDX-License-Identifier: GPL-2.0
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>
14 #include <dm/device_compat.h>
15 #include <linux/delay.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/usb/otg.h>
24 #define readl_poll_timeout_atomic readl_poll_timeout
25 #define usleep_range(a, b) udelay((b))
27 * cdns3_set_mode - change mode of OTG Core
28 * @cdns: pointer to context structure
29 * @mode: selected mode from cdns_role
31 * Returns 0 on success otherwise negative errno
33 int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode)
39 case USB_DR_MODE_PERIPHERAL:
41 case USB_DR_MODE_HOST:
44 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
45 if (cdns->version == CDNS3_CONTROLLER_V1) {
46 reg = readl(&cdns->otg_v1_regs->override);
47 reg |= OVERRIDE_IDPULLUP;
48 writel(reg, &cdns->otg_v1_regs->override);
50 reg = readl(&cdns->otg_v0_regs->ctrl1);
51 reg |= OVERRIDE_IDPULLUP_V0;
52 writel(reg, &cdns->otg_v0_regs->ctrl1);
56 * Hardware specification says: "ID_VALUE must be valid within
57 * 50ms after idpullup is set to '1" so driver must wait
58 * 50ms before reading this pin.
60 usleep_range(50000, 60000);
63 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
70 int cdns3_get_id(struct cdns3 *cdns)
74 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
75 dev_dbg(cdns->dev, "OTG ID: %d", id);
80 int cdns3_get_vbus(struct cdns3 *cdns)
84 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
85 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
90 int cdns3_is_host(struct cdns3 *cdns)
92 if (cdns->dr_mode == USB_DR_MODE_HOST)
94 else if (!cdns3_get_id(cdns))
100 int cdns3_is_device(struct cdns3 *cdns)
102 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
104 else if (cdns->dr_mode == USB_DR_MODE_OTG)
105 if (cdns3_get_id(cdns))
112 * cdns3_drd_switch_host - start/stop host
113 * @cdns: Pointer to controller context structure
114 * @on: 1 for start, 0 for stop
116 * Returns 0 on success otherwise negative errno
118 int cdns3_drd_switch_host(struct cdns3 *cdns, int on)
121 u32 reg = OTGCMD_OTG_DIS;
123 /* switch OTG core */
125 writel(OTGCMD_HOST_BUS_REQ | reg, &cdns->otg_regs->cmd);
127 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
128 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
129 val & OTGSTS_XHCI_READY,
132 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
136 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
137 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
138 &cdns->otg_regs->cmd);
139 /* Waiting till H_IDLE state.*/
140 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
141 !(val & OTGSTATE_HOST_STATE_MASK),
149 * cdns3_drd_switch_gadget - start/stop gadget
150 * @cdns: Pointer to controller context structure
151 * @on: 1 for start, 0 for stop
153 * Returns 0 on success otherwise negative errno
155 int cdns3_drd_switch_gadget(struct cdns3 *cdns, int on)
158 u32 reg = OTGCMD_OTG_DIS;
160 /* switch OTG core */
162 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
164 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
166 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
167 val & OTGSTS_DEV_READY,
170 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
175 * driver should wait at least 10us after disabling Device
176 * before turning-off Device (DEV_BUS_DROP)
178 usleep_range(20, 30);
179 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
180 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
181 &cdns->otg_regs->cmd);
182 /* Waiting till DEV_IDLE state.*/
183 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
184 !(val & OTGSTATE_DEV_STATE_MASK),
192 * cdns3_init_otg_mode - initialize drd controller
193 * @cdns: Pointer to controller context structure
195 * Returns 0 on success otherwise negative errno
197 static int cdns3_init_otg_mode(struct cdns3 *cdns)
201 /* clear all interrupts */
202 writel(~0, &cdns->otg_regs->ivect);
204 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG);
212 * cdns3_drd_update_mode - initialize mode of operation
213 * @cdns: Pointer to controller context structure
215 * Returns 0 on success otherwise negative errno
217 int cdns3_drd_update_mode(struct cdns3 *cdns)
221 switch (cdns->dr_mode) {
222 case USB_DR_MODE_PERIPHERAL:
223 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
225 case USB_DR_MODE_HOST:
226 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
228 case USB_DR_MODE_OTG:
229 ret = cdns3_init_otg_mode(cdns);
232 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
240 int cdns3_drd_init(struct cdns3 *cdns)
246 regs = dev_remap_addr_name(cdns->dev, "otg");
250 /* Detection of DRD version. Controller has been released
251 * in two versions. Both are similar, but they have same changes
253 * The first register in old version is command register and it's read
254 * only, so driver should read 0 from it. On the other hand, in v1
255 * the first register contains device ID number which is not set to 0.
256 * Driver uses this fact to detect the proper version of
259 cdns->otg_v0_regs = regs;
260 if (!readl(&cdns->otg_v0_regs->cmd)) {
261 cdns->version = CDNS3_CONTROLLER_V0;
262 cdns->otg_v1_regs = NULL;
263 cdns->otg_regs = regs;
264 writel(1, &cdns->otg_v0_regs->simulate);
265 dev_info(cdns->dev, "DRD version v0 (%08x)\n",
266 readl(&cdns->otg_v0_regs->version));
268 cdns->otg_v0_regs = NULL;
269 cdns->otg_v1_regs = regs;
270 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd;
271 cdns->version = CDNS3_CONTROLLER_V1;
272 writel(1, &cdns->otg_v1_regs->simulate);
273 dev_info(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
274 readl(&cdns->otg_v1_regs->did),
275 readl(&cdns->otg_v1_regs->rid));
278 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
280 /* Update dr_mode according to STRAP configuration. */
281 cdns->dr_mode = USB_DR_MODE_OTG;
282 if (state == OTGSTS_STRAP_HOST) {
283 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
284 cdns->dr_mode = USB_DR_MODE_HOST;
285 } else if (state == OTGSTS_STRAP_GADGET) {
286 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
287 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
290 state = readl(&cdns->otg_regs->sts);
291 if (OTGSTS_OTG_NRDY(state) != 0) {
292 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
299 int cdns3_drd_exit(struct cdns3 *cdns)