ohci-s3c2410.c 11 KB

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