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