xhci-hub.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <asm/unaligned.h>
  23. #include "xhci.h"
  24. static void xhci_hub_descriptor(struct xhci_hcd *xhci,
  25. struct usb_hub_descriptor *desc)
  26. {
  27. int ports;
  28. u16 temp;
  29. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  30. /* USB 3.0 hubs have a different descriptor, but we fake this for now */
  31. desc->bDescriptorType = 0x29;
  32. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  33. desc->bHubContrCurrent = 0;
  34. desc->bNbrPorts = ports;
  35. temp = 1 + (ports / 8);
  36. desc->bDescLength = 7 + 2 * temp;
  37. /* Why does core/hcd.h define bitmap? It's just confusing. */
  38. memset(&desc->DeviceRemovable[0], 0, temp);
  39. memset(&desc->DeviceRemovable[temp], 0xff, temp);
  40. /* Ugh, these should be #defines, FIXME */
  41. /* Using table 11-13 in USB 2.0 spec. */
  42. temp = 0;
  43. /* Bits 1:0 - support port power switching, or power always on */
  44. if (HCC_PPC(xhci->hcc_params))
  45. temp |= 0x0001;
  46. else
  47. temp |= 0x0002;
  48. /* Bit 2 - root hubs are not part of a compound device */
  49. /* Bits 4:3 - individual port over current protection */
  50. temp |= 0x0008;
  51. /* Bits 6:5 - no TTs in root ports */
  52. /* Bit 7 - no port indicators */
  53. desc->wHubCharacteristics = (__force __u16) cpu_to_le16(temp);
  54. }
  55. static unsigned int xhci_port_speed(unsigned int port_status)
  56. {
  57. if (DEV_LOWSPEED(port_status))
  58. return 1 << USB_PORT_FEAT_LOWSPEED;
  59. if (DEV_HIGHSPEED(port_status))
  60. return 1 << USB_PORT_FEAT_HIGHSPEED;
  61. if (DEV_SUPERSPEED(port_status))
  62. return 1 << USB_PORT_FEAT_SUPERSPEED;
  63. /*
  64. * FIXME: Yes, we should check for full speed, but the core uses that as
  65. * a default in portspeed() in usb/core/hub.c (which is the only place
  66. * USB_PORT_FEAT_*SPEED is used).
  67. */
  68. return 0;
  69. }
  70. /*
  71. * These bits are Read Only (RO) and should be saved and written to the
  72. * registers: 0, 3, 10:13, 30
  73. * connect status, over-current status, port speed, and device removable.
  74. * connect status and port speed are also sticky - meaning they're in
  75. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  76. */
  77. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  78. /*
  79. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  80. * bits 5:8, 9, 14:15, 25:27
  81. * link state, port power, port indicator state, "wake on" enable state
  82. */
  83. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  84. /*
  85. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  86. * bit 4 (port reset)
  87. */
  88. #define XHCI_PORT_RW1S ((1<<4))
  89. /*
  90. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  91. * bits 1, 17, 18, 19, 20, 21, 22, 23
  92. * port enable/disable, and
  93. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  94. * over-current, reset, link state, and L1 change
  95. */
  96. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  97. /*
  98. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  99. * latched in
  100. */
  101. #define XHCI_PORT_RW ((1<<16))
  102. /*
  103. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  104. * bits 2, 24, 28:31
  105. */
  106. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  107. /*
  108. * Given a port state, this function returns a value that would result in the
  109. * port being in the same state, if the value was written to the port status
  110. * control register.
  111. * Save Read Only (RO) bits and save read/write bits where
  112. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  113. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  114. */
  115. static u32 xhci_port_state_to_neutral(u32 state)
  116. {
  117. /* Save read-only status and port state */
  118. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  119. }
  120. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  121. u16 wIndex, char *buf, u16 wLength)
  122. {
  123. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  124. int ports;
  125. unsigned long flags;
  126. u32 temp, status;
  127. int retval = 0;
  128. u32 __iomem *addr;
  129. char *port_change_bit;
  130. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  131. spin_lock_irqsave(&xhci->lock, flags);
  132. switch (typeReq) {
  133. case GetHubStatus:
  134. /* No power source, over-current reported per port */
  135. memset(buf, 0, 4);
  136. break;
  137. case GetHubDescriptor:
  138. xhci_hub_descriptor(xhci, (struct usb_hub_descriptor *) buf);
  139. break;
  140. case GetPortStatus:
  141. if (!wIndex || wIndex > ports)
  142. goto error;
  143. wIndex--;
  144. status = 0;
  145. addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
  146. temp = xhci_readl(xhci, addr);
  147. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  148. /* wPortChange bits */
  149. if (temp & PORT_CSC)
  150. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  151. if (temp & PORT_PEC)
  152. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  153. if ((temp & PORT_OCC))
  154. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  155. /*
  156. * FIXME ignoring suspend, reset, and USB 2.1/3.0 specific
  157. * changes
  158. */
  159. if (temp & PORT_CONNECT) {
  160. status |= 1 << USB_PORT_FEAT_CONNECTION;
  161. status |= xhci_port_speed(temp);
  162. }
  163. if (temp & PORT_PE)
  164. status |= 1 << USB_PORT_FEAT_ENABLE;
  165. if (temp & PORT_OC)
  166. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  167. if (temp & PORT_RESET)
  168. status |= 1 << USB_PORT_FEAT_RESET;
  169. if (temp & PORT_POWER)
  170. status |= 1 << USB_PORT_FEAT_POWER;
  171. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  172. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  173. break;
  174. case SetPortFeature:
  175. wIndex &= 0xff;
  176. if (!wIndex || wIndex > ports)
  177. goto error;
  178. wIndex--;
  179. addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
  180. temp = xhci_readl(xhci, addr);
  181. temp = xhci_port_state_to_neutral(temp);
  182. switch (wValue) {
  183. case USB_PORT_FEAT_POWER:
  184. /*
  185. * Turn on ports, even if there isn't per-port switching.
  186. * HC will report connect events even before this is set.
  187. * However, khubd will ignore the roothub events until
  188. * the roothub is registered.
  189. */
  190. xhci_writel(xhci, temp | PORT_POWER, addr);
  191. temp = xhci_readl(xhci, addr);
  192. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  193. break;
  194. case USB_PORT_FEAT_RESET:
  195. temp = (temp | PORT_RESET);
  196. xhci_writel(xhci, temp, addr);
  197. temp = xhci_readl(xhci, addr);
  198. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  199. break;
  200. default:
  201. goto error;
  202. }
  203. temp = xhci_readl(xhci, addr); /* unblock any posted writes */
  204. break;
  205. case ClearPortFeature:
  206. if (!wIndex || wIndex > ports)
  207. goto error;
  208. wIndex--;
  209. addr = &xhci->op_regs->port_status_base +
  210. NUM_PORT_REGS*(wIndex & 0xff);
  211. temp = xhci_readl(xhci, addr);
  212. temp = xhci_port_state_to_neutral(temp);
  213. switch (wValue) {
  214. case USB_PORT_FEAT_C_RESET:
  215. status = PORT_RC;
  216. port_change_bit = "reset";
  217. break;
  218. case USB_PORT_FEAT_C_CONNECTION:
  219. status = PORT_CSC;
  220. port_change_bit = "connect";
  221. break;
  222. case USB_PORT_FEAT_C_OVER_CURRENT:
  223. status = PORT_OCC;
  224. port_change_bit = "over-current";
  225. break;
  226. default:
  227. goto error;
  228. }
  229. /* Change bits are all write 1 to clear */
  230. xhci_writel(xhci, temp | status, addr);
  231. temp = xhci_readl(xhci, addr);
  232. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  233. port_change_bit, wIndex, temp);
  234. temp = xhci_readl(xhci, addr); /* unblock any posted writes */
  235. break;
  236. default:
  237. error:
  238. /* "stall" on error */
  239. retval = -EPIPE;
  240. }
  241. spin_unlock_irqrestore(&xhci->lock, flags);
  242. return retval;
  243. }
  244. /*
  245. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  246. * Ports are 0-indexed from the HCD point of view,
  247. * and 1-indexed from the USB core pointer of view.
  248. * xHCI instances can have up to 127 ports, so FIXME if you see more than 15.
  249. *
  250. * Note that the status change bits will be cleared as soon as a port status
  251. * change event is generated, so we use the saved status from that event.
  252. */
  253. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  254. {
  255. unsigned long flags;
  256. u32 temp, status;
  257. int i, retval;
  258. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  259. int ports;
  260. u32 __iomem *addr;
  261. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  262. /* Initial status is no changes */
  263. buf[0] = 0;
  264. status = 0;
  265. if (ports > 7) {
  266. buf[1] = 0;
  267. retval = 2;
  268. } else {
  269. retval = 1;
  270. }
  271. spin_lock_irqsave(&xhci->lock, flags);
  272. /* For each port, did anything change? If so, set that bit in buf. */
  273. for (i = 0; i < ports; i++) {
  274. addr = &xhci->op_regs->port_status_base +
  275. NUM_PORT_REGS*i;
  276. temp = xhci_readl(xhci, addr);
  277. if (temp & (PORT_CSC | PORT_PEC | PORT_OCC)) {
  278. if (i < 7)
  279. buf[0] |= 1 << (i + 1);
  280. else
  281. buf[1] |= 1 << (i - 7);
  282. status = 1;
  283. }
  284. }
  285. spin_unlock_irqrestore(&xhci->lock, flags);
  286. return status ? retval : 0;
  287. }