xref: /rk3399_rockchip-uboot/drivers/usb/host/ohci-sunxi.c (revision 045ae7e3393fc940b183b3f2ea9c9edd62d9e810)
16a72e804SHans de Goede /*
26a72e804SHans de Goede  * Sunxi ohci glue
36a72e804SHans de Goede  *
46a72e804SHans de Goede  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
56a72e804SHans de Goede  *
66a72e804SHans de Goede  * Based on code from
76a72e804SHans de Goede  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
86a72e804SHans de Goede  *
96a72e804SHans de Goede  * SPDX-License-Identifier:	GPL-2.0+
106a72e804SHans de Goede  */
116a72e804SHans de Goede 
126a72e804SHans de Goede #include <common.h>
136a72e804SHans de Goede #include <asm/arch/clock.h>
146a72e804SHans de Goede #include <asm/arch/usb_phy.h>
156a72e804SHans de Goede #include <asm/io.h>
166a72e804SHans de Goede #include <dm.h>
176a72e804SHans de Goede #include <usb.h>
186a72e804SHans de Goede #include "ohci.h"
196a72e804SHans de Goede 
20948603d4SHans de Goede #ifdef CONFIG_SUNXI_GEN_SUN4I
21948603d4SHans de Goede #define BASE_DIST		0x8000
22948603d4SHans de Goede #define AHB_CLK_DIST		2
23948603d4SHans de Goede #else
24948603d4SHans de Goede #define BASE_DIST		0x1000
25948603d4SHans de Goede #define AHB_CLK_DIST		1
26948603d4SHans de Goede #endif
27948603d4SHans de Goede 
286a72e804SHans de Goede struct ohci_sunxi_priv {
296a72e804SHans de Goede 	ohci_t ohci;
306a72e804SHans de Goede 	int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
316a72e804SHans de Goede 	int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
326a72e804SHans de Goede 	int phy_index;     /* Index of the usb-phy attached to this hcd */
336a72e804SHans de Goede };
346a72e804SHans de Goede 
356a72e804SHans de Goede static int ohci_usb_probe(struct udevice *dev)
366a72e804SHans de Goede {
376a72e804SHans de Goede 	struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
386a72e804SHans de Goede 	struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
396a72e804SHans de Goede 	struct ohci_sunxi_priv *priv = dev_get_priv(dev);
406a72e804SHans de Goede 	struct ohci_regs *regs = (struct ohci_regs *)dev_get_addr(dev);
416a72e804SHans de Goede 
426a72e804SHans de Goede 	bus_priv->companion = true;
436a72e804SHans de Goede 
446a72e804SHans de Goede 	/*
456a72e804SHans de Goede 	 * This should go away once we've moved to the driver model for
466a72e804SHans de Goede 	 * clocks resp. phys.
476a72e804SHans de Goede 	 */
486a72e804SHans de Goede 	priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
49dc44fd8aSJelle van der Waa #ifdef CONFIG_MACH_SUN8I_H3
50dc44fd8aSJelle van der Waa 	priv->ahb_gate_mask |= 1 << AHB_GATE_OFFSET_USB_EHCI0;
51dc44fd8aSJelle van der Waa #endif
526a72e804SHans de Goede 	priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
53948603d4SHans de Goede 	priv->phy_index = ((u32)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST;
54948603d4SHans de Goede 	priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
55948603d4SHans de Goede 	priv->usb_gate_mask <<= priv->phy_index;
56948603d4SHans de Goede 	priv->phy_index++; /* Non otg phys start at 1 */
576a72e804SHans de Goede 
586a72e804SHans de Goede 	setbits_le32(&ccm->ahb_gate0, priv->ahb_gate_mask);
596a72e804SHans de Goede 	setbits_le32(&ccm->usb_clk_cfg, priv->usb_gate_mask);
606a72e804SHans de Goede #ifdef CONFIG_SUNXI_GEN_SUN6I
616a72e804SHans de Goede 	setbits_le32(&ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
626a72e804SHans de Goede #endif
636a72e804SHans de Goede 
646a72e804SHans de Goede 	sunxi_usb_phy_init(priv->phy_index);
656a72e804SHans de Goede 	sunxi_usb_phy_power_on(priv->phy_index);
666a72e804SHans de Goede 
676a72e804SHans de Goede 	return ohci_register(dev, regs);
686a72e804SHans de Goede }
696a72e804SHans de Goede 
706a72e804SHans de Goede static int ohci_usb_remove(struct udevice *dev)
716a72e804SHans de Goede {
726a72e804SHans de Goede 	struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
736a72e804SHans de Goede 	struct ohci_sunxi_priv *priv = dev_get_priv(dev);
746a72e804SHans de Goede 	int ret;
756a72e804SHans de Goede 
766a72e804SHans de Goede 	ret = ohci_deregister(dev);
776a72e804SHans de Goede 	if (ret)
786a72e804SHans de Goede 		return ret;
796a72e804SHans de Goede 
806a72e804SHans de Goede 	sunxi_usb_phy_exit(priv->phy_index);
816a72e804SHans de Goede 
826a72e804SHans de Goede #ifdef CONFIG_SUNXI_GEN_SUN6I
836a72e804SHans de Goede 	clrbits_le32(&ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
846a72e804SHans de Goede #endif
856a72e804SHans de Goede 	clrbits_le32(&ccm->usb_clk_cfg, priv->usb_gate_mask);
866a72e804SHans de Goede 	clrbits_le32(&ccm->ahb_gate0, priv->ahb_gate_mask);
876a72e804SHans de Goede 
886a72e804SHans de Goede 	return 0;
896a72e804SHans de Goede }
906a72e804SHans de Goede 
916a72e804SHans de Goede static const struct udevice_id ohci_usb_ids[] = {
926a72e804SHans de Goede 	{ .compatible = "allwinner,sun4i-a10-ohci", },
936a72e804SHans de Goede 	{ .compatible = "allwinner,sun5i-a13-ohci", },
946a72e804SHans de Goede 	{ .compatible = "allwinner,sun6i-a31-ohci", },
956a72e804SHans de Goede 	{ .compatible = "allwinner,sun7i-a20-ohci", },
966a72e804SHans de Goede 	{ .compatible = "allwinner,sun8i-a23-ohci", },
97*045ae7e3SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a83t-ohci", },
98dc44fd8aSJelle van der Waa 	{ .compatible = "allwinner,sun8i-h3-ohci",  },
996a72e804SHans de Goede 	{ .compatible = "allwinner,sun9i-a80-ohci", },
1006a72e804SHans de Goede 	{ }
1016a72e804SHans de Goede };
1026a72e804SHans de Goede 
1036a72e804SHans de Goede U_BOOT_DRIVER(usb_ohci) = {
1046a72e804SHans de Goede 	.name	= "ohci_sunxi",
1056a72e804SHans de Goede 	.id	= UCLASS_USB,
1066a72e804SHans de Goede 	.of_match = ohci_usb_ids,
1076a72e804SHans de Goede 	.probe = ohci_usb_probe,
1086a72e804SHans de Goede 	.remove = ohci_usb_remove,
1096a72e804SHans de Goede 	.ops	= &ohci_usb_ops,
1106a72e804SHans de Goede 	.platdata_auto_alloc_size = sizeof(struct usb_platdata),
1116a72e804SHans de Goede 	.priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
1126a72e804SHans de Goede 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
1136a72e804SHans de Goede };
114