ohci-s3c2410.c 12 KB

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