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 <[email protected]>
  6. * (C) Copyright 2000-2002 David Brownell <[email protected]>
  7. * (C) Copyright 2002 Hewlett-Packard Company
  8. *
  9. * USB Bus Glue for Samsung S3C2410
  10. *
  11. * Written by Christopher Hoover <[email protected]>
  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, <[email protected]>
  16. * Copyright (C) 2004 Simtec Electronics
  17. *
  18. * Thanks to [email protected] 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 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. unsigned long flags;
  225. int portno;
  226. if (info == NULL)
  227. return;
  228. port = &info->port[0];
  229. local_irq_save(flags);
  230. for (portno = 0; portno < 2; port++, portno++) {
  231. if (port_oc & (1<<portno) &&
  232. port->flags & S3C_HCDFLG_USED) {
  233. port->oc_status = 1;
  234. port->oc_changed = 1;
  235. /* ok, once over-current is detected,
  236. the port needs to be powered down */
  237. s3c2410_usb_set_power(info, portno+1, 0);
  238. }
  239. }
  240. local_irq_restore(flags);
  241. }
  242. /* may be called without controller electrically present */
  243. /* may be called with controller, bus, and devices active */
  244. /*
  245. * ohci_hcd_s3c2410_remove - shutdown processing for HCD
  246. * @dev: USB Host Controller being removed
  247. *
  248. * Context: task context, might sleep
  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. static int
  255. ohci_hcd_s3c2410_remove(struct platform_device *dev)
  256. {
  257. struct usb_hcd *hcd = platform_get_drvdata(dev);
  258. usb_remove_hcd(hcd);
  259. s3c2410_stop_hc(dev);
  260. usb_put_hcd(hcd);
  261. return 0;
  262. }
  263. /*
  264. * ohci_hcd_s3c2410_probe - initialize S3C2410-based HCDs
  265. * @dev: USB Host Controller to be probed
  266. *
  267. * Context: task context, might sleep
  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. 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, irq;
  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. irq = platform_get_irq(dev, 0);
  303. if (irq < 0) {
  304. retval = irq;
  305. goto err_put;
  306. }
  307. s3c2410_start_hc(dev, hcd);
  308. retval = usb_add_hcd(hcd, irq, 0);
  309. if (retval != 0)
  310. goto err_ioremap;
  311. device_wakeup_enable(hcd->self.controller);
  312. return 0;
  313. err_ioremap:
  314. s3c2410_stop_hc(dev);
  315. err_put:
  316. usb_put_hcd(hcd);
  317. return retval;
  318. }
  319. /*-------------------------------------------------------------------------*/
  320. #ifdef CONFIG_PM
  321. static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
  322. {
  323. struct usb_hcd *hcd = dev_get_drvdata(dev);
  324. struct platform_device *pdev = to_platform_device(dev);
  325. bool do_wakeup = device_may_wakeup(dev);
  326. int rc = 0;
  327. rc = ohci_suspend(hcd, do_wakeup);
  328. if (rc)
  329. return rc;
  330. s3c2410_stop_hc(pdev);
  331. return rc;
  332. }
  333. static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
  334. {
  335. struct usb_hcd *hcd = dev_get_drvdata(dev);
  336. struct platform_device *pdev = to_platform_device(dev);
  337. s3c2410_start_hc(pdev, hcd);
  338. ohci_resume(hcd, false);
  339. return 0;
  340. }
  341. #else
  342. #define ohci_hcd_s3c2410_drv_suspend NULL
  343. #define ohci_hcd_s3c2410_drv_resume NULL
  344. #endif
  345. static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
  346. .suspend = ohci_hcd_s3c2410_drv_suspend,
  347. .resume = ohci_hcd_s3c2410_drv_resume,
  348. };
  349. static const struct of_device_id ohci_hcd_s3c2410_dt_ids[] = {
  350. { .compatible = "samsung,s3c2410-ohci" },
  351. { /* sentinel */ }
  352. };
  353. MODULE_DEVICE_TABLE(of, ohci_hcd_s3c2410_dt_ids);
  354. static struct platform_driver ohci_hcd_s3c2410_driver = {
  355. .probe = ohci_hcd_s3c2410_probe,
  356. .remove = ohci_hcd_s3c2410_remove,
  357. .shutdown = usb_hcd_platform_shutdown,
  358. .driver = {
  359. .name = "s3c2410-ohci",
  360. .pm = &ohci_hcd_s3c2410_pm_ops,
  361. .of_match_table = ohci_hcd_s3c2410_dt_ids,
  362. },
  363. };
  364. static int __init ohci_s3c2410_init(void)
  365. {
  366. if (usb_disabled())
  367. return -ENODEV;
  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");