xref: /openbmc/u-boot/drivers/usb/host/xhci-mvebu.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Marvell International Ltd.
4  *
5  * MVEBU USB HOST xHCI Controller
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <fdtdec.h>
11 #include <usb.h>
12 #include <power/regulator.h>
13 #include <asm/gpio.h>
14 
15 #include "xhci.h"
16 
17 struct mvebu_xhci_platdata {
18 	fdt_addr_t hcd_base;
19 };
20 
21 /**
22  * Contains pointers to register base addresses
23  * for the usb controller.
24  */
25 struct mvebu_xhci {
26 	struct xhci_ctrl ctrl;	/* Needs to come first in this struct! */
27 	struct usb_platdata usb_plat;
28 	struct xhci_hccr *hcd;
29 };
30 
31 /*
32  * Dummy implementation that can be overwritten by a board
33  * specific function
34  */
35 __weak int board_xhci_enable(fdt_addr_t base)
36 {
37 	return 0;
38 }
39 
40 static int xhci_usb_probe(struct udevice *dev)
41 {
42 	struct mvebu_xhci_platdata *plat = dev_get_platdata(dev);
43 	struct mvebu_xhci *ctx = dev_get_priv(dev);
44 	struct xhci_hcor *hcor;
45 	int len, ret;
46 	struct udevice *regulator;
47 
48 	ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
49 	len = HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase));
50 	hcor = (struct xhci_hcor *)((uintptr_t)ctx->hcd + len);
51 
52 	ret = device_get_supply_regulator(dev, "vbus-supply", &regulator);
53 	if (!ret) {
54 		ret = regulator_set_enable(regulator, true);
55 		if (ret) {
56 			printf("Failed to turn ON the VBUS regulator\n");
57 			return ret;
58 		}
59 	}
60 
61 	/* Enable USB xHCI (VBUS, reset etc) in board specific code */
62 	board_xhci_enable(devfdt_get_addr_index(dev, 1));
63 
64 	return xhci_register(dev, ctx->hcd, hcor);
65 }
66 
67 static int xhci_usb_ofdata_to_platdata(struct udevice *dev)
68 {
69 	struct mvebu_xhci_platdata *plat = dev_get_platdata(dev);
70 
71 	/*
72 	 * Get the base address for XHCI controller from the device node
73 	 */
74 	plat->hcd_base = devfdt_get_addr(dev);
75 	if (plat->hcd_base == FDT_ADDR_T_NONE) {
76 		debug("Can't get the XHCI register base address\n");
77 		return -ENXIO;
78 	}
79 
80 	return 0;
81 }
82 
83 static const struct udevice_id xhci_usb_ids[] = {
84 	{ .compatible = "marvell,armada3700-xhci" },
85 	{ .compatible = "marvell,armada-380-xhci" },
86 	{ .compatible = "marvell,armada-8k-xhci" },
87 	{ }
88 };
89 
90 U_BOOT_DRIVER(usb_xhci) = {
91 	.name	= "xhci_mvebu",
92 	.id	= UCLASS_USB,
93 	.of_match = xhci_usb_ids,
94 	.ofdata_to_platdata = xhci_usb_ofdata_to_platdata,
95 	.probe = xhci_usb_probe,
96 	.remove = xhci_deregister,
97 	.ops	= &xhci_usb_ops,
98 	.platdata_auto_alloc_size = sizeof(struct mvebu_xhci_platdata),
99 	.priv_auto_alloc_size = sizeof(struct mvebu_xhci),
100 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
101 };
102