1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2024 9elements GmbH
4  *
5  * GENERIC USB HOST xHCI Controller
6  */
7 #include <dm.h>
8 #include <fdtdec.h>
9 #include <log.h>
10 #include <usb.h>
11 #include <asm/io.h>
12 #include <dm/device_compat.h>
13 #include <usb/xhci.h>
14 
15 struct generic_xhci_plat {
16 	fdt_addr_t hcd_base;
17 };
18 
19 /**
20  * Contains pointers to register base addresses
21  * for the usb controller.
22  */
23 struct generic_xhci {
24 	struct xhci_ctrl ctrl;	/* Needs to come first in this struct! */
25 	struct usb_plat usb_plat;
26 	struct xhci_hccr *hcd;
27 };
28 
xhci_usb_probe(struct udevice * dev)29 static int xhci_usb_probe(struct udevice *dev)
30 {
31 	struct generic_xhci_plat *plat = dev_get_plat(dev);
32 	struct generic_xhci *ctx = dev_get_priv(dev);
33 	struct xhci_hcor *hcor;
34 	int len;
35 
36 	ctx->hcd = (struct xhci_hccr *)phys_to_virt(plat->hcd_base);
37 	len = HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase));
38 	hcor = (struct xhci_hcor *)((uintptr_t)ctx->hcd + len);
39 
40 	return xhci_register(dev, ctx->hcd, hcor);
41 }
42 
xhci_usb_of_to_plat(struct udevice * dev)43 static int xhci_usb_of_to_plat(struct udevice *dev)
44 {
45 	struct generic_xhci_plat *plat = dev_get_plat(dev);
46 
47 	/*
48 	 * Get the base address for XHCI controller from the device node
49 	 */
50 	plat->hcd_base = dev_read_addr(dev);
51 	if (plat->hcd_base == FDT_ADDR_T_NONE) {
52 		dev_dbg(dev, "Can't get the XHCI register base address\n");
53 		return -ENXIO;
54 	}
55 
56 	return 0;
57 }
58 
59 static const struct udevice_id xhci_usb_ids[] = {
60 	{ .compatible = "generic-xhci" },
61 	{ }
62 };
63 
64 U_BOOT_DRIVER(usb_xhci) = {
65 	.name	= "xhci_generic",
66 	.id	= UCLASS_USB,
67 	.of_match = xhci_usb_ids,
68 	.of_to_plat = xhci_usb_of_to_plat,
69 	.probe = xhci_usb_probe,
70 	.remove = xhci_deregister,
71 	.ops	= &xhci_usb_ops,
72 	.plat_auto	= sizeof(struct generic_xhci_plat),
73 	.priv_auto	= sizeof(struct generic_xhci),
74 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
75 };
76