summaryrefslogtreecommitdiff
path: root/drivers/usb/host/ehci-pxa168.c
blob: ac0c16e8f539b9c5d1b9004166d1dc0ed7cce40b (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * drivers/usb/host/ehci-pxa168.c
 *
 * Tanmay Upadhyay <tanmay.upadhyay@einfochips.com>
 *
 * Based on drivers/usb/host/ehci-orion.c
 *
 * This file is licensed under  the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <mach/pxa168.h>

#define USB_PHY_CTRL_REG 0x4
#define USB_PHY_PLL_REG  0x8
#define USB_PHY_TX_REG   0xc

#define FBDIV_SHIFT      4

#define ICP_SHIFT        12
#define ICP_15            2
#define ICP_20            3
#define ICP_25            4

#define KVCO_SHIFT       15

#define PLLCALI12_SHIFT  25
#define CALI12_VDD        0
#define CALI12_09         1
#define CALI12_10         2
#define CALI12_11         3

#define PLLVDD12_SHIFT   27
#define VDD12_VDD         0
#define VDD12_10          1
#define VDD12_11          2
#define VDD12_12          3

#define PLLVDD18_SHIFT   29
#define VDD18_19          0
#define VDD18_20          1
#define VDD18_21          2
#define VDD18_22          3


#define PLL_READY        (1 << 23)
#define VCOCAL_START     (1 << 21)
#define REG_RCAL_START   (1 << 12)

struct pxa168_usb_drv_data {
	struct ehci_hcd ehci;
	struct clk	*pxa168_usb_clk;
	struct resource	*usb_phy_res;
	void __iomem	*usb_phy_reg_base;
};

static int ehci_pxa168_setup(struct usb_hcd *hcd)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	int retval;

	ehci_reset(ehci);
	retval = ehci_halt(ehci);
	if (retval)
		return retval;

	/*
	 * data structure init
	 */
	retval = ehci_init(hcd);
	if (retval)
		return retval;

	hcd->has_tt = 1;

	ehci_port_power(ehci, 0);

	return retval;
}

static const struct hc_driver ehci_pxa168_hc_driver = {
	.description = hcd_name,
	.product_desc = "Marvell PXA168 EHCI",
	.hcd_priv_size = sizeof(struct pxa168_usb_drv_data),

	/*
	 * generic hardware linkage
	 */
	.irq = ehci_irq,
	.flags = HCD_MEMORY | HCD_USB2,

	/*
	 * basic lifecycle operations
	 */
	.reset = ehci_pxa168_setup,
	.start = ehci_run,
	.stop = ehci_stop,
	.shutdown = ehci_shutdown,

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue = ehci_urb_enqueue,
	.urb_dequeue = ehci_urb_dequeue,
	.endpoint_disable = ehci_endpoint_disable,
	.endpoint_reset = ehci_endpoint_reset,

	/*
	 * scheduling support
	 */
	.get_frame_number = ehci_get_frame,

	/*
	 * root hub support
	 */
	.hub_status_data = ehci_hub_status_data,
	.hub_control = ehci_hub_control,
	.bus_suspend = ehci_bus_suspend,
	.bus_resume = ehci_bus_resume,
	.relinquish_port = ehci_relinquish_port,
	.port_handed_over = ehci_port_handed_over,

	.clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
};

static int pxa168_usb_phy_init(struct platform_device *pdev)
{
	struct resource *res;
	void __iomem *usb_phy_reg_base;
	struct pxa168_usb_pdata *pdata;
	struct pxa168_usb_drv_data *drv_data;
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	unsigned long reg_val;
	int pll_retry_cont = 10000, err = 0;

	drv_data = (struct pxa168_usb_drv_data *)hcd->hcd_priv;
	pdata = (struct pxa168_usb_pdata *)pdev->dev.platform_data;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (!res) {
		dev_err(&pdev->dev,
			"Found HC with no PHY register addr. Check %s setup!\n",
			dev_name(&pdev->dev));
		return -ENODEV;
	}

	if (!request_mem_region(res->start, resource_size(res),
				ehci_pxa168_hc_driver.description)) {
		dev_dbg(&pdev->dev, "controller already in use\n");
		return -EBUSY;
	}

	usb_phy_reg_base = ioremap(res->start, resource_size(res));
	if (usb_phy_reg_base == NULL) {
		dev_dbg(&pdev->dev, "error mapping memory\n");
		err = -EFAULT;
		goto err1;
	}
	drv_data->usb_phy_reg_base = usb_phy_reg_base;
	drv_data->usb_phy_res = res;

	/* If someone wants to init USB phy in board specific way */
	if (pdata && pdata->phy_init)
		return pdata->phy_init(usb_phy_reg_base);

	/* Power up the PHY and PLL */
	writel(readl(usb_phy_reg_base + USB_PHY_CTRL_REG) | 0x3,
		usb_phy_reg_base + USB_PHY_CTRL_REG);

	/* Configure PHY PLL */
	reg_val = readl(usb_phy_reg_base + USB_PHY_PLL_REG) & ~(0x7e03ffff);
	reg_val |= (VDD18_22 << PLLVDD18_SHIFT | VDD12_12 << PLLVDD12_SHIFT |
		CALI12_11 << PLLCALI12_SHIFT | 3 << KVCO_SHIFT |
		ICP_15 << ICP_SHIFT | 0xee << FBDIV_SHIFT | 0xb);
	writel(reg_val, usb_phy_reg_base + USB_PHY_PLL_REG);

	/* Make sure PHY PLL is ready */
	while (!(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & PLL_READY)) {
		if (!(pll_retry_cont--)) {
			dev_dbg(&pdev->dev, "USB PHY PLL not ready\n");
			err = -EIO;
			goto err2;
		}
	}

	/* Toggle VCOCAL_START bit of U2PLL for PLL calibration */
	udelay(200);
	writel(readl(usb_phy_reg_base + USB_PHY_PLL_REG) | VCOCAL_START,
		usb_phy_reg_base + USB_PHY_PLL_REG);
	udelay(40);
	writel(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & ~VCOCAL_START,
		usb_phy_reg_base + USB_PHY_PLL_REG);

	/* Toggle REG_RCAL_START bit of U2PTX for impedance calibration */
	udelay(400);
	writel(readl(usb_phy_reg_base + USB_PHY_TX_REG) | REG_RCAL_START,
		usb_phy_reg_base + USB_PHY_TX_REG);
	udelay(40);
	writel(readl(usb_phy_reg_base + USB_PHY_TX_REG) & ~REG_RCAL_START,
		usb_phy_reg_base + USB_PHY_TX_REG);

	/* Make sure PHY PLL is ready again */
	pll_retry_cont = 0;
	while (!(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & PLL_READY)) {
		if (!(pll_retry_cont--)) {
			dev_dbg(&pdev->dev, "USB PHY PLL not ready\n");
			err = -EIO;
			goto err2;
		}
	}

	return 0;
err2:
	iounmap(usb_phy_reg_base);
err1:
	release_mem_region(res->start, resource_size(res));
	return err;
}

static int __devinit ehci_pxa168_drv_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct usb_hcd *hcd;
	struct ehci_hcd *ehci;
	struct pxa168_usb_drv_data *drv_data;
	void __iomem *regs;
	int irq, err = 0;

	if (usb_disabled())
		return -ENODEV;

	pr_debug("Initializing pxa168-SoC USB Host Controller\n");

	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
		dev_err(&pdev->dev,
			"Found HC with no IRQ. Check %s setup!\n",
			dev_name(&pdev->dev));
		err = -ENODEV;
		goto err1;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev,
			"Found HC with no register addr. Check %s setup!\n",
			dev_name(&pdev->dev));
		err = -ENODEV;
		goto err1;
	}

	if (!request_mem_region(res->start, resource_size(res),
				ehci_pxa168_hc_driver.description)) {
		dev_dbg(&pdev->dev, "controller already in use\n");
		err = -EBUSY;
		goto err1;
	}

	regs = ioremap(res->start, resource_size(res));
	if (regs == NULL) {
		dev_dbg(&pdev->dev, "error mapping memory\n");
		err = -EFAULT;
		goto err2;
	}

	hcd = usb_create_hcd(&ehci_pxa168_hc_driver,
			&pdev->dev, dev_name(&pdev->dev));
	if (!hcd) {
		err = -ENOMEM;
		goto err3;
	}

	drv_data = (struct pxa168_usb_drv_data *)hcd->hcd_priv;

	/* Enable USB clock */
	drv_data->pxa168_usb_clk = clk_get(&pdev->dev, "PXA168-USBCLK");
	if (IS_ERR(drv_data->pxa168_usb_clk)) {
		dev_err(&pdev->dev, "Couldn't get USB clock\n");
		err = PTR_ERR(drv_data->pxa168_usb_clk);
		goto err4;
	}
	clk_enable(drv_data->pxa168_usb_clk);

	err = pxa168_usb_phy_init(pdev);
	if (err) {
		dev_err(&pdev->dev, "USB PHY initialization failed\n");
		goto err5;
	}

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

	ehci = hcd_to_ehci(hcd);
	ehci->caps = hcd->regs + 0x100;
	ehci->regs = hcd->regs + 0x100 +
		HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
	ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
	hcd->has_tt = 1;
	ehci->sbrn = 0x20;

	err = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
	if (err)
		goto err5;

	return 0;

err5:
	clk_disable(drv_data->pxa168_usb_clk);
	clk_put(drv_data->pxa168_usb_clk);
err4:
	usb_put_hcd(hcd);
err3:
	iounmap(regs);
err2:
	release_mem_region(res->start, resource_size(res));
err1:
	dev_err(&pdev->dev, "init %s fail, %d\n",
		dev_name(&pdev->dev), err);

	return err;
}

static int __exit ehci_pxa168_drv_remove(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct pxa168_usb_drv_data *drv_data =
		(struct pxa168_usb_drv_data *)hcd->hcd_priv;

	usb_remove_hcd(hcd);

	/* Power down PHY & PLL */
	writel(readl(drv_data->usb_phy_reg_base + USB_PHY_CTRL_REG) & (~0x3),
		drv_data->usb_phy_reg_base + USB_PHY_CTRL_REG);

	clk_disable(drv_data->pxa168_usb_clk);
	clk_put(drv_data->pxa168_usb_clk);

	iounmap(hcd->regs);
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);

	iounmap(drv_data->usb_phy_reg_base);
	release_mem_region(drv_data->usb_phy_res->start,
			resource_size(drv_data->usb_phy_res));

	usb_put_hcd(hcd);

	return 0;
}

MODULE_ALIAS("platform:pxa168-ehci");

static struct platform_driver ehci_pxa168_driver = {
	.probe		= ehci_pxa168_drv_probe,
	.remove		= __exit_p(ehci_pxa168_drv_remove),
	.shutdown	= usb_hcd_platform_shutdown,
	.driver.name	= "pxa168-ehci",
};