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>
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
)
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
);
47 reg
= readl(&cdns
->otg_v0_regs
->ctrl1
);
48 reg
|= OVERRIDE_IDPULLUP_V0
;
49 writel(reg
, &cdns
->otg_v0_regs
->ctrl1
);
53 * Hardware specification says: "ID_VALUE must be valid within
54 * 50ms after idpullup is set to '1" so driver must wait
55 * 50ms before reading this pin.
57 usleep_range(50000, 60000);
60 dev_err(cdns
->dev
, "Unsupported mode of operation %d\n", mode
);
67 int cdns3_get_id(struct cdns3
*cdns
)
71 id
= readl(&cdns
->otg_regs
->sts
) & OTGSTS_ID_VALUE
;
72 dev_dbg(cdns
->dev
, "OTG ID: %d", id
);
77 int cdns3_get_vbus(struct cdns3
*cdns
)
81 vbus
= !!(readl(&cdns
->otg_regs
->sts
) & OTGSTS_VBUS_VALID
);
82 dev_dbg(cdns
->dev
, "OTG VBUS: %d", vbus
);
87 int cdns3_is_host(struct cdns3
*cdns
)
89 if (cdns
->dr_mode
== USB_DR_MODE_HOST
)
91 else if (!cdns3_get_id(cdns
))
97 int cdns3_is_device(struct cdns3
*cdns
)
99 if (cdns
->dr_mode
== USB_DR_MODE_PERIPHERAL
)
101 else if (cdns
->dr_mode
== USB_DR_MODE_OTG
)
102 if (cdns3_get_id(cdns
))
109 * cdns3_otg_disable_irq - Disable all OTG interrupts
110 * @cdns: Pointer to controller context structure
112 static void cdns3_otg_disable_irq(struct cdns3
*cdns
)
114 writel(0, &cdns
->otg_regs
->ien
);
118 * cdns3_otg_enable_irq - enable id and sess_valid interrupts
119 * @cdns: Pointer to controller context structure
121 static void cdns3_otg_enable_irq(struct cdns3
*cdns
)
123 writel(OTGIEN_ID_CHANGE_INT
| OTGIEN_VBUSVALID_RISE_INT
|
124 OTGIEN_VBUSVALID_FALL_INT
, &cdns
->otg_regs
->ien
);
128 * cdns3_drd_switch_host - start/stop host
129 * @cdns: Pointer to controller context structure
130 * @on: 1 for start, 0 for stop
132 * Returns 0 on success otherwise negative errno
134 int cdns3_drd_switch_host(struct cdns3
*cdns
, int on
)
137 u32 reg
= OTGCMD_OTG_DIS
;
139 /* switch OTG core */
141 writel(OTGCMD_HOST_BUS_REQ
| reg
, &cdns
->otg_regs
->cmd
);
143 dev_dbg(cdns
->dev
, "Waiting till Host mode is turned on\n");
144 ret
= readl_poll_timeout_atomic(&cdns
->otg_regs
->sts
, val
,
145 val
& OTGSTS_XHCI_READY
,
148 dev_err(cdns
->dev
, "timeout waiting for xhci_ready\n");
152 writel(OTGCMD_HOST_BUS_DROP
| OTGCMD_DEV_BUS_DROP
|
153 OTGCMD_DEV_POWER_OFF
| OTGCMD_HOST_POWER_OFF
,
154 &cdns
->otg_regs
->cmd
);
155 /* Waiting till H_IDLE state.*/
156 readl_poll_timeout_atomic(&cdns
->otg_regs
->state
, val
,
157 !(val
& OTGSTATE_HOST_STATE_MASK
),
165 * cdns3_drd_switch_gadget - start/stop gadget
166 * @cdns: Pointer to controller context structure
167 * @on: 1 for start, 0 for stop
169 * Returns 0 on success otherwise negative errno
171 int cdns3_drd_switch_gadget(struct cdns3
*cdns
, int on
)
174 u32 reg
= OTGCMD_OTG_DIS
;
176 /* switch OTG core */
178 writel(OTGCMD_DEV_BUS_REQ
| reg
, &cdns
->otg_regs
->cmd
);
180 dev_dbg(cdns
->dev
, "Waiting till Device mode is turned on\n");
182 ret
= readl_poll_timeout_atomic(&cdns
->otg_regs
->sts
, val
,
183 val
& OTGSTS_DEV_READY
,
186 dev_err(cdns
->dev
, "timeout waiting for dev_ready\n");
191 * driver should wait at least 10us after disabling Device
192 * before turning-off Device (DEV_BUS_DROP)
194 usleep_range(20, 30);
195 writel(OTGCMD_HOST_BUS_DROP
| OTGCMD_DEV_BUS_DROP
|
196 OTGCMD_DEV_POWER_OFF
| OTGCMD_HOST_POWER_OFF
,
197 &cdns
->otg_regs
->cmd
);
198 /* Waiting till DEV_IDLE state.*/
199 readl_poll_timeout_atomic(&cdns
->otg_regs
->state
, val
,
200 !(val
& OTGSTATE_DEV_STATE_MASK
),
208 * cdns3_init_otg_mode - initialize drd controller
209 * @cdns: Pointer to controller context structure
211 * Returns 0 on success otherwise negative errno
213 static int cdns3_init_otg_mode(struct cdns3
*cdns
)
217 cdns3_otg_disable_irq(cdns
);
218 /* clear all interrupts */
219 writel(~0, &cdns
->otg_regs
->ivect
);
221 ret
= cdns3_set_mode(cdns
, USB_DR_MODE_OTG
);
225 cdns3_otg_enable_irq(cdns
);
230 * cdns3_drd_update_mode - initialize mode of operation
231 * @cdns: Pointer to controller context structure
233 * Returns 0 on success otherwise negative errno
235 int cdns3_drd_update_mode(struct cdns3
*cdns
)
239 switch (cdns
->dr_mode
) {
240 case USB_DR_MODE_PERIPHERAL
:
241 ret
= cdns3_set_mode(cdns
, USB_DR_MODE_PERIPHERAL
);
243 case USB_DR_MODE_HOST
:
244 ret
= cdns3_set_mode(cdns
, USB_DR_MODE_HOST
);
246 case USB_DR_MODE_OTG
:
247 ret
= cdns3_init_otg_mode(cdns
);
250 dev_err(cdns
->dev
, "Unsupported mode of operation %d\n",
258 static irqreturn_t
cdns3_drd_thread_irq(int irq
, void *data
)
260 struct cdns3
*cdns
= data
;
262 cdns3_hw_role_switch(cdns
);
268 * cdns3_drd_irq - interrupt handler for OTG events
270 * @irq: irq number for cdns3 core device
271 * @data: structure of cdns3
273 * Returns IRQ_HANDLED or IRQ_NONE
275 static irqreturn_t
cdns3_drd_irq(int irq
, void *data
)
277 irqreturn_t ret
= IRQ_NONE
;
278 struct cdns3
*cdns
= data
;
281 if (cdns
->dr_mode
!= USB_DR_MODE_OTG
)
284 reg
= readl(&cdns
->otg_regs
->ivect
);
289 if (reg
& OTGIEN_ID_CHANGE_INT
) {
290 dev_dbg(cdns
->dev
, "OTG IRQ: new ID: %d\n",
293 ret
= IRQ_WAKE_THREAD
;
296 if (reg
& (OTGIEN_VBUSVALID_RISE_INT
| OTGIEN_VBUSVALID_FALL_INT
)) {
297 dev_dbg(cdns
->dev
, "OTG IRQ: new VBUS: %d\n",
298 cdns3_get_vbus(cdns
));
300 ret
= IRQ_WAKE_THREAD
;
303 writel(~0, &cdns
->otg_regs
->ivect
);
307 int cdns3_drd_init(struct cdns3
*cdns
)
313 regs
= devm_ioremap_resource(cdns
->dev
, &cdns
->otg_res
);
315 return PTR_ERR(regs
);
317 /* Detection of DRD version. Controller has been released
318 * in two versions. Both are similar, but they have same changes
320 * The first register in old version is command register and it's read
321 * only, so driver should read 0 from it. On the other hand, in v1
322 * the first register contains device ID number which is not set to 0.
323 * Driver uses this fact to detect the proper version of
326 cdns
->otg_v0_regs
= regs
;
327 if (!readl(&cdns
->otg_v0_regs
->cmd
)) {
328 cdns
->version
= CDNS3_CONTROLLER_V0
;
329 cdns
->otg_v1_regs
= NULL
;
330 cdns
->otg_regs
= regs
;
331 writel(1, &cdns
->otg_v0_regs
->simulate
);
332 dev_info(cdns
->dev
, "DRD version v0 (%08x)\n",
333 readl(&cdns
->otg_v0_regs
->version
));
335 cdns
->otg_v0_regs
= NULL
;
336 cdns
->otg_v1_regs
= regs
;
337 cdns
->otg_regs
= (void *)&cdns
->otg_v1_regs
->cmd
;
338 cdns
->version
= CDNS3_CONTROLLER_V1
;
339 writel(1, &cdns
->otg_v1_regs
->simulate
);
340 dev_info(cdns
->dev
, "DRD version v1 (ID: %08x, rev: %08x)\n",
341 readl(&cdns
->otg_v1_regs
->did
),
342 readl(&cdns
->otg_v1_regs
->rid
));
345 state
= OTGSTS_STRAP(readl(&cdns
->otg_regs
->sts
));
347 /* Update dr_mode according to STRAP configuration. */
348 cdns
->dr_mode
= USB_DR_MODE_OTG
;
349 if (state
== OTGSTS_STRAP_HOST
) {
350 dev_dbg(cdns
->dev
, "Controller strapped to HOST\n");
351 cdns
->dr_mode
= USB_DR_MODE_HOST
;
352 } else if (state
== OTGSTS_STRAP_GADGET
) {
353 dev_dbg(cdns
->dev
, "Controller strapped to PERIPHERAL\n");
354 cdns
->dr_mode
= USB_DR_MODE_PERIPHERAL
;
357 ret
= devm_request_threaded_irq(cdns
->dev
, cdns
->otg_irq
,
359 cdns3_drd_thread_irq
,
361 dev_name(cdns
->dev
), cdns
);
364 dev_err(cdns
->dev
, "couldn't get otg_irq\n");
368 state
= readl(&cdns
->otg_regs
->sts
);
369 if (OTGSTS_OTG_NRDY(state
) != 0) {
370 dev_err(cdns
->dev
, "Cadence USB3 OTG device not ready\n");
377 int cdns3_drd_exit(struct cdns3
*cdns
)
379 cdns3_otg_disable_irq(cdns
);