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