ohci-s3c2410.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. unsigned long flags;
  226. int portno;
  227. if (info == NULL)
  228. return;
  229. port = &info->port[0];
  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. * ohci_hcd_s3c2410_remove - shutdown processing for HCD
  247. * @dev: USB Host Controller being removed
  248. * Context: !in_interrupt()
  249. *
  250. * Reverses the effect of ohci_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 int
  256. ohci_hcd_s3c2410_remove(struct platform_device *dev)
  257. {
  258. struct usb_hcd *hcd = platform_get_drvdata(dev);
  259. usb_remove_hcd(hcd);
  260. s3c2410_stop_hc(dev);
  261. usb_put_hcd(hcd);
  262. return 0;
  263. }
  264. /**
  265. * ohci_hcd_s3c2410_probe - initialize S3C2410-based HCDs
  266. * Context: !in_interrupt()
  267. *
  268. * Allocates basic resources for this USB host controller, and
  269. * then invokes the start() method for the HCD associated with it
  270. * through the hotplug entry's driver_data.
  271. *
  272. */
  273. static int ohci_hcd_s3c2410_probe(struct platform_device *dev)
  274. {
  275. struct usb_hcd *hcd = NULL;
  276. struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
  277. int retval;
  278. s3c2410_usb_set_power(info, 1, 1);
  279. s3c2410_usb_set_power(info, 2, 1);
  280. hcd = usb_create_hcd(&ohci_s3c2410_hc_driver, &dev->dev, "s3c24xx");
  281. if (hcd == NULL)
  282. return -ENOMEM;
  283. hcd->rsrc_start = dev->resource[0].start;
  284. hcd->rsrc_len = resource_size(&dev->resource[0]);
  285. hcd->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]);
  286. if (IS_ERR(hcd->regs)) {
  287. retval = PTR_ERR(hcd->regs);
  288. goto err_put;
  289. }
  290. clk = devm_clk_get(&dev->dev, "usb-host");
  291. if (IS_ERR(clk)) {
  292. dev_err(&dev->dev, "cannot get usb-host clock\n");
  293. retval = PTR_ERR(clk);
  294. goto err_put;
  295. }
  296. usb_clk = devm_clk_get(&dev->dev, "usb-bus-host");
  297. if (IS_ERR(usb_clk)) {
  298. dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
  299. retval = PTR_ERR(usb_clk);
  300. goto err_put;
  301. }
  302. s3c2410_start_hc(dev, hcd);
  303. retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
  304. if (retval != 0)
  305. goto err_ioremap;
  306. device_wakeup_enable(hcd->self.controller);
  307. return 0;
  308. err_ioremap:
  309. s3c2410_stop_hc(dev);
  310. err_put:
  311. usb_put_hcd(hcd);
  312. return retval;
  313. }
  314. /*-------------------------------------------------------------------------*/
  315. #ifdef CONFIG_PM
  316. static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
  317. {
  318. struct usb_hcd *hcd = dev_get_drvdata(dev);
  319. struct platform_device *pdev = to_platform_device(dev);
  320. bool do_wakeup = device_may_wakeup(dev);
  321. int rc = 0;
  322. rc = ohci_suspend(hcd, do_wakeup);
  323. if (rc)
  324. return rc;
  325. s3c2410_stop_hc(pdev);
  326. return rc;
  327. }
  328. static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
  329. {
  330. struct usb_hcd *hcd = dev_get_drvdata(dev);
  331. struct platform_device *pdev = to_platform_device(dev);
  332. s3c2410_start_hc(pdev, hcd);
  333. ohci_resume(hcd, false);
  334. return 0;
  335. }
  336. #else
  337. #define ohci_hcd_s3c2410_drv_suspend NULL
  338. #define ohci_hcd_s3c2410_drv_resume NULL
  339. #endif
  340. static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
  341. .suspend = ohci_hcd_s3c2410_drv_suspend,
  342. .resume = ohci_hcd_s3c2410_drv_resume,
  343. };
  344. static const struct of_device_id ohci_hcd_s3c2410_dt_ids[] = {
  345. { .compatible = "samsung,s3c2410-ohci" },
  346. { /* sentinel */ }
  347. };
  348. MODULE_DEVICE_TABLE(of, ohci_hcd_s3c2410_dt_ids);
  349. static struct platform_driver ohci_hcd_s3c2410_driver = {
  350. .probe = ohci_hcd_s3c2410_probe,
  351. .remove = ohci_hcd_s3c2410_remove,
  352. .shutdown = usb_hcd_platform_shutdown,
  353. .driver = {
  354. .name = "s3c2410-ohci",
  355. .pm = &ohci_hcd_s3c2410_pm_ops,
  356. .of_match_table = ohci_hcd_s3c2410_dt_ids,
  357. },
  358. };
  359. static int __init ohci_s3c2410_init(void)
  360. {
  361. if (usb_disabled())
  362. return -ENODEV;
  363. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  364. ohci_init_driver(&ohci_s3c2410_hc_driver, NULL);
  365. /*
  366. * The Samsung HW has some unusual quirks, which require
  367. * Sumsung-specific workarounds. We override certain hc_driver
  368. * functions here to achieve that. We explicitly do not enhance
  369. * ohci_driver_overrides to allow this more easily, since this
  370. * is an unusual case, and we don't want to encourage others to
  371. * override these functions by making it too easy.
  372. */
  373. ohci_s3c2410_hc_driver.hub_status_data = ohci_s3c2410_hub_status_data;
  374. ohci_s3c2410_hc_driver.hub_control = ohci_s3c2410_hub_control;
  375. return platform_driver_register(&ohci_hcd_s3c2410_driver);
  376. }
  377. module_init(ohci_s3c2410_init);
  378. static void __exit ohci_s3c2410_cleanup(void)
  379. {
  380. platform_driver_unregister(&ohci_hcd_s3c2410_driver);
  381. }
  382. module_exit(ohci_s3c2410_cleanup);
  383. MODULE_DESCRIPTION(DRIVER_DESC);
  384. MODULE_LICENSE("GPL");
  385. MODULE_ALIAS("platform:s3c2410-ohci");