Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> |
| 4 | * Copyright (C) 2010 Freescale Semiconductor, Inc. |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 8 | #include <clk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 10 | #include <usb.h> |
| 11 | #include <errno.h> |
Mateusz Kulikowski | 4073b83 | 2016-01-23 11:54:32 +0100 | [diff] [blame] | 12 | #include <wait_bit.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Mateusz Kulikowski | 3add69e | 2016-03-31 23:12:23 +0200 | [diff] [blame] | 16 | #include <usb/ehci-ci.h> |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/arch/imx-regs.h> |
| 19 | #include <asm/arch/clock.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 20 | #include <asm/mach-imx/iomux-v3.h> |
| 21 | #include <asm/mach-imx/sys_proto.h> |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 22 | #include <dm.h> |
Simon Glass | 0ffb9d6 | 2017-05-31 19:47:48 -0600 | [diff] [blame] | 23 | #include <asm/mach-types.h> |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 24 | #include <power/regulator.h> |
Adam Ford | 15287f0 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 25 | #include <linux/usb/otg.h> |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 26 | |
| 27 | #include "ehci.h" |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 28 | |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 31 | #define USB_OTGREGS_OFFSET 0x000 |
| 32 | #define USB_H1REGS_OFFSET 0x200 |
| 33 | #define USB_H2REGS_OFFSET 0x400 |
| 34 | #define USB_H3REGS_OFFSET 0x600 |
| 35 | #define USB_OTHERREGS_OFFSET 0x800 |
| 36 | |
| 37 | #define USB_H1_CTRL_OFFSET 0x04 |
| 38 | |
| 39 | #define USBPHY_CTRL 0x00000030 |
| 40 | #define USBPHY_CTRL_SET 0x00000034 |
| 41 | #define USBPHY_CTRL_CLR 0x00000038 |
| 42 | #define USBPHY_CTRL_TOG 0x0000003c |
| 43 | |
| 44 | #define USBPHY_PWD 0x00000000 |
| 45 | #define USBPHY_CTRL_SFTRST 0x80000000 |
| 46 | #define USBPHY_CTRL_CLKGATE 0x40000000 |
| 47 | #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000 |
| 48 | #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000 |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 49 | #define USBPHY_CTRL_OTG_ID 0x08000000 |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 50 | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 51 | #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000 |
| 52 | #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000 |
| 53 | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 54 | #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000 |
| 55 | #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000 |
| 56 | #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000 |
| 57 | #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040 |
| 58 | |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 59 | #define USBNC_OFFSET 0x200 |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 60 | #define USBNC_PHY_STATUS_OFFSET 0x23C |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 61 | #define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */ |
| 62 | #define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */ |
Stefan Agner | 475cf91 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 63 | #define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */ |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 64 | #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */ |
| 65 | #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */ |
| 66 | |
| 67 | /* USBCMD */ |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 68 | #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */ |
| 69 | #define UCMD_RESET (1 << 1) /* controller reset */ |
| 70 | |
Marek Vasut | 793e575 | 2021-03-31 22:19:00 +0200 | [diff] [blame] | 71 | /* Base address for this IP block is 0x02184800 */ |
| 72 | struct usbnc_regs { |
| 73 | u32 ctrl[4]; /* otg/host1-3 */ |
| 74 | u32 uh2_hsic_ctrl; |
| 75 | u32 uh3_hsic_ctrl; |
| 76 | u32 otg_phy_ctrl_0; |
| 77 | u32 uh1_phy_ctrl_0; |
| 78 | u32 reserve1[4]; |
| 79 | u32 phy_cfg1; |
| 80 | u32 phy_cfg2; |
| 81 | u32 reserve2; |
| 82 | u32 phy_status; |
| 83 | u32 reserve3[4]; |
| 84 | u32 adp_cfg1; |
| 85 | u32 adp_cfg2; |
| 86 | u32 adp_status; |
| 87 | }; |
| 88 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 89 | #if defined(CONFIG_MX6) && !defined(CONFIG_PHY) |
| 90 | static void usb_power_config_mx6(struct anatop_regs __iomem *anatop, |
| 91 | int anatop_bits_index) |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 92 | { |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 93 | void __iomem *chrg_detect; |
| 94 | void __iomem *pll_480_ctrl_clr; |
| 95 | void __iomem *pll_480_ctrl_set; |
| 96 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 97 | if (!is_mx6()) |
| 98 | return; |
| 99 | |
| 100 | switch (anatop_bits_index) { |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 101 | case 0: |
| 102 | chrg_detect = &anatop->usb1_chrg_detect; |
| 103 | pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr; |
| 104 | pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set; |
| 105 | break; |
| 106 | case 1: |
| 107 | chrg_detect = &anatop->usb2_chrg_detect; |
| 108 | pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr; |
| 109 | pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set; |
| 110 | break; |
| 111 | default: |
| 112 | return; |
| 113 | } |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 114 | /* |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 115 | * Some phy and power's special controls |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 116 | * 1. The external charger detector needs to be disabled |
| 117 | * or the signal at DP will be poor |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 118 | * 2. The PLL's power and output to usb |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 119 | * is totally controlled by IC, so the Software only needs |
| 120 | * to enable them at initializtion. |
| 121 | */ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 122 | writel(ANADIG_USB2_CHRG_DETECT_EN_B | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 123 | ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B, |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 124 | chrg_detect); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 125 | |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 126 | writel(ANADIG_USB2_PLL_480_CTRL_BYPASS, |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 127 | pll_480_ctrl_clr); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 128 | |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 129 | writel(ANADIG_USB2_PLL_480_CTRL_ENABLE | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 130 | ANADIG_USB2_PLL_480_CTRL_POWER | |
| 131 | ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS, |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 132 | pll_480_ctrl_set); |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 133 | } |
| 134 | #else |
| 135 | static void __maybe_unused |
| 136 | usb_power_config_mx6(void *anatop, int anatop_bits_index) { } |
| 137 | #endif |
Ye Li | 9da57ea | 2019-10-24 10:29:32 -0300 | [diff] [blame] | 138 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 139 | #if defined(CONFIG_MX7) && !defined(CONFIG_PHY) |
| 140 | static void usb_power_config_mx7(struct usbnc_regs *usbnc) |
| 141 | { |
| 142 | void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2); |
| 143 | |
| 144 | if (!is_mx7()) |
| 145 | return; |
| 146 | |
| 147 | /* |
| 148 | * Clear the ACAENB to enable usb_otg_id detection, |
| 149 | * otherwise it is the ACA detection enabled. |
| 150 | */ |
| 151 | clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB); |
| 152 | } |
| 153 | #else |
| 154 | static void __maybe_unused |
| 155 | usb_power_config_mx7(void *usbnc) { } |
Ye Li | 9da57ea | 2019-10-24 10:29:32 -0300 | [diff] [blame] | 156 | #endif |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 157 | |
| 158 | #if defined(CONFIG_MX7ULP) && !defined(CONFIG_PHY) |
| 159 | static void usb_power_config_mx7ulp(struct usbphy_regs __iomem *usbphy) |
| 160 | { |
| 161 | if (!is_mx7ulp()) |
| 162 | return; |
| 163 | |
| 164 | writel(ANADIG_USB2_CHRG_DETECT_EN_B | |
| 165 | ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B, |
| 166 | &usbphy->usb1_chrg_detect); |
| 167 | |
| 168 | scg_enable_usb_pll(true); |
| 169 | } |
| 170 | #else |
| 171 | static void __maybe_unused |
| 172 | usb_power_config_mx7ulp(void *usbphy) { } |
| 173 | #endif |
| 174 | |
| 175 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) |
| 176 | static const unsigned phy_bases[] = { |
| 177 | USB_PHY0_BASE_ADDR, |
| 178 | #if defined(USB_PHY1_BASE_ADDR) |
| 179 | USB_PHY1_BASE_ADDR, |
| 180 | #endif |
| 181 | }; |
| 182 | |
| 183 | #if !defined(CONFIG_PHY) |
| 184 | static void usb_internal_phy_clock_gate(void __iomem *phy_reg, int on) |
| 185 | { |
| 186 | phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET; |
| 187 | writel(USBPHY_CTRL_CLKGATE, phy_reg); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 190 | /* Return 0 : host node, <>0 : device mode */ |
Marek Vasut | 6b6df64 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 191 | static int usb_phy_enable(struct usb_ehci *ehci, void __iomem *phy_reg) |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 192 | { |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 193 | void __iomem *phy_ctrl; |
| 194 | void __iomem *usb_cmd; |
Adrian Alonso | c52eb1c | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 195 | int ret; |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 196 | |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 197 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 198 | usb_cmd = (void __iomem *)&ehci->usbcmd; |
| 199 | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 200 | /* Stop then Reset */ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 201 | clrbits_le32(usb_cmd, UCMD_RUN_STOP); |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 202 | ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false); |
Adrian Alonso | c52eb1c | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 203 | if (ret) |
| 204 | return ret; |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 205 | |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 206 | setbits_le32(usb_cmd, UCMD_RESET); |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 207 | ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false); |
Adrian Alonso | c52eb1c | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 208 | if (ret) |
| 209 | return ret; |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 210 | |
| 211 | /* Reset USBPHY module */ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 212 | setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 213 | udelay(10); |
| 214 | |
| 215 | /* Remove CLKGATE and SFTRST */ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 216 | clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 217 | udelay(10); |
| 218 | |
| 219 | /* Power up the PHY */ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 220 | writel(0, phy_reg + USBPHY_PWD); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 221 | /* enable FS/LS device */ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 222 | setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 | |
| 223 | USBPHY_CTRL_ENUTMILEVEL3); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 224 | |
Peng Fan | 220402e | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 225 | return 0; |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 226 | } |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 227 | #endif |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 228 | |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 229 | int usb_phy_mode(int port) |
| 230 | { |
| 231 | void __iomem *phy_reg; |
| 232 | void __iomem *phy_ctrl; |
| 233 | u32 val; |
| 234 | |
| 235 | phy_reg = (void __iomem *)phy_bases[port]; |
| 236 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 237 | |
| 238 | val = readl(phy_ctrl); |
| 239 | |
| 240 | if (val & USBPHY_CTRL_OTG_ID) |
| 241 | return USB_INIT_DEVICE; |
| 242 | else |
| 243 | return USB_INIT_HOST; |
| 244 | } |
| 245 | |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 246 | #elif defined(CONFIG_MX7) |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 247 | int usb_phy_mode(int port) |
| 248 | { |
| 249 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 250 | (0x10000 * port) + USBNC_OFFSET); |
| 251 | void __iomem *status = (void __iomem *)(&usbnc->phy_status); |
| 252 | u32 val; |
| 253 | |
| 254 | val = readl(status); |
| 255 | |
| 256 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 257 | return USB_INIT_DEVICE; |
| 258 | else |
| 259 | return USB_INIT_HOST; |
| 260 | } |
| 261 | #endif |
| 262 | |
Marek Vasut | 1fa4243 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 263 | static void usb_oc_config(struct usbnc_regs *usbnc, int index) |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 264 | { |
Marek Vasut | 793e575 | 2021-03-31 22:19:00 +0200 | [diff] [blame] | 265 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 266 | |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 267 | #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2 |
| 268 | /* mx6qarm2 seems to required a different setting*/ |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 269 | clrbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 270 | #else |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 271 | setbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 272 | #endif |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 273 | |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 274 | setbits_le32(ctrl, UCTRL_OVER_CUR_DIS); |
Ye Li | 9da57ea | 2019-10-24 10:29:32 -0300 | [diff] [blame] | 275 | |
| 276 | /* Set power polarity to high active */ |
| 277 | #ifdef CONFIG_MXC_USB_OTG_HACTIVE |
| 278 | setbits_le32(ctrl, UCTRL_PWR_POL); |
| 279 | #else |
| 280 | clrbits_le32(ctrl, UCTRL_PWR_POL); |
| 281 | #endif |
Peng Fan | 220402e | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 282 | } |
| 283 | |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 284 | #if !CONFIG_IS_ENABLED(DM_USB) |
Adrian Alonso | 14dfbbb | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 285 | /** |
Stefan Agner | 3dfd3a0 | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 286 | * board_usb_phy_mode - override usb phy mode |
Adrian Alonso | 14dfbbb | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 287 | * @port: usb host/otg port |
| 288 | * |
| 289 | * Target board specific, override usb_phy_mode. |
| 290 | * When usb-otg is used as usb host port, iomux pad usb_otg_id can be |
| 291 | * left disconnected in this case usb_phy_mode will not be able to identify |
| 292 | * the phy mode that usb port is used. |
| 293 | * Machine file overrides board_usb_phy_mode. |
| 294 | * |
| 295 | * Return: USB_INIT_DEVICE or USB_INIT_HOST |
| 296 | */ |
Peng Fan | 220402e | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 297 | int __weak board_usb_phy_mode(int port) |
| 298 | { |
| 299 | return usb_phy_mode(port); |
| 300 | } |
| 301 | |
Adrian Alonso | 14dfbbb | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 302 | /** |
| 303 | * board_ehci_hcd_init - set usb vbus voltage |
| 304 | * @port: usb otg port |
| 305 | * |
| 306 | * Target board specific, setup iomux pad to setup supply vbus voltage |
| 307 | * for usb otg port. Machine board file overrides board_ehci_hcd_init |
| 308 | * |
| 309 | * Return: 0 Success |
| 310 | */ |
Benoît Thébaudeau | 98023c1 | 2012-11-13 09:58:35 +0000 | [diff] [blame] | 311 | int __weak board_ehci_hcd_init(int port) |
| 312 | { |
| 313 | return 0; |
| 314 | } |
| 315 | |
Adrian Alonso | 14dfbbb | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 316 | /** |
| 317 | * board_ehci_power - enables/disables usb vbus voltage |
| 318 | * @port: usb otg port |
| 319 | * @on: on/off vbus voltage |
| 320 | * |
| 321 | * Enables/disables supply vbus voltage for usb otg port. |
| 322 | * Machine board file overrides board_ehci_power |
| 323 | * |
| 324 | * Return: 0 Success |
| 325 | */ |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 326 | int __weak board_ehci_power(int port, int on) |
| 327 | { |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Troy Kisky | 7d6bbb9 | 2013-10-10 15:27:57 -0700 | [diff] [blame] | 331 | int ehci_hcd_init(int index, enum usb_init_type init, |
| 332 | struct ehci_hccr **hccr, struct ehci_hcor **hcor) |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 333 | { |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 334 | enum usb_init_type type; |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 335 | #if defined(CONFIG_MX6) |
| 336 | u32 controller_spacing = 0x200; |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 337 | struct anatop_regs __iomem *anatop = |
| 338 | (struct anatop_regs __iomem *)ANATOP_BASE_ADDR; |
Marek Vasut | 1fa4243 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 339 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 340 | USB_OTHERREGS_OFFSET); |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 341 | #elif defined(CONFIG_MX7) |
| 342 | u32 controller_spacing = 0x10000; |
| 343 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 344 | (0x10000 * index) + USBNC_OFFSET); |
| 345 | #elif defined(CONFIG_MX7ULP) |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 346 | u32 controller_spacing = 0x10000; |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 347 | struct usbphy_regs __iomem *usbphy = |
| 348 | (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR; |
Marek Vasut | 1fa4243 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 349 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 350 | (0x10000 * index) + USBNC_OFFSET); |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 351 | #endif |
Ye.Li | f93453a | 2014-09-15 17:23:14 +0800 | [diff] [blame] | 352 | struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR + |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 353 | (controller_spacing * index)); |
Stefan Agner | 3dfd3a0 | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 354 | int ret; |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 355 | |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 356 | if (index > 3) |
| 357 | return -EINVAL; |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 358 | |
Peng Fan | f8b2719 | 2020-05-01 22:08:36 +0800 | [diff] [blame] | 359 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 360 | if (usb_fused((ulong)ehci)) { |
| 361 | printf("SoC fuse indicates USB@0x%lx is unavailable.\n", |
| 362 | (ulong)ehci); |
| 363 | return -ENODEV; |
| 364 | } |
| 365 | } |
| 366 | |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 367 | enable_usboh3_clk(1); |
| 368 | mdelay(1); |
| 369 | |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 370 | /* Do board specific initialization */ |
| 371 | ret = board_ehci_hcd_init(index); |
| 372 | if (ret) { |
| 373 | enable_usboh3_clk(0); |
Stefan Agner | 3dfd3a0 | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 374 | return ret; |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 377 | #if defined(CONFIG_MX6) |
| 378 | usb_power_config_mx6(anatop, index); |
| 379 | #elif defined (CONFIG_MX7) |
| 380 | usb_power_config_mx7(usbnc); |
| 381 | #elif defined (CONFIG_MX7ULP) |
| 382 | usb_power_config_mx7ulp(usbphy); |
| 383 | #endif |
| 384 | |
Marek Vasut | 1fa4243 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 385 | usb_oc_config(usbnc, index); |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 386 | |
| 387 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) |
Marek Vasut | 6b6df64 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 388 | if (index < ARRAY_SIZE(phy_bases)) { |
| 389 | usb_internal_phy_clock_gate((void __iomem *)phy_bases[index], 1); |
| 390 | usb_phy_enable(ehci, (void __iomem *)phy_bases[index]); |
| 391 | } |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 392 | #endif |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 393 | |
Peng Fan | 220402e | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 394 | type = board_usb_phy_mode(index); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 395 | |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 396 | if (hccr && hcor) { |
| 397 | *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 398 | *hcor = (struct ehci_hcor *)((uint32_t)*hccr + |
| 399 | HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase))); |
| 400 | } |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 401 | |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 402 | if ((type == init) || (type == USB_INIT_DEVICE)) |
| 403 | board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1); |
| 404 | if (type != init) |
| 405 | return -ENODEV; |
| 406 | if (type == USB_INIT_DEVICE) |
| 407 | return 0; |
Adrian Alonso | f31599f | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 408 | |
Troy Kisky | ed72a9e | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 409 | setbits_le32(&ehci->usbmode, CM_HOST); |
Adrian Alonso | aee79b4 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 410 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 411 | setbits_le32(&ehci->portsc, USB_EN); |
| 412 | |
| 413 | mdelay(10); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 418 | int ehci_hcd_stop(int index) |
Wolfgang Grandegger | 1859b70 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 419 | { |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | #else |
| 423 | struct ehci_mx6_priv_data { |
| 424 | struct ehci_ctrl ctrl; |
| 425 | struct usb_ehci *ehci; |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 426 | struct udevice *vbus_supply; |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 427 | struct clk clk; |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 428 | enum usb_init_type init_type; |
| 429 | int portnr; |
Marek Vasut | ebaf3ae | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 430 | void __iomem *phy_addr; |
| 431 | void __iomem *misc_addr; |
| 432 | void __iomem *anatop_addr; |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | static int mx6_init_after_reset(struct ehci_ctrl *dev) |
| 436 | { |
| 437 | struct ehci_mx6_priv_data *priv = dev->priv; |
| 438 | enum usb_init_type type = priv->init_type; |
| 439 | struct usb_ehci *ehci = priv->ehci; |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 440 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 441 | #if !defined(CONFIG_PHY) |
| 442 | usb_power_config_mx6(priv->anatop_addr, priv->portnr); |
| 443 | usb_power_config_mx7(priv->misc_addr); |
| 444 | usb_power_config_mx7ulp(priv->phy_addr); |
| 445 | #endif |
| 446 | |
Marek Vasut | 1fa4243 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 447 | usb_oc_config(priv->misc_addr, priv->portnr); |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 448 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 449 | #if !defined(CONFIG_PHY) && (defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)) |
Marek Vasut | 6b6df64 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 450 | usb_internal_phy_clock_gate(priv->phy_addr, 1); |
| 451 | usb_phy_enable(ehci, priv->phy_addr); |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 452 | #endif |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 453 | |
Abel Vesa | 888a946 | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 454 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 455 | if (priv->vbus_supply) { |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 456 | int ret; |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 457 | ret = regulator_set_enable(priv->vbus_supply, |
| 458 | (type == USB_INIT_DEVICE) ? |
| 459 | false : true); |
Marek Vasut | 2737045 | 2020-05-21 23:32:23 +0200 | [diff] [blame] | 460 | if (ret && ret != -ENOSYS) { |
Marek Vasut | a86d51a | 2020-05-21 23:34:06 +0200 | [diff] [blame] | 461 | printf("Error enabling VBUS supply (ret=%i)\n", ret); |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 462 | return ret; |
| 463 | } |
| 464 | } |
Abel Vesa | 888a946 | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 465 | #endif |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 466 | |
| 467 | if (type == USB_INIT_DEVICE) |
| 468 | return 0; |
| 469 | |
| 470 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 471 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
| 472 | setbits_le32(&ehci->portsc, USB_EN); |
| 473 | |
| 474 | mdelay(10); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static const struct ehci_ops mx6_ehci_ops = { |
| 480 | .init_after_reset = mx6_init_after_reset |
| 481 | }; |
| 482 | |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 483 | static int ehci_usb_phy_mode(struct udevice *dev) |
| 484 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 485 | struct usb_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 486 | void *__iomem addr = dev_read_addr_ptr(dev); |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 487 | void *__iomem phy_ctrl, *__iomem phy_status; |
| 488 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 489 | int offset = dev_of_offset(dev), phy_off; |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 490 | u32 val; |
| 491 | |
| 492 | /* |
| 493 | * About fsl,usbphy, Refer to |
| 494 | * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt. |
| 495 | */ |
Ye Li | 9da57ea | 2019-10-24 10:29:32 -0300 | [diff] [blame] | 496 | if (is_mx6() || is_mx7ulp()) { |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 497 | phy_off = fdtdec_lookup_phandle(blob, |
| 498 | offset, |
| 499 | "fsl,usbphy"); |
| 500 | if (phy_off < 0) |
| 501 | return -EINVAL; |
| 502 | |
| 503 | addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, |
| 504 | "reg"); |
| 505 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 506 | return -EINVAL; |
| 507 | |
| 508 | phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL); |
| 509 | val = readl(phy_ctrl); |
| 510 | |
| 511 | if (val & USBPHY_CTRL_OTG_ID) |
| 512 | plat->init_type = USB_INIT_DEVICE; |
| 513 | else |
| 514 | plat->init_type = USB_INIT_HOST; |
| 515 | } else if (is_mx7()) { |
| 516 | phy_status = (void __iomem *)(addr + |
| 517 | USBNC_PHY_STATUS_OFFSET); |
| 518 | val = readl(phy_status); |
| 519 | |
| 520 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 521 | plat->init_type = USB_INIT_DEVICE; |
| 522 | else |
| 523 | plat->init_type = USB_INIT_HOST; |
| 524 | } else { |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 531 | static int ehci_usb_of_to_plat(struct udevice *dev) |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 532 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 533 | struct usb_plat *plat = dev_get_plat(dev); |
Adam Ford | 15287f0 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 534 | enum usb_dr_mode dr_mode; |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 535 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 536 | dr_mode = usb_get_dr_mode(dev_ofnode(dev)); |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 537 | |
Adam Ford | 15287f0 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 538 | switch (dr_mode) { |
| 539 | case USB_DR_MODE_HOST: |
| 540 | plat->init_type = USB_INIT_HOST; |
| 541 | break; |
| 542 | case USB_DR_MODE_PERIPHERAL: |
| 543 | plat->init_type = USB_INIT_DEVICE; |
| 544 | break; |
| 545 | case USB_DR_MODE_OTG: |
| 546 | case USB_DR_MODE_UNKNOWN: |
| 547 | return ehci_usb_phy_mode(dev); |
| 548 | }; |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 549 | |
Adam Ford | 15287f0 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 550 | return 0; |
Peng Fan | 9e3eab3 | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 551 | } |
| 552 | |
Marek Vasut | ebaf3ae | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 553 | static int mx6_parse_dt_addrs(struct udevice *dev) |
| 554 | { |
| 555 | struct ehci_mx6_priv_data *priv = dev_get_priv(dev); |
| 556 | int phy_off, misc_off; |
| 557 | const void *blob = gd->fdt_blob; |
| 558 | int offset = dev_of_offset(dev); |
| 559 | void *__iomem addr; |
Marek Vasut | 5becd18 | 2021-04-02 13:56:28 +0200 | [diff] [blame^] | 560 | int ret, devnump; |
Marek Vasut | ebaf3ae | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 561 | |
| 562 | phy_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbphy"); |
| 563 | if (phy_off < 0) { |
| 564 | phy_off = fdtdec_lookup_phandle(blob, offset, "phys"); |
| 565 | if (phy_off < 0) |
| 566 | return -EINVAL; |
| 567 | } |
| 568 | |
Marek Vasut | 5becd18 | 2021-04-02 13:56:28 +0200 | [diff] [blame^] | 569 | ret = fdtdec_get_alias_seq(blob, dev->uclass->uc_drv->name, |
| 570 | phy_off, &devnump); |
| 571 | if (ret < 0) |
| 572 | return ret; |
| 573 | |
Marek Vasut | ebaf3ae | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 574 | misc_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbmisc"); |
| 575 | if (misc_off < 0) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, "reg"); |
| 579 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 580 | return -EINVAL; |
| 581 | |
| 582 | priv->phy_addr = addr; |
Marek Vasut | 5becd18 | 2021-04-02 13:56:28 +0200 | [diff] [blame^] | 583 | priv->portnr = devnump; |
Marek Vasut | ebaf3ae | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 584 | |
| 585 | addr = (void __iomem *)fdtdec_get_addr(blob, misc_off, "reg"); |
| 586 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 587 | return -EINVAL; |
| 588 | |
| 589 | priv->misc_addr = addr; |
| 590 | |
| 591 | #if !defined(CONFIG_PHY) && defined(CONFIG_MX6) |
| 592 | int anatop_off; |
| 593 | |
| 594 | /* Resolve ANATOP offset through USB PHY node */ |
| 595 | anatop_off = fdtdec_lookup_phandle(blob, phy_off, "fsl,anatop"); |
| 596 | if (anatop_off < 0) |
| 597 | return -EINVAL; |
| 598 | |
| 599 | addr = (void __iomem *)fdtdec_get_addr(blob, anatop_off, "reg"); |
| 600 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | priv->anatop_addr = addr; |
| 604 | #endif |
| 605 | return 0; |
| 606 | } |
| 607 | |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 608 | static int ehci_usb_probe(struct udevice *dev) |
| 609 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 610 | struct usb_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 611 | struct usb_ehci *ehci = dev_read_addr_ptr(dev); |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 612 | struct ehci_mx6_priv_data *priv = dev_get_priv(dev); |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 613 | enum usb_init_type type = plat->init_type; |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 614 | struct ehci_hccr *hccr; |
| 615 | struct ehci_hcor *hcor; |
| 616 | int ret; |
| 617 | |
Peng Fan | f8b2719 | 2020-05-01 22:08:36 +0800 | [diff] [blame] | 618 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 619 | if (usb_fused((ulong)ehci)) { |
| 620 | printf("SoC fuse indicates USB@0x%lx is unavailable.\n", |
| 621 | (ulong)ehci); |
| 622 | return -ENODEV; |
| 623 | } |
| 624 | } |
| 625 | |
Marek Vasut | ebaf3ae | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 626 | ret = mx6_parse_dt_addrs(dev); |
| 627 | if (ret) |
| 628 | return ret; |
| 629 | |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 630 | priv->ehci = ehci; |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 631 | priv->init_type = type; |
| 632 | |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 633 | #if CONFIG_IS_ENABLED(CLK) |
| 634 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 635 | if (ret < 0) |
| 636 | return ret; |
| 637 | |
| 638 | ret = clk_enable(&priv->clk); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | #else |
| 642 | /* Compatibility with DM_USB and !CLK */ |
| 643 | enable_usboh3_clk(1); |
| 644 | mdelay(1); |
| 645 | #endif |
| 646 | |
Abel Vesa | 888a946 | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 647 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 648 | ret = device_get_supply_regulator(dev, "vbus-supply", |
| 649 | &priv->vbus_supply); |
| 650 | if (ret) |
| 651 | debug("%s: No vbus supply\n", dev->name); |
Abel Vesa | 888a946 | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 652 | #endif |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 653 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 654 | #if !defined(CONFIG_PHY) |
| 655 | usb_power_config_mx6(priv->anatop_addr, priv->portnr); |
| 656 | usb_power_config_mx7(priv->misc_addr); |
| 657 | usb_power_config_mx7ulp(priv->phy_addr); |
| 658 | #endif |
| 659 | |
Marek Vasut | 1fa4243 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 660 | usb_oc_config(priv->misc_addr, priv->portnr); |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 661 | |
Marek Vasut | 41e8139 | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 662 | #if !defined(CONFIG_PHY) && (defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)) |
Marek Vasut | 6b6df64 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 663 | usb_internal_phy_clock_gate(priv->phy_addr, 1); |
| 664 | usb_phy_enable(ehci, priv->phy_addr); |
Marek Vasut | 09dc070 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 665 | #endif |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 666 | |
Abel Vesa | 888a946 | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 667 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 668 | if (priv->vbus_supply) { |
| 669 | ret = regulator_set_enable(priv->vbus_supply, |
| 670 | (type == USB_INIT_DEVICE) ? |
| 671 | false : true); |
Marek Vasut | 2737045 | 2020-05-21 23:32:23 +0200 | [diff] [blame] | 672 | if (ret && ret != -ENOSYS) { |
Marek Vasut | a86d51a | 2020-05-21 23:34:06 +0200 | [diff] [blame] | 673 | printf("Error enabling VBUS supply (ret=%i)\n", ret); |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 674 | goto err_clk; |
Peng Fan | 1335133 | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 675 | } |
| 676 | } |
Abel Vesa | 888a946 | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 677 | #endif |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 678 | |
| 679 | if (priv->init_type == USB_INIT_HOST) { |
| 680 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 681 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
| 682 | setbits_le32(&ehci->portsc, USB_EN); |
| 683 | } |
| 684 | |
| 685 | mdelay(10); |
| 686 | |
| 687 | hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 688 | hcor = (struct ehci_hcor *)((uint32_t)hccr + |
| 689 | HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); |
| 690 | |
Marek Vasut | f36841e | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 691 | ret = ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type); |
| 692 | if (ret) |
| 693 | goto err_regulator; |
| 694 | |
| 695 | return ret; |
| 696 | |
| 697 | err_regulator: |
| 698 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 699 | if (priv->vbus_supply) |
| 700 | regulator_set_enable(priv->vbus_supply, false); |
| 701 | #endif |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 702 | err_clk: |
| 703 | #if CONFIG_IS_ENABLED(CLK) |
| 704 | clk_disable(&priv->clk); |
| 705 | #else |
| 706 | /* Compatibility with DM_USB and !CLK */ |
| 707 | enable_usboh3_clk(0); |
| 708 | #endif |
Marek Vasut | f36841e | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | int ehci_usb_remove(struct udevice *dev) |
| 713 | { |
| 714 | struct ehci_mx6_priv_data *priv __maybe_unused = dev_get_priv(dev); |
| 715 | |
| 716 | ehci_deregister(dev); |
| 717 | |
| 718 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 719 | if (priv->vbus_supply) |
| 720 | regulator_set_enable(priv->vbus_supply, false); |
| 721 | #endif |
| 722 | |
Marek Vasut | f22fde7 | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 723 | #if CONFIG_IS_ENABLED(CLK) |
| 724 | clk_disable(&priv->clk); |
| 725 | #endif |
| 726 | |
Marek Vasut | f36841e | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 727 | return 0; |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 728 | } |
| 729 | |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 730 | static const struct udevice_id mx6_usb_ids[] = { |
| 731 | { .compatible = "fsl,imx27-usb" }, |
| 732 | { } |
| 733 | }; |
| 734 | |
| 735 | U_BOOT_DRIVER(usb_mx6) = { |
| 736 | .name = "ehci_mx6", |
| 737 | .id = UCLASS_USB, |
| 738 | .of_match = mx6_usb_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 739 | .of_to_plat = ehci_usb_of_to_plat, |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 740 | .probe = ehci_usb_probe, |
Marek Vasut | f36841e | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 741 | .remove = ehci_usb_remove, |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 742 | .ops = &ehci_usb_ops, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 743 | .plat_auto = sizeof(struct usb_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 744 | .priv_auto = sizeof(struct ehci_mx6_priv_data), |
Peng Fan | 5c363c1 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 745 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 746 | }; |
| 747 | #endif |