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