ohci-s3c2410.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/platform_data/usb-ohci-s3c2410.h>
  24. #define valid_port(idx) ((idx) == 1 || (idx) == 2)
  25. /* clock device associated with the hcd */
  26. static struct clk *clk;
  27. static struct clk *usb_clk;
  28. /* forward definitions */
  29. static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
  30. /* conversion functions */
  31. static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
  32. {
  33. return hcd->self.controller->platform_data;
  34. }
  35. static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
  36. {
  37. struct s3c2410_hcd_info *info = dev->dev.platform_data;
  38. dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
  39. clk_enable(usb_clk);
  40. mdelay(2); /* let the bus clock stabilise */
  41. clk_enable(clk);
  42. if (info != NULL) {
  43. info->hcd = hcd;
  44. info->report_oc = s3c2410_hcd_oc;
  45. if (info->enable_oc != NULL)
  46. (info->enable_oc)(info, 1);
  47. }
  48. }
  49. static void s3c2410_stop_hc(struct platform_device *dev)
  50. {
  51. struct s3c2410_hcd_info *info = dev->dev.platform_data;
  52. dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
  53. if (info != NULL) {
  54. info->report_oc = NULL;
  55. info->hcd = NULL;
  56. if (info->enable_oc != NULL)
  57. (info->enable_oc)(info, 0);
  58. }
  59. clk_disable(clk);
  60. clk_disable(usb_clk);
  61. }
  62. /* ohci_s3c2410_hub_status_data
  63. *
  64. * update the status data from the hub with anything that
  65. * has been detected by our system
  66. */
  67. static int
  68. ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
  69. {
  70. struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
  71. struct s3c2410_hcd_port *port;
  72. int orig;
  73. int portno;
  74. orig = ohci_hub_status_data(hcd, buf);
  75. if (info == NULL)
  76. return orig;
  77. port = &info->port[0];
  78. /* mark any changed port as changed */
  79. for (portno = 0; portno < 2; port++, portno++) {
  80. if (port->oc_changed == 1 &&
  81. port->flags & S3C_HCDFLG_USED) {
  82. dev_dbg(hcd->self.controller,
  83. "oc change on port %d\n", portno);
  84. if (orig < 1)
  85. orig = 1;
  86. buf[0] |= 1<<(portno+1);
  87. }
  88. }
  89. return orig;
  90. }
  91. /* s3c2410_usb_set_power
  92. *
  93. * configure the power on a port, by calling the platform device
  94. * routine registered with the platform device
  95. */
  96. static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
  97. int port, int to)
  98. {
  99. if (info == NULL)
  100. return;
  101. if (info->power_control != NULL) {
  102. info->port[port-1].power = to;
  103. (info->power_control)(port-1, to);
  104. }
  105. }
  106. /* ohci_s3c2410_hub_control
  107. *
  108. * look at control requests to the hub, and see if we need
  109. * to take any action or over-ride the results from the
  110. * request.
  111. */
  112. static int ohci_s3c2410_hub_control(
  113. struct usb_hcd *hcd,
  114. u16 typeReq,
  115. u16 wValue,
  116. u16 wIndex,
  117. char *buf,
  118. u16 wLength)
  119. {
  120. struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
  121. struct usb_hub_descriptor *desc;
  122. int ret = -EINVAL;
  123. u32 *data = (u32 *)buf;
  124. dev_dbg(hcd->self.controller,
  125. "s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
  126. hcd, typeReq, wValue, wIndex, buf, wLength);
  127. /* if we are only an humble host without any special capabilities
  128. * process the request straight away and exit */
  129. if (info == NULL) {
  130. ret = ohci_hub_control(hcd, typeReq, wValue,
  131. wIndex, buf, wLength);
  132. goto out;
  133. }
  134. /* check the request to see if it needs handling */
  135. switch (typeReq) {
  136. case SetPortFeature:
  137. if (wValue == USB_PORT_FEAT_POWER) {
  138. dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
  139. s3c2410_usb_set_power(info, wIndex, 1);
  140. goto out;
  141. }
  142. break;
  143. case ClearPortFeature:
  144. switch (wValue) {
  145. case USB_PORT_FEAT_C_OVER_CURRENT:
  146. dev_dbg(hcd->self.controller,
  147. "ClearPortFeature: C_OVER_CURRENT\n");
  148. if (valid_port(wIndex)) {
  149. info->port[wIndex-1].oc_changed = 0;
  150. info->port[wIndex-1].oc_status = 0;
  151. }
  152. goto out;
  153. case USB_PORT_FEAT_OVER_CURRENT:
  154. dev_dbg(hcd->self.controller,
  155. "ClearPortFeature: OVER_CURRENT\n");
  156. if (valid_port(wIndex))
  157. info->port[wIndex-1].oc_status = 0;
  158. goto out;
  159. case USB_PORT_FEAT_POWER:
  160. dev_dbg(hcd->self.controller,
  161. "ClearPortFeature: POWER\n");
  162. if (valid_port(wIndex)) {
  163. s3c2410_usb_set_power(info, wIndex, 0);
  164. return 0;
  165. }
  166. }
  167. break;
  168. }
  169. ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  170. if (ret)
  171. goto out;
  172. switch (typeReq) {
  173. case GetHubDescriptor:
  174. /* update the hub's descriptor */
  175. desc = (struct usb_hub_descriptor *)buf;
  176. if (info->power_control == NULL)
  177. return ret;
  178. dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
  179. desc->wHubCharacteristics);
  180. /* remove the old configurations for power-switching, and
  181. * over-current protection, and insert our new configuration
  182. */
  183. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
  184. desc->wHubCharacteristics |= cpu_to_le16(0x0001);
  185. if (info->enable_oc) {
  186. desc->wHubCharacteristics &= ~cpu_to_le16(
  187. HUB_CHAR_OCPM);
  188. desc->wHubCharacteristics |= cpu_to_le16(
  189. 0x0008 |
  190. 0x0001);
  191. }
  192. dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
  193. desc->wHubCharacteristics);
  194. return ret;
  195. case GetPortStatus:
  196. /* check port status */
  197. dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
  198. if (valid_port(wIndex)) {
  199. if (info->port[wIndex-1].oc_changed)
  200. *data |= cpu_to_le32(RH_PS_OCIC);
  201. if (info->port[wIndex-1].oc_status)
  202. *data |= cpu_to_le32(RH_PS_POCI);
  203. }
  204. }
  205. out:
  206. return ret;
  207. }
  208. /* s3c2410_hcd_oc
  209. *
  210. * handle an over-current report
  211. */
  212. static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
  213. {
  214. struct s3c2410_hcd_port *port;
  215. struct usb_hcd *hcd;
  216. unsigned long flags;
  217. int portno;
  218. if (info == NULL)
  219. return;
  220. port = &info->port[0];
  221. hcd = info->hcd;
  222. local_irq_save(flags);
  223. for (portno = 0; portno < 2; port++, portno++) {
  224. if (port_oc & (1<<portno) &&
  225. port->flags & S3C_HCDFLG_USED) {
  226. port->oc_status = 1;
  227. port->oc_changed = 1;
  228. /* ok, once over-current is detected,
  229. the port needs to be powered down */
  230. s3c2410_usb_set_power(info, portno+1, 0);
  231. }
  232. }
  233. local_irq_restore(flags);
  234. }
  235. /* may be called without controller electrically present */
  236. /* may be called with controller, bus, and devices active */
  237. /*
  238. * usb_hcd_s3c2410_remove - shutdown processing for HCD
  239. * @dev: USB Host Controller being removed
  240. * Context: !in_interrupt()
  241. *
  242. * Reverses the effect of usb_hcd_3c2410_probe(), first invoking
  243. * the HCD's stop() method. It is always called from a thread
  244. * context, normally "rmmod", "apmd", or something similar.
  245. *
  246. */
  247. static void
  248. usb_hcd_s3c2410_remove(struct usb_hcd *hcd, struct platform_device *dev)
  249. {
  250. usb_remove_hcd(hcd);
  251. s3c2410_stop_hc(dev);
  252. usb_put_hcd(hcd);
  253. }
  254. /**
  255. * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs
  256. * Context: !in_interrupt()
  257. *
  258. * Allocates basic resources for this USB host controller, and
  259. * then invokes the start() method for the HCD associated with it
  260. * through the hotplug entry's driver_data.
  261. *
  262. */
  263. static int usb_hcd_s3c2410_probe(const struct hc_driver *driver,
  264. struct platform_device *dev)
  265. {
  266. struct usb_hcd *hcd = NULL;
  267. int retval;
  268. s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
  269. s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
  270. hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
  271. if (hcd == NULL)
  272. return -ENOMEM;
  273. hcd->rsrc_start = dev->resource[0].start;
  274. hcd->rsrc_len = resource_size(&dev->resource[0]);
  275. hcd->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]);
  276. if (IS_ERR(hcd->regs)) {
  277. retval = PTR_ERR(hcd->regs);
  278. goto err_put;
  279. }
  280. clk = devm_clk_get(&dev->dev, "usb-host");
  281. if (IS_ERR(clk)) {
  282. dev_err(&dev->dev, "cannot get usb-host clock\n");
  283. retval = PTR_ERR(clk);
  284. goto err_put;
  285. }
  286. usb_clk = devm_clk_get(&dev->dev, "usb-bus-host");
  287. if (IS_ERR(usb_clk)) {
  288. dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
  289. retval = PTR_ERR(usb_clk);
  290. goto err_put;
  291. }
  292. s3c2410_start_hc(dev, hcd);
  293. ohci_hcd_init(hcd_to_ohci(hcd));
  294. retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
  295. if (retval != 0)
  296. goto err_ioremap;
  297. return 0;
  298. err_ioremap:
  299. s3c2410_stop_hc(dev);
  300. err_put:
  301. usb_put_hcd(hcd);
  302. return retval;
  303. }
  304. /*-------------------------------------------------------------------------*/
  305. static int
  306. ohci_s3c2410_start(struct usb_hcd *hcd)
  307. {
  308. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  309. int ret;
  310. ret = ohci_init(ohci);
  311. if (ret < 0)
  312. return ret;
  313. ret = ohci_run(ohci);
  314. if (ret < 0) {
  315. dev_err(hcd->self.controller, "can't start %s\n",
  316. hcd->self.bus_name);
  317. ohci_stop(hcd);
  318. return ret;
  319. }
  320. return 0;
  321. }
  322. static const struct hc_driver ohci_s3c2410_hc_driver = {
  323. .description = hcd_name,
  324. .product_desc = "S3C24XX OHCI",
  325. .hcd_priv_size = sizeof(struct ohci_hcd),
  326. /*
  327. * generic hardware linkage
  328. */
  329. .irq = ohci_irq,
  330. .flags = HCD_USB11 | HCD_MEMORY,
  331. /*
  332. * basic lifecycle operations
  333. */
  334. .start = ohci_s3c2410_start,
  335. .stop = ohci_stop,
  336. .shutdown = ohci_shutdown,
  337. /*
  338. * managing i/o requests and associated device resources
  339. */
  340. .urb_enqueue = ohci_urb_enqueue,
  341. .urb_dequeue = ohci_urb_dequeue,
  342. .endpoint_disable = ohci_endpoint_disable,
  343. /*
  344. * scheduling support
  345. */
  346. .get_frame_number = ohci_get_frame,
  347. /*
  348. * root hub support
  349. */
  350. .hub_status_data = ohci_s3c2410_hub_status_data,
  351. .hub_control = ohci_s3c2410_hub_control,
  352. #ifdef CONFIG_PM
  353. .bus_suspend = ohci_bus_suspend,
  354. .bus_resume = ohci_bus_resume,
  355. #endif
  356. .start_port_reset = ohci_start_port_reset,
  357. };
  358. /* device driver */
  359. static int ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
  360. {
  361. return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
  362. }
  363. static int ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
  364. {
  365. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  366. usb_hcd_s3c2410_remove(hcd, pdev);
  367. return 0;
  368. }
  369. #ifdef CONFIG_PM
  370. static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
  371. {
  372. struct usb_hcd *hcd = dev_get_drvdata(dev);
  373. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  374. struct platform_device *pdev = to_platform_device(dev);
  375. unsigned long flags;
  376. int rc = 0;
  377. /*
  378. * Root hub was already suspended. Disable irq emission and
  379. * mark HW unaccessible, bail out if RH has been resumed. Use
  380. * the spinlock to properly synchronize with possible pending
  381. * RH suspend or resume activity.
  382. */
  383. spin_lock_irqsave(&ohci->lock, flags);
  384. if (ohci->rh_state != OHCI_RH_SUSPENDED) {
  385. rc = -EINVAL;
  386. goto bail;
  387. }
  388. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  389. s3c2410_stop_hc(pdev);
  390. bail:
  391. spin_unlock_irqrestore(&ohci->lock, flags);
  392. return rc;
  393. }
  394. static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
  395. {
  396. struct usb_hcd *hcd = dev_get_drvdata(dev);
  397. struct platform_device *pdev = to_platform_device(dev);
  398. s3c2410_start_hc(pdev, hcd);
  399. ohci_resume(hcd, false);
  400. return 0;
  401. }
  402. #else
  403. #define ohci_hcd_s3c2410_drv_suspend NULL
  404. #define ohci_hcd_s3c2410_drv_resume NULL
  405. #endif
  406. static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
  407. .suspend = ohci_hcd_s3c2410_drv_suspend,
  408. .resume = ohci_hcd_s3c2410_drv_resume,
  409. };
  410. static struct platform_driver ohci_hcd_s3c2410_driver = {
  411. .probe = ohci_hcd_s3c2410_drv_probe,
  412. .remove = ohci_hcd_s3c2410_drv_remove,
  413. .shutdown = usb_hcd_platform_shutdown,
  414. .driver = {
  415. .owner = THIS_MODULE,
  416. .name = "s3c2410-ohci",
  417. .pm = &ohci_hcd_s3c2410_pm_ops,
  418. },
  419. };
  420. MODULE_ALIAS("platform:s3c2410-ohci");