fhci-hub.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <[email protected]>
  7. * Jerry Huang <[email protected]>
  8. * Copyright (c) Logic Product Development, Inc. 2007
  9. * Peter Barada <[email protected]>
  10. * Copyright (c) MontaVista Software, Inc. 2008.
  11. * Anton Vorontsov <[email protected]>
  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/consumer.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. struct gpio_desc *gpiod = fhci->gpiods[gpio_nr];
  39. if (!gpiod)
  40. return;
  41. gpiod_set_value(gpiod, on);
  42. mdelay(5);
  43. }
  44. void fhci_config_transceiver(struct fhci_hcd *fhci,
  45. enum fhci_port_status status)
  46. {
  47. fhci_dbg(fhci, "-> %s: %d\n", __func__, status);
  48. switch (status) {
  49. case FHCI_PORT_POWER_OFF:
  50. fhci_gpio_set_value(fhci, GPIO_POWER, false);
  51. break;
  52. case FHCI_PORT_DISABLED:
  53. case FHCI_PORT_WAITING:
  54. fhci_gpio_set_value(fhci, GPIO_POWER, true);
  55. break;
  56. case FHCI_PORT_LOW:
  57. fhci_gpio_set_value(fhci, GPIO_SPEED, false);
  58. break;
  59. case FHCI_PORT_FULL:
  60. fhci_gpio_set_value(fhci, GPIO_SPEED, true);
  61. break;
  62. default:
  63. WARN_ON(1);
  64. break;
  65. }
  66. fhci_dbg(fhci, "<- %s: %d\n", __func__, status);
  67. }
  68. /* disable the USB port by clearing the EN bit in the USBMOD register */
  69. void fhci_port_disable(struct fhci_hcd *fhci)
  70. {
  71. struct fhci_usb *usb = (struct fhci_usb *)fhci->usb_lld;
  72. enum fhci_port_status port_status;
  73. fhci_dbg(fhci, "-> %s\n", __func__);
  74. fhci_stop_sof_timer(fhci);
  75. fhci_flush_all_transmissions(usb);
  76. fhci_usb_disable_interrupt((struct fhci_usb *)fhci->usb_lld);
  77. port_status = usb->port_status;
  78. usb->port_status = FHCI_PORT_DISABLED;
  79. /* Enable IDLE since we want to know if something comes along */
  80. usb->saved_msk |= USB_E_IDLE_MASK;
  81. out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
  82. /* check if during the disconnection process attached new device */
  83. if (port_status == FHCI_PORT_WAITING)
  84. fhci_device_connected_interrupt(fhci);
  85. usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_ENABLE;
  86. usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_ENABLE;
  87. fhci_usb_enable_interrupt((struct fhci_usb *)fhci->usb_lld);
  88. fhci_dbg(fhci, "<- %s\n", __func__);
  89. }
  90. /* enable the USB port by setting the EN bit in the USBMOD register */
  91. void fhci_port_enable(void *lld)
  92. {
  93. struct fhci_usb *usb = (struct fhci_usb *)lld;
  94. struct fhci_hcd *fhci = usb->fhci;
  95. fhci_dbg(fhci, "-> %s\n", __func__);
  96. fhci_config_transceiver(fhci, usb->port_status);
  97. if ((usb->port_status != FHCI_PORT_FULL) &&
  98. (usb->port_status != FHCI_PORT_LOW))
  99. fhci_start_sof_timer(fhci);
  100. usb->vroot_hub->port.wPortStatus |= USB_PORT_STAT_ENABLE;
  101. usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_ENABLE;
  102. fhci_dbg(fhci, "<- %s\n", __func__);
  103. }
  104. void fhci_io_port_generate_reset(struct fhci_hcd *fhci)
  105. {
  106. fhci_dbg(fhci, "-> %s\n", __func__);
  107. gpiod_direction_output(fhci->gpiods[GPIO_USBOE], 0);
  108. gpiod_direction_output(fhci->gpiods[GPIO_USBTP], 0);
  109. gpiod_direction_output(fhci->gpiods[GPIO_USBTN], 0);
  110. mdelay(5);
  111. qe_pin_set_dedicated(fhci->pins[PIN_USBOE]);
  112. qe_pin_set_dedicated(fhci->pins[PIN_USBTP]);
  113. qe_pin_set_dedicated(fhci->pins[PIN_USBTN]);
  114. fhci_dbg(fhci, "<- %s\n", __func__);
  115. }
  116. /* generate the RESET condition on the bus */
  117. void fhci_port_reset(void *lld)
  118. {
  119. struct fhci_usb *usb = (struct fhci_usb *)lld;
  120. struct fhci_hcd *fhci = usb->fhci;
  121. u8 mode;
  122. u16 mask;
  123. fhci_dbg(fhci, "-> %s\n", __func__);
  124. fhci_stop_sof_timer(fhci);
  125. /* disable the USB controller */
  126. mode = in_8(&fhci->regs->usb_usmod);
  127. out_8(&fhci->regs->usb_usmod, mode & (~USB_MODE_EN));
  128. /* disable idle interrupts */
  129. mask = in_be16(&fhci->regs->usb_usbmr);
  130. out_be16(&fhci->regs->usb_usbmr, mask & (~USB_E_IDLE_MASK));
  131. fhci_io_port_generate_reset(fhci);
  132. /* enable interrupt on this endpoint */
  133. out_be16(&fhci->regs->usb_usbmr, mask);
  134. /* enable the USB controller */
  135. mode = in_8(&fhci->regs->usb_usmod);
  136. out_8(&fhci->regs->usb_usmod, mode | USB_MODE_EN);
  137. fhci_start_sof_timer(fhci);
  138. fhci_dbg(fhci, "<- %s\n", __func__);
  139. }
  140. int fhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  141. {
  142. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  143. int ret = 0;
  144. unsigned long flags;
  145. fhci_dbg(fhci, "-> %s\n", __func__);
  146. spin_lock_irqsave(&fhci->lock, flags);
  147. if (fhci->vroot_hub->port.wPortChange & (USB_PORT_STAT_C_CONNECTION |
  148. USB_PORT_STAT_C_ENABLE | USB_PORT_STAT_C_SUSPEND |
  149. USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_OVERCURRENT)) {
  150. *buf = 1 << 1;
  151. ret = 1;
  152. fhci_dbg(fhci, "-- %s\n", __func__);
  153. }
  154. spin_unlock_irqrestore(&fhci->lock, flags);
  155. fhci_dbg(fhci, "<- %s\n", __func__);
  156. return ret;
  157. }
  158. int fhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  159. u16 wIndex, char *buf, u16 wLength)
  160. {
  161. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  162. int retval = 0;
  163. struct usb_hub_status *hub_status;
  164. struct usb_port_status *port_status;
  165. unsigned long flags;
  166. spin_lock_irqsave(&fhci->lock, flags);
  167. fhci_dbg(fhci, "-> %s\n", __func__);
  168. switch (typeReq) {
  169. case ClearHubFeature:
  170. switch (wValue) {
  171. case C_HUB_LOCAL_POWER:
  172. case C_HUB_OVER_CURRENT:
  173. break;
  174. default:
  175. goto error;
  176. }
  177. break;
  178. case ClearPortFeature:
  179. fhci->vroot_hub->feature &= (1 << wValue);
  180. switch (wValue) {
  181. case USB_PORT_FEAT_ENABLE:
  182. fhci->vroot_hub->port.wPortStatus &=
  183. ~USB_PORT_STAT_ENABLE;
  184. fhci_port_disable(fhci);
  185. break;
  186. case USB_PORT_FEAT_C_ENABLE:
  187. fhci->vroot_hub->port.wPortChange &=
  188. ~USB_PORT_STAT_C_ENABLE;
  189. break;
  190. case USB_PORT_FEAT_SUSPEND:
  191. fhci->vroot_hub->port.wPortStatus &=
  192. ~USB_PORT_STAT_SUSPEND;
  193. fhci_stop_sof_timer(fhci);
  194. break;
  195. case USB_PORT_FEAT_C_SUSPEND:
  196. fhci->vroot_hub->port.wPortChange &=
  197. ~USB_PORT_STAT_C_SUSPEND;
  198. break;
  199. case USB_PORT_FEAT_POWER:
  200. fhci->vroot_hub->port.wPortStatus &=
  201. ~USB_PORT_STAT_POWER;
  202. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  203. break;
  204. case USB_PORT_FEAT_C_CONNECTION:
  205. fhci->vroot_hub->port.wPortChange &=
  206. ~USB_PORT_STAT_C_CONNECTION;
  207. break;
  208. case USB_PORT_FEAT_C_OVER_CURRENT:
  209. fhci->vroot_hub->port.wPortChange &=
  210. ~USB_PORT_STAT_C_OVERCURRENT;
  211. break;
  212. case USB_PORT_FEAT_C_RESET:
  213. fhci->vroot_hub->port.wPortChange &=
  214. ~USB_PORT_STAT_C_RESET;
  215. break;
  216. default:
  217. goto error;
  218. }
  219. break;
  220. case GetHubDescriptor:
  221. memcpy(buf, root_hub_des, sizeof(root_hub_des));
  222. break;
  223. case GetHubStatus:
  224. hub_status = (struct usb_hub_status *)buf;
  225. hub_status->wHubStatus =
  226. cpu_to_le16(fhci->vroot_hub->hub.wHubStatus);
  227. hub_status->wHubChange =
  228. cpu_to_le16(fhci->vroot_hub->hub.wHubChange);
  229. break;
  230. case GetPortStatus:
  231. port_status = (struct usb_port_status *)buf;
  232. port_status->wPortStatus =
  233. cpu_to_le16(fhci->vroot_hub->port.wPortStatus);
  234. port_status->wPortChange =
  235. cpu_to_le16(fhci->vroot_hub->port.wPortChange);
  236. break;
  237. case SetHubFeature:
  238. switch (wValue) {
  239. case C_HUB_OVER_CURRENT:
  240. case C_HUB_LOCAL_POWER:
  241. break;
  242. default:
  243. goto error;
  244. }
  245. break;
  246. case SetPortFeature:
  247. fhci->vroot_hub->feature |= (1 << wValue);
  248. switch (wValue) {
  249. case USB_PORT_FEAT_ENABLE:
  250. fhci->vroot_hub->port.wPortStatus |=
  251. USB_PORT_STAT_ENABLE;
  252. fhci_port_enable(fhci->usb_lld);
  253. break;
  254. case USB_PORT_FEAT_SUSPEND:
  255. fhci->vroot_hub->port.wPortStatus |=
  256. USB_PORT_STAT_SUSPEND;
  257. fhci_stop_sof_timer(fhci);
  258. break;
  259. case USB_PORT_FEAT_RESET:
  260. fhci->vroot_hub->port.wPortStatus |=
  261. USB_PORT_STAT_RESET;
  262. fhci_port_reset(fhci->usb_lld);
  263. fhci->vroot_hub->port.wPortStatus |=
  264. USB_PORT_STAT_ENABLE;
  265. fhci->vroot_hub->port.wPortStatus &=
  266. ~USB_PORT_STAT_RESET;
  267. break;
  268. case USB_PORT_FEAT_POWER:
  269. fhci->vroot_hub->port.wPortStatus |=
  270. USB_PORT_STAT_POWER;
  271. fhci_config_transceiver(fhci, FHCI_PORT_WAITING);
  272. break;
  273. default:
  274. goto error;
  275. }
  276. break;
  277. default:
  278. error:
  279. retval = -EPIPE;
  280. }
  281. fhci_dbg(fhci, "<- %s\n", __func__);
  282. spin_unlock_irqrestore(&fhci->lock, flags);
  283. return retval;
  284. }