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