ohci-s3c2410.c 11 KB

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