aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/gemini/files/drivers/usb/host/ehci-fotg2.c
blob: 0717abce9c48ab6f8a5c2ba516fa227d1260efbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 *  Gemini EHCI Host Controller driver
 *
 *  Copyright (C) 2014 Roman Yeryomin <roman@advem.lv>
 *  Copyright (C) 2012 Tobias Waldvogel
 *  based on GPLd code from Sony Computer Entertainment Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 */
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/usb/ehci_pdriver.h>

#include <mach/hardware.h>
#include <mach/global_reg.h>

#include "ehci.h"

#define DRV_NAME			"ehci-fotg2"

#define HCD_MISC			0x40

#define OTGC_SCR			0x80
#define OTGC_INT_STS			0x84
#define OTGC_INT_EN			0x88

#define GLOBAL_ISR			0xC0
#define GLOBAL_ICR			0xC4

#define GLOBAL_INT_POLARITY		(1 << 3)
#define GLOBAL_INT_MASK_HC		(1 << 2)
#define GLOBAL_INT_MASK_OTG		(1 << 1)
#define GLOBAL_INT_MASK_DEV		(1 << 0)

#define OTGC_SCR_ID			(1 << 21)
#define OTGC_SCR_CROLE			(1 << 20)
#define OTGC_SCR_VBUS_VLD		(1 << 19)
#define OTGC_SCR_A_SRP_RESP_TYPE	(1 << 8)
#define OTGC_SCR_A_SRP_DET_EN		(1 << 7)
#define OTGC_SCR_A_SET_B_HNP_EN		(1 << 6)
#define OTGC_SCR_A_BUS_DROP		(1 << 5)
#define OTGC_SCR_A_BUS_REQ		(1 << 4)

#define OTGC_INT_APLGRMV		(1 << 12)
#define OTGC_INT_BPLGRMV		(1 << 11)
#define OTGC_INT_OVC			(1 << 10)
#define OTGC_INT_IDCHG			(1 << 9)
#define OTGC_INT_RLCHG			(1 << 8)
#define OTGC_INT_AVBUSERR		(1 << 5)
#define OTGC_INT_ASRPDET		(1 << 4)
#define OTGC_INT_BSRPDN			(1 << 0)

#define OTGC_INT_A_TYPE	(		\
 		OTGC_INT_ASRPDET |	\
		OTGC_INT_AVBUSERR |	\
		OTGC_INT_OVC |		\
		OTGC_INT_RLCHG |	\
		OTGC_INT_IDCHG |	\
		OTGC_INT_APLGRMV	\
	)
#define OTGC_INT_B_TYPE	(		\
		OTGC_INT_AVBUSERR |	\
		OTGC_INT_OVC |		\
		OTGC_INT_RLCHG |	\
		OTGC_INT_IDCHG		\
	)


static void fotg2_otg_init(struct usb_hcd *hcd)
{
	u32 val;

	writel(GLOBAL_INT_POLARITY | GLOBAL_INT_MASK_HC |
	       GLOBAL_INT_MASK_OTG | GLOBAL_INT_MASK_DEV,
		hcd->regs + GLOBAL_ICR);

	val = readl(hcd->regs + OTGC_SCR);
	val &= ~(OTGC_SCR_A_SRP_RESP_TYPE | OTGC_SCR_A_SRP_DET_EN |
		 OTGC_SCR_A_BUS_DROP      | OTGC_SCR_A_SET_B_HNP_EN);
	val |= OTGC_SCR_A_BUS_REQ;
	writel(val, hcd->regs + OTGC_SCR);

	writel(OTGC_INT_A_TYPE, hcd->regs + OTGC_INT_EN);

	/* setup MISC register, fixes timing problems */
	val = readl(hcd->regs + HCD_MISC);
	val |= 0xD;
	writel(val, hcd->regs + HCD_MISC);

	writel(~0, hcd->regs + GLOBAL_ISR);
	writel(~0, hcd->regs + OTGC_INT_STS);
}

static int fotg2_ehci_reset(struct usb_hcd *hcd)
{
	int retval;

	retval = ehci_setup(hcd);
	if (retval)
		return retval;

	writel(GLOBAL_INT_POLARITY, hcd->regs + GLOBAL_ICR);
	return 0;
}

static const struct hc_driver fotg2_ehci_hc_driver = {
	.description		= hcd_name,
	.product_desc		= "FOTG2 EHCI Host Controller",
	.hcd_priv_size		= sizeof(struct ehci_hcd),
	.irq			= ehci_irq,
	.flags			= HCD_MEMORY | HCD_USB2,
	.reset			= fotg2_ehci_reset,
	.start			= ehci_run,
	.stop			= ehci_stop,
	.shutdown		= ehci_shutdown,
	.urb_enqueue		= ehci_urb_enqueue,
	.urb_dequeue		= ehci_urb_dequeue,
	.endpoint_disable	= ehci_endpoint_disable,
	.endpoint_reset		= ehci_endpoint_reset,
	.get_frame_number	= ehci_get_frame,
	.hub_status_data	= ehci_hub_status_data,
	.hub_control		= ehci_hub_control,
#if defined(CONFIG_PM)
	.bus_suspend		= ehci_bus_suspend,
	.bus_resume		= ehci_bus_resume,
#endif
	.relinquish_port	= ehci_relinquish_port,
	.port_handed_over	= ehci_port_handed_over,

	.clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
};

static irqreturn_t fotg2_ehci_irq(int irq, void *data)
{
	struct usb_hcd *hcd = data;
	u32 icr, sts;
	irqreturn_t retval;

	icr = readl(hcd->regs + GLOBAL_ICR);
	writel(GLOBAL_INT_POLARITY | GLOBAL_INT_MASK_HC |
	       GLOBAL_INT_MASK_OTG | GLOBAL_INT_MASK_DEV,
		hcd->regs + GLOBAL_ICR);

	retval = IRQ_NONE;

	sts = ~icr;
	sts &= GLOBAL_INT_MASK_HC | GLOBAL_INT_MASK_OTG | GLOBAL_INT_MASK_DEV;
	sts &= readl(hcd->regs + GLOBAL_ISR);
	writel(sts, hcd->regs + GLOBAL_ISR);

	if (unlikely(sts & GLOBAL_INT_MASK_DEV)) {
		ehci_warn(hcd_to_ehci(hcd),
			"Received unexpected irq for device role\n");
		retval = IRQ_HANDLED;
	}

	if (unlikely(sts & GLOBAL_INT_MASK_OTG)) {
		u32	otg_sts;

		otg_sts = readl(hcd->regs + OTGC_INT_STS);
		writel(otg_sts, hcd->regs + OTGC_INT_STS);

		ehci_warn(hcd_to_ehci(hcd),
			"Received unexpected irq for OTG management\n");
		retval = IRQ_HANDLED;
	}

	if (sts & GLOBAL_INT_MASK_HC) {
		retval = IRQ_NONE;
	}

	writel(icr, hcd->regs + GLOBAL_ICR);
	return retval;
}

static int fotg2_ehci_probe(struct platform_device *pdev)
{
	struct usb_hcd *hcd;
	struct resource *res;
	int irq , err;

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		pr_err("no irq provided");
		return irq;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		pr_err("no memory resource provided");
		return -ENXIO;
	}

	hcd = usb_create_hcd(&fotg2_ehci_hc_driver, &pdev->dev,
			     dev_name(&pdev->dev));
	if (!hcd)
		return -ENOMEM;

	hcd->rsrc_start = res->start;
	hcd->rsrc_len = resource_size(res);

	hcd->regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(hcd->regs)) {
		err = -ENOMEM;
		goto err_put_hcd;
	}

	hcd->has_tt = 1;
	hcd_to_ehci(hcd)->caps = hcd->regs;

	fotg2_otg_init(hcd);

	err = request_irq(irq, &fotg2_ehci_irq, IRQF_SHARED, "fotg2", hcd);
	if (err)
		goto err_put_hcd;

	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
	if (err)
		goto err_put_hcd;

	platform_set_drvdata(pdev, hcd);
	return 0;

err_put_hcd:
	usb_put_hcd(hcd);
	return err;
}

static int fotg2_ehci_remove(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);

	writel(GLOBAL_INT_POLARITY | GLOBAL_INT_MASK_HC |
	       GLOBAL_INT_MASK_OTG | GLOBAL_INT_MASK_DEV,
		hcd->regs + GLOBAL_ICR);

	free_irq(hcd->irq, hcd);
	usb_remove_hcd(hcd);
	usb_put_hcd(hcd);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

MODULE_ALIAS("platform:" DRV_NAME);

static struct platform_driver ehci_fotg2_driver = {
	.probe		= fotg2_ehci_probe,
	.remove		= fotg2_ehci_remove,
	.driver.name	= DRV_NAME,
};