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