xhci-hub.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 USB_PORT_STAT_LOW_SPEED;
  59. if (DEV_HIGHSPEED(port_status))
  60. return USB_PORT_STAT_HIGH_SPEED;
  61. if (DEV_SUPERSPEED(port_status))
  62. return USB_PORT_STAT_SUPER_SPEED;
  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_STAT_*_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. static void xhci_disable_port(struct xhci_hcd *xhci, u16 wIndex,
  121. u32 __iomem *addr, u32 port_status)
  122. {
  123. /* Write 1 to disable the port */
  124. xhci_writel(xhci, port_status | PORT_PE, addr);
  125. port_status = xhci_readl(xhci, addr);
  126. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  127. wIndex, port_status);
  128. }
  129. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  130. u16 wIndex, u32 __iomem *addr, u32 port_status)
  131. {
  132. char *port_change_bit;
  133. u32 status;
  134. switch (wValue) {
  135. case USB_PORT_FEAT_C_RESET:
  136. status = PORT_RC;
  137. port_change_bit = "reset";
  138. break;
  139. case USB_PORT_FEAT_C_CONNECTION:
  140. status = PORT_CSC;
  141. port_change_bit = "connect";
  142. break;
  143. case USB_PORT_FEAT_C_OVER_CURRENT:
  144. status = PORT_OCC;
  145. port_change_bit = "over-current";
  146. break;
  147. case USB_PORT_FEAT_C_ENABLE:
  148. status = PORT_PEC;
  149. port_change_bit = "enable/disable";
  150. break;
  151. default:
  152. /* Should never happen */
  153. return;
  154. }
  155. /* Change bits are all write 1 to clear */
  156. xhci_writel(xhci, port_status | status, addr);
  157. port_status = xhci_readl(xhci, addr);
  158. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  159. port_change_bit, wIndex, port_status);
  160. }
  161. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  162. u16 wIndex, char *buf, u16 wLength)
  163. {
  164. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  165. int ports;
  166. unsigned long flags;
  167. u32 temp, status;
  168. int retval = 0;
  169. u32 __iomem *addr;
  170. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  171. spin_lock_irqsave(&xhci->lock, flags);
  172. switch (typeReq) {
  173. case GetHubStatus:
  174. /* No power source, over-current reported per port */
  175. memset(buf, 0, 4);
  176. break;
  177. case GetHubDescriptor:
  178. xhci_hub_descriptor(xhci, (struct usb_hub_descriptor *) buf);
  179. break;
  180. case GetPortStatus:
  181. if (!wIndex || wIndex > ports)
  182. goto error;
  183. wIndex--;
  184. status = 0;
  185. addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
  186. temp = xhci_readl(xhci, addr);
  187. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  188. /* wPortChange bits */
  189. if (temp & PORT_CSC)
  190. status |= USB_PORT_STAT_C_CONNECTION << 16;
  191. if (temp & PORT_PEC)
  192. status |= USB_PORT_STAT_C_ENABLE << 16;
  193. if ((temp & PORT_OCC))
  194. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  195. /*
  196. * FIXME ignoring suspend, reset, and USB 2.1/3.0 specific
  197. * changes
  198. */
  199. if (temp & PORT_CONNECT) {
  200. status |= USB_PORT_STAT_CONNECTION;
  201. status |= xhci_port_speed(temp);
  202. }
  203. if (temp & PORT_PE)
  204. status |= USB_PORT_STAT_ENABLE;
  205. if (temp & PORT_OC)
  206. status |= USB_PORT_STAT_OVERCURRENT;
  207. if (temp & PORT_RESET)
  208. status |= USB_PORT_STAT_RESET;
  209. if (temp & PORT_POWER)
  210. status |= USB_PORT_STAT_POWER;
  211. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  212. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  213. break;
  214. case SetPortFeature:
  215. wIndex &= 0xff;
  216. if (!wIndex || wIndex > ports)
  217. goto error;
  218. wIndex--;
  219. addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
  220. temp = xhci_readl(xhci, addr);
  221. temp = xhci_port_state_to_neutral(temp);
  222. switch (wValue) {
  223. case USB_PORT_FEAT_POWER:
  224. /*
  225. * Turn on ports, even if there isn't per-port switching.
  226. * HC will report connect events even before this is set.
  227. * However, khubd will ignore the roothub events until
  228. * the roothub is registered.
  229. */
  230. xhci_writel(xhci, temp | PORT_POWER, addr);
  231. temp = xhci_readl(xhci, addr);
  232. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  233. break;
  234. case USB_PORT_FEAT_RESET:
  235. temp = (temp | PORT_RESET);
  236. xhci_writel(xhci, temp, addr);
  237. temp = xhci_readl(xhci, addr);
  238. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  239. break;
  240. default:
  241. goto error;
  242. }
  243. temp = xhci_readl(xhci, addr); /* unblock any posted writes */
  244. break;
  245. case ClearPortFeature:
  246. if (!wIndex || wIndex > ports)
  247. goto error;
  248. wIndex--;
  249. addr = &xhci->op_regs->port_status_base +
  250. NUM_PORT_REGS*(wIndex & 0xff);
  251. temp = xhci_readl(xhci, addr);
  252. temp = xhci_port_state_to_neutral(temp);
  253. switch (wValue) {
  254. case USB_PORT_FEAT_C_RESET:
  255. case USB_PORT_FEAT_C_CONNECTION:
  256. case USB_PORT_FEAT_C_OVER_CURRENT:
  257. case USB_PORT_FEAT_C_ENABLE:
  258. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  259. addr, temp);
  260. break;
  261. case USB_PORT_FEAT_ENABLE:
  262. xhci_disable_port(xhci, wIndex, addr, temp);
  263. break;
  264. default:
  265. goto error;
  266. }
  267. break;
  268. default:
  269. error:
  270. /* "stall" on error */
  271. retval = -EPIPE;
  272. }
  273. spin_unlock_irqrestore(&xhci->lock, flags);
  274. return retval;
  275. }
  276. /*
  277. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  278. * Ports are 0-indexed from the HCD point of view,
  279. * and 1-indexed from the USB core pointer of view.
  280. *
  281. * Note that the status change bits will be cleared as soon as a port status
  282. * change event is generated, so we use the saved status from that event.
  283. */
  284. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  285. {
  286. unsigned long flags;
  287. u32 temp, status;
  288. int i, retval;
  289. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  290. int ports;
  291. u32 __iomem *addr;
  292. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  293. /* Initial status is no changes */
  294. retval = (ports + 8) / 8;
  295. memset(buf, 0, retval);
  296. status = 0;
  297. spin_lock_irqsave(&xhci->lock, flags);
  298. /* For each port, did anything change? If so, set that bit in buf. */
  299. for (i = 0; i < ports; i++) {
  300. addr = &xhci->op_regs->port_status_base +
  301. NUM_PORT_REGS*i;
  302. temp = xhci_readl(xhci, addr);
  303. if (temp & (PORT_CSC | PORT_PEC | PORT_OCC)) {
  304. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  305. status = 1;
  306. }
  307. }
  308. spin_unlock_irqrestore(&xhci->lock, flags);
  309. return status ? retval : 0;
  310. }