blob: 6829814b7aafbd6faa17288ebd1999a11ff9dec4 [file] [log] [blame]
Ben Dooks3eb0c5f2005-07-29 12:18:03 -07001/*
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 Rusell 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
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070025#include <asm/hardware.h>
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070026#include <asm/arch/usb-control.h>
27
28#define valid_port(idx) ((idx) == 1 || (idx) == 2)
29
30/* clock device associated with the hcd */
31
32static struct clk *clk;
Ben Dooks3799c402006-04-02 01:45:00 +010033static struct clk *usb_clk;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070034
35/* forward definitions */
36
37static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
38
39/* conversion functions */
40
Ben Dooksf096e042006-03-21 22:54:47 +000041static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070042{
43 return hcd->self.controller->platform_data;
44}
45
46static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
47{
48 struct s3c2410_hcd_info *info = dev->dev.platform_data;
49
50 dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
Ben Dooks3799c402006-04-02 01:45:00 +010051
52 clk_enable(usb_clk);
53 mdelay(2); /* let the bus clock stabilise */
54
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070055 clk_enable(clk);
56
57 if (info != NULL) {
58 info->hcd = hcd;
59 info->report_oc = s3c2410_hcd_oc;
60
61 if (info->enable_oc != NULL) {
62 (info->enable_oc)(info, 1);
63 }
64 }
65}
66
67static void s3c2410_stop_hc(struct platform_device *dev)
68{
69 struct s3c2410_hcd_info *info = dev->dev.platform_data;
70
71 dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
72
73 if (info != NULL) {
74 info->report_oc = NULL;
75 info->hcd = NULL;
76
77 if (info->enable_oc != NULL) {
78 (info->enable_oc)(info, 0);
79 }
80 }
81
82 clk_disable(clk);
Ben Dooks3799c402006-04-02 01:45:00 +010083 clk_disable(usb_clk);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -070084}
85
86/* ohci_s3c2410_hub_status_data
87 *
88 * update the status data from the hub with anything that
89 * has been detected by our system
90*/
91
92static int
93ohci_s3c2410_hub_status_data (struct usb_hcd *hcd, char *buf)
94{
95 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
96 struct s3c2410_hcd_port *port;
97 int orig;
98 int portno;
99
100 orig = ohci_hub_status_data (hcd, buf);
101
102 if (info == NULL)
103 return orig;
104
105 port = &info->port[0];
106
107 /* mark any changed port as changed */
108
109 for (portno = 0; portno < 2; port++, portno++) {
110 if (port->oc_changed == 1 &&
111 port->flags & S3C_HCDFLG_USED) {
112 dev_dbg(hcd->self.controller,
113 "oc change on port %d\n", portno);
114
115 if (orig < 1)
116 orig = 1;
117
118 buf[0] |= 1<<(portno+1);
119 }
120 }
121
122 return orig;
123}
124
125/* s3c2410_usb_set_power
126 *
127 * configure the power on a port, by calling the platform device
128 * routine registered with the platform device
129*/
130
131static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
132 int port, int to)
133{
134 if (info == NULL)
135 return;
136
137 if (info->power_control != NULL) {
138 info->port[port-1].power = to;
Ben Dooksba44e7c2005-08-09 15:04:00 +0100139 (info->power_control)(port-1, to);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700140 }
141}
142
143/* ohci_s3c2410_hub_control
144 *
145 * look at control requests to the hub, and see if we need
146 * to take any action or over-ride the results from the
147 * request.
148*/
149
150static int ohci_s3c2410_hub_control (
151 struct usb_hcd *hcd,
152 u16 typeReq,
153 u16 wValue,
154 u16 wIndex,
155 char *buf,
156 u16 wLength)
157{
158 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
159 struct usb_hub_descriptor *desc;
160 int ret = -EINVAL;
161 u32 *data = (u32 *)buf;
162
163 dev_dbg(hcd->self.controller,
164 "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
165 hcd, typeReq, wValue, wIndex, buf, wLength);
166
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800167 /* if we are only an humble host without any special capabilities
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700168 * process the request straight away and exit */
169
170 if (info == NULL) {
171 ret = ohci_hub_control(hcd, typeReq, wValue,
172 wIndex, buf, wLength);
173 goto out;
174 }
175
176 /* check the request to see if it needs handling */
177
178 switch (typeReq) {
179 case SetPortFeature:
180 if (wValue == USB_PORT_FEAT_POWER) {
181 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
182 s3c2410_usb_set_power(info, wIndex, 1);
183 goto out;
184 }
185 break;
186
187 case ClearPortFeature:
188 switch (wValue) {
189 case USB_PORT_FEAT_C_OVER_CURRENT:
190 dev_dbg(hcd->self.controller,
191 "ClearPortFeature: C_OVER_CURRENT\n");
192
193 if (valid_port(wIndex)) {
194 info->port[wIndex-1].oc_changed = 0;
195 info->port[wIndex-1].oc_status = 0;
196 }
197
198 goto out;
199
200 case USB_PORT_FEAT_OVER_CURRENT:
201 dev_dbg(hcd->self.controller,
202 "ClearPortFeature: OVER_CURRENT\n");
203
204 if (valid_port(wIndex)) {
205 info->port[wIndex-1].oc_status = 0;
206 }
207
208 goto out;
209
210 case USB_PORT_FEAT_POWER:
211 dev_dbg(hcd->self.controller,
212 "ClearPortFeature: POWER\n");
213
214 if (valid_port(wIndex)) {
215 s3c2410_usb_set_power(info, wIndex, 0);
216 return 0;
217 }
218 }
219 break;
220 }
221
222 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
223 if (ret)
224 goto out;
225
226 switch (typeReq) {
227 case GetHubDescriptor:
228
229 /* update the hub's descriptor */
230
231 desc = (struct usb_hub_descriptor *)buf;
232
233 if (info->power_control == NULL)
234 return ret;
235
236 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
237 desc->wHubCharacteristics);
238
239 /* remove the old configurations for power-switching, and
240 * over-current protection, and insert our new configuration
241 */
242
243 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
244 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
245
246 if (info->enable_oc) {
247 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
248 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
249 }
250
251 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
252 desc->wHubCharacteristics);
253
254 return ret;
255
256 case GetPortStatus:
257 /* check port status */
258
259 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
260
261 if (valid_port(wIndex)) {
262 if (info->port[wIndex-1].oc_changed) {
263 *data |= cpu_to_le32(RH_PS_OCIC);
264 }
265
266 if (info->port[wIndex-1].oc_status) {
267 *data |= cpu_to_le32(RH_PS_POCI);
268 }
269 }
270 }
271
272 out:
273 return ret;
274}
275
276/* s3c2410_hcd_oc
277 *
278 * handle an over-current report
279*/
280
281static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
282{
283 struct s3c2410_hcd_port *port;
284 struct usb_hcd *hcd;
285 unsigned long flags;
286 int portno;
287
288 if (info == NULL)
289 return;
290
291 port = &info->port[0];
292 hcd = info->hcd;
293
294 local_irq_save(flags);
295
296 for (portno = 0; portno < 2; port++, portno++) {
297 if (port_oc & (1<<portno) &&
298 port->flags & S3C_HCDFLG_USED) {
299 port->oc_status = 1;
300 port->oc_changed = 1;
301
302 /* ok, once over-current is detected,
303 the port needs to be powered down */
304 s3c2410_usb_set_power(info, portno+1, 0);
305 }
306 }
307
308 local_irq_restore(flags);
309}
310
311/* may be called without controller electrically present */
312/* may be called with controller, bus, and devices active */
313
314/*
315 * usb_hcd_s3c2410_remove - shutdown processing for HCD
316 * @dev: USB Host Controller being removed
317 * Context: !in_interrupt()
318 *
319 * Reverses the effect of usb_hcd_3c2410_probe(), first invoking
320 * the HCD's stop() method. It is always called from a thread
321 * context, normally "rmmod", "apmd", or something similar.
322 *
323*/
324
Ben Dooksf096e042006-03-21 22:54:47 +0000325static void
326usb_hcd_s3c2410_remove (struct usb_hcd *hcd, struct platform_device *dev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700327{
328 usb_remove_hcd(hcd);
329 s3c2410_stop_hc(dev);
330 iounmap(hcd->regs);
331 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
332 usb_put_hcd(hcd);
333}
334
335/**
336 * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs
337 * Context: !in_interrupt()
338 *
339 * Allocates basic resources for this USB host controller, and
340 * then invokes the start() method for the HCD associated with it
341 * through the hotplug entry's driver_data.
342 *
343 */
Ben Dooksf096e042006-03-21 22:54:47 +0000344static int usb_hcd_s3c2410_probe (const struct hc_driver *driver,
345 struct platform_device *dev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700346{
347 struct usb_hcd *hcd = NULL;
348 int retval;
349
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700350 s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
Ben Dooksba44e7c2005-08-09 15:04:00 +0100351 s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700352
353 hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
354 if (hcd == NULL)
355 return -ENOMEM;
356
357 hcd->rsrc_start = dev->resource[0].start;
358 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
359
360 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
361 dev_err(&dev->dev, "request_mem_region failed");
362 retval = -EBUSY;
Ben Dooks3799c402006-04-02 01:45:00 +0100363 goto err_put;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700364 }
365
Ben Dooks3799c402006-04-02 01:45:00 +0100366 clk = clk_get(&dev->dev, "usb-host");
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700367 if (IS_ERR(clk)) {
368 dev_err(&dev->dev, "cannot get usb-host clock\n");
369 retval = -ENOENT;
Ben Dooks3799c402006-04-02 01:45:00 +0100370 goto err_mem;
371 }
372
Ben Dooksb2a8e092006-06-26 23:36:07 +0100373 usb_clk = clk_get(&dev->dev, "usb-bus-host");
Ben Dooks3799c402006-04-02 01:45:00 +0100374 if (IS_ERR(usb_clk)) {
375 dev_err(&dev->dev, "cannot get usb-host clock\n");
376 retval = -ENOENT;
377 goto err_clk;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700378 }
379
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700380 s3c2410_start_hc(dev, hcd);
381
382 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
383 if (!hcd->regs) {
384 dev_err(&dev->dev, "ioremap failed\n");
385 retval = -ENOMEM;
Ben Dooks3799c402006-04-02 01:45:00 +0100386 goto err_ioremap;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700387 }
388
389 ohci_hcd_init(hcd_to_ohci(hcd));
390
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -0700391 retval = usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700392 if (retval != 0)
Ben Dooks3799c402006-04-02 01:45:00 +0100393 goto err_ioremap;
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700394
395 return 0;
396
Ben Dooks3799c402006-04-02 01:45:00 +0100397 err_ioremap:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700398 s3c2410_stop_hc(dev);
399 iounmap(hcd->regs);
Ben Dooks3799c402006-04-02 01:45:00 +0100400 clk_put(usb_clk);
401
402 err_clk:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700403 clk_put(clk);
404
Ben Dooks3799c402006-04-02 01:45:00 +0100405 err_mem:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700406 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
407
Ben Dooks3799c402006-04-02 01:45:00 +0100408 err_put:
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700409 usb_put_hcd(hcd);
410 return retval;
411}
412
413/*-------------------------------------------------------------------------*/
414
415static int
416ohci_s3c2410_start (struct usb_hcd *hcd)
417{
418 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
419 int ret;
420
421 if ((ret = ohci_init(ohci)) < 0)
422 return ret;
423
424 if ((ret = ohci_run (ohci)) < 0) {
425 err ("can't start %s", hcd->self.bus_name);
426 ohci_stop (hcd);
427 return ret;
428 }
429
430 return 0;
431}
432
433
434static const struct hc_driver ohci_s3c2410_hc_driver = {
435 .description = hcd_name,
436 .product_desc = "S3C24XX OHCI",
437 .hcd_priv_size = sizeof(struct ohci_hcd),
438
439 /*
440 * generic hardware linkage
441 */
442 .irq = ohci_irq,
443 .flags = HCD_USB11 | HCD_MEMORY,
444
445 /*
446 * basic lifecycle operations
447 */
448 .start = ohci_s3c2410_start,
449 .stop = ohci_stop,
David Brownelldd9048a2006-12-05 03:18:31 -0800450 .shutdown = ohci_shutdown,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700451
452 /*
453 * managing i/o requests and associated device resources
454 */
455 .urb_enqueue = ohci_urb_enqueue,
456 .urb_dequeue = ohci_urb_dequeue,
457 .endpoint_disable = ohci_endpoint_disable,
458
459 /*
460 * scheduling support
461 */
462 .get_frame_number = ohci_get_frame,
463
464 /*
465 * root hub support
466 */
467 .hub_status_data = ohci_s3c2410_hub_status_data,
468 .hub_control = ohci_s3c2410_hub_control,
David Brownelld4139842006-08-04 11:31:55 -0700469 .hub_irq_enable = ohci_rhsc_enable,
David Brownell8ad7fe12005-09-13 19:59:11 -0700470#ifdef CONFIG_PM
Alan Stern0c0382e2005-10-13 17:08:02 -0400471 .bus_suspend = ohci_bus_suspend,
472 .bus_resume = ohci_bus_resume,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700473#endif
David Brownell92936772005-09-22 22:32:11 -0700474 .start_port_reset = ohci_start_port_reset,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700475};
476
477/* device driver */
478
Russell King3ae5eae2005-11-09 22:32:44 +0000479static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700480{
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700481 return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
482}
483
Russell King3ae5eae2005-11-09 22:32:44 +0000484static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700485{
Russell King3ae5eae2005-11-09 22:32:44 +0000486 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700487
488 usb_hcd_s3c2410_remove(hcd, pdev);
489 return 0;
490}
491
Russell King3ae5eae2005-11-09 22:32:44 +0000492static struct platform_driver ohci_hcd_s3c2410_driver = {
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700493 .probe = ohci_hcd_s3c2410_drv_probe,
494 .remove = ohci_hcd_s3c2410_drv_remove,
David Brownelldd9048a2006-12-05 03:18:31 -0800495 .shutdown = usb_hcd_platform_shutdown,
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700496 /*.suspend = ohci_hcd_s3c2410_drv_suspend, */
497 /*.resume = ohci_hcd_s3c2410_drv_resume, */
Russell King3ae5eae2005-11-09 22:32:44 +0000498 .driver = {
499 .owner = THIS_MODULE,
500 .name = "s3c2410-ohci",
501 },
Ben Dooks3eb0c5f2005-07-29 12:18:03 -0700502};
503