ohci-s3c2410.c 11 KB

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