ohci-s3c2410.c 11 KB

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