fhci-hub.c 8.6 KB

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