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> |
| 8 | #include <usb.h> |
| 9 | #include <errno.h> |
Mateusz Kulikowski | 8c25c25 | 2016-01-23 11:54:32 +0100 | [diff] [blame] | 10 | #include <wait_bit.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Mateusz Kulikowski | e162c6b | 2016-03-31 23:12:23 +0200 | [diff] [blame] | 12 | #include <usb/ehci-ci.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/imx-regs.h> |
| 15 | #include <asm/arch/clock.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 16 | #include <asm/mach-imx/iomux-v3.h> |
| 17 | #include <asm/mach-imx/sys_proto.h> |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 18 | #include <dm.h> |
Simon Glass | c62db35 | 2017-05-31 19:47:48 -0600 | [diff] [blame] | 19 | #include <asm/mach-types.h> |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 20 | #include <power/regulator.h> |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 21 | #include <linux/usb/otg.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 22 | |
| 23 | #include "ehci.h" |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 24 | |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 27 | #define USB_OTGREGS_OFFSET 0x000 |
| 28 | #define USB_H1REGS_OFFSET 0x200 |
| 29 | #define USB_H2REGS_OFFSET 0x400 |
| 30 | #define USB_H3REGS_OFFSET 0x600 |
| 31 | #define USB_OTHERREGS_OFFSET 0x800 |
| 32 | |
| 33 | #define USB_H1_CTRL_OFFSET 0x04 |
| 34 | |
| 35 | #define USBPHY_CTRL 0x00000030 |
| 36 | #define USBPHY_CTRL_SET 0x00000034 |
| 37 | #define USBPHY_CTRL_CLR 0x00000038 |
| 38 | #define USBPHY_CTRL_TOG 0x0000003c |
| 39 | |
| 40 | #define USBPHY_PWD 0x00000000 |
| 41 | #define USBPHY_CTRL_SFTRST 0x80000000 |
| 42 | #define USBPHY_CTRL_CLKGATE 0x40000000 |
| 43 | #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000 |
| 44 | #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000 |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 45 | #define USBPHY_CTRL_OTG_ID 0x08000000 |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 46 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 47 | #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000 |
| 48 | #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000 |
| 49 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 50 | #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000 |
| 51 | #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000 |
| 52 | #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000 |
| 53 | #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040 |
| 54 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 55 | #define USBNC_OFFSET 0x200 |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 56 | #define USBNC_PHY_STATUS_OFFSET 0x23C |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 57 | #define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */ |
| 58 | #define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */ |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 59 | #define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */ |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 60 | #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */ |
| 61 | #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */ |
| 62 | |
| 63 | /* USBCMD */ |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 64 | #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */ |
| 65 | #define UCMD_RESET (1 << 1) /* controller reset */ |
| 66 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 67 | #if defined(CONFIG_MX6) |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 68 | static const unsigned phy_bases[] = { |
| 69 | USB_PHY0_BASE_ADDR, |
| 70 | USB_PHY1_BASE_ADDR, |
| 71 | }; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 72 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 73 | static void usb_internal_phy_clock_gate(int index, int on) |
| 74 | { |
| 75 | void __iomem *phy_reg; |
| 76 | |
| 77 | if (index >= ARRAY_SIZE(phy_bases)) |
| 78 | return; |
| 79 | |
| 80 | phy_reg = (void __iomem *)phy_bases[index]; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 81 | phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET; |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 82 | writel(USBPHY_CTRL_CLKGATE, phy_reg); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 85 | static void usb_power_config(int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 86 | { |
Wolfgang Grandegger | 3f29d96 | 2012-05-02 04:36:39 +0000 | [diff] [blame] | 87 | struct anatop_regs __iomem *anatop = |
| 88 | (struct anatop_regs __iomem *)ANATOP_BASE_ADDR; |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 89 | void __iomem *chrg_detect; |
| 90 | void __iomem *pll_480_ctrl_clr; |
| 91 | void __iomem *pll_480_ctrl_set; |
| 92 | |
| 93 | switch (index) { |
| 94 | case 0: |
| 95 | chrg_detect = &anatop->usb1_chrg_detect; |
| 96 | pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr; |
| 97 | pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set; |
| 98 | break; |
| 99 | case 1: |
| 100 | chrg_detect = &anatop->usb2_chrg_detect; |
| 101 | pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr; |
| 102 | pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set; |
| 103 | break; |
| 104 | default: |
| 105 | return; |
| 106 | } |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 107 | /* |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 108 | * Some phy and power's special controls |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 109 | * 1. The external charger detector needs to be disabled |
| 110 | * or the signal at DP will be poor |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 111 | * 2. The PLL's power and output to usb |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 112 | * is totally controlled by IC, so the Software only needs |
| 113 | * to enable them at initializtion. |
| 114 | */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 115 | writel(ANADIG_USB2_CHRG_DETECT_EN_B | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 116 | ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 117 | chrg_detect); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 118 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 119 | writel(ANADIG_USB2_PLL_480_CTRL_BYPASS, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 120 | pll_480_ctrl_clr); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 121 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 122 | writel(ANADIG_USB2_PLL_480_CTRL_ENABLE | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 123 | ANADIG_USB2_PLL_480_CTRL_POWER | |
| 124 | ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 125 | pll_480_ctrl_set); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 128 | /* Return 0 : host node, <>0 : device mode */ |
| 129 | static int usb_phy_enable(int index, struct usb_ehci *ehci) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 130 | { |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 131 | void __iomem *phy_reg; |
| 132 | void __iomem *phy_ctrl; |
| 133 | void __iomem *usb_cmd; |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 134 | int ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 135 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 136 | if (index >= ARRAY_SIZE(phy_bases)) |
| 137 | return 0; |
| 138 | |
| 139 | phy_reg = (void __iomem *)phy_bases[index]; |
| 140 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 141 | usb_cmd = (void __iomem *)&ehci->usbcmd; |
| 142 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 143 | /* Stop then Reset */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 144 | clrbits_le32(usb_cmd, UCMD_RUN_STOP); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 145 | 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] | 146 | if (ret) |
| 147 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 148 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 149 | setbits_le32(usb_cmd, UCMD_RESET); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 150 | ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false); |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 151 | if (ret) |
| 152 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 153 | |
| 154 | /* Reset USBPHY module */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 155 | setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 156 | udelay(10); |
| 157 | |
| 158 | /* Remove CLKGATE and SFTRST */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 159 | clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 160 | udelay(10); |
| 161 | |
| 162 | /* Power up the PHY */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 163 | writel(0, phy_reg + USBPHY_PWD); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 164 | /* enable FS/LS device */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 165 | setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 | |
| 166 | USBPHY_CTRL_ENUTMILEVEL3); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 167 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 168 | return 0; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 171 | int usb_phy_mode(int port) |
| 172 | { |
| 173 | void __iomem *phy_reg; |
| 174 | void __iomem *phy_ctrl; |
| 175 | u32 val; |
| 176 | |
| 177 | phy_reg = (void __iomem *)phy_bases[port]; |
| 178 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 179 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 180 | val = readl(phy_ctrl); |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 181 | |
| 182 | if (val & USBPHY_CTRL_OTG_ID) |
| 183 | return USB_INIT_DEVICE; |
| 184 | else |
| 185 | return USB_INIT_HOST; |
| 186 | } |
| 187 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 188 | /* Base address for this IP block is 0x02184800 */ |
| 189 | struct usbnc_regs { |
| 190 | u32 ctrl[4]; /* otg/host1-3 */ |
| 191 | u32 uh2_hsic_ctrl; |
| 192 | u32 uh3_hsic_ctrl; |
| 193 | u32 otg_phy_ctrl_0; |
| 194 | u32 uh1_phy_ctrl_0; |
| 195 | }; |
| 196 | #elif defined(CONFIG_MX7) |
| 197 | struct usbnc_regs { |
| 198 | u32 ctrl1; |
| 199 | u32 ctrl2; |
| 200 | u32 reserve1[10]; |
| 201 | u32 phy_cfg1; |
| 202 | u32 phy_cfg2; |
Peng Fan | 429ff44 | 2016-06-20 09:43:08 +0800 | [diff] [blame] | 203 | u32 reserve2; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 204 | u32 phy_status; |
Peng Fan | 429ff44 | 2016-06-20 09:43:08 +0800 | [diff] [blame] | 205 | u32 reserve3[4]; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 206 | u32 adp_cfg1; |
| 207 | u32 adp_cfg2; |
| 208 | u32 adp_status; |
| 209 | }; |
| 210 | |
| 211 | static void usb_power_config(int index) |
| 212 | { |
| 213 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 214 | (0x10000 * index) + USBNC_OFFSET); |
| 215 | void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2); |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 216 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 217 | |
Peng Fan | 57de41e | 2016-06-20 09:43:09 +0800 | [diff] [blame] | 218 | /* |
| 219 | * Clear the ACAENB to enable usb_otg_id detection, |
| 220 | * otherwise it is the ACA detection enabled. |
| 221 | */ |
| 222 | clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB); |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 223 | |
| 224 | /* Set power polarity to high active */ |
Stefan Agner | c448309 | 2016-07-13 00:25:38 -0700 | [diff] [blame] | 225 | #ifdef CONFIG_MXC_USB_OTG_HACTIVE |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 226 | setbits_le32(ctrl, UCTRL_PWR_POL); |
Stefan Agner | c448309 | 2016-07-13 00:25:38 -0700 | [diff] [blame] | 227 | #else |
| 228 | clrbits_le32(ctrl, UCTRL_PWR_POL); |
| 229 | #endif |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int usb_phy_mode(int port) |
| 233 | { |
| 234 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 235 | (0x10000 * port) + USBNC_OFFSET); |
| 236 | void __iomem *status = (void __iomem *)(&usbnc->phy_status); |
| 237 | u32 val; |
| 238 | |
| 239 | val = readl(status); |
| 240 | |
| 241 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 242 | return USB_INIT_DEVICE; |
| 243 | else |
| 244 | return USB_INIT_HOST; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | static void usb_oc_config(int index) |
| 249 | { |
| 250 | #if defined(CONFIG_MX6) |
| 251 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 252 | USB_OTHERREGS_OFFSET); |
| 253 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]); |
| 254 | #elif defined(CONFIG_MX7) |
| 255 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 256 | (0x10000 * index) + USBNC_OFFSET); |
| 257 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1); |
| 258 | #endif |
| 259 | |
| 260 | #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2 |
| 261 | /* mx6qarm2 seems to required a different setting*/ |
| 262 | clrbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
| 263 | #else |
| 264 | setbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
| 265 | #endif |
| 266 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 267 | setbits_le32(ctrl, UCTRL_OVER_CUR_DIS); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 270 | /** |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 271 | * board_usb_phy_mode - override usb phy mode |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 272 | * @port: usb host/otg port |
| 273 | * |
| 274 | * Target board specific, override usb_phy_mode. |
| 275 | * When usb-otg is used as usb host port, iomux pad usb_otg_id can be |
| 276 | * left disconnected in this case usb_phy_mode will not be able to identify |
| 277 | * the phy mode that usb port is used. |
| 278 | * Machine file overrides board_usb_phy_mode. |
| 279 | * |
| 280 | * Return: USB_INIT_DEVICE or USB_INIT_HOST |
| 281 | */ |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 282 | int __weak board_usb_phy_mode(int port) |
| 283 | { |
| 284 | return usb_phy_mode(port); |
| 285 | } |
| 286 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 287 | /** |
| 288 | * board_ehci_hcd_init - set usb vbus voltage |
| 289 | * @port: usb otg port |
| 290 | * |
| 291 | * Target board specific, setup iomux pad to setup supply vbus voltage |
| 292 | * for usb otg port. Machine board file overrides board_ehci_hcd_init |
| 293 | * |
| 294 | * Return: 0 Success |
| 295 | */ |
Benoît Thébaudeau | f22e4fa | 2012-11-13 09:58:35 +0000 | [diff] [blame] | 296 | int __weak board_ehci_hcd_init(int port) |
| 297 | { |
| 298 | return 0; |
| 299 | } |
| 300 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 301 | /** |
| 302 | * board_ehci_power - enables/disables usb vbus voltage |
| 303 | * @port: usb otg port |
| 304 | * @on: on/off vbus voltage |
| 305 | * |
| 306 | * Enables/disables supply vbus voltage for usb otg port. |
| 307 | * Machine board file overrides board_ehci_power |
| 308 | * |
| 309 | * Return: 0 Success |
| 310 | */ |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 311 | int __weak board_ehci_power(int port, int on) |
| 312 | { |
| 313 | return 0; |
| 314 | } |
| 315 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 316 | int ehci_mx6_common_init(struct usb_ehci *ehci, int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 317 | { |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 318 | int ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 319 | |
| 320 | enable_usboh3_clk(1); |
| 321 | mdelay(1); |
| 322 | |
| 323 | /* Do board specific initialization */ |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 324 | ret = board_ehci_hcd_init(index); |
| 325 | if (ret) |
| 326 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 327 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 328 | usb_power_config(index); |
| 329 | usb_oc_config(index); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 330 | |
| 331 | #if defined(CONFIG_MX6) |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 332 | usb_internal_phy_clock_gate(index, 1); |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 333 | usb_phy_enable(index, ehci); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 334 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 339 | #if !CONFIG_IS_ENABLED(DM_USB) |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 340 | int ehci_hcd_init(int index, enum usb_init_type init, |
| 341 | struct ehci_hccr **hccr, struct ehci_hcor **hcor) |
| 342 | { |
| 343 | enum usb_init_type type; |
| 344 | #if defined(CONFIG_MX6) |
| 345 | u32 controller_spacing = 0x200; |
| 346 | #elif defined(CONFIG_MX7) |
| 347 | u32 controller_spacing = 0x10000; |
| 348 | #endif |
| 349 | struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR + |
| 350 | (controller_spacing * index)); |
| 351 | int ret; |
| 352 | |
| 353 | if (index > 3) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | ret = ehci_mx6_common_init(ehci, index); |
| 357 | if (ret) |
| 358 | return ret; |
| 359 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 360 | type = board_usb_phy_mode(index); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 361 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 362 | if (hccr && hcor) { |
| 363 | *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 364 | *hcor = (struct ehci_hcor *)((uint32_t)*hccr + |
| 365 | HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase))); |
| 366 | } |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 367 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 368 | if ((type == init) || (type == USB_INIT_DEVICE)) |
| 369 | board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1); |
| 370 | if (type != init) |
| 371 | return -ENODEV; |
| 372 | if (type == USB_INIT_DEVICE) |
| 373 | return 0; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 374 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 375 | setbits_le32(&ehci->usbmode, CM_HOST); |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 376 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 377 | setbits_le32(&ehci->portsc, USB_EN); |
| 378 | |
| 379 | mdelay(10); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Lucas Stach | 676ae06 | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 384 | int ehci_hcd_stop(int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 385 | { |
| 386 | return 0; |
| 387 | } |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 388 | #else |
| 389 | struct ehci_mx6_priv_data { |
| 390 | struct ehci_ctrl ctrl; |
| 391 | struct usb_ehci *ehci; |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 392 | struct udevice *vbus_supply; |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 393 | enum usb_init_type init_type; |
| 394 | int portnr; |
| 395 | }; |
| 396 | |
| 397 | static int mx6_init_after_reset(struct ehci_ctrl *dev) |
| 398 | { |
| 399 | struct ehci_mx6_priv_data *priv = dev->priv; |
| 400 | enum usb_init_type type = priv->init_type; |
| 401 | struct usb_ehci *ehci = priv->ehci; |
| 402 | int ret; |
| 403 | |
| 404 | ret = ehci_mx6_common_init(priv->ehci, priv->portnr); |
| 405 | if (ret) |
| 406 | return ret; |
| 407 | |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 408 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 409 | if (priv->vbus_supply) { |
| 410 | ret = regulator_set_enable(priv->vbus_supply, |
| 411 | (type == USB_INIT_DEVICE) ? |
| 412 | false : true); |
| 413 | if (ret) { |
| 414 | puts("Error enabling VBUS supply\n"); |
| 415 | return ret; |
| 416 | } |
| 417 | } |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 418 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 419 | |
| 420 | if (type == USB_INIT_DEVICE) |
| 421 | return 0; |
| 422 | |
| 423 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 424 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
| 425 | setbits_le32(&ehci->portsc, USB_EN); |
| 426 | |
| 427 | mdelay(10); |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static const struct ehci_ops mx6_ehci_ops = { |
| 433 | .init_after_reset = mx6_init_after_reset |
| 434 | }; |
| 435 | |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 436 | static int ehci_usb_phy_mode(struct udevice *dev) |
| 437 | { |
| 438 | struct usb_platdata *plat = dev_get_platdata(dev); |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 439 | void *__iomem addr = (void *__iomem)devfdt_get_addr(dev); |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 440 | void *__iomem phy_ctrl, *__iomem phy_status; |
| 441 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 442 | int offset = dev_of_offset(dev), phy_off; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 443 | u32 val; |
| 444 | |
| 445 | /* |
| 446 | * About fsl,usbphy, Refer to |
| 447 | * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt. |
| 448 | */ |
| 449 | if (is_mx6()) { |
| 450 | phy_off = fdtdec_lookup_phandle(blob, |
| 451 | offset, |
| 452 | "fsl,usbphy"); |
| 453 | if (phy_off < 0) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, |
| 457 | "reg"); |
| 458 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 459 | return -EINVAL; |
| 460 | |
| 461 | phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL); |
| 462 | val = readl(phy_ctrl); |
| 463 | |
| 464 | if (val & USBPHY_CTRL_OTG_ID) |
| 465 | plat->init_type = USB_INIT_DEVICE; |
| 466 | else |
| 467 | plat->init_type = USB_INIT_HOST; |
| 468 | } else if (is_mx7()) { |
| 469 | phy_status = (void __iomem *)(addr + |
| 470 | USBNC_PHY_STATUS_OFFSET); |
| 471 | val = readl(phy_status); |
| 472 | |
| 473 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 474 | plat->init_type = USB_INIT_DEVICE; |
| 475 | else |
| 476 | plat->init_type = USB_INIT_HOST; |
| 477 | } else { |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int ehci_usb_ofdata_to_platdata(struct udevice *dev) |
| 485 | { |
| 486 | struct usb_platdata *plat = dev_get_platdata(dev); |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 487 | enum usb_dr_mode dr_mode; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 488 | |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 489 | dr_mode = usb_get_dr_mode(dev_of_offset(dev)); |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 490 | |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 491 | switch (dr_mode) { |
| 492 | case USB_DR_MODE_HOST: |
| 493 | plat->init_type = USB_INIT_HOST; |
| 494 | break; |
| 495 | case USB_DR_MODE_PERIPHERAL: |
| 496 | plat->init_type = USB_INIT_DEVICE; |
| 497 | break; |
| 498 | case USB_DR_MODE_OTG: |
| 499 | case USB_DR_MODE_UNKNOWN: |
| 500 | return ehci_usb_phy_mode(dev); |
| 501 | }; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 502 | |
Adam Ford | 69535b3 | 2019-04-03 08:41:56 -0500 | [diff] [blame] | 503 | return 0; |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 504 | } |
| 505 | |
Marek Vasut | 501547c | 2019-06-24 19:05:47 +0200 | [diff] [blame] | 506 | static int ehci_usb_bind(struct udevice *dev) |
| 507 | { |
| 508 | /* |
| 509 | * TODO: |
| 510 | * This driver is only partly converted to DT probing and still uses |
| 511 | * a tremendous amount of hard-coded addresses. To make things worse, |
| 512 | * the driver depends on specific sequential indexing of controllers, |
| 513 | * from which it derives offsets in the PHY and ANATOP register sets. |
| 514 | * |
| 515 | * Here we attempt to calculate these indexes from DT information as |
| 516 | * well as we can. The USB controllers on all existing iMX6/iMX7 SoCs |
| 517 | * are placed next to each other, at addresses incremented by 0x200. |
| 518 | * Thus, the index is derived from the multiple of 0x200 offset from |
| 519 | * the first controller address. |
| 520 | * |
| 521 | * However, to complete conversion of this driver to DT probing, the |
| 522 | * following has to be done: |
| 523 | * - DM clock framework support for iMX must be implemented |
| 524 | * - usb_power_config() has to be converted to clock framework |
| 525 | * -> Thus, the ad-hoc "index" variable goes away. |
| 526 | * - USB PHY handling has to be factored out into separate driver |
| 527 | * -> Thus, the ad-hoc "index" variable goes away from the PHY |
| 528 | * code, the PHY driver must parse it's address from DT. This |
| 529 | * USB driver must find the PHY driver via DT phandle. |
| 530 | * -> usb_power_config() shall be moved to PHY driver |
| 531 | * With these changes in place, the ad-hoc indexing goes away and |
| 532 | * the driver is fully converted to DT probing. |
| 533 | */ |
| 534 | fdt_size_t size; |
| 535 | fdt_addr_t addr = devfdt_get_addr_size_index(dev, 0, &size); |
| 536 | |
| 537 | dev->req_seq = (addr - USB_BASE_ADDR) / size; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 542 | static int ehci_usb_probe(struct udevice *dev) |
| 543 | { |
| 544 | struct usb_platdata *plat = dev_get_platdata(dev); |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 545 | struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev); |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 546 | struct ehci_mx6_priv_data *priv = dev_get_priv(dev); |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 547 | enum usb_init_type type = plat->init_type; |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 548 | struct ehci_hccr *hccr; |
| 549 | struct ehci_hcor *hcor; |
| 550 | int ret; |
| 551 | |
| 552 | priv->ehci = ehci; |
| 553 | priv->portnr = dev->seq; |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 554 | priv->init_type = type; |
| 555 | |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 556 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 557 | ret = device_get_supply_regulator(dev, "vbus-supply", |
| 558 | &priv->vbus_supply); |
| 559 | if (ret) |
| 560 | debug("%s: No vbus supply\n", dev->name); |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 561 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 562 | ret = ehci_mx6_common_init(ehci, priv->portnr); |
| 563 | if (ret) |
| 564 | return ret; |
| 565 | |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 566 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | fcf9f9f | 2016-12-22 17:06:43 +0800 | [diff] [blame] | 567 | if (priv->vbus_supply) { |
| 568 | ret = regulator_set_enable(priv->vbus_supply, |
| 569 | (type == USB_INIT_DEVICE) ? |
| 570 | false : true); |
| 571 | if (ret) { |
| 572 | puts("Error enabling VBUS supply\n"); |
| 573 | return ret; |
| 574 | } |
| 575 | } |
Abel Vesa | 921208e | 2019-02-01 16:40:08 +0000 | [diff] [blame] | 576 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 577 | |
| 578 | if (priv->init_type == USB_INIT_HOST) { |
| 579 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 580 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
| 581 | setbits_le32(&ehci->portsc, USB_EN); |
| 582 | } |
| 583 | |
| 584 | mdelay(10); |
| 585 | |
| 586 | hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 587 | hcor = (struct ehci_hcor *)((uint32_t)hccr + |
| 588 | HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); |
| 589 | |
| 590 | return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type); |
| 591 | } |
| 592 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 593 | static const struct udevice_id mx6_usb_ids[] = { |
| 594 | { .compatible = "fsl,imx27-usb" }, |
| 595 | { } |
| 596 | }; |
| 597 | |
| 598 | U_BOOT_DRIVER(usb_mx6) = { |
| 599 | .name = "ehci_mx6", |
| 600 | .id = UCLASS_USB, |
| 601 | .of_match = mx6_usb_ids, |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame] | 602 | .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, |
Marek Vasut | 501547c | 2019-06-24 19:05:47 +0200 | [diff] [blame] | 603 | .bind = ehci_usb_bind, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 604 | .probe = ehci_usb_probe, |
Masahiro Yamada | 4052734 | 2016-09-06 22:17:34 +0900 | [diff] [blame] | 605 | .remove = ehci_deregister, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 606 | .ops = &ehci_usb_ops, |
| 607 | .platdata_auto_alloc_size = sizeof(struct usb_platdata), |
| 608 | .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data), |
| 609 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 610 | }; |
| 611 | #endif |