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