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