1 /*
2  * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
3  *
4  * Derived from linux/arch/mips/bcm63xx/usb-common.c:
5  *	Copyright 2008 Maxime Bizon <mbizon@freebox.fr>
6  *	Copyright 2013 Florian Fainelli <florian@openwrt.org>
7  *
8  * SPDX-License-Identifier: GPL-2.0+
9  */
10 
11 #include <common.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 /* USBH Swap Control register */
19 #define USBH_SWAP_REG		0x00
20 #define USBH_SWAP_OHCI_DATA	BIT(0)
21 #define USBH_SWAP_OHCI_ENDIAN	BIT(1)
22 #define USBH_SWAP_EHCI_DATA	BIT(3)
23 #define USBH_SWAP_EHCI_ENDIAN	BIT(4)
24 
25 /* USBH Test register */
26 #define USBH_TEST_REG		0x24
27 #define USBH_TEST_PORT_CTL	0x1c0020
28 
29 struct bcm6358_usbh_priv {
30 	void __iomem *regs;
31 };
32 
33 static int bcm6358_usbh_init(struct phy *phy)
34 {
35 	struct bcm6358_usbh_priv *priv = dev_get_priv(phy->dev);
36 
37 	/* configure to work in native cpu endian */
38 	clrsetbits_be32(priv->regs + USBH_SWAP_REG,
39 			USBH_SWAP_EHCI_ENDIAN | USBH_SWAP_OHCI_ENDIAN,
40 			USBH_SWAP_EHCI_DATA | USBH_SWAP_OHCI_DATA);
41 
42 	/* test port control */
43 	writel_be(USBH_TEST_PORT_CTL, priv->regs + USBH_TEST_REG);
44 
45 	return 0;
46 }
47 
48 static struct phy_ops bcm6358_usbh_ops = {
49 	.init = bcm6358_usbh_init,
50 };
51 
52 static const struct udevice_id bcm6358_usbh_ids[] = {
53 	{ .compatible = "brcm,bcm6358-usbh" },
54 	{ /* sentinel */ }
55 };
56 
57 static int bcm6358_usbh_probe(struct udevice *dev)
58 {
59 	struct bcm6358_usbh_priv *priv = dev_get_priv(dev);
60 	struct reset_ctl rst_ctl;
61 	fdt_addr_t addr;
62 	fdt_size_t size;
63 	int ret;
64 
65 	addr = devfdt_get_addr_size_index(dev, 0, &size);
66 	if (addr == FDT_ADDR_T_NONE)
67 		return -EINVAL;
68 
69 	priv->regs = ioremap(addr, size);
70 
71 	/* perform reset */
72 	ret = reset_get_by_index(dev, 0, &rst_ctl);
73 	if (ret < 0)
74 		return ret;
75 
76 	ret = reset_deassert(&rst_ctl);
77 	if (ret < 0)
78 		return ret;
79 
80 	ret = reset_free(&rst_ctl);
81 	if (ret < 0)
82 		return ret;
83 
84 	return 0;
85 }
86 
87 U_BOOT_DRIVER(bcm6358_usbh) = {
88 	.name = "bcm6358-usbh",
89 	.id = UCLASS_PHY,
90 	.of_match = bcm6358_usbh_ids,
91 	.ops = &bcm6358_usbh_ops,
92 	.priv_auto_alloc_size = sizeof(struct bcm6358_usbh_priv),
93 	.probe = bcm6358_usbh_probe,
94 };
95