aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorSarah Sharp <sarah.a.sharp@linux.intel.com>2009-04-27 22:52:28 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2009-06-16 00:44:48 -0400
commit66d4eadd8d067269ea8fead1a50fe87c2979a80d (patch)
treeb0324868818058161e528bd194f6888456924fd4 /drivers/usb
parent74c6874199af98e602bb7c5fb1beb9cffda98729 (diff)
USB: xhci: BIOS handoff and HW initialization.
Add PCI initialization code to take control of the xHCI host controller away from the BIOS, halt, and reset the host controller. The xHCI spec says that BIOSes must give up the host controller within 5 seconds. Add some host controller glue functions to handle hardware initialization and memory allocation for the host controller. The current xHCI prototypes use PCI interrupts, but the xHCI spec requires MSI-X interrupts. Add code to support MSI-X interrupts, but use the PCI interrupts for now. Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/core/hcd.h1
-rw-r--r--drivers/usb/host/pci-quirks.c123
-rw-r--r--drivers/usb/host/xhci-hcd.c377
-rw-r--r--drivers/usb/host/xhci-mem.c75
-rw-r--r--drivers/usb/host/xhci-pci.c150
-rw-r--r--drivers/usb/host/xhci.h63
6 files changed, 787 insertions, 2 deletions
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 174170f14f06..7a47498b0aac 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -173,6 +173,7 @@ struct hc_driver {
173#define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */ 173#define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */
174#define HCD_USB11 0x0010 /* USB 1.1 */ 174#define HCD_USB11 0x0010 /* USB 1.1 */
175#define HCD_USB2 0x0020 /* USB 2.0 */ 175#define HCD_USB2 0x0020 /* USB 2.0 */
176#define HCD_USB3 0x0040 /* USB 3.0 */
176 177
177 /* called to init HCD and root hub */ 178 /* called to init HCD and root hub */
178 int (*reset) (struct usb_hcd *hcd); 179 int (*reset) (struct usb_hcd *hcd);
diff --git a/drivers/usb/host/pci-quirks.c b/drivers/usb/host/pci-quirks.c
index 033c2846ce59..83b5f9cea85a 100644
--- a/drivers/usb/host/pci-quirks.c
+++ b/drivers/usb/host/pci-quirks.c
@@ -15,6 +15,7 @@
15#include <linux/delay.h> 15#include <linux/delay.h>
16#include <linux/acpi.h> 16#include <linux/acpi.h>
17#include "pci-quirks.h" 17#include "pci-quirks.h"
18#include "xhci-ext-caps.h"
18 19
19 20
20#define UHCI_USBLEGSUP 0xc0 /* legacy support */ 21#define UHCI_USBLEGSUP 0xc0 /* legacy support */
@@ -341,7 +342,127 @@ static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
341 return; 342 return;
342} 343}
343 344
345/*
346 * handshake - spin reading a register until handshake completes
347 * @ptr: address of hc register to be read
348 * @mask: bits to look at in result of read
349 * @done: value of those bits when handshake succeeds
350 * @wait_usec: timeout in microseconds
351 * @delay_usec: delay in microseconds to wait between polling
352 *
353 * Polls a register every delay_usec microseconds.
354 * Returns 0 when the mask bits have the value done.
355 * Returns -ETIMEDOUT if this condition is not true after
356 * wait_usec microseconds have passed.
357 */
358static int handshake(void __iomem *ptr, u32 mask, u32 done,
359 int wait_usec, int delay_usec)
360{
361 u32 result;
362
363 do {
364 result = readl(ptr);
365 result &= mask;
366 if (result == done)
367 return 0;
368 udelay(delay_usec);
369 wait_usec -= delay_usec;
370 } while (wait_usec > 0);
371 return -ETIMEDOUT;
372}
373
374/**
375 * PCI Quirks for xHCI.
376 *
377 * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
378 * It signals to the BIOS that the OS wants control of the host controller,
379 * and then waits 5 seconds for the BIOS to hand over control.
380 * If we timeout, assume the BIOS is broken and take control anyway.
381 */
382static void __devinit quirk_usb_handoff_xhci(struct pci_dev *pdev)
383{
384 void __iomem *base;
385 int ext_cap_offset;
386 void __iomem *op_reg_base;
387 u32 val;
388 int timeout;
389
390 if (!mmio_resource_enabled(pdev, 0))
391 return;
392
393 base = ioremap_nocache(pci_resource_start(pdev, 0),
394 pci_resource_len(pdev, 0));
395 if (base == NULL)
396 return;
344 397
398 /*
399 * Find the Legacy Support Capability register -
400 * this is optional for xHCI host controllers.
401 */
402 ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
403 do {
404 if (!ext_cap_offset)
405 /* We've reached the end of the extended capabilities */
406 goto hc_init;
407 val = readl(base + ext_cap_offset);
408 if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
409 break;
410 ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
411 } while (1);
412
413 /* If the BIOS owns the HC, signal that the OS wants it, and wait */
414 if (val & XHCI_HC_BIOS_OWNED) {
415 writel(val & XHCI_HC_OS_OWNED, base + ext_cap_offset);
416
417 /* Wait for 5 seconds with 10 microsecond polling interval */
418 timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
419 0, 5000, 10);
420
421 /* Assume a buggy BIOS and take HC ownership anyway */
422 if (timeout) {
423 dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
424 " (BIOS bug ?) %08x\n", val);
425 writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
426 }
427 }
428
429 /* Disable any BIOS SMIs */
430 writel(XHCI_LEGACY_DISABLE_SMI,
431 base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
432
433hc_init:
434 op_reg_base = base + XHCI_HC_LENGTH(readl(base));
435
436 /* Wait for the host controller to be ready before writing any
437 * operational or runtime registers. Wait 5 seconds and no more.
438 */
439 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
440 5000, 10);
441 /* Assume a buggy HC and start HC initialization anyway */
442 if (timeout) {
443 val = readl(op_reg_base + XHCI_STS_OFFSET);
444 dev_warn(&pdev->dev,
445 "xHCI HW not ready after 5 sec (HC bug?) "
446 "status = 0x%x\n", val);
447 }
448
449 /* Send the halt and disable interrupts command */
450 val = readl(op_reg_base + XHCI_CMD_OFFSET);
451 val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
452 writel(val, op_reg_base + XHCI_CMD_OFFSET);
453
454 /* Wait for the HC to halt - poll every 125 usec (one microframe). */
455 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
456 XHCI_MAX_HALT_USEC, 125);
457 if (timeout) {
458 val = readl(op_reg_base + XHCI_STS_OFFSET);
459 dev_warn(&pdev->dev,
460 "xHCI HW did not halt within %d usec "
461 "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
462 }
463
464 iounmap(base);
465}
345 466
346static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev) 467static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
347{ 468{
@@ -351,5 +472,7 @@ static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
351 quirk_usb_handoff_ohci(pdev); 472 quirk_usb_handoff_ohci(pdev);
352 else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI) 473 else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
353 quirk_usb_disable_ehci(pdev); 474 quirk_usb_disable_ehci(pdev);
475 else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
476 quirk_usb_handoff_xhci(pdev);
354} 477}
355DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff); 478DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);
diff --git a/drivers/usb/host/xhci-hcd.c b/drivers/usb/host/xhci-hcd.c
new file mode 100644
index 000000000000..64fcc22e9d59
--- /dev/null
+++ b/drivers/usb/host/xhci-hcd.c
@@ -0,0 +1,377 @@
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
23#include <linux/irq.h>
24#include <linux/module.h>
25
26#include "xhci.h"
27
28#define DRIVER_AUTHOR "Sarah Sharp"
29#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
30
31/* TODO: copied from ehci-hcd.c - can this be refactored? */
32/*
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
38 *
39 * Returns negative errno, or zero on success
40 *
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).
44 */
45static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
46 u32 mask, u32 done, int usec)
47{
48 u32 result;
49
50 do {
51 result = xhci_readl(xhci, ptr);
52 if (result == ~(u32)0) /* card removed */
53 return -ENODEV;
54 result &= mask;
55 if (result == done)
56 return 0;
57 udelay(1);
58 usec--;
59 } while (usec > 0);
60 return -ETIMEDOUT;
61}
62
63/*
64 * Force HC into halt state.
65 *
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?
71 */
72int xhci_halt(struct xhci_hcd *xhci)
73{
74 u32 halted;
75 u32 cmd;
76 u32 mask;
77
78 xhci_dbg(xhci, "// Halt the HC\n");
79 /* Disable all interrupts from the host controller */
80 mask = ~(XHCI_IRQS);
81 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
82 if (!halted)
83 mask &= ~CMD_RUN;
84
85 cmd = xhci_readl(xhci, &xhci->op_regs->command);
86 cmd &= mask;
87 xhci_writel(xhci, cmd, &xhci->op_regs->command);
88
89 return handshake(xhci, &xhci->op_regs->status,
90 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
91}
92
93/*
94 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
95 *
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.
99 */
100int xhci_reset(struct xhci_hcd *xhci)
101{
102 u32 command;
103 u32 state;
104
105 state = xhci_readl(xhci, &xhci->op_regs->status);
106 BUG_ON((state & STS_HALT) == 0);
107
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;
114
115 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
116}
117
118/*
119 * Stop the HC from processing the endpoint queues.
120 */
121static void xhci_quiesce(struct xhci_hcd *xhci)
122{
123 /*
124 * Queues are per endpoint, so we need to disable an endpoint or slot.
125 *
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).
129 *
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.
134 *
135 * TODO: Implement after command ring code is done.
136 */
137 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
138 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
139}
140
141#if 0
142/* Set up MSI-X table for entry 0 (may claim other entries later) */
143static int xhci_setup_msix(struct xhci_hcd *xhci)
144{
145 int ret;
146 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
147
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");
153 return -ENOMEM;
154 }
155 xhci->msix_entries[0].entry = 0;
156
157 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
158 if (ret) {
159 xhci_err(xhci, "Failed to enable MSI-X\n");
160 goto free_entries;
161 }
162
163 /*
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.
166 */
167 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
168 "xHCI", xhci_to_hcd(xhci));
169 if (ret) {
170 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
171 goto disable_msix;
172 }
173 xhci_dbg(xhci, "Finished setting up MSI-X\n");
174 return 0;
175
176disable_msix:
177 pci_disable_msix(pdev);
178free_entries:
179 kfree(xhci->msix_entries);
180 xhci->msix_entries = NULL;
181 return ret;
182}
183
184/* XXX: code duplication; can xhci_setup_msix call this? */
185/* Free any IRQs and disable MSI-X */
186static void xhci_cleanup_msix(struct xhci_hcd *xhci)
187{
188 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
189 if (!xhci->msix_entries)
190 return;
191
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");
197}
198#endif
199
200/*
201 * Initialize memory for HCD and xHC (one-time init).
202 *
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).
206 */
207int xhci_init(struct usb_hcd *hcd)
208{
209 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
210 int retval = 0;
211
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");
216
217 return retval;
218}
219
220/*
221 * Start the HC after it was halted.
222 *
223 * This function is called by the USB core when the HC driver is added.
224 * Its opposite is xhci_stop().
225 *
226 * xhci_init() must be called once before this function can be called.
227 * Reset the HC, enable device slot contexts, program DCBAAP, and
228 * set command ring pointer and event ring pointer.
229 *
230 * Setup MSI-X vectors and enable interrupts.
231 */
232int xhci_run(struct usb_hcd *hcd)
233{
234 u32 temp;
235 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
236 xhci_dbg(xhci, "xhci_run\n");
237
238#if 0 /* FIXME: MSI not setup yet */
239 /* Do this at the very last minute */
240 ret = xhci_setup_msix(xhci);
241 if (!ret)
242 return ret;
243
244 return -ENOSYS;
245#endif
246 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
247 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
248 temp &= 0xffff;
249 temp |= (u32) 160;
250 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
251
252 /* Set the HCD state before we enable the irqs */
253 hcd->state = HC_STATE_RUNNING;
254 temp = xhci_readl(xhci, &xhci->op_regs->command);
255 temp |= (CMD_EIE);
256 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
257 temp);
258 xhci_writel(xhci, temp, &xhci->op_regs->command);
259
260 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
261 xhci_dbg(xhci, "// Enabling event ring interrupter 0x%x"
262 " by writing 0x%x to irq_pending\n",
263 (unsigned int) xhci->ir_set,
264 (unsigned int) ER_IRQ_ENABLE(temp));
265 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
266 &xhci->ir_set->irq_pending);
267 xhci_print_ir_set(xhci, xhci->ir_set, 0);
268
269 temp = xhci_readl(xhci, &xhci->op_regs->command);
270 temp |= (CMD_RUN);
271 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
272 temp);
273 xhci_writel(xhci, temp, &xhci->op_regs->command);
274 /* Flush PCI posted writes */
275 temp = xhci_readl(xhci, &xhci->op_regs->command);
276 xhci_dbg(xhci, "// @%x = 0x%x\n",
277 (unsigned int) &xhci->op_regs->command, temp);
278
279 xhci_dbg(xhci, "Finished xhci_run\n");
280 return 0;
281}
282
283/*
284 * Stop xHCI driver.
285 *
286 * This function is called by the USB core when the HC driver is removed.
287 * Its opposite is xhci_run().
288 *
289 * Disable device contexts, disable IRQs, and quiesce the HC.
290 * Reset the HC, finish any completed transactions, and cleanup memory.
291 */
292void xhci_stop(struct usb_hcd *hcd)
293{
294 u32 temp;
295 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
296
297 spin_lock_irq(&xhci->lock);
298 if (HC_IS_RUNNING(hcd->state))
299 xhci_quiesce(xhci);
300 xhci_halt(xhci);
301 xhci_reset(xhci);
302 spin_unlock_irq(&xhci->lock);
303
304#if 0 /* No MSI yet */
305 xhci_cleanup_msix(xhci);
306#endif
307 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
308 temp = xhci_readl(xhci, &xhci->op_regs->status);
309 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
310 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
311 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
312 &xhci->ir_set->irq_pending);
313 xhci_print_ir_set(xhci, xhci->ir_set, 0);
314
315 xhci_dbg(xhci, "cleaning up memory\n");
316 xhci_mem_cleanup(xhci);
317 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
318 xhci_readl(xhci, &xhci->op_regs->status));
319}
320
321/*
322 * Shutdown HC (not bus-specific)
323 *
324 * This is called when the machine is rebooting or halting. We assume that the
325 * machine will be powered off, and the HC's internal state will be reset.
326 * Don't bother to free memory.
327 */
328void xhci_shutdown(struct usb_hcd *hcd)
329{
330 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
331
332 spin_lock_irq(&xhci->lock);
333 xhci_halt(xhci);
334 spin_unlock_irq(&xhci->lock);
335
336#if 0
337 xhci_cleanup_msix(xhci);
338#endif
339
340 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
341 xhci_readl(xhci, &xhci->op_regs->status));
342}
343
344int xhci_get_frame(struct usb_hcd *hcd)
345{
346 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
347 /* EHCI mods by the periodic size. Why? */
348 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
349}
350
351MODULE_DESCRIPTION(DRIVER_DESC);
352MODULE_AUTHOR(DRIVER_AUTHOR);
353MODULE_LICENSE("GPL");
354
355static int __init xhci_hcd_init(void)
356{
357#ifdef CONFIG_PCI
358 int retval = 0;
359
360 retval = xhci_register_pci();
361
362 if (retval < 0) {
363 printk(KERN_DEBUG "Problem registering PCI driver.");
364 return retval;
365 }
366#endif
367 return 0;
368}
369module_init(xhci_hcd_init);
370
371static void __exit xhci_hcd_cleanup(void)
372{
373#ifdef CONFIG_PCI
374 xhci_unregister_pci();
375#endif
376}
377module_exit(xhci_hcd_cleanup);
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
new file mode 100644
index 000000000000..0e383f9c380c
--- /dev/null
+++ b/drivers/usb/host/xhci-mem.c
@@ -0,0 +1,75 @@
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
23#include <linux/usb.h>
24
25#include "xhci.h"
26
27void xhci_mem_cleanup(struct xhci_hcd *xhci)
28{
29 xhci->page_size = 0;
30 xhci->page_shift = 0;
31}
32
33int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
34{
35 unsigned int val, val2;
36 u32 page_size;
37 int i;
38
39 page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
40 xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
41 for (i = 0; i < 16; i++) {
42 if ((0x1 & page_size) != 0)
43 break;
44 page_size = page_size >> 1;
45 }
46 if (i < 16)
47 xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
48 else
49 xhci_warn(xhci, "WARN: no supported page size\n");
50 /* Use 4K pages, since that's common and the minimum the HC supports */
51 xhci->page_shift = 12;
52 xhci->page_size = 1 << xhci->page_shift;
53 xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
54
55 /*
56 * Program the Number of Device Slots Enabled field in the CONFIG
57 * register with the max value of slots the HC can handle.
58 */
59 val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
60 xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
61 (unsigned int) val);
62 val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
63 val |= (val2 & ~HCS_SLOTS_MASK);
64 xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
65 (unsigned int) val);
66 xhci_writel(xhci, val, &xhci->op_regs->config_reg);
67
68 xhci->ir_set = &xhci->run_regs->ir_set[0];
69
70 return 0;
71fail:
72 xhci_warn(xhci, "Couldn't initialize memory\n");
73 xhci_mem_cleanup(xhci);
74 return -ENOMEM;
75}
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
new file mode 100644
index 000000000000..4015082adf60
--- /dev/null
+++ b/drivers/usb/host/xhci-pci.c
@@ -0,0 +1,150 @@
1/*
2 * xHCI host controller driver PCI Bus Glue.
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
23#include <linux/pci.h>
24
25#include "xhci.h"
26
27static const char hcd_name[] = "xhci_hcd";
28
29/* called after powerup, by probe or system-pm "wakeup" */
30static int xhci_pci_reinit(struct xhci_hcd *xhci, struct pci_dev *pdev)
31{
32 /*
33 * TODO: Implement finding debug ports later.
34 * TODO: see if there are any quirks that need to be added to handle
35 * new extended capabilities.
36 */
37
38 /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
39 if (!pci_set_mwi(pdev))
40 xhci_dbg(xhci, "MWI active\n");
41
42 xhci_dbg(xhci, "Finished xhci_pci_reinit\n");
43 return 0;
44}
45
46/* called during probe() after chip reset completes */
47static int xhci_pci_setup(struct usb_hcd *hcd)
48{
49 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
50 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
51 int retval;
52
53 xhci->cap_regs = hcd->regs;
54 xhci->op_regs = hcd->regs +
55 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
56 xhci->run_regs = hcd->regs +
57 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
58 /* Cache read-only capability registers */
59 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
60 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
61 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
62 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
63 xhci_print_registers(xhci);
64
65 /* Make sure the HC is halted. */
66 retval = xhci_halt(xhci);
67 if (retval)
68 return retval;
69
70 xhci_dbg(xhci, "Resetting HCD\n");
71 /* Reset the internal HC memory state and registers. */
72 retval = xhci_reset(xhci);
73 if (retval)
74 return retval;
75 xhci_dbg(xhci, "Reset complete\n");
76
77 xhci_dbg(xhci, "Calling HCD init\n");
78 /* Initialize HCD and host controller data structures. */
79 retval = xhci_init(hcd);
80 if (retval)
81 return retval;
82 xhci_dbg(xhci, "Called HCD init\n");
83
84 pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
85 xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn);
86
87 /* Find any debug ports */
88 return xhci_pci_reinit(xhci, pdev);
89}
90
91static const struct hc_driver xhci_pci_hc_driver = {
92 .description = hcd_name,
93 .product_desc = "xHCI Host Controller",
94 .hcd_priv_size = sizeof(struct xhci_hcd),
95
96 /*
97 * generic hardware linkage
98 */
99 .flags = HCD_MEMORY | HCD_USB3,
100
101 /*
102 * basic lifecycle operations
103 */
104 .reset = xhci_pci_setup,
105 .start = xhci_run,
106 /* suspend and resume implemented later */
107 .stop = xhci_stop,
108 .shutdown = xhci_shutdown,
109
110 /*
111 * scheduling support
112 */
113 .get_frame_number = xhci_get_frame,
114
115 /* Implement root hub support later. */
116};
117
118/*-------------------------------------------------------------------------*/
119
120/* PCI driver selection metadata; PCI hotplugging uses this */
121static const struct pci_device_id pci_ids[] = { {
122 /* handle any USB 3.0 xHCI controller */
123 PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0),
124 .driver_data = (unsigned long) &xhci_pci_hc_driver,
125 },
126 { /* end: all zeroes */ }
127};
128MODULE_DEVICE_TABLE(pci, pci_ids);
129
130/* pci driver glue; this is a "new style" PCI driver module */
131static struct pci_driver xhci_pci_driver = {
132 .name = (char *) hcd_name,
133 .id_table = pci_ids,
134
135 .probe = usb_hcd_pci_probe,
136 .remove = usb_hcd_pci_remove,
137 /* suspend and resume implemented later */
138
139 .shutdown = usb_hcd_pci_shutdown,
140};
141
142int xhci_register_pci()
143{
144 return pci_register_driver(&xhci_pci_driver);
145}
146
147void xhci_unregister_pci()
148{
149 pci_unregister_driver(&xhci_pci_driver);
150}
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index a4d44aad0697..59fae2e5ea59 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -32,6 +32,9 @@
32/* xHCI PCI Configuration Registers */ 32/* xHCI PCI Configuration Registers */
33#define XHCI_SBRN_OFFSET (0x60) 33#define XHCI_SBRN_OFFSET (0x60)
34 34
35/* Max number of USB devices for any host controller - limit in section 6.1 */
36#define MAX_HC_SLOTS 256
37
35/* 38/*
36 * xHCI register interface. 39 * xHCI register interface.
37 * This corresponds to the eXtensible Host Controller Interface (xHCI) 40 * This corresponds to the eXtensible Host Controller Interface (xHCI)
@@ -359,10 +362,35 @@ struct intr_reg {
359 u32 erst_dequeue[2]; 362 u32 erst_dequeue[2];
360} __attribute__ ((packed)); 363} __attribute__ ((packed));
361 364
365/* irq_pending bitmasks */
362#define ER_IRQ_PENDING(p) ((p) & 0x1) 366#define ER_IRQ_PENDING(p) ((p) & 0x1)
363#define ER_IRQ_ENABLE(p) ((p) | 0x2) 367/* bits 2:31 need to be preserved */
368#define ER_IRQ_CLEAR(p) ((p) & 0xfffffffe)
369#define ER_IRQ_ENABLE(p) ((ER_IRQ_CLEAR(p)) | 0x2)
370#define ER_IRQ_DISABLE(p) ((ER_IRQ_CLEAR(p)) & ~(0x2))
371
372/* irq_control bitmasks */
373/* Minimum interval between interrupts (in 250ns intervals). The interval
374 * between interrupts will be longer if there are no events on the event ring.
375 * Default is 4000 (1 ms).
376 */
377#define ER_IRQ_INTERVAL_MASK (0xffff)
378/* Counter used to count down the time to the next interrupt - HW use only */
379#define ER_IRQ_COUNTER_MASK (0xffff << 16)
380
381/* erst_size bitmasks */
364/* Preserve bits 16:31 of erst_size */ 382/* Preserve bits 16:31 of erst_size */
365#define ERST_SIZE_MASK (0xffff<<16) 383#define ERST_SIZE_MASK (0xffff << 16)
384
385/* erst_dequeue bitmasks */
386/* Dequeue ERST Segment Index (DESI) - Segment number (or alias)
387 * where the current dequeue pointer lies. This is an optional HW hint.
388 */
389#define ERST_DESI_MASK (0x7)
390/* Event Handler Busy (EHB) - is the event ring scheduled to be serviced by
391 * a work queue (or delayed service routine)?
392 */
393#define ERST_EHB (1 << 3)
366 394
367/** 395/**
368 * struct xhci_run_regs 396 * struct xhci_run_regs
@@ -386,6 +414,8 @@ struct xhci_hcd {
386 struct xhci_cap_regs __iomem *cap_regs; 414 struct xhci_cap_regs __iomem *cap_regs;
387 struct xhci_op_regs __iomem *op_regs; 415 struct xhci_op_regs __iomem *op_regs;
388 struct xhci_run_regs __iomem *run_regs; 416 struct xhci_run_regs __iomem *run_regs;
417 /* Our HCD's current interrupter register set */
418 struct intr_reg __iomem *ir_set;
389 419
390 /* Cached register copies of read-only HC data */ 420 /* Cached register copies of read-only HC data */
391 __u32 hcs_params1; 421 __u32 hcs_params1;
@@ -404,7 +434,13 @@ struct xhci_hcd {
404 u8 isoc_threshold; 434 u8 isoc_threshold;
405 int event_ring_max; 435 int event_ring_max;
406 int addr_64; 436 int addr_64;
437 /* 4KB min, 128MB max */
407 int page_size; 438 int page_size;
439 /* Valid values are 12 to 20, inclusive */
440 int page_shift;
441 /* only one MSI vector for now, but might need more later */
442 int msix_count;
443 struct msix_entry *msix_entries;
408}; 444};
409 445
410/* convert between an HCD pointer and the corresponding EHCI_HCD */ 446/* convert between an HCD pointer and the corresponding EHCI_HCD */
@@ -449,4 +485,27 @@ static inline void xhci_writel(const struct xhci_hcd *xhci,
449 writel(val, regs); 485 writel(val, regs);
450} 486}
451 487
488/* xHCI debugging */
489void xhci_print_ir_set(struct xhci_hcd *xhci, struct intr_reg *ir_set, int set_num);
490void xhci_print_registers(struct xhci_hcd *xhci);
491
492/* xHCI memory managment */
493void xhci_mem_cleanup(struct xhci_hcd *xhci);
494int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags);
495
496#ifdef CONFIG_PCI
497/* xHCI PCI glue */
498int xhci_register_pci(void);
499void xhci_unregister_pci(void);
500#endif
501
502/* xHCI host controller glue */
503int xhci_halt(struct xhci_hcd *xhci);
504int xhci_reset(struct xhci_hcd *xhci);
505int xhci_init(struct usb_hcd *hcd);
506int xhci_run(struct usb_hcd *hcd);
507void xhci_stop(struct usb_hcd *hcd);
508void xhci_shutdown(struct usb_hcd *hcd);
509int xhci_get_frame(struct usb_hcd *hcd);
510
452#endif /* __LINUX_XHCI_HCD_H */ 511#endif /* __LINUX_XHCI_HCD_H */