1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/platform_device.h>
23#include <linux/clk.h>
24#include <linux/platform_data/usb-ohci-s3c2410.h>
25
26#define valid_port(idx) ((idx) == 1 || (idx) == 2)
27
28
29
30static struct clk *clk;
31static struct clk *usb_clk;
32
33
34
35static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
36
37
38
39static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
40{
41 return hcd->self.controller->platform_data;
42}
43
44static 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");
49
50 clk_enable(usb_clk);
51 mdelay(2);
52
53 clk_enable(clk);
54
55 if (info != NULL) {
56 info->hcd = hcd;
57 info->report_oc = s3c2410_hcd_oc;
58
59 if (info->enable_oc != NULL)
60 (info->enable_oc)(info, 1);
61 }
62}
63
64static 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
74 if (info->enable_oc != NULL)
75 (info->enable_oc)(info, 0);
76 }
77
78 clk_disable(clk);
79 clk_disable(usb_clk);
80}
81
82
83
84
85
86
87
88static int
89ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
90{
91 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
92 struct s3c2410_hcd_port *port;
93 int orig;
94 int portno;
95
96 orig = ohci_hub_status_data(hcd, buf);
97
98 if (info == NULL)
99 return orig;
100
101 port = &info->port[0];
102
103
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
122
123
124
125
126
127static 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;
135 (info->power_control)(port-1, to);
136 }
137}
138
139
140
141
142
143
144
145
146static int ohci_s3c2410_hub_control(
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
163
164
165
166 if (info == NULL) {
167 ret = ohci_hub_control(hcd, typeReq, wValue,
168 wIndex, buf, wLength);
169 goto out;
170 }
171
172
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
200 if (valid_port(wIndex))
201 info->port[wIndex-1].oc_status = 0;
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
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
235
236
237
238 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
239 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
240
241 if (info->enable_oc) {
242 desc->wHubCharacteristics &= ~cpu_to_le16(
243 HUB_CHAR_OCPM);
244 desc->wHubCharacteristics |= cpu_to_le16(
245 0x0008 |
246 0x0001);
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
256
257 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
258
259 if (valid_port(wIndex)) {
260 if (info->port[wIndex-1].oc_changed)
261 *data |= cpu_to_le32(RH_PS_OCIC);
262
263 if (info->port[wIndex-1].oc_status)
264 *data |= cpu_to_le32(RH_PS_POCI);
265 }
266 }
267
268 out:
269 return ret;
270}
271
272
273
274
275
276
277static 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
299
300 s3c2410_usb_set_power(info, portno+1, 0);
301 }
302 }
303
304 local_irq_restore(flags);
305}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321static void
322usb_hcd_s3c2410_remove(struct usb_hcd *hcd, struct platform_device *dev)
323{
324 usb_remove_hcd(hcd);
325 s3c2410_stop_hc(dev);
326 usb_put_hcd(hcd);
327}
328
329
330
331
332
333
334
335
336
337
338static int usb_hcd_s3c2410_probe(const struct hc_driver *driver,
339 struct platform_device *dev)
340{
341 struct usb_hcd *hcd = NULL;
342 int retval;
343
344 s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
345 s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
346
347 hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
348 if (hcd == NULL)
349 return -ENOMEM;
350
351 hcd->rsrc_start = dev->resource[0].start;
352 hcd->rsrc_len = resource_size(&dev->resource[0]);
353
354 hcd->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]);
355 if (IS_ERR(hcd->regs)) {
356 retval = PTR_ERR(hcd->regs);
357 goto err_put;
358 }
359
360 clk = devm_clk_get(&dev->dev, "usb-host");
361 if (IS_ERR(clk)) {
362 dev_err(&dev->dev, "cannot get usb-host clock\n");
363 retval = PTR_ERR(clk);
364 goto err_put;
365 }
366
367 usb_clk = devm_clk_get(&dev->dev, "usb-bus-host");
368 if (IS_ERR(usb_clk)) {
369 dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
370 retval = PTR_ERR(usb_clk);
371 goto err_put;
372 }
373
374 s3c2410_start_hc(dev, hcd);
375
376 ohci_hcd_init(hcd_to_ohci(hcd));
377
378 retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
379 if (retval != 0)
380 goto err_ioremap;
381
382 return 0;
383
384 err_ioremap:
385 s3c2410_stop_hc(dev);
386
387 err_put:
388 usb_put_hcd(hcd);
389 return retval;
390}
391
392
393
394static int
395ohci_s3c2410_start(struct usb_hcd *hcd)
396{
397 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
398 int ret;
399
400 ret = ohci_init(ohci);
401 if (ret < 0)
402 return ret;
403
404 ret = ohci_run(ohci);
405 if (ret < 0) {
406 dev_err(hcd->self.controller, "can't start %s\n",
407 hcd->self.bus_name);
408 ohci_stop(hcd);
409 return ret;
410 }
411
412 return 0;
413}
414
415
416static const struct hc_driver ohci_s3c2410_hc_driver = {
417 .description = hcd_name,
418 .product_desc = "S3C24XX OHCI",
419 .hcd_priv_size = sizeof(struct ohci_hcd),
420
421
422
423
424 .irq = ohci_irq,
425 .flags = HCD_USB11 | HCD_MEMORY,
426
427
428
429
430 .start = ohci_s3c2410_start,
431 .stop = ohci_stop,
432 .shutdown = ohci_shutdown,
433
434
435
436
437 .urb_enqueue = ohci_urb_enqueue,
438 .urb_dequeue = ohci_urb_dequeue,
439 .endpoint_disable = ohci_endpoint_disable,
440
441
442
443
444 .get_frame_number = ohci_get_frame,
445
446
447
448
449 .hub_status_data = ohci_s3c2410_hub_status_data,
450 .hub_control = ohci_s3c2410_hub_control,
451#ifdef CONFIG_PM
452 .bus_suspend = ohci_bus_suspend,
453 .bus_resume = ohci_bus_resume,
454#endif
455 .start_port_reset = ohci_start_port_reset,
456};
457
458
459
460static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
461{
462 return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
463}
464
465static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
466{
467 struct usb_hcd *hcd = platform_get_drvdata(pdev);
468
469 usb_hcd_s3c2410_remove(hcd, pdev);
470 return 0;
471}
472
473#ifdef CONFIG_PM
474static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
475{
476 struct usb_hcd *hcd = dev_get_drvdata(dev);
477 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
478 struct platform_device *pdev = to_platform_device(dev);
479 unsigned long flags;
480 int rc = 0;
481
482
483
484
485
486
487
488 spin_lock_irqsave(&ohci->lock, flags);
489 if (ohci->rh_state != OHCI_RH_SUSPENDED) {
490 rc = -EINVAL;
491 goto bail;
492 }
493
494 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
495
496 s3c2410_stop_hc(pdev);
497bail:
498 spin_unlock_irqrestore(&ohci->lock, flags);
499
500 return rc;
501}
502
503static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
504{
505 struct usb_hcd *hcd = dev_get_drvdata(dev);
506 struct platform_device *pdev = to_platform_device(dev);
507
508 s3c2410_start_hc(pdev, hcd);
509
510 ohci_resume(hcd, false);
511
512 return 0;
513}
514#else
515#define ohci_hcd_s3c2410_drv_suspend NULL
516#define ohci_hcd_s3c2410_drv_resume NULL
517#endif
518
519static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
520 .suspend = ohci_hcd_s3c2410_drv_suspend,
521 .resume = ohci_hcd_s3c2410_drv_resume,
522};
523
524static struct platform_driver ohci_hcd_s3c2410_driver = {
525 .probe = ohci_hcd_s3c2410_drv_probe,
526 .remove = ohci_hcd_s3c2410_drv_remove,
527 .shutdown = usb_hcd_platform_shutdown,
528 .driver = {
529 .owner = THIS_MODULE,
530 .name = "s3c2410-ohci",
531 .pm = &ohci_hcd_s3c2410_pm_ops,
532 },
533};
534
535MODULE_ALIAS("platform:s3c2410-ohci");
536