xhci-hcd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <linux/irq.h>
  23. #include <linux/module.h>
  24. #include "xhci.h"
  25. #define DRIVER_AUTHOR "Sarah Sharp"
  26. #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
  27. /* TODO: copied from ehci-hcd.c - can this be refactored? */
  28. /*
  29. * handshake - spin reading hc until handshake completes or fails
  30. * @ptr: address of hc register to be read
  31. * @mask: bits to look at in result of read
  32. * @done: value of those bits when handshake succeeds
  33. * @usec: timeout in microseconds
  34. *
  35. * Returns negative errno, or zero on success
  36. *
  37. * Success happens when the "mask" bits have the specified value (hardware
  38. * handshake done). There are two failure modes: "usec" have passed (major
  39. * hardware flakeout), or the register reads as all-ones (hardware removed).
  40. */
  41. static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
  42. u32 mask, u32 done, int usec)
  43. {
  44. u32 result;
  45. do {
  46. result = xhci_readl(xhci, ptr);
  47. if (result == ~(u32)0) /* card removed */
  48. return -ENODEV;
  49. result &= mask;
  50. if (result == done)
  51. return 0;
  52. udelay(1);
  53. usec--;
  54. } while (usec > 0);
  55. return -ETIMEDOUT;
  56. }
  57. /*
  58. * Force HC into halt state.
  59. *
  60. * Disable any IRQs and clear the run/stop bit.
  61. * HC will complete any current and actively pipelined transactions, and
  62. * should halt within 16 microframes of the run/stop bit being cleared.
  63. * Read HC Halted bit in the status register to see when the HC is finished.
  64. * XXX: shouldn't we set HC_STATE_HALT here somewhere?
  65. */
  66. int xhci_halt(struct xhci_hcd *xhci)
  67. {
  68. u32 halted;
  69. u32 cmd;
  70. u32 mask;
  71. xhci_dbg(xhci, "// Halt the HC\n");
  72. /* Disable all interrupts from the host controller */
  73. mask = ~(XHCI_IRQS);
  74. halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
  75. if (!halted)
  76. mask &= ~CMD_RUN;
  77. cmd = xhci_readl(xhci, &xhci->op_regs->command);
  78. cmd &= mask;
  79. xhci_writel(xhci, cmd, &xhci->op_regs->command);
  80. return handshake(xhci, &xhci->op_regs->status,
  81. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  82. }
  83. /*
  84. * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
  85. *
  86. * This resets pipelines, timers, counters, state machines, etc.
  87. * Transactions will be terminated immediately, and operational registers
  88. * will be set to their defaults.
  89. */
  90. int xhci_reset(struct xhci_hcd *xhci)
  91. {
  92. u32 command;
  93. u32 state;
  94. state = xhci_readl(xhci, &xhci->op_regs->status);
  95. BUG_ON((state & STS_HALT) == 0);
  96. xhci_dbg(xhci, "// Reset the HC\n");
  97. command = xhci_readl(xhci, &xhci->op_regs->command);
  98. command |= CMD_RESET;
  99. xhci_writel(xhci, command, &xhci->op_regs->command);
  100. /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
  101. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  102. return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
  103. }
  104. /*
  105. * Stop the HC from processing the endpoint queues.
  106. */
  107. static void xhci_quiesce(struct xhci_hcd *xhci)
  108. {
  109. /*
  110. * Queues are per endpoint, so we need to disable an endpoint or slot.
  111. *
  112. * To disable a slot, we need to insert a disable slot command on the
  113. * command ring and ring the doorbell. This will also free any internal
  114. * resources associated with the slot (which might not be what we want).
  115. *
  116. * A Release Endpoint command sounds better - doesn't free internal HC
  117. * memory, but removes the endpoints from the schedule and releases the
  118. * bandwidth, disables the doorbells, and clears the endpoint enable
  119. * flag. Usually used prior to a set interface command.
  120. *
  121. * TODO: Implement after command ring code is done.
  122. */
  123. BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
  124. xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
  125. }
  126. #if 0
  127. /* Set up MSI-X table for entry 0 (may claim other entries later) */
  128. static int xhci_setup_msix(struct xhci_hcd *xhci)
  129. {
  130. int ret;
  131. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  132. xhci->msix_count = 0;
  133. /* XXX: did I do this right? ixgbe does kcalloc for more than one */
  134. xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
  135. if (!xhci->msix_entries) {
  136. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  137. return -ENOMEM;
  138. }
  139. xhci->msix_entries[0].entry = 0;
  140. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  141. if (ret) {
  142. xhci_err(xhci, "Failed to enable MSI-X\n");
  143. goto free_entries;
  144. }
  145. /*
  146. * Pass the xhci pointer value as the request_irq "cookie".
  147. * If more irqs are added, this will need to be unique for each one.
  148. */
  149. ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
  150. "xHCI", xhci_to_hcd(xhci));
  151. if (ret) {
  152. xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
  153. goto disable_msix;
  154. }
  155. xhci_dbg(xhci, "Finished setting up MSI-X\n");
  156. return 0;
  157. disable_msix:
  158. pci_disable_msix(pdev);
  159. free_entries:
  160. kfree(xhci->msix_entries);
  161. xhci->msix_entries = NULL;
  162. return ret;
  163. }
  164. /* XXX: code duplication; can xhci_setup_msix call this? */
  165. /* Free any IRQs and disable MSI-X */
  166. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  167. {
  168. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  169. if (!xhci->msix_entries)
  170. return;
  171. free_irq(xhci->msix_entries[0].vector, xhci);
  172. pci_disable_msix(pdev);
  173. kfree(xhci->msix_entries);
  174. xhci->msix_entries = NULL;
  175. xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
  176. }
  177. #endif
  178. /*
  179. * Initialize memory for HCD and xHC (one-time init).
  180. *
  181. * Program the PAGESIZE register, initialize the device context array, create
  182. * device contexts (?), set up a command ring segment (or two?), create event
  183. * ring (one for now).
  184. */
  185. int xhci_init(struct usb_hcd *hcd)
  186. {
  187. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  188. int retval = 0;
  189. xhci_dbg(xhci, "xhci_init\n");
  190. spin_lock_init(&xhci->lock);
  191. retval = xhci_mem_init(xhci, GFP_KERNEL);
  192. xhci_dbg(xhci, "Finished xhci_init\n");
  193. return retval;
  194. }
  195. /*
  196. * Start the HC after it was halted.
  197. *
  198. * This function is called by the USB core when the HC driver is added.
  199. * Its opposite is xhci_stop().
  200. *
  201. * xhci_init() must be called once before this function can be called.
  202. * Reset the HC, enable device slot contexts, program DCBAAP, and
  203. * set command ring pointer and event ring pointer.
  204. *
  205. * Setup MSI-X vectors and enable interrupts.
  206. */
  207. int xhci_run(struct usb_hcd *hcd)
  208. {
  209. u32 temp;
  210. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  211. xhci_dbg(xhci, "xhci_run\n");
  212. #if 0 /* FIXME: MSI not setup yet */
  213. /* Do this at the very last minute */
  214. ret = xhci_setup_msix(xhci);
  215. if (!ret)
  216. return ret;
  217. return -ENOSYS;
  218. #endif
  219. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  220. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  221. temp &= 0xffff;
  222. temp |= (u32) 160;
  223. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  224. /* Set the HCD state before we enable the irqs */
  225. hcd->state = HC_STATE_RUNNING;
  226. temp = xhci_readl(xhci, &xhci->op_regs->command);
  227. temp |= (CMD_EIE);
  228. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  229. temp);
  230. xhci_writel(xhci, temp, &xhci->op_regs->command);
  231. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  232. xhci_dbg(xhci, "// Enabling event ring interrupter 0x%x"
  233. " by writing 0x%x to irq_pending\n",
  234. (unsigned int) xhci->ir_set,
  235. (unsigned int) ER_IRQ_ENABLE(temp));
  236. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  237. &xhci->ir_set->irq_pending);
  238. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  239. temp = xhci_readl(xhci, &xhci->op_regs->command);
  240. temp |= (CMD_RUN);
  241. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  242. temp);
  243. xhci_writel(xhci, temp, &xhci->op_regs->command);
  244. /* Flush PCI posted writes */
  245. temp = xhci_readl(xhci, &xhci->op_regs->command);
  246. xhci_dbg(xhci, "// @%x = 0x%x\n",
  247. (unsigned int) &xhci->op_regs->command, temp);
  248. xhci_dbg(xhci, "Finished xhci_run\n");
  249. return 0;
  250. }
  251. /*
  252. * Stop xHCI driver.
  253. *
  254. * This function is called by the USB core when the HC driver is removed.
  255. * Its opposite is xhci_run().
  256. *
  257. * Disable device contexts, disable IRQs, and quiesce the HC.
  258. * Reset the HC, finish any completed transactions, and cleanup memory.
  259. */
  260. void xhci_stop(struct usb_hcd *hcd)
  261. {
  262. u32 temp;
  263. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  264. spin_lock_irq(&xhci->lock);
  265. if (HC_IS_RUNNING(hcd->state))
  266. xhci_quiesce(xhci);
  267. xhci_halt(xhci);
  268. xhci_reset(xhci);
  269. spin_unlock_irq(&xhci->lock);
  270. #if 0 /* No MSI yet */
  271. xhci_cleanup_msix(xhci);
  272. #endif
  273. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  274. temp = xhci_readl(xhci, &xhci->op_regs->status);
  275. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  276. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  277. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  278. &xhci->ir_set->irq_pending);
  279. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  280. xhci_dbg(xhci, "cleaning up memory\n");
  281. xhci_mem_cleanup(xhci);
  282. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  283. xhci_readl(xhci, &xhci->op_regs->status));
  284. }
  285. /*
  286. * Shutdown HC (not bus-specific)
  287. *
  288. * This is called when the machine is rebooting or halting. We assume that the
  289. * machine will be powered off, and the HC's internal state will be reset.
  290. * Don't bother to free memory.
  291. */
  292. void xhci_shutdown(struct usb_hcd *hcd)
  293. {
  294. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  295. spin_lock_irq(&xhci->lock);
  296. xhci_halt(xhci);
  297. spin_unlock_irq(&xhci->lock);
  298. #if 0
  299. xhci_cleanup_msix(xhci);
  300. #endif
  301. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  302. xhci_readl(xhci, &xhci->op_regs->status));
  303. }
  304. int xhci_get_frame(struct usb_hcd *hcd)
  305. {
  306. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  307. /* EHCI mods by the periodic size. Why? */
  308. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  309. }
  310. MODULE_DESCRIPTION(DRIVER_DESC);
  311. MODULE_AUTHOR(DRIVER_AUTHOR);
  312. MODULE_LICENSE("GPL");
  313. static int __init xhci_hcd_init(void)
  314. {
  315. #ifdef CONFIG_PCI
  316. int retval = 0;
  317. retval = xhci_register_pci();
  318. if (retval < 0) {
  319. printk(KERN_DEBUG "Problem registering PCI driver.");
  320. return retval;
  321. }
  322. #endif
  323. return 0;
  324. }
  325. module_init(xhci_hcd_init);
  326. static void __exit xhci_hcd_cleanup(void)
  327. {
  328. #ifdef CONFIG_PCI
  329. xhci_unregister_pci();
  330. #endif
  331. }
  332. module_exit(xhci_hcd_cleanup);