Commit | Line | Data |
---|---|---|
3eb0c5f4 BD |
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> | |
ac310bb5 | 11 | * Based on fragments of previous driver by Russell King et al. |
3eb0c5f4 BD |
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 | ||
d052d1be | 22 | #include <linux/platform_device.h> |
f8ce2547 | 23 | #include <linux/clk.h> |
436d42c6 | 24 | #include <linux/platform_data/usb-ohci-s3c2410.h> |
3eb0c5f4 BD |
25 | |
26 | #define valid_port(idx) ((idx) == 1 || (idx) == 2) | |
27 | ||
28 | /* clock device associated with the hcd */ | |
29 | ||
30 | static struct clk *clk; | |
3799c401 | 31 | static struct clk *usb_clk; |
3eb0c5f4 BD |
32 | |
33 | /* forward definitions */ | |
34 | ||
35 | static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc); | |
36 | ||
37 | /* conversion functions */ | |
38 | ||
f096e043 | 39 | static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd) |
3eb0c5f4 BD |
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"); | |
3799c401 BD |
49 | |
50 | clk_enable(usb_clk); | |
51 | mdelay(2); /* let the bus clock stabilise */ | |
52 | ||
3eb0c5f4 BD |
53 | clk_enable(clk); |
54 | ||
55 | if (info != NULL) { | |
56 | info->hcd = hcd; | |
57 | info->report_oc = s3c2410_hcd_oc; | |
58 | ||
7af85a85 | 59 | if (info->enable_oc != NULL) |
3eb0c5f4 | 60 | (info->enable_oc)(info, 1); |
3eb0c5f4 BD |
61 | } |
62 | } | |
63 | ||
64 | static void s3c2410_stop_hc(struct platform_device *dev) | |
65 | { | |
66 | struct s3c2410_hcd_info *info = dev->dev.platform_data; | |
67 | ||
68 | dev_dbg(&dev->dev, "s3c2410_stop_hc:\n"); | |
69 | ||
70 | if (info != NULL) { | |
71 | info->report_oc = NULL; | |
72 | info->hcd = NULL; | |
73 | ||
7af85a85 | 74 | if (info->enable_oc != NULL) |
3eb0c5f4 | 75 | (info->enable_oc)(info, 0); |
3eb0c5f4 BD |
76 | } |
77 | ||
78 | clk_disable(clk); | |
3799c401 | 79 | clk_disable(usb_clk); |
3eb0c5f4 BD |
80 | } |
81 | ||
82 | /* ohci_s3c2410_hub_status_data | |
83 | * | |
84 | * update the status data from the hub with anything that | |
85 | * has been detected by our system | |
86 | */ | |
87 | ||
88 | static int | |
7af85a85 | 89 | ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf) |
3eb0c5f4 BD |
90 | { |
91 | struct s3c2410_hcd_info *info = to_s3c2410_info(hcd); | |
92 | struct s3c2410_hcd_port *port; | |
93 | int orig; | |
94 | int portno; | |
95 | ||
7af85a85 | 96 | orig = ohci_hub_status_data(hcd, buf); |
3eb0c5f4 BD |
97 | |
98 | if (info == NULL) | |
99 | return orig; | |
100 | ||
101 | port = &info->port[0]; | |
102 | ||
103 | /* mark any changed port as changed */ | |
104 | ||
105 | for (portno = 0; portno < 2; port++, portno++) { | |
106 | if (port->oc_changed == 1 && | |
107 | port->flags & S3C_HCDFLG_USED) { | |
108 | dev_dbg(hcd->self.controller, | |
109 | "oc change on port %d\n", portno); | |
110 | ||
111 | if (orig < 1) | |
112 | orig = 1; | |
113 | ||
114 | buf[0] |= 1<<(portno+1); | |
115 | } | |
116 | } | |
117 | ||
118 | return orig; | |
119 | } | |
120 | ||
121 | /* s3c2410_usb_set_power | |
122 | * | |
123 | * configure the power on a port, by calling the platform device | |
124 | * routine registered with the platform device | |
125 | */ | |
126 | ||
127 | static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info, | |
128 | int port, int to) | |
129 | { | |
130 | if (info == NULL) | |
131 | return; | |
132 | ||
133 | if (info->power_control != NULL) { | |
134 | info->port[port-1].power = to; | |
ba44e7c4 | 135 | (info->power_control)(port-1, to); |
3eb0c5f4 BD |
136 | } |
137 | } | |
138 | ||
139 | /* ohci_s3c2410_hub_control | |
140 | * | |
141 | * look at control requests to the hub, and see if we need | |
142 | * to take any action or over-ride the results from the | |
143 | * request. | |
144 | */ | |
145 | ||
7af85a85 | 146 | static int ohci_s3c2410_hub_control( |
3eb0c5f4 BD |
147 | struct usb_hcd *hcd, |
148 | u16 typeReq, | |
149 | u16 wValue, | |
150 | u16 wIndex, | |
151 | char *buf, | |
152 | u16 wLength) | |
153 | { | |
154 | struct s3c2410_hcd_info *info = to_s3c2410_info(hcd); | |
155 | struct usb_hub_descriptor *desc; | |
156 | int ret = -EINVAL; | |
157 | u32 *data = (u32 *)buf; | |
158 | ||
159 | dev_dbg(hcd->self.controller, | |
160 | "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n", | |
161 | hcd, typeReq, wValue, wIndex, buf, wLength); | |
162 | ||
7f927fcc | 163 | /* if we are only an humble host without any special capabilities |
3eb0c5f4 BD |
164 | * process the request straight away and exit */ |
165 | ||
166 | if (info == NULL) { | |
167 | ret = ohci_hub_control(hcd, typeReq, wValue, | |
168 | wIndex, buf, wLength); | |
169 | goto out; | |
170 | } | |
171 | ||
172 | /* check the request to see if it needs handling */ | |
173 | ||
174 | switch (typeReq) { | |
175 | case SetPortFeature: | |
176 | if (wValue == USB_PORT_FEAT_POWER) { | |
177 | dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n"); | |
178 | s3c2410_usb_set_power(info, wIndex, 1); | |
179 | goto out; | |
180 | } | |
181 | break; | |
182 | ||
183 | case ClearPortFeature: | |
184 | switch (wValue) { | |
185 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
186 | dev_dbg(hcd->self.controller, | |
187 | "ClearPortFeature: C_OVER_CURRENT\n"); | |
188 | ||
189 | if (valid_port(wIndex)) { | |
190 | info->port[wIndex-1].oc_changed = 0; | |
191 | info->port[wIndex-1].oc_status = 0; | |
192 | } | |
193 | ||
194 | goto out; | |
195 | ||
196 | case USB_PORT_FEAT_OVER_CURRENT: | |
197 | dev_dbg(hcd->self.controller, | |
198 | "ClearPortFeature: OVER_CURRENT\n"); | |
199 | ||
7af85a85 | 200 | if (valid_port(wIndex)) |
3eb0c5f4 | 201 | info->port[wIndex-1].oc_status = 0; |
3eb0c5f4 BD |
202 | |
203 | goto out; | |
204 | ||
205 | case USB_PORT_FEAT_POWER: | |
206 | dev_dbg(hcd->self.controller, | |
207 | "ClearPortFeature: POWER\n"); | |
208 | ||
209 | if (valid_port(wIndex)) { | |
210 | s3c2410_usb_set_power(info, wIndex, 0); | |
211 | return 0; | |
212 | } | |
213 | } | |
214 | break; | |
215 | } | |
216 | ||
217 | ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength); | |
218 | if (ret) | |
219 | goto out; | |
220 | ||
221 | switch (typeReq) { | |
222 | case GetHubDescriptor: | |
223 | ||
224 | /* update the hub's descriptor */ | |
225 | ||
226 | desc = (struct usb_hub_descriptor *)buf; | |
227 | ||
228 | if (info->power_control == NULL) | |
229 | return ret; | |
230 | ||
231 | dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n", | |
232 | desc->wHubCharacteristics); | |
233 | ||
234 | /* remove the old configurations for power-switching, and | |
235 | * over-current protection, and insert our new configuration | |
236 | */ | |
237 | ||
238 | desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM); | |
239 | desc->wHubCharacteristics |= cpu_to_le16(0x0001); | |
240 | ||
241 | if (info->enable_oc) { | |
7af85a85 JH |
242 | desc->wHubCharacteristics &= ~cpu_to_le16( |
243 | HUB_CHAR_OCPM); | |
244 | desc->wHubCharacteristics |= cpu_to_le16( | |
245 | 0x0008 | | |
246 | 0x0001); | |
3eb0c5f4 BD |
247 | } |
248 | ||
249 | dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n", | |
250 | desc->wHubCharacteristics); | |
251 | ||
252 | return ret; | |
253 | ||
254 | case GetPortStatus: | |
255 | /* check port status */ | |
256 | ||
257 | dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex); | |
258 | ||
259 | if (valid_port(wIndex)) { | |
7af85a85 | 260 | if (info->port[wIndex-1].oc_changed) |
3eb0c5f4 | 261 | *data |= cpu_to_le32(RH_PS_OCIC); |
3eb0c5f4 | 262 | |
7af85a85 | 263 | if (info->port[wIndex-1].oc_status) |
3eb0c5f4 | 264 | *data |= cpu_to_le32(RH_PS_POCI); |
3eb0c5f4 BD |
265 | } |
266 | } | |
267 | ||
268 | out: | |
269 | return ret; | |
270 | } | |
271 | ||
272 | /* s3c2410_hcd_oc | |
273 | * | |
274 | * handle an over-current report | |
275 | */ | |
276 | ||
277 | static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc) | |
278 | { | |
279 | struct s3c2410_hcd_port *port; | |
280 | struct usb_hcd *hcd; | |
281 | unsigned long flags; | |
282 | int portno; | |
283 | ||
284 | if (info == NULL) | |
285 | return; | |
286 | ||
287 | port = &info->port[0]; | |
288 | hcd = info->hcd; | |
289 | ||
290 | local_irq_save(flags); | |
291 | ||
292 | for (portno = 0; portno < 2; port++, portno++) { | |
293 | if (port_oc & (1<<portno) && | |
294 | port->flags & S3C_HCDFLG_USED) { | |
295 | port->oc_status = 1; | |
296 | port->oc_changed = 1; | |
297 | ||
298 | /* ok, once over-current is detected, | |
299 | the port needs to be powered down */ | |
300 | s3c2410_usb_set_power(info, portno+1, 0); | |
301 | } | |
302 | } | |
303 | ||
304 | local_irq_restore(flags); | |
305 | } | |
306 | ||
307 | /* may be called without controller electrically present */ | |
308 | /* may be called with controller, bus, and devices active */ | |
309 | ||
310 | /* | |
311 | * usb_hcd_s3c2410_remove - shutdown processing for HCD | |
312 | * @dev: USB Host Controller being removed | |
313 | * Context: !in_interrupt() | |
314 | * | |
315 | * Reverses the effect of usb_hcd_3c2410_probe(), first invoking | |
316 | * the HCD's stop() method. It is always called from a thread | |
317 | * context, normally "rmmod", "apmd", or something similar. | |
318 | * | |
319 | */ | |
320 | ||
f096e043 | 321 | static void |
7af85a85 | 322 | usb_hcd_s3c2410_remove(struct usb_hcd *hcd, struct platform_device *dev) |
3eb0c5f4 BD |
323 | { |
324 | usb_remove_hcd(hcd); | |
325 | s3c2410_stop_hc(dev); | |
326 | iounmap(hcd->regs); | |
327 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); | |
328 | usb_put_hcd(hcd); | |
329 | } | |
330 | ||
331 | /** | |
332 | * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs | |
333 | * Context: !in_interrupt() | |
334 | * | |
335 | * Allocates basic resources for this USB host controller, and | |
336 | * then invokes the start() method for the HCD associated with it | |
337 | * through the hotplug entry's driver_data. | |
338 | * | |
339 | */ | |
7af85a85 | 340 | static int usb_hcd_s3c2410_probe(const struct hc_driver *driver, |
f096e043 | 341 | struct platform_device *dev) |
3eb0c5f4 BD |
342 | { |
343 | struct usb_hcd *hcd = NULL; | |
344 | int retval; | |
345 | ||
3eb0c5f4 | 346 | s3c2410_usb_set_power(dev->dev.platform_data, 1, 1); |
ba44e7c4 | 347 | s3c2410_usb_set_power(dev->dev.platform_data, 2, 1); |
3eb0c5f4 BD |
348 | |
349 | hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx"); | |
350 | if (hcd == NULL) | |
351 | return -ENOMEM; | |
352 | ||
353 | hcd->rsrc_start = dev->resource[0].start; | |
3c86c07b | 354 | hcd->rsrc_len = resource_size(&dev->resource[0]); |
3eb0c5f4 BD |
355 | |
356 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { | |
898eb71c | 357 | dev_err(&dev->dev, "request_mem_region failed\n"); |
3eb0c5f4 | 358 | retval = -EBUSY; |
3799c401 | 359 | goto err_put; |
3eb0c5f4 BD |
360 | } |
361 | ||
3799c401 | 362 | clk = clk_get(&dev->dev, "usb-host"); |
3eb0c5f4 BD |
363 | if (IS_ERR(clk)) { |
364 | dev_err(&dev->dev, "cannot get usb-host clock\n"); | |
69248d42 | 365 | retval = PTR_ERR(clk); |
3799c401 BD |
366 | goto err_mem; |
367 | } | |
368 | ||
b2a8e097 | 369 | usb_clk = clk_get(&dev->dev, "usb-bus-host"); |
3799c401 | 370 | if (IS_ERR(usb_clk)) { |
2dfa319a | 371 | dev_err(&dev->dev, "cannot get usb-bus-host clock\n"); |
69248d42 | 372 | retval = PTR_ERR(usb_clk); |
3799c401 | 373 | goto err_clk; |
3eb0c5f4 BD |
374 | } |
375 | ||
3eb0c5f4 BD |
376 | s3c2410_start_hc(dev, hcd); |
377 | ||
378 | hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); | |
379 | if (!hcd->regs) { | |
380 | dev_err(&dev->dev, "ioremap failed\n"); | |
381 | retval = -ENOMEM; | |
3799c401 | 382 | goto err_ioremap; |
3eb0c5f4 BD |
383 | } |
384 | ||
385 | ohci_hcd_init(hcd_to_ohci(hcd)); | |
386 | ||
b5dd18d8 | 387 | retval = usb_add_hcd(hcd, dev->resource[1].start, 0); |
3eb0c5f4 | 388 | if (retval != 0) |
3799c401 | 389 | goto err_ioremap; |
3eb0c5f4 BD |
390 | |
391 | return 0; | |
392 | ||
3799c401 | 393 | err_ioremap: |
3eb0c5f4 BD |
394 | s3c2410_stop_hc(dev); |
395 | iounmap(hcd->regs); | |
3799c401 BD |
396 | clk_put(usb_clk); |
397 | ||
398 | err_clk: | |
3eb0c5f4 BD |
399 | clk_put(clk); |
400 | ||
3799c401 | 401 | err_mem: |
3eb0c5f4 BD |
402 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
403 | ||
3799c401 | 404 | err_put: |
3eb0c5f4 BD |
405 | usb_put_hcd(hcd); |
406 | return retval; | |
407 | } | |
408 | ||
409 | /*-------------------------------------------------------------------------*/ | |
410 | ||
411 | static int | |
7af85a85 | 412 | ohci_s3c2410_start(struct usb_hcd *hcd) |
3eb0c5f4 | 413 | { |
7af85a85 | 414 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
3eb0c5f4 BD |
415 | int ret; |
416 | ||
7af85a85 JH |
417 | ret = ohci_init(ohci); |
418 | if (ret < 0) | |
3eb0c5f4 BD |
419 | return ret; |
420 | ||
7af85a85 JH |
421 | ret = ohci_run(ohci); |
422 | if (ret < 0) { | |
1041ac11 GKH |
423 | dev_err(hcd->self.controller, "can't start %s\n", |
424 | hcd->self.bus_name); | |
7af85a85 | 425 | ohci_stop(hcd); |
3eb0c5f4 BD |
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, | |
dd9048af | 449 | .shutdown = ohci_shutdown, |
3eb0c5f4 BD |
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, | |
8ad7fe16 | 468 | #ifdef CONFIG_PM |
0c0382e3 AS |
469 | .bus_suspend = ohci_bus_suspend, |
470 | .bus_resume = ohci_bus_resume, | |
3eb0c5f4 | 471 | #endif |
9293677a | 472 | .start_port_reset = ohci_start_port_reset, |
3eb0c5f4 BD |
473 | }; |
474 | ||
475 | /* device driver */ | |
476 | ||
e7f84331 | 477 | static int __devinit ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev) |
3eb0c5f4 | 478 | { |
3eb0c5f4 BD |
479 | return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev); |
480 | } | |
481 | ||
e7f84331 | 482 | static int __devexit ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev) |
3eb0c5f4 | 483 | { |
3ae5eaec | 484 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
3eb0c5f4 BD |
485 | |
486 | usb_hcd_s3c2410_remove(hcd, pdev); | |
487 | return 0; | |
488 | } | |
489 | ||
b870defe JH |
490 | #ifdef CONFIG_PM |
491 | static int ohci_hcd_s3c2410_drv_suspend(struct device *dev) | |
492 | { | |
493 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
494 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); | |
495 | struct platform_device *pdev = to_platform_device(dev); | |
496 | unsigned long flags; | |
497 | int rc = 0; | |
498 | ||
499 | /* | |
500 | * Root hub was already suspended. Disable irq emission and | |
501 | * mark HW unaccessible, bail out if RH has been resumed. Use | |
502 | * the spinlock to properly synchronize with possible pending | |
503 | * RH suspend or resume activity. | |
504 | */ | |
505 | spin_lock_irqsave(&ohci->lock, flags); | |
506 | if (ohci->rh_state != OHCI_RH_SUSPENDED) { | |
507 | rc = -EINVAL; | |
508 | goto bail; | |
509 | } | |
510 | ||
511 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); | |
512 | ||
513 | s3c2410_stop_hc(pdev); | |
514 | bail: | |
515 | spin_unlock_irqrestore(&ohci->lock, flags); | |
516 | ||
517 | return rc; | |
518 | } | |
519 | ||
520 | static int ohci_hcd_s3c2410_drv_resume(struct device *dev) | |
521 | { | |
522 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
523 | struct platform_device *pdev = to_platform_device(dev); | |
524 | ||
525 | s3c2410_start_hc(pdev, hcd); | |
526 | ||
527 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); | |
528 | ohci_finish_controller_resume(hcd); | |
529 | ||
530 | return 0; | |
531 | } | |
532 | #else | |
533 | #define ohci_hcd_s3c2410_drv_suspend NULL | |
534 | #define ohci_hcd_s3c2410_drv_resume NULL | |
535 | #endif | |
536 | ||
537 | static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = { | |
538 | .suspend = ohci_hcd_s3c2410_drv_suspend, | |
539 | .resume = ohci_hcd_s3c2410_drv_resume, | |
540 | }; | |
541 | ||
3ae5eaec | 542 | static struct platform_driver ohci_hcd_s3c2410_driver = { |
3eb0c5f4 | 543 | .probe = ohci_hcd_s3c2410_drv_probe, |
e7f84331 | 544 | .remove = __devexit_p(ohci_hcd_s3c2410_drv_remove), |
dd9048af | 545 | .shutdown = usb_hcd_platform_shutdown, |
3ae5eaec RK |
546 | .driver = { |
547 | .owner = THIS_MODULE, | |
548 | .name = "s3c2410-ohci", | |
b870defe | 549 | .pm = &ohci_hcd_s3c2410_pm_ops, |
3ae5eaec | 550 | }, |
3eb0c5f4 BD |
551 | }; |
552 | ||
f4fce61d | 553 | MODULE_ALIAS("platform:s3c2410-ohci"); |