blob: a7ddd5d14a5172c7bdc0c5013772e0f3efb6b2d3 [file] [log] [blame]
Ben Dooks3eb0c5f2005-07-29 12:18:03 -07001/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6 * (C) Copyright 2002 Hewlett-Packard Company
7 *
8 * USB Bus Glue for Samsung S3C2410
9 *
10 * Written by Christopher Hoover <ch@hpl.hp.com>
Uwe Kleine-Königac310bb2008-07-10 17:30:46 -070011 * Based on fragments of previous driver by Russell King et al.
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070012 *
13 * Modified for S3C2410 from ohci-sa1111.c, ohci-omap.c and ohci-lh7a40.c
14 * by Ben Dooks, <ben@simtec.co.uk>
15 * Copyright (C) 2004 Simtec Electronics
16 *
17 * Thanks to basprog@mail.ru for updates to newer kernels
18 *
19 * This file is licenced under the GPL.
20*/
21
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/usb-control.h>
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070026
27#define valid_port(idx) ((idx) == 1 || (idx) == 2)
28
29/* clock device associated with the hcd */
30
31static struct clk *clk;
Ben Dooks3799c402006-04-02 01:45:00 +010032static struct clk *usb_clk;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070033
34/* forward definitions */
35
36static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
37
38/* conversion functions */
39
Ben Dooksf096e042006-03-21 22:54:47 +000040static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070041{
42 return hcd->self.controller->platform_data;
43}
44
45static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
46{
47 struct s3c2410_hcd_info *info = dev->dev.platform_data;
48
49 dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
Ben Dooks3799c402006-04-02 01:45:00 +010050
51 clk_enable(usb_clk);
52 mdelay(2); /* let the bus clock stabilise */
53
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070054 clk_enable(clk);
55
56 if (info != NULL) {
57 info->hcd = hcd;
58 info->report_oc = s3c2410_hcd_oc;
59
60 if (info->enable_oc != NULL) {
61 (info->enable_oc)(info, 1);
62 }
63 }
64}
65
66static void s3c2410_stop_hc(struct platform_device *dev)
67{
68 struct s3c2410_hcd_info *info = dev->dev.platform_data;
69
70 dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
71
72 if (info != NULL) {
73 info->report_oc = NULL;
74 info->hcd = NULL;
75
76 if (info->enable_oc != NULL) {
77 (info->enable_oc)(info, 0);
78 }
79 }
80
81 clk_disable(clk);
Ben Dooks3799c402006-04-02 01:45:00 +010082 clk_disable(usb_clk);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070083}
84
85/* ohci_s3c2410_hub_status_data
86 *
87 * update the status data from the hub with anything that
88 * has been detected by our system
89*/
90
91static int
92ohci_s3c2410_hub_status_data (struct usb_hcd *hcd, char *buf)
93{
94 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
95 struct s3c2410_hcd_port *port;
96 int orig;
97 int portno;
98
99 orig = ohci_hub_status_data (hcd, buf);
100
101 if (info == NULL)
102 return orig;
103
104 port = &info->port[0];
105
106 /* mark any changed port as changed */
107
108 for (portno = 0; portno < 2; port++, portno++) {
109 if (port->oc_changed == 1 &&
110 port->flags & S3C_HCDFLG_USED) {
111 dev_dbg(hcd->self.controller,
112 "oc change on port %d\n", portno);
113
114 if (orig < 1)
115 orig = 1;
116
117 buf[0] |= 1<<(portno+1);
118 }
119 }
120
121 return orig;
122}
123
124/* s3c2410_usb_set_power
125 *
126 * configure the power on a port, by calling the platform device
127 * routine registered with the platform device
128*/
129
130static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
131 int port, int to)
132{
133 if (info == NULL)
134 return;
135
136 if (info->power_control != NULL) {
137 info->port[port-1].power = to;
Ben Dooksba44e7c2005-08-09 15:04:00 +0100138 (info->power_control)(port-1, to);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700139 }
140}
141
142/* ohci_s3c2410_hub_control
143 *
144 * look at control requests to the hub, and see if we need
145 * to take any action or over-ride the results from the
146 * request.
147*/
148
149static int ohci_s3c2410_hub_control (
150 struct usb_hcd *hcd,
151 u16 typeReq,
152 u16 wValue,
153 u16 wIndex,
154 char *buf,
155 u16 wLength)
156{
157 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
158 struct usb_hub_descriptor *desc;
159 int ret = -EINVAL;
160 u32 *data = (u32 *)buf;
161
162 dev_dbg(hcd->self.controller,
163 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
164 hcd, typeReq, wValue, wIndex, buf, wLength);
165
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800166 /* if we are only an humble host without any special capabilities
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700167 * process the request straight away and exit */
168
169 if (info == NULL) {
170 ret = ohci_hub_control(hcd, typeReq, wValue,
171 wIndex, buf, wLength);
172 goto out;
173 }
174
175 /* check the request to see if it needs handling */
176
177 switch (typeReq) {
178 case SetPortFeature:
179 if (wValue == USB_PORT_FEAT_POWER) {
180 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
181 s3c2410_usb_set_power(info, wIndex, 1);
182 goto out;
183 }
184 break;
185
186 case ClearPortFeature:
187 switch (wValue) {
188 case USB_PORT_FEAT_C_OVER_CURRENT:
189 dev_dbg(hcd->self.controller,
190 "ClearPortFeature: C_OVER_CURRENT\n");
191
192 if (valid_port(wIndex)) {
193 info->port[wIndex-1].oc_changed = 0;
194 info->port[wIndex-1].oc_status = 0;
195 }
196
197 goto out;
198
199 case USB_PORT_FEAT_OVER_CURRENT:
200 dev_dbg(hcd->self.controller,
201 "ClearPortFeature: OVER_CURRENT\n");
202
203 if (valid_port(wIndex)) {
204 info->port[wIndex-1].oc_status = 0;
205 }
206
207 goto out;
208
209 case USB_PORT_FEAT_POWER:
210 dev_dbg(hcd->self.controller,
211 "ClearPortFeature: POWER\n");
212
213 if (valid_port(wIndex)) {
214 s3c2410_usb_set_power(info, wIndex, 0);
215 return 0;
216 }
217 }
218 break;
219 }
220
221 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
222 if (ret)
223 goto out;
224
225 switch (typeReq) {
226 case GetHubDescriptor:
227
228 /* update the hub's descriptor */
229
230 desc = (struct usb_hub_descriptor *)buf;
231
232 if (info->power_control == NULL)
233 return ret;
234
235 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
236 desc->wHubCharacteristics);
237
238 /* remove the old configurations for power-switching, and
239 * over-current protection, and insert our new configuration
240 */
241
242 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
243 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
244
245 if (info->enable_oc) {
246 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
247 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
248 }
249
250 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
251 desc->wHubCharacteristics);
252
253 return ret;
254
255 case GetPortStatus:
256 /* check port status */
257
258 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
259
260 if (valid_port(wIndex)) {
261 if (info->port[wIndex-1].oc_changed) {
262 *data |= cpu_to_le32(RH_PS_OCIC);
263 }
264
265 if (info->port[wIndex-1].oc_status) {
266 *data |= cpu_to_le32(RH_PS_POCI);
267 }
268 }
269 }
270
271 out:
272 return ret;
273}
274
275/* s3c2410_hcd_oc
276 *
277 * handle an over-current report
278*/
279
280static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
281{
282 struct s3c2410_hcd_port *port;
283 struct usb_hcd *hcd;
284 unsigned long flags;
285 int portno;
286
287 if (info == NULL)
288 return;
289
290 port = &info->port[0];
291 hcd = info->hcd;
292
293 local_irq_save(flags);
294
295 for (portno = 0; portno < 2; port++, portno++) {
296 if (port_oc & (1<<portno) &&
297 port->flags & S3C_HCDFLG_USED) {
298 port->oc_status = 1;
299 port->oc_changed = 1;
300
301 /* ok, once over-current is detected,
302 the port needs to be powered down */
303 s3c2410_usb_set_power(info, portno+1, 0);
304 }
305 }
306
307 local_irq_restore(flags);
308}
309
310/* may be called without controller electrically present */
311/* may be called with controller, bus, and devices active */
312
313/*
314 * usb_hcd_s3c2410_remove - shutdown processing for HCD
315 * @dev: USB Host Controller being removed
316 * Context: !in_interrupt()
317 *
318 * Reverses the effect of usb_hcd_3c2410_probe(), first invoking
319 * the HCD's stop() method. It is always called from a thread
320 * context, normally "rmmod", "apmd", or something similar.
321 *
322*/
323
Ben Dooksf096e042006-03-21 22:54:47 +0000324static void
325usb_hcd_s3c2410_remove (struct usb_hcd *hcd, struct platform_device *dev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700326{
327 usb_remove_hcd(hcd);
328 s3c2410_stop_hc(dev);
329 iounmap(hcd->regs);
330 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
331 usb_put_hcd(hcd);
332}
333
334/**
335 * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs
336 * Context: !in_interrupt()
337 *
338 * Allocates basic resources for this USB host controller, and
339 * then invokes the start() method for the HCD associated with it
340 * through the hotplug entry's driver_data.
341 *
342 */
Ben Dooksf096e042006-03-21 22:54:47 +0000343static int usb_hcd_s3c2410_probe (const struct hc_driver *driver,
344 struct platform_device *dev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700345{
346 struct usb_hcd *hcd = NULL;
347 int retval;
348
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700349 s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
Ben Dooksba44e7c2005-08-09 15:04:00 +0100350 s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700351
352 hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
353 if (hcd == NULL)
354 return -ENOMEM;
355
356 hcd->rsrc_start = dev->resource[0].start;
357 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
358
359 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
Joe Perches898eb712007-10-18 03:06:30 -0700360 dev_err(&dev->dev, "request_mem_region failed\n");
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700361 retval = -EBUSY;
Ben Dooks3799c402006-04-02 01:45:00 +0100362 goto err_put;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700363 }
364
Ben Dooks3799c402006-04-02 01:45:00 +0100365 clk = clk_get(&dev->dev, "usb-host");
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700366 if (IS_ERR(clk)) {
367 dev_err(&dev->dev, "cannot get usb-host clock\n");
368 retval = -ENOENT;
Ben Dooks3799c402006-04-02 01:45:00 +0100369 goto err_mem;
370 }
371
Ben Dooksb2a8e092006-06-26 23:36:07 +0100372 usb_clk = clk_get(&dev->dev, "usb-bus-host");
Ben Dooks3799c402006-04-02 01:45:00 +0100373 if (IS_ERR(usb_clk)) {
Ben Dooks2dfa3192009-02-26 23:03:15 +0000374 dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
Ben Dooks3799c402006-04-02 01:45:00 +0100375 retval = -ENOENT;
376 goto err_clk;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700377 }
378
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700379 s3c2410_start_hc(dev, hcd);
380
381 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
382 if (!hcd->regs) {
383 dev_err(&dev->dev, "ioremap failed\n");
384 retval = -ENOMEM;
Ben Dooks3799c402006-04-02 01:45:00 +0100385 goto err_ioremap;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700386 }
387
388 ohci_hcd_init(hcd_to_ohci(hcd));
389
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -0700390 retval = usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700391 if (retval != 0)
Ben Dooks3799c402006-04-02 01:45:00 +0100392 goto err_ioremap;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700393
394 return 0;
395
Ben Dooks3799c402006-04-02 01:45:00 +0100396 err_ioremap:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700397 s3c2410_stop_hc(dev);
398 iounmap(hcd->regs);
Ben Dooks3799c402006-04-02 01:45:00 +0100399 clk_put(usb_clk);
400
401 err_clk:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700402 clk_put(clk);
403
Ben Dooks3799c402006-04-02 01:45:00 +0100404 err_mem:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700405 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
406
Ben Dooks3799c402006-04-02 01:45:00 +0100407 err_put:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700408 usb_put_hcd(hcd);
409 return retval;
410}
411
412/*-------------------------------------------------------------------------*/
413
414static int
415ohci_s3c2410_start (struct usb_hcd *hcd)
416{
417 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
418 int ret;
419
420 if ((ret = ohci_init(ohci)) < 0)
421 return ret;
422
423 if ((ret = ohci_run (ohci)) < 0) {
424 err ("can't start %s", hcd->self.bus_name);
425 ohci_stop (hcd);
426 return ret;
427 }
428
429 return 0;
430}
431
432
433static const struct hc_driver ohci_s3c2410_hc_driver = {
434 .description = hcd_name,
435 .product_desc = "S3C24XX OHCI",
436 .hcd_priv_size = sizeof(struct ohci_hcd),
437
438 /*
439 * generic hardware linkage
440 */
441 .irq = ohci_irq,
442 .flags = HCD_USB11 | HCD_MEMORY,
443
444 /*
445 * basic lifecycle operations
446 */
447 .start = ohci_s3c2410_start,
448 .stop = ohci_stop,
David Brownelldd9048a2006-12-05 03:18:31 -0800449 .shutdown = ohci_shutdown,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700450
451 /*
452 * managing i/o requests and associated device resources
453 */
454 .urb_enqueue = ohci_urb_enqueue,
455 .urb_dequeue = ohci_urb_dequeue,
456 .endpoint_disable = ohci_endpoint_disable,
457
458 /*
459 * scheduling support
460 */
461 .get_frame_number = ohci_get_frame,
462
463 /*
464 * root hub support
465 */
466 .hub_status_data = ohci_s3c2410_hub_status_data,
467 .hub_control = ohci_s3c2410_hub_control,
David Brownell8ad7fe12005-09-13 19:59:11 -0700468#ifdef CONFIG_PM
Alan Stern0c0382e2005-10-13 17:08:02 -0400469 .bus_suspend = ohci_bus_suspend,
470 .bus_resume = ohci_bus_resume,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700471#endif
David Brownell92936772005-09-22 22:32:11 -0700472 .start_port_reset = ohci_start_port_reset,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700473};
474
475/* device driver */
476
Russell King3ae5eae2005-11-09 22:32:44 +0000477static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700478{
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700479 return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
480}
481
Russell King3ae5eae2005-11-09 22:32:44 +0000482static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700483{
Russell King3ae5eae2005-11-09 22:32:44 +0000484 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700485
486 usb_hcd_s3c2410_remove(hcd, pdev);
487 return 0;
488}
489
Russell King3ae5eae2005-11-09 22:32:44 +0000490static struct platform_driver ohci_hcd_s3c2410_driver = {
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700491 .probe = ohci_hcd_s3c2410_drv_probe,
492 .remove = ohci_hcd_s3c2410_drv_remove,
David Brownelldd9048a2006-12-05 03:18:31 -0800493 .shutdown = usb_hcd_platform_shutdown,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700494 /*.suspend = ohci_hcd_s3c2410_drv_suspend, */
495 /*.resume = ohci_hcd_s3c2410_drv_resume, */
Russell King3ae5eae2005-11-09 22:32:44 +0000496 .driver = {
497 .owner = THIS_MODULE,
498 .name = "s3c2410-ohci",
499 },
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700500};
501
Kay Sieversf4fce612008-04-10 21:29:22 -0700502MODULE_ALIAS("platform:s3c2410-ohci");