]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/host/ohci-s3c2410.c
USB: ohci-s3c2410: fix name of bus clock
[linux-2.6-omap-h63xx.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 Russell 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 <mach/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 static struct clk *usb_clk;
33
34 /* forward definitions */
35
36 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
37
38 /* conversion functions */
39
40 static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
41 {
42         return hcd->self.controller->platform_data;
43 }
44
45 static 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
51         clk_enable(usb_clk);
52         mdelay(2);                      /* let the bus clock stabilise */
53
54         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
66 static 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);
82         clk_disable(usb_clk);
83 }
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
91 static int
92 ohci_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
130 static 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;
138                 (info->power_control)(port-1, to);
139         }
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
149 static 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
166         /* if we are only an humble host without any special capabilities
167          * 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
280 static 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
324 static void
325 usb_hcd_s3c2410_remove (struct usb_hcd *hcd, struct platform_device *dev)
326 {
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  */
343 static int usb_hcd_s3c2410_probe (const struct hc_driver *driver,
344                                   struct platform_device *dev)
345 {
346         struct usb_hcd *hcd = NULL;
347         int retval;
348
349         s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
350         s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
351
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)) {
360                 dev_err(&dev->dev, "request_mem_region failed\n");
361                 retval = -EBUSY;
362                 goto err_put;
363         }
364
365         clk = clk_get(&dev->dev, "usb-host");
366         if (IS_ERR(clk)) {
367                 dev_err(&dev->dev, "cannot get usb-host clock\n");
368                 retval = -ENOENT;
369                 goto err_mem;
370         }
371
372         usb_clk = clk_get(&dev->dev, "usb-bus-host");
373         if (IS_ERR(usb_clk)) {
374                 dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
375                 retval = -ENOENT;
376                 goto err_clk;
377         }
378
379         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;
385                 goto err_ioremap;
386         }
387
388         ohci_hcd_init(hcd_to_ohci(hcd));
389
390         retval = usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED);
391         if (retval != 0)
392                 goto err_ioremap;
393
394         return 0;
395
396  err_ioremap:
397         s3c2410_stop_hc(dev);
398         iounmap(hcd->regs);
399         clk_put(usb_clk);
400
401  err_clk:
402         clk_put(clk);
403
404  err_mem:
405         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
406
407  err_put:
408         usb_put_hcd(hcd);
409         return retval;
410 }
411
412 /*-------------------------------------------------------------------------*/
413
414 static int
415 ohci_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
433 static 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,
449         .shutdown =             ohci_shutdown,
450
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,
468 #ifdef  CONFIG_PM
469         .bus_suspend =          ohci_bus_suspend,
470         .bus_resume =           ohci_bus_resume,
471 #endif
472         .start_port_reset =     ohci_start_port_reset,
473 };
474
475 /* device driver */
476
477 static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
478 {
479         return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
480 }
481
482 static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
483 {
484         struct usb_hcd *hcd = platform_get_drvdata(pdev);
485
486         usb_hcd_s3c2410_remove(hcd, pdev);
487         return 0;
488 }
489
490 static struct platform_driver ohci_hcd_s3c2410_driver = {
491         .probe          = ohci_hcd_s3c2410_drv_probe,
492         .remove         = ohci_hcd_s3c2410_drv_remove,
493         .shutdown       = usb_hcd_platform_shutdown,
494         /*.suspend      = ohci_hcd_s3c2410_drv_suspend, */
495         /*.resume       = ohci_hcd_s3c2410_drv_resume, */
496         .driver         = {
497                 .owner  = THIS_MODULE,
498                 .name   = "s3c2410-ohci",
499         },
500 };
501
502 MODULE_ALIAS("platform:s3c2410-ohci");