Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Grandegger | 3f46752 | 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 | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 8 | #include <clk.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 10 | #include <usb.h> |
| 11 | #include <errno.h> |
Mateusz Kulikowski | 8c25c25 | 2016-01-23 11:54:32 +0100 | [diff] [blame] | 12 | #include <wait_bit.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Mateusz Kulikowski | e162c6b | 2016-03-31 23:12:23 +0200 | [diff] [blame] | 16 | #include <usb/ehci-ci.h> |
Wolfgang Grandegger | 3f46752 | 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 | 552a848 | 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 | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 22 | #include <dm.h> |
Simon Glass | c62db35 | 2017-05-31 19:47:48 -0600 | [diff] [blame] | 23 | #include <asm/mach-types.h> |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 24 | #include <power/regulator.h> |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 25 | #include <linux/usb/otg.h> |
Matthias Schiffer | 0f513c5 | 2021-09-20 15:37:25 +0200 | [diff] [blame] | 26 | #include <linux/usb/phy.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 27 | |
| 28 | #include "ehci.h" |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 29 | |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Wolfgang Grandegger | 3f46752 | 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 | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 50 | #define USBPHY_CTRL_OTG_ID 0x08000000 |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 51 | |
Wolfgang Grandegger | 3f46752 | 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 | 3f46752 | 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 | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 60 | #define USBNC_OFFSET 0x200 |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 61 | #define USBNC_PHY_STATUS_OFFSET 0x23C |
Adrian Alonso | 35554fc | 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 | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 64 | #define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */ |
Wolfgang Grandegger | 3f46752 | 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 | 3f46752 | 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 | 1aae8a3 | 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 | 598fa7e | 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 | 849763b | 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) |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 98 | { |
Troy Kisky | d1a5286 | 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 | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 103 | if (!is_mx6()) |
| 104 | return; |
| 105 | |
| 106 | switch (anatop_bits_index) { |
Troy Kisky | d1a5286 | 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 | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 120 | /* |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 121 | * Some phy and power's special controls |
Wolfgang Grandegger | 3f46752 | 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 | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 124 | * 2. The PLL's power and output to usb |
Wolfgang Grandegger | 3f46752 | 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 | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 128 | writel(ANADIG_USB2_CHRG_DETECT_EN_B | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 129 | ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 130 | chrg_detect); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 131 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 132 | writel(ANADIG_USB2_PLL_480_CTRL_BYPASS, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 133 | pll_480_ctrl_clr); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 134 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 135 | writel(ANADIG_USB2_PLL_480_CTRL_ENABLE | |
Wolfgang Grandegger | 3f46752 | 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 | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 138 | pll_480_ctrl_set); |
Marek Vasut | 849763b | 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) { } |
Ye Li | 235f5e1 | 2019-10-24 10:29:32 -0300 | [diff] [blame] | 143 | #endif |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 144 | |
| 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) { } |
| 162 | #endif |
| 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 | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 181 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) || defined(CONFIG_IMXRT) |
Marek Vasut | 849763b | 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 | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 196 | /* Return 0 : host node, <>0 : device mode */ |
Marek Vasut | eb64f59 | 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 | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 198 | { |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 199 | void __iomem *phy_ctrl; |
| 200 | void __iomem *usb_cmd; |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 201 | int ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 202 | |
Troy Kisky | d1a5286 | 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 | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 206 | /* Stop then Reset */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 207 | clrbits_le32(usb_cmd, UCMD_RUN_STOP); |
Álvaro Fernández Rojas | 4826350 | 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 | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 209 | if (ret) |
| 210 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 211 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 212 | setbits_le32(usb_cmd, UCMD_RESET); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 213 | ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false); |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 214 | if (ret) |
| 215 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 216 | |
| 217 | /* Reset USBPHY module */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 218 | setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 219 | udelay(10); |
| 220 | |
| 221 | /* Remove CLKGATE and SFTRST */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 222 | clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 223 | udelay(10); |
| 224 | |
| 225 | /* Power up the PHY */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 226 | writel(0, phy_reg + USBPHY_PWD); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 227 | /* enable FS/LS device */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 228 | setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 | |
| 229 | USBPHY_CTRL_ENUTMILEVEL3); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 230 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 231 | return 0; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 232 | } |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 233 | #endif |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 234 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [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 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 244 | val = readl(phy_ctrl); |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 245 | |
| 246 | if (val & USBPHY_CTRL_OTG_ID) |
| 247 | return USB_INIT_DEVICE; |
| 248 | else |
| 249 | return USB_INIT_HOST; |
| 250 | } |
| 251 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 252 | #elif defined(CONFIG_MX7) |
Adrian Alonso | 35554fc | 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 | 53396d6 | 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 | 6686469 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 271 | static void usb_oc_config(struct usbnc_regs *usbnc, int index) |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 272 | { |
Marek Vasut | 598fa7e | 2021-03-31 22:19:00 +0200 | [diff] [blame] | 273 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 274 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 275 | setbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 276 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 277 | setbits_le32(ctrl, UCTRL_OVER_CUR_DIS); |
Ye Li | 235f5e1 | 2019-10-24 10:29:32 -0300 | [diff] [blame] | 278 | |
| 279 | /* Set power polarity to high active */ |
| 280 | #ifdef CONFIG_MXC_USB_OTG_HACTIVE |
| 281 | setbits_le32(ctrl, UCTRL_PWR_POL); |
| 282 | #else |
| 283 | clrbits_le32(ctrl, UCTRL_PWR_POL); |
| 284 | #endif |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 285 | } |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 286 | #endif |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 287 | |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 288 | #if !CONFIG_IS_ENABLED(DM_USB) |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 289 | /** |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 290 | * board_usb_phy_mode - override usb phy mode |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 291 | * @port: usb host/otg port |
| 292 | * |
| 293 | * Target board specific, override usb_phy_mode. |
| 294 | * When usb-otg is used as usb host port, iomux pad usb_otg_id can be |
| 295 | * left disconnected in this case usb_phy_mode will not be able to identify |
| 296 | * the phy mode that usb port is used. |
| 297 | * Machine file overrides board_usb_phy_mode. |
| 298 | * |
| 299 | * Return: USB_INIT_DEVICE or USB_INIT_HOST |
| 300 | */ |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 301 | int __weak board_usb_phy_mode(int port) |
| 302 | { |
| 303 | return usb_phy_mode(port); |
| 304 | } |
| 305 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 306 | /** |
| 307 | * board_ehci_hcd_init - set usb vbus voltage |
| 308 | * @port: usb otg port |
| 309 | * |
| 310 | * Target board specific, setup iomux pad to setup supply vbus voltage |
| 311 | * for usb otg port. Machine board file overrides board_ehci_hcd_init |
| 312 | * |
| 313 | * Return: 0 Success |
| 314 | */ |
Benoît Thébaudeau | f22e4fa | 2012-11-13 09:58:35 +0000 | [diff] [blame] | 315 | int __weak board_ehci_hcd_init(int port) |
| 316 | { |
| 317 | return 0; |
| 318 | } |
| 319 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 320 | /** |
| 321 | * board_ehci_power - enables/disables usb vbus voltage |
| 322 | * @port: usb otg port |
| 323 | * @on: on/off vbus voltage |
| 324 | * |
| 325 | * Enables/disables supply vbus voltage for usb otg port. |
| 326 | * Machine board file overrides board_ehci_power |
| 327 | * |
| 328 | * Return: 0 Success |
| 329 | */ |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 330 | int __weak board_ehci_power(int port, int on) |
| 331 | { |
| 332 | return 0; |
| 333 | } |
| 334 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 335 | int ehci_hcd_init(int index, enum usb_init_type init, |
| 336 | struct ehci_hccr **hccr, struct ehci_hcor **hcor) |
| 337 | { |
| 338 | enum usb_init_type type; |
Giulio Benetti | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 339 | #if defined(CONFIG_MX6) || defined(CONFIG_IMXRT) |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 340 | u32 controller_spacing = 0x200; |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 341 | struct anatop_regs __iomem *anatop = |
| 342 | (struct anatop_regs __iomem *)ANATOP_BASE_ADDR; |
Marek Vasut | 6686469 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 343 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 344 | USB_OTHERREGS_OFFSET); |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 345 | #elif defined(CONFIG_MX7) |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 346 | u32 controller_spacing = 0x10000; |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 347 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 348 | (0x10000 * index) + USBNC_OFFSET); |
| 349 | #elif defined(CONFIG_MX7ULP) |
| 350 | u32 controller_spacing = 0x10000; |
| 351 | struct usbphy_regs __iomem *usbphy = |
| 352 | (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR; |
Marek Vasut | 6686469 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 353 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 354 | (0x10000 * index) + USBNC_OFFSET); |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 355 | #endif |
| 356 | struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR + |
| 357 | (controller_spacing * index)); |
| 358 | int ret; |
| 359 | |
| 360 | if (index > 3) |
| 361 | return -EINVAL; |
| 362 | |
Peng Fan | 0bd3d91 | 2020-05-01 22:08:36 +0800 | [diff] [blame] | 363 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 364 | if (usb_fused((ulong)ehci)) { |
| 365 | printf("SoC fuse indicates USB@0x%lx is unavailable.\n", |
| 366 | (ulong)ehci); |
| 367 | return -ENODEV; |
| 368 | } |
| 369 | } |
| 370 | |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 371 | enable_usboh3_clk(1); |
| 372 | mdelay(1); |
| 373 | |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 374 | /* Do board specific initialization */ |
| 375 | ret = board_ehci_hcd_init(index); |
| 376 | if (ret) { |
| 377 | enable_usboh3_clk(0); |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 378 | return ret; |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Giulio Benetti | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 381 | #if defined(CONFIG_MX6) || defined(CONFIG_IMXRT) |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 382 | usb_power_config_mx6(anatop, index); |
| 383 | #elif defined (CONFIG_MX7) |
| 384 | usb_power_config_mx7(usbnc); |
| 385 | #elif defined (CONFIG_MX7ULP) |
| 386 | usb_power_config_mx7ulp(usbphy); |
| 387 | #endif |
| 388 | |
Marek Vasut | 6686469 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 389 | usb_oc_config(usbnc, index); |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 390 | |
Giulio Benetti | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 391 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) || defined(CONFIG_IMXRT) |
Marek Vasut | eb64f59 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 392 | if (index < ARRAY_SIZE(phy_bases)) { |
| 393 | usb_internal_phy_clock_gate((void __iomem *)phy_bases[index], 1); |
| 394 | usb_phy_enable(ehci, (void __iomem *)phy_bases[index]); |
| 395 | } |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 396 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 397 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 398 | type = board_usb_phy_mode(index); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 399 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 400 | if (hccr && hcor) { |
Marek Vasut | f444f89 | 2021-04-06 20:37:16 +0200 | [diff] [blame] | 401 | *hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength); |
| 402 | *hcor = (struct ehci_hcor *)((uintptr_t)*hccr + |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 403 | HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase))); |
| 404 | } |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 405 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 406 | if ((type == init) || (type == USB_INIT_DEVICE)) |
| 407 | board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1); |
| 408 | if (type != init) |
| 409 | return -ENODEV; |
| 410 | if (type == USB_INIT_DEVICE) |
| 411 | return 0; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 412 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 413 | setbits_le32(&ehci->usbmode, CM_HOST); |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 414 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 415 | setbits_le32(&ehci->portsc, USB_EN); |
| 416 | |
| 417 | mdelay(10); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Lucas Stach | 676ae06 | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 422 | int ehci_hcd_stop(int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 423 | { |
| 424 | return 0; |
| 425 | } |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 426 | #else |
| 427 | struct ehci_mx6_priv_data { |
| 428 | struct ehci_ctrl ctrl; |
| 429 | struct usb_ehci *ehci; |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 430 | struct udevice *vbus_supply; |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 431 | struct clk clk; |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 432 | struct phy phy; |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 433 | enum usb_init_type init_type; |
Matthias Schiffer | 0f513c5 | 2021-09-20 15:37:25 +0200 | [diff] [blame] | 434 | enum usb_phy_interface phy_type; |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 435 | #if !defined(CONFIG_PHY) |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 436 | int portnr; |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 437 | void __iomem *phy_addr; |
| 438 | void __iomem *misc_addr; |
| 439 | void __iomem *anatop_addr; |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 440 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 441 | }; |
| 442 | |
Matthias Schiffer | 0f513c5 | 2021-09-20 15:37:25 +0200 | [diff] [blame] | 443 | static u32 mx6_portsc(enum usb_phy_interface phy_type) |
| 444 | { |
| 445 | switch (phy_type) { |
| 446 | case USBPHY_INTERFACE_MODE_UTMI: |
| 447 | return PORT_PTS_UTMI; |
| 448 | case USBPHY_INTERFACE_MODE_UTMIW: |
| 449 | return PORT_PTS_UTMI | PORT_PTS_PTW; |
| 450 | case USBPHY_INTERFACE_MODE_ULPI: |
| 451 | return PORT_PTS_ULPI; |
| 452 | case USBPHY_INTERFACE_MODE_SERIAL: |
| 453 | return PORT_PTS_SERIAL; |
| 454 | case USBPHY_INTERFACE_MODE_HSIC: |
| 455 | return PORT_PTS_HSIC; |
| 456 | default: |
| 457 | return CONFIG_MXC_USB_PORTSC; |
| 458 | } |
| 459 | } |
| 460 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 461 | static int mx6_init_after_reset(struct ehci_ctrl *dev) |
| 462 | { |
| 463 | struct ehci_mx6_priv_data *priv = dev->priv; |
| 464 | enum usb_init_type type = priv->init_type; |
| 465 | struct usb_ehci *ehci = priv->ehci; |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 466 | |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 467 | #if !defined(CONFIG_PHY) |
| 468 | usb_power_config_mx6(priv->anatop_addr, priv->portnr); |
| 469 | usb_power_config_mx7(priv->misc_addr); |
| 470 | usb_power_config_mx7ulp(priv->phy_addr); |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 471 | |
Marek Vasut | 6686469 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 472 | usb_oc_config(priv->misc_addr, priv->portnr); |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 473 | |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 474 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) |
Marek Vasut | eb64f59 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 475 | usb_internal_phy_clock_gate(priv->phy_addr, 1); |
| 476 | usb_phy_enable(ehci, priv->phy_addr); |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 477 | #endif |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 478 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 479 | |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 480 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 481 | if (priv->vbus_supply) { |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 482 | int ret; |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 483 | ret = regulator_set_enable(priv->vbus_supply, |
| 484 | (type == USB_INIT_DEVICE) ? |
| 485 | false : true); |
Marek Vasut | 10bcafb | 2020-05-21 23:32:23 +0200 | [diff] [blame] | 486 | if (ret && ret != -ENOSYS) { |
Marek Vasut | 73021d1 | 2020-05-21 23:34:06 +0200 | [diff] [blame] | 487 | printf("Error enabling VBUS supply (ret=%i)\n", ret); |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 488 | return ret; |
| 489 | } |
| 490 | } |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 491 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 492 | |
| 493 | if (type == USB_INIT_DEVICE) |
| 494 | return 0; |
| 495 | |
| 496 | setbits_le32(&ehci->usbmode, CM_HOST); |
Matthias Schiffer | 0f513c5 | 2021-09-20 15:37:25 +0200 | [diff] [blame] | 497 | writel(mx6_portsc(priv->phy_type), &ehci->portsc); |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 498 | setbits_le32(&ehci->portsc, USB_EN); |
| 499 | |
| 500 | mdelay(10); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static const struct ehci_ops mx6_ehci_ops = { |
| 506 | .init_after_reset = mx6_init_after_reset |
| 507 | }; |
| 508 | |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 509 | static int ehci_usb_phy_mode(struct udevice *dev) |
| 510 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 511 | struct usb_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 512 | void *__iomem addr = dev_read_addr_ptr(dev); |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 513 | void *__iomem phy_ctrl, *__iomem phy_status; |
| 514 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 515 | int offset = dev_of_offset(dev), phy_off; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 516 | u32 val; |
| 517 | |
| 518 | /* |
| 519 | * About fsl,usbphy, Refer to |
| 520 | * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt. |
| 521 | */ |
Giulio Benetti | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 522 | if (is_mx6() || is_mx7ulp() || is_imxrt()) { |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 523 | phy_off = fdtdec_lookup_phandle(blob, |
| 524 | offset, |
| 525 | "fsl,usbphy"); |
| 526 | if (phy_off < 0) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, |
| 530 | "reg"); |
| 531 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 532 | return -EINVAL; |
| 533 | |
| 534 | phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL); |
| 535 | val = readl(phy_ctrl); |
| 536 | |
| 537 | if (val & USBPHY_CTRL_OTG_ID) |
| 538 | plat->init_type = USB_INIT_DEVICE; |
| 539 | else |
| 540 | plat->init_type = USB_INIT_HOST; |
Adam Ford | 078dfef | 2022-02-03 15:20:11 -0600 | [diff] [blame] | 541 | } else if (is_mx7() || is_imx8mm() || is_imx8mn()) { |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 542 | phy_status = (void __iomem *)(addr + |
| 543 | USBNC_PHY_STATUS_OFFSET); |
| 544 | val = readl(phy_status); |
| 545 | |
| 546 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 547 | plat->init_type = USB_INIT_DEVICE; |
| 548 | else |
| 549 | plat->init_type = USB_INIT_HOST; |
| 550 | } else { |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 557 | static int ehci_usb_of_to_plat(struct udevice *dev) |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 558 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 559 | struct usb_plat *plat = dev_get_plat(dev); |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 560 | enum usb_dr_mode dr_mode; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 561 | |
Simon Glass | f10643c | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 562 | dr_mode = usb_get_dr_mode(dev_ofnode(dev)); |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 563 | |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 564 | switch (dr_mode) { |
| 565 | case USB_DR_MODE_HOST: |
| 566 | plat->init_type = USB_INIT_HOST; |
| 567 | break; |
| 568 | case USB_DR_MODE_PERIPHERAL: |
| 569 | plat->init_type = USB_INIT_DEVICE; |
| 570 | break; |
Adam Ford | 078dfef | 2022-02-03 15:20:11 -0600 | [diff] [blame] | 571 | default: |
| 572 | plat->init_type = USB_INIT_UNKNOWN; |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 573 | }; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 574 | |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 575 | return 0; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 576 | } |
| 577 | |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 578 | static int mx6_parse_dt_addrs(struct udevice *dev) |
| 579 | { |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 580 | #if !defined(CONFIG_PHY) |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 581 | struct ehci_mx6_priv_data *priv = dev_get_priv(dev); |
| 582 | int phy_off, misc_off; |
| 583 | const void *blob = gd->fdt_blob; |
| 584 | int offset = dev_of_offset(dev); |
| 585 | void *__iomem addr; |
| 586 | |
| 587 | phy_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbphy"); |
| 588 | if (phy_off < 0) { |
| 589 | phy_off = fdtdec_lookup_phandle(blob, offset, "phys"); |
| 590 | if (phy_off < 0) |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | misc_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbmisc"); |
| 595 | if (misc_off < 0) |
| 596 | return -EINVAL; |
| 597 | |
| 598 | addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, "reg"); |
| 599 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
Fabio Estevam | 4822114 | 2021-06-20 12:00:52 -0300 | [diff] [blame] | 600 | addr = NULL; |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 601 | |
| 602 | priv->phy_addr = addr; |
| 603 | |
| 604 | addr = (void __iomem *)fdtdec_get_addr(blob, misc_off, "reg"); |
| 605 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | priv->misc_addr = addr; |
| 609 | |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 610 | #if defined(CONFIG_MX6) |
Fabio Estevam | ec326b9 | 2021-06-20 12:00:51 -0300 | [diff] [blame] | 611 | int anatop_off, ret, devnump; |
| 612 | |
| 613 | ret = fdtdec_get_alias_seq(blob, dev->uclass->uc_drv->name, |
| 614 | phy_off, &devnump); |
| 615 | if (ret < 0) |
| 616 | return ret; |
| 617 | priv->portnr = devnump; |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 618 | |
| 619 | /* Resolve ANATOP offset through USB PHY node */ |
| 620 | anatop_off = fdtdec_lookup_phandle(blob, phy_off, "fsl,anatop"); |
| 621 | if (anatop_off < 0) |
| 622 | return -EINVAL; |
| 623 | |
| 624 | addr = (void __iomem *)fdtdec_get_addr(blob, anatop_off, "reg"); |
| 625 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 626 | return -EINVAL; |
| 627 | |
| 628 | priv->anatop_addr = addr; |
| 629 | #endif |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 630 | #endif |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 634 | static int ehci_usb_probe(struct udevice *dev) |
| 635 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 636 | struct usb_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 637 | struct usb_ehci *ehci = dev_read_addr_ptr(dev); |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 638 | struct ehci_mx6_priv_data *priv = dev_get_priv(dev); |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 639 | enum usb_init_type type = plat->init_type; |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 640 | struct ehci_hccr *hccr; |
| 641 | struct ehci_hcor *hcor; |
| 642 | int ret; |
| 643 | |
Peng Fan | 0bd3d91 | 2020-05-01 22:08:36 +0800 | [diff] [blame] | 644 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 645 | if (usb_fused((ulong)ehci)) { |
| 646 | printf("SoC fuse indicates USB@0x%lx is unavailable.\n", |
| 647 | (ulong)ehci); |
| 648 | return -ENODEV; |
| 649 | } |
| 650 | } |
| 651 | |
Marek Vasut | 4dcfa3b | 2021-03-31 23:06:07 +0200 | [diff] [blame] | 652 | ret = mx6_parse_dt_addrs(dev); |
| 653 | if (ret) |
| 654 | return ret; |
| 655 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 656 | priv->ehci = ehci; |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 657 | priv->init_type = type; |
Matthias Schiffer | 0f513c5 | 2021-09-20 15:37:25 +0200 | [diff] [blame] | 658 | priv->phy_type = usb_get_phy_mode(dev_ofnode(dev)); |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 659 | |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 660 | #if CONFIG_IS_ENABLED(CLK) |
| 661 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 662 | if (ret < 0) |
| 663 | return ret; |
| 664 | |
| 665 | ret = clk_enable(&priv->clk); |
| 666 | if (ret) |
| 667 | return ret; |
| 668 | #else |
| 669 | /* Compatibility with DM_USB and !CLK */ |
| 670 | enable_usboh3_clk(1); |
| 671 | mdelay(1); |
| 672 | #endif |
| 673 | |
Adam Ford | 078dfef | 2022-02-03 15:20:11 -0600 | [diff] [blame] | 674 | /* |
| 675 | * If the device tree didn't specify host or device, |
| 676 | * the default is USB_INIT_UNKNOWN, so we need to check |
| 677 | * the register. For imx8mm and imx8mn, the clocks need to be |
| 678 | * running first, so we defer the check until they are. |
| 679 | */ |
| 680 | if (priv->init_type == USB_INIT_UNKNOWN) { |
| 681 | ret = ehci_usb_phy_mode(dev); |
| 682 | if (ret) |
| 683 | goto err_clk; |
| 684 | else |
| 685 | priv->init_type = plat->init_type; |
| 686 | } |
| 687 | |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 688 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 689 | ret = device_get_supply_regulator(dev, "vbus-supply", |
| 690 | &priv->vbus_supply); |
| 691 | if (ret) |
| 692 | debug("%s: No vbus supply\n", dev->name); |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 693 | #endif |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 694 | |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 695 | #if !defined(CONFIG_PHY) |
| 696 | usb_power_config_mx6(priv->anatop_addr, priv->portnr); |
| 697 | usb_power_config_mx7(priv->misc_addr); |
| 698 | usb_power_config_mx7ulp(priv->phy_addr); |
Marek Vasut | 849763b | 2021-03-31 23:00:23 +0200 | [diff] [blame] | 699 | |
Marek Vasut | 6686469 | 2021-03-31 23:24:41 +0200 | [diff] [blame] | 700 | usb_oc_config(priv->misc_addr, priv->portnr); |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 701 | |
Giulio Benetti | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 702 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) || defined(CONFIG_IMXRT) |
Marek Vasut | eb64f59 | 2021-03-31 22:10:35 +0200 | [diff] [blame] | 703 | usb_internal_phy_clock_gate(priv->phy_addr, 1); |
| 704 | usb_phy_enable(ehci, priv->phy_addr); |
Marek Vasut | ef464e4 | 2021-03-31 21:40:24 +0200 | [diff] [blame] | 705 | #endif |
Marek Vasut | 53396d6 | 2021-04-22 21:06:40 +0200 | [diff] [blame] | 706 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 707 | |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 708 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 709 | if (priv->vbus_supply) { |
| 710 | ret = regulator_set_enable(priv->vbus_supply, |
| 711 | (type == USB_INIT_DEVICE) ? |
| 712 | false : true); |
Marek Vasut | 10bcafb | 2020-05-21 23:32:23 +0200 | [diff] [blame] | 713 | if (ret && ret != -ENOSYS) { |
Marek Vasut | 73021d1 | 2020-05-21 23:34:06 +0200 | [diff] [blame] | 714 | printf("Error enabling VBUS supply (ret=%i)\n", ret); |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 715 | goto err_clk; |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 716 | } |
| 717 | } |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 718 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 719 | |
| 720 | if (priv->init_type == USB_INIT_HOST) { |
| 721 | setbits_le32(&ehci->usbmode, CM_HOST); |
Matthias Schiffer | 0f513c5 | 2021-09-20 15:37:25 +0200 | [diff] [blame] | 722 | writel(mx6_portsc(priv->phy_type), &ehci->portsc); |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 723 | setbits_le32(&ehci->portsc, USB_EN); |
| 724 | } |
| 725 | |
| 726 | mdelay(10); |
| 727 | |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 728 | #if defined(CONFIG_PHY) |
| 729 | ret = ehci_setup_phy(dev, &priv->phy, 0); |
| 730 | if (ret) |
| 731 | goto err_regulator; |
| 732 | #endif |
| 733 | |
Marek Vasut | f444f89 | 2021-04-06 20:37:16 +0200 | [diff] [blame] | 734 | hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength); |
| 735 | hcor = (struct ehci_hcor *)((uintptr_t)hccr + |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 736 | HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); |
| 737 | |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 738 | ret = ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type); |
| 739 | if (ret) |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 740 | goto err_phy; |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 741 | |
| 742 | return ret; |
| 743 | |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 744 | err_phy: |
| 745 | #if defined(CONFIG_PHY) |
| 746 | ehci_shutdown_phy(dev, &priv->phy); |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 747 | err_regulator: |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 748 | #endif |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 749 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 750 | if (priv->vbus_supply) |
| 751 | regulator_set_enable(priv->vbus_supply, false); |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 752 | #endif |
Adam Ford | 078dfef | 2022-02-03 15:20:11 -0600 | [diff] [blame] | 753 | err_clk: |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 754 | #if CONFIG_IS_ENABLED(CLK) |
| 755 | clk_disable(&priv->clk); |
| 756 | #else |
| 757 | /* Compatibility with DM_USB and !CLK */ |
| 758 | enable_usboh3_clk(0); |
| 759 | #endif |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | int ehci_usb_remove(struct udevice *dev) |
| 764 | { |
| 765 | struct ehci_mx6_priv_data *priv __maybe_unused = dev_get_priv(dev); |
| 766 | |
| 767 | ehci_deregister(dev); |
| 768 | |
Marek Vasut | 50d0146 | 2021-04-02 13:07:49 +0200 | [diff] [blame] | 769 | #if defined(CONFIG_PHY) |
| 770 | ehci_shutdown_phy(dev, &priv->phy); |
| 771 | #endif |
| 772 | |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 773 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 774 | if (priv->vbus_supply) |
| 775 | regulator_set_enable(priv->vbus_supply, false); |
| 776 | #endif |
| 777 | |
Marek Vasut | 7f2c10e | 2021-03-31 12:28:03 +0200 | [diff] [blame] | 778 | #if CONFIG_IS_ENABLED(CLK) |
| 779 | clk_disable(&priv->clk); |
| 780 | #endif |
| 781 | |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 782 | return 0; |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 783 | } |
| 784 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 785 | static const struct udevice_id mx6_usb_ids[] = { |
| 786 | { .compatible = "fsl,imx27-usb" }, |
Marek Vasut | e87015f | 2021-04-02 13:07:59 +0200 | [diff] [blame] | 787 | { .compatible = "fsl,imx7d-usb" }, |
Giulio Benetti | e7e81e8 | 2021-05-20 16:10:15 +0200 | [diff] [blame] | 788 | { .compatible = "fsl,imxrt-usb" }, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 789 | { } |
| 790 | }; |
| 791 | |
| 792 | U_BOOT_DRIVER(usb_mx6) = { |
| 793 | .name = "ehci_mx6", |
| 794 | .id = UCLASS_USB, |
| 795 | .of_match = mx6_usb_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 796 | .of_to_plat = ehci_usb_of_to_plat, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 797 | .probe = ehci_usb_probe, |
Marek Vasut | 7e1f1e1 | 2021-03-31 12:19:27 +0200 | [diff] [blame] | 798 | .remove = ehci_usb_remove, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 799 | .ops = &ehci_usb_ops, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 800 | .plat_auto = sizeof(struct usb_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 801 | .priv_auto = sizeof(struct ehci_mx6_priv_data), |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 802 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 803 | }; |
| 804 | #endif |