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