1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015, Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  * All rights reserved.
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <init.h>
11 #include <log.h>
12 #include <pci.h>
13 #include <usb.h>
14 #include <usb/xhci.h>
15 
xhci_pci_init(struct udevice * dev,struct xhci_hccr ** ret_hccr,struct xhci_hcor ** ret_hcor)16 static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
17 			 struct xhci_hcor **ret_hcor)
18 {
19 	struct xhci_hccr *hccr;
20 	struct xhci_hcor *hcor;
21 	u32 cmd;
22 
23 	hccr = (struct xhci_hccr *)dm_pci_map_bar(dev,
24 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
25 	if (!hccr) {
26 		printf("xhci-pci init cannot map PCI mem bar\n");
27 		return -EIO;
28 	}
29 
30 	hcor = (struct xhci_hcor *)((uintptr_t) hccr +
31 			HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
32 
33 	debug("XHCI-PCI init hccr %p and hcor %p hc_length %d\n",
34 	      hccr, hcor, (u32)HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
35 
36 	*ret_hccr = hccr;
37 	*ret_hcor = hcor;
38 
39 	/* enable busmaster */
40 	dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
41 	cmd |= PCI_COMMAND_MASTER;
42 	dm_pci_write_config32(dev, PCI_COMMAND, cmd);
43 	return 0;
44 }
45 
xhci_pci_probe(struct udevice * dev)46 static int xhci_pci_probe(struct udevice *dev)
47 {
48 	struct xhci_hccr *hccr;
49 	struct xhci_hcor *hcor;
50 	int ret;
51 
52 	ret = xhci_pci_init(dev, &hccr, &hcor);
53 	if (ret)
54 		return ret;
55 
56 	return xhci_register(dev, hccr, hcor);
57 }
58 
59 static const struct udevice_id xhci_pci_ids[] = {
60 	{ .compatible = "xhci-pci" },
61 	{ }
62 };
63 
64 U_BOOT_DRIVER(xhci_pci) = {
65 	.name	= "xhci_pci",
66 	.id	= UCLASS_USB,
67 	.probe = xhci_pci_probe,
68 	.remove = xhci_deregister,
69 	.of_match = xhci_pci_ids,
70 	.ops	= &xhci_usb_ops,
71 	.plat_auto	= sizeof(struct usb_plat),
72 	.priv_auto	= sizeof(struct xhci_ctrl),
73 	.flags	= DM_FLAG_OS_PREPARE | DM_FLAG_ALLOC_PRIV_DMA,
74 };
75 
76 static struct pci_device_id xhci_pci_supported[] = {
77 	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0) },
78 	{},
79 };
80 
81 U_BOOT_PCI_DEVICE(xhci_pci, xhci_pci_supported);
82