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