Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> |
| 3 | * Copyright (C) 2010 Freescale Semiconductor, Inc. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <usb.h> |
| 10 | #include <errno.h> |
Mateusz Kulikowski | 8c25c25 | 2016-01-23 11:54:32 +0100 | [diff] [blame] | 11 | #include <wait_bit.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Mateusz Kulikowski | e162c6b | 2016-03-31 23:12:23 +0200 | [diff] [blame] | 13 | #include <usb/ehci-ci.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <asm/arch/imx-regs.h> |
| 16 | #include <asm/arch/clock.h> |
Troy Kisky | af2a35f | 2012-07-19 08:18:22 +0000 | [diff] [blame] | 17 | #include <asm/imx-common/iomux-v3.h> |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame^] | 18 | #include <asm/imx-common/sys_proto.h> |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 19 | #include <dm.h> |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 20 | |
| 21 | #include "ehci.h" |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 22 | |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame^] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 25 | #define USB_OTGREGS_OFFSET 0x000 |
| 26 | #define USB_H1REGS_OFFSET 0x200 |
| 27 | #define USB_H2REGS_OFFSET 0x400 |
| 28 | #define USB_H3REGS_OFFSET 0x600 |
| 29 | #define USB_OTHERREGS_OFFSET 0x800 |
| 30 | |
| 31 | #define USB_H1_CTRL_OFFSET 0x04 |
| 32 | |
| 33 | #define USBPHY_CTRL 0x00000030 |
| 34 | #define USBPHY_CTRL_SET 0x00000034 |
| 35 | #define USBPHY_CTRL_CLR 0x00000038 |
| 36 | #define USBPHY_CTRL_TOG 0x0000003c |
| 37 | |
| 38 | #define USBPHY_PWD 0x00000000 |
| 39 | #define USBPHY_CTRL_SFTRST 0x80000000 |
| 40 | #define USBPHY_CTRL_CLKGATE 0x40000000 |
| 41 | #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000 |
| 42 | #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000 |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 43 | #define USBPHY_CTRL_OTG_ID 0x08000000 |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 44 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 45 | #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000 |
| 46 | #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000 |
| 47 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 48 | #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000 |
| 49 | #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000 |
| 50 | #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000 |
| 51 | #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040 |
| 52 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 53 | #define USBNC_OFFSET 0x200 |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame^] | 54 | #define USBNC_PHY_STATUS_OFFSET 0x23C |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 55 | #define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */ |
| 56 | #define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */ |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 57 | #define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */ |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 58 | #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */ |
| 59 | #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */ |
| 60 | |
| 61 | /* USBCMD */ |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 62 | #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */ |
| 63 | #define UCMD_RESET (1 << 1) /* controller reset */ |
| 64 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 65 | #if defined(CONFIG_MX6) |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 66 | static const unsigned phy_bases[] = { |
| 67 | USB_PHY0_BASE_ADDR, |
| 68 | USB_PHY1_BASE_ADDR, |
| 69 | }; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 70 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 71 | static void usb_internal_phy_clock_gate(int index, int on) |
| 72 | { |
| 73 | void __iomem *phy_reg; |
| 74 | |
| 75 | if (index >= ARRAY_SIZE(phy_bases)) |
| 76 | return; |
| 77 | |
| 78 | phy_reg = (void __iomem *)phy_bases[index]; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 79 | phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET; |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 80 | writel(USBPHY_CTRL_CLKGATE, phy_reg); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 83 | static void usb_power_config(int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 84 | { |
Wolfgang Grandegger | 3f29d96 | 2012-05-02 04:36:39 +0000 | [diff] [blame] | 85 | struct anatop_regs __iomem *anatop = |
| 86 | (struct anatop_regs __iomem *)ANATOP_BASE_ADDR; |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 87 | void __iomem *chrg_detect; |
| 88 | void __iomem *pll_480_ctrl_clr; |
| 89 | void __iomem *pll_480_ctrl_set; |
| 90 | |
| 91 | switch (index) { |
| 92 | case 0: |
| 93 | chrg_detect = &anatop->usb1_chrg_detect; |
| 94 | pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr; |
| 95 | pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set; |
| 96 | break; |
| 97 | case 1: |
| 98 | chrg_detect = &anatop->usb2_chrg_detect; |
| 99 | pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr; |
| 100 | pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set; |
| 101 | break; |
| 102 | default: |
| 103 | return; |
| 104 | } |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 105 | /* |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 106 | * Some phy and power's special controls |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 107 | * 1. The external charger detector needs to be disabled |
| 108 | * or the signal at DP will be poor |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 109 | * 2. The PLL's power and output to usb |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 110 | * is totally controlled by IC, so the Software only needs |
| 111 | * to enable them at initializtion. |
| 112 | */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 113 | writel(ANADIG_USB2_CHRG_DETECT_EN_B | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 114 | ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 115 | chrg_detect); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 116 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 117 | writel(ANADIG_USB2_PLL_480_CTRL_BYPASS, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 118 | pll_480_ctrl_clr); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 119 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 120 | writel(ANADIG_USB2_PLL_480_CTRL_ENABLE | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 121 | ANADIG_USB2_PLL_480_CTRL_POWER | |
| 122 | ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS, |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 123 | pll_480_ctrl_set); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 126 | /* Return 0 : host node, <>0 : device mode */ |
| 127 | static int usb_phy_enable(int index, struct usb_ehci *ehci) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 128 | { |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 129 | void __iomem *phy_reg; |
| 130 | void __iomem *phy_ctrl; |
| 131 | void __iomem *usb_cmd; |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 132 | int ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 133 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 134 | if (index >= ARRAY_SIZE(phy_bases)) |
| 135 | return 0; |
| 136 | |
| 137 | phy_reg = (void __iomem *)phy_bases[index]; |
| 138 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 139 | usb_cmd = (void __iomem *)&ehci->usbcmd; |
| 140 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 141 | /* Stop then Reset */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 142 | clrbits_le32(usb_cmd, UCMD_RUN_STOP); |
Mateusz Kulikowski | 8c25c25 | 2016-01-23 11:54:32 +0100 | [diff] [blame] | 143 | ret = wait_for_bit(__func__, usb_cmd, UCMD_RUN_STOP, false, 10000, |
| 144 | false); |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 145 | if (ret) |
| 146 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 147 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 148 | setbits_le32(usb_cmd, UCMD_RESET); |
Mateusz Kulikowski | 8c25c25 | 2016-01-23 11:54:32 +0100 | [diff] [blame] | 149 | ret = wait_for_bit(__func__, usb_cmd, UCMD_RESET, false, 10000, false); |
Adrian Alonso | f0c89d5 | 2015-08-06 15:46:03 -0500 | [diff] [blame] | 150 | if (ret) |
| 151 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 152 | |
| 153 | /* Reset USBPHY module */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 154 | setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 155 | udelay(10); |
| 156 | |
| 157 | /* Remove CLKGATE and SFTRST */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 158 | clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 159 | udelay(10); |
| 160 | |
| 161 | /* Power up the PHY */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 162 | writel(0, phy_reg + USBPHY_PWD); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 163 | /* enable FS/LS device */ |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 164 | setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 | |
| 165 | USBPHY_CTRL_ENUTMILEVEL3); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 166 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 167 | return 0; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 170 | int usb_phy_mode(int port) |
| 171 | { |
| 172 | void __iomem *phy_reg; |
| 173 | void __iomem *phy_ctrl; |
| 174 | u32 val; |
| 175 | |
| 176 | phy_reg = (void __iomem *)phy_bases[port]; |
| 177 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 178 | |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 179 | val = readl(phy_ctrl); |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 180 | |
| 181 | if (val & USBPHY_CTRL_OTG_ID) |
| 182 | return USB_INIT_DEVICE; |
| 183 | else |
| 184 | return USB_INIT_HOST; |
| 185 | } |
| 186 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 187 | /* Base address for this IP block is 0x02184800 */ |
| 188 | struct usbnc_regs { |
| 189 | u32 ctrl[4]; /* otg/host1-3 */ |
| 190 | u32 uh2_hsic_ctrl; |
| 191 | u32 uh3_hsic_ctrl; |
| 192 | u32 otg_phy_ctrl_0; |
| 193 | u32 uh1_phy_ctrl_0; |
| 194 | }; |
| 195 | #elif defined(CONFIG_MX7) |
| 196 | struct usbnc_regs { |
| 197 | u32 ctrl1; |
| 198 | u32 ctrl2; |
| 199 | u32 reserve1[10]; |
| 200 | u32 phy_cfg1; |
| 201 | u32 phy_cfg2; |
Peng Fan | 429ff44 | 2016-06-20 09:43:08 +0800 | [diff] [blame] | 202 | u32 reserve2; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 203 | u32 phy_status; |
Peng Fan | 429ff44 | 2016-06-20 09:43:08 +0800 | [diff] [blame] | 204 | u32 reserve3[4]; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 205 | u32 adp_cfg1; |
| 206 | u32 adp_cfg2; |
| 207 | u32 adp_status; |
| 208 | }; |
| 209 | |
| 210 | static void usb_power_config(int index) |
| 211 | { |
| 212 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 213 | (0x10000 * index) + USBNC_OFFSET); |
| 214 | void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2); |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 215 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 216 | |
Peng Fan | 57de41e | 2016-06-20 09:43:09 +0800 | [diff] [blame] | 217 | /* |
| 218 | * Clear the ACAENB to enable usb_otg_id detection, |
| 219 | * otherwise it is the ACA detection enabled. |
| 220 | */ |
| 221 | clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB); |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 222 | |
| 223 | /* Set power polarity to high active */ |
Stefan Agner | c448309 | 2016-07-13 00:25:38 -0700 | [diff] [blame] | 224 | #ifdef CONFIG_MXC_USB_OTG_HACTIVE |
Stefan Agner | 9a88180 | 2016-07-13 00:25:37 -0700 | [diff] [blame] | 225 | setbits_le32(ctrl, UCTRL_PWR_POL); |
Stefan Agner | c448309 | 2016-07-13 00:25:38 -0700 | [diff] [blame] | 226 | #else |
| 227 | clrbits_le32(ctrl, UCTRL_PWR_POL); |
| 228 | #endif |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int usb_phy_mode(int port) |
| 232 | { |
| 233 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 234 | (0x10000 * port) + USBNC_OFFSET); |
| 235 | void __iomem *status = (void __iomem *)(&usbnc->phy_status); |
| 236 | u32 val; |
| 237 | |
| 238 | val = readl(status); |
| 239 | |
| 240 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 241 | return USB_INIT_DEVICE; |
| 242 | else |
| 243 | return USB_INIT_HOST; |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | static void usb_oc_config(int index) |
| 248 | { |
| 249 | #if defined(CONFIG_MX6) |
| 250 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 251 | USB_OTHERREGS_OFFSET); |
| 252 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]); |
| 253 | #elif defined(CONFIG_MX7) |
| 254 | struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + |
| 255 | (0x10000 * index) + USBNC_OFFSET); |
| 256 | void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1); |
| 257 | #endif |
| 258 | |
| 259 | #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2 |
| 260 | /* mx6qarm2 seems to required a different setting*/ |
| 261 | clrbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
| 262 | #else |
| 263 | setbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
| 264 | #endif |
| 265 | |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 266 | setbits_le32(ctrl, UCTRL_OVER_CUR_DIS); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 269 | /** |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 270 | * board_usb_phy_mode - override usb phy mode |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 271 | * @port: usb host/otg port |
| 272 | * |
| 273 | * Target board specific, override usb_phy_mode. |
| 274 | * When usb-otg is used as usb host port, iomux pad usb_otg_id can be |
| 275 | * left disconnected in this case usb_phy_mode will not be able to identify |
| 276 | * the phy mode that usb port is used. |
| 277 | * Machine file overrides board_usb_phy_mode. |
| 278 | * |
| 279 | * Return: USB_INIT_DEVICE or USB_INIT_HOST |
| 280 | */ |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 281 | int __weak board_usb_phy_mode(int port) |
| 282 | { |
| 283 | return usb_phy_mode(port); |
| 284 | } |
| 285 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 286 | /** |
| 287 | * board_ehci_hcd_init - set usb vbus voltage |
| 288 | * @port: usb otg port |
| 289 | * |
| 290 | * Target board specific, setup iomux pad to setup supply vbus voltage |
| 291 | * for usb otg port. Machine board file overrides board_ehci_hcd_init |
| 292 | * |
| 293 | * Return: 0 Success |
| 294 | */ |
Benoît Thébaudeau | f22e4fa | 2012-11-13 09:58:35 +0000 | [diff] [blame] | 295 | int __weak board_ehci_hcd_init(int port) |
| 296 | { |
| 297 | return 0; |
| 298 | } |
| 299 | |
Adrian Alonso | 74f0610 | 2015-08-06 15:43:16 -0500 | [diff] [blame] | 300 | /** |
| 301 | * board_ehci_power - enables/disables usb vbus voltage |
| 302 | * @port: usb otg port |
| 303 | * @on: on/off vbus voltage |
| 304 | * |
| 305 | * Enables/disables supply vbus voltage for usb otg port. |
| 306 | * Machine board file overrides board_ehci_power |
| 307 | * |
| 308 | * Return: 0 Success |
| 309 | */ |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 310 | int __weak board_ehci_power(int port, int on) |
| 311 | { |
| 312 | return 0; |
| 313 | } |
| 314 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 315 | int ehci_mx6_common_init(struct usb_ehci *ehci, int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 316 | { |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 317 | int ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 318 | |
| 319 | enable_usboh3_clk(1); |
| 320 | mdelay(1); |
| 321 | |
| 322 | /* Do board specific initialization */ |
Stefan Agner | 79d867c | 2016-05-05 16:59:12 -0700 | [diff] [blame] | 323 | ret = board_ehci_hcd_init(index); |
| 324 | if (ret) |
| 325 | return ret; |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 326 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 327 | usb_power_config(index); |
| 328 | usb_oc_config(index); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 329 | |
| 330 | #if defined(CONFIG_MX6) |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 331 | usb_internal_phy_clock_gate(index, 1); |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 332 | usb_phy_enable(index, ehci); |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 333 | #endif |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | #ifndef CONFIG_DM_USB |
| 339 | int ehci_hcd_init(int index, enum usb_init_type init, |
| 340 | struct ehci_hccr **hccr, struct ehci_hcor **hcor) |
| 341 | { |
| 342 | enum usb_init_type type; |
| 343 | #if defined(CONFIG_MX6) |
| 344 | u32 controller_spacing = 0x200; |
| 345 | #elif defined(CONFIG_MX7) |
| 346 | u32 controller_spacing = 0x10000; |
| 347 | #endif |
| 348 | struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR + |
| 349 | (controller_spacing * index)); |
| 350 | int ret; |
| 351 | |
| 352 | if (index > 3) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | ret = ehci_mx6_common_init(ehci, index); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | |
Peng Fan | 229dbba | 2014-11-10 08:50:39 +0800 | [diff] [blame] | 359 | type = board_usb_phy_mode(index); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 360 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 361 | if (hccr && hcor) { |
| 362 | *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 363 | *hcor = (struct ehci_hcor *)((uint32_t)*hccr + |
| 364 | HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase))); |
| 365 | } |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 366 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 367 | if ((type == init) || (type == USB_INIT_DEVICE)) |
| 368 | board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1); |
| 369 | if (type != init) |
| 370 | return -ENODEV; |
| 371 | if (type == USB_INIT_DEVICE) |
| 372 | return 0; |
Adrian Alonso | 35554fc | 2015-08-06 15:43:17 -0500 | [diff] [blame] | 373 | |
Troy Kisky | d1a5286 | 2013-10-10 15:27:59 -0700 | [diff] [blame] | 374 | setbits_le32(&ehci->usbmode, CM_HOST); |
Adrian Alonso | e38ff30 | 2015-08-06 15:43:15 -0500 | [diff] [blame] | 375 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 376 | setbits_le32(&ehci->portsc, USB_EN); |
| 377 | |
| 378 | mdelay(10); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
Lucas Stach | 676ae06 | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 383 | int ehci_hcd_stop(int index) |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 384 | { |
| 385 | return 0; |
| 386 | } |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 387 | #else |
| 388 | struct ehci_mx6_priv_data { |
| 389 | struct ehci_ctrl ctrl; |
| 390 | struct usb_ehci *ehci; |
| 391 | enum usb_init_type init_type; |
| 392 | int portnr; |
| 393 | }; |
| 394 | |
| 395 | static int mx6_init_after_reset(struct ehci_ctrl *dev) |
| 396 | { |
| 397 | struct ehci_mx6_priv_data *priv = dev->priv; |
| 398 | enum usb_init_type type = priv->init_type; |
| 399 | struct usb_ehci *ehci = priv->ehci; |
| 400 | int ret; |
| 401 | |
| 402 | ret = ehci_mx6_common_init(priv->ehci, priv->portnr); |
| 403 | if (ret) |
| 404 | return ret; |
| 405 | |
| 406 | board_ehci_power(priv->portnr, (type == USB_INIT_DEVICE) ? 0 : 1); |
| 407 | |
| 408 | if (type == USB_INIT_DEVICE) |
| 409 | return 0; |
| 410 | |
| 411 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 412 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
| 413 | setbits_le32(&ehci->portsc, USB_EN); |
| 414 | |
| 415 | mdelay(10); |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static const struct ehci_ops mx6_ehci_ops = { |
| 421 | .init_after_reset = mx6_init_after_reset |
| 422 | }; |
| 423 | |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame^] | 424 | static int ehci_usb_phy_mode(struct udevice *dev) |
| 425 | { |
| 426 | struct usb_platdata *plat = dev_get_platdata(dev); |
| 427 | void *__iomem addr = (void *__iomem)dev_get_addr(dev); |
| 428 | void *__iomem phy_ctrl, *__iomem phy_status; |
| 429 | const void *blob = gd->fdt_blob; |
| 430 | int offset = dev->of_offset, phy_off; |
| 431 | u32 val; |
| 432 | |
| 433 | /* |
| 434 | * About fsl,usbphy, Refer to |
| 435 | * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt. |
| 436 | */ |
| 437 | if (is_mx6()) { |
| 438 | phy_off = fdtdec_lookup_phandle(blob, |
| 439 | offset, |
| 440 | "fsl,usbphy"); |
| 441 | if (phy_off < 0) |
| 442 | return -EINVAL; |
| 443 | |
| 444 | addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, |
| 445 | "reg"); |
| 446 | if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL); |
| 450 | val = readl(phy_ctrl); |
| 451 | |
| 452 | if (val & USBPHY_CTRL_OTG_ID) |
| 453 | plat->init_type = USB_INIT_DEVICE; |
| 454 | else |
| 455 | plat->init_type = USB_INIT_HOST; |
| 456 | } else if (is_mx7()) { |
| 457 | phy_status = (void __iomem *)(addr + |
| 458 | USBNC_PHY_STATUS_OFFSET); |
| 459 | val = readl(phy_status); |
| 460 | |
| 461 | if (val & USBNC_PHYSTATUS_ID_DIG) |
| 462 | plat->init_type = USB_INIT_DEVICE; |
| 463 | else |
| 464 | plat->init_type = USB_INIT_HOST; |
| 465 | } else { |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int ehci_usb_ofdata_to_platdata(struct udevice *dev) |
| 473 | { |
| 474 | struct usb_platdata *plat = dev_get_platdata(dev); |
| 475 | const char *mode; |
| 476 | |
| 477 | mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "dr_mode", NULL); |
| 478 | if (mode) { |
| 479 | if (strcmp(mode, "peripheral") == 0) |
| 480 | plat->init_type = USB_INIT_DEVICE; |
| 481 | else if (strcmp(mode, "host") == 0) |
| 482 | plat->init_type = USB_INIT_HOST; |
| 483 | else if (strcmp(mode, "otg") == 0) |
| 484 | return ehci_usb_phy_mode(dev); |
| 485 | else |
| 486 | return -EINVAL; |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | return ehci_usb_phy_mode(dev); |
| 492 | } |
| 493 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 494 | static int ehci_usb_probe(struct udevice *dev) |
| 495 | { |
| 496 | struct usb_platdata *plat = dev_get_platdata(dev); |
| 497 | struct usb_ehci *ehci = (struct usb_ehci *)dev_get_addr(dev); |
| 498 | struct ehci_mx6_priv_data *priv = dev_get_priv(dev); |
| 499 | struct ehci_hccr *hccr; |
| 500 | struct ehci_hcor *hcor; |
| 501 | int ret; |
| 502 | |
| 503 | priv->ehci = ehci; |
| 504 | priv->portnr = dev->seq; |
| 505 | priv->init_type = plat->init_type; |
| 506 | |
| 507 | ret = ehci_mx6_common_init(ehci, priv->portnr); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | |
| 511 | board_ehci_power(priv->portnr, (priv->init_type == USB_INIT_DEVICE) ? 0 : 1); |
| 512 | |
| 513 | if (priv->init_type == USB_INIT_HOST) { |
| 514 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 515 | writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); |
| 516 | setbits_le32(&ehci->portsc, USB_EN); |
| 517 | } |
| 518 | |
| 519 | mdelay(10); |
| 520 | |
| 521 | hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 522 | hcor = (struct ehci_hcor *)((uint32_t)hccr + |
| 523 | HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); |
| 524 | |
| 525 | return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type); |
| 526 | } |
| 527 | |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 528 | static const struct udevice_id mx6_usb_ids[] = { |
| 529 | { .compatible = "fsl,imx27-usb" }, |
| 530 | { } |
| 531 | }; |
| 532 | |
| 533 | U_BOOT_DRIVER(usb_mx6) = { |
| 534 | .name = "ehci_mx6", |
| 535 | .id = UCLASS_USB, |
| 536 | .of_match = mx6_usb_ids, |
Peng Fan | cccbddc | 2016-12-22 17:06:42 +0800 | [diff] [blame^] | 537 | .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 538 | .probe = ehci_usb_probe, |
Masahiro Yamada | 4052734 | 2016-09-06 22:17:34 +0900 | [diff] [blame] | 539 | .remove = ehci_deregister, |
Peng Fan | bb42fb4 | 2016-06-17 14:19:27 +0800 | [diff] [blame] | 540 | .ops = &ehci_usb_ops, |
| 541 | .platdata_auto_alloc_size = sizeof(struct usb_platdata), |
| 542 | .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data), |
| 543 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 544 | }; |
| 545 | #endif |