1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/clk.h>
23#include <linux/io.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/platform_device.h>
27#include <linux/platform_data/usb-ohci-s3c2410.h>
28#include <linux/usb.h>
29#include <linux/usb/hcd.h>
30
31#include "ohci.h"
32
33
34#define valid_port(idx) ((idx) == 1 || (idx) == 2)
35
36
37
38
39#define DRIVER_DESC "OHCI S3C2410 driver"
40
41static const char hcd_name[] = "ohci-s3c2410";
42
43static struct clk *clk;
44static struct clk *usb_clk;
45
46
47
48static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
49 u16 wValue, u16 wIndex, char *buf, u16 wLength);
50static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
51
52static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
53
54
55
56static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
57{
58 return dev_get_platdata(hcd->self.controller);
59}
60
61static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
62{
63 struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
64
65 dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
66
67 clk_prepare_enable(usb_clk);
68 mdelay(2);
69
70 clk_prepare_enable(clk);
71
72 if (info != NULL) {
73 info->hcd = hcd;
74 info->report_oc = s3c2410_hcd_oc;
75
76 if (info->enable_oc != NULL)
77 (info->enable_oc)(info, 1);
78 }
79}
80
81static void s3c2410_stop_hc(struct platform_device *dev)
82{
83 struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
84
85 dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
86
87 if (info != NULL) {
88 info->report_oc = NULL;
89 info->hcd = NULL;
90
91 if (info->enable_oc != NULL)
92 (info->enable_oc)(info, 0);
93 }
94
95 clk_disable_unprepare(clk);
96 clk_disable_unprepare(usb_clk);
97}
98
99
100
101
102
103
104
105static int
106ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
107{
108 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
109 struct s3c2410_hcd_port *port;
110 int orig;
111 int portno;
112
113 orig = orig_ohci_hub_status_data(hcd, buf);
114
115 if (info == NULL)
116 return orig;
117
118 port = &info->port[0];
119
120
121
122 for (portno = 0; portno < 2; port++, portno++) {
123 if (port->oc_changed == 1 &&
124 port->flags & S3C_HCDFLG_USED) {
125 dev_dbg(hcd->self.controller,
126 "oc change on port %d\n", portno);
127
128 if (orig < 1)
129 orig = 1;
130
131 buf[0] |= 1<<(portno+1);
132 }
133 }
134
135 return orig;
136}
137
138
139
140
141
142
143
144static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
145 int port, int to)
146{
147 if (info == NULL)
148 return;
149
150 if (info->power_control != NULL) {
151 info->port[port-1].power = to;
152 (info->power_control)(port-1, to);
153 }
154}
155
156
157
158
159
160
161
162
163static int ohci_s3c2410_hub_control(
164 struct usb_hcd *hcd,
165 u16 typeReq,
166 u16 wValue,
167 u16 wIndex,
168 char *buf,
169 u16 wLength)
170{
171 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
172 struct usb_hub_descriptor *desc;
173 int ret = -EINVAL;
174 u32 *data = (u32 *)buf;
175
176 dev_dbg(hcd->self.controller,
177 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
178 hcd, typeReq, wValue, wIndex, buf, wLength);
179
180
181
182
183 if (info == NULL) {
184 ret = orig_ohci_hub_control(hcd, typeReq, wValue,
185 wIndex, buf, wLength);
186 goto out;
187 }
188
189
190
191 switch (typeReq) {
192 case SetPortFeature:
193 if (wValue == USB_PORT_FEAT_POWER) {
194 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
195 s3c2410_usb_set_power(info, wIndex, 1);
196 goto out;
197 }
198 break;
199
200 case ClearPortFeature:
201 switch (wValue) {
202 case USB_PORT_FEAT_C_OVER_CURRENT:
203 dev_dbg(hcd->self.controller,
204 "ClearPortFeature: C_OVER_CURRENT\n");
205
206 if (valid_port(wIndex)) {
207 info->port[wIndex-1].oc_changed = 0;
208 info->port[wIndex-1].oc_status = 0;
209 }
210
211 goto out;
212
213 case USB_PORT_FEAT_OVER_CURRENT:
214 dev_dbg(hcd->self.controller,
215 "ClearPortFeature: OVER_CURRENT\n");
216
217 if (valid_port(wIndex))
218 info->port[wIndex-1].oc_status = 0;
219
220 goto out;
221
222 case USB_PORT_FEAT_POWER:
223 dev_dbg(hcd->self.controller,
224 "ClearPortFeature: POWER\n");
225
226 if (valid_port(wIndex)) {
227 s3c2410_usb_set_power(info, wIndex, 0);
228 return 0;
229 }
230 }
231 break;
232 }
233
234 ret = orig_ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
235 if (ret)
236 goto out;
237
238 switch (typeReq) {
239 case GetHubDescriptor:
240
241
242
243 desc = (struct usb_hub_descriptor *)buf;
244
245 if (info->power_control == NULL)
246 return ret;
247
248 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
249 desc->wHubCharacteristics);
250
251
252
253
254
255 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
256 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
257
258 if (info->enable_oc) {
259 desc->wHubCharacteristics &= ~cpu_to_le16(
260 HUB_CHAR_OCPM);
261 desc->wHubCharacteristics |= cpu_to_le16(
262 0x0008 |
263 0x0001);
264 }
265
266 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
267 desc->wHubCharacteristics);
268
269 return ret;
270
271 case GetPortStatus:
272
273
274 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
275
276 if (valid_port(wIndex)) {
277 if (info->port[wIndex-1].oc_changed)
278 *data |= cpu_to_le32(RH_PS_OCIC);
279
280 if (info->port[wIndex-1].oc_status)
281 *data |= cpu_to_le32(RH_PS_POCI);
282 }
283 }
284
285 out:
286 return ret;
287}
288
289
290
291
292
293
294static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
295{
296 struct s3c2410_hcd_port *port;
297 struct usb_hcd *hcd;
298 unsigned long flags;
299 int portno;
300
301 if (info == NULL)
302 return;
303
304 port = &info->port[0];
305 hcd = info->hcd;
306
307 local_irq_save(flags);
308
309 for (portno = 0; portno < 2; port++, portno++) {
310 if (port_oc & (1<<portno) &&
311 port->flags & S3C_HCDFLG_USED) {
312 port->oc_status = 1;
313 port->oc_changed = 1;
314
315
316
317 s3c2410_usb_set_power(info, portno+1, 0);
318 }
319 }
320
321 local_irq_restore(flags);
322}
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338static void
339usb_hcd_s3c2410_remove(struct usb_hcd *hcd, struct platform_device *dev)
340{
341 usb_remove_hcd(hcd);
342 s3c2410_stop_hc(dev);
343 usb_put_hcd(hcd);
344}
345
346
347
348
349
350
351
352
353
354
355static int usb_hcd_s3c2410_probe(const struct hc_driver *driver,
356 struct platform_device *dev)
357{
358 struct usb_hcd *hcd = NULL;
359 struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
360 int retval;
361
362 s3c2410_usb_set_power(info, 1, 1);
363 s3c2410_usb_set_power(info, 2, 1);
364
365 hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
366 if (hcd == NULL)
367 return -ENOMEM;
368
369 hcd->rsrc_start = dev->resource[0].start;
370 hcd->rsrc_len = resource_size(&dev->resource[0]);
371
372 hcd->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]);
373 if (IS_ERR(hcd->regs)) {
374 retval = PTR_ERR(hcd->regs);
375 goto err_put;
376 }
377
378 clk = devm_clk_get(&dev->dev, "usb-host");
379 if (IS_ERR(clk)) {
380 dev_err(&dev->dev, "cannot get usb-host clock\n");
381 retval = PTR_ERR(clk);
382 goto err_put;
383 }
384
385 usb_clk = devm_clk_get(&dev->dev, "usb-bus-host");
386 if (IS_ERR(usb_clk)) {
387 dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
388 retval = PTR_ERR(usb_clk);
389 goto err_put;
390 }
391
392 s3c2410_start_hc(dev, hcd);
393
394 retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
395 if (retval != 0)
396 goto err_ioremap;
397
398 device_wakeup_enable(hcd->self.controller);
399 return 0;
400
401 err_ioremap:
402 s3c2410_stop_hc(dev);
403
404 err_put:
405 usb_put_hcd(hcd);
406 return retval;
407}
408
409
410
411static struct hc_driver __read_mostly ohci_s3c2410_hc_driver;
412
413static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
414{
415 return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
416}
417
418static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
419{
420 struct usb_hcd *hcd = platform_get_drvdata(pdev);
421
422 usb_hcd_s3c2410_remove(hcd, pdev);
423 return 0;
424}
425
426#ifdef CONFIG_PM
427static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
428{
429 struct usb_hcd *hcd = dev_get_drvdata(dev);
430 struct platform_device *pdev = to_platform_device(dev);
431 bool do_wakeup = device_may_wakeup(dev);
432 int rc = 0;
433
434 rc = ohci_suspend(hcd, do_wakeup);
435 if (rc)
436 return rc;
437
438 s3c2410_stop_hc(pdev);
439
440 return rc;
441}
442
443static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
444{
445 struct usb_hcd *hcd = dev_get_drvdata(dev);
446 struct platform_device *pdev = to_platform_device(dev);
447
448 s3c2410_start_hc(pdev, hcd);
449
450 ohci_resume(hcd, false);
451
452 return 0;
453}
454#else
455#define ohci_hcd_s3c2410_drv_suspend NULL
456#define ohci_hcd_s3c2410_drv_resume NULL
457#endif
458
459static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
460 .suspend = ohci_hcd_s3c2410_drv_suspend,
461 .resume = ohci_hcd_s3c2410_drv_resume,
462};
463
464static struct platform_driver ohci_hcd_s3c2410_driver = {
465 .probe = ohci_hcd_s3c2410_drv_probe,
466 .remove = ohci_hcd_s3c2410_drv_remove,
467 .shutdown = usb_hcd_platform_shutdown,
468 .driver = {
469 .owner = THIS_MODULE,
470 .name = "s3c2410-ohci",
471 .pm = &ohci_hcd_s3c2410_pm_ops,
472 },
473};
474
475static int __init ohci_s3c2410_init(void)
476{
477 if (usb_disabled())
478 return -ENODEV;
479
480 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
481 ohci_init_driver(&ohci_s3c2410_hc_driver, NULL);
482
483
484
485
486
487
488
489
490
491
492 orig_ohci_hub_control = ohci_s3c2410_hc_driver.hub_control;
493 orig_ohci_hub_status_data = ohci_s3c2410_hc_driver.hub_status_data;
494
495 ohci_s3c2410_hc_driver.hub_status_data = ohci_s3c2410_hub_status_data;
496 ohci_s3c2410_hc_driver.hub_control = ohci_s3c2410_hub_control;
497
498 return platform_driver_register(&ohci_hcd_s3c2410_driver);
499}
500module_init(ohci_s3c2410_init);
501
502static void __exit ohci_s3c2410_cleanup(void)
503{
504 platform_driver_unregister(&ohci_hcd_s3c2410_driver);
505}
506module_exit(ohci_s3c2410_cleanup);
507
508MODULE_DESCRIPTION(DRIVER_DESC);
509MODULE_LICENSE("GPL");
510MODULE_ALIAS("platform:s3c2410-ohci");
511