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