1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
4  *
5  * Derived from linux/arch/mips/bcm63xx/usb-common.c:
6  *	Copyright 2008 Maxime Bizon <mbizon@freebox.fr>
7  *	Copyright 2013 Florian Fainelli <florian@openwrt.org>
8  */
9 
10 #include <common.h>
11 #include <clk.h>
12 #include <dm.h>
13 #include <generic-phy.h>
14 #include <reset.h>
15 #include <asm/io.h>
16 #include <dm/device.h>
17 
18 #define USBH_SETUP_PORT1_EN	BIT(0)
19 
20 struct bcm6348_usbh_priv {
21 	void __iomem *regs;
22 };
23 
24 static int bcm6348_usbh_init(struct phy *phy)
25 {
26 	struct bcm6348_usbh_priv *priv = dev_get_priv(phy->dev);
27 
28 	writel_be(USBH_SETUP_PORT1_EN, priv->regs);
29 
30 	return 0;
31 }
32 
33 static struct phy_ops bcm6348_usbh_ops = {
34 	.init = bcm6348_usbh_init,
35 };
36 
37 static const struct udevice_id bcm6348_usbh_ids[] = {
38 	{ .compatible = "brcm,bcm6348-usbh" },
39 	{ /* sentinel */ }
40 };
41 
42 static int bcm6348_usbh_probe(struct udevice *dev)
43 {
44 	struct bcm6348_usbh_priv *priv = dev_get_priv(dev);
45 	struct reset_ctl rst_ctl;
46 	struct clk clk;
47 	fdt_addr_t addr;
48 	fdt_size_t size;
49 	int ret;
50 
51 	addr = devfdt_get_addr_size_index(dev, 0, &size);
52 	if (addr == FDT_ADDR_T_NONE)
53 		return -EINVAL;
54 
55 	priv->regs = ioremap(addr, size);
56 
57 	/* enable usbh clock */
58 	ret = clk_get_by_name(dev, "usbh", &clk);
59 	if (ret < 0)
60 		return ret;
61 
62 	ret = clk_enable(&clk);
63 	if (ret < 0)
64 		return ret;
65 
66 	ret = clk_free(&clk);
67 	if (ret < 0)
68 		return ret;
69 
70 	/* perform reset */
71 	ret = reset_get_by_index(dev, 0, &rst_ctl);
72 	if (ret < 0)
73 		return ret;
74 
75 	ret = reset_deassert(&rst_ctl);
76 	if (ret < 0)
77 		return ret;
78 
79 	ret = reset_free(&rst_ctl);
80 	if (ret < 0)
81 		return ret;
82 
83 	return 0;
84 }
85 
86 U_BOOT_DRIVER(bcm6348_usbh) = {
87 	.name = "bcm6348-usbh",
88 	.id = UCLASS_PHY,
89 	.of_match = bcm6348_usbh_ids,
90 	.ops = &bcm6348_usbh_ops,
91 	.priv_auto_alloc_size = sizeof(struct bcm6348_usbh_priv),
92 	.probe = bcm6348_usbh_probe,
93 };
94