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