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