2 * xHCI host controller driver
4 * Copyright (C) 2008 Intel Corp.
7 * Some code borrowed from the Linux EHCI driver.
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.
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
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.
23 #include <linux/irq.h>
24 #include <linux/module.h>
28 #define DRIVER_AUTHOR "Sarah Sharp"
29 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
31 /* TODO: copied from ehci-hcd.c - can this be refactored? */
33 * handshake - spin reading hc until handshake completes or fails
34 * @ptr: address of hc register to be read
35 * @mask: bits to look at in result of read
36 * @done: value of those bits when handshake succeeds
37 * @usec: timeout in microseconds
39 * Returns negative errno, or zero on success
41 * Success happens when the "mask" bits have the specified value (hardware
42 * handshake done). There are two failure modes: "usec" have passed (major
43 * hardware flakeout), or the register reads as all-ones (hardware removed).
45 static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
46 u32 mask, u32 done, int usec)
51 result = xhci_readl(xhci, ptr);
52 if (result == ~(u32)0) /* card removed */
64 * Force HC into halt state.
66 * Disable any IRQs and clear the run/stop bit.
67 * HC will complete any current and actively pipelined transactions, and
68 * should halt within 16 microframes of the run/stop bit being cleared.
69 * Read HC Halted bit in the status register to see when the HC is finished.
70 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
72 int xhci_halt(struct xhci_hcd *xhci)
78 xhci_dbg(xhci, "// Halt the HC\n");
79 /* Disable all interrupts from the host controller */
81 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
85 cmd = xhci_readl(xhci, &xhci->op_regs->command);
87 xhci_writel(xhci, cmd, &xhci->op_regs->command);
89 return handshake(xhci, &xhci->op_regs->status,
90 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
94 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
96 * This resets pipelines, timers, counters, state machines, etc.
97 * Transactions will be terminated immediately, and operational registers
98 * will be set to their defaults.
100 int xhci_reset(struct xhci_hcd *xhci)
105 state = xhci_readl(xhci, &xhci->op_regs->status);
106 BUG_ON((state & STS_HALT) == 0);
108 xhci_dbg(xhci, "// Reset the HC\n");
109 command = xhci_readl(xhci, &xhci->op_regs->command);
110 command |= CMD_RESET;
111 xhci_writel(xhci, command, &xhci->op_regs->command);
112 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
113 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
115 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
119 * Stop the HC from processing the endpoint queues.
121 static void xhci_quiesce(struct xhci_hcd *xhci)
124 * Queues are per endpoint, so we need to disable an endpoint or slot.
126 * To disable a slot, we need to insert a disable slot command on the
127 * command ring and ring the doorbell. This will also free any internal
128 * resources associated with the slot (which might not be what we want).
130 * A Release Endpoint command sounds better - doesn't free internal HC
131 * memory, but removes the endpoints from the schedule and releases the
132 * bandwidth, disables the doorbells, and clears the endpoint enable
133 * flag. Usually used prior to a set interface command.
135 * TODO: Implement after command ring code is done.
137 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
138 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
142 /* Set up MSI-X table for entry 0 (may claim other entries later) */
143 static int xhci_setup_msix(struct xhci_hcd *xhci)
146 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
148 xhci->msix_count = 0;
149 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
150 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
151 if (!xhci->msix_entries) {
152 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
155 xhci->msix_entries[0].entry = 0;
157 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
159 xhci_err(xhci, "Failed to enable MSI-X\n");
164 * Pass the xhci pointer value as the request_irq "cookie".
165 * If more irqs are added, this will need to be unique for each one.
167 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
168 "xHCI", xhci_to_hcd(xhci));
170 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
173 xhci_dbg(xhci, "Finished setting up MSI-X\n");
177 pci_disable_msix(pdev);
179 kfree(xhci->msix_entries);
180 xhci->msix_entries = NULL;
184 /* XXX: code duplication; can xhci_setup_msix call this? */
185 /* Free any IRQs and disable MSI-X */
186 static void xhci_cleanup_msix(struct xhci_hcd *xhci)
188 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
189 if (!xhci->msix_entries)
192 free_irq(xhci->msix_entries[0].vector, xhci);
193 pci_disable_msix(pdev);
194 kfree(xhci->msix_entries);
195 xhci->msix_entries = NULL;
196 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
201 * Initialize memory for HCD and xHC (one-time init).
203 * Program the PAGESIZE register, initialize the device context array, create
204 * device contexts (?), set up a command ring segment (or two?), create event
205 * ring (one for now).
207 int xhci_init(struct usb_hcd *hcd)
209 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
212 xhci_dbg(xhci, "xhci_init\n");
213 spin_lock_init(&xhci->lock);
214 retval = xhci_mem_init(xhci, GFP_KERNEL);
215 xhci_dbg(xhci, "Finished xhci_init\n");
221 * Called in interrupt context when there might be work
222 * queued on the event ring
224 * xhci->lock must be held by caller.
226 static void xhci_work(struct xhci_hcd *xhci)
231 * Clear the op reg interrupt status first,
232 * so we can receive interrupts from other MSI-X interrupters.
233 * Write 1 to clear the interrupt status.
235 temp = xhci_readl(xhci, &xhci->op_regs->status);
237 xhci_writel(xhci, temp, &xhci->op_regs->status);
238 /* FIXME when MSI-X is supported and there are multiple vectors */
239 /* Clear the MSI-X event interrupt status */
241 /* Acknowledge the interrupt */
242 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
244 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
245 /* Flush posted writes */
246 xhci_readl(xhci, &xhci->ir_set->irq_pending);
248 /* FIXME this should be a delayed service routine that clears the EHB */
251 /* Clear the event handler busy flag; the event ring should be empty. */
252 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
253 xhci_writel(xhci, temp & ~ERST_EHB, &xhci->ir_set->erst_dequeue[0]);
254 /* Flush posted writes -- FIXME is this necessary? */
255 xhci_readl(xhci, &xhci->ir_set->irq_pending);
258 /*-------------------------------------------------------------------------*/
261 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
262 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
263 * indicators of an event TRB error, but we check the status *first* to be safe.
265 irqreturn_t xhci_irq(struct usb_hcd *hcd)
267 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
270 spin_lock(&xhci->lock);
271 /* Check if the xHC generated the interrupt, or the irq is shared */
272 temp = xhci_readl(xhci, &xhci->op_regs->status);
273 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
274 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
275 spin_unlock(&xhci->lock);
279 temp = xhci_readl(xhci, &xhci->op_regs->status);
280 if (temp & STS_FATAL) {
281 xhci_warn(xhci, "WARNING: Host System Error\n");
283 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
288 spin_unlock(&xhci->lock);
293 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
294 void event_ring_work(unsigned long arg)
298 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
301 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
303 spin_lock_irqsave(&xhci->lock, flags);
304 temp = xhci_readl(xhci, &xhci->op_regs->status);
305 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
306 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
307 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
308 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
309 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
310 xhci->error_bitmask = 0;
311 xhci_dbg(xhci, "Event ring:\n");
312 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
313 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
314 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
315 temp &= ERST_PTR_MASK;
316 xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
317 xhci_dbg(xhci, "Command ring:\n");
318 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
319 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
320 xhci_dbg_cmd_ptrs(xhci);
322 if (xhci->noops_submitted != NUM_TEST_NOOPS)
323 if (setup_one_noop(xhci))
325 spin_unlock_irqrestore(&xhci->lock, flags);
328 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
330 xhci_dbg(xhci, "Quit polling the event ring.\n");
335 * Start the HC after it was halted.
337 * This function is called by the USB core when the HC driver is added.
338 * Its opposite is xhci_stop().
340 * xhci_init() must be called once before this function can be called.
341 * Reset the HC, enable device slot contexts, program DCBAAP, and
342 * set command ring pointer and event ring pointer.
344 * Setup MSI-X vectors and enable interrupts.
346 int xhci_run(struct usb_hcd *hcd)
349 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
350 void (*doorbell)(struct xhci_hcd *) = NULL;
352 xhci_dbg(xhci, "xhci_run\n");
353 #if 0 /* FIXME: MSI not setup yet */
354 /* Do this at the very last minute */
355 ret = xhci_setup_msix(xhci);
361 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
362 init_timer(&xhci->event_ring_timer);
363 xhci->event_ring_timer.data = (unsigned long) xhci;
364 xhci->event_ring_timer.function = event_ring_work;
365 /* Poll the event ring */
366 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
368 xhci_dbg(xhci, "Setting event ring polling timer\n");
369 add_timer(&xhci->event_ring_timer);
372 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
373 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
376 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
378 /* Set the HCD state before we enable the irqs */
379 hcd->state = HC_STATE_RUNNING;
380 temp = xhci_readl(xhci, &xhci->op_regs->command);
382 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
384 xhci_writel(xhci, temp, &xhci->op_regs->command);
386 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
387 xhci_dbg(xhci, "// Enabling event ring interrupter 0x%x"
388 " by writing 0x%x to irq_pending\n",
389 (unsigned int) xhci->ir_set,
390 (unsigned int) ER_IRQ_ENABLE(temp));
391 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
392 &xhci->ir_set->irq_pending);
393 xhci_print_ir_set(xhci, xhci->ir_set, 0);
395 if (NUM_TEST_NOOPS > 0)
396 doorbell = setup_one_noop(xhci);
398 xhci_dbg(xhci, "Command ring memory map follows:\n");
399 xhci_debug_ring(xhci, xhci->cmd_ring);
400 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
401 xhci_dbg_cmd_ptrs(xhci);
403 xhci_dbg(xhci, "ERST memory map follows:\n");
404 xhci_dbg_erst(xhci, &xhci->erst);
405 xhci_dbg(xhci, "Event ring:\n");
406 xhci_debug_ring(xhci, xhci->event_ring);
407 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
408 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[1]);
409 xhci_dbg(xhci, "ERST deq upper = 0x%x\n", temp);
410 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
411 temp &= ERST_PTR_MASK;
412 xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
414 temp = xhci_readl(xhci, &xhci->op_regs->command);
416 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
418 xhci_writel(xhci, temp, &xhci->op_regs->command);
419 /* Flush PCI posted writes */
420 temp = xhci_readl(xhci, &xhci->op_regs->command);
421 xhci_dbg(xhci, "// @%x = 0x%x\n",
422 (unsigned int) &xhci->op_regs->command, temp);
426 xhci_dbg(xhci, "Finished xhci_run\n");
433 * This function is called by the USB core when the HC driver is removed.
434 * Its opposite is xhci_run().
436 * Disable device contexts, disable IRQs, and quiesce the HC.
437 * Reset the HC, finish any completed transactions, and cleanup memory.
439 void xhci_stop(struct usb_hcd *hcd)
442 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
444 spin_lock_irq(&xhci->lock);
445 if (HC_IS_RUNNING(hcd->state))
449 spin_unlock_irq(&xhci->lock);
451 #if 0 /* No MSI yet */
452 xhci_cleanup_msix(xhci);
454 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
455 /* Tell the event ring poll function not to reschedule */
457 del_timer_sync(&xhci->event_ring_timer);
460 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
461 temp = xhci_readl(xhci, &xhci->op_regs->status);
462 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
463 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
464 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
465 &xhci->ir_set->irq_pending);
466 xhci_print_ir_set(xhci, xhci->ir_set, 0);
468 xhci_dbg(xhci, "cleaning up memory\n");
469 xhci_mem_cleanup(xhci);
470 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
471 xhci_readl(xhci, &xhci->op_regs->status));
475 * Shutdown HC (not bus-specific)
477 * This is called when the machine is rebooting or halting. We assume that the
478 * machine will be powered off, and the HC's internal state will be reset.
479 * Don't bother to free memory.
481 void xhci_shutdown(struct usb_hcd *hcd)
483 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
485 spin_lock_irq(&xhci->lock);
487 spin_unlock_irq(&xhci->lock);
490 xhci_cleanup_msix(xhci);
493 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
494 xhci_readl(xhci, &xhci->op_regs->status));
497 /*-------------------------------------------------------------------------*/
499 int xhci_get_frame(struct usb_hcd *hcd)
501 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
502 /* EHCI mods by the periodic size. Why? */
503 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
506 MODULE_DESCRIPTION(DRIVER_DESC);
507 MODULE_AUTHOR(DRIVER_AUTHOR);
508 MODULE_LICENSE("GPL");
510 static int __init xhci_hcd_init(void)
515 retval = xhci_register_pci();
518 printk(KERN_DEBUG "Problem registering PCI driver.");
524 module_init(xhci_hcd_init);
526 static void __exit xhci_hcd_cleanup(void)
529 xhci_unregister_pci();
532 module_exit(xhci_hcd_cleanup);