ohci-s3c2410.c 11 KB

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