ohci-s3c2410.c 11 KB

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