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>
24 * cdns3_set_mode - change mode of OTG Core
25 * @cdns: pointer to context structure
26 * @mode: selected mode from cdns_role
28 * Returns 0 on success otherwise negative errno
30 int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode)
35 case USB_DR_MODE_PERIPHERAL:
37 case USB_DR_MODE_HOST:
40 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
41 if (cdns->version == CDNS3_CONTROLLER_V1) {
42 reg = readl(&cdns->otg_v1_regs->override);
43 reg |= OVERRIDE_IDPULLUP;
44 writel(reg, &cdns->otg_v1_regs->override);
46 reg = readl(&cdns->otg_v0_regs->ctrl1);
47 reg |= OVERRIDE_IDPULLUP_V0;
48 writel(reg, &cdns->otg_v0_regs->ctrl1);
52 * Hardware specification says: "ID_VALUE must be valid within
53 * 50ms after idpullup is set to '1" so driver must wait
54 * 50ms before reading this pin.
56 usleep_range(50000, 60000);
59 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
66 int cdns3_get_id(struct cdns3 *cdns)
70 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
71 dev_dbg(cdns->dev, "OTG ID: %d", id);
76 int cdns3_get_vbus(struct cdns3 *cdns)
80 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
81 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
86 bool cdns3_is_host(struct cdns3 *cdns)
88 if (cdns->dr_mode == USB_DR_MODE_HOST)
90 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST)
96 bool cdns3_is_device(struct cdns3 *cdns)
98 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
100 else if (cdns->dr_mode == USB_DR_MODE_OTG)
101 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL)
108 * cdns3_otg_disable_irq - Disable all OTG interrupts
109 * @cdns: Pointer to controller context structure
111 static void cdns3_otg_disable_irq(struct cdns3 *cdns)
113 writel(0, &cdns->otg_regs->ien);
117 * cdns3_otg_enable_irq - enable id and sess_valid interrupts
118 * @cdns: Pointer to controller context structure
120 static void cdns3_otg_enable_irq(struct cdns3 *cdns)
122 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
123 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_regs->ien);
127 * cdns3_drd_host_on - start host.
128 * @cdns: Pointer to controller context structure.
130 * Returns 0 on success otherwise negative errno.
132 int cdns3_drd_host_on(struct cdns3 *cdns)
137 /* Enable host mode. */
138 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
139 &cdns->otg_regs->cmd);
141 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
142 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
143 val & OTGSTS_XHCI_READY, 1, 100000);
146 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
152 * cdns3_drd_host_off - stop host.
153 * @cdns: Pointer to controller context structure.
155 void cdns3_drd_host_off(struct cdns3 *cdns)
159 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
160 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
161 &cdns->otg_regs->cmd);
163 /* Waiting till H_IDLE state.*/
164 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
165 !(val & OTGSTATE_HOST_STATE_MASK),
170 * cdns3_drd_gadget_on - start gadget.
171 * @cdns: Pointer to controller context structure.
173 * Returns 0 on success otherwise negative errno
175 int cdns3_drd_gadget_on(struct cdns3 *cdns)
178 u32 reg = OTGCMD_OTG_DIS;
180 /* switch OTG core */
181 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
183 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
185 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
186 val & OTGSTS_DEV_READY,
189 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
197 * cdns3_drd_gadget_off - stop gadget.
198 * @cdns: Pointer to controller context structure.
200 void cdns3_drd_gadget_off(struct cdns3 *cdns)
205 * Driver should wait at least 10us after disabling Device
206 * before turning-off Device (DEV_BUS_DROP).
208 usleep_range(20, 30);
209 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
210 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
211 &cdns->otg_regs->cmd);
212 /* Waiting till DEV_IDLE state.*/
213 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
214 !(val & OTGSTATE_DEV_STATE_MASK),
219 * cdns3_init_otg_mode - initialize drd controller
220 * @cdns: Pointer to controller context structure
222 * Returns 0 on success otherwise negative errno
224 static int cdns3_init_otg_mode(struct cdns3 *cdns)
228 cdns3_otg_disable_irq(cdns);
229 /* clear all interrupts */
230 writel(~0, &cdns->otg_regs->ivect);
232 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG);
236 cdns3_otg_enable_irq(cdns);
242 * cdns3_drd_update_mode - initialize mode of operation
243 * @cdns: Pointer to controller context structure
245 * Returns 0 on success otherwise negative errno
247 int cdns3_drd_update_mode(struct cdns3 *cdns)
251 switch (cdns->dr_mode) {
252 case USB_DR_MODE_PERIPHERAL:
253 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
255 case USB_DR_MODE_HOST:
256 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
258 case USB_DR_MODE_OTG:
259 ret = cdns3_init_otg_mode(cdns);
262 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
270 static irqreturn_t cdns3_drd_thread_irq(int irq, void *data)
272 struct cdns3 *cdns = data;
274 cdns3_hw_role_switch(cdns);
280 * cdns3_drd_irq - interrupt handler for OTG events
282 * @irq: irq number for cdns3 core device
283 * @data: structure of cdns3
285 * Returns IRQ_HANDLED or IRQ_NONE
287 static irqreturn_t cdns3_drd_irq(int irq, void *data)
289 irqreturn_t ret = IRQ_NONE;
290 struct cdns3 *cdns = data;
293 if (cdns->dr_mode != USB_DR_MODE_OTG)
296 reg = readl(&cdns->otg_regs->ivect);
301 if (reg & OTGIEN_ID_CHANGE_INT) {
302 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
305 ret = IRQ_WAKE_THREAD;
308 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
309 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
310 cdns3_get_vbus(cdns));
312 ret = IRQ_WAKE_THREAD;
315 writel(~0, &cdns->otg_regs->ivect);
319 int cdns3_drd_init(struct cdns3 *cdns)
325 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
327 return PTR_ERR(regs);
329 /* Detection of DRD version. Controller has been released
330 * in two versions. Both are similar, but they have same changes
332 * The first register in old version is command register and it's read
333 * only, so driver should read 0 from it. On the other hand, in v1
334 * the first register contains device ID number which is not set to 0.
335 * Driver uses this fact to detect the proper version of
338 cdns->otg_v0_regs = regs;
339 if (!readl(&cdns->otg_v0_regs->cmd)) {
340 cdns->version = CDNS3_CONTROLLER_V0;
341 cdns->otg_v1_regs = NULL;
342 cdns->otg_regs = regs;
343 writel(1, &cdns->otg_v0_regs->simulate);
344 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
345 readl(&cdns->otg_v0_regs->version));
347 cdns->otg_v0_regs = NULL;
348 cdns->otg_v1_regs = regs;
349 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd;
350 cdns->version = CDNS3_CONTROLLER_V1;
351 writel(1, &cdns->otg_v1_regs->simulate);
352 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
353 readl(&cdns->otg_v1_regs->did),
354 readl(&cdns->otg_v1_regs->rid));
357 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
359 /* Update dr_mode according to STRAP configuration. */
360 cdns->dr_mode = USB_DR_MODE_OTG;
361 if (state == OTGSTS_STRAP_HOST) {
362 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
363 cdns->dr_mode = USB_DR_MODE_HOST;
364 } else if (state == OTGSTS_STRAP_GADGET) {
365 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
366 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
369 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
371 cdns3_drd_thread_irq,
373 dev_name(cdns->dev), cdns);
375 dev_err(cdns->dev, "couldn't get otg_irq\n");
379 state = readl(&cdns->otg_regs->sts);
380 if (OTGSTS_OTG_NRDY(state)) {
381 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
388 int cdns3_drd_exit(struct cdns3 *cdns)
390 cdns3_otg_disable_irq(cdns);