Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/**
  3 * dwc3-omap.c - OMAP Specific Glue layer
  4 *
  5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  6 *
  7 * Authors: Felipe Balbi <balbi@ti.com>,
  8 *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/kernel.h>
 13#include <linux/slab.h>
 14#include <linux/irq.h>
 15#include <linux/interrupt.h>
 
 16#include <linux/platform_device.h>
 17#include <linux/pm_runtime.h>
 18#include <linux/dma-mapping.h>
 19#include <linux/ioport.h>
 20#include <linux/io.h>
 21#include <linux/of.h>
 22#include <linux/of_platform.h>
 23#include <linux/extcon.h>
 24#include <linux/regulator/consumer.h>
 25
 26#include <linux/usb/otg.h>
 27
 28/*
 29 * All these registers belong to OMAP's Wrapper around the
 30 * DesignWare USB3 Core.
 31 */
 32
 33#define USBOTGSS_REVISION			0x0000
 34#define USBOTGSS_SYSCONFIG			0x0010
 35#define USBOTGSS_IRQ_EOI			0x0020
 36#define USBOTGSS_EOI_OFFSET			0x0008
 37#define USBOTGSS_IRQSTATUS_RAW_0		0x0024
 38#define USBOTGSS_IRQSTATUS_0			0x0028
 39#define USBOTGSS_IRQENABLE_SET_0		0x002c
 40#define USBOTGSS_IRQENABLE_CLR_0		0x0030
 41#define USBOTGSS_IRQ0_OFFSET			0x0004
 42#define USBOTGSS_IRQSTATUS_RAW_1		0x0030
 43#define USBOTGSS_IRQSTATUS_1			0x0034
 44#define USBOTGSS_IRQENABLE_SET_1		0x0038
 45#define USBOTGSS_IRQENABLE_CLR_1		0x003c
 46#define USBOTGSS_IRQSTATUS_RAW_2		0x0040
 47#define USBOTGSS_IRQSTATUS_2			0x0044
 48#define USBOTGSS_IRQENABLE_SET_2		0x0048
 49#define USBOTGSS_IRQENABLE_CLR_2		0x004c
 50#define USBOTGSS_IRQSTATUS_RAW_3		0x0050
 51#define USBOTGSS_IRQSTATUS_3			0x0054
 52#define USBOTGSS_IRQENABLE_SET_3		0x0058
 53#define USBOTGSS_IRQENABLE_CLR_3		0x005c
 54#define USBOTGSS_IRQSTATUS_EOI_MISC		0x0030
 55#define USBOTGSS_IRQSTATUS_RAW_MISC		0x0034
 56#define USBOTGSS_IRQSTATUS_MISC			0x0038
 57#define USBOTGSS_IRQENABLE_SET_MISC		0x003c
 58#define USBOTGSS_IRQENABLE_CLR_MISC		0x0040
 59#define USBOTGSS_IRQMISC_OFFSET			0x03fc
 60#define USBOTGSS_UTMI_OTG_STATUS		0x0080
 61#define USBOTGSS_UTMI_OTG_CTRL			0x0084
 62#define USBOTGSS_UTMI_OTG_OFFSET		0x0480
 63#define USBOTGSS_TXFIFO_DEPTH			0x0508
 64#define USBOTGSS_RXFIFO_DEPTH			0x050c
 65#define USBOTGSS_MMRAM_OFFSET			0x0100
 66#define USBOTGSS_FLADJ				0x0104
 67#define USBOTGSS_DEBUG_CFG			0x0108
 68#define USBOTGSS_DEBUG_DATA			0x010c
 69#define USBOTGSS_DEV_EBC_EN			0x0110
 70#define USBOTGSS_DEBUG_OFFSET			0x0600
 71
 72/* SYSCONFIG REGISTER */
 73#define USBOTGSS_SYSCONFIG_DMADISABLE		BIT(16)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 74
 75/* IRQ_EOI REGISTER */
 76#define USBOTGSS_IRQ_EOI_LINE_NUMBER		BIT(0)
 77
 78/* IRQS0 BITS */
 79#define USBOTGSS_IRQO_COREIRQ_ST		BIT(0)
 80
 81/* IRQMISC BITS */
 82#define USBOTGSS_IRQMISC_DMADISABLECLR		BIT(17)
 83#define USBOTGSS_IRQMISC_OEVT			BIT(16)
 84#define USBOTGSS_IRQMISC_DRVVBUS_RISE		BIT(13)
 85#define USBOTGSS_IRQMISC_CHRGVBUS_RISE		BIT(12)
 86#define USBOTGSS_IRQMISC_DISCHRGVBUS_RISE	BIT(11)
 87#define USBOTGSS_IRQMISC_IDPULLUP_RISE		BIT(8)
 88#define USBOTGSS_IRQMISC_DRVVBUS_FALL		BIT(5)
 89#define USBOTGSS_IRQMISC_CHRGVBUS_FALL		BIT(4)
 90#define USBOTGSS_IRQMISC_DISCHRGVBUS_FALL		BIT(3)
 91#define USBOTGSS_IRQMISC_IDPULLUP_FALL		BIT(0)
 92
 93/* UTMI_OTG_STATUS REGISTER */
 94#define USBOTGSS_UTMI_OTG_STATUS_DRVVBUS	BIT(5)
 95#define USBOTGSS_UTMI_OTG_STATUS_CHRGVBUS	BIT(4)
 96#define USBOTGSS_UTMI_OTG_STATUS_DISCHRGVBUS	BIT(3)
 97#define USBOTGSS_UTMI_OTG_STATUS_IDPULLUP	BIT(0)
 98
 99/* UTMI_OTG_CTRL REGISTER */
100#define USBOTGSS_UTMI_OTG_CTRL_SW_MODE		BIT(31)
101#define USBOTGSS_UTMI_OTG_CTRL_POWERPRESENT	BIT(9)
102#define USBOTGSS_UTMI_OTG_CTRL_TXBITSTUFFENABLE BIT(8)
103#define USBOTGSS_UTMI_OTG_CTRL_IDDIG		BIT(4)
104#define USBOTGSS_UTMI_OTG_CTRL_SESSEND		BIT(3)
105#define USBOTGSS_UTMI_OTG_CTRL_SESSVALID	BIT(2)
106#define USBOTGSS_UTMI_OTG_CTRL_VBUSVALID	BIT(1)
107
108enum dwc3_omap_utmi_mode {
109	DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
110	DWC3_OMAP_UTMI_MODE_HW,
111	DWC3_OMAP_UTMI_MODE_SW,
112};
113
114struct dwc3_omap {
 
 
 
 
115	struct device		*dev;
116
117	int			irq;
118	void __iomem		*base;
119
120	u32			utmi_otg_ctrl;
121	u32			utmi_otg_offset;
122	u32			irqmisc_offset;
123	u32			irq_eoi_offset;
124	u32			debug_offset;
125	u32			irq0_offset;
126
127	struct extcon_dev	*edev;
128	struct notifier_block	vbus_nb;
129	struct notifier_block	id_nb;
130
131	struct regulator	*vbus_reg;
132};
133
134enum omap_dwc3_vbus_id_status {
135	OMAP_DWC3_ID_FLOAT,
136	OMAP_DWC3_ID_GROUND,
137	OMAP_DWC3_VBUS_OFF,
138	OMAP_DWC3_VBUS_VALID,
139};
140
141static inline u32 dwc3_omap_readl(void __iomem *base, u32 offset)
142{
143	return readl(base + offset);
144}
145
146static inline void dwc3_omap_writel(void __iomem *base, u32 offset, u32 value)
147{
148	writel(value, base + offset);
149}
150
151static u32 dwc3_omap_read_utmi_ctrl(struct dwc3_omap *omap)
152{
153	return dwc3_omap_readl(omap->base, USBOTGSS_UTMI_OTG_CTRL +
154							omap->utmi_otg_offset);
155}
156
157static void dwc3_omap_write_utmi_ctrl(struct dwc3_omap *omap, u32 value)
158{
159	dwc3_omap_writel(omap->base, USBOTGSS_UTMI_OTG_CTRL +
160					omap->utmi_otg_offset, value);
161
162}
163
164static u32 dwc3_omap_read_irq0_status(struct dwc3_omap *omap)
165{
166	return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_RAW_0 -
167						omap->irq0_offset);
168}
169
170static void dwc3_omap_write_irq0_status(struct dwc3_omap *omap, u32 value)
171{
172	dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_0 -
173						omap->irq0_offset, value);
174
175}
176
177static u32 dwc3_omap_read_irqmisc_status(struct dwc3_omap *omap)
178{
179	return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_RAW_MISC +
180						omap->irqmisc_offset);
181}
182
183static void dwc3_omap_write_irqmisc_status(struct dwc3_omap *omap, u32 value)
184{
185	dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_MISC +
186					omap->irqmisc_offset, value);
187
188}
189
190static void dwc3_omap_write_irqmisc_set(struct dwc3_omap *omap, u32 value)
191{
192	dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_MISC +
193						omap->irqmisc_offset, value);
194
195}
196
197static void dwc3_omap_write_irq0_set(struct dwc3_omap *omap, u32 value)
198{
199	dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_0 -
200						omap->irq0_offset, value);
201}
202
203static void dwc3_omap_write_irqmisc_clr(struct dwc3_omap *omap, u32 value)
204{
205	dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_CLR_MISC +
206						omap->irqmisc_offset, value);
207}
208
209static void dwc3_omap_write_irq0_clr(struct dwc3_omap *omap, u32 value)
210{
211	dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_CLR_0 -
212						omap->irq0_offset, value);
213}
214
215static void dwc3_omap_set_mailbox(struct dwc3_omap *omap,
216	enum omap_dwc3_vbus_id_status status)
217{
218	int	ret;
219	u32	val;
220
221	switch (status) {
222	case OMAP_DWC3_ID_GROUND:
223		if (omap->vbus_reg) {
224			ret = regulator_enable(omap->vbus_reg);
225			if (ret) {
226				dev_err(omap->dev, "regulator enable failed\n");
227				return;
228			}
229		}
230
231		val = dwc3_omap_read_utmi_ctrl(omap);
232		val &= ~USBOTGSS_UTMI_OTG_CTRL_IDDIG;
233		dwc3_omap_write_utmi_ctrl(omap, val);
234		break;
235
236	case OMAP_DWC3_VBUS_VALID:
237		val = dwc3_omap_read_utmi_ctrl(omap);
238		val &= ~USBOTGSS_UTMI_OTG_CTRL_SESSEND;
239		val |= USBOTGSS_UTMI_OTG_CTRL_VBUSVALID
240				| USBOTGSS_UTMI_OTG_CTRL_SESSVALID;
241		dwc3_omap_write_utmi_ctrl(omap, val);
242		break;
243
244	case OMAP_DWC3_ID_FLOAT:
245		if (omap->vbus_reg)
246			regulator_disable(omap->vbus_reg);
247		val = dwc3_omap_read_utmi_ctrl(omap);
248		val |= USBOTGSS_UTMI_OTG_CTRL_IDDIG;
249		dwc3_omap_write_utmi_ctrl(omap, val);
250		break;
251
252	case OMAP_DWC3_VBUS_OFF:
253		val = dwc3_omap_read_utmi_ctrl(omap);
254		val &= ~(USBOTGSS_UTMI_OTG_CTRL_SESSVALID
255				| USBOTGSS_UTMI_OTG_CTRL_VBUSVALID);
256		val |= USBOTGSS_UTMI_OTG_CTRL_SESSEND;
257		dwc3_omap_write_utmi_ctrl(omap, val);
258		break;
259
260	default:
261		dev_WARN(omap->dev, "invalid state\n");
262	}
263}
264
265static void dwc3_omap_enable_irqs(struct dwc3_omap *omap);
266static void dwc3_omap_disable_irqs(struct dwc3_omap *omap);
267
268static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
269{
270	struct dwc3_omap	*omap = _omap;
271
272	if (dwc3_omap_read_irqmisc_status(omap) ||
273	    dwc3_omap_read_irq0_status(omap)) {
274		/* mask irqs */
275		dwc3_omap_disable_irqs(omap);
276		return IRQ_WAKE_THREAD;
277	}
278
279	return IRQ_NONE;
280}
281
282static irqreturn_t dwc3_omap_interrupt_thread(int irq, void *_omap)
283{
284	struct dwc3_omap	*omap = _omap;
285	u32			reg;
286
287	/* clear irq status flags */
288	reg = dwc3_omap_read_irqmisc_status(omap);
289	dwc3_omap_write_irqmisc_status(omap, reg);
290
291	reg = dwc3_omap_read_irq0_status(omap);
292	dwc3_omap_write_irq0_status(omap, reg);
293
294	/* unmask irqs */
295	dwc3_omap_enable_irqs(omap);
296
297	return IRQ_HANDLED;
298}
299
300static void dwc3_omap_enable_irqs(struct dwc3_omap *omap)
301{
302	u32			reg;
303
304	/* enable all IRQs */
305	reg = USBOTGSS_IRQO_COREIRQ_ST;
306	dwc3_omap_write_irq0_set(omap, reg);
307
308	reg = (USBOTGSS_IRQMISC_OEVT |
309			USBOTGSS_IRQMISC_DRVVBUS_RISE |
310			USBOTGSS_IRQMISC_CHRGVBUS_RISE |
311			USBOTGSS_IRQMISC_DISCHRGVBUS_RISE |
312			USBOTGSS_IRQMISC_IDPULLUP_RISE |
313			USBOTGSS_IRQMISC_DRVVBUS_FALL |
314			USBOTGSS_IRQMISC_CHRGVBUS_FALL |
315			USBOTGSS_IRQMISC_DISCHRGVBUS_FALL |
316			USBOTGSS_IRQMISC_IDPULLUP_FALL);
317
318	dwc3_omap_write_irqmisc_set(omap, reg);
319}
320
321static void dwc3_omap_disable_irqs(struct dwc3_omap *omap)
322{
323	u32			reg;
324
325	/* disable all IRQs */
326	reg = USBOTGSS_IRQO_COREIRQ_ST;
327	dwc3_omap_write_irq0_clr(omap, reg);
328
329	reg = (USBOTGSS_IRQMISC_OEVT |
330			USBOTGSS_IRQMISC_DRVVBUS_RISE |
331			USBOTGSS_IRQMISC_CHRGVBUS_RISE |
332			USBOTGSS_IRQMISC_DISCHRGVBUS_RISE |
333			USBOTGSS_IRQMISC_IDPULLUP_RISE |
334			USBOTGSS_IRQMISC_DRVVBUS_FALL |
335			USBOTGSS_IRQMISC_CHRGVBUS_FALL |
336			USBOTGSS_IRQMISC_DISCHRGVBUS_FALL |
337			USBOTGSS_IRQMISC_IDPULLUP_FALL);
338
339	dwc3_omap_write_irqmisc_clr(omap, reg);
340}
 
 
341
342static int dwc3_omap_id_notifier(struct notifier_block *nb,
343	unsigned long event, void *ptr)
344{
345	struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, id_nb);
346
347	if (event)
348		dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
349	else
350		dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_FLOAT);
351
352	return NOTIFY_DONE;
353}
354
355static int dwc3_omap_vbus_notifier(struct notifier_block *nb,
356	unsigned long event, void *ptr)
357{
358	struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, vbus_nb);
359
360	if (event)
361		dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
362	else
363		dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_OFF);
364
365	return NOTIFY_DONE;
366}
367
368static void dwc3_omap_map_offset(struct dwc3_omap *omap)
369{
370	struct device_node	*node = omap->dev->of_node;
371
372	/*
373	 * Differentiate between OMAP5 and AM437x.
374	 *
375	 * For OMAP5(ES2.0) and AM437x wrapper revision is same, even
376	 * though there are changes in wrapper register offsets.
377	 *
378	 * Using dt compatible to differentiate AM437x.
379	 */
380	if (of_device_is_compatible(node, "ti,am437x-dwc3")) {
381		omap->irq_eoi_offset = USBOTGSS_EOI_OFFSET;
382		omap->irq0_offset = USBOTGSS_IRQ0_OFFSET;
383		omap->irqmisc_offset = USBOTGSS_IRQMISC_OFFSET;
384		omap->utmi_otg_offset = USBOTGSS_UTMI_OTG_OFFSET;
385		omap->debug_offset = USBOTGSS_DEBUG_OFFSET;
386	}
387}
388
389static void dwc3_omap_set_utmi_mode(struct dwc3_omap *omap)
390{
391	u32			reg;
392	struct device_node	*node = omap->dev->of_node;
393	u32			utmi_mode = 0;
394
395	reg = dwc3_omap_read_utmi_ctrl(omap);
396
397	of_property_read_u32(node, "utmi-mode", &utmi_mode);
398
399	switch (utmi_mode) {
400	case DWC3_OMAP_UTMI_MODE_SW:
401		reg |= USBOTGSS_UTMI_OTG_CTRL_SW_MODE;
402		break;
403	case DWC3_OMAP_UTMI_MODE_HW:
404		reg &= ~USBOTGSS_UTMI_OTG_CTRL_SW_MODE;
405		break;
406	default:
407		dev_WARN(omap->dev, "UNKNOWN utmi mode %d\n", utmi_mode);
408	}
409
410	dwc3_omap_write_utmi_ctrl(omap, reg);
411}
412
413static int dwc3_omap_extcon_register(struct dwc3_omap *omap)
414{
415	int			ret;
416	struct device_node	*node = omap->dev->of_node;
417	struct extcon_dev	*edev;
418
419	if (of_property_read_bool(node, "extcon")) {
420		edev = extcon_get_edev_by_phandle(omap->dev, 0);
421		if (IS_ERR(edev)) {
422			dev_vdbg(omap->dev, "couldn't get extcon device\n");
423			return -EPROBE_DEFER;
424		}
425
426		omap->vbus_nb.notifier_call = dwc3_omap_vbus_notifier;
427		ret = devm_extcon_register_notifier(omap->dev, edev,
428						EXTCON_USB, &omap->vbus_nb);
429		if (ret < 0)
430			dev_vdbg(omap->dev, "failed to register notifier for USB\n");
431
432		omap->id_nb.notifier_call = dwc3_omap_id_notifier;
433		ret = devm_extcon_register_notifier(omap->dev, edev,
434						EXTCON_USB_HOST, &omap->id_nb);
435		if (ret < 0)
436			dev_vdbg(omap->dev, "failed to register notifier for USB-HOST\n");
437
438		if (extcon_get_state(edev, EXTCON_USB) == true)
439			dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
440		if (extcon_get_state(edev, EXTCON_USB_HOST) == true)
441			dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
442
443		omap->edev = edev;
444	}
445
446	return 0;
447}
448
449static int dwc3_omap_probe(struct platform_device *pdev)
450{
 
451	struct device_node	*node = pdev->dev.of_node;
452
 
453	struct dwc3_omap	*omap;
 
454	struct device		*dev = &pdev->dev;
455	struct regulator	*vbus_reg = NULL;
456
457	int			ret;
 
 
458	int			irq;
459
 
460	u32			reg;
461
462	void __iomem		*base;
 
 
 
 
 
 
 
463
464	if (!node) {
465		dev_err(dev, "device node not found\n");
 
 
 
466		return -EINVAL;
467	}
468
469	omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
470	if (!omap)
 
 
 
 
 
 
 
471		return -ENOMEM;
 
472
473	platform_set_drvdata(pdev, omap);
 
 
 
 
 
 
 
 
474
475	irq = platform_get_irq(pdev, 0);
476	if (irq < 0)
477		return irq;
478
479	base = devm_platform_ioremap_resource(pdev, 0);
480	if (IS_ERR(base))
481		return PTR_ERR(base);
482
483	if (of_property_read_bool(node, "vbus-supply")) {
484		vbus_reg = devm_regulator_get(dev, "vbus");
485		if (IS_ERR(vbus_reg)) {
486			dev_err(dev, "vbus init failed\n");
487			return PTR_ERR(vbus_reg);
488		}
489	}
490
 
 
 
 
 
 
 
 
491	omap->dev	= dev;
492	omap->irq	= irq;
493	omap->base	= base;
494	omap->vbus_reg	= vbus_reg;
495
496	pm_runtime_enable(dev);
497	ret = pm_runtime_get_sync(dev);
498	if (ret < 0) {
499		dev_err(dev, "get_sync failed with err %d\n", ret);
500		goto err1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501	}
502
503	dwc3_omap_map_offset(omap);
504	dwc3_omap_set_utmi_mode(omap);
505
506	/* check the DMA Status */
507	reg = dwc3_omap_readl(omap->base, USBOTGSS_SYSCONFIG);
 
508
509	ret = dwc3_omap_extcon_register(omap);
510	if (ret < 0)
511		goto err1;
512
513	ret = of_platform_populate(node, NULL, NULL, dev);
514	if (ret) {
515		dev_err(&pdev->dev, "failed to create dwc3 core\n");
516		goto err1;
517	}
518
519	ret = devm_request_threaded_irq(dev, omap->irq, dwc3_omap_interrupt,
520					dwc3_omap_interrupt_thread, IRQF_SHARED,
521					"dwc3-omap", omap);
522	if (ret) {
523		dev_err(dev, "failed to request IRQ #%d --> %d\n",
524			omap->irq, ret);
525		goto err1;
526	}
527	dwc3_omap_enable_irqs(omap);
528	return 0;
529
530err1:
531	pm_runtime_put_sync(dev);
532	pm_runtime_disable(dev);
533
534	return ret;
535}
 
 
 
 
 
 
 
536
537static int dwc3_omap_remove(struct platform_device *pdev)
538{
539	struct dwc3_omap	*omap = platform_get_drvdata(pdev);
540
541	dwc3_omap_disable_irqs(omap);
542	disable_irq(omap->irq);
543	of_platform_depopulate(omap->dev);
544	pm_runtime_put_sync(&pdev->dev);
545	pm_runtime_disable(&pdev->dev);
 
546
547	return 0;
548}
 
 
 
549
550static const struct of_device_id of_dwc3_match[] = {
551	{
552		.compatible =	"ti,dwc3"
553	},
554	{
555		.compatible =	"ti,am437x-dwc3"
556	},
557	{ },
558};
559MODULE_DEVICE_TABLE(of, of_dwc3_match);
560
561#ifdef CONFIG_PM_SLEEP
562static int dwc3_omap_suspend(struct device *dev)
563{
564	struct dwc3_omap	*omap = dev_get_drvdata(dev);
565
566	omap->utmi_otg_ctrl = dwc3_omap_read_utmi_ctrl(omap);
567	dwc3_omap_disable_irqs(omap);
568
569	return 0;
570}
571
572static int dwc3_omap_resume(struct device *dev)
573{
574	struct dwc3_omap	*omap = dev_get_drvdata(dev);
575
576	dwc3_omap_write_utmi_ctrl(omap, omap->utmi_otg_ctrl);
577	dwc3_omap_enable_irqs(omap);
578
579	pm_runtime_disable(dev);
580	pm_runtime_set_active(dev);
581	pm_runtime_enable(dev);
582
583	return 0;
584}
585
586static void dwc3_omap_complete(struct device *dev)
587{
588	struct dwc3_omap	*omap = dev_get_drvdata(dev);
589
590	if (extcon_get_state(omap->edev, EXTCON_USB))
591		dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
592	else
593		dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_OFF);
594
595	if (extcon_get_state(omap->edev, EXTCON_USB_HOST))
596		dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
597	else
598		dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_FLOAT);
599}
600
601static const struct dev_pm_ops dwc3_omap_dev_pm_ops = {
602
603	SET_SYSTEM_SLEEP_PM_OPS(dwc3_omap_suspend, dwc3_omap_resume)
604	.complete = dwc3_omap_complete,
605};
606
607#define DEV_PM_OPS	(&dwc3_omap_dev_pm_ops)
608#else
609#define DEV_PM_OPS	NULL
610#endif /* CONFIG_PM_SLEEP */
611
612static struct platform_driver dwc3_omap_driver = {
613	.probe		= dwc3_omap_probe,
614	.remove		= dwc3_omap_remove,
615	.driver		= {
616		.name	= "omap-dwc3",
617		.of_match_table	= of_dwc3_match,
618		.pm	= DEV_PM_OPS,
619	},
620};
621
622module_platform_driver(dwc3_omap_driver);
623
624MODULE_ALIAS("platform:omap-dwc3");
625MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
626MODULE_LICENSE("GPL v2");
627MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");
v3.5.6
 
  1/**
  2 * dwc3-omap.c - OMAP Specific Glue layer
  3 *
  4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5 *
  6 * Authors: Felipe Balbi <balbi@ti.com>,
  7 *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8 *
  9 * Redistribution and use in source and binary forms, with or without
 10 * modification, are permitted provided that the following conditions
 11 * are met:
 12 * 1. Redistributions of source code must retain the above copyright
 13 *    notice, this list of conditions, and the following disclaimer,
 14 *    without modification.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 *    notice, this list of conditions and the following disclaimer in the
 17 *    documentation and/or other materials provided with the distribution.
 18 * 3. The names of the above-listed copyright holders may not be used
 19 *    to endorse or promote products derived from this software without
 20 *    specific prior written permission.
 21 *
 22 * ALTERNATIVELY, this software may be distributed under the terms of the
 23 * GNU General Public License ("GPL") version 2, as published by the Free
 24 * Software Foundation.
 25 *
 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 37 */
 38
 39#include <linux/module.h>
 40#include <linux/kernel.h>
 41#include <linux/slab.h>
 
 42#include <linux/interrupt.h>
 43#include <linux/spinlock.h>
 44#include <linux/platform_device.h>
 45#include <linux/platform_data/dwc3-omap.h>
 46#include <linux/dma-mapping.h>
 47#include <linux/ioport.h>
 48#include <linux/io.h>
 49#include <linux/of.h>
 
 
 
 50
 51#include "core.h"
 52
 53/*
 54 * All these registers belong to OMAP's Wrapper around the
 55 * DesignWare USB3 Core.
 56 */
 57
 58#define USBOTGSS_REVISION			0x0000
 59#define USBOTGSS_SYSCONFIG			0x0010
 60#define USBOTGSS_IRQ_EOI			0x0020
 
 61#define USBOTGSS_IRQSTATUS_RAW_0		0x0024
 62#define USBOTGSS_IRQSTATUS_0			0x0028
 63#define USBOTGSS_IRQENABLE_SET_0		0x002c
 64#define USBOTGSS_IRQENABLE_CLR_0		0x0030
 65#define USBOTGSS_IRQSTATUS_RAW_1		0x0034
 66#define USBOTGSS_IRQSTATUS_1			0x0038
 67#define USBOTGSS_IRQENABLE_SET_1		0x003c
 68#define USBOTGSS_IRQENABLE_CLR_1		0x0040
 69#define USBOTGSS_UTMI_OTG_CTRL			0x0080
 70#define USBOTGSS_UTMI_OTG_STATUS		0x0084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 71#define USBOTGSS_MMRAM_OFFSET			0x0100
 72#define USBOTGSS_FLADJ				0x0104
 73#define USBOTGSS_DEBUG_CFG			0x0108
 74#define USBOTGSS_DEBUG_DATA			0x010c
 
 
 75
 76/* SYSCONFIG REGISTER */
 77#define USBOTGSS_SYSCONFIG_DMADISABLE		(1 << 16)
 78#define USBOTGSS_SYSCONFIG_STANDBYMODE(x)	((x) << 4)
 79
 80#define USBOTGSS_STANDBYMODE_FORCE_STANDBY	0
 81#define USBOTGSS_STANDBYMODE_NO_STANDBY		1
 82#define USBOTGSS_STANDBYMODE_SMART_STANDBY	2
 83#define USBOTGSS_STANDBYMODE_SMART_WAKEUP	3
 84
 85#define USBOTGSS_STANDBYMODE_MASK		(0x03 << 4)
 86
 87#define USBOTGSS_SYSCONFIG_IDLEMODE(x)		((x) << 2)
 88
 89#define USBOTGSS_IDLEMODE_FORCE_IDLE		0
 90#define USBOTGSS_IDLEMODE_NO_IDLE		1
 91#define USBOTGSS_IDLEMODE_SMART_IDLE		2
 92#define USBOTGSS_IDLEMODE_SMART_WAKEUP		3
 93
 94#define USBOTGSS_IDLEMODE_MASK			(0x03 << 2)
 95
 96/* IRQ_EOI REGISTER */
 97#define USBOTGSS_IRQ_EOI_LINE_NUMBER		(1 << 0)
 98
 99/* IRQS0 BITS */
100#define USBOTGSS_IRQO_COREIRQ_ST		(1 << 0)
101
102/* IRQ1 BITS */
103#define USBOTGSS_IRQ1_DMADISABLECLR		(1 << 17)
104#define USBOTGSS_IRQ1_OEVT			(1 << 16)
105#define USBOTGSS_IRQ1_DRVVBUS_RISE		(1 << 13)
106#define USBOTGSS_IRQ1_CHRGVBUS_RISE		(1 << 12)
107#define USBOTGSS_IRQ1_DISCHRGVBUS_RISE		(1 << 11)
108#define USBOTGSS_IRQ1_IDPULLUP_RISE		(1 << 8)
109#define USBOTGSS_IRQ1_DRVVBUS_FALL		(1 << 5)
110#define USBOTGSS_IRQ1_CHRGVBUS_FALL		(1 << 4)
111#define USBOTGSS_IRQ1_DISCHRGVBUS_FALL		(1 << 3)
112#define USBOTGSS_IRQ1_IDPULLUP_FALL		(1 << 0)
 
 
 
 
 
 
113
114/* UTMI_OTG_CTRL REGISTER */
115#define USBOTGSS_UTMI_OTG_CTRL_DRVVBUS		(1 << 5)
116#define USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS		(1 << 4)
117#define USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS	(1 << 3)
118#define USBOTGSS_UTMI_OTG_CTRL_IDPULLUP		(1 << 0)
119
120/* UTMI_OTG_STATUS REGISTER */
121#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE	(1 << 31)
122#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT	(1 << 9)
123#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE (1 << 8)
124#define USBOTGSS_UTMI_OTG_STATUS_IDDIG		(1 << 4)
125#define USBOTGSS_UTMI_OTG_STATUS_SESSEND	(1 << 3)
126#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID	(1 << 2)
127#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID	(1 << 1)
128
129struct dwc3_omap {
130	/* device lock */
131	spinlock_t		lock;
132
133	struct platform_device	*dwc3;
134	struct device		*dev;
135
136	int			irq;
137	void __iomem		*base;
138
139	void			*context;
140	u32			resource_size;
 
 
 
 
 
 
 
 
 
 
 
141
142	u32			dma_status:1;
 
 
 
 
143};
144
145static inline u32 dwc3_omap_readl(void __iomem *base, u32 offset)
146{
147	return readl(base + offset);
148}
149
150static inline void dwc3_omap_writel(void __iomem *base, u32 offset, u32 value)
151{
152	writel(value, base + offset);
153}
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
156static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
157{
158	struct dwc3_omap	*omap = _omap;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159	u32			reg;
160
161	spin_lock(&omap->lock);
 
 
162
163	reg = dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_1);
 
 
 
 
 
 
 
 
164
165	if (reg & USBOTGSS_IRQ1_DMADISABLECLR) {
166		dev_dbg(omap->dev, "DMA Disable was Cleared\n");
167		omap->dma_status = false;
168	}
169
170	if (reg & USBOTGSS_IRQ1_OEVT)
171		dev_dbg(omap->dev, "OTG Event\n");
 
 
172
173	if (reg & USBOTGSS_IRQ1_DRVVBUS_RISE)
174		dev_dbg(omap->dev, "DRVVBUS Rise\n");
 
 
175
176	if (reg & USBOTGSS_IRQ1_CHRGVBUS_RISE)
177		dev_dbg(omap->dev, "CHRGVBUS Rise\n");
178
179	if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_RISE)
180		dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
 
 
181
182	if (reg & USBOTGSS_IRQ1_IDPULLUP_RISE)
183		dev_dbg(omap->dev, "IDPULLUP Rise\n");
 
 
184
185	if (reg & USBOTGSS_IRQ1_DRVVBUS_FALL)
186		dev_dbg(omap->dev, "DRVVBUS Fall\n");
187
188	if (reg & USBOTGSS_IRQ1_CHRGVBUS_FALL)
189		dev_dbg(omap->dev, "CHRGVBUS Fall\n");
 
190
191	if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_FALL)
192		dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
194	if (reg & USBOTGSS_IRQ1_IDPULLUP_FALL)
195		dev_dbg(omap->dev, "IDPULLUP Fall\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
197	dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_1, reg);
 
198
199	reg = dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_0);
200	dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_0, reg);
 
 
 
 
 
 
 
 
 
 
201
202	spin_unlock(&omap->lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
204	return IRQ_HANDLED;
205}
206
207static int __devinit dwc3_omap_probe(struct platform_device *pdev)
208{
209	struct dwc3_omap_data	*pdata = pdev->dev.platform_data;
210	struct device_node	*node = pdev->dev.of_node;
211
212	struct platform_device	*dwc3;
213	struct dwc3_omap	*omap;
214	struct resource		*res;
215	struct device		*dev = &pdev->dev;
 
216
217	int			devid;
218	int			size;
219	int			ret = -ENOMEM;
220	int			irq;
221
222	const u32		*utmi_mode;
223	u32			reg;
224
225	void __iomem		*base;
226	void			*context;
227
228	omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
229	if (!omap) {
230		dev_err(dev, "not enough memory\n");
231		return -ENOMEM;
232	}
233
234	platform_set_drvdata(pdev, omap);
235
236	irq = platform_get_irq(pdev, 1);
237	if (irq < 0) {
238		dev_err(dev, "missing IRQ resource\n");
239		return -EINVAL;
240	}
241
242	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
243	if (!res) {
244		dev_err(dev, "missing memory base resource\n");
245		return -EINVAL;
246	}
247
248	base = devm_ioremap_nocache(dev, res->start, resource_size(res));
249	if (!base) {
250		dev_err(dev, "ioremap failed\n");
251		return -ENOMEM;
252	}
253
254	devid = dwc3_get_device_id();
255	if (devid < 0)
256		return -ENODEV;
257
258	dwc3 = platform_device_alloc("dwc3", devid);
259	if (!dwc3) {
260		dev_err(dev, "couldn't allocate dwc3 device\n");
261		goto err1;
262	}
263
264	context = devm_kzalloc(dev, resource_size(res), GFP_KERNEL);
265	if (!context) {
266		dev_err(dev, "couldn't allocate dwc3 context memory\n");
267		goto err2;
 
 
 
 
 
 
 
 
 
 
268	}
269
270	spin_lock_init(&omap->lock);
271	dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
272
273	dwc3->dev.parent = dev;
274	dwc3->dev.dma_mask = dev->dma_mask;
275	dwc3->dev.dma_parms = dev->dma_parms;
276	omap->resource_size = resource_size(res);
277	omap->context	= context;
278	omap->dev	= dev;
279	omap->irq	= irq;
280	omap->base	= base;
281	omap->dwc3	= dwc3;
282
283	reg = dwc3_omap_readl(omap->base, USBOTGSS_UTMI_OTG_STATUS);
284
285	utmi_mode = of_get_property(node, "utmi-mode", &size);
286	if (utmi_mode && size == sizeof(*utmi_mode)) {
287		reg |= *utmi_mode;
288	} else {
289		if (!pdata) {
290			dev_dbg(dev, "missing platform data\n");
291		} else {
292			switch (pdata->utmi_mode) {
293			case DWC3_OMAP_UTMI_MODE_SW:
294				reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
295				break;
296			case DWC3_OMAP_UTMI_MODE_HW:
297				reg &= ~USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
298				break;
299			default:
300				dev_dbg(dev, "UNKNOWN utmi mode %d\n",
301						pdata->utmi_mode);
302			}
303		}
304	}
305
306	dwc3_omap_writel(omap->base, USBOTGSS_UTMI_OTG_STATUS, reg);
 
307
308	/* check the DMA Status */
309	reg = dwc3_omap_readl(omap->base, USBOTGSS_SYSCONFIG);
310	omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
311
312	/* Set No-Idle and No-Standby */
313	reg &= ~(USBOTGSS_STANDBYMODE_MASK
314			| USBOTGSS_IDLEMODE_MASK);
315
316	reg |= (USBOTGSS_SYSCONFIG_STANDBYMODE(USBOTGSS_STANDBYMODE_NO_STANDBY)
317		| USBOTGSS_SYSCONFIG_IDLEMODE(USBOTGSS_IDLEMODE_NO_IDLE));
318
319	dwc3_omap_writel(omap->base, USBOTGSS_SYSCONFIG, reg);
 
320
321	ret = devm_request_irq(dev, omap->irq, dwc3_omap_interrupt, 0,
322			"dwc3-omap", omap);
 
323	if (ret) {
324		dev_err(dev, "failed to request IRQ #%d --> %d\n",
325				omap->irq, ret);
326		goto err2;
327	}
 
 
328
329	/* enable all IRQs */
330	reg = USBOTGSS_IRQO_COREIRQ_ST;
331	dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_0, reg);
332
333	reg = (USBOTGSS_IRQ1_OEVT |
334			USBOTGSS_IRQ1_DRVVBUS_RISE |
335			USBOTGSS_IRQ1_CHRGVBUS_RISE |
336			USBOTGSS_IRQ1_DISCHRGVBUS_RISE |
337			USBOTGSS_IRQ1_IDPULLUP_RISE |
338			USBOTGSS_IRQ1_DRVVBUS_FALL |
339			USBOTGSS_IRQ1_CHRGVBUS_FALL |
340			USBOTGSS_IRQ1_DISCHRGVBUS_FALL |
341			USBOTGSS_IRQ1_IDPULLUP_FALL);
342
343	dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_1, reg);
 
 
344
345	ret = platform_device_add_resources(dwc3, pdev->resource,
346			pdev->num_resources);
347	if (ret) {
348		dev_err(dev, "couldn't add resources to dwc3 device\n");
349		goto err2;
350	}
351
352	ret = platform_device_add(dwc3);
353	if (ret) {
354		dev_err(dev, "failed to register dwc3 device\n");
355		goto err2;
356	}
357
358	return 0;
 
 
 
 
 
 
 
 
 
359
360err2:
361	platform_device_put(dwc3);
 
 
362
363err1:
364	dwc3_put_device_id(devid);
365
366	return ret;
367}
368
369static int __devexit dwc3_omap_remove(struct platform_device *pdev)
370{
371	struct dwc3_omap	*omap = platform_get_drvdata(pdev);
372
373	platform_device_unregister(omap->dwc3);
 
374
375	dwc3_put_device_id(omap->dwc3->id);
 
 
376
377	return 0;
378}
379
380static const struct of_device_id of_dwc3_matach[] = {
381	{
382		"ti,dwc3",
383	},
384	{ },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385};
386MODULE_DEVICE_TABLE(of, of_dwc3_matach);
 
 
 
 
387
388static struct platform_driver dwc3_omap_driver = {
389	.probe		= dwc3_omap_probe,
390	.remove		= __devexit_p(dwc3_omap_remove),
391	.driver		= {
392		.name	= "omap-dwc3",
393		.of_match_table	= of_dwc3_matach,
 
394	},
395};
396
397module_platform_driver(dwc3_omap_driver);
398
399MODULE_ALIAS("platform:omap-dwc3");
400MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
401MODULE_LICENSE("Dual BSD/GPL");
402MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");