Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com> |
| 4 | * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org> |
| 5 | * (C) Copyright 2008 Armadeus Systems nc |
| 6 | * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
| 7 | * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de> |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 16 | #include <memalign.h> |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 17 | #include <miiphy.h> |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 18 | #include <net.h> |
Jeroen Hofstee | 84f64c8 | 2014-10-08 22:57:40 +0200 | [diff] [blame] | 19 | #include <netdev.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Martin Fuzzey | ad8c43c | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 23 | #include <power/regulator.h> |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 24 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 25 | #include <asm/io.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/compiler.h> |
| 28 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 29 | #include <asm/arch/clock.h> |
| 30 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 31 | #include <asm/mach-imx/sys_proto.h> |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 32 | #include <asm-generic/gpio.h> |
| 33 | |
| 34 | #include "fec_mxc.h" |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 35 | #include <eth_phy.h> |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 36 | |
| 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Marek Vasut | bc1ce15 | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 39 | /* |
| 40 | * Timeout the transfer after 5 mS. This is usually a bit more, since |
| 41 | * the code in the tightloops this timeout is used in adds some overhead. |
| 42 | */ |
| 43 | #define FEC_XFER_TIMEOUT 5000 |
| 44 | |
Fabio Estevam | db5b7f5 | 2014-08-25 13:34:16 -0300 | [diff] [blame] | 45 | /* |
| 46 | * The standard 32-byte DMA alignment does not work on mx6solox, which requires |
| 47 | * 64-byte alignment in the DMA RX FEC buffer. |
| 48 | * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also |
| 49 | * satisfies the alignment on other SoCs (32-bytes) |
| 50 | */ |
| 51 | #define FEC_DMA_RX_MINALIGN 64 |
| 52 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 53 | #ifndef CONFIG_MII |
| 54 | #error "CONFIG_MII has to be defined!" |
| 55 | #endif |
| 56 | |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 57 | #ifndef CONFIG_FEC_XCV_TYPE |
| 58 | #define CONFIG_FEC_XCV_TYPE MII100 |
Marek Vasut | 392b850 | 2011-09-11 18:05:33 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Marek Vasut | be7e87e | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 61 | /* |
| 62 | * The i.MX28 operates with packets in big endian. We need to swap them before |
| 63 | * sending and after receiving. |
| 64 | */ |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_MX28 |
| 66 | #define CONFIG_FEC_MXC_SWAP_PACKET |
| 67 | #endif |
| 68 | |
| 69 | #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd)) |
| 70 | |
| 71 | /* Check various alignment issues at compile time */ |
| 72 | #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0)) |
| 73 | #error "ARCH_DMA_MINALIGN must be multiple of 16!" |
| 74 | #endif |
| 75 | |
| 76 | #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \ |
| 77 | (PKTALIGN % ARCH_DMA_MINALIGN != 0)) |
| 78 | #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!" |
Marek Vasut | be7e87e | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 81 | #undef DEBUG |
| 82 | |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 83 | #ifdef CONFIG_FEC_MXC_SWAP_PACKET |
Marek Vasut | be7e87e | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 84 | static void swap_packet(uint32_t *packet, int length) |
| 85 | { |
| 86 | int i; |
| 87 | |
| 88 | for (i = 0; i < DIV_ROUND_UP(length, 4); i++) |
| 89 | packet[i] = __swab32(packet[i]); |
| 90 | } |
| 91 | #endif |
| 92 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 93 | /* MII-interface related functions */ |
| 94 | static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr, |
| 95 | uint8_t regaddr) |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 96 | { |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 97 | uint32_t reg; /* convenient holder for the PHY register */ |
| 98 | uint32_t phy; /* convenient holder for the PHY */ |
| 99 | uint32_t start; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 100 | int val; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * reading from any PHY's register is done by properly |
| 104 | * programming the FEC's MII data register. |
| 105 | */ |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 106 | writel(FEC_IEVENT_MII, ð->ievent); |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 107 | reg = regaddr << FEC_MII_DATA_RA_SHIFT; |
| 108 | phy = phyaddr << FEC_MII_DATA_PA_SHIFT; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 109 | |
| 110 | writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 111 | phy | reg, ð->mii_data); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 112 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 113 | /* wait for the related interrupt */ |
Graeme Russ | a60d1e5 | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 114 | start = get_timer(0); |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 115 | while (!(readl(ð->ievent) & FEC_IEVENT_MII)) { |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 116 | if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) { |
| 117 | printf("Read MDIO failed...\n"); |
| 118 | return -1; |
| 119 | } |
| 120 | } |
| 121 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 122 | /* clear mii interrupt bit */ |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 123 | writel(FEC_IEVENT_MII, ð->ievent); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 124 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 125 | /* it's now safe to read the PHY's register */ |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 126 | val = (unsigned short)readl(ð->mii_data); |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 127 | debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr, |
| 128 | regaddr, val); |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 129 | return val; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 130 | } |
| 131 | |
Peng Fan | 673f659 | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 132 | #ifndef imx_get_fecclk |
| 133 | u32 __weak imx_get_fecclk(void) |
| 134 | { |
| 135 | return 0; |
| 136 | } |
| 137 | #endif |
| 138 | |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 139 | static int fec_get_clk_rate(void *udev, int idx) |
| 140 | { |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 141 | struct fec_priv *fec; |
| 142 | struct udevice *dev; |
| 143 | int ret; |
| 144 | |
Peng Fan | 673f659 | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 145 | if (IS_ENABLED(CONFIG_IMX8) || |
| 146 | CONFIG_IS_ENABLED(CLK_CCF)) { |
| 147 | dev = udev; |
| 148 | if (!dev) { |
Tim Harvey | b247fa7 | 2021-06-30 16:50:03 -0700 | [diff] [blame] | 149 | ret = uclass_get_device_by_seq(UCLASS_ETH, idx, &dev); |
Peng Fan | 673f659 | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 150 | if (ret < 0) { |
| 151 | debug("Can't get FEC udev: %d\n", ret); |
| 152 | return ret; |
| 153 | } |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 154 | } |
Peng Fan | 673f659 | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 155 | |
| 156 | fec = dev_get_priv(dev); |
| 157 | if (fec) |
| 158 | return fec->clk_rate; |
| 159 | |
| 160 | return -EINVAL; |
| 161 | } else { |
| 162 | return imx_get_fecclk(); |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 163 | } |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Troy Kisky | 575c5cc | 2012-10-22 16:40:41 +0000 | [diff] [blame] | 166 | static void fec_mii_setspeed(struct ethernet_regs *eth) |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 167 | { |
| 168 | /* |
| 169 | * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock |
| 170 | * and do not drop the Preamble. |
Måns Rullgård | 843a3e5 | 2015-12-08 15:38:45 +0000 | [diff] [blame] | 171 | * |
| 172 | * The i.MX28 and i.MX6 types have another field in the MSCR (aka |
| 173 | * MII_SPEED) register that defines the MDIO output hold time. Earlier |
| 174 | * versions are RAZ there, so just ignore the difference and write the |
| 175 | * register always. |
| 176 | * The minimal hold time according to IEE802.3 (clause 22) is 10 ns. |
| 177 | * HOLDTIME + 1 is the number of clk cycles the fec is holding the |
| 178 | * output. |
| 179 | * The HOLDTIME bitfield takes values between 0 and 7 (inclusive). |
| 180 | * Given that ceil(clkrate / 5000000) <= 64, the calculation for |
| 181 | * holdtime cannot result in a value greater than 3. |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 182 | */ |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 183 | u32 pclk; |
| 184 | u32 speed; |
| 185 | u32 hold; |
| 186 | int ret; |
| 187 | |
| 188 | ret = fec_get_clk_rate(NULL, 0); |
| 189 | if (ret < 0) { |
| 190 | printf("Can't find FEC0 clk rate: %d\n", ret); |
| 191 | return; |
| 192 | } |
| 193 | pclk = ret; |
| 194 | speed = DIV_ROUND_UP(pclk, 5000000); |
| 195 | hold = DIV_ROUND_UP(pclk, 100000000) - 1; |
| 196 | |
Markus Niebel | 6ba45cc | 2014-02-05 10:54:11 +0100 | [diff] [blame] | 197 | #ifdef FEC_QUIRK_ENET_MAC |
| 198 | speed--; |
| 199 | #endif |
Måns Rullgård | 843a3e5 | 2015-12-08 15:38:45 +0000 | [diff] [blame] | 200 | writel(speed << 1 | hold << 8, ð->mii_speed); |
Troy Kisky | 575c5cc | 2012-10-22 16:40:41 +0000 | [diff] [blame] | 201 | debug("%s: mii_speed %08x\n", __func__, readl(ð->mii_speed)); |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 202 | } |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 203 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 204 | static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr, |
| 205 | uint8_t regaddr, uint16_t data) |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 206 | { |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 207 | uint32_t reg; /* convenient holder for the PHY register */ |
| 208 | uint32_t phy; /* convenient holder for the PHY */ |
| 209 | uint32_t start; |
| 210 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 211 | reg = regaddr << FEC_MII_DATA_RA_SHIFT; |
| 212 | phy = phyaddr << FEC_MII_DATA_PA_SHIFT; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 213 | |
| 214 | writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR | |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 215 | FEC_MII_DATA_TA | phy | reg | data, ð->mii_data); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 216 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 217 | /* wait for the MII interrupt */ |
Graeme Russ | a60d1e5 | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 218 | start = get_timer(0); |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 219 | while (!(readl(ð->ievent) & FEC_IEVENT_MII)) { |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 220 | if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) { |
| 221 | printf("Write MDIO failed...\n"); |
| 222 | return -1; |
| 223 | } |
| 224 | } |
| 225 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 226 | /* clear MII interrupt bit */ |
Marek Vasut | d133b88 | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 227 | writel(FEC_IEVENT_MII, ð->ievent); |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 228 | debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr, |
| 229 | regaddr, data); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 234 | static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr, |
| 235 | int regaddr) |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 236 | { |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 237 | return fec_mdio_read(bus->priv, phyaddr, regaddr); |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 240 | static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr, |
| 241 | int regaddr, u16 data) |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 242 | { |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 243 | return fec_mdio_write(bus->priv, phyaddr, regaddr, data); |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | #ifndef CONFIG_PHYLIB |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 247 | static int miiphy_restart_aneg(struct eth_device *dev) |
| 248 | { |
Stefano Babic | b774fe9 | 2012-02-22 00:24:35 +0000 | [diff] [blame] | 249 | int ret = 0; |
| 250 | #if !defined(CONFIG_FEC_MXC_NO_ANEG) |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 251 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 252 | struct ethernet_regs *eth = fec->bus->priv; |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 253 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 254 | /* |
| 255 | * Wake up from sleep if necessary |
| 256 | * Reset PHY, then delay 300ns |
| 257 | */ |
John Rigby | cb17b92 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 258 | #ifdef CONFIG_MX27 |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 259 | fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF); |
John Rigby | cb17b92 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 260 | #endif |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 261 | fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 262 | udelay(1000); |
| 263 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 264 | /* Set the auto-negotiation advertisement register bits */ |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 265 | fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 266 | LPA_100FULL | LPA_100HALF | LPA_10FULL | |
| 267 | LPA_10HALF | PHY_ANLPAR_PSB_802_3); |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 268 | fec_mdio_write(eth, fec->phy_id, MII_BMCR, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 269 | BMCR_ANENABLE | BMCR_ANRESTART); |
Marek Vasut | 2e5f442 | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 270 | |
| 271 | if (fec->mii_postcall) |
| 272 | ret = fec->mii_postcall(fec->phy_id); |
| 273 | |
Stefano Babic | b774fe9 | 2012-02-22 00:24:35 +0000 | [diff] [blame] | 274 | #endif |
Marek Vasut | 2e5f442 | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 275 | return ret; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 276 | } |
| 277 | |
Hannes Schmelzer | 0750701 | 2016-06-22 12:07:14 +0200 | [diff] [blame] | 278 | #ifndef CONFIG_FEC_FIXED_SPEED |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 279 | static int miiphy_wait_aneg(struct eth_device *dev) |
| 280 | { |
| 281 | uint32_t start; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 282 | int status; |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 283 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 284 | struct ethernet_regs *eth = fec->bus->priv; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 285 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 286 | /* Wait for AN completion */ |
Graeme Russ | a60d1e5 | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 287 | start = get_timer(0); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 288 | do { |
| 289 | if (get_timer(start) > (CONFIG_SYS_HZ * 5)) { |
| 290 | printf("%s: Autonegotiation timeout\n", dev->name); |
| 291 | return -1; |
| 292 | } |
| 293 | |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 294 | status = fec_mdio_read(eth, fec->phy_id, MII_BMSR); |
| 295 | if (status < 0) { |
| 296 | printf("%s: Autonegotiation failed. status: %d\n", |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 297 | dev->name, status); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 298 | return -1; |
| 299 | } |
Mike Frysinger | 8ef583a | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 300 | } while (!(status & BMSR_LSTATUS)); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 301 | |
| 302 | return 0; |
| 303 | } |
Hannes Schmelzer | 0750701 | 2016-06-22 12:07:14 +0200 | [diff] [blame] | 304 | #endif /* CONFIG_FEC_FIXED_SPEED */ |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 305 | #endif |
| 306 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 307 | static int fec_rx_task_enable(struct fec_priv *fec) |
| 308 | { |
Marek Vasut | c0b5a3b | 2012-08-29 03:49:51 +0000 | [diff] [blame] | 309 | writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int fec_rx_task_disable(struct fec_priv *fec) |
| 314 | { |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int fec_tx_task_enable(struct fec_priv *fec) |
| 319 | { |
Marek Vasut | c0b5a3b | 2012-08-29 03:49:51 +0000 | [diff] [blame] | 320 | writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int fec_tx_task_disable(struct fec_priv *fec) |
| 325 | { |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Initialize receive task's buffer descriptors |
| 331 | * @param[in] fec all we know about the device yet |
| 332 | * @param[in] count receive buffer count to be allocated |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 333 | * @param[in] dsize desired size of each receive buffer |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 334 | * @return 0 on success |
| 335 | * |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 336 | * Init all RX descriptors to default values. |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 337 | */ |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 338 | static void fec_rbd_init(struct fec_priv *fec, int count, int dsize) |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 339 | { |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 340 | uint32_t size; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 341 | ulong data; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 342 | int i; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 343 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 344 | /* |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 345 | * Reload the RX descriptors with default values and wipe |
| 346 | * the RX buffers. |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 347 | */ |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 348 | size = roundup(dsize, ARCH_DMA_MINALIGN); |
| 349 | for (i = 0; i < count; i++) { |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 350 | data = fec->rbd_base[i].data_pointer; |
| 351 | memset((void *)data, 0, dsize); |
| 352 | flush_dcache_range(data, data + size); |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 353 | |
| 354 | fec->rbd_base[i].status = FEC_RBD_EMPTY; |
| 355 | fec->rbd_base[i].data_length = 0; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* Mark the last RBD to close the ring. */ |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 359 | fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 360 | fec->rbd_index = 0; |
| 361 | |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 362 | flush_dcache_range((ulong)fec->rbd_base, |
| 363 | (ulong)fec->rbd_base + size); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Initialize transmit task's buffer descriptors |
| 368 | * @param[in] fec all we know about the device yet |
| 369 | * |
| 370 | * Transmit buffers are created externally. We only have to init the BDs here.\n |
| 371 | * Note: There is a race condition in the hardware. When only one BD is in |
| 372 | * use it must be marked with the WRAP bit to use it for every transmitt. |
| 373 | * This bit in combination with the READY bit results into double transmit |
| 374 | * of each data buffer. It seems the state machine checks READY earlier then |
| 375 | * resetting it after the first transfer. |
| 376 | * Using two BDs solves this issue. |
| 377 | */ |
| 378 | static void fec_tbd_init(struct fec_priv *fec) |
| 379 | { |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 380 | ulong addr = (ulong)fec->tbd_base; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 381 | unsigned size = roundup(2 * sizeof(struct fec_bd), |
| 382 | ARCH_DMA_MINALIGN); |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 383 | |
| 384 | memset(fec->tbd_base, 0, size); |
| 385 | fec->tbd_base[0].status = 0; |
| 386 | fec->tbd_base[1].status = FEC_TBD_WRAP; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 387 | fec->tbd_index = 0; |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 388 | flush_dcache_range(addr, addr + size); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Mark the given read buffer descriptor as free |
| 393 | * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0 |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 394 | * @param[in] prbd buffer descriptor to mark free again |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 395 | */ |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 396 | static void fec_rbd_clean(int last, struct fec_bd *prbd) |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 397 | { |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 398 | unsigned short flags = FEC_RBD_EMPTY; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 399 | if (last) |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 400 | flags |= FEC_RBD_WRAP; |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 401 | writew(flags, &prbd->status); |
| 402 | writew(0, &prbd->data_length); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 403 | } |
| 404 | |
Jagan Teki | f54183e | 2016-12-06 00:00:48 +0100 | [diff] [blame] | 405 | static int fec_get_hwaddr(int dev_id, unsigned char *mac) |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 406 | { |
Fabio Estevam | be252b6 | 2011-12-20 05:46:31 +0000 | [diff] [blame] | 407 | imx_get_mac_from_fuse(dev_id, mac); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 408 | return !is_valid_ethaddr(mac); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 409 | } |
| 410 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 411 | #ifdef CONFIG_DM_ETH |
| 412 | static int fecmxc_set_hwaddr(struct udevice *dev) |
| 413 | #else |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 414 | static int fec_set_hwaddr(struct eth_device *dev) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 415 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 416 | { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 417 | #ifdef CONFIG_DM_ETH |
| 418 | struct fec_priv *fec = dev_get_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 419 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 420 | uchar *mac = pdata->enetaddr; |
| 421 | #else |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 422 | uchar *mac = dev->enetaddr; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 423 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 424 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 425 | |
| 426 | writel(0, &fec->eth->iaddr1); |
| 427 | writel(0, &fec->eth->iaddr2); |
| 428 | writel(0, &fec->eth->gaddr1); |
| 429 | writel(0, &fec->eth->gaddr2); |
| 430 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 431 | /* Set physical address */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 432 | writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3], |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 433 | &fec->eth->paddr1); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 434 | writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2); |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 439 | /* Do initial configuration of the FEC registers */ |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 440 | static void fec_reg_setup(struct fec_priv *fec) |
| 441 | { |
| 442 | uint32_t rcntrl; |
| 443 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 444 | /* Set interrupt mask register */ |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 445 | writel(0x00000000, &fec->eth->imask); |
| 446 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 447 | /* Clear FEC-Lite interrupt event register(IEVENT) */ |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 448 | writel(0xffffffff, &fec->eth->ievent); |
| 449 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 450 | /* Set FEC-Lite receive control register(R_CNTRL): */ |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 451 | |
| 452 | /* Start with frame length = 1518, common for all modes. */ |
| 453 | rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT; |
benoit.thebaudeau@advans | 9d2d924 | 2012-07-19 02:12:46 +0000 | [diff] [blame] | 454 | if (fec->xcv_type != SEVENWIRE) /* xMII modes */ |
| 455 | rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE; |
| 456 | if (fec->xcv_type == RGMII) |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 457 | rcntrl |= FEC_RCNTRL_RGMII; |
| 458 | else if (fec->xcv_type == RMII) |
| 459 | rcntrl |= FEC_RCNTRL_RMII; |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 460 | |
Tim Harvey | 87550a8 | 2021-06-30 16:50:06 -0700 | [diff] [blame] | 461 | if (fec->promisc) |
| 462 | rcntrl |= 0x8; |
| 463 | |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 464 | writel(rcntrl, &fec->eth->r_cntrl); |
| 465 | } |
| 466 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 467 | /** |
| 468 | * Start the FEC engine |
| 469 | * @param[in] dev Our device to handle |
| 470 | */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 471 | #ifdef CONFIG_DM_ETH |
| 472 | static int fec_open(struct udevice *dev) |
| 473 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 474 | static int fec_open(struct eth_device *edev) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 475 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 476 | { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 477 | #ifdef CONFIG_DM_ETH |
| 478 | struct fec_priv *fec = dev_get_priv(dev); |
| 479 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 480 | struct fec_priv *fec = (struct fec_priv *)edev->priv; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 481 | #endif |
Troy Kisky | 28774cb | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 482 | int speed; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 483 | ulong addr, size; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 484 | int i; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 485 | |
| 486 | debug("fec_open: fec_open(dev)\n"); |
| 487 | /* full-duplex, heartbeat disabled */ |
| 488 | writel(1 << 2, &fec->eth->x_cntrl); |
| 489 | fec->rbd_index = 0; |
| 490 | |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 491 | /* Invalidate all descriptors */ |
| 492 | for (i = 0; i < FEC_RBD_NUM - 1; i++) |
| 493 | fec_rbd_clean(0, &fec->rbd_base[i]); |
| 494 | fec_rbd_clean(1, &fec->rbd_base[i]); |
| 495 | |
| 496 | /* Flush the descriptors into RAM */ |
| 497 | size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), |
| 498 | ARCH_DMA_MINALIGN); |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 499 | addr = (ulong)fec->rbd_base; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 500 | flush_dcache_range(addr, addr + size); |
| 501 | |
Troy Kisky | 28774cb | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 502 | #ifdef FEC_QUIRK_ENET_MAC |
Jason Liu | 2ef2b95 | 2011-12-16 05:17:07 +0000 | [diff] [blame] | 503 | /* Enable ENET HW endian SWAP */ |
| 504 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 505 | &fec->eth->ecntrl); |
Jason Liu | 2ef2b95 | 2011-12-16 05:17:07 +0000 | [diff] [blame] | 506 | /* Enable ENET store and forward mode */ |
| 507 | writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 508 | &fec->eth->x_wmrk); |
Jason Liu | 2ef2b95 | 2011-12-16 05:17:07 +0000 | [diff] [blame] | 509 | #endif |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 510 | /* Enable FEC-Lite controller */ |
John Rigby | cb17b92 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 511 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 512 | &fec->eth->ecntrl); |
| 513 | |
Philippe Schenker | a1a34fa | 2020-03-11 11:52:58 +0100 | [diff] [blame] | 514 | #ifdef FEC_ENET_ENABLE_TXC_DELAY |
| 515 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_TXC_DLY, |
| 516 | &fec->eth->ecntrl); |
| 517 | #endif |
| 518 | |
| 519 | #ifdef FEC_ENET_ENABLE_RXC_DELAY |
| 520 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RXC_DLY, |
| 521 | &fec->eth->ecntrl); |
| 522 | #endif |
| 523 | |
Tom Rini | 8ba5960 | 2021-09-09 07:54:50 -0400 | [diff] [blame] | 524 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6SL) |
John Rigby | 740d6ae | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 525 | udelay(100); |
John Rigby | 740d6ae | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 526 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 527 | /* setup the MII gasket for RMII mode */ |
John Rigby | 740d6ae | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 528 | /* disable the gasket */ |
| 529 | writew(0, &fec->eth->miigsk_enr); |
| 530 | |
| 531 | /* wait for the gasket to be disabled */ |
| 532 | while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) |
| 533 | udelay(2); |
| 534 | |
| 535 | /* configure gasket for RMII, 50 MHz, no loopback, and no echo */ |
| 536 | writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr); |
| 537 | |
| 538 | /* re-enable the gasket */ |
| 539 | writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr); |
| 540 | |
| 541 | /* wait until MII gasket is ready */ |
| 542 | int max_loops = 10; |
| 543 | while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) { |
| 544 | if (--max_loops <= 0) { |
| 545 | printf("WAIT for MII Gasket ready timed out\n"); |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 550 | |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 551 | #ifdef CONFIG_PHYLIB |
Troy Kisky | 4dc27ee | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 552 | { |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 553 | /* Start up the PHY */ |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 554 | int ret = phy_startup(fec->phydev); |
| 555 | |
| 556 | if (ret) { |
| 557 | printf("Could not initialize PHY %s\n", |
| 558 | fec->phydev->dev->name); |
| 559 | return ret; |
| 560 | } |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 561 | speed = fec->phydev->speed; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 562 | } |
Hannes Schmelzer | 0750701 | 2016-06-22 12:07:14 +0200 | [diff] [blame] | 563 | #elif CONFIG_FEC_FIXED_SPEED |
| 564 | speed = CONFIG_FEC_FIXED_SPEED; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 565 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 566 | miiphy_wait_aneg(edev); |
Troy Kisky | 28774cb | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 567 | speed = miiphy_speed(edev->name, fec->phy_id); |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 568 | miiphy_duplex(edev->name, fec->phy_id); |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 569 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 570 | |
Troy Kisky | 28774cb | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 571 | #ifdef FEC_QUIRK_ENET_MAC |
| 572 | { |
| 573 | u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED; |
Alison Wang | bcb6e90 | 2013-05-27 22:55:43 +0000 | [diff] [blame] | 574 | u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T; |
Troy Kisky | 28774cb | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 575 | if (speed == _1000BASET) |
| 576 | ecr |= FEC_ECNTRL_SPEED; |
| 577 | else if (speed != _100BASET) |
| 578 | rcr |= FEC_RCNTRL_RMII_10T; |
| 579 | writel(ecr, &fec->eth->ecntrl); |
| 580 | writel(rcr, &fec->eth->r_cntrl); |
| 581 | } |
| 582 | #endif |
| 583 | debug("%s:Speed=%i\n", __func__, speed); |
| 584 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 585 | /* Enable SmartDMA receive task */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 586 | fec_rx_task_enable(fec); |
| 587 | |
| 588 | udelay(100000); |
| 589 | return 0; |
| 590 | } |
| 591 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 592 | #ifdef CONFIG_DM_ETH |
| 593 | static int fecmxc_init(struct udevice *dev) |
| 594 | #else |
Masahiro Yamada | bb5a2cf | 2020-06-26 15:13:34 +0900 | [diff] [blame] | 595 | static int fec_init(struct eth_device *dev, struct bd_info *bd) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 596 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 597 | { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 598 | #ifdef CONFIG_DM_ETH |
| 599 | struct fec_priv *fec = dev_get_priv(dev); |
| 600 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 601 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 602 | #endif |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 603 | u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop; |
| 604 | u8 *i; |
| 605 | ulong addr; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 606 | |
John Rigby | e9319f1 | 2010-10-13 14:31:08 -0600 | [diff] [blame] | 607 | /* Initialize MAC address */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 608 | #ifdef CONFIG_DM_ETH |
| 609 | fecmxc_set_hwaddr(dev); |
| 610 | #else |
John Rigby | e9319f1 | 2010-10-13 14:31:08 -0600 | [diff] [blame] | 611 | fec_set_hwaddr(dev); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 612 | #endif |
John Rigby | e9319f1 | 2010-10-13 14:31:08 -0600 | [diff] [blame] | 613 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 614 | /* Setup transmit descriptors, there are two in total. */ |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 615 | fec_tbd_init(fec); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 616 | |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 617 | /* Setup receive descriptors. */ |
| 618 | fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 619 | |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 620 | fec_reg_setup(fec); |
Marek Vasut | 9eb3770 | 2011-09-11 18:05:31 +0000 | [diff] [blame] | 621 | |
benoit.thebaudeau@advans | f41471e | 2012-07-19 02:12:58 +0000 | [diff] [blame] | 622 | if (fec->xcv_type != SEVENWIRE) |
Troy Kisky | 575c5cc | 2012-10-22 16:40:41 +0000 | [diff] [blame] | 623 | fec_mii_setspeed(fec->bus->priv); |
Marek Vasut | 9eb3770 | 2011-09-11 18:05:31 +0000 | [diff] [blame] | 624 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 625 | /* Set Opcode/Pause Duration Register */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 626 | writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */ |
| 627 | writel(0x2, &fec->eth->x_wmrk); |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 628 | |
| 629 | /* Set multicast address filter */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 630 | writel(0x00000000, &fec->eth->gaddr1); |
| 631 | writel(0x00000000, &fec->eth->gaddr2); |
| 632 | |
Peng Fan | 238a53c | 2018-01-10 13:20:43 +0800 | [diff] [blame] | 633 | /* Do not access reserved register */ |
Peng Fan | 06918de | 2021-08-07 16:00:42 +0800 | [diff] [blame] | 634 | if (!is_mx6ul() && !is_mx6ull() && !is_imx8() && !is_imx8m() && !is_imx8ulp()) { |
Peng Fan | fbecbaa | 2015-08-12 17:46:51 +0800 | [diff] [blame] | 635 | /* clear MIB RAM */ |
| 636 | for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4) |
| 637 | writel(0, i); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 638 | |
Peng Fan | fbecbaa | 2015-08-12 17:46:51 +0800 | [diff] [blame] | 639 | /* FIFO receive start register */ |
| 640 | writel(0x520, &fec->eth->r_fstart); |
| 641 | } |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 642 | |
| 643 | /* size and address of each buffer */ |
| 644 | writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr); |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 645 | |
| 646 | addr = (ulong)fec->tbd_base; |
| 647 | writel((uint32_t)addr, &fec->eth->etdsr); |
| 648 | |
| 649 | addr = (ulong)fec->rbd_base; |
| 650 | writel((uint32_t)addr, &fec->eth->erdsr); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 651 | |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 652 | #ifndef CONFIG_PHYLIB |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 653 | if (fec->xcv_type != SEVENWIRE) |
| 654 | miiphy_restart_aneg(dev); |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 655 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 656 | fec_open(dev); |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Halt the FEC engine |
| 662 | * @param[in] dev Our device to handle |
| 663 | */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 664 | #ifdef CONFIG_DM_ETH |
| 665 | static void fecmxc_halt(struct udevice *dev) |
| 666 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 667 | static void fec_halt(struct eth_device *dev) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 668 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 669 | { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 670 | #ifdef CONFIG_DM_ETH |
| 671 | struct fec_priv *fec = dev_get_priv(dev); |
| 672 | #else |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 673 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 674 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 675 | int counter = 0xffff; |
| 676 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 677 | /* issue graceful stop command to the FEC transmitter if necessary */ |
John Rigby | cb17b92 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 678 | writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl), |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 679 | &fec->eth->x_cntrl); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 680 | |
| 681 | debug("eth_halt: wait for stop regs\n"); |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 682 | /* wait for graceful stop to register */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 683 | while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA))) |
John Rigby | cb17b92 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 684 | udelay(1); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 685 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 686 | /* Disable SmartDMA tasks */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 687 | fec_tx_task_disable(fec); |
| 688 | fec_rx_task_disable(fec); |
| 689 | |
| 690 | /* |
| 691 | * Disable the Ethernet Controller |
| 692 | * Note: this will also reset the BD index counter! |
| 693 | */ |
John Rigby | 740d6ae | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 694 | writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 695 | &fec->eth->ecntrl); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 696 | fec->rbd_index = 0; |
| 697 | fec->tbd_index = 0; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 698 | debug("eth_halt: done\n"); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Transmit one frame |
| 703 | * @param[in] dev Our ethernet device to handle |
| 704 | * @param[in] packet Pointer to the data to be transmitted |
| 705 | * @param[in] length Data count in bytes |
| 706 | * @return 0 on success |
| 707 | */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 708 | #ifdef CONFIG_DM_ETH |
| 709 | static int fecmxc_send(struct udevice *dev, void *packet, int length) |
| 710 | #else |
Joe Hershberger | 442dac4 | 2012-05-21 14:45:27 +0000 | [diff] [blame] | 711 | static int fec_send(struct eth_device *dev, void *packet, int length) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 712 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 713 | { |
| 714 | unsigned int status; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 715 | u32 size; |
| 716 | ulong addr, end; |
Marek Vasut | bc1ce15 | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 717 | int timeout = FEC_XFER_TIMEOUT; |
| 718 | int ret = 0; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 719 | |
| 720 | /* |
| 721 | * This routine transmits one frame. This routine only accepts |
| 722 | * 6-byte Ethernet addresses. |
| 723 | */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 724 | #ifdef CONFIG_DM_ETH |
| 725 | struct fec_priv *fec = dev_get_priv(dev); |
| 726 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 727 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 728 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 729 | |
| 730 | /* |
| 731 | * Check for valid length of data. |
| 732 | */ |
| 733 | if ((length > 1500) || (length <= 0)) { |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 734 | printf("Payload (%d) too large\n", length); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 735 | return -1; |
| 736 | } |
| 737 | |
| 738 | /* |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 739 | * Setup the transmit buffer. We are always using the first buffer for |
| 740 | * transmission, the second will be empty and only used to stop the DMA |
| 741 | * engine. We also flush the packet to RAM here to avoid cache trouble. |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 742 | */ |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 743 | #ifdef CONFIG_FEC_MXC_SWAP_PACKET |
Marek Vasut | be7e87e | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 744 | swap_packet((uint32_t *)packet, length); |
| 745 | #endif |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 746 | |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 747 | addr = (ulong)packet; |
Marek Vasut | efe24d2 | 2012-08-26 10:19:21 +0000 | [diff] [blame] | 748 | end = roundup(addr + length, ARCH_DMA_MINALIGN); |
| 749 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 750 | flush_dcache_range(addr, end); |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 751 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 752 | writew(length, &fec->tbd_base[fec->tbd_index].data_length); |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 753 | writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer); |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 754 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 755 | /* |
| 756 | * update BD's status now |
| 757 | * This block: |
| 758 | * - is always the last in a chain (means no chain) |
| 759 | * - should transmitt the CRC |
| 760 | * - might be the last BD in the list, so the address counter should |
| 761 | * wrap (-> keep the WRAP flag) |
| 762 | */ |
| 763 | status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP; |
| 764 | status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY; |
| 765 | writew(status, &fec->tbd_base[fec->tbd_index].status); |
| 766 | |
| 767 | /* |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 768 | * Flush data cache. This code flushes both TX descriptors to RAM. |
| 769 | * After this code, the descriptors will be safely in RAM and we |
| 770 | * can start DMA. |
| 771 | */ |
| 772 | size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 773 | addr = (ulong)fec->tbd_base; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 774 | flush_dcache_range(addr, addr + size); |
| 775 | |
| 776 | /* |
Marek Vasut | ab94cd4 | 2013-07-12 01:03:04 +0200 | [diff] [blame] | 777 | * Below we read the DMA descriptor's last four bytes back from the |
| 778 | * DRAM. This is important in order to make sure that all WRITE |
| 779 | * operations on the bus that were triggered by previous cache FLUSH |
| 780 | * have completed. |
| 781 | * |
| 782 | * Otherwise, on MX28, it is possible to observe a corruption of the |
| 783 | * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM |
| 784 | * for the bus structure of MX28. The scenario is as follows: |
| 785 | * |
| 786 | * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going |
| 787 | * to DRAM due to flush_dcache_range() |
| 788 | * 2) ARM core writes the FEC registers via AHB_ARB2 |
| 789 | * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3 |
| 790 | * |
| 791 | * Note that 2) does sometimes finish before 1) due to reordering of |
| 792 | * WRITE accesses on the AHB bus, therefore triggering 3) before the |
| 793 | * DMA descriptor is fully written into DRAM. This results in occasional |
| 794 | * corruption of the DMA descriptor. |
| 795 | */ |
| 796 | readl(addr + size - 4); |
| 797 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 798 | /* Enable SmartDMA transmit task */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 799 | fec_tx_task_enable(fec); |
| 800 | |
| 801 | /* |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 802 | * Wait until frame is sent. On each turn of the wait cycle, we must |
| 803 | * invalidate data cache to see what's really in RAM. Also, we need |
| 804 | * barrier here. |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 805 | */ |
Marek Vasut | 6744909 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 806 | while (--timeout) { |
Marek Vasut | c0b5a3b | 2012-08-29 03:49:51 +0000 | [diff] [blame] | 807 | if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR)) |
Marek Vasut | bc1ce15 | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 808 | break; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 809 | } |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 810 | |
Fabio Estevam | f599288 | 2014-08-25 13:34:17 -0300 | [diff] [blame] | 811 | if (!timeout) { |
| 812 | ret = -EINVAL; |
| 813 | goto out; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * The TDAR bit is cleared when the descriptors are all out from TX |
| 818 | * but on mx6solox we noticed that the READY bit is still not cleared |
| 819 | * right after TDAR. |
| 820 | * These are two distinct signals, and in IC simulation, we found that |
| 821 | * TDAR always gets cleared prior than the READY bit of last BD becomes |
| 822 | * cleared. |
| 823 | * In mx6solox, we use a later version of FEC IP. It looks like that |
| 824 | * this intrinsic behaviour of TDAR bit has changed in this newer FEC |
| 825 | * version. |
| 826 | * |
| 827 | * Fix this by polling the READY bit of BD after the TDAR polling, |
| 828 | * which covers the mx6solox case and does not harm the other SoCs. |
| 829 | */ |
| 830 | timeout = FEC_XFER_TIMEOUT; |
| 831 | while (--timeout) { |
| 832 | invalidate_dcache_range(addr, addr + size); |
| 833 | if (!(readw(&fec->tbd_base[fec->tbd_index].status) & |
| 834 | FEC_TBD_READY)) |
| 835 | break; |
| 836 | } |
| 837 | |
Marek Vasut | 6744909 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 838 | if (!timeout) |
| 839 | ret = -EINVAL; |
| 840 | |
Fabio Estevam | f599288 | 2014-08-25 13:34:17 -0300 | [diff] [blame] | 841 | out: |
Marek Vasut | 6744909 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 842 | debug("fec_send: status 0x%x index %d ret %i\n", |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 843 | readw(&fec->tbd_base[fec->tbd_index].status), |
| 844 | fec->tbd_index, ret); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 845 | /* for next transmission use the other buffer */ |
| 846 | if (fec->tbd_index) |
| 847 | fec->tbd_index = 0; |
| 848 | else |
| 849 | fec->tbd_index = 1; |
| 850 | |
Marek Vasut | bc1ce15 | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 851 | return ret; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Pull one frame from the card |
| 856 | * @param[in] dev Our ethernet device to handle |
| 857 | * @return Length of packet read |
| 858 | */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 859 | #ifdef CONFIG_DM_ETH |
| 860 | static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp) |
| 861 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 862 | static int fec_recv(struct eth_device *dev) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 863 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 864 | { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 865 | #ifdef CONFIG_DM_ETH |
| 866 | struct fec_priv *fec = dev_get_priv(dev); |
| 867 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 868 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 869 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 870 | struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index]; |
| 871 | unsigned long ievent; |
| 872 | int frame_length, len = 0; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 873 | uint16_t bd_status; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 874 | ulong addr, size, end; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 875 | int i; |
Ye Li | 07763ac | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 876 | |
| 877 | #ifdef CONFIG_DM_ETH |
| 878 | *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE); |
| 879 | if (*packetp == 0) { |
| 880 | printf("%s: error allocating packetp\n", __func__); |
| 881 | return -ENOMEM; |
| 882 | } |
| 883 | #else |
Fabio Estevam | fd37f19 | 2013-09-17 23:13:10 -0300 | [diff] [blame] | 884 | ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE); |
Ye Li | 07763ac | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 885 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 886 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 887 | /* Check if any critical events have happened */ |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 888 | ievent = readl(&fec->eth->ievent); |
| 889 | writel(ievent, &fec->eth->ievent); |
Marek Vasut | eda959f | 2011-10-24 23:40:03 +0000 | [diff] [blame] | 890 | debug("fec_recv: ievent 0x%lx\n", ievent); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 891 | if (ievent & FEC_IEVENT_BABR) { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 892 | #ifdef CONFIG_DM_ETH |
| 893 | fecmxc_halt(dev); |
| 894 | fecmxc_init(dev); |
| 895 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 896 | fec_halt(dev); |
| 897 | fec_init(dev, fec->bd); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 898 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 899 | printf("some error: 0x%08lx\n", ievent); |
| 900 | return 0; |
| 901 | } |
| 902 | if (ievent & FEC_IEVENT_HBERR) { |
| 903 | /* Heartbeat error */ |
| 904 | writel(0x00000001 | readl(&fec->eth->x_cntrl), |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 905 | &fec->eth->x_cntrl); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 906 | } |
| 907 | if (ievent & FEC_IEVENT_GRA) { |
| 908 | /* Graceful stop complete */ |
| 909 | if (readl(&fec->eth->x_cntrl) & 0x00000001) { |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 910 | #ifdef CONFIG_DM_ETH |
| 911 | fecmxc_halt(dev); |
| 912 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 913 | fec_halt(dev); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 914 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 915 | writel(~0x00000001 & readl(&fec->eth->x_cntrl), |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 916 | &fec->eth->x_cntrl); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 917 | #ifdef CONFIG_DM_ETH |
| 918 | fecmxc_init(dev); |
| 919 | #else |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 920 | fec_init(dev, fec->bd); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 921 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | /* |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 926 | * Read the buffer status. Before the status can be read, the data cache |
| 927 | * must be invalidated, because the data in RAM might have been changed |
| 928 | * by DMA. The descriptors are properly aligned to cachelines so there's |
| 929 | * no need to worry they'd overlap. |
| 930 | * |
| 931 | * WARNING: By invalidating the descriptor here, we also invalidate |
| 932 | * the descriptors surrounding this one. Therefore we can NOT change the |
| 933 | * contents of this descriptor nor the surrounding ones. The problem is |
| 934 | * that in order to mark the descriptor as processed, we need to change |
| 935 | * the descriptor. The solution is to mark the whole cache line when all |
| 936 | * descriptors in the cache line are processed. |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 937 | */ |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 938 | addr = (ulong)rbd; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 939 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 940 | size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
| 941 | invalidate_dcache_range(addr, addr + size); |
| 942 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 943 | bd_status = readw(&rbd->status); |
| 944 | debug("fec_recv: status 0x%x\n", bd_status); |
| 945 | |
| 946 | if (!(bd_status & FEC_RBD_EMPTY)) { |
| 947 | if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) && |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 948 | ((readw(&rbd->data_length) - 4) > 14)) { |
| 949 | /* Get buffer address and size */ |
Albert ARIBAUD \(3ADEV\) | b189584 | 2015-06-19 14:18:27 +0200 | [diff] [blame] | 950 | addr = readl(&rbd->data_pointer); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 951 | frame_length = readw(&rbd->data_length) - 4; |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 952 | /* Invalidate data cache over the buffer */ |
Marek Vasut | efe24d2 | 2012-08-26 10:19:21 +0000 | [diff] [blame] | 953 | end = roundup(addr + frame_length, ARCH_DMA_MINALIGN); |
| 954 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 955 | invalidate_dcache_range(addr, end); |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 956 | |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 957 | /* Fill the buffer and pass it to upper layers */ |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 958 | #ifdef CONFIG_FEC_MXC_SWAP_PACKET |
Albert ARIBAUD \(3ADEV\) | b189584 | 2015-06-19 14:18:27 +0200 | [diff] [blame] | 959 | swap_packet((uint32_t *)addr, frame_length); |
Marek Vasut | be7e87e | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 960 | #endif |
Ye Li | 07763ac | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 961 | |
| 962 | #ifdef CONFIG_DM_ETH |
| 963 | memcpy(*packetp, (char *)addr, frame_length); |
| 964 | #else |
Albert ARIBAUD \(3ADEV\) | b189584 | 2015-06-19 14:18:27 +0200 | [diff] [blame] | 965 | memcpy(buff, (char *)addr, frame_length); |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 966 | net_process_received_packet(buff, frame_length); |
Ye Li | 07763ac | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 967 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 968 | len = frame_length; |
| 969 | } else { |
| 970 | if (bd_status & FEC_RBD_ERR) |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 971 | debug("error frame: 0x%08lx 0x%08x\n", |
| 972 | addr, bd_status); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 973 | } |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 974 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 975 | /* |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 976 | * Free the current buffer, restart the engine and move forward |
| 977 | * to the next buffer. Here we check if the whole cacheline of |
| 978 | * descriptors was already processed and if so, we mark it free |
| 979 | * as whole. |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 980 | */ |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 981 | size = RXDESC_PER_CACHELINE - 1; |
| 982 | if ((fec->rbd_index & size) == size) { |
| 983 | i = fec->rbd_index - size; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 984 | addr = (ulong)&fec->rbd_base[i]; |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 985 | for (; i <= fec->rbd_index ; i++) { |
| 986 | fec_rbd_clean(i == (FEC_RBD_NUM - 1), |
| 987 | &fec->rbd_base[i]); |
| 988 | } |
| 989 | flush_dcache_range(addr, |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 990 | addr + ARCH_DMA_MINALIGN); |
Eric Nelson | 5c1ad3e | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 993 | fec_rx_task_enable(fec); |
| 994 | fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM; |
| 995 | } |
| 996 | debug("fec_recv: stop\n"); |
| 997 | |
| 998 | return len; |
| 999 | } |
| 1000 | |
Troy Kisky | ef8e3a3 | 2012-10-22 16:40:44 +0000 | [diff] [blame] | 1001 | static void fec_set_dev_name(char *dest, int dev_id) |
| 1002 | { |
| 1003 | sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id); |
| 1004 | } |
| 1005 | |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1006 | static int fec_alloc_descs(struct fec_priv *fec) |
| 1007 | { |
| 1008 | unsigned int size; |
| 1009 | int i; |
| 1010 | uint8_t *data; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1011 | ulong addr; |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1012 | |
| 1013 | /* Allocate TX descriptors. */ |
| 1014 | size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
| 1015 | fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size); |
| 1016 | if (!fec->tbd_base) |
| 1017 | goto err_tx; |
| 1018 | |
| 1019 | /* Allocate RX descriptors. */ |
| 1020 | size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
| 1021 | fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size); |
| 1022 | if (!fec->rbd_base) |
| 1023 | goto err_rx; |
| 1024 | |
| 1025 | memset(fec->rbd_base, 0, size); |
| 1026 | |
| 1027 | /* Allocate RX buffers. */ |
| 1028 | |
| 1029 | /* Maximum RX buffer size. */ |
Fabio Estevam | db5b7f5 | 2014-08-25 13:34:16 -0300 | [diff] [blame] | 1030 | size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN); |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1031 | for (i = 0; i < FEC_RBD_NUM; i++) { |
Fabio Estevam | db5b7f5 | 2014-08-25 13:34:16 -0300 | [diff] [blame] | 1032 | data = memalign(FEC_DMA_RX_MINALIGN, size); |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1033 | if (!data) { |
| 1034 | printf("%s: error allocating rxbuf %d\n", __func__, i); |
| 1035 | goto err_ring; |
| 1036 | } |
| 1037 | |
| 1038 | memset(data, 0, size); |
| 1039 | |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1040 | addr = (ulong)data; |
| 1041 | fec->rbd_base[i].data_pointer = (uint32_t)addr; |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1042 | fec->rbd_base[i].status = FEC_RBD_EMPTY; |
| 1043 | fec->rbd_base[i].data_length = 0; |
| 1044 | /* Flush the buffer to memory. */ |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1045 | flush_dcache_range(addr, addr + size); |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* Mark the last RBD to close the ring. */ |
| 1049 | fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY; |
| 1050 | |
| 1051 | fec->rbd_index = 0; |
| 1052 | fec->tbd_index = 0; |
| 1053 | |
| 1054 | return 0; |
| 1055 | |
| 1056 | err_ring: |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1057 | for (; i >= 0; i--) { |
| 1058 | addr = fec->rbd_base[i].data_pointer; |
| 1059 | free((void *)addr); |
| 1060 | } |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1061 | free(fec->rbd_base); |
| 1062 | err_rx: |
| 1063 | free(fec->tbd_base); |
| 1064 | err_tx: |
| 1065 | return -ENOMEM; |
| 1066 | } |
| 1067 | |
| 1068 | static void fec_free_descs(struct fec_priv *fec) |
| 1069 | { |
| 1070 | int i; |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1071 | ulong addr; |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1072 | |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1073 | for (i = 0; i < FEC_RBD_NUM; i++) { |
| 1074 | addr = fec->rbd_base[i].data_pointer; |
| 1075 | free((void *)addr); |
| 1076 | } |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1077 | free(fec->rbd_base); |
| 1078 | free(fec->tbd_base); |
| 1079 | } |
| 1080 | |
Peng Fan | 1bcabd7 | 2018-03-28 20:54:12 +0800 | [diff] [blame] | 1081 | struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1082 | { |
Peng Fan | 1bcabd7 | 2018-03-28 20:54:12 +0800 | [diff] [blame] | 1083 | struct ethernet_regs *eth = (struct ethernet_regs *)base_addr; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1084 | struct mii_dev *bus; |
| 1085 | int ret; |
| 1086 | |
| 1087 | bus = mdio_alloc(); |
| 1088 | if (!bus) { |
| 1089 | printf("mdio_alloc failed\n"); |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | bus->read = fec_phy_read; |
| 1093 | bus->write = fec_phy_write; |
| 1094 | bus->priv = eth; |
| 1095 | fec_set_dev_name(bus->name, dev_id); |
| 1096 | |
| 1097 | ret = mdio_register(bus); |
| 1098 | if (ret) { |
| 1099 | printf("mdio_register failed\n"); |
| 1100 | free(bus); |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | fec_mii_setspeed(eth); |
| 1104 | return bus; |
| 1105 | } |
| 1106 | |
| 1107 | #ifndef CONFIG_DM_ETH |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1108 | #ifdef CONFIG_PHYLIB |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 1109 | int fec_probe(struct bd_info *bd, int dev_id, uint32_t base_addr, |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1110 | struct mii_dev *bus, struct phy_device *phydev) |
| 1111 | #else |
Masahiro Yamada | bb5a2cf | 2020-06-26 15:13:34 +0900 | [diff] [blame] | 1112 | static int fec_probe(struct bd_info *bd, int dev_id, uint32_t base_addr, |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1113 | struct mii_dev *bus, int phy_id) |
| 1114 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1115 | { |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1116 | struct eth_device *edev; |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1117 | struct fec_priv *fec; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1118 | unsigned char ethaddr[6]; |
Andy Duan | 979a589 | 2017-04-10 19:44:35 +0800 | [diff] [blame] | 1119 | char mac[16]; |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1120 | uint32_t start; |
| 1121 | int ret = 0; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1122 | |
| 1123 | /* create and fill edev struct */ |
| 1124 | edev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
| 1125 | if (!edev) { |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1126 | puts("fec_mxc: not enough malloc memory for eth_device\n"); |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1127 | ret = -ENOMEM; |
| 1128 | goto err1; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1129 | } |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1130 | |
| 1131 | fec = (struct fec_priv *)malloc(sizeof(struct fec_priv)); |
| 1132 | if (!fec) { |
| 1133 | puts("fec_mxc: not enough malloc memory for fec_priv\n"); |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1134 | ret = -ENOMEM; |
| 1135 | goto err2; |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Nobuhiro Iwamatsu | de0b957 | 2010-10-19 14:03:42 +0900 | [diff] [blame] | 1138 | memset(edev, 0, sizeof(*edev)); |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1139 | memset(fec, 0, sizeof(*fec)); |
| 1140 | |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1141 | ret = fec_alloc_descs(fec); |
| 1142 | if (ret) |
| 1143 | goto err3; |
| 1144 | |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1145 | edev->priv = fec; |
| 1146 | edev->init = fec_init; |
| 1147 | edev->send = fec_send; |
| 1148 | edev->recv = fec_recv; |
| 1149 | edev->halt = fec_halt; |
Heiko Schocher | fb57ec9 | 2010-04-27 07:43:52 +0200 | [diff] [blame] | 1150 | edev->write_hwaddr = fec_set_hwaddr; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1151 | |
Ye Li | f24e482 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1152 | fec->eth = (struct ethernet_regs *)(ulong)base_addr; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1153 | fec->bd = bd; |
| 1154 | |
Marek Vasut | 392b850 | 2011-09-11 18:05:33 +0000 | [diff] [blame] | 1155 | fec->xcv_type = CONFIG_FEC_XCV_TYPE; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1156 | |
| 1157 | /* Reset chip. */ |
John Rigby | cb17b92 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 1158 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl); |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1159 | start = get_timer(0); |
| 1160 | while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) { |
| 1161 | if (get_timer(start) > (CONFIG_SYS_HZ * 5)) { |
Vagrant Cascadian | 3450a85 | 2016-10-23 20:45:19 -0700 | [diff] [blame] | 1162 | printf("FEC MXC: Timeout resetting chip\n"); |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1163 | goto err4; |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1164 | } |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1165 | udelay(10); |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1166 | } |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1167 | |
Marek Vasut | a5990b2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 1168 | fec_reg_setup(fec); |
Troy Kisky | ef8e3a3 | 2012-10-22 16:40:44 +0000 | [diff] [blame] | 1169 | fec_set_dev_name(edev->name, dev_id); |
| 1170 | fec->dev_id = (dev_id == -1) ? 0 : dev_id; |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1171 | fec->bus = bus; |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1172 | fec_mii_setspeed(bus->priv); |
| 1173 | #ifdef CONFIG_PHYLIB |
| 1174 | fec->phydev = phydev; |
| 1175 | phy_connect_dev(phydev, edev); |
| 1176 | /* Configure phy */ |
| 1177 | phy_config(phydev); |
| 1178 | #else |
| 1179 | fec->phy_id = phy_id; |
| 1180 | #endif |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1181 | eth_register(edev); |
Andy Duan | 979a589 | 2017-04-10 19:44:35 +0800 | [diff] [blame] | 1182 | /* only support one eth device, the index number pointed by dev_id */ |
| 1183 | edev->index = fec->dev_id; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1184 | |
Andy Duan | f01e4e1 | 2017-04-10 19:44:34 +0800 | [diff] [blame] | 1185 | if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) { |
| 1186 | debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr); |
Stefano Babic | 4294b24 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 1187 | memcpy(edev->enetaddr, ethaddr, 6); |
Andy Duan | 979a589 | 2017-04-10 19:44:35 +0800 | [diff] [blame] | 1188 | if (fec->dev_id) |
| 1189 | sprintf(mac, "eth%daddr", fec->dev_id); |
| 1190 | else |
| 1191 | strcpy(mac, "ethaddr"); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1192 | if (!env_get(mac)) |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 1193 | eth_env_set_enetaddr(mac, ethaddr); |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1194 | } |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1195 | return ret; |
Marek Vasut | 79e5f27 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1196 | err4: |
| 1197 | fec_free_descs(fec); |
Marek Vasut | e382fb4 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1198 | err3: |
| 1199 | free(fec); |
| 1200 | err2: |
| 1201 | free(edev); |
| 1202 | err1: |
| 1203 | return ret; |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1204 | } |
| 1205 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 1206 | int fecmxc_initialize_multi(struct bd_info *bd, int dev_id, int phy_id, |
| 1207 | uint32_t addr) |
Troy Kisky | eef2448 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1208 | { |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1209 | uint32_t base_mii; |
| 1210 | struct mii_dev *bus = NULL; |
| 1211 | #ifdef CONFIG_PHYLIB |
| 1212 | struct phy_device *phydev = NULL; |
| 1213 | #endif |
| 1214 | int ret; |
| 1215 | |
Peng Fan | 3b26d52 | 2020-05-01 22:08:37 +0800 | [diff] [blame] | 1216 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 1217 | if (enet_fused((ulong)addr)) { |
| 1218 | printf("SoC fuse indicates Ethernet@0x%x is unavailable.\n", addr); |
| 1219 | return -ENODEV; |
| 1220 | } |
| 1221 | } |
| 1222 | |
Peng Fan | fbada48 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1223 | #ifdef CONFIG_FEC_MXC_MDIO_BASE |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1224 | /* |
| 1225 | * The i.MX28 has two ethernet interfaces, but they are not equal. |
| 1226 | * Only the first one can access the MDIO bus. |
| 1227 | */ |
Peng Fan | fbada48 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1228 | base_mii = CONFIG_FEC_MXC_MDIO_BASE; |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1229 | #else |
| 1230 | base_mii = addr; |
| 1231 | #endif |
Troy Kisky | eef2448 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1232 | debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr); |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1233 | bus = fec_get_miibus(base_mii, dev_id); |
| 1234 | if (!bus) |
| 1235 | return -ENOMEM; |
| 1236 | #ifdef CONFIG_PHYLIB |
| 1237 | phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII); |
| 1238 | if (!phydev) { |
Måns Rullgård | 845a57b | 2015-12-08 15:38:46 +0000 | [diff] [blame] | 1239 | mdio_unregister(bus); |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1240 | free(bus); |
| 1241 | return -ENOMEM; |
| 1242 | } |
| 1243 | ret = fec_probe(bd, dev_id, addr, bus, phydev); |
| 1244 | #else |
| 1245 | ret = fec_probe(bd, dev_id, addr, bus, phy_id); |
| 1246 | #endif |
| 1247 | if (ret) { |
| 1248 | #ifdef CONFIG_PHYLIB |
| 1249 | free(phydev); |
| 1250 | #endif |
Måns Rullgård | 845a57b | 2015-12-08 15:38:46 +0000 | [diff] [blame] | 1251 | mdio_unregister(bus); |
Troy Kisky | fe428b9 | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1252 | free(bus); |
| 1253 | } |
| 1254 | return ret; |
Troy Kisky | eef2448 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Troy Kisky | 09439c3 | 2012-10-22 16:40:40 +0000 | [diff] [blame] | 1257 | #ifdef CONFIG_FEC_MXC_PHYADDR |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 1258 | int fecmxc_initialize(struct bd_info *bd) |
Ilya Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1259 | { |
Troy Kisky | eef2448 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1260 | return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR, |
| 1261 | IMX_FEC_BASE); |
Marek Vasut | 9e27e9d | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1262 | } |
| 1263 | #endif |
| 1264 | |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1265 | #ifndef CONFIG_PHYLIB |
Marek Vasut | 2e5f442 | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 1266 | int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int)) |
| 1267 | { |
| 1268 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
| 1269 | fec->mii_postcall = cb; |
| 1270 | return 0; |
| 1271 | } |
Troy Kisky | 13947f4 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1272 | #endif |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1273 | |
| 1274 | #else |
| 1275 | |
Jagan Teki | 1ed2570 | 2016-12-06 00:00:51 +0100 | [diff] [blame] | 1276 | static int fecmxc_read_rom_hwaddr(struct udevice *dev) |
| 1277 | { |
| 1278 | struct fec_priv *priv = dev_get_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1279 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 1ed2570 | 2016-12-06 00:00:51 +0100 | [diff] [blame] | 1280 | |
| 1281 | return fec_get_hwaddr(priv->dev_id, pdata->enetaddr); |
| 1282 | } |
| 1283 | |
Tim Harvey | 87550a8 | 2021-06-30 16:50:06 -0700 | [diff] [blame] | 1284 | static int fecmxc_set_promisc(struct udevice *dev, bool enable) |
| 1285 | { |
| 1286 | struct fec_priv *priv = dev_get_priv(dev); |
| 1287 | |
| 1288 | priv->promisc = enable; |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Ye Li | 07763ac | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 1293 | static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 1294 | { |
| 1295 | if (packet) |
| 1296 | free(packet); |
| 1297 | |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1301 | static const struct eth_ops fecmxc_ops = { |
| 1302 | .start = fecmxc_init, |
| 1303 | .send = fecmxc_send, |
| 1304 | .recv = fecmxc_recv, |
Ye Li | 07763ac | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 1305 | .free_pkt = fecmxc_free_pkt, |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1306 | .stop = fecmxc_halt, |
| 1307 | .write_hwaddr = fecmxc_set_hwaddr, |
Jagan Teki | 1ed2570 | 2016-12-06 00:00:51 +0100 | [diff] [blame] | 1308 | .read_rom_hwaddr = fecmxc_read_rom_hwaddr, |
Tim Harvey | 87550a8 | 2021-06-30 16:50:06 -0700 | [diff] [blame] | 1309 | .set_promisc = fecmxc_set_promisc, |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1310 | }; |
| 1311 | |
Fabio Estevam | 89b5bd5 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1312 | static int device_get_phy_addr(struct fec_priv *priv, struct udevice *dev) |
Martyn Welch | 774ec60 | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1313 | { |
| 1314 | struct ofnode_phandle_args phandle_args; |
Sean Anderson | eccd132 | 2021-04-15 13:06:08 -0400 | [diff] [blame] | 1315 | int reg, ret; |
Martyn Welch | 774ec60 | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1316 | |
Sean Anderson | eccd132 | 2021-04-15 13:06:08 -0400 | [diff] [blame] | 1317 | ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, |
| 1318 | &phandle_args); |
| 1319 | if (ret) { |
Tim Harvey | 69c81d6 | 2021-06-30 16:50:04 -0700 | [diff] [blame] | 1320 | priv->phy_of_node = ofnode_find_subnode(dev_ofnode(dev), |
| 1321 | "fixed-link"); |
| 1322 | if (ofnode_valid(priv->phy_of_node)) |
| 1323 | return 0; |
| 1324 | debug("Failed to find phy-handle (err = %d)\n", ret); |
Sean Anderson | eccd132 | 2021-04-15 13:06:08 -0400 | [diff] [blame] | 1325 | return ret; |
Martyn Welch | 774ec60 | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Sean Anderson | eccd132 | 2021-04-15 13:06:08 -0400 | [diff] [blame] | 1328 | if (!ofnode_is_available(phandle_args.node)) |
| 1329 | return -ENOENT; |
Fabio Estevam | 89b5bd5 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1330 | |
Sean Anderson | eccd132 | 2021-04-15 13:06:08 -0400 | [diff] [blame] | 1331 | priv->phy_of_node = phandle_args.node; |
Martyn Welch | 774ec60 | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1332 | reg = ofnode_read_u32_default(phandle_args.node, "reg", 0); |
| 1333 | |
| 1334 | return reg; |
| 1335 | } |
| 1336 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1337 | static int fec_phy_init(struct fec_priv *priv, struct udevice *dev) |
| 1338 | { |
| 1339 | struct phy_device *phydev; |
Martyn Welch | 774ec60 | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1340 | int addr; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1341 | |
Fabio Estevam | 89b5bd5 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1342 | addr = device_get_phy_addr(priv, dev); |
Lukasz Majewski | 178d4f0 | 2018-04-15 21:45:54 +0200 | [diff] [blame] | 1343 | #ifdef CONFIG_FEC_MXC_PHYADDR |
Hannes Schmelzer | b882005 | 2019-02-15 10:30:18 +0100 | [diff] [blame] | 1344 | addr = CONFIG_FEC_MXC_PHYADDR; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1345 | #endif |
| 1346 | |
Hannes Schmelzer | b882005 | 2019-02-15 10:30:18 +0100 | [diff] [blame] | 1347 | phydev = phy_connect(priv->bus, addr, dev, priv->interface); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1348 | if (!phydev) |
| 1349 | return -ENODEV; |
| 1350 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1351 | priv->phydev = phydev; |
Fabio Estevam | 89b5bd5 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1352 | priv->phydev->node = priv->phy_of_node; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1353 | phy_config(phydev); |
| 1354 | |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1358 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1359 | /* FEC GPIO reset */ |
| 1360 | static void fec_gpio_reset(struct fec_priv *priv) |
| 1361 | { |
| 1362 | debug("fec_gpio_reset: fec_gpio_reset(dev)\n"); |
| 1363 | if (dm_gpio_is_valid(&priv->phy_reset_gpio)) { |
| 1364 | dm_gpio_set_value(&priv->phy_reset_gpio, 1); |
Martin Fuzzey | 9b8b918 | 2018-10-04 19:59:18 +0200 | [diff] [blame] | 1365 | mdelay(priv->reset_delay); |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1366 | dm_gpio_set_value(&priv->phy_reset_gpio, 0); |
Andrejs Cainikovs | 31d4045 | 2019-03-01 13:27:59 +0000 | [diff] [blame] | 1367 | if (priv->reset_post_delay) |
| 1368 | mdelay(priv->reset_post_delay); |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | #endif |
| 1372 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1373 | static int fecmxc_probe(struct udevice *dev) |
| 1374 | { |
Sean Anderson | cd43591 | 2021-04-15 13:06:09 -0400 | [diff] [blame] | 1375 | bool dm_mii_bus = true; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1376 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1377 | struct fec_priv *priv = dev_get_priv(dev); |
| 1378 | struct mii_dev *bus = NULL; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1379 | uint32_t start; |
| 1380 | int ret; |
| 1381 | |
Peng Fan | 3b26d52 | 2020-05-01 22:08:37 +0800 | [diff] [blame] | 1382 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 1383 | if (enet_fused((ulong)priv->eth)) { |
| 1384 | printf("SoC fuse indicates Ethernet@0x%lx is unavailable.\n", (ulong)priv->eth); |
| 1385 | return -ENODEV; |
| 1386 | } |
| 1387 | } |
| 1388 | |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 1389 | if (IS_ENABLED(CONFIG_IMX8)) { |
| 1390 | ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk); |
| 1391 | if (ret < 0) { |
| 1392 | debug("Can't get FEC ipg clk: %d\n", ret); |
| 1393 | return ret; |
| 1394 | } |
| 1395 | ret = clk_enable(&priv->ipg_clk); |
| 1396 | if (ret < 0) { |
| 1397 | debug("Can't enable FEC ipg clk: %d\n", ret); |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
| 1401 | priv->clk_rate = clk_get_rate(&priv->ipg_clk); |
Peng Fan | 673f659 | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 1402 | } else if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 1403 | ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk); |
| 1404 | if (ret < 0) { |
| 1405 | debug("Can't get FEC ipg clk: %d\n", ret); |
| 1406 | return ret; |
| 1407 | } |
| 1408 | ret = clk_enable(&priv->ipg_clk); |
| 1409 | if(ret) |
| 1410 | return ret; |
| 1411 | |
| 1412 | ret = clk_get_by_name(dev, "ahb", &priv->ahb_clk); |
| 1413 | if (ret < 0) { |
| 1414 | debug("Can't get FEC ahb clk: %d\n", ret); |
| 1415 | return ret; |
| 1416 | } |
| 1417 | ret = clk_enable(&priv->ahb_clk); |
| 1418 | if (ret) |
| 1419 | return ret; |
| 1420 | |
| 1421 | ret = clk_get_by_name(dev, "enet_out", &priv->clk_enet_out); |
| 1422 | if (!ret) { |
| 1423 | ret = clk_enable(&priv->clk_enet_out); |
| 1424 | if (ret) |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | ret = clk_get_by_name(dev, "enet_clk_ref", &priv->clk_ref); |
| 1429 | if (!ret) { |
| 1430 | ret = clk_enable(&priv->clk_ref); |
| 1431 | if (ret) |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | ret = clk_get_by_name(dev, "ptp", &priv->clk_ptp); |
| 1436 | if (!ret) { |
| 1437 | ret = clk_enable(&priv->clk_ptp); |
| 1438 | if (ret) |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
| 1442 | priv->clk_rate = clk_get_rate(&priv->ipg_clk); |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1445 | ret = fec_alloc_descs(priv); |
| 1446 | if (ret) |
| 1447 | return ret; |
| 1448 | |
Martin Fuzzey | ad8c43c | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1449 | #ifdef CONFIG_DM_REGULATOR |
| 1450 | if (priv->phy_supply) { |
Adam Ford | 8f1a5ac | 2019-01-15 11:26:48 -0600 | [diff] [blame] | 1451 | ret = regulator_set_enable(priv->phy_supply, true); |
Martin Fuzzey | ad8c43c | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1452 | if (ret) { |
| 1453 | printf("%s: Error enabling phy supply\n", dev->name); |
| 1454 | return ret; |
| 1455 | } |
| 1456 | } |
| 1457 | #endif |
| 1458 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1459 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1460 | fec_gpio_reset(priv); |
| 1461 | #endif |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1462 | /* Reset chip. */ |
Jagan Teki | 567173a | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 1463 | writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET, |
| 1464 | &priv->eth->ecntrl); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1465 | start = get_timer(0); |
| 1466 | while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) { |
| 1467 | if (get_timer(start) > (CONFIG_SYS_HZ * 5)) { |
| 1468 | printf("FEC MXC: Timeout reseting chip\n"); |
| 1469 | goto err_timeout; |
| 1470 | } |
| 1471 | udelay(10); |
| 1472 | } |
| 1473 | |
| 1474 | fec_reg_setup(priv); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1475 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1476 | priv->dev_id = dev_seq(dev); |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1477 | |
| 1478 | #ifdef CONFIG_DM_ETH_PHY |
| 1479 | bus = eth_phy_get_mdio_bus(dev); |
Peng Fan | fbada48 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1480 | #endif |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1481 | |
| 1482 | if (!bus) { |
Sean Anderson | cd43591 | 2021-04-15 13:06:09 -0400 | [diff] [blame] | 1483 | dm_mii_bus = false; |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1484 | #ifdef CONFIG_FEC_MXC_MDIO_BASE |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1485 | bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, |
| 1486 | dev_seq(dev)); |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1487 | #else |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1488 | bus = fec_get_miibus((ulong)priv->eth, dev_seq(dev)); |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1489 | #endif |
| 1490 | } |
Lothar Waßmann | 306dd7d | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1491 | if (!bus) { |
| 1492 | ret = -ENOMEM; |
| 1493 | goto err_mii; |
| 1494 | } |
| 1495 | |
Ye Li | 6a895d0 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1496 | #ifdef CONFIG_DM_ETH_PHY |
| 1497 | eth_phy_set_mdio_bus(dev, bus); |
| 1498 | #endif |
| 1499 | |
Lothar Waßmann | 306dd7d | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1500 | priv->bus = bus; |
Lothar Waßmann | 306dd7d | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1501 | priv->interface = pdata->phy_interface; |
Martin Fuzzey | 0126c64 | 2018-10-04 19:59:21 +0200 | [diff] [blame] | 1502 | switch (priv->interface) { |
| 1503 | case PHY_INTERFACE_MODE_MII: |
| 1504 | priv->xcv_type = MII100; |
| 1505 | break; |
| 1506 | case PHY_INTERFACE_MODE_RMII: |
| 1507 | priv->xcv_type = RMII; |
| 1508 | break; |
| 1509 | case PHY_INTERFACE_MODE_RGMII: |
| 1510 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 1511 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 1512 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 1513 | priv->xcv_type = RGMII; |
| 1514 | break; |
| 1515 | default: |
| 1516 | priv->xcv_type = CONFIG_FEC_XCV_TYPE; |
| 1517 | printf("Unsupported interface type %d defaulting to %d\n", |
| 1518 | priv->interface, priv->xcv_type); |
| 1519 | break; |
| 1520 | } |
| 1521 | |
Lothar Waßmann | 306dd7d | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1522 | ret = fec_phy_init(priv, dev); |
| 1523 | if (ret) |
| 1524 | goto err_phy; |
| 1525 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1526 | return 0; |
| 1527 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1528 | err_phy: |
Sean Anderson | cd43591 | 2021-04-15 13:06:09 -0400 | [diff] [blame] | 1529 | if (!dm_mii_bus) { |
| 1530 | mdio_unregister(bus); |
| 1531 | free(bus); |
| 1532 | } |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1533 | err_mii: |
Ye Li | 2087eac | 2018-03-28 20:54:16 +0800 | [diff] [blame] | 1534 | err_timeout: |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1535 | fec_free_descs(priv); |
| 1536 | return ret; |
| 1537 | } |
| 1538 | |
| 1539 | static int fecmxc_remove(struct udevice *dev) |
| 1540 | { |
| 1541 | struct fec_priv *priv = dev_get_priv(dev); |
| 1542 | |
| 1543 | free(priv->phydev); |
| 1544 | fec_free_descs(priv); |
| 1545 | mdio_unregister(priv->bus); |
| 1546 | mdio_free(priv->bus); |
| 1547 | |
Martin Fuzzey | ad8c43c | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1548 | #ifdef CONFIG_DM_REGULATOR |
| 1549 | if (priv->phy_supply) |
| 1550 | regulator_set_enable(priv->phy_supply, false); |
| 1551 | #endif |
| 1552 | |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
| 1555 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1556 | static int fecmxc_of_to_plat(struct udevice *dev) |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1557 | { |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1558 | int ret = 0; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1559 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1560 | struct fec_priv *priv = dev_get_priv(dev); |
| 1561 | const char *phy_mode; |
| 1562 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 1563 | pdata->iobase = dev_read_addr(dev); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1564 | priv->eth = (struct ethernet_regs *)pdata->iobase; |
| 1565 | |
| 1566 | pdata->phy_interface = -1; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 1567 | phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode", |
| 1568 | NULL); |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1569 | if (phy_mode) |
| 1570 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 1571 | if (pdata->phy_interface == -1) { |
| 1572 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 1573 | return -EINVAL; |
| 1574 | } |
| 1575 | |
Martin Fuzzey | ad8c43c | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1576 | #ifdef CONFIG_DM_REGULATOR |
| 1577 | device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply); |
| 1578 | #endif |
| 1579 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1580 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1581 | ret = gpio_request_by_name(dev, "phy-reset-gpios", 0, |
Martin Fuzzey | 331fcab | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1582 | &priv->phy_reset_gpio, GPIOD_IS_OUT); |
| 1583 | if (ret < 0) |
| 1584 | return 0; /* property is optional, don't return error! */ |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1585 | |
Martin Fuzzey | 331fcab | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1586 | priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1); |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1587 | if (priv->reset_delay > 1000) { |
Martin Fuzzey | 331fcab | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1588 | printf("FEC MXC: phy reset duration should be <= 1000ms\n"); |
| 1589 | /* property value wrong, use default value */ |
| 1590 | priv->reset_delay = 1; |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1591 | } |
Andrejs Cainikovs | 31d4045 | 2019-03-01 13:27:59 +0000 | [diff] [blame] | 1592 | |
| 1593 | priv->reset_post_delay = dev_read_u32_default(dev, |
| 1594 | "phy-reset-post-delay", |
| 1595 | 0); |
| 1596 | if (priv->reset_post_delay > 1000) { |
| 1597 | printf("FEC MXC: phy reset post delay should be <= 1000ms\n"); |
| 1598 | /* property value wrong, use default value */ |
| 1599 | priv->reset_post_delay = 0; |
| 1600 | } |
Michael Trimarchi | efd0b79 | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1601 | #endif |
| 1602 | |
Martin Fuzzey | 331fcab | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1603 | return 0; |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | static const struct udevice_id fecmxc_ids[] = { |
Lukasz Majewski | 7782f4e | 2019-06-19 17:31:03 +0200 | [diff] [blame] | 1607 | { .compatible = "fsl,imx28-fec" }, |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1608 | { .compatible = "fsl,imx6q-fec" }, |
Peng Fan | 979e0fc | 2018-03-28 20:54:15 +0800 | [diff] [blame] | 1609 | { .compatible = "fsl,imx6sl-fec" }, |
| 1610 | { .compatible = "fsl,imx6sx-fec" }, |
| 1611 | { .compatible = "fsl,imx6ul-fec" }, |
Lukasz Majewski | 948239e | 2018-04-15 21:54:22 +0200 | [diff] [blame] | 1612 | { .compatible = "fsl,imx53-fec" }, |
Anatolij Gustschin | 58ec4d3 | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 1613 | { .compatible = "fsl,imx7d-fec" }, |
Lukasz Majewski | 27589e7 | 2019-02-13 22:46:38 +0100 | [diff] [blame] | 1614 | { .compatible = "fsl,mvf600-fec" }, |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1615 | { } |
| 1616 | }; |
| 1617 | |
| 1618 | U_BOOT_DRIVER(fecmxc_gem) = { |
| 1619 | .name = "fecmxc", |
| 1620 | .id = UCLASS_ETH, |
| 1621 | .of_match = fecmxc_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1622 | .of_to_plat = fecmxc_of_to_plat, |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1623 | .probe = fecmxc_probe, |
| 1624 | .remove = fecmxc_remove, |
| 1625 | .ops = &fecmxc_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1626 | .priv_auto = sizeof(struct fec_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1627 | .plat_auto = sizeof(struct eth_pdata), |
Jagan Teki | 60752ca | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1628 | }; |
| 1629 | #endif |