blob: f2d83e34bc1b89ebca17aee5a966c94ae490f10e [file] [log] [blame]
Roman Byshko8d154002014-07-27 19:32:44 +02001/*
Hans de Goede7b798652015-04-27 14:54:47 +02002 * Sunxi ehci glue
Roman Byshko8d154002014-07-27 19:32:44 +02003 *
Hans de Goede7b798652015-04-27 14:54:47 +02004 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
Roman Byshko8d154002014-07-27 19:32:44 +02006 *
7 * Based on code from
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
Roman Byshko8d154002014-07-27 19:32:44 +020013#include <common.h>
Hans de Goede375de012015-04-27 11:44:22 +020014#include <asm/arch/clock.h>
Hans de Goede2aacc422015-04-27 15:05:10 +020015#include <asm/arch/usb_phy.h>
Hans de Goede375de012015-04-27 11:44:22 +020016#include <asm/io.h>
Hans de Goede8d837a12015-05-10 14:10:26 +020017#include <dm.h>
Roman Byshko8d154002014-07-27 19:32:44 +020018#include "ehci.h"
19
Hans de Goede948603d2016-03-21 14:44:35 +010020#ifdef CONFIG_SUNXI_GEN_SUN4I
21#define BASE_DIST 0x8000
22#define AHB_CLK_DIST 2
23#else
24#define BASE_DIST 0x1000
25#define AHB_CLK_DIST 1
26#endif
27
Hans de Goede8d837a12015-05-10 14:10:26 +020028struct ehci_sunxi_priv {
29 struct ehci_ctrl ehci;
30 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
31 int phy_index; /* Index of the usb-phy attached to this hcd */
32};
33
34static int ehci_usb_probe(struct udevice *dev)
Roman Byshko8d154002014-07-27 19:32:44 +020035{
Hans de Goede375de012015-04-27 11:44:22 +020036 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede8d837a12015-05-10 14:10:26 +020037 struct usb_platdata *plat = dev_get_platdata(dev);
38 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
39 struct ehci_hccr *hccr = (struct ehci_hccr *)dev_get_addr(dev);
40 struct ehci_hcor *hcor;
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020041 int extra_ahb_gate_mask = 0;
Hans de Goede115200c2014-11-07 16:09:00 +010042
Hans de Goede8d837a12015-05-10 14:10:26 +020043 /*
44 * This should go away once we've moved to the driver model for
45 * clocks resp. phys.
46 */
Jelle van der Waadc44fd82016-02-09 23:59:33 +010047 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
48#ifdef CONFIG_MACH_SUN8I_H3
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020049 extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
Jelle van der Waadc44fd82016-02-09 23:59:33 +010050#endif
Hans de Goede948603d2016-03-21 14:44:35 +010051 priv->phy_index = ((u32)hccr - SUNXI_USB1_BASE) / BASE_DIST;
52 priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020053 extra_ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
Hans de Goede948603d2016-03-21 14:44:35 +010054 priv->phy_index++; /* Non otg phys start at 1 */
Hans de Goede8d837a12015-05-10 14:10:26 +020055
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020056 setbits_le32(&ccm->ahb_gate0,
57 priv->ahb_gate_mask | extra_ahb_gate_mask);
Hans de Goede375de012015-04-27 11:44:22 +020058#ifdef CONFIG_SUNXI_GEN_SUN6I
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020059 setbits_le32(&ccm->ahb_reset0_cfg,
60 priv->ahb_gate_mask | extra_ahb_gate_mask);
Hans de Goede375de012015-04-27 11:44:22 +020061#endif
62
Hans de Goede8d837a12015-05-10 14:10:26 +020063 sunxi_usb_phy_init(priv->phy_index);
64 sunxi_usb_phy_power_on(priv->phy_index);
Roman Byshko8d154002014-07-27 19:32:44 +020065
Hans de Goede8d837a12015-05-10 14:10:26 +020066 hcor = (struct ehci_hcor *)((uint32_t)hccr +
67 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Roman Byshko8d154002014-07-27 19:32:44 +020068
Hans de Goede8d837a12015-05-10 14:10:26 +020069 return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
70}
Roman Byshko8d154002014-07-27 19:32:44 +020071
Hans de Goede8d837a12015-05-10 14:10:26 +020072static int ehci_usb_remove(struct udevice *dev)
73{
74 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
75 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
76 int ret;
77
78 ret = ehci_deregister(dev);
79 if (ret)
80 return ret;
81
Hans de Goede8d837a12015-05-10 14:10:26 +020082 sunxi_usb_phy_exit(priv->phy_index);
83
84#ifdef CONFIG_SUNXI_GEN_SUN6I
85 clrbits_le32(&ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
86#endif
87 clrbits_le32(&ccm->ahb_gate0, priv->ahb_gate_mask);
Roman Byshko8d154002014-07-27 19:32:44 +020088
Roman Byshko8d154002014-07-27 19:32:44 +020089 return 0;
90}
91
Hans de Goede8d837a12015-05-10 14:10:26 +020092static const struct udevice_id ehci_usb_ids[] = {
93 { .compatible = "allwinner,sun4i-a10-ehci", },
94 { .compatible = "allwinner,sun5i-a13-ehci", },
95 { .compatible = "allwinner,sun6i-a31-ehci", },
96 { .compatible = "allwinner,sun7i-a20-ehci", },
97 { .compatible = "allwinner,sun8i-a23-ehci", },
Chen-Yu Tsai3655f282016-03-30 00:26:53 +080098 { .compatible = "allwinner,sun8i-a83t-ehci", },
Jelle van der Waadc44fd82016-02-09 23:59:33 +010099 { .compatible = "allwinner,sun8i-h3-ehci", },
Hans de Goede8d837a12015-05-10 14:10:26 +0200100 { .compatible = "allwinner,sun9i-a80-ehci", },
101 { }
102};
Hans de Goede375de012015-04-27 11:44:22 +0200103
Marek Vasut40c92082015-11-30 18:15:33 +0100104U_BOOT_DRIVER(ehci_sunxi) = {
Hans de Goede8d837a12015-05-10 14:10:26 +0200105 .name = "ehci_sunxi",
106 .id = UCLASS_USB,
107 .of_match = ehci_usb_ids,
108 .probe = ehci_usb_probe,
109 .remove = ehci_usb_remove,
110 .ops = &ehci_usb_ops,
111 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
112 .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
113 .flags = DM_FLAG_ALLOC_PRIV_DMA,
114};