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