Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 2 | /* |
| 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 Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <asm/arch/clock.h> |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <dm.h> |
| 15 | #include <usb.h> |
| 16 | #include "ohci.h" |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 17 | #include <generic-phy.h> |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 18 | |
Hans de Goede | 8b6ab7d | 2016-03-21 14:44:35 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_SUNXI_GEN_SUN4I |
Hans de Goede | 8b6ab7d | 2016-03-21 14:44:35 +0100 | [diff] [blame] | 20 | #define AHB_CLK_DIST 2 |
| 21 | #else |
Hans de Goede | 8b6ab7d | 2016-03-21 14:44:35 +0100 | [diff] [blame] | 22 | #define AHB_CLK_DIST 1 |
| 23 | #endif |
| 24 | |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 25 | struct ohci_sunxi_cfg { |
| 26 | bool has_reset; |
| 27 | u32 extra_ahb_gate_mask; |
Vasily Khoruzhick | a03cbbb | 2018-06-07 19:23:41 -0700 | [diff] [blame^] | 28 | u32 extra_usb_gate_mask; |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 31 | struct ohci_sunxi_priv { |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 32 | struct sunxi_ccm_reg *ccm; |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 33 | ohci_t ohci; |
| 34 | int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */ |
| 35 | int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */ |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 36 | struct phy phy; |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 37 | const struct ohci_sunxi_cfg *cfg; |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static int ohci_usb_probe(struct udevice *dev) |
| 41 | { |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 42 | struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev); |
| 43 | struct ohci_sunxi_priv *priv = dev_get_priv(dev); |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 44 | struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev); |
Hans de Goede | 52b5e5a | 2016-04-09 15:00:52 +0200 | [diff] [blame] | 45 | int extra_ahb_gate_mask = 0; |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 46 | int phys, ret; |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 47 | |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 48 | priv->cfg = (const struct ohci_sunxi_cfg *)dev_get_driver_data(dev); |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 49 | priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 50 | if (IS_ERR(priv->ccm)) |
| 51 | return PTR_ERR(priv->ccm); |
| 52 | |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 53 | phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells"); |
| 54 | if (phys < 0) { |
| 55 | phys = 0; |
| 56 | goto no_phy; |
| 57 | } |
| 58 | |
| 59 | ret = generic_phy_get_by_name(dev, "usb", &priv->phy); |
| 60 | if (ret) { |
| 61 | pr_err("failed to get %s usb PHY\n", dev->name); |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | ret = generic_phy_init(&priv->phy); |
| 66 | if (ret) { |
| 67 | pr_err("failed to init %s USB PHY\n", dev->name); |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | ret = generic_phy_power_on(&priv->phy); |
| 72 | if (ret) { |
| 73 | pr_err("failed to power on %s USB PHY\n", dev->name); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | no_phy: |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 78 | bus_priv->companion = true; |
| 79 | |
| 80 | /* |
| 81 | * This should go away once we've moved to the driver model for |
| 82 | * clocks resp. phys. |
| 83 | */ |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 84 | priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0; |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 85 | extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask; |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 86 | priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK; |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 87 | priv->ahb_gate_mask <<= phys * AHB_CLK_DIST; |
| 88 | extra_ahb_gate_mask <<= phys * AHB_CLK_DIST; |
| 89 | priv->usb_gate_mask <<= phys; |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 90 | |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 91 | setbits_le32(&priv->ccm->ahb_gate0, |
Hans de Goede | 52b5e5a | 2016-04-09 15:00:52 +0200 | [diff] [blame] | 92 | priv->ahb_gate_mask | extra_ahb_gate_mask); |
Vasily Khoruzhick | a03cbbb | 2018-06-07 19:23:41 -0700 | [diff] [blame^] | 93 | setbits_le32(&priv->ccm->usb_clk_cfg, |
| 94 | priv->usb_gate_mask | priv->cfg->extra_usb_gate_mask); |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 95 | if (priv->cfg->has_reset) |
| 96 | setbits_le32(&priv->ccm->ahb_reset0_cfg, |
| 97 | priv->ahb_gate_mask | extra_ahb_gate_mask); |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 98 | |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 99 | return ohci_register(dev, regs); |
| 100 | } |
| 101 | |
| 102 | static int ohci_usb_remove(struct udevice *dev) |
| 103 | { |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 104 | struct ohci_sunxi_priv *priv = dev_get_priv(dev); |
| 105 | int ret; |
| 106 | |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 107 | if (generic_phy_valid(&priv->phy)) { |
| 108 | ret = generic_phy_exit(&priv->phy); |
| 109 | if (ret) { |
| 110 | pr_err("failed to exit %s USB PHY\n", dev->name); |
| 111 | return ret; |
| 112 | } |
| 113 | } |
| 114 | |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 115 | ret = ohci_deregister(dev); |
| 116 | if (ret) |
| 117 | return ret; |
| 118 | |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 119 | if (priv->cfg->has_reset) |
| 120 | clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask); |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 121 | clrbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask); |
| 122 | clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask); |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 127 | static const struct ohci_sunxi_cfg sun4i_a10_cfg = { |
| 128 | .has_reset = false, |
| 129 | }; |
| 130 | |
| 131 | static const struct ohci_sunxi_cfg sun6i_a31_cfg = { |
| 132 | .has_reset = true, |
| 133 | }; |
| 134 | |
| 135 | static const struct ohci_sunxi_cfg sun8i_h3_cfg = { |
| 136 | .has_reset = true, |
| 137 | .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0, |
| 138 | }; |
| 139 | |
Vasily Khoruzhick | a03cbbb | 2018-06-07 19:23:41 -0700 | [diff] [blame^] | 140 | static const struct ohci_sunxi_cfg sun50i_a64_cfg = { |
| 141 | .has_reset = true, |
| 142 | .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0, |
| 143 | .extra_usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK, |
| 144 | }; |
| 145 | |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 146 | static const struct udevice_id ohci_usb_ids[] = { |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 147 | { |
| 148 | .compatible = "allwinner,sun4i-a10-ohci", |
| 149 | .data = (ulong)&sun4i_a10_cfg, |
| 150 | }, |
| 151 | { |
| 152 | .compatible = "allwinner,sun5i-a13-ohci", |
| 153 | .data = (ulong)&sun4i_a10_cfg, |
| 154 | }, |
| 155 | { |
| 156 | .compatible = "allwinner,sun6i-a31-ohci", |
| 157 | .data = (ulong)&sun6i_a31_cfg, |
| 158 | }, |
| 159 | { |
| 160 | .compatible = "allwinner,sun7i-a20-ohci", |
| 161 | .data = (ulong)&sun4i_a10_cfg, |
| 162 | }, |
| 163 | { |
| 164 | .compatible = "allwinner,sun8i-a23-ohci", |
| 165 | .data = (ulong)&sun6i_a31_cfg, |
| 166 | }, |
| 167 | { |
| 168 | .compatible = "allwinner,sun8i-a83t-ohci", |
| 169 | .data = (ulong)&sun6i_a31_cfg, |
| 170 | }, |
| 171 | { |
| 172 | .compatible = "allwinner,sun8i-h3-ohci", |
| 173 | .data = (ulong)&sun8i_h3_cfg, |
| 174 | }, |
| 175 | { |
| 176 | .compatible = "allwinner,sun9i-a80-ohci", |
| 177 | .data = (ulong)&sun6i_a31_cfg, |
| 178 | }, |
| 179 | { |
| 180 | .compatible = "allwinner,sun50i-a64-ohci", |
Vasily Khoruzhick | a03cbbb | 2018-06-07 19:23:41 -0700 | [diff] [blame^] | 181 | .data = (ulong)&sun50i_a64_cfg, |
Vasily Khoruzhick | acabc91 | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 182 | }, |
| 183 | { /* sentinel */ } |
Hans de Goede | 804fa57 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | U_BOOT_DRIVER(usb_ohci) = { |
| 187 | .name = "ohci_sunxi", |
| 188 | .id = UCLASS_USB, |
| 189 | .of_match = ohci_usb_ids, |
| 190 | .probe = ohci_usb_probe, |
| 191 | .remove = ohci_usb_remove, |
| 192 | .ops = &ohci_usb_ops, |
| 193 | .platdata_auto_alloc_size = sizeof(struct usb_platdata), |
| 194 | .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv), |
| 195 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 196 | }; |