Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / usb / host / ohci-s3c2410.c
1 /*
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
22 #include <asm/hardware.h>
23 #include <asm/mach-types.h>
24 #include <asm/hardware/clock.h>
25 #include <asm/arch/usb-control.h>
26
27 #define valid_port(idx) ((idx) == 1 || (idx) == 2)
28
29 /* clock device associated with the hcd */
30
31 static struct clk *clk;
32
33 /* forward definitions */
34
35 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
36
37 /* conversion functions */
38
39 struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
40 {
41         return hcd->self.controller->platform_data;
42 }
43
44 static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
45 {
46         struct s3c2410_hcd_info *info = dev->dev.platform_data;
47
48         dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
49         clk_enable(clk);
50
51         if (info != NULL) {
52                 info->hcd       = hcd;
53                 info->report_oc = s3c2410_hcd_oc;
54
55                 if (info->enable_oc != NULL) {
56                         (info->enable_oc)(info, 1);
57                 }
58         }
59 }
60
61 static void s3c2410_stop_hc(struct platform_device *dev)
62 {
63         struct s3c2410_hcd_info *info = dev->dev.platform_data;
64
65         dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
66
67         if (info != NULL) {
68                 info->report_oc = NULL;
69                 info->hcd       = NULL;
70
71                 if (info->enable_oc != NULL) {
72                         (info->enable_oc)(info, 0);
73                 }
74         }
75
76         clk_disable(clk);
77 }
78
79 /* ohci_s3c2410_hub_status_data
80  *
81  * update the status data from the hub with anything that
82  * has been detected by our system
83 */
84
85 static int
86 ohci_s3c2410_hub_status_data (struct usb_hcd *hcd, char *buf)
87 {
88         struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
89         struct s3c2410_hcd_port *port;
90         int orig;
91         int portno;
92
93         orig  = ohci_hub_status_data (hcd, buf);
94
95         if (info == NULL)
96                 return orig;
97
98         port = &info->port[0];
99
100         /* mark any changed port as changed */
101
102         for (portno = 0; portno < 2; port++, portno++) {
103                 if (port->oc_changed == 1 &&
104                     port->flags & S3C_HCDFLG_USED) {
105                         dev_dbg(hcd->self.controller,
106                                 "oc change on port %d\n", portno);
107
108                         if (orig < 1)
109                                 orig = 1;
110
111                         buf[0] |= 1<<(portno+1);
112                 }
113         }
114
115         return orig;
116 }
117
118 /* s3c2410_usb_set_power
119  *
120  * configure the power on a port, by calling the platform device
121  * routine registered with the platform device
122 */
123
124 static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
125                                   int port, int to)
126 {
127         if (info == NULL)
128                 return;
129
130         if (info->power_control != NULL) {
131                 info->port[port-1].power = to;
132                 (info->power_control)(port-1, to);
133         }
134 }
135
136 /* ohci_s3c2410_hub_control
137  *
138  * look at control requests to the hub, and see if we need
139  * to take any action or over-ride the results from the
140  * request.
141 */
142
143 static int ohci_s3c2410_hub_control (
144         struct usb_hcd  *hcd,
145         u16             typeReq,
146         u16             wValue,
147         u16             wIndex,
148         char            *buf,
149         u16             wLength)
150 {
151         struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
152         struct usb_hub_descriptor *desc;
153         int ret = -EINVAL;
154         u32 *data = (u32 *)buf;
155
156         dev_dbg(hcd->self.controller,
157                 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
158                 hcd, typeReq, wValue, wIndex, buf, wLength);
159
160         /* if we are only an humble host without any special capabilites
161          * process the request straight away and exit */
162
163         if (info == NULL) {
164                 ret = ohci_hub_control(hcd, typeReq, wValue,
165                                        wIndex, buf, wLength);
166                 goto out;
167         }
168
169         /* check the request to see if it needs handling */
170
171         switch (typeReq) {
172         case SetPortFeature:
173                 if (wValue == USB_PORT_FEAT_POWER) {
174                         dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
175                         s3c2410_usb_set_power(info, wIndex, 1);
176                         goto out;
177                 }
178                 break;
179
180         case ClearPortFeature:
181                 switch (wValue) {
182                 case USB_PORT_FEAT_C_OVER_CURRENT:
183                         dev_dbg(hcd->self.controller,
184                                 "ClearPortFeature: C_OVER_CURRENT\n");
185
186                         if (valid_port(wIndex)) {
187                                 info->port[wIndex-1].oc_changed = 0;
188                                 info->port[wIndex-1].oc_status = 0;
189                         }
190
191                         goto out;
192
193                 case USB_PORT_FEAT_OVER_CURRENT:
194                         dev_dbg(hcd->self.controller,
195                                 "ClearPortFeature: OVER_CURRENT\n");
196
197                         if (valid_port(wIndex)) {
198                                 info->port[wIndex-1].oc_status = 0;
199                         }
200
201                         goto out;
202
203                 case USB_PORT_FEAT_POWER:
204                         dev_dbg(hcd->self.controller,
205                                 "ClearPortFeature: POWER\n");
206
207                         if (valid_port(wIndex)) {
208                                 s3c2410_usb_set_power(info, wIndex, 0);
209                                 return 0;
210                         }
211                 }
212                 break;
213         }
214
215         ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
216         if (ret)
217                 goto out;
218
219         switch (typeReq) {
220         case GetHubDescriptor:
221
222                 /* update the hub's descriptor */
223
224                 desc = (struct usb_hub_descriptor *)buf;
225
226                 if (info->power_control == NULL)
227                         return ret;
228
229                 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
230                         desc->wHubCharacteristics);
231
232                 /* remove the old configurations for power-switching, and
233                  * over-current protection, and insert our new configuration
234                  */
235
236                 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
237                 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
238
239                 if (info->enable_oc) {
240                         desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
241                         desc->wHubCharacteristics |=  cpu_to_le16(0x0008|0x0001);
242                 }
243
244                 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
245                         desc->wHubCharacteristics);
246
247                 return ret;
248
249         case GetPortStatus:
250                 /* check port status */
251
252                 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
253
254                 if (valid_port(wIndex)) {
255                         if (info->port[wIndex-1].oc_changed) {
256                                 *data |= cpu_to_le32(RH_PS_OCIC);
257                         }
258
259                         if (info->port[wIndex-1].oc_status) {
260                                 *data |= cpu_to_le32(RH_PS_POCI);
261                         }
262                 }
263         }
264
265  out:
266         return ret;
267 }
268
269 /* s3c2410_hcd_oc
270  *
271  * handle an over-current report
272 */
273
274 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
275 {
276         struct s3c2410_hcd_port *port;
277         struct usb_hcd *hcd;
278         unsigned long flags;
279         int portno;
280
281         if (info == NULL)
282                 return;
283
284         port = &info->port[0];
285         hcd = info->hcd;
286
287         local_irq_save(flags);
288
289         for (portno = 0; portno < 2; port++, portno++) {
290                 if (port_oc & (1<<portno) &&
291                     port->flags & S3C_HCDFLG_USED) {
292                         port->oc_status = 1;
293                         port->oc_changed = 1;
294
295                         /* ok, once over-current is detected,
296                            the port needs to be powered down */
297                         s3c2410_usb_set_power(info, portno+1, 0);
298                 }
299         }
300
301         local_irq_restore(flags);
302 }
303
304 /* may be called without controller electrically present */
305 /* may be called with controller, bus, and devices active */
306
307 /*
308  * usb_hcd_s3c2410_remove - shutdown processing for HCD
309  * @dev: USB Host Controller being removed
310  * Context: !in_interrupt()
311  *
312  * Reverses the effect of usb_hcd_3c2410_probe(), first invoking
313  * the HCD's stop() method.  It is always called from a thread
314  * context, normally "rmmod", "apmd", or something similar.
315  *
316 */
317
318 void usb_hcd_s3c2410_remove (struct usb_hcd *hcd, struct platform_device *dev)
319 {
320         usb_remove_hcd(hcd);
321         s3c2410_stop_hc(dev);
322         iounmap(hcd->regs);
323         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
324         usb_put_hcd(hcd);
325 }
326
327 /**
328  * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs
329  * Context: !in_interrupt()
330  *
331  * Allocates basic resources for this USB host controller, and
332  * then invokes the start() method for the HCD associated with it
333  * through the hotplug entry's driver_data.
334  *
335  */
336 int usb_hcd_s3c2410_probe (const struct hc_driver *driver,
337                            struct platform_device *dev)
338 {
339         struct usb_hcd *hcd = NULL;
340         int retval;
341
342         s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
343         s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
344
345         hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
346         if (hcd == NULL)
347                 return -ENOMEM;
348
349         hcd->rsrc_start = dev->resource[0].start;
350         hcd->rsrc_len   = dev->resource[0].end - dev->resource[0].start + 1;
351
352         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
353                 dev_err(&dev->dev, "request_mem_region failed");
354                 retval = -EBUSY;
355                 goto err0;
356         }
357
358         clk = clk_get(NULL, "usb-host");
359         if (IS_ERR(clk)) {
360                 dev_err(&dev->dev, "cannot get usb-host clock\n");
361                 retval = -ENOENT;
362                 goto err1;
363         }
364
365         clk_use(clk);
366         s3c2410_start_hc(dev, hcd);
367
368         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
369         if (!hcd->regs) {
370                 dev_err(&dev->dev, "ioremap failed\n");
371                 retval = -ENOMEM;
372                 goto err2;
373         }
374
375         ohci_hcd_init(hcd_to_ohci(hcd));
376
377         retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT);
378         if (retval != 0)
379                 goto err2;
380
381         return 0;
382
383  err2:
384         s3c2410_stop_hc(dev);
385         iounmap(hcd->regs);
386         clk_unuse(clk);
387         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
399 static int
400 ohci_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
418 static 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,
452
453 #if defined(CONFIG_USB_SUSPEND) && 0
454         .hub_suspend =          ohci_hub_suspend,
455         .hub_resume =           ohci_hub_resume,
456 #endif
457 };
458
459 /* device driver */
460
461 static int ohci_hcd_s3c2410_drv_probe(struct device *dev)
462 {
463         struct platform_device *pdev = to_platform_device(dev);
464         return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
465 }
466
467 static int ohci_hcd_s3c2410_drv_remove(struct device *dev)
468 {
469         struct platform_device *pdev = to_platform_device(dev);
470         struct usb_hcd *hcd = dev_get_drvdata(dev);
471
472         usb_hcd_s3c2410_remove(hcd, pdev);
473         return 0;
474 }
475
476 static struct device_driver ohci_hcd_s3c2410_driver = {
477         .name           = "s3c2410-ohci",
478         .bus            = &platform_bus_type,
479         .probe          = ohci_hcd_s3c2410_drv_probe,
480         .remove         = ohci_hcd_s3c2410_drv_remove,
481         /*.suspend      = ohci_hcd_s3c2410_drv_suspend, */
482         /*.resume       = ohci_hcd_s3c2410_drv_resume, */
483 };
484
485 static int __init ohci_hcd_s3c2410_init (void)
486 {
487         return driver_register(&ohci_hcd_s3c2410_driver);
488 }
489
490 static void __exit ohci_hcd_s3c2410_cleanup (void)
491 {
492         driver_unregister(&ohci_hcd_s3c2410_driver);
493 }
494
495 module_init (ohci_hcd_s3c2410_init);
496 module_exit (ohci_hcd_s3c2410_cleanup);