blob: af35a304bc430d530c6da593062cfc9e457ca974 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goede6a72e802015-05-10 14:10:27 +02002/*
3 * Sunxi ohci glue
4 *
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 *
7 * Based on code from
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
Hans de Goede6a72e802015-05-10 14:10:27 +02009 */
10
11#include <common.h>
12#include <asm/arch/clock.h>
13#include <asm/arch/usb_phy.h>
14#include <asm/io.h>
15#include <dm.h>
16#include <usb.h>
17#include "ohci.h"
18
Hans de Goede948603d2016-03-21 14:44:35 +010019#ifdef CONFIG_SUNXI_GEN_SUN4I
20#define BASE_DIST 0x8000
21#define AHB_CLK_DIST 2
22#else
23#define BASE_DIST 0x1000
24#define AHB_CLK_DIST 1
25#endif
26
Hans de Goede6a72e802015-05-10 14:10:27 +020027struct ohci_sunxi_priv {
Jagan Teki831cc982018-05-07 13:03:17 +053028 struct sunxi_ccm_reg *ccm;
Hans de Goede6a72e802015-05-10 14:10:27 +020029 ohci_t ohci;
30 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
31 int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
32 int phy_index; /* Index of the usb-phy attached to this hcd */
33};
34
35static int ohci_usb_probe(struct udevice *dev)
36{
Hans de Goede6a72e802015-05-10 14:10:27 +020037 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
38 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
Simon Glassa821c4a2017-05-17 17:18:05 -060039 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020040 int extra_ahb_gate_mask = 0;
Hans de Goede6a72e802015-05-10 14:10:27 +020041
Jagan Teki831cc982018-05-07 13:03:17 +053042 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
43 if (IS_ERR(priv->ccm))
44 return PTR_ERR(priv->ccm);
45
Hans de Goede6a72e802015-05-10 14:10:27 +020046 bus_priv->companion = true;
47
48 /*
49 * This should go away once we've moved to the driver model for
50 * clocks resp. phys.
51 */
Jelle van der Waadc44fd82016-02-09 23:59:33 +010052 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
53#ifdef CONFIG_MACH_SUN8I_H3
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020054 extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
Jelle van der Waadc44fd82016-02-09 23:59:33 +010055#endif
56 priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
Andre Przywara57faca12016-10-21 02:24:29 +010057 priv->phy_index = ((uintptr_t)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST;
Hans de Goede948603d2016-03-21 14:44:35 +010058 priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020059 extra_ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
Hans de Goede948603d2016-03-21 14:44:35 +010060 priv->usb_gate_mask <<= priv->phy_index;
61 priv->phy_index++; /* Non otg phys start at 1 */
Hans de Goede6a72e802015-05-10 14:10:27 +020062
Jagan Teki831cc982018-05-07 13:03:17 +053063 setbits_le32(&priv->ccm->ahb_gate0,
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020064 priv->ahb_gate_mask | extra_ahb_gate_mask);
Jagan Teki831cc982018-05-07 13:03:17 +053065 setbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
Hans de Goede6a72e802015-05-10 14:10:27 +020066#ifdef CONFIG_SUNXI_GEN_SUN6I
Jagan Teki831cc982018-05-07 13:03:17 +053067 setbits_le32(&priv->ccm->ahb_reset0_cfg,
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020068 priv->ahb_gate_mask | extra_ahb_gate_mask);
Hans de Goede6a72e802015-05-10 14:10:27 +020069#endif
70
71 sunxi_usb_phy_init(priv->phy_index);
72 sunxi_usb_phy_power_on(priv->phy_index);
73
74 return ohci_register(dev, regs);
75}
76
77static int ohci_usb_remove(struct udevice *dev)
78{
Hans de Goede6a72e802015-05-10 14:10:27 +020079 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
80 int ret;
81
82 ret = ohci_deregister(dev);
83 if (ret)
84 return ret;
85
Hans de Goede6a72e802015-05-10 14:10:27 +020086 sunxi_usb_phy_exit(priv->phy_index);
87
88#ifdef CONFIG_SUNXI_GEN_SUN6I
Jagan Teki831cc982018-05-07 13:03:17 +053089 clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
Hans de Goede6a72e802015-05-10 14:10:27 +020090#endif
Jagan Teki831cc982018-05-07 13:03:17 +053091 clrbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
92 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
Hans de Goede6a72e802015-05-10 14:10:27 +020093
94 return 0;
95}
96
97static const struct udevice_id ohci_usb_ids[] = {
98 { .compatible = "allwinner,sun4i-a10-ohci", },
99 { .compatible = "allwinner,sun5i-a13-ohci", },
100 { .compatible = "allwinner,sun6i-a31-ohci", },
101 { .compatible = "allwinner,sun7i-a20-ohci", },
102 { .compatible = "allwinner,sun8i-a23-ohci", },
Chen-Yu Tsai045ae7e2016-03-30 00:26:54 +0800103 { .compatible = "allwinner,sun8i-a83t-ohci", },
Jelle van der Waadc44fd82016-02-09 23:59:33 +0100104 { .compatible = "allwinner,sun8i-h3-ohci", },
Hans de Goede6a72e802015-05-10 14:10:27 +0200105 { .compatible = "allwinner,sun9i-a80-ohci", },
Amit Singh Tomar9d6c9d92016-10-21 02:24:30 +0100106 { .compatible = "allwinner,sun50i-a64-ohci", },
Hans de Goede6a72e802015-05-10 14:10:27 +0200107 { }
108};
109
110U_BOOT_DRIVER(usb_ohci) = {
111 .name = "ohci_sunxi",
112 .id = UCLASS_USB,
113 .of_match = ohci_usb_ids,
114 .probe = ohci_usb_probe,
115 .remove = ohci_usb_remove,
116 .ops = &ohci_usb_ops,
117 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
118 .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
119 .flags = DM_FLAG_ALLOC_PRIV_DMA,
120};