blob: b27669fe9f0f3ccb37a51d73250798b3ceba0be5 [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>
11 * Based on fragments of previous driver by Rusell King et al.
12 *
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
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070025#include <asm/hardware.h>
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070026#include <asm/arch/usb-control.h>
27
28#define valid_port(idx) ((idx) == 1 || (idx) == 2)
29
30/* clock device associated with the hcd */
31
32static struct clk *clk;
33
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");
50 clk_enable(clk);
51
52 if (info != NULL) {
53 info->hcd = hcd;
54 info->report_oc = s3c2410_hcd_oc;
55
56 if (info->enable_oc != NULL) {
57 (info->enable_oc)(info, 1);
58 }
59 }
60}
61
62static void s3c2410_stop_hc(struct platform_device *dev)
63{
64 struct s3c2410_hcd_info *info = dev->dev.platform_data;
65
66 dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
67
68 if (info != NULL) {
69 info->report_oc = NULL;
70 info->hcd = NULL;
71
72 if (info->enable_oc != NULL) {
73 (info->enable_oc)(info, 0);
74 }
75 }
76
77 clk_disable(clk);
78}
79
80/* ohci_s3c2410_hub_status_data
81 *
82 * update the status data from the hub with anything that
83 * has been detected by our system
84*/
85
86static int
87ohci_s3c2410_hub_status_data (struct usb_hcd *hcd, char *buf)
88{
89 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
90 struct s3c2410_hcd_port *port;
91 int orig;
92 int portno;
93
94 orig = ohci_hub_status_data (hcd, buf);
95
96 if (info == NULL)
97 return orig;
98
99 port = &info->port[0];
100
101 /* mark any changed port as changed */
102
103 for (portno = 0; portno < 2; port++, portno++) {
104 if (port->oc_changed == 1 &&
105 port->flags & S3C_HCDFLG_USED) {
106 dev_dbg(hcd->self.controller,
107 "oc change on port %d\n", portno);
108
109 if (orig < 1)
110 orig = 1;
111
112 buf[0] |= 1<<(portno+1);
113 }
114 }
115
116 return orig;
117}
118
119/* s3c2410_usb_set_power
120 *
121 * configure the power on a port, by calling the platform device
122 * routine registered with the platform device
123*/
124
125static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
126 int port, int to)
127{
128 if (info == NULL)
129 return;
130
131 if (info->power_control != NULL) {
132 info->port[port-1].power = to;
Ben Dooksba44e7c2005-08-09 15:04:00 +0100133 (info->power_control)(port-1, to);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700134 }
135}
136
137/* ohci_s3c2410_hub_control
138 *
139 * look at control requests to the hub, and see if we need
140 * to take any action or over-ride the results from the
141 * request.
142*/
143
144static int ohci_s3c2410_hub_control (
145 struct usb_hcd *hcd,
146 u16 typeReq,
147 u16 wValue,
148 u16 wIndex,
149 char *buf,
150 u16 wLength)
151{
152 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
153 struct usb_hub_descriptor *desc;
154 int ret = -EINVAL;
155 u32 *data = (u32 *)buf;
156
157 dev_dbg(hcd->self.controller,
158 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
159 hcd, typeReq, wValue, wIndex, buf, wLength);
160
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800161 /* if we are only an humble host without any special capabilities
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700162 * process the request straight away and exit */
163
164 if (info == NULL) {
165 ret = ohci_hub_control(hcd, typeReq, wValue,
166 wIndex, buf, wLength);
167 goto out;
168 }
169
170 /* check the request to see if it needs handling */
171
172 switch (typeReq) {
173 case SetPortFeature:
174 if (wValue == USB_PORT_FEAT_POWER) {
175 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
176 s3c2410_usb_set_power(info, wIndex, 1);
177 goto out;
178 }
179 break;
180
181 case ClearPortFeature:
182 switch (wValue) {
183 case USB_PORT_FEAT_C_OVER_CURRENT:
184 dev_dbg(hcd->self.controller,
185 "ClearPortFeature: C_OVER_CURRENT\n");
186
187 if (valid_port(wIndex)) {
188 info->port[wIndex-1].oc_changed = 0;
189 info->port[wIndex-1].oc_status = 0;
190 }
191
192 goto out;
193
194 case USB_PORT_FEAT_OVER_CURRENT:
195 dev_dbg(hcd->self.controller,
196 "ClearPortFeature: OVER_CURRENT\n");
197
198 if (valid_port(wIndex)) {
199 info->port[wIndex-1].oc_status = 0;
200 }
201
202 goto out;
203
204 case USB_PORT_FEAT_POWER:
205 dev_dbg(hcd->self.controller,
206 "ClearPortFeature: POWER\n");
207
208 if (valid_port(wIndex)) {
209 s3c2410_usb_set_power(info, wIndex, 0);
210 return 0;
211 }
212 }
213 break;
214 }
215
216 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
217 if (ret)
218 goto out;
219
220 switch (typeReq) {
221 case GetHubDescriptor:
222
223 /* update the hub's descriptor */
224
225 desc = (struct usb_hub_descriptor *)buf;
226
227 if (info->power_control == NULL)
228 return ret;
229
230 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
231 desc->wHubCharacteristics);
232
233 /* remove the old configurations for power-switching, and
234 * over-current protection, and insert our new configuration
235 */
236
237 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
238 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
239
240 if (info->enable_oc) {
241 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
242 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
243 }
244
245 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
246 desc->wHubCharacteristics);
247
248 return ret;
249
250 case GetPortStatus:
251 /* check port status */
252
253 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
254
255 if (valid_port(wIndex)) {
256 if (info->port[wIndex-1].oc_changed) {
257 *data |= cpu_to_le32(RH_PS_OCIC);
258 }
259
260 if (info->port[wIndex-1].oc_status) {
261 *data |= cpu_to_le32(RH_PS_POCI);
262 }
263 }
264 }
265
266 out:
267 return ret;
268}
269
270/* s3c2410_hcd_oc
271 *
272 * handle an over-current report
273*/
274
275static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
276{
277 struct s3c2410_hcd_port *port;
278 struct usb_hcd *hcd;
279 unsigned long flags;
280 int portno;
281
282 if (info == NULL)
283 return;
284
285 port = &info->port[0];
286 hcd = info->hcd;
287
288 local_irq_save(flags);
289
290 for (portno = 0; portno < 2; port++, portno++) {
291 if (port_oc & (1<<portno) &&
292 port->flags & S3C_HCDFLG_USED) {
293 port->oc_status = 1;
294 port->oc_changed = 1;
295
296 /* ok, once over-current is detected,
297 the port needs to be powered down */
298 s3c2410_usb_set_power(info, portno+1, 0);
299 }
300 }
301
302 local_irq_restore(flags);
303}
304
305/* may be called without controller electrically present */
306/* may be called with controller, bus, and devices active */
307
308/*
309 * usb_hcd_s3c2410_remove - shutdown processing for HCD
310 * @dev: USB Host Controller being removed
311 * Context: !in_interrupt()
312 *
313 * Reverses the effect of usb_hcd_3c2410_probe(), first invoking
314 * the HCD's stop() method. It is always called from a thread
315 * context, normally "rmmod", "apmd", or something similar.
316 *
317*/
318
Ben Dooksf096e042006-03-21 22:54:47 +0000319static void
320usb_hcd_s3c2410_remove (struct usb_hcd *hcd, struct platform_device *dev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700321{
322 usb_remove_hcd(hcd);
323 s3c2410_stop_hc(dev);
324 iounmap(hcd->regs);
325 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
326 usb_put_hcd(hcd);
327}
328
329/**
330 * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs
331 * Context: !in_interrupt()
332 *
333 * Allocates basic resources for this USB host controller, and
334 * then invokes the start() method for the HCD associated with it
335 * through the hotplug entry's driver_data.
336 *
337 */
Ben Dooksf096e042006-03-21 22:54:47 +0000338static int usb_hcd_s3c2410_probe (const struct hc_driver *driver,
339 struct platform_device *dev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700340{
341 struct usb_hcd *hcd = NULL;
342 int retval;
343
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700344 s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
Ben Dooksba44e7c2005-08-09 15:04:00 +0100345 s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700346
347 hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
348 if (hcd == NULL)
349 return -ENOMEM;
350
351 hcd->rsrc_start = dev->resource[0].start;
352 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
353
354 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
355 dev_err(&dev->dev, "request_mem_region failed");
356 retval = -EBUSY;
357 goto err0;
358 }
359
360 clk = clk_get(NULL, "usb-host");
361 if (IS_ERR(clk)) {
362 dev_err(&dev->dev, "cannot get usb-host clock\n");
363 retval = -ENOENT;
364 goto err1;
365 }
366
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700367 s3c2410_start_hc(dev, hcd);
368
369 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
370 if (!hcd->regs) {
371 dev_err(&dev->dev, "ioremap failed\n");
372 retval = -ENOMEM;
373 goto err2;
374 }
375
376 ohci_hcd_init(hcd_to_ohci(hcd));
377
378 retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT);
379 if (retval != 0)
380 goto err2;
381
382 return 0;
383
384 err2:
385 s3c2410_stop_hc(dev);
386 iounmap(hcd->regs);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700387 clk_put(clk);
388
389 err1:
390 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
391
392 err0:
393 usb_put_hcd(hcd);
394 return retval;
395}
396
397/*-------------------------------------------------------------------------*/
398
399static int
400ohci_s3c2410_start (struct usb_hcd *hcd)
401{
402 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
403 int ret;
404
405 if ((ret = ohci_init(ohci)) < 0)
406 return ret;
407
408 if ((ret = ohci_run (ohci)) < 0) {
409 err ("can't start %s", hcd->self.bus_name);
410 ohci_stop (hcd);
411 return ret;
412 }
413
414 return 0;
415}
416
417
418static const struct hc_driver ohci_s3c2410_hc_driver = {
419 .description = hcd_name,
420 .product_desc = "S3C24XX OHCI",
421 .hcd_priv_size = sizeof(struct ohci_hcd),
422
423 /*
424 * generic hardware linkage
425 */
426 .irq = ohci_irq,
427 .flags = HCD_USB11 | HCD_MEMORY,
428
429 /*
430 * basic lifecycle operations
431 */
432 .start = ohci_s3c2410_start,
433 .stop = ohci_stop,
434
435 /*
436 * managing i/o requests and associated device resources
437 */
438 .urb_enqueue = ohci_urb_enqueue,
439 .urb_dequeue = ohci_urb_dequeue,
440 .endpoint_disable = ohci_endpoint_disable,
441
442 /*
443 * scheduling support
444 */
445 .get_frame_number = ohci_get_frame,
446
447 /*
448 * root hub support
449 */
450 .hub_status_data = ohci_s3c2410_hub_status_data,
451 .hub_control = ohci_s3c2410_hub_control,
David Brownell8ad7fe12005-09-13 19:59:11 -0700452#ifdef CONFIG_PM
Alan Stern0c0382e2005-10-13 17:08:02 -0400453 .bus_suspend = ohci_bus_suspend,
454 .bus_resume = ohci_bus_resume,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700455#endif
David Brownell92936772005-09-22 22:32:11 -0700456 .start_port_reset = ohci_start_port_reset,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700457};
458
459/* device driver */
460
Russell King3ae5eae2005-11-09 22:32:44 +0000461static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700462{
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700463 return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
464}
465
Russell King3ae5eae2005-11-09 22:32:44 +0000466static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700467{
Russell King3ae5eae2005-11-09 22:32:44 +0000468 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700469
470 usb_hcd_s3c2410_remove(hcd, pdev);
471 return 0;
472}
473
Russell King3ae5eae2005-11-09 22:32:44 +0000474static struct platform_driver ohci_hcd_s3c2410_driver = {
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700475 .probe = ohci_hcd_s3c2410_drv_probe,
476 .remove = ohci_hcd_s3c2410_drv_remove,
477 /*.suspend = ohci_hcd_s3c2410_drv_suspend, */
478 /*.resume = ohci_hcd_s3c2410_drv_resume, */
Russell King3ae5eae2005-11-09 22:32:44 +0000479 .driver = {
480 .owner = THIS_MODULE,
481 .name = "s3c2410-ohci",
482 },
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700483};
484
485static int __init ohci_hcd_s3c2410_init (void)
486{
Russell King3ae5eae2005-11-09 22:32:44 +0000487 return platform_driver_register(&ohci_hcd_s3c2410_driver);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700488}
489
490static void __exit ohci_hcd_s3c2410_cleanup (void)
491{
Russell King3ae5eae2005-11-09 22:32:44 +0000492 platform_driver_unregister(&ohci_hcd_s3c2410_driver);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700493}
494
495module_init (ohci_hcd_s3c2410_init);
496module_exit (ohci_hcd_s3c2410_cleanup);