xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-sunxi.c (revision fb3bfbb24a72a495f3fb1716d386fb4bbe5990fa)
18d154002SRoman Byshko /*
27b798658SHans de Goede  * Sunxi ehci glue
38d154002SRoman Byshko  *
47b798658SHans de Goede  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
57b798658SHans de Goede  * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
68d154002SRoman Byshko  *
78d154002SRoman Byshko  * Based on code from
88d154002SRoman Byshko  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
98d154002SRoman Byshko  *
108d154002SRoman Byshko  * SPDX-License-Identifier:	GPL-2.0+
118d154002SRoman Byshko  */
128d154002SRoman Byshko 
138d154002SRoman Byshko #include <common.h>
14375de017SHans de Goede #include <asm/arch/clock.h>
152aacc423SHans de Goede #include <asm/arch/usb_phy.h>
16375de017SHans de Goede #include <asm/io.h>
178d837a1fSHans de Goede #include <dm.h>
188d154002SRoman Byshko #include "ehci.h"
198d154002SRoman Byshko 
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 
288d837a1fSHans de Goede struct ehci_sunxi_priv {
298d837a1fSHans de Goede 	struct ehci_ctrl ehci;
308d837a1fSHans de Goede 	int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
318d837a1fSHans de Goede 	int phy_index;     /* Index of the usb-phy attached to this hcd */
328d837a1fSHans de Goede };
338d837a1fSHans de Goede 
348d837a1fSHans de Goede static int ehci_usb_probe(struct udevice *dev)
358d154002SRoman Byshko {
36375de017SHans de Goede 	struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
378d837a1fSHans de Goede 	struct usb_platdata *plat = dev_get_platdata(dev);
388d837a1fSHans de Goede 	struct ehci_sunxi_priv *priv = dev_get_priv(dev);
398d837a1fSHans de Goede 	struct ehci_hccr *hccr = (struct ehci_hccr *)dev_get_addr(dev);
408d837a1fSHans de Goede 	struct ehci_hcor *hcor;
41*fb3bfbb2SHans de Goede 	int extra_ahb_gate_mask = 0;
4244fd5914SHans de Goede 
438d837a1fSHans de Goede 	/*
448d837a1fSHans de Goede 	 * This should go away once we've moved to the driver model for
458d837a1fSHans de Goede 	 * clocks resp. phys.
468d837a1fSHans de Goede 	 */
478d837a1fSHans de Goede 	priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
48dc44fd8aSJelle van der Waa #ifdef CONFIG_MACH_SUN8I_H3
49*fb3bfbb2SHans de Goede 	extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
50dc44fd8aSJelle van der Waa #endif
51948603d4SHans de Goede 	priv->phy_index = ((u32)hccr - SUNXI_USB1_BASE) / BASE_DIST;
52948603d4SHans de Goede 	priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
53*fb3bfbb2SHans de Goede 	extra_ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
54948603d4SHans de Goede 	priv->phy_index++; /* Non otg phys start at 1 */
558d837a1fSHans de Goede 
56*fb3bfbb2SHans de Goede 	setbits_le32(&ccm->ahb_gate0,
57*fb3bfbb2SHans de Goede 		     priv->ahb_gate_mask | extra_ahb_gate_mask);
58375de017SHans de Goede #ifdef CONFIG_SUNXI_GEN_SUN6I
59*fb3bfbb2SHans de Goede 	setbits_le32(&ccm->ahb_reset0_cfg,
60*fb3bfbb2SHans de Goede 		     priv->ahb_gate_mask | extra_ahb_gate_mask);
61375de017SHans de Goede #endif
62375de017SHans de Goede 
638d837a1fSHans de Goede 	sunxi_usb_phy_init(priv->phy_index);
648d837a1fSHans de Goede 	sunxi_usb_phy_power_on(priv->phy_index);
658d154002SRoman Byshko 
668d837a1fSHans de Goede 	hcor = (struct ehci_hcor *)((uint32_t)hccr +
678d837a1fSHans de Goede 				    HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
688d154002SRoman Byshko 
698d837a1fSHans de Goede 	return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
708d837a1fSHans de Goede }
718d154002SRoman Byshko 
728d837a1fSHans de Goede static int ehci_usb_remove(struct udevice *dev)
738d837a1fSHans de Goede {
748d837a1fSHans de Goede 	struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
758d837a1fSHans de Goede 	struct ehci_sunxi_priv *priv = dev_get_priv(dev);
768d837a1fSHans de Goede 	int ret;
778d837a1fSHans de Goede 
788d837a1fSHans de Goede 	ret = ehci_deregister(dev);
798d837a1fSHans de Goede 	if (ret)
808d837a1fSHans de Goede 		return ret;
818d837a1fSHans de Goede 
828d837a1fSHans de Goede 	sunxi_usb_phy_exit(priv->phy_index);
838d837a1fSHans de Goede 
848d837a1fSHans de Goede #ifdef CONFIG_SUNXI_GEN_SUN6I
858d837a1fSHans de Goede 	clrbits_le32(&ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
868d837a1fSHans de Goede #endif
878d837a1fSHans de Goede 	clrbits_le32(&ccm->ahb_gate0, priv->ahb_gate_mask);
888d154002SRoman Byshko 
898d154002SRoman Byshko 	return 0;
908d154002SRoman Byshko }
918d154002SRoman Byshko 
928d837a1fSHans de Goede static const struct udevice_id ehci_usb_ids[] = {
938d837a1fSHans de Goede 	{ .compatible = "allwinner,sun4i-a10-ehci", },
948d837a1fSHans de Goede 	{ .compatible = "allwinner,sun5i-a13-ehci", },
958d837a1fSHans de Goede 	{ .compatible = "allwinner,sun6i-a31-ehci", },
968d837a1fSHans de Goede 	{ .compatible = "allwinner,sun7i-a20-ehci", },
978d837a1fSHans de Goede 	{ .compatible = "allwinner,sun8i-a23-ehci", },
983655f287SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a83t-ehci", },
99dc44fd8aSJelle van der Waa 	{ .compatible = "allwinner,sun8i-h3-ehci",  },
1008d837a1fSHans de Goede 	{ .compatible = "allwinner,sun9i-a80-ehci", },
1018d837a1fSHans de Goede 	{ }
1028d837a1fSHans de Goede };
103375de017SHans de Goede 
10440c92082SMarek Vasut U_BOOT_DRIVER(ehci_sunxi) = {
1058d837a1fSHans de Goede 	.name	= "ehci_sunxi",
1068d837a1fSHans de Goede 	.id	= UCLASS_USB,
1078d837a1fSHans de Goede 	.of_match = ehci_usb_ids,
1088d837a1fSHans de Goede 	.probe = ehci_usb_probe,
1098d837a1fSHans de Goede 	.remove = ehci_usb_remove,
1108d837a1fSHans de Goede 	.ops	= &ehci_usb_ops,
1118d837a1fSHans de Goede 	.platdata_auto_alloc_size = sizeof(struct usb_platdata),
1128d837a1fSHans de Goede 	.priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
1138d837a1fSHans de Goede 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
1148d837a1fSHans de Goede };
115