ohci-s3c2410.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * OHCI HCD (Host Controller Driver) for USB.
  4. *
  5. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  6. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  7. * (C) Copyright 2002 Hewlett-Packard Company
  8. *
  9. * USB Bus Glue for Samsung S3C2410
  10. *
  11. * Written by Christopher Hoover <ch@hpl.hp.com>
  12. * Based on fragments of previous driver by Russell King et al.
  13. *
  14. * Modified for S3C2410 from ohci-sa1111.c, ohci-omap.c and ohci-lh7a40.c
  15. * by Ben Dooks, <ben@simtec.co.uk>
  16. * Copyright (C) 2004 Simtec Electronics
  17. *
  18. * Thanks to basprog@mail.ru for updates to newer kernels
  19. *
  20. * This file is licenced under the GPL.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/platform_data/usb-ohci-s3c2410.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/hcd.h>
  30. #include "ohci.h"
  31. #define valid_port(idx) ((idx) == 1 || (idx) == 2)
  32. /* clock device associated with the hcd */
  33. #define DRIVER_DESC "OHCI S3C2410 driver"
  34. static const char hcd_name[] = "ohci-s3c2410";
  35. static struct clk *clk;
  36. static struct clk *usb_clk;
  37. static struct hc_driver __read_mostly ohci_s3c2410_hc_driver;
  38. /* forward definitions */
  39. static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
  40. /* conversion functions */
  41. static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
  42. {
  43. return dev_get_platdata(hcd->self.controller);
  44. }
  45. static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
  46. {
  47. struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
  48. dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
  49. clk_prepare_enable(usb_clk);
  50. mdelay(2); /* let the bus clock stabilise */
  51. clk_prepare_enable(clk);
  52. if (info != NULL) {
  53. info->hcd = hcd;
  54. info->report_oc = s3c2410_hcd_oc;
  55. if (info->enable_oc != NULL)
  56. (info->enable_oc)(info, 1);
  57. }
  58. }
  59. static void s3c2410_stop_hc(struct platform_device *dev)
  60. {
  61. struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
  62. dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
  63. if (info != NULL) {
  64. info->report_oc = NULL;
  65. info->hcd = NULL;
  66. if (info->enable_oc != NULL)
  67. (info->enable_oc)(info, 0);
  68. }
  69. clk_disable_unprepare(clk);
  70. clk_disable_unprepare(usb_clk);
  71. }
  72. /* ohci_s3c2410_hub_status_data
  73. *
  74. * update the status data from the hub with anything that
  75. * has been detected by our system
  76. */
  77. static int
  78. ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
  79. {
  80. struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
  81. struct s3c2410_hcd_port *port;
  82. int orig;
  83. int portno;
  84. orig = ohci_hub_status_data(hcd, buf);
  85. if (info == NULL)
  86. return orig;
  87. port = &info->port[0];
  88. /* mark any changed port as changed */
  89. for (portno = 0; portno < 2; port++, portno++) {
  90. if (port->oc_changed == 1 &&
  91. port->flags & S3C_HCDFLG_USED) {
  92. dev_dbg(hcd->self.controller,
  93. "oc change on port %d\n", portno);
  94. if (orig < 1)
  95. orig = 1;
  96. buf[0] |= 1<<(portno+1);
  97. }
  98. }
  99. return orig;
  100. }
  101. /* s3c2410_usb_set_power
  102. *
  103. * configure the power on a port, by calling the platform device
  104. * routine registered with the platform device
  105. */
  106. static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
  107. int port, int to)
  108. {
  109. if (info == NULL)
  110. return;
  111. if (info->power_control != NULL) {
  112. info->port[port-1].power = to;
  113. (info->power_control)(port-1, to);
  114. }
  115. }
  116. /* ohci_s3c2410_hub_control
  117. *
  118. * look at control requests to the hub, and see if we need
  119. * to take any action or over-ride the results from the
  120. * request.
  121. */
  122. static int ohci_s3c2410_hub_control(
  123. struct usb_hcd *hcd,
  124. u16 typeReq,
  125. u16 wValue,
  126. u16 wIndex,
  127. char *buf,
  128. u16 wLength)
  129. {
  130. struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
  131. struct usb_hub_descriptor *desc;
  132. int ret = -EINVAL;
  133. u32 *data = (u32 *)buf;
  134. dev_dbg(hcd->self.controller,
  135. "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
  136. hcd, typeReq, wValue, wIndex, buf, wLength);
  137. /* if we are only an humble host without any special capabilities
  138. * process the request straight away and exit */
  139. if (info == NULL) {
  140. ret = ohci_hub_control(hcd, typeReq, wValue,
  141. wIndex, buf, wLength);
  142. goto out;
  143. }
  144. /* check the request to see if it needs handling */
  145. switch (typeReq) {
  146. case SetPortFeature:
  147. if (wValue == USB_PORT_FEAT_POWER) {
  148. dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
  149. s3c2410_usb_set_power(info, wIndex, 1);
  150. goto out;
  151. }
  152. break;
  153. case ClearPortFeature:
  154. switch (wValue) {
  155. case USB_PORT_FEAT_C_OVER_CURRENT:
  156. dev_dbg(hcd->self.controller,
  157. "ClearPortFeature: C_OVER_CURRENT\n");
  158. if (valid_port(wIndex)) {
  159. info->port[wIndex-1].oc_changed = 0;
  160. info->port[wIndex-1].oc_status = 0;
  161. }
  162. goto out;
  163. case USB_PORT_FEAT_OVER_CURRENT:
  164. dev_dbg(hcd->self.controller,
  165. "ClearPortFeature: OVER_CURRENT\n");
  166. if (valid_port(wIndex))
  167. info->port[wIndex-1].oc_status = 0;
  168. goto out;
  169. case USB_PORT_FEAT_POWER:
  170. dev_dbg(hcd->self.controller,
  171. "ClearPortFeature: POWER\n");
  172. if (valid_port(wIndex)) {
  173. s3c2410_usb_set_power(info, wIndex, 0);
  174. return 0;
  175. }
  176. }
  177. break;
  178. }
  179. ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  180. if (ret)
  181. goto out;
  182. switch (typeReq) {
  183. case GetHubDescriptor:
  184. /* update the hub's descriptor */
  185. desc = (struct usb_hub_descriptor *)buf;
  186. if (info->power_control == NULL)
  187. return ret;
  188. dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
  189. desc->wHubCharacteristics);
  190. /* remove the old configurations for power-switching, and
  191. * over-current protection, and insert our new configuration
  192. */
  193. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
  194. desc->wHubCharacteristics |= cpu_to_le16(
  195. HUB_CHAR_INDV_PORT_LPSM);
  196. if (info->enable_oc) {
  197. desc->wHubCharacteristics &= ~cpu_to_le16(
  198. HUB_CHAR_OCPM);
  199. desc->wHubCharacteristics |= cpu_to_le16(
  200. HUB_CHAR_INDV_PORT_OCPM);
  201. }
  202. dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
  203. desc->wHubCharacteristics);
  204. return ret;
  205. case GetPortStatus:
  206. /* check port status */
  207. dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
  208. if (valid_port(wIndex)) {
  209. if (info->port[wIndex-1].oc_changed)
  210. *data |= cpu_to_le32(RH_PS_OCIC);
  211. if (info->port[wIndex-1].oc_status)
  212. *data |= cpu_to_le32(RH_PS_POCI);
  213. }
  214. }
  215. out:
  216. return ret;
  217. }
  218. /* s3c2410_hcd_oc
  219. *
  220. * handle an over-current report
  221. */
  222. static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
  223. {
  224. struct s3c2410_hcd_port *port;
  225. struct usb_hcd *hcd;
  226. unsigned long flags;
  227. int portno;
  228. if (info == NULL)
  229. return;
  230. port = &info->port[0];
  231. hcd = info->hcd;
  232. local_irq_save(flags);
  233. for (portno = 0; portno < 2; port++, portno++) {
  234. if (port_oc & (1<<portno) &&
  235. port->flags & S3C_HCDFLG_USED) {
  236. port->oc_status = 1;
  237. port->oc_changed = 1;
  238. /* ok, once over-current is detected,
  239. the port needs to be powered down */
  240. s3c2410_usb_set_power(info, portno+1, 0);
  241. }
  242. }
  243. local_irq_restore(flags);
  244. }
  245. /* may be called without controller electrically present */
  246. /* may be called with controller, bus, and devices active */
  247. /*
  248. * ohci_hcd_s3c2410_remove - shutdown processing for HCD
  249. * @dev: USB Host Controller being removed
  250. * Context: !in_interrupt()
  251. *
  252. * Reverses the effect of ohci_hcd_3c2410_probe(), first invoking
  253. * the HCD's stop() method. It is always called from a thread
  254. * context, normally "rmmod", "apmd", or something similar.
  255. *
  256. */
  257. static int
  258. ohci_hcd_s3c2410_remove(struct platform_device *dev)
  259. {
  260. struct usb_hcd *hcd = platform_get_drvdata(dev);
  261. usb_remove_hcd(hcd);
  262. s3c2410_stop_hc(dev);
  263. usb_put_hcd(hcd);
  264. return 0;
  265. }
  266. /**
  267. * ohci_hcd_s3c2410_probe - initialize S3C2410-based HCDs
  268. * Context: !in_interrupt()
  269. *
  270. * Allocates basic resources for this USB host controller, and
  271. * then invokes the start() method for the HCD associated with it
  272. * through the hotplug entry's driver_data.
  273. *
  274. */
  275. static int ohci_hcd_s3c2410_probe(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(&ohci_s3c2410_hc_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. device_wakeup_enable(hcd->self.controller);
  309. return 0;
  310. err_ioremap:
  311. s3c2410_stop_hc(dev);
  312. err_put:
  313. usb_put_hcd(hcd);
  314. return retval;
  315. }
  316. /*-------------------------------------------------------------------------*/
  317. #ifdef CONFIG_PM
  318. static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
  319. {
  320. struct usb_hcd *hcd = dev_get_drvdata(dev);
  321. struct platform_device *pdev = to_platform_device(dev);
  322. bool do_wakeup = device_may_wakeup(dev);
  323. int rc = 0;
  324. rc = ohci_suspend(hcd, do_wakeup);
  325. if (rc)
  326. return rc;
  327. s3c2410_stop_hc(pdev);
  328. return rc;
  329. }
  330. static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
  331. {
  332. struct usb_hcd *hcd = dev_get_drvdata(dev);
  333. struct platform_device *pdev = to_platform_device(dev);
  334. s3c2410_start_hc(pdev, hcd);
  335. ohci_resume(hcd, false);
  336. return 0;
  337. }
  338. #else
  339. #define ohci_hcd_s3c2410_drv_suspend NULL
  340. #define ohci_hcd_s3c2410_drv_resume NULL
  341. #endif
  342. static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
  343. .suspend = ohci_hcd_s3c2410_drv_suspend,
  344. .resume = ohci_hcd_s3c2410_drv_resume,
  345. };
  346. static const struct of_device_id ohci_hcd_s3c2410_dt_ids[] = {
  347. { .compatible = "samsung,s3c2410-ohci" },
  348. { /* sentinel */ }
  349. };
  350. MODULE_DEVICE_TABLE(of, ohci_hcd_s3c2410_dt_ids);
  351. static struct platform_driver ohci_hcd_s3c2410_driver = {
  352. .probe = ohci_hcd_s3c2410_probe,
  353. .remove = ohci_hcd_s3c2410_remove,
  354. .shutdown = usb_hcd_platform_shutdown,
  355. .driver = {
  356. .name = "s3c2410-ohci",
  357. .pm = &ohci_hcd_s3c2410_pm_ops,
  358. .of_match_table = ohci_hcd_s3c2410_dt_ids,
  359. },
  360. };
  361. static int __init ohci_s3c2410_init(void)
  362. {
  363. if (usb_disabled())
  364. return -ENODEV;
  365. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  366. ohci_init_driver(&ohci_s3c2410_hc_driver, NULL);
  367. /*
  368. * The Samsung HW has some unusual quirks, which require
  369. * Sumsung-specific workarounds. We override certain hc_driver
  370. * functions here to achieve that. We explicitly do not enhance
  371. * ohci_driver_overrides to allow this more easily, since this
  372. * is an unusual case, and we don't want to encourage others to
  373. * override these functions by making it too easy.
  374. */
  375. ohci_s3c2410_hc_driver.hub_status_data = ohci_s3c2410_hub_status_data;
  376. ohci_s3c2410_hc_driver.hub_control = ohci_s3c2410_hub_control;
  377. return platform_driver_register(&ohci_hcd_s3c2410_driver);
  378. }
  379. module_init(ohci_s3c2410_init);
  380. static void __exit ohci_s3c2410_cleanup(void)
  381. {
  382. platform_driver_unregister(&ohci_hcd_s3c2410_driver);
  383. }
  384. module_exit(ohci_s3c2410_cleanup);
  385. MODULE_DESCRIPTION(DRIVER_DESC);
  386. MODULE_LICENSE("GPL");
  387. MODULE_ALIAS("platform:s3c2410-ohci");