fhci-hub.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. USB_DT_HUB, /* bDescriptorType;hub-descriptor */
  32. 0x01, /* bNbrPorts */
  33. HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM, /* wHubCharacteristics */
  34. 0x00, /* per-port power, no overcurrent */
  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_usbmr, 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_usmod);
  132. out_8(&fhci->regs->usb_usmod, mode & (~USB_MODE_EN));
  133. /* disable idle interrupts */
  134. mask = in_be16(&fhci->regs->usb_usbmr);
  135. out_be16(&fhci->regs->usb_usbmr, mask & (~USB_E_IDLE_MASK));
  136. fhci_io_port_generate_reset(fhci);
  137. /* enable interrupt on this endpoint */
  138. out_be16(&fhci->regs->usb_usbmr, mask);
  139. /* enable the USB controller */
  140. mode = in_8(&fhci->regs->usb_usmod);
  141. out_8(&fhci->regs->usb_usmod, 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. struct usb_hub_status *hub_status;
  169. struct usb_port_status *port_status;
  170. unsigned long flags;
  171. spin_lock_irqsave(&fhci->lock, flags);
  172. fhci_dbg(fhci, "-> %s\n", __func__);
  173. switch (typeReq) {
  174. case ClearHubFeature:
  175. switch (wValue) {
  176. case C_HUB_LOCAL_POWER:
  177. case C_HUB_OVER_CURRENT:
  178. break;
  179. default:
  180. goto error;
  181. }
  182. break;
  183. case ClearPortFeature:
  184. fhci->vroot_hub->feature &= (1 << wValue);
  185. switch (wValue) {
  186. case USB_PORT_FEAT_ENABLE:
  187. fhci->vroot_hub->port.wPortStatus &=
  188. ~USB_PORT_STAT_ENABLE;
  189. fhci_port_disable(fhci);
  190. break;
  191. case USB_PORT_FEAT_C_ENABLE:
  192. fhci->vroot_hub->port.wPortChange &=
  193. ~USB_PORT_STAT_C_ENABLE;
  194. break;
  195. case USB_PORT_FEAT_SUSPEND:
  196. fhci->vroot_hub->port.wPortStatus &=
  197. ~USB_PORT_STAT_SUSPEND;
  198. fhci_stop_sof_timer(fhci);
  199. break;
  200. case USB_PORT_FEAT_C_SUSPEND:
  201. fhci->vroot_hub->port.wPortChange &=
  202. ~USB_PORT_STAT_C_SUSPEND;
  203. break;
  204. case USB_PORT_FEAT_POWER:
  205. fhci->vroot_hub->port.wPortStatus &=
  206. ~USB_PORT_STAT_POWER;
  207. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  208. break;
  209. case USB_PORT_FEAT_C_CONNECTION:
  210. fhci->vroot_hub->port.wPortChange &=
  211. ~USB_PORT_STAT_C_CONNECTION;
  212. break;
  213. case USB_PORT_FEAT_C_OVER_CURRENT:
  214. fhci->vroot_hub->port.wPortChange &=
  215. ~USB_PORT_STAT_C_OVERCURRENT;
  216. break;
  217. case USB_PORT_FEAT_C_RESET:
  218. fhci->vroot_hub->port.wPortChange &=
  219. ~USB_PORT_STAT_C_RESET;
  220. break;
  221. default:
  222. goto error;
  223. }
  224. break;
  225. case GetHubDescriptor:
  226. memcpy(buf, root_hub_des, sizeof(root_hub_des));
  227. break;
  228. case GetHubStatus:
  229. hub_status = (struct usb_hub_status *)buf;
  230. hub_status->wHubStatus =
  231. cpu_to_le16(fhci->vroot_hub->hub.wHubStatus);
  232. hub_status->wHubChange =
  233. cpu_to_le16(fhci->vroot_hub->hub.wHubChange);
  234. break;
  235. case GetPortStatus:
  236. port_status = (struct usb_port_status *)buf;
  237. port_status->wPortStatus =
  238. cpu_to_le16(fhci->vroot_hub->port.wPortStatus);
  239. port_status->wPortChange =
  240. cpu_to_le16(fhci->vroot_hub->port.wPortChange);
  241. break;
  242. case SetHubFeature:
  243. switch (wValue) {
  244. case C_HUB_OVER_CURRENT:
  245. case C_HUB_LOCAL_POWER:
  246. break;
  247. default:
  248. goto error;
  249. }
  250. break;
  251. case SetPortFeature:
  252. fhci->vroot_hub->feature |= (1 << wValue);
  253. switch (wValue) {
  254. case USB_PORT_FEAT_ENABLE:
  255. fhci->vroot_hub->port.wPortStatus |=
  256. USB_PORT_STAT_ENABLE;
  257. fhci_port_enable(fhci->usb_lld);
  258. break;
  259. case USB_PORT_FEAT_SUSPEND:
  260. fhci->vroot_hub->port.wPortStatus |=
  261. USB_PORT_STAT_SUSPEND;
  262. fhci_stop_sof_timer(fhci);
  263. break;
  264. case USB_PORT_FEAT_RESET:
  265. fhci->vroot_hub->port.wPortStatus |=
  266. USB_PORT_STAT_RESET;
  267. fhci_port_reset(fhci->usb_lld);
  268. fhci->vroot_hub->port.wPortStatus |=
  269. USB_PORT_STAT_ENABLE;
  270. fhci->vroot_hub->port.wPortStatus &=
  271. ~USB_PORT_STAT_RESET;
  272. break;
  273. case USB_PORT_FEAT_POWER:
  274. fhci->vroot_hub->port.wPortStatus |=
  275. USB_PORT_STAT_POWER;
  276. fhci_config_transceiver(fhci, FHCI_PORT_WAITING);
  277. break;
  278. default:
  279. goto error;
  280. }
  281. break;
  282. default:
  283. error:
  284. retval = -EPIPE;
  285. }
  286. fhci_dbg(fhci, "<- %s\n", __func__);
  287. spin_unlock_irqrestore(&fhci->lock, flags);
  288. return retval;
  289. }