fhci-hub.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Freescale QUICC Engine USB Host Controller Driver
  3. *
  4. * Copyright (c) Freescale Semicondutor, Inc. 2006.
  5. * Shlomi Gridish <gridish@freescale.com>
  6. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  7. * Copyright (c) Logic Product Development, Inc. 2007
  8. * Peter Barada <peterb@logicpd.com>
  9. * Copyright (c) MontaVista Software, Inc. 2008.
  10. * Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/io.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/hcd.h>
  25. #include <linux/gpio.h>
  26. #include <asm/qe.h>
  27. #include "fhci.h"
  28. /* virtual root hub specific descriptor */
  29. static u8 root_hub_des[] = {
  30. 0x09, /* blength */
  31. 0x29, /* bDescriptorType;hub-descriptor */
  32. 0x01, /* bNbrPorts */
  33. 0x00, /* wHubCharacteristics */
  34. 0x00,
  35. 0x01, /* bPwrOn2pwrGood;2ms */
  36. 0x00, /* bHubContrCurrent;0mA */
  37. 0x00, /* DeviceRemoveable */
  38. 0xff, /* PortPwrCtrlMask */
  39. };
  40. static void fhci_gpio_set_value(struct fhci_hcd *fhci, int gpio_nr, bool on)
  41. {
  42. int gpio = fhci->gpios[gpio_nr];
  43. bool alow = fhci->alow_gpios[gpio_nr];
  44. if (!gpio_is_valid(gpio))
  45. return;
  46. gpio_set_value(gpio, on ^ alow);
  47. mdelay(5);
  48. }
  49. void fhci_config_transceiver(struct fhci_hcd *fhci,
  50. enum fhci_port_status status)
  51. {
  52. fhci_dbg(fhci, "-> %s: %d\n", __func__, status);
  53. switch (status) {
  54. case FHCI_PORT_POWER_OFF:
  55. fhci_gpio_set_value(fhci, GPIO_POWER, false);
  56. break;
  57. case FHCI_PORT_DISABLED:
  58. case FHCI_PORT_WAITING:
  59. fhci_gpio_set_value(fhci, GPIO_POWER, true);
  60. break;
  61. case FHCI_PORT_LOW:
  62. fhci_gpio_set_value(fhci, GPIO_SPEED, false);
  63. break;
  64. case FHCI_PORT_FULL:
  65. fhci_gpio_set_value(fhci, GPIO_SPEED, true);
  66. break;
  67. default:
  68. WARN_ON(1);
  69. break;
  70. }
  71. fhci_dbg(fhci, "<- %s: %d\n", __func__, status);
  72. }
  73. /* disable the USB port by clearing the EN bit in the USBMOD register */
  74. void fhci_port_disable(struct fhci_hcd *fhci)
  75. {
  76. struct fhci_usb *usb = (struct fhci_usb *)fhci->usb_lld;
  77. enum fhci_port_status port_status;
  78. fhci_dbg(fhci, "-> %s\n", __func__);
  79. fhci_stop_sof_timer(fhci);
  80. fhci_flush_all_transmissions(usb);
  81. fhci_usb_disable_interrupt((struct fhci_usb *)fhci->usb_lld);
  82. port_status = usb->port_status;
  83. usb->port_status = FHCI_PORT_DISABLED;
  84. /* Enable IDLE since we want to know if something comes along */
  85. usb->saved_msk |= USB_E_IDLE_MASK;
  86. out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
  87. /* check if during the disconnection process attached new device */
  88. if (port_status == FHCI_PORT_WAITING)
  89. fhci_device_connected_interrupt(fhci);
  90. usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_ENABLE;
  91. usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_ENABLE;
  92. fhci_usb_enable_interrupt((struct fhci_usb *)fhci->usb_lld);
  93. fhci_dbg(fhci, "<- %s\n", __func__);
  94. }
  95. /* enable the USB port by setting the EN bit in the USBMOD register */
  96. void fhci_port_enable(void *lld)
  97. {
  98. struct fhci_usb *usb = (struct fhci_usb *)lld;
  99. struct fhci_hcd *fhci = usb->fhci;
  100. fhci_dbg(fhci, "-> %s\n", __func__);
  101. fhci_config_transceiver(fhci, usb->port_status);
  102. if ((usb->port_status != FHCI_PORT_FULL) &&
  103. (usb->port_status != FHCI_PORT_LOW))
  104. fhci_start_sof_timer(fhci);
  105. usb->vroot_hub->port.wPortStatus |= USB_PORT_STAT_ENABLE;
  106. usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_ENABLE;
  107. fhci_dbg(fhci, "<- %s\n", __func__);
  108. }
  109. void fhci_io_port_generate_reset(struct fhci_hcd *fhci)
  110. {
  111. fhci_dbg(fhci, "-> %s\n", __func__);
  112. gpio_direction_output(fhci->gpios[GPIO_USBOE], 0);
  113. gpio_direction_output(fhci->gpios[GPIO_USBTP], 0);
  114. gpio_direction_output(fhci->gpios[GPIO_USBTN], 0);
  115. mdelay(5);
  116. qe_pin_set_dedicated(fhci->pins[PIN_USBOE]);
  117. qe_pin_set_dedicated(fhci->pins[PIN_USBTP]);
  118. qe_pin_set_dedicated(fhci->pins[PIN_USBTN]);
  119. fhci_dbg(fhci, "<- %s\n", __func__);
  120. }
  121. /* generate the RESET condition on the bus */
  122. void fhci_port_reset(void *lld)
  123. {
  124. struct fhci_usb *usb = (struct fhci_usb *)lld;
  125. struct fhci_hcd *fhci = usb->fhci;
  126. u8 mode;
  127. u16 mask;
  128. fhci_dbg(fhci, "-> %s\n", __func__);
  129. fhci_stop_sof_timer(fhci);
  130. /* disable the USB controller */
  131. mode = in_8(&fhci->regs->usb_mod);
  132. out_8(&fhci->regs->usb_mod, mode & (~USB_MODE_EN));
  133. /* disable idle interrupts */
  134. mask = in_be16(&fhci->regs->usb_mask);
  135. out_be16(&fhci->regs->usb_mask, mask & (~USB_E_IDLE_MASK));
  136. fhci_io_port_generate_reset(fhci);
  137. /* enable interrupt on this endpoint */
  138. out_be16(&fhci->regs->usb_mask, mask);
  139. /* enable the USB controller */
  140. mode = in_8(&fhci->regs->usb_mod);
  141. out_8(&fhci->regs->usb_mod, mode | USB_MODE_EN);
  142. fhci_start_sof_timer(fhci);
  143. fhci_dbg(fhci, "<- %s\n", __func__);
  144. }
  145. int fhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  146. {
  147. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  148. int ret = 0;
  149. unsigned long flags;
  150. fhci_dbg(fhci, "-> %s\n", __func__);
  151. spin_lock_irqsave(&fhci->lock, flags);
  152. if (fhci->vroot_hub->port.wPortChange & (USB_PORT_STAT_C_CONNECTION |
  153. USB_PORT_STAT_C_ENABLE | USB_PORT_STAT_C_SUSPEND |
  154. USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_OVERCURRENT)) {
  155. *buf = 1 << 1;
  156. ret = 1;
  157. fhci_dbg(fhci, "-- %s\n", __func__);
  158. }
  159. spin_unlock_irqrestore(&fhci->lock, flags);
  160. fhci_dbg(fhci, "<- %s\n", __func__);
  161. return ret;
  162. }
  163. int fhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  164. u16 wIndex, char *buf, u16 wLength)
  165. {
  166. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  167. int retval = 0;
  168. int len = 0;
  169. struct usb_hub_status *hub_status;
  170. struct usb_port_status *port_status;
  171. unsigned long flags;
  172. spin_lock_irqsave(&fhci->lock, flags);
  173. fhci_dbg(fhci, "-> %s\n", __func__);
  174. switch (typeReq) {
  175. case ClearHubFeature:
  176. switch (wValue) {
  177. case C_HUB_LOCAL_POWER:
  178. case C_HUB_OVER_CURRENT:
  179. break;
  180. default:
  181. goto error;
  182. }
  183. break;
  184. case ClearPortFeature:
  185. fhci->vroot_hub->feature &= (1 << wValue);
  186. switch (wValue) {
  187. case USB_PORT_FEAT_ENABLE:
  188. fhci->vroot_hub->port.wPortStatus &=
  189. ~USB_PORT_STAT_ENABLE;
  190. fhci_port_disable(fhci);
  191. break;
  192. case USB_PORT_FEAT_C_ENABLE:
  193. fhci->vroot_hub->port.wPortChange &=
  194. ~USB_PORT_STAT_C_ENABLE;
  195. break;
  196. case USB_PORT_FEAT_SUSPEND:
  197. fhci->vroot_hub->port.wPortStatus &=
  198. ~USB_PORT_STAT_SUSPEND;
  199. fhci_stop_sof_timer(fhci);
  200. break;
  201. case USB_PORT_FEAT_C_SUSPEND:
  202. fhci->vroot_hub->port.wPortChange &=
  203. ~USB_PORT_STAT_C_SUSPEND;
  204. break;
  205. case USB_PORT_FEAT_POWER:
  206. fhci->vroot_hub->port.wPortStatus &=
  207. ~USB_PORT_STAT_POWER;
  208. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  209. break;
  210. case USB_PORT_FEAT_C_CONNECTION:
  211. fhci->vroot_hub->port.wPortChange &=
  212. ~USB_PORT_STAT_C_CONNECTION;
  213. break;
  214. case USB_PORT_FEAT_C_OVER_CURRENT:
  215. fhci->vroot_hub->port.wPortChange &=
  216. ~USB_PORT_STAT_C_OVERCURRENT;
  217. break;
  218. case USB_PORT_FEAT_C_RESET:
  219. fhci->vroot_hub->port.wPortChange &=
  220. ~USB_PORT_STAT_C_RESET;
  221. break;
  222. default:
  223. goto error;
  224. }
  225. break;
  226. case GetHubDescriptor:
  227. memcpy(buf, root_hub_des, sizeof(root_hub_des));
  228. buf[3] = 0x11; /* per-port power, no ovrcrnt */
  229. len = (buf[0] < wLength) ? buf[0] : wLength;
  230. break;
  231. case GetHubStatus:
  232. hub_status = (struct usb_hub_status *)buf;
  233. hub_status->wHubStatus =
  234. cpu_to_le16(fhci->vroot_hub->hub.wHubStatus);
  235. hub_status->wHubChange =
  236. cpu_to_le16(fhci->vroot_hub->hub.wHubChange);
  237. len = 4;
  238. break;
  239. case GetPortStatus:
  240. port_status = (struct usb_port_status *)buf;
  241. port_status->wPortStatus =
  242. cpu_to_le16(fhci->vroot_hub->port.wPortStatus);
  243. port_status->wPortChange =
  244. cpu_to_le16(fhci->vroot_hub->port.wPortChange);
  245. len = 4;
  246. break;
  247. case SetHubFeature:
  248. switch (wValue) {
  249. case C_HUB_OVER_CURRENT:
  250. case C_HUB_LOCAL_POWER:
  251. break;
  252. default:
  253. goto error;
  254. }
  255. break;
  256. case SetPortFeature:
  257. fhci->vroot_hub->feature |= (1 << wValue);
  258. switch (wValue) {
  259. case USB_PORT_FEAT_ENABLE:
  260. fhci->vroot_hub->port.wPortStatus |=
  261. USB_PORT_STAT_ENABLE;
  262. fhci_port_enable(fhci->usb_lld);
  263. break;
  264. case USB_PORT_FEAT_SUSPEND:
  265. fhci->vroot_hub->port.wPortStatus |=
  266. USB_PORT_STAT_SUSPEND;
  267. fhci_stop_sof_timer(fhci);
  268. break;
  269. case USB_PORT_FEAT_RESET:
  270. fhci->vroot_hub->port.wPortStatus |=
  271. USB_PORT_STAT_RESET;
  272. fhci_port_reset(fhci->usb_lld);
  273. fhci->vroot_hub->port.wPortStatus |=
  274. USB_PORT_STAT_ENABLE;
  275. fhci->vroot_hub->port.wPortStatus &=
  276. ~USB_PORT_STAT_RESET;
  277. break;
  278. case USB_PORT_FEAT_POWER:
  279. fhci->vroot_hub->port.wPortStatus |=
  280. USB_PORT_STAT_POWER;
  281. fhci_config_transceiver(fhci, FHCI_PORT_WAITING);
  282. break;
  283. default:
  284. goto error;
  285. }
  286. break;
  287. default:
  288. error:
  289. retval = -EPIPE;
  290. }
  291. fhci_dbg(fhci, "<- %s\n", __func__);
  292. spin_unlock_irqrestore(&fhci->lock, flags);
  293. return retval;
  294. }