Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ilya Yanok | e93a4a5 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 16 | #include <memalign.h> |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 17 | #include <miiphy.h> |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 18 | #include <net.h> |
Jeroen Hofstee | 120f43f | 2014-10-08 22:57:40 +0200 | [diff] [blame] | 19 | #include <netdev.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Martin Fuzzey | 9a6a2c9 | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 23 | #include <power/regulator.h> |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 24 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 25 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 26 | #include <linux/errno.h> |
Marek Vasut | 4d85b03 | 2012-08-26 10:19:20 +0000 | [diff] [blame] | 27 | #include <linux/compiler.h> |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 28 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 29 | #include <asm/arch/clock.h> |
| 30 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 31 | #include <asm/mach-imx/sys_proto.h> |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 32 | #include <asm-generic/gpio.h> |
| 33 | |
| 34 | #include "fec_mxc.h" |
Ye Li | ad122b7 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 35 | #include <eth_phy.h> |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 36 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Marek Vasut | 5f1631d | 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 | 8b798b2 | 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 | e93a4a5 | 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 | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 57 | #ifndef CONFIG_FEC_XCV_TYPE |
| 58 | #define CONFIG_FEC_XCV_TYPE MII100 |
Marek Vasut | dbb4fce | 2011-09-11 18:05:33 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Marek Vasut | 6a5fd4c | 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 | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_MX28 |
| 66 | #define CONFIG_FEC_MXC_SWAP_PACKET |
Marek Vasut | 6a5fd4c | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 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!" |
| 79 | #endif |
| 80 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 81 | #undef DEBUG |
| 82 | |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 83 | #ifdef CONFIG_FEC_MXC_SWAP_PACKET |
Marek Vasut | 6a5fd4c | 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 | c6cd8d5 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 96 | { |
Ilya Yanok | e93a4a5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 100 | int val; |
Ilya Yanok | e93a4a5 | 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 | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 106 | writel(FEC_IEVENT_MII, ð->ievent); |
Jagan Teki | c6cd8d5 | 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 | e93a4a5 | 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 | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 111 | phy | reg, ð->mii_data); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 112 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 113 | /* wait for the related interrupt */ |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 114 | start = get_timer(0); |
Marek Vasut | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 115 | while (!(readl(ð->ievent) & FEC_IEVENT_MII)) { |
Ilya Yanok | e93a4a5 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 122 | /* clear mii interrupt bit */ |
Marek Vasut | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 123 | writel(FEC_IEVENT_MII, ð->ievent); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 124 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 125 | /* it's now safe to read the PHY's register */ |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 126 | val = (unsigned short)readl(ð->mii_data); |
Jagan Teki | c6cd8d5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 129 | return val; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 130 | } |
| 131 | |
Peng Fan | dcf5e1b | 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 | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 139 | static int fec_get_clk_rate(void *udev, int idx) |
| 140 | { |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 141 | struct fec_priv *fec; |
| 142 | struct udevice *dev; |
| 143 | int ret; |
| 144 | |
Peng Fan | dcf5e1b | 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) { |
| 149 | ret = uclass_get_device(UCLASS_ETH, idx, &dev); |
| 150 | if (ret < 0) { |
| 151 | debug("Can't get FEC udev: %d\n", ret); |
| 152 | return ret; |
| 153 | } |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 154 | } |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 155 | |
Peng Fan | dcf5e1b | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 156 | fec = dev_get_priv(dev); |
| 157 | if (fec) |
| 158 | return fec->clk_rate; |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 159 | |
Peng Fan | dcf5e1b | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | } else { |
| 162 | return imx_get_fecclk(); |
| 163 | } |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Troy Kisky | 5e76265 | 2012-10-22 16:40:41 +0000 | [diff] [blame] | 166 | static void fec_mii_setspeed(struct ethernet_regs *eth) |
Stefano Babic | 889f2e2 | 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 | 4aeddb7 | 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 | 889f2e2 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 182 | */ |
Anatolij Gustschin | b71fc5e | 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 | 1af8274 | 2014-02-05 10:54:11 +0100 | [diff] [blame] | 197 | #ifdef FEC_QUIRK_ENET_MAC |
| 198 | speed--; |
| 199 | #endif |
Måns Rullgård | 4aeddb7 | 2015-12-08 15:38:45 +0000 | [diff] [blame] | 200 | writel(speed << 1 | hold << 8, ð->mii_speed); |
Troy Kisky | 5e76265 | 2012-10-22 16:40:41 +0000 | [diff] [blame] | 201 | debug("%s: mii_speed %08x\n", __func__, readl(ð->mii_speed)); |
Stefano Babic | 889f2e2 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 202 | } |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 203 | |
Jagan Teki | c6cd8d5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 206 | { |
Ilya Yanok | e93a4a5 | 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 | c6cd8d5 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 213 | |
| 214 | writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR | |
Marek Vasut | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 215 | FEC_MII_DATA_TA | phy | reg | data, ð->mii_data); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 216 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 217 | /* wait for the MII interrupt */ |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 218 | start = get_timer(0); |
Marek Vasut | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 219 | while (!(readl(ð->ievent) & FEC_IEVENT_MII)) { |
Ilya Yanok | e93a4a5 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 226 | /* clear MII interrupt bit */ |
Marek Vasut | bf2386b | 2011-09-11 18:05:34 +0000 | [diff] [blame] | 227 | writel(FEC_IEVENT_MII, ð->ievent); |
Jagan Teki | c6cd8d5 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jagan Teki | c6cd8d5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 236 | { |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 237 | return fec_mdio_read(bus->priv, phyaddr, regaddr); |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jagan Teki | c6cd8d5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 242 | { |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 243 | return fec_mdio_write(bus->priv, phyaddr, regaddr, data); |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | #ifndef CONFIG_PHYLIB |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 247 | static int miiphy_restart_aneg(struct eth_device *dev) |
| 248 | { |
Stefano Babic | d622817 | 2012-02-22 00:24:35 +0000 | [diff] [blame] | 249 | int ret = 0; |
| 250 | #if !defined(CONFIG_FEC_MXC_NO_ANEG) |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 251 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 252 | struct ethernet_regs *eth = fec->bus->priv; |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 253 | |
Ilya Yanok | e93a4a5 | 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 | e650e49 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 258 | #ifdef CONFIG_MX27 |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 259 | fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF); |
John Rigby | e650e49 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 260 | #endif |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 261 | fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 262 | udelay(1000); |
| 263 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 264 | /* Set the auto-negotiation advertisement register bits */ |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 265 | fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE, |
Jagan Teki | c6cd8d5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 268 | fec_mdio_write(eth, fec->phy_id, MII_BMCR, |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 269 | BMCR_ANENABLE | BMCR_ANRESTART); |
Marek Vasut | 539ecee | 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 | d622817 | 2012-02-22 00:24:35 +0000 | [diff] [blame] | 274 | #endif |
Marek Vasut | 539ecee | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 275 | return ret; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 276 | } |
| 277 | |
Hannes Schmelzer | 5a15c1a | 2016-06-22 12:07:14 +0200 | [diff] [blame] | 278 | #ifndef CONFIG_FEC_FIXED_SPEED |
Ilya Yanok | e93a4a5 | 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 | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 282 | int status; |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 283 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 284 | struct ethernet_regs *eth = fec->bus->priv; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 285 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 286 | /* Wait for AN completion */ |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 287 | start = get_timer(0); |
Ilya Yanok | e93a4a5 | 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 | 2000c66 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 297 | dev->name, status); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 298 | return -1; |
| 299 | } |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 300 | } while (!(status & BMSR_LSTATUS)); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 301 | |
| 302 | return 0; |
| 303 | } |
Hannes Schmelzer | 5a15c1a | 2016-06-22 12:07:14 +0200 | [diff] [blame] | 304 | #endif /* CONFIG_FEC_FIXED_SPEED */ |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 305 | #endif |
| 306 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 307 | static int fec_rx_task_enable(struct fec_priv *fec) |
| 308 | { |
Marek Vasut | c1582c0 | 2012-08-29 03:49:51 +0000 | [diff] [blame] | 309 | writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active); |
Ilya Yanok | e93a4a5 | 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 | c1582c0 | 2012-08-29 03:49:51 +0000 | [diff] [blame] | 320 | writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active); |
Ilya Yanok | e93a4a5 | 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 | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 333 | * @param[in] dsize desired size of each receive buffer |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 334 | * @return 0 on success |
| 335 | * |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 336 | * Init all RX descriptors to default values. |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 337 | */ |
Marek Vasut | 0388045 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 339 | { |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 340 | uint32_t size; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 341 | ulong data; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 342 | int i; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 343 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 344 | /* |
Marek Vasut | 0388045 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 347 | */ |
Eric Nelson | 3d2f727 | 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 | e267091 | 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 | 0388045 | 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 | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* Mark the last RBD to close the ring. */ |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 359 | fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 360 | fec->rbd_index = 0; |
| 361 | |
Ye Li | e267091 | 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 | e93a4a5 | 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 | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 380 | ulong addr = (ulong)fec->tbd_base; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 381 | unsigned size = roundup(2 * sizeof(struct fec_bd), |
| 382 | ARCH_DMA_MINALIGN); |
Marek Vasut | 0388045 | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 387 | fec->tbd_index = 0; |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 388 | flush_dcache_range(addr, addr + size); |
Ilya Yanok | e93a4a5 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 394 | * @param[in] prbd buffer descriptor to mark free again |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 395 | */ |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 396 | static void fec_rbd_clean(int last, struct fec_bd *prbd) |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 397 | { |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 398 | unsigned short flags = FEC_RBD_EMPTY; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 399 | if (last) |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 400 | flags |= FEC_RBD_WRAP; |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 401 | writew(flags, &prbd->status); |
| 402 | writew(0, &prbd->data_length); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 403 | } |
| 404 | |
Jagan Teki | bc5fb46 | 2016-12-06 00:00:48 +0100 | [diff] [blame] | 405 | static int fec_get_hwaddr(int dev_id, unsigned char *mac) |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 406 | { |
Fabio Estevam | 04fc128 | 2011-12-20 05:46:31 +0000 | [diff] [blame] | 407 | imx_get_mac_from_fuse(dev_id, mac); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 408 | return !is_valid_ethaddr(mac); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 409 | } |
| 410 | |
Jagan Teki | 484f021 | 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 | 889f2e2 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 414 | static int fec_set_hwaddr(struct eth_device *dev) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 415 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 416 | { |
Jagan Teki | 484f021 | 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 | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 419 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 420 | uchar *mac = pdata->enetaddr; |
| 421 | #else |
Stefano Babic | 889f2e2 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 422 | uchar *mac = dev->enetaddr; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 423 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 424 | #endif |
Ilya Yanok | e93a4a5 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 431 | /* Set physical address */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 432 | writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3], |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 433 | &fec->eth->paddr1); |
Ilya Yanok | e93a4a5 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 439 | /* Do initial configuration of the FEC registers */ |
Marek Vasut | 335cbd2 | 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 | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 444 | /* Set interrupt mask register */ |
Marek Vasut | 335cbd2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 445 | writel(0x00000000, &fec->eth->imask); |
| 446 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 447 | /* Clear FEC-Lite interrupt event register(IEVENT) */ |
Marek Vasut | 335cbd2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 448 | writel(0xffffffff, &fec->eth->ievent); |
| 449 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 450 | /* Set FEC-Lite receive control register(R_CNTRL): */ |
Marek Vasut | 335cbd2 | 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 | acc7a28 | 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 | 335cbd2 | 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 | 335cbd2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 460 | |
| 461 | writel(rcntrl, &fec->eth->r_cntrl); |
| 462 | } |
| 463 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 464 | /** |
| 465 | * Start the FEC engine |
| 466 | * @param[in] dev Our device to handle |
| 467 | */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 468 | #ifdef CONFIG_DM_ETH |
| 469 | static int fec_open(struct udevice *dev) |
| 470 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 471 | static int fec_open(struct eth_device *edev) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 472 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 473 | { |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 474 | #ifdef CONFIG_DM_ETH |
| 475 | struct fec_priv *fec = dev_get_priv(dev); |
| 476 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 477 | struct fec_priv *fec = (struct fec_priv *)edev->priv; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 478 | #endif |
Troy Kisky | 0111213 | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 479 | int speed; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 480 | ulong addr, size; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 481 | int i; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 482 | |
| 483 | debug("fec_open: fec_open(dev)\n"); |
| 484 | /* full-duplex, heartbeat disabled */ |
| 485 | writel(1 << 2, &fec->eth->x_cntrl); |
| 486 | fec->rbd_index = 0; |
| 487 | |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 488 | /* Invalidate all descriptors */ |
| 489 | for (i = 0; i < FEC_RBD_NUM - 1; i++) |
| 490 | fec_rbd_clean(0, &fec->rbd_base[i]); |
| 491 | fec_rbd_clean(1, &fec->rbd_base[i]); |
| 492 | |
| 493 | /* Flush the descriptors into RAM */ |
| 494 | size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), |
| 495 | ARCH_DMA_MINALIGN); |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 496 | addr = (ulong)fec->rbd_base; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 497 | flush_dcache_range(addr, addr + size); |
| 498 | |
Troy Kisky | 0111213 | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 499 | #ifdef FEC_QUIRK_ENET_MAC |
Jason Liu | bbcef6c | 2011-12-16 05:17:07 +0000 | [diff] [blame] | 500 | /* Enable ENET HW endian SWAP */ |
| 501 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP, |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 502 | &fec->eth->ecntrl); |
Jason Liu | bbcef6c | 2011-12-16 05:17:07 +0000 | [diff] [blame] | 503 | /* Enable ENET store and forward mode */ |
| 504 | writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD, |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 505 | &fec->eth->x_wmrk); |
Jason Liu | bbcef6c | 2011-12-16 05:17:07 +0000 | [diff] [blame] | 506 | #endif |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 507 | /* Enable FEC-Lite controller */ |
John Rigby | e650e49 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 508 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN, |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 509 | &fec->eth->ecntrl); |
| 510 | |
Philippe Schenker | 7b8ee9b | 2020-03-11 11:52:58 +0100 | [diff] [blame] | 511 | #ifdef FEC_ENET_ENABLE_TXC_DELAY |
| 512 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_TXC_DLY, |
| 513 | &fec->eth->ecntrl); |
| 514 | #endif |
| 515 | |
| 516 | #ifdef FEC_ENET_ENABLE_RXC_DELAY |
| 517 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RXC_DLY, |
| 518 | &fec->eth->ecntrl); |
| 519 | #endif |
| 520 | |
Fabio Estevam | 84c1f52 | 2013-09-13 00:36:27 -0300 | [diff] [blame] | 521 | #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL) |
John Rigby | 99d5fed | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 522 | udelay(100); |
John Rigby | 99d5fed | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 523 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 524 | /* setup the MII gasket for RMII mode */ |
John Rigby | 99d5fed | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 525 | /* disable the gasket */ |
| 526 | writew(0, &fec->eth->miigsk_enr); |
| 527 | |
| 528 | /* wait for the gasket to be disabled */ |
| 529 | while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) |
| 530 | udelay(2); |
| 531 | |
| 532 | /* configure gasket for RMII, 50 MHz, no loopback, and no echo */ |
| 533 | writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr); |
| 534 | |
| 535 | /* re-enable the gasket */ |
| 536 | writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr); |
| 537 | |
| 538 | /* wait until MII gasket is ready */ |
| 539 | int max_loops = 10; |
| 540 | while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) { |
| 541 | if (--max_loops <= 0) { |
| 542 | printf("WAIT for MII Gasket ready timed out\n"); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 547 | |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 548 | #ifdef CONFIG_PHYLIB |
Troy Kisky | 2c42b3c | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 549 | { |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 550 | /* Start up the PHY */ |
Timur Tabi | 4238746 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 551 | int ret = phy_startup(fec->phydev); |
| 552 | |
| 553 | if (ret) { |
| 554 | printf("Could not initialize PHY %s\n", |
| 555 | fec->phydev->dev->name); |
| 556 | return ret; |
| 557 | } |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 558 | speed = fec->phydev->speed; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 559 | } |
Hannes Schmelzer | 5a15c1a | 2016-06-22 12:07:14 +0200 | [diff] [blame] | 560 | #elif CONFIG_FEC_FIXED_SPEED |
| 561 | speed = CONFIG_FEC_FIXED_SPEED; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 562 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 563 | miiphy_wait_aneg(edev); |
Troy Kisky | 0111213 | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 564 | speed = miiphy_speed(edev->name, fec->phy_id); |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 565 | miiphy_duplex(edev->name, fec->phy_id); |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 566 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 567 | |
Troy Kisky | 0111213 | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 568 | #ifdef FEC_QUIRK_ENET_MAC |
| 569 | { |
| 570 | u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED; |
Alison Wang | 89d932a | 2013-05-27 22:55:43 +0000 | [diff] [blame] | 571 | u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T; |
Troy Kisky | 0111213 | 2012-02-07 14:08:46 +0000 | [diff] [blame] | 572 | if (speed == _1000BASET) |
| 573 | ecr |= FEC_ECNTRL_SPEED; |
| 574 | else if (speed != _100BASET) |
| 575 | rcr |= FEC_RCNTRL_RMII_10T; |
| 576 | writel(ecr, &fec->eth->ecntrl); |
| 577 | writel(rcr, &fec->eth->r_cntrl); |
| 578 | } |
| 579 | #endif |
| 580 | debug("%s:Speed=%i\n", __func__, speed); |
| 581 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 582 | /* Enable SmartDMA receive task */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 583 | fec_rx_task_enable(fec); |
| 584 | |
| 585 | udelay(100000); |
| 586 | return 0; |
| 587 | } |
| 588 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 589 | #ifdef CONFIG_DM_ETH |
| 590 | static int fecmxc_init(struct udevice *dev) |
| 591 | #else |
Masahiro Yamada | 940a922 | 2020-06-26 15:13:34 +0900 | [diff] [blame] | 592 | static int fec_init(struct eth_device *dev, struct bd_info *bd) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 593 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 594 | { |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 595 | #ifdef CONFIG_DM_ETH |
| 596 | struct fec_priv *fec = dev_get_priv(dev); |
| 597 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 598 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 599 | #endif |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 600 | u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop; |
| 601 | u8 *i; |
| 602 | ulong addr; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 603 | |
John Rigby | a4a3055 | 2010-10-13 14:31:08 -0600 | [diff] [blame] | 604 | /* Initialize MAC address */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 605 | #ifdef CONFIG_DM_ETH |
| 606 | fecmxc_set_hwaddr(dev); |
| 607 | #else |
John Rigby | a4a3055 | 2010-10-13 14:31:08 -0600 | [diff] [blame] | 608 | fec_set_hwaddr(dev); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 609 | #endif |
John Rigby | a4a3055 | 2010-10-13 14:31:08 -0600 | [diff] [blame] | 610 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 611 | /* Setup transmit descriptors, there are two in total. */ |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 612 | fec_tbd_init(fec); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 613 | |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 614 | /* Setup receive descriptors. */ |
| 615 | fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 616 | |
Marek Vasut | 335cbd2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 617 | fec_reg_setup(fec); |
Marek Vasut | b8f8856 | 2011-09-11 18:05:31 +0000 | [diff] [blame] | 618 | |
benoit.thebaudeau@advans | 551bb36 | 2012-07-19 02:12:58 +0000 | [diff] [blame] | 619 | if (fec->xcv_type != SEVENWIRE) |
Troy Kisky | 5e76265 | 2012-10-22 16:40:41 +0000 | [diff] [blame] | 620 | fec_mii_setspeed(fec->bus->priv); |
Marek Vasut | b8f8856 | 2011-09-11 18:05:31 +0000 | [diff] [blame] | 621 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 622 | /* Set Opcode/Pause Duration Register */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 623 | writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */ |
| 624 | writel(0x2, &fec->eth->x_wmrk); |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 625 | |
| 626 | /* Set multicast address filter */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 627 | writel(0x00000000, &fec->eth->gaddr1); |
| 628 | writel(0x00000000, &fec->eth->gaddr2); |
| 629 | |
Peng Fan | bf8e58b | 2018-01-10 13:20:43 +0800 | [diff] [blame] | 630 | /* Do not access reserved register */ |
Peng Fan | 6146a08 | 2019-04-15 05:18:33 +0000 | [diff] [blame] | 631 | if (!is_mx6ul() && !is_mx6ull() && !is_imx8() && !is_imx8m()) { |
Peng Fan | 13433fd | 2015-08-12 17:46:51 +0800 | [diff] [blame] | 632 | /* clear MIB RAM */ |
| 633 | for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4) |
| 634 | writel(0, i); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 635 | |
Peng Fan | 13433fd | 2015-08-12 17:46:51 +0800 | [diff] [blame] | 636 | /* FIFO receive start register */ |
| 637 | writel(0x520, &fec->eth->r_fstart); |
| 638 | } |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 639 | |
| 640 | /* size and address of each buffer */ |
| 641 | writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr); |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 642 | |
| 643 | addr = (ulong)fec->tbd_base; |
| 644 | writel((uint32_t)addr, &fec->eth->etdsr); |
| 645 | |
| 646 | addr = (ulong)fec->rbd_base; |
| 647 | writel((uint32_t)addr, &fec->eth->erdsr); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 648 | |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 649 | #ifndef CONFIG_PHYLIB |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 650 | if (fec->xcv_type != SEVENWIRE) |
| 651 | miiphy_restart_aneg(dev); |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 652 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 653 | fec_open(dev); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Halt the FEC engine |
| 659 | * @param[in] dev Our device to handle |
| 660 | */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 661 | #ifdef CONFIG_DM_ETH |
| 662 | static void fecmxc_halt(struct udevice *dev) |
| 663 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 664 | static void fec_halt(struct eth_device *dev) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 665 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 666 | { |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 667 | #ifdef CONFIG_DM_ETH |
| 668 | struct fec_priv *fec = dev_get_priv(dev); |
| 669 | #else |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 670 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 671 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 672 | int counter = 0xffff; |
| 673 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 674 | /* issue graceful stop command to the FEC transmitter if necessary */ |
John Rigby | e650e49 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 675 | writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl), |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 676 | &fec->eth->x_cntrl); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 677 | |
| 678 | debug("eth_halt: wait for stop regs\n"); |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 679 | /* wait for graceful stop to register */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 680 | while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA))) |
John Rigby | e650e49 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 681 | udelay(1); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 682 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 683 | /* Disable SmartDMA tasks */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 684 | fec_tx_task_disable(fec); |
| 685 | fec_rx_task_disable(fec); |
| 686 | |
| 687 | /* |
| 688 | * Disable the Ethernet Controller |
| 689 | * Note: this will also reset the BD index counter! |
| 690 | */ |
John Rigby | 99d5fed | 2010-01-25 23:12:57 -0700 | [diff] [blame] | 691 | writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN, |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 692 | &fec->eth->ecntrl); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 693 | fec->rbd_index = 0; |
| 694 | fec->tbd_index = 0; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 695 | debug("eth_halt: done\n"); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Transmit one frame |
| 700 | * @param[in] dev Our ethernet device to handle |
| 701 | * @param[in] packet Pointer to the data to be transmitted |
| 702 | * @param[in] length Data count in bytes |
| 703 | * @return 0 on success |
| 704 | */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 705 | #ifdef CONFIG_DM_ETH |
| 706 | static int fecmxc_send(struct udevice *dev, void *packet, int length) |
| 707 | #else |
Joe Hershberger | 7c31bd1 | 2012-05-21 14:45:27 +0000 | [diff] [blame] | 708 | static int fec_send(struct eth_device *dev, void *packet, int length) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 709 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 710 | { |
| 711 | unsigned int status; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 712 | u32 size; |
| 713 | ulong addr, end; |
Marek Vasut | 5f1631d | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 714 | int timeout = FEC_XFER_TIMEOUT; |
| 715 | int ret = 0; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 716 | |
| 717 | /* |
| 718 | * This routine transmits one frame. This routine only accepts |
| 719 | * 6-byte Ethernet addresses. |
| 720 | */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 721 | #ifdef CONFIG_DM_ETH |
| 722 | struct fec_priv *fec = dev_get_priv(dev); |
| 723 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 724 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 725 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 726 | |
| 727 | /* |
| 728 | * Check for valid length of data. |
| 729 | */ |
| 730 | if ((length > 1500) || (length <= 0)) { |
Stefano Babic | 889f2e2 | 2010-02-01 14:51:30 +0100 | [diff] [blame] | 731 | printf("Payload (%d) too large\n", length); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | /* |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 736 | * Setup the transmit buffer. We are always using the first buffer for |
| 737 | * transmission, the second will be empty and only used to stop the DMA |
| 738 | * engine. We also flush the packet to RAM here to avoid cache trouble. |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 739 | */ |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 740 | #ifdef CONFIG_FEC_MXC_SWAP_PACKET |
Marek Vasut | 6a5fd4c | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 741 | swap_packet((uint32_t *)packet, length); |
| 742 | #endif |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 743 | |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 744 | addr = (ulong)packet; |
Marek Vasut | 4325d24 | 2012-08-26 10:19:21 +0000 | [diff] [blame] | 745 | end = roundup(addr + length, ARCH_DMA_MINALIGN); |
| 746 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 747 | flush_dcache_range(addr, end); |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 748 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 749 | writew(length, &fec->tbd_base[fec->tbd_index].data_length); |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 750 | writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer); |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 751 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 752 | /* |
| 753 | * update BD's status now |
| 754 | * This block: |
| 755 | * - is always the last in a chain (means no chain) |
| 756 | * - should transmitt the CRC |
| 757 | * - might be the last BD in the list, so the address counter should |
| 758 | * wrap (-> keep the WRAP flag) |
| 759 | */ |
| 760 | status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP; |
| 761 | status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY; |
| 762 | writew(status, &fec->tbd_base[fec->tbd_index].status); |
| 763 | |
| 764 | /* |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 765 | * Flush data cache. This code flushes both TX descriptors to RAM. |
| 766 | * After this code, the descriptors will be safely in RAM and we |
| 767 | * can start DMA. |
| 768 | */ |
| 769 | size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 770 | addr = (ulong)fec->tbd_base; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 771 | flush_dcache_range(addr, addr + size); |
| 772 | |
| 773 | /* |
Marek Vasut | d521b3c | 2013-07-12 01:03:04 +0200 | [diff] [blame] | 774 | * Below we read the DMA descriptor's last four bytes back from the |
| 775 | * DRAM. This is important in order to make sure that all WRITE |
| 776 | * operations on the bus that were triggered by previous cache FLUSH |
| 777 | * have completed. |
| 778 | * |
| 779 | * Otherwise, on MX28, it is possible to observe a corruption of the |
| 780 | * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM |
| 781 | * for the bus structure of MX28. The scenario is as follows: |
| 782 | * |
| 783 | * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going |
| 784 | * to DRAM due to flush_dcache_range() |
| 785 | * 2) ARM core writes the FEC registers via AHB_ARB2 |
| 786 | * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3 |
| 787 | * |
| 788 | * Note that 2) does sometimes finish before 1) due to reordering of |
| 789 | * WRITE accesses on the AHB bus, therefore triggering 3) before the |
| 790 | * DMA descriptor is fully written into DRAM. This results in occasional |
| 791 | * corruption of the DMA descriptor. |
| 792 | */ |
| 793 | readl(addr + size - 4); |
| 794 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 795 | /* Enable SmartDMA transmit task */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 796 | fec_tx_task_enable(fec); |
| 797 | |
| 798 | /* |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 799 | * Wait until frame is sent. On each turn of the wait cycle, we must |
| 800 | * invalidate data cache to see what's really in RAM. Also, we need |
| 801 | * barrier here. |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 802 | */ |
Marek Vasut | 9bf7bf0 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 803 | while (--timeout) { |
Marek Vasut | c1582c0 | 2012-08-29 03:49:51 +0000 | [diff] [blame] | 804 | if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR)) |
Marek Vasut | 5f1631d | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 805 | break; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 806 | } |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 807 | |
Fabio Estevam | c34e99f | 2014-08-25 13:34:17 -0300 | [diff] [blame] | 808 | if (!timeout) { |
Marek Vasut | 9bf7bf0 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 809 | ret = -EINVAL; |
Fabio Estevam | c34e99f | 2014-08-25 13:34:17 -0300 | [diff] [blame] | 810 | goto out; |
| 811 | } |
Marek Vasut | 9bf7bf0 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 812 | |
Fabio Estevam | c34e99f | 2014-08-25 13:34:17 -0300 | [diff] [blame] | 813 | /* |
| 814 | * The TDAR bit is cleared when the descriptors are all out from TX |
| 815 | * but on mx6solox we noticed that the READY bit is still not cleared |
| 816 | * right after TDAR. |
| 817 | * These are two distinct signals, and in IC simulation, we found that |
| 818 | * TDAR always gets cleared prior than the READY bit of last BD becomes |
| 819 | * cleared. |
| 820 | * In mx6solox, we use a later version of FEC IP. It looks like that |
| 821 | * this intrinsic behaviour of TDAR bit has changed in this newer FEC |
| 822 | * version. |
| 823 | * |
| 824 | * Fix this by polling the READY bit of BD after the TDAR polling, |
| 825 | * which covers the mx6solox case and does not harm the other SoCs. |
| 826 | */ |
| 827 | timeout = FEC_XFER_TIMEOUT; |
| 828 | while (--timeout) { |
| 829 | invalidate_dcache_range(addr, addr + size); |
| 830 | if (!(readw(&fec->tbd_base[fec->tbd_index].status) & |
| 831 | FEC_TBD_READY)) |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | if (!timeout) |
Marek Vasut | 9bf7bf0 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 836 | ret = -EINVAL; |
| 837 | |
Fabio Estevam | c34e99f | 2014-08-25 13:34:17 -0300 | [diff] [blame] | 838 | out: |
Marek Vasut | 9bf7bf0 | 2012-08-29 03:49:50 +0000 | [diff] [blame] | 839 | debug("fec_send: status 0x%x index %d ret %i\n", |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 840 | readw(&fec->tbd_base[fec->tbd_index].status), |
| 841 | fec->tbd_index, ret); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 842 | /* for next transmission use the other buffer */ |
| 843 | if (fec->tbd_index) |
| 844 | fec->tbd_index = 0; |
| 845 | else |
| 846 | fec->tbd_index = 1; |
| 847 | |
Marek Vasut | 5f1631d | 2012-08-29 03:49:49 +0000 | [diff] [blame] | 848 | return ret; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Pull one frame from the card |
| 853 | * @param[in] dev Our ethernet device to handle |
| 854 | * @return Length of packet read |
| 855 | */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 856 | #ifdef CONFIG_DM_ETH |
| 857 | static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp) |
| 858 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 859 | static int fec_recv(struct eth_device *dev) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 860 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 861 | { |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 862 | #ifdef CONFIG_DM_ETH |
| 863 | struct fec_priv *fec = dev_get_priv(dev); |
| 864 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 865 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 866 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 867 | struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index]; |
| 868 | unsigned long ievent; |
| 869 | int frame_length, len = 0; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 870 | uint16_t bd_status; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 871 | ulong addr, size, end; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 872 | int i; |
Ye Li | bd7e538 | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 873 | |
| 874 | #ifdef CONFIG_DM_ETH |
| 875 | *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE); |
| 876 | if (*packetp == 0) { |
| 877 | printf("%s: error allocating packetp\n", __func__); |
| 878 | return -ENOMEM; |
| 879 | } |
| 880 | #else |
Fabio Estevam | cc95608 | 2013-09-17 23:13:10 -0300 | [diff] [blame] | 881 | ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE); |
Ye Li | bd7e538 | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 882 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 883 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 884 | /* Check if any critical events have happened */ |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 885 | ievent = readl(&fec->eth->ievent); |
| 886 | writel(ievent, &fec->eth->ievent); |
Marek Vasut | 478e2d0 | 2011-10-24 23:40:03 +0000 | [diff] [blame] | 887 | debug("fec_recv: ievent 0x%lx\n", ievent); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 888 | if (ievent & FEC_IEVENT_BABR) { |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 889 | #ifdef CONFIG_DM_ETH |
| 890 | fecmxc_halt(dev); |
| 891 | fecmxc_init(dev); |
| 892 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 893 | fec_halt(dev); |
| 894 | fec_init(dev, fec->bd); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 895 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 896 | printf("some error: 0x%08lx\n", ievent); |
| 897 | return 0; |
| 898 | } |
| 899 | if (ievent & FEC_IEVENT_HBERR) { |
| 900 | /* Heartbeat error */ |
| 901 | writel(0x00000001 | readl(&fec->eth->x_cntrl), |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 902 | &fec->eth->x_cntrl); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 903 | } |
| 904 | if (ievent & FEC_IEVENT_GRA) { |
| 905 | /* Graceful stop complete */ |
| 906 | if (readl(&fec->eth->x_cntrl) & 0x00000001) { |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 907 | #ifdef CONFIG_DM_ETH |
| 908 | fecmxc_halt(dev); |
| 909 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 910 | fec_halt(dev); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 911 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 912 | writel(~0x00000001 & readl(&fec->eth->x_cntrl), |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 913 | &fec->eth->x_cntrl); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 914 | #ifdef CONFIG_DM_ETH |
| 915 | fecmxc_init(dev); |
| 916 | #else |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 917 | fec_init(dev, fec->bd); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 918 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | /* |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 923 | * Read the buffer status. Before the status can be read, the data cache |
| 924 | * must be invalidated, because the data in RAM might have been changed |
| 925 | * by DMA. The descriptors are properly aligned to cachelines so there's |
| 926 | * no need to worry they'd overlap. |
| 927 | * |
| 928 | * WARNING: By invalidating the descriptor here, we also invalidate |
| 929 | * the descriptors surrounding this one. Therefore we can NOT change the |
| 930 | * contents of this descriptor nor the surrounding ones. The problem is |
| 931 | * that in order to mark the descriptor as processed, we need to change |
| 932 | * the descriptor. The solution is to mark the whole cache line when all |
| 933 | * descriptors in the cache line are processed. |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 934 | */ |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 935 | addr = (ulong)rbd; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 936 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 937 | size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
| 938 | invalidate_dcache_range(addr, addr + size); |
| 939 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 940 | bd_status = readw(&rbd->status); |
| 941 | debug("fec_recv: status 0x%x\n", bd_status); |
| 942 | |
| 943 | if (!(bd_status & FEC_RBD_EMPTY)) { |
| 944 | if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) && |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 945 | ((readw(&rbd->data_length) - 4) > 14)) { |
| 946 | /* Get buffer address and size */ |
Albert ARIBAUD \(3ADEV\) | 1342030 | 2015-06-19 14:18:27 +0200 | [diff] [blame] | 947 | addr = readl(&rbd->data_pointer); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 948 | frame_length = readw(&rbd->data_length) - 4; |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 949 | /* Invalidate data cache over the buffer */ |
Marek Vasut | 4325d24 | 2012-08-26 10:19:21 +0000 | [diff] [blame] | 950 | end = roundup(addr + frame_length, ARCH_DMA_MINALIGN); |
| 951 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 952 | invalidate_dcache_range(addr, end); |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 953 | |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 954 | /* Fill the buffer and pass it to upper layers */ |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 955 | #ifdef CONFIG_FEC_MXC_SWAP_PACKET |
Albert ARIBAUD \(3ADEV\) | 1342030 | 2015-06-19 14:18:27 +0200 | [diff] [blame] | 956 | swap_packet((uint32_t *)addr, frame_length); |
Marek Vasut | 6a5fd4c | 2011-11-08 23:18:10 +0000 | [diff] [blame] | 957 | #endif |
Ye Li | bd7e538 | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 958 | |
| 959 | #ifdef CONFIG_DM_ETH |
| 960 | memcpy(*packetp, (char *)addr, frame_length); |
| 961 | #else |
Albert ARIBAUD \(3ADEV\) | 1342030 | 2015-06-19 14:18:27 +0200 | [diff] [blame] | 962 | memcpy(buff, (char *)addr, frame_length); |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 963 | net_process_received_packet(buff, frame_length); |
Ye Li | bd7e538 | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 964 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 965 | len = frame_length; |
| 966 | } else { |
| 967 | if (bd_status & FEC_RBD_ERR) |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 968 | debug("error frame: 0x%08lx 0x%08x\n", |
| 969 | addr, bd_status); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 970 | } |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 971 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 972 | /* |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 973 | * Free the current buffer, restart the engine and move forward |
| 974 | * to the next buffer. Here we check if the whole cacheline of |
| 975 | * descriptors was already processed and if so, we mark it free |
| 976 | * as whole. |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 977 | */ |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 978 | size = RXDESC_PER_CACHELINE - 1; |
| 979 | if ((fec->rbd_index & size) == size) { |
| 980 | i = fec->rbd_index - size; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 981 | addr = (ulong)&fec->rbd_base[i]; |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 982 | for (; i <= fec->rbd_index ; i++) { |
| 983 | fec_rbd_clean(i == (FEC_RBD_NUM - 1), |
| 984 | &fec->rbd_base[i]); |
| 985 | } |
| 986 | flush_dcache_range(addr, |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 987 | addr + ARCH_DMA_MINALIGN); |
Eric Nelson | 3d2f727 | 2012-03-15 18:33:25 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 990 | fec_rx_task_enable(fec); |
| 991 | fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM; |
| 992 | } |
| 993 | debug("fec_recv: stop\n"); |
| 994 | |
| 995 | return len; |
| 996 | } |
| 997 | |
Troy Kisky | 4c2ddec | 2012-10-22 16:40:44 +0000 | [diff] [blame] | 998 | static void fec_set_dev_name(char *dest, int dev_id) |
| 999 | { |
| 1000 | sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id); |
| 1001 | } |
| 1002 | |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1003 | static int fec_alloc_descs(struct fec_priv *fec) |
| 1004 | { |
| 1005 | unsigned int size; |
| 1006 | int i; |
| 1007 | uint8_t *data; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1008 | ulong addr; |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* Allocate TX descriptors. */ |
| 1011 | size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
| 1012 | fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size); |
| 1013 | if (!fec->tbd_base) |
| 1014 | goto err_tx; |
| 1015 | |
| 1016 | /* Allocate RX descriptors. */ |
| 1017 | size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN); |
| 1018 | fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size); |
| 1019 | if (!fec->rbd_base) |
| 1020 | goto err_rx; |
| 1021 | |
| 1022 | memset(fec->rbd_base, 0, size); |
| 1023 | |
| 1024 | /* Allocate RX buffers. */ |
| 1025 | |
| 1026 | /* Maximum RX buffer size. */ |
Fabio Estevam | 8b798b2 | 2014-08-25 13:34:16 -0300 | [diff] [blame] | 1027 | size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN); |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1028 | for (i = 0; i < FEC_RBD_NUM; i++) { |
Fabio Estevam | 8b798b2 | 2014-08-25 13:34:16 -0300 | [diff] [blame] | 1029 | data = memalign(FEC_DMA_RX_MINALIGN, size); |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1030 | if (!data) { |
| 1031 | printf("%s: error allocating rxbuf %d\n", __func__, i); |
| 1032 | goto err_ring; |
| 1033 | } |
| 1034 | |
| 1035 | memset(data, 0, size); |
| 1036 | |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1037 | addr = (ulong)data; |
| 1038 | fec->rbd_base[i].data_pointer = (uint32_t)addr; |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1039 | fec->rbd_base[i].status = FEC_RBD_EMPTY; |
| 1040 | fec->rbd_base[i].data_length = 0; |
| 1041 | /* Flush the buffer to memory. */ |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1042 | flush_dcache_range(addr, addr + size); |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* Mark the last RBD to close the ring. */ |
| 1046 | fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY; |
| 1047 | |
| 1048 | fec->rbd_index = 0; |
| 1049 | fec->tbd_index = 0; |
| 1050 | |
| 1051 | return 0; |
| 1052 | |
| 1053 | err_ring: |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1054 | for (; i >= 0; i--) { |
| 1055 | addr = fec->rbd_base[i].data_pointer; |
| 1056 | free((void *)addr); |
| 1057 | } |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1058 | free(fec->rbd_base); |
| 1059 | err_rx: |
| 1060 | free(fec->tbd_base); |
| 1061 | err_tx: |
| 1062 | return -ENOMEM; |
| 1063 | } |
| 1064 | |
| 1065 | static void fec_free_descs(struct fec_priv *fec) |
| 1066 | { |
| 1067 | int i; |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1068 | ulong addr; |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1069 | |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1070 | for (i = 0; i < FEC_RBD_NUM; i++) { |
| 1071 | addr = fec->rbd_base[i].data_pointer; |
| 1072 | free((void *)addr); |
| 1073 | } |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1074 | free(fec->rbd_base); |
| 1075 | free(fec->tbd_base); |
| 1076 | } |
| 1077 | |
Peng Fan | 0c59c4f | 2018-03-28 20:54:12 +0800 | [diff] [blame] | 1078 | struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1079 | { |
Peng Fan | 0c59c4f | 2018-03-28 20:54:12 +0800 | [diff] [blame] | 1080 | struct ethernet_regs *eth = (struct ethernet_regs *)base_addr; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1081 | struct mii_dev *bus; |
| 1082 | int ret; |
| 1083 | |
| 1084 | bus = mdio_alloc(); |
| 1085 | if (!bus) { |
| 1086 | printf("mdio_alloc failed\n"); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | bus->read = fec_phy_read; |
| 1090 | bus->write = fec_phy_write; |
| 1091 | bus->priv = eth; |
| 1092 | fec_set_dev_name(bus->name, dev_id); |
| 1093 | |
| 1094 | ret = mdio_register(bus); |
| 1095 | if (ret) { |
| 1096 | printf("mdio_register failed\n"); |
| 1097 | free(bus); |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | fec_mii_setspeed(eth); |
| 1101 | return bus; |
| 1102 | } |
| 1103 | |
| 1104 | #ifndef CONFIG_DM_ETH |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1105 | #ifdef CONFIG_PHYLIB |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 1106 | int fec_probe(struct bd_info *bd, int dev_id, uint32_t base_addr, |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1107 | struct mii_dev *bus, struct phy_device *phydev) |
| 1108 | #else |
Masahiro Yamada | 940a922 | 2020-06-26 15:13:34 +0900 | [diff] [blame] | 1109 | static int fec_probe(struct bd_info *bd, int dev_id, uint32_t base_addr, |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1110 | struct mii_dev *bus, int phy_id) |
| 1111 | #endif |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1112 | { |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1113 | struct eth_device *edev; |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1114 | struct fec_priv *fec; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1115 | unsigned char ethaddr[6]; |
Andy Duan | 8f8e458 | 2017-04-10 19:44:35 +0800 | [diff] [blame] | 1116 | char mac[16]; |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1117 | uint32_t start; |
| 1118 | int ret = 0; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1119 | |
| 1120 | /* create and fill edev struct */ |
| 1121 | edev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
| 1122 | if (!edev) { |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1123 | puts("fec_mxc: not enough malloc memory for eth_device\n"); |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1124 | ret = -ENOMEM; |
| 1125 | goto err1; |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | fec = (struct fec_priv *)malloc(sizeof(struct fec_priv)); |
| 1129 | if (!fec) { |
| 1130 | puts("fec_mxc: not enough malloc memory for fec_priv\n"); |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1131 | ret = -ENOMEM; |
| 1132 | goto err2; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1133 | } |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1134 | |
Nobuhiro Iwamatsu | 1843c5b | 2010-10-19 14:03:42 +0900 | [diff] [blame] | 1135 | memset(edev, 0, sizeof(*edev)); |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1136 | memset(fec, 0, sizeof(*fec)); |
| 1137 | |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1138 | ret = fec_alloc_descs(fec); |
| 1139 | if (ret) |
| 1140 | goto err3; |
| 1141 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1142 | edev->priv = fec; |
| 1143 | edev->init = fec_init; |
| 1144 | edev->send = fec_send; |
| 1145 | edev->recv = fec_recv; |
| 1146 | edev->halt = fec_halt; |
Heiko Schocher | 9ada5e6 | 2010-04-27 07:43:52 +0200 | [diff] [blame] | 1147 | edev->write_hwaddr = fec_set_hwaddr; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1148 | |
Ye Li | e267091 | 2018-01-10 13:20:44 +0800 | [diff] [blame] | 1149 | fec->eth = (struct ethernet_regs *)(ulong)base_addr; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1150 | fec->bd = bd; |
| 1151 | |
Marek Vasut | dbb4fce | 2011-09-11 18:05:33 +0000 | [diff] [blame] | 1152 | fec->xcv_type = CONFIG_FEC_XCV_TYPE; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1153 | |
| 1154 | /* Reset chip. */ |
John Rigby | e650e49 | 2010-01-25 23:12:55 -0700 | [diff] [blame] | 1155 | writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl); |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1156 | start = get_timer(0); |
| 1157 | while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) { |
| 1158 | if (get_timer(start) > (CONFIG_SYS_HZ * 5)) { |
Vagrant Cascadian | 259b1fb | 2016-10-23 20:45:19 -0700 | [diff] [blame] | 1159 | printf("FEC MXC: Timeout resetting chip\n"); |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1160 | goto err4; |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1161 | } |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1162 | udelay(10); |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1163 | } |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1164 | |
Marek Vasut | 335cbd2 | 2012-05-01 11:09:41 +0000 | [diff] [blame] | 1165 | fec_reg_setup(fec); |
Troy Kisky | 4c2ddec | 2012-10-22 16:40:44 +0000 | [diff] [blame] | 1166 | fec_set_dev_name(edev->name, dev_id); |
| 1167 | fec->dev_id = (dev_id == -1) ? 0 : dev_id; |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1168 | fec->bus = bus; |
| 1169 | fec_mii_setspeed(bus->priv); |
| 1170 | #ifdef CONFIG_PHYLIB |
| 1171 | fec->phydev = phydev; |
| 1172 | phy_connect_dev(phydev, edev); |
| 1173 | /* Configure phy */ |
| 1174 | phy_config(phydev); |
| 1175 | #else |
Marek Vasut | edcd6c0 | 2011-09-16 01:13:47 +0200 | [diff] [blame] | 1176 | fec->phy_id = phy_id; |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1177 | #endif |
| 1178 | eth_register(edev); |
Andy Duan | 8f8e458 | 2017-04-10 19:44:35 +0800 | [diff] [blame] | 1179 | /* only support one eth device, the index number pointed by dev_id */ |
| 1180 | edev->index = fec->dev_id; |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1181 | |
Andy Duan | 0eaaf83 | 2017-04-10 19:44:34 +0800 | [diff] [blame] | 1182 | if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) { |
| 1183 | debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr); |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1184 | memcpy(edev->enetaddr, ethaddr, 6); |
Andy Duan | 8f8e458 | 2017-04-10 19:44:35 +0800 | [diff] [blame] | 1185 | if (fec->dev_id) |
| 1186 | sprintf(mac, "eth%daddr", fec->dev_id); |
| 1187 | else |
| 1188 | strcpy(mac, "ethaddr"); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1189 | if (!env_get(mac)) |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 1190 | eth_env_set_enetaddr(mac, ethaddr); |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1191 | } |
| 1192 | return ret; |
Marek Vasut | 0388045 | 2013-10-12 20:36:25 +0200 | [diff] [blame] | 1193 | err4: |
| 1194 | fec_free_descs(fec); |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1195 | err3: |
| 1196 | free(fec); |
| 1197 | err2: |
| 1198 | free(edev); |
| 1199 | err1: |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 1203 | int fecmxc_initialize_multi(struct bd_info *bd, int dev_id, int phy_id, |
| 1204 | uint32_t addr) |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1205 | { |
| 1206 | uint32_t base_mii; |
| 1207 | struct mii_dev *bus = NULL; |
| 1208 | #ifdef CONFIG_PHYLIB |
| 1209 | struct phy_device *phydev = NULL; |
| 1210 | #endif |
| 1211 | int ret; |
| 1212 | |
Peng Fan | 075497c | 2020-05-01 22:08:37 +0800 | [diff] [blame] | 1213 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 1214 | if (enet_fused((ulong)addr)) { |
| 1215 | printf("SoC fuse indicates Ethernet@0x%x is unavailable.\n", addr); |
| 1216 | return -ENODEV; |
| 1217 | } |
| 1218 | } |
| 1219 | |
Peng Fan | a65e036 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1220 | #ifdef CONFIG_FEC_MXC_MDIO_BASE |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1221 | /* |
| 1222 | * The i.MX28 has two ethernet interfaces, but they are not equal. |
| 1223 | * Only the first one can access the MDIO bus. |
| 1224 | */ |
Peng Fan | a65e036 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1225 | base_mii = CONFIG_FEC_MXC_MDIO_BASE; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1226 | #else |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1227 | base_mii = addr; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1228 | #endif |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1229 | debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr); |
| 1230 | bus = fec_get_miibus(base_mii, dev_id); |
| 1231 | if (!bus) |
| 1232 | return -ENOMEM; |
Troy Kisky | 2c42b3c | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 1233 | #ifdef CONFIG_PHYLIB |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1234 | phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII); |
Troy Kisky | 2c42b3c | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 1235 | if (!phydev) { |
Måns Rullgård | c6e4a86 | 2015-12-08 15:38:46 +0000 | [diff] [blame] | 1236 | mdio_unregister(bus); |
Troy Kisky | 2c42b3c | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 1237 | free(bus); |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1238 | return -ENOMEM; |
Troy Kisky | 2c42b3c | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 1239 | } |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1240 | ret = fec_probe(bd, dev_id, addr, bus, phydev); |
| 1241 | #else |
| 1242 | ret = fec_probe(bd, dev_id, addr, bus, phy_id); |
Troy Kisky | 2c42b3c | 2012-10-22 16:40:45 +0000 | [diff] [blame] | 1243 | #endif |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1244 | if (ret) { |
| 1245 | #ifdef CONFIG_PHYLIB |
| 1246 | free(phydev); |
| 1247 | #endif |
Måns Rullgård | c6e4a86 | 2015-12-08 15:38:46 +0000 | [diff] [blame] | 1248 | mdio_unregister(bus); |
Troy Kisky | dce4def | 2012-10-22 16:40:46 +0000 | [diff] [blame] | 1249 | free(bus); |
| 1250 | } |
Marek Vasut | 43b1030 | 2011-09-11 18:05:37 +0000 | [diff] [blame] | 1251 | return ret; |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1252 | } |
| 1253 | |
Troy Kisky | 4e0eae6 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1254 | #ifdef CONFIG_FEC_MXC_PHYADDR |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 1255 | int fecmxc_initialize(struct bd_info *bd) |
Troy Kisky | 4e0eae6 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1256 | { |
| 1257 | return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR, |
| 1258 | IMX_FEC_BASE); |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 1259 | } |
Troy Kisky | 4e0eae6 | 2012-10-22 16:40:42 +0000 | [diff] [blame] | 1260 | #endif |
Marek Vasut | 539ecee | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 1261 | |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1262 | #ifndef CONFIG_PHYLIB |
Marek Vasut | 539ecee | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 1263 | int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int)) |
| 1264 | { |
| 1265 | struct fec_priv *fec = (struct fec_priv *)dev->priv; |
| 1266 | fec->mii_postcall = cb; |
| 1267 | return 0; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1268 | } |
| 1269 | #endif |
| 1270 | |
| 1271 | #else |
| 1272 | |
Jagan Teki | 87e7f35 | 2016-12-06 00:00:51 +0100 | [diff] [blame] | 1273 | static int fecmxc_read_rom_hwaddr(struct udevice *dev) |
| 1274 | { |
| 1275 | struct fec_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1276 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 87e7f35 | 2016-12-06 00:00:51 +0100 | [diff] [blame] | 1277 | |
| 1278 | return fec_get_hwaddr(priv->dev_id, pdata->enetaddr); |
| 1279 | } |
| 1280 | |
Ye Li | bd7e538 | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 1281 | static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 1282 | { |
| 1283 | if (packet) |
| 1284 | free(packet); |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1289 | static const struct eth_ops fecmxc_ops = { |
| 1290 | .start = fecmxc_init, |
| 1291 | .send = fecmxc_send, |
| 1292 | .recv = fecmxc_recv, |
Ye Li | bd7e538 | 2018-03-28 20:54:11 +0800 | [diff] [blame] | 1293 | .free_pkt = fecmxc_free_pkt, |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1294 | .stop = fecmxc_halt, |
| 1295 | .write_hwaddr = fecmxc_set_hwaddr, |
Jagan Teki | 87e7f35 | 2016-12-06 00:00:51 +0100 | [diff] [blame] | 1296 | .read_rom_hwaddr = fecmxc_read_rom_hwaddr, |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1297 | }; |
| 1298 | |
Fabio Estevam | c9eb520 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1299 | static int device_get_phy_addr(struct fec_priv *priv, struct udevice *dev) |
Martyn Welch | d1ac23f | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1300 | { |
| 1301 | struct ofnode_phandle_args phandle_args; |
| 1302 | int reg; |
| 1303 | |
| 1304 | if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, |
| 1305 | &phandle_args)) { |
| 1306 | debug("Failed to find phy-handle"); |
| 1307 | return -ENODEV; |
| 1308 | } |
| 1309 | |
Fabio Estevam | c9eb520 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1310 | priv->phy_of_node = phandle_args.node; |
| 1311 | |
Martyn Welch | d1ac23f | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1312 | reg = ofnode_read_u32_default(phandle_args.node, "reg", 0); |
| 1313 | |
| 1314 | return reg; |
| 1315 | } |
| 1316 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1317 | static int fec_phy_init(struct fec_priv *priv, struct udevice *dev) |
| 1318 | { |
| 1319 | struct phy_device *phydev; |
Martyn Welch | d1ac23f | 2018-12-11 11:34:45 +0000 | [diff] [blame] | 1320 | int addr; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1321 | |
Fabio Estevam | c9eb520 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1322 | addr = device_get_phy_addr(priv, dev); |
Lukasz Majewski | 07b75a3 | 2018-04-15 21:45:54 +0200 | [diff] [blame] | 1323 | #ifdef CONFIG_FEC_MXC_PHYADDR |
Hannes Schmelzer | f769430 | 2019-02-15 10:30:18 +0100 | [diff] [blame] | 1324 | addr = CONFIG_FEC_MXC_PHYADDR; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1325 | #endif |
| 1326 | |
Hannes Schmelzer | f769430 | 2019-02-15 10:30:18 +0100 | [diff] [blame] | 1327 | phydev = phy_connect(priv->bus, addr, dev, priv->interface); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1328 | if (!phydev) |
| 1329 | return -ENODEV; |
| 1330 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1331 | priv->phydev = phydev; |
Fabio Estevam | c9eb520 | 2020-06-18 20:21:18 -0300 | [diff] [blame] | 1332 | priv->phydev->node = priv->phy_of_node; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1333 | phy_config(phydev); |
| 1334 | |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1338 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1339 | /* FEC GPIO reset */ |
| 1340 | static void fec_gpio_reset(struct fec_priv *priv) |
| 1341 | { |
| 1342 | debug("fec_gpio_reset: fec_gpio_reset(dev)\n"); |
| 1343 | if (dm_gpio_is_valid(&priv->phy_reset_gpio)) { |
| 1344 | dm_gpio_set_value(&priv->phy_reset_gpio, 1); |
Martin Fuzzey | 9c3f97a | 2018-10-04 19:59:18 +0200 | [diff] [blame] | 1345 | mdelay(priv->reset_delay); |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1346 | dm_gpio_set_value(&priv->phy_reset_gpio, 0); |
Andrejs Cainikovs | 24b6aac | 2019-03-01 13:27:59 +0000 | [diff] [blame] | 1347 | if (priv->reset_post_delay) |
| 1348 | mdelay(priv->reset_post_delay); |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | #endif |
| 1352 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1353 | static int fecmxc_probe(struct udevice *dev) |
| 1354 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1355 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1356 | struct fec_priv *priv = dev_get_priv(dev); |
| 1357 | struct mii_dev *bus = NULL; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1358 | uint32_t start; |
| 1359 | int ret; |
| 1360 | |
Peng Fan | 075497c | 2020-05-01 22:08:37 +0800 | [diff] [blame] | 1361 | if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) { |
| 1362 | if (enet_fused((ulong)priv->eth)) { |
| 1363 | printf("SoC fuse indicates Ethernet@0x%lx is unavailable.\n", (ulong)priv->eth); |
| 1364 | return -ENODEV; |
| 1365 | } |
| 1366 | } |
| 1367 | |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 1368 | if (IS_ENABLED(CONFIG_IMX8)) { |
| 1369 | ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk); |
| 1370 | if (ret < 0) { |
| 1371 | debug("Can't get FEC ipg clk: %d\n", ret); |
| 1372 | return ret; |
| 1373 | } |
| 1374 | ret = clk_enable(&priv->ipg_clk); |
| 1375 | if (ret < 0) { |
| 1376 | debug("Can't enable FEC ipg clk: %d\n", ret); |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | priv->clk_rate = clk_get_rate(&priv->ipg_clk); |
Peng Fan | dcf5e1b | 2019-10-25 09:48:02 +0000 | [diff] [blame] | 1381 | } else if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 1382 | ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk); |
| 1383 | if (ret < 0) { |
| 1384 | debug("Can't get FEC ipg clk: %d\n", ret); |
| 1385 | return ret; |
| 1386 | } |
| 1387 | ret = clk_enable(&priv->ipg_clk); |
| 1388 | if(ret) |
| 1389 | return ret; |
| 1390 | |
| 1391 | ret = clk_get_by_name(dev, "ahb", &priv->ahb_clk); |
| 1392 | if (ret < 0) { |
| 1393 | debug("Can't get FEC ahb clk: %d\n", ret); |
| 1394 | return ret; |
| 1395 | } |
| 1396 | ret = clk_enable(&priv->ahb_clk); |
| 1397 | if (ret) |
| 1398 | return ret; |
| 1399 | |
| 1400 | ret = clk_get_by_name(dev, "enet_out", &priv->clk_enet_out); |
| 1401 | if (!ret) { |
| 1402 | ret = clk_enable(&priv->clk_enet_out); |
| 1403 | if (ret) |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | ret = clk_get_by_name(dev, "enet_clk_ref", &priv->clk_ref); |
| 1408 | if (!ret) { |
| 1409 | ret = clk_enable(&priv->clk_ref); |
| 1410 | if (ret) |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
| 1414 | ret = clk_get_by_name(dev, "ptp", &priv->clk_ptp); |
| 1415 | if (!ret) { |
| 1416 | ret = clk_enable(&priv->clk_ptp); |
| 1417 | if (ret) |
| 1418 | return ret; |
| 1419 | } |
| 1420 | |
| 1421 | priv->clk_rate = clk_get_rate(&priv->ipg_clk); |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1424 | ret = fec_alloc_descs(priv); |
| 1425 | if (ret) |
| 1426 | return ret; |
| 1427 | |
Martin Fuzzey | 9a6a2c9 | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1428 | #ifdef CONFIG_DM_REGULATOR |
| 1429 | if (priv->phy_supply) { |
Adam Ford | b3301b6 | 2019-01-15 11:26:48 -0600 | [diff] [blame] | 1430 | ret = regulator_set_enable(priv->phy_supply, true); |
Martin Fuzzey | 9a6a2c9 | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1431 | if (ret) { |
| 1432 | printf("%s: Error enabling phy supply\n", dev->name); |
| 1433 | return ret; |
| 1434 | } |
| 1435 | } |
| 1436 | #endif |
| 1437 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1438 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1439 | fec_gpio_reset(priv); |
| 1440 | #endif |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1441 | /* Reset chip. */ |
Jagan Teki | c6cd8d5 | 2016-12-06 00:00:50 +0100 | [diff] [blame] | 1442 | writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET, |
| 1443 | &priv->eth->ecntrl); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1444 | start = get_timer(0); |
| 1445 | while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) { |
| 1446 | if (get_timer(start) > (CONFIG_SYS_HZ * 5)) { |
| 1447 | printf("FEC MXC: Timeout reseting chip\n"); |
| 1448 | goto err_timeout; |
| 1449 | } |
| 1450 | udelay(10); |
| 1451 | } |
| 1452 | |
| 1453 | fec_reg_setup(priv); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1454 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1455 | priv->dev_id = dev_seq(dev); |
Ye Li | ad122b7 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1456 | |
| 1457 | #ifdef CONFIG_DM_ETH_PHY |
| 1458 | bus = eth_phy_get_mdio_bus(dev); |
| 1459 | #endif |
| 1460 | |
| 1461 | if (!bus) { |
Peng Fan | a65e036 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1462 | #ifdef CONFIG_FEC_MXC_MDIO_BASE |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1463 | bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, |
| 1464 | dev_seq(dev)); |
Peng Fan | a65e036 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1465 | #else |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1466 | bus = fec_get_miibus((ulong)priv->eth, dev_seq(dev)); |
Peng Fan | a65e036 | 2018-03-28 20:54:14 +0800 | [diff] [blame] | 1467 | #endif |
Ye Li | ad122b7 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1468 | } |
Lothar Waßmann | d33e9ee | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1469 | if (!bus) { |
| 1470 | ret = -ENOMEM; |
| 1471 | goto err_mii; |
| 1472 | } |
| 1473 | |
Ye Li | ad122b7 | 2020-05-03 22:41:15 +0800 | [diff] [blame] | 1474 | #ifdef CONFIG_DM_ETH_PHY |
| 1475 | eth_phy_set_mdio_bus(dev, bus); |
| 1476 | #endif |
| 1477 | |
Lothar Waßmann | d33e9ee | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1478 | priv->bus = bus; |
Lothar Waßmann | d33e9ee | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1479 | priv->interface = pdata->phy_interface; |
Martin Fuzzey | f08eb3d | 2018-10-04 19:59:21 +0200 | [diff] [blame] | 1480 | switch (priv->interface) { |
| 1481 | case PHY_INTERFACE_MODE_MII: |
| 1482 | priv->xcv_type = MII100; |
| 1483 | break; |
| 1484 | case PHY_INTERFACE_MODE_RMII: |
| 1485 | priv->xcv_type = RMII; |
| 1486 | break; |
| 1487 | case PHY_INTERFACE_MODE_RGMII: |
| 1488 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 1489 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 1490 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 1491 | priv->xcv_type = RGMII; |
| 1492 | break; |
| 1493 | default: |
| 1494 | priv->xcv_type = CONFIG_FEC_XCV_TYPE; |
| 1495 | printf("Unsupported interface type %d defaulting to %d\n", |
| 1496 | priv->interface, priv->xcv_type); |
| 1497 | break; |
| 1498 | } |
| 1499 | |
Lothar Waßmann | d33e9ee | 2017-06-27 15:23:16 +0200 | [diff] [blame] | 1500 | ret = fec_phy_init(priv, dev); |
| 1501 | if (ret) |
| 1502 | goto err_phy; |
| 1503 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1504 | return 0; |
| 1505 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1506 | err_phy: |
| 1507 | mdio_unregister(bus); |
| 1508 | free(bus); |
| 1509 | err_mii: |
Ye Li | 5fa556c | 2018-03-28 20:54:16 +0800 | [diff] [blame] | 1510 | err_timeout: |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1511 | fec_free_descs(priv); |
| 1512 | return ret; |
Marek Vasut | 539ecee | 2011-09-11 18:05:36 +0000 | [diff] [blame] | 1513 | } |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1514 | |
| 1515 | static int fecmxc_remove(struct udevice *dev) |
| 1516 | { |
| 1517 | struct fec_priv *priv = dev_get_priv(dev); |
| 1518 | |
| 1519 | free(priv->phydev); |
| 1520 | fec_free_descs(priv); |
| 1521 | mdio_unregister(priv->bus); |
| 1522 | mdio_free(priv->bus); |
| 1523 | |
Martin Fuzzey | 9a6a2c9 | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1524 | #ifdef CONFIG_DM_REGULATOR |
| 1525 | if (priv->phy_supply) |
| 1526 | regulator_set_enable(priv->phy_supply, false); |
| 1527 | #endif |
| 1528 | |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1529 | return 0; |
| 1530 | } |
| 1531 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1532 | static int fecmxc_of_to_plat(struct udevice *dev) |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1533 | { |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1534 | int ret = 0; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1535 | struct eth_pdata *pdata = dev_get_plat(dev); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1536 | struct fec_priv *priv = dev_get_priv(dev); |
| 1537 | const char *phy_mode; |
| 1538 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 1539 | pdata->iobase = dev_read_addr(dev); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1540 | priv->eth = (struct ethernet_regs *)pdata->iobase; |
| 1541 | |
| 1542 | pdata->phy_interface = -1; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 1543 | phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode", |
| 1544 | NULL); |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1545 | if (phy_mode) |
| 1546 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 1547 | if (pdata->phy_interface == -1) { |
| 1548 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 1549 | return -EINVAL; |
| 1550 | } |
| 1551 | |
Martin Fuzzey | 9a6a2c9 | 2018-10-04 19:59:20 +0200 | [diff] [blame] | 1552 | #ifdef CONFIG_DM_REGULATOR |
| 1553 | device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply); |
| 1554 | #endif |
| 1555 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1556 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1557 | ret = gpio_request_by_name(dev, "phy-reset-gpios", 0, |
Martin Fuzzey | 185e3b8 | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1558 | &priv->phy_reset_gpio, GPIOD_IS_OUT); |
| 1559 | if (ret < 0) |
| 1560 | return 0; /* property is optional, don't return error! */ |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1561 | |
Martin Fuzzey | 185e3b8 | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1562 | priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1); |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1563 | if (priv->reset_delay > 1000) { |
Martin Fuzzey | 185e3b8 | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1564 | printf("FEC MXC: phy reset duration should be <= 1000ms\n"); |
| 1565 | /* property value wrong, use default value */ |
| 1566 | priv->reset_delay = 1; |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1567 | } |
Andrejs Cainikovs | 24b6aac | 2019-03-01 13:27:59 +0000 | [diff] [blame] | 1568 | |
| 1569 | priv->reset_post_delay = dev_read_u32_default(dev, |
| 1570 | "phy-reset-post-delay", |
| 1571 | 0); |
| 1572 | if (priv->reset_post_delay > 1000) { |
| 1573 | printf("FEC MXC: phy reset post delay should be <= 1000ms\n"); |
| 1574 | /* property value wrong, use default value */ |
| 1575 | priv->reset_post_delay = 0; |
| 1576 | } |
Michael Trimarchi | 0e5cccf | 2018-06-17 15:22:39 +0200 | [diff] [blame] | 1577 | #endif |
| 1578 | |
Martin Fuzzey | 185e3b8 | 2018-10-04 19:59:19 +0200 | [diff] [blame] | 1579 | return 0; |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | static const struct udevice_id fecmxc_ids[] = { |
Lukasz Majewski | 8a8f5a6 | 2019-06-19 17:31:03 +0200 | [diff] [blame] | 1583 | { .compatible = "fsl,imx28-fec" }, |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1584 | { .compatible = "fsl,imx6q-fec" }, |
Peng Fan | 5640630 | 2018-03-28 20:54:15 +0800 | [diff] [blame] | 1585 | { .compatible = "fsl,imx6sl-fec" }, |
| 1586 | { .compatible = "fsl,imx6sx-fec" }, |
| 1587 | { .compatible = "fsl,imx6ul-fec" }, |
Lukasz Majewski | 4731122 | 2018-04-15 21:54:22 +0200 | [diff] [blame] | 1588 | { .compatible = "fsl,imx53-fec" }, |
Anatolij Gustschin | b71fc5e | 2018-10-18 16:15:11 +0200 | [diff] [blame] | 1589 | { .compatible = "fsl,imx7d-fec" }, |
Lukasz Majewski | 6b94b0e | 2019-02-13 22:46:38 +0100 | [diff] [blame] | 1590 | { .compatible = "fsl,mvf600-fec" }, |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1591 | { } |
| 1592 | }; |
| 1593 | |
| 1594 | U_BOOT_DRIVER(fecmxc_gem) = { |
| 1595 | .name = "fecmxc", |
| 1596 | .id = UCLASS_ETH, |
| 1597 | .of_match = fecmxc_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1598 | .of_to_plat = fecmxc_of_to_plat, |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1599 | .probe = fecmxc_probe, |
| 1600 | .remove = fecmxc_remove, |
| 1601 | .ops = &fecmxc_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1602 | .priv_auto = sizeof(struct fec_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1603 | .plat_auto = sizeof(struct eth_pdata), |
Jagan Teki | 484f021 | 2016-12-06 00:00:49 +0100 | [diff] [blame] | 1604 | }; |
Troy Kisky | 2000c66 | 2012-02-07 14:08:47 +0000 | [diff] [blame] | 1605 | #endif |