ohci-s3c2410.c 12 KB

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