blob: 5c3b90fe003a7ece6c4feb02dd12d247247a239a [file] [log] [blame]
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001/*
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>
34
35DECLARE_GLOBAL_DATA_PTR;
36
37#ifndef CONFIG_MII
38#error "CONFIG_MII has to be defined!"
39#endif
40
Eric Nelson3d2f7272012-03-15 18:33:25 +000041#ifndef CONFIG_FEC_XCV_TYPE
42#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasutdbb4fce2011-09-11 18:05:33 +000043#endif
44
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000045/*
46 * The i.MX28 operates with packets in big endian. We need to swap them before
47 * sending and after receiving.
48 */
Eric Nelson3d2f7272012-03-15 18:33:25 +000049#ifdef CONFIG_MX28
50#define CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000051#endif
52
Eric Nelson3d2f7272012-03-15 18:33:25 +000053#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
54
55/* Check various alignment issues at compile time */
56#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
57#error "ARCH_DMA_MINALIGN must be multiple of 16!"
58#endif
59
60#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
61 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
62#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
63#endif
64
Ilya Yanoke93a4a52009-07-21 19:32:21 +040065#undef DEBUG
66
67struct nbuf {
68 uint8_t data[1500]; /**< actual data */
69 int length; /**< actual length */
70 int used; /**< buffer in use or not */
71 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
72};
73
Eric Nelson3d2f7272012-03-15 18:33:25 +000074#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000075static void swap_packet(uint32_t *packet, int length)
76{
77 int i;
78
79 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
80 packet[i] = __swab32(packet[i]);
81}
82#endif
83
Ilya Yanoke93a4a52009-07-21 19:32:21 +040084/*
85 * MII-interface related functions
86 */
Troy Kisky2000c662012-02-07 14:08:47 +000087static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
88 uint8_t regAddr)
Ilya Yanoke93a4a52009-07-21 19:32:21 +040089{
Ilya Yanoke93a4a52009-07-21 19:32:21 +040090 uint32_t reg; /* convenient holder for the PHY register */
91 uint32_t phy; /* convenient holder for the PHY */
92 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +000093 int val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +040094
95 /*
96 * reading from any PHY's register is done by properly
97 * programming the FEC's MII data register.
98 */
Marek Vasutbf2386b2011-09-11 18:05:34 +000099 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400100 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
101 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
102
103 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000104 phy | reg, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400105
106 /*
107 * wait for the related interrupt
108 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000109 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000110 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400111 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
112 printf("Read MDIO failed...\n");
113 return -1;
114 }
115 }
116
117 /*
118 * clear mii interrupt bit
119 */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000120 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400121
122 /*
123 * it's now safe to read the PHY's register
124 */
Troy Kisky2000c662012-02-07 14:08:47 +0000125 val = (unsigned short)readl(&eth->mii_data);
126 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
127 regAddr, val);
128 return val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400129}
130
Stefano Babic889f2e22010-02-01 14:51:30 +0100131static void fec_mii_setspeed(struct fec_priv *fec)
132{
133 /*
134 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
135 * and do not drop the Preamble.
136 */
137 writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
138 &fec->eth->mii_speed);
Troy Kisky2000c662012-02-07 14:08:47 +0000139 debug("%s: mii_speed %08x\n", __func__, readl(&fec->eth->mii_speed));
Stefano Babic889f2e22010-02-01 14:51:30 +0100140}
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400141
Troy Kisky2000c662012-02-07 14:08:47 +0000142static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
143 uint8_t regAddr, uint16_t data)
144{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400145 uint32_t reg; /* convenient holder for the PHY register */
146 uint32_t phy; /* convenient holder for the PHY */
147 uint32_t start;
148
149 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
150 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
151
152 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000153 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400154
155 /*
156 * wait for the MII interrupt
157 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000158 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000159 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400160 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
161 printf("Write MDIO failed...\n");
162 return -1;
163 }
164 }
165
166 /*
167 * clear MII interrupt bit
168 */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000169 writel(FEC_IEVENT_MII, &eth->ievent);
Troy Kisky2000c662012-02-07 14:08:47 +0000170 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400171 regAddr, data);
172
173 return 0;
174}
175
Troy Kisky2000c662012-02-07 14:08:47 +0000176int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
177{
178 return fec_mdio_read(bus->priv, phyAddr, regAddr);
179}
180
181int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
182 u16 data)
183{
184 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
185}
186
187#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400188static int miiphy_restart_aneg(struct eth_device *dev)
189{
Stefano Babicd6228172012-02-22 00:24:35 +0000190 int ret = 0;
191#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasutedcd6c02011-09-16 01:13:47 +0200192 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000193 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200194
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400195 /*
196 * Wake up from sleep if necessary
197 * Reset PHY, then delay 300ns
198 */
John Rigbye650e492010-01-25 23:12:55 -0700199#ifdef CONFIG_MX27
Troy Kisky2000c662012-02-07 14:08:47 +0000200 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700201#endif
Troy Kisky2000c662012-02-07 14:08:47 +0000202 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400203 udelay(1000);
204
205 /*
206 * Set the auto-negotiation advertisement register bits
207 */
Troy Kisky2000c662012-02-07 14:08:47 +0000208 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Mike Frysingerd63ee712010-12-23 15:40:12 -0500209 LPA_100FULL | LPA_100HALF | LPA_10FULL |
210 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky2000c662012-02-07 14:08:47 +0000211 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Mike Frysingerd63ee712010-12-23 15:40:12 -0500212 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut539ecee2011-09-11 18:05:36 +0000213
214 if (fec->mii_postcall)
215 ret = fec->mii_postcall(fec->phy_id);
216
Stefano Babicd6228172012-02-22 00:24:35 +0000217#endif
Marek Vasut539ecee2011-09-11 18:05:36 +0000218 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400219}
220
221static int miiphy_wait_aneg(struct eth_device *dev)
222{
223 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000224 int status;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200225 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000226 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400227
228 /*
229 * Wait for AN completion
230 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000231 start = get_timer(0);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400232 do {
233 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
234 printf("%s: Autonegotiation timeout\n", dev->name);
235 return -1;
236 }
237
Troy Kisky2000c662012-02-07 14:08:47 +0000238 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
239 if (status < 0) {
240 printf("%s: Autonegotiation failed. status: %d\n",
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400241 dev->name, status);
242 return -1;
243 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500244 } while (!(status & BMSR_LSTATUS));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400245
246 return 0;
247}
Troy Kisky2000c662012-02-07 14:08:47 +0000248#endif
249
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400250static int fec_rx_task_enable(struct fec_priv *fec)
251{
252 writel(1 << 24, &fec->eth->r_des_active);
253 return 0;
254}
255
256static int fec_rx_task_disable(struct fec_priv *fec)
257{
258 return 0;
259}
260
261static int fec_tx_task_enable(struct fec_priv *fec)
262{
263 writel(1 << 24, &fec->eth->x_des_active);
264 return 0;
265}
266
267static int fec_tx_task_disable(struct fec_priv *fec)
268{
269 return 0;
270}
271
272/**
273 * Initialize receive task's buffer descriptors
274 * @param[in] fec all we know about the device yet
275 * @param[in] count receive buffer count to be allocated
Eric Nelson3d2f7272012-03-15 18:33:25 +0000276 * @param[in] dsize desired size of each receive buffer
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400277 * @return 0 on success
278 *
279 * For this task we need additional memory for the data buffers. And each
280 * data buffer requires some alignment. Thy must be aligned to a specific
Eric Nelson3d2f7272012-03-15 18:33:25 +0000281 * boundary each.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400282 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000283static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400284{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000285 uint32_t size;
286 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400287
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400288 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000289 * Allocate memory for the buffers. This allocation respects the
290 * alignment
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400291 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000292 size = roundup(dsize, ARCH_DMA_MINALIGN);
293 for (i = 0; i < count; i++) {
294 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
295 if (data_ptr == 0) {
296 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
297 size);
298 if (!data) {
299 printf("%s: error allocating rxbuf %d\n",
300 __func__, i);
301 goto err;
302 }
303 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
304 } /* needs allocation */
305 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
306 writew(0, &fec->rbd_base[i].data_length);
307 }
308
309 /* Mark the last RBD to close the ring. */
310 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400311 fec->rbd_index = 0;
312
313 return 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000314
315err:
316 for (; i >= 0; i--) {
317 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
318 free((void *)data_ptr);
319 }
320
321 return -ENOMEM;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400322}
323
324/**
325 * Initialize transmit task's buffer descriptors
326 * @param[in] fec all we know about the device yet
327 *
328 * Transmit buffers are created externally. We only have to init the BDs here.\n
329 * Note: There is a race condition in the hardware. When only one BD is in
330 * use it must be marked with the WRAP bit to use it for every transmitt.
331 * This bit in combination with the READY bit results into double transmit
332 * of each data buffer. It seems the state machine checks READY earlier then
333 * resetting it after the first transfer.
334 * Using two BDs solves this issue.
335 */
336static void fec_tbd_init(struct fec_priv *fec)
337{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000338 unsigned addr = (unsigned)fec->tbd_base;
339 unsigned size = roundup(2 * sizeof(struct fec_bd),
340 ARCH_DMA_MINALIGN);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400341 writew(0x0000, &fec->tbd_base[0].status);
342 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
343 fec->tbd_index = 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000344 flush_dcache_range(addr, addr+size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400345}
346
347/**
348 * Mark the given read buffer descriptor as free
349 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
350 * @param[in] pRbd buffer descriptor to mark free again
351 */
352static void fec_rbd_clean(int last, struct fec_bd *pRbd)
353{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000354 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400355 if (last)
Eric Nelson3d2f7272012-03-15 18:33:25 +0000356 flags |= FEC_RBD_WRAP;
357 writew(flags, &pRbd->status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400358 writew(0, &pRbd->data_length);
359}
360
Fabio Estevam04fc1282011-12-20 05:46:31 +0000361static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
362 unsigned char *mac)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400363{
Fabio Estevam04fc1282011-12-20 05:46:31 +0000364 imx_get_mac_from_fuse(dev_id, mac);
Eric Jarrigecc0d0d42010-04-16 00:03:19 +0200365 return !is_valid_ether_addr(mac);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400366}
367
Stefano Babic889f2e22010-02-01 14:51:30 +0100368static int fec_set_hwaddr(struct eth_device *dev)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400369{
Stefano Babic889f2e22010-02-01 14:51:30 +0100370 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400371 struct fec_priv *fec = (struct fec_priv *)dev->priv;
372
373 writel(0, &fec->eth->iaddr1);
374 writel(0, &fec->eth->iaddr2);
375 writel(0, &fec->eth->gaddr1);
376 writel(0, &fec->eth->gaddr2);
377
378 /*
379 * Set physical address
380 */
381 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
382 &fec->eth->paddr1);
383 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
384
385 return 0;
386}
387
Troy Kisky2000c662012-02-07 14:08:47 +0000388static void fec_eth_phy_config(struct eth_device *dev)
389{
390#ifdef CONFIG_PHYLIB
391 struct fec_priv *fec = (struct fec_priv *)dev->priv;
392 struct phy_device *phydev;
393
394 phydev = phy_connect(fec->bus, fec->phy_id, dev,
395 PHY_INTERFACE_MODE_RGMII);
396 if (phydev) {
397 fec->phydev = phydev;
398 phy_config(phydev);
399 }
400#endif
401}
402
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400403/**
404 * Start the FEC engine
405 * @param[in] dev Our device to handle
406 */
407static int fec_open(struct eth_device *edev)
408{
409 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Troy Kisky01112132012-02-07 14:08:46 +0000410 int speed;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000411 uint32_t addr, size;
412 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400413
414 debug("fec_open: fec_open(dev)\n");
415 /* full-duplex, heartbeat disabled */
416 writel(1 << 2, &fec->eth->x_cntrl);
417 fec->rbd_index = 0;
418
Eric Nelson3d2f7272012-03-15 18:33:25 +0000419 /* Invalidate all descriptors */
420 for (i = 0; i < FEC_RBD_NUM - 1; i++)
421 fec_rbd_clean(0, &fec->rbd_base[i]);
422 fec_rbd_clean(1, &fec->rbd_base[i]);
423
424 /* Flush the descriptors into RAM */
425 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
426 ARCH_DMA_MINALIGN);
427 addr = (uint32_t)fec->rbd_base;
428 flush_dcache_range(addr, addr + size);
429
Troy Kisky01112132012-02-07 14:08:46 +0000430#ifdef FEC_QUIRK_ENET_MAC
Jason Liubbcef6c2011-12-16 05:17:07 +0000431 /* Enable ENET HW endian SWAP */
432 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
433 &fec->eth->ecntrl);
434 /* Enable ENET store and forward mode */
435 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
436 &fec->eth->x_wmrk);
437#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400438 /*
439 * Enable FEC-Lite controller
440 */
John Rigbye650e492010-01-25 23:12:55 -0700441 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
442 &fec->eth->ecntrl);
Liu Hui-R64343c11cf872011-01-03 22:27:36 +0000443#if defined(CONFIG_MX25) || defined(CONFIG_MX53)
John Rigby99d5fed2010-01-25 23:12:57 -0700444 udelay(100);
445 /*
446 * setup the MII gasket for RMII mode
447 */
448
449 /* disable the gasket */
450 writew(0, &fec->eth->miigsk_enr);
451
452 /* wait for the gasket to be disabled */
453 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
454 udelay(2);
455
456 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
457 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
458
459 /* re-enable the gasket */
460 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
461
462 /* wait until MII gasket is ready */
463 int max_loops = 10;
464 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
465 if (--max_loops <= 0) {
466 printf("WAIT for MII Gasket ready timed out\n");
467 break;
468 }
469 }
470#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400471
Troy Kisky2000c662012-02-07 14:08:47 +0000472#ifdef CONFIG_PHYLIB
473 if (!fec->phydev)
474 fec_eth_phy_config(edev);
475 if (fec->phydev) {
476 /* Start up the PHY */
477 phy_startup(fec->phydev);
478 speed = fec->phydev->speed;
479 } else {
480 speed = _100BASET;
481 }
482#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400483 miiphy_wait_aneg(edev);
Troy Kisky01112132012-02-07 14:08:46 +0000484 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasutedcd6c02011-09-16 01:13:47 +0200485 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky2000c662012-02-07 14:08:47 +0000486#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400487
Troy Kisky01112132012-02-07 14:08:46 +0000488#ifdef FEC_QUIRK_ENET_MAC
489 {
490 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
491 u32 rcr = (readl(&fec->eth->r_cntrl) &
492 ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
493 FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
494 if (speed == _1000BASET)
495 ecr |= FEC_ECNTRL_SPEED;
496 else if (speed != _100BASET)
497 rcr |= FEC_RCNTRL_RMII_10T;
498 writel(ecr, &fec->eth->ecntrl);
499 writel(rcr, &fec->eth->r_cntrl);
500 }
501#endif
502 debug("%s:Speed=%i\n", __func__, speed);
503
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400504 /*
505 * Enable SmartDMA receive task
506 */
507 fec_rx_task_enable(fec);
508
509 udelay(100000);
510 return 0;
511}
512
513static int fec_init(struct eth_device *dev, bd_t* bd)
514{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400515 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200516 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
Marek Vasutb8f88562011-09-11 18:05:31 +0000517 uint32_t rcntrl;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000518 uint32_t size;
519 int i, ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400520
John Rigbya4a30552010-10-13 14:31:08 -0600521 /* Initialize MAC address */
522 fec_set_hwaddr(dev);
523
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400524 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000525 * Allocate transmit descriptors, there are two in total. This
526 * allocation respects cache alignment.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400527 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000528 if (!fec->tbd_base) {
529 size = roundup(2 * sizeof(struct fec_bd),
530 ARCH_DMA_MINALIGN);
531 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
532 if (!fec->tbd_base) {
533 ret = -ENOMEM;
534 goto err1;
535 }
536 memset(fec->tbd_base, 0, size);
537 fec_tbd_init(fec);
538 flush_dcache_range((unsigned)fec->tbd_base, size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400539 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400540
Eric Nelson3d2f7272012-03-15 18:33:25 +0000541 /*
542 * Allocate receive descriptors. This allocation respects cache
543 * alignment.
544 */
545 if (!fec->rbd_base) {
546 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
547 ARCH_DMA_MINALIGN);
548 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
549 if (!fec->rbd_base) {
550 ret = -ENOMEM;
551 goto err2;
552 }
553 memset(fec->rbd_base, 0, size);
554 /*
555 * Initialize RxBD ring
556 */
557 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
558 ret = -ENOMEM;
559 goto err3;
560 }
561 flush_dcache_range((unsigned)fec->rbd_base,
562 (unsigned)fec->rbd_base + size);
563 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400564
565 /*
566 * Set interrupt mask register
567 */
568 writel(0x00000000, &fec->eth->imask);
569
570 /*
571 * Clear FEC-Lite interrupt event register(IEVENT)
572 */
573 writel(0xffffffff, &fec->eth->ievent);
574
575
576 /*
577 * Set FEC-Lite receive control register(R_CNTRL):
578 */
Stefano Babic889f2e22010-02-01 14:51:30 +0100579
Marek Vasutb8f88562011-09-11 18:05:31 +0000580 /* Start with frame length = 1518, common for all modes. */
581 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
582 if (fec->xcv_type == SEVENWIRE)
583 rcntrl |= FEC_RCNTRL_FCE;
Jason Liubbcef6c2011-12-16 05:17:07 +0000584 else if (fec->xcv_type == RGMII)
585 rcntrl |= FEC_RCNTRL_RGMII;
Marek Vasut95efbfa2011-09-11 18:05:32 +0000586 else if (fec->xcv_type == RMII)
587 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasutb8f88562011-09-11 18:05:31 +0000588 else /* MII mode */
589 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
590
591 writel(rcntrl, &fec->eth->r_cntrl);
592
593 if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
Stefano Babic889f2e22010-02-01 14:51:30 +0100594 fec_mii_setspeed(fec);
Marek Vasutb8f88562011-09-11 18:05:31 +0000595
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400596 /*
597 * Set Opcode/Pause Duration Register
598 */
599 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
600 writel(0x2, &fec->eth->x_wmrk);
601 /*
602 * Set multicast address filter
603 */
604 writel(0x00000000, &fec->eth->gaddr1);
605 writel(0x00000000, &fec->eth->gaddr2);
606
607
608 /* clear MIB RAM */
Marek Vasutedcd6c02011-09-16 01:13:47 +0200609 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
610 writel(0, i);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400611
612 /* FIFO receive start register */
613 writel(0x520, &fec->eth->r_fstart);
614
615 /* size and address of each buffer */
616 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
617 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
618 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
619
Troy Kisky2000c662012-02-07 14:08:47 +0000620#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400621 if (fec->xcv_type != SEVENWIRE)
622 miiphy_restart_aneg(dev);
Troy Kisky2000c662012-02-07 14:08:47 +0000623#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400624 fec_open(dev);
625 return 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000626
627err3:
628 free(fec->rbd_base);
629err2:
630 free(fec->tbd_base);
631err1:
632 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400633}
634
635/**
636 * Halt the FEC engine
637 * @param[in] dev Our device to handle
638 */
639static void fec_halt(struct eth_device *dev)
640{
Marek Vasutedcd6c02011-09-16 01:13:47 +0200641 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400642 int counter = 0xffff;
643
644 /*
645 * issue graceful stop command to the FEC transmitter if necessary
646 */
John Rigbye650e492010-01-25 23:12:55 -0700647 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400648 &fec->eth->x_cntrl);
649
650 debug("eth_halt: wait for stop regs\n");
651 /*
652 * wait for graceful stop to register
653 */
654 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700655 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400656
657 /*
658 * Disable SmartDMA tasks
659 */
660 fec_tx_task_disable(fec);
661 fec_rx_task_disable(fec);
662
663 /*
664 * Disable the Ethernet Controller
665 * Note: this will also reset the BD index counter!
666 */
John Rigby99d5fed2010-01-25 23:12:57 -0700667 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
668 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400669 fec->rbd_index = 0;
670 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400671 debug("eth_halt: done\n");
672}
673
674/**
675 * Transmit one frame
676 * @param[in] dev Our ethernet device to handle
677 * @param[in] packet Pointer to the data to be transmitted
678 * @param[in] length Data count in bytes
679 * @return 0 on success
680 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000681static int fec_send(struct eth_device *dev, volatile void *packet, int length)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400682{
683 unsigned int status;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000684 uint32_t size;
685 uint32_t addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400686
687 /*
688 * This routine transmits one frame. This routine only accepts
689 * 6-byte Ethernet addresses.
690 */
691 struct fec_priv *fec = (struct fec_priv *)dev->priv;
692
693 /*
694 * Check for valid length of data.
695 */
696 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100697 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400698 return -1;
699 }
700
701 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000702 * Setup the transmit buffer. We are always using the first buffer for
703 * transmission, the second will be empty and only used to stop the DMA
704 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400705 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000706#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000707 swap_packet((uint32_t *)packet, length);
708#endif
Eric Nelson3d2f7272012-03-15 18:33:25 +0000709
710 addr = (uint32_t)packet;
711 size = roundup(length, ARCH_DMA_MINALIGN);
712 flush_dcache_range(addr, addr + size);
713
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400714 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000715 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
716
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400717 /*
718 * update BD's status now
719 * This block:
720 * - is always the last in a chain (means no chain)
721 * - should transmitt the CRC
722 * - might be the last BD in the list, so the address counter should
723 * wrap (-> keep the WRAP flag)
724 */
725 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
726 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
727 writew(status, &fec->tbd_base[fec->tbd_index].status);
728
729 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000730 * Flush data cache. This code flushes both TX descriptors to RAM.
731 * After this code, the descriptors will be safely in RAM and we
732 * can start DMA.
733 */
734 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
735 addr = (uint32_t)fec->tbd_base;
736 flush_dcache_range(addr, addr + size);
737
738 /*
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400739 * Enable SmartDMA transmit task
740 */
741 fec_tx_task_enable(fec);
742
743 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000744 * Wait until frame is sent. On each turn of the wait cycle, we must
745 * invalidate data cache to see what's really in RAM. Also, we need
746 * barrier here.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400747 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000748 invalidate_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400749 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
John Rigbye650e492010-01-25 23:12:55 -0700750 udelay(1);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000751 invalidate_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400752 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000753
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400754 debug("fec_send: status 0x%x index %d\n",
755 readw(&fec->tbd_base[fec->tbd_index].status),
756 fec->tbd_index);
757 /* for next transmission use the other buffer */
758 if (fec->tbd_index)
759 fec->tbd_index = 0;
760 else
761 fec->tbd_index = 1;
762
763 return 0;
764}
765
766/**
767 * Pull one frame from the card
768 * @param[in] dev Our ethernet device to handle
769 * @return Length of packet read
770 */
771static int fec_recv(struct eth_device *dev)
772{
773 struct fec_priv *fec = (struct fec_priv *)dev->priv;
774 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
775 unsigned long ievent;
776 int frame_length, len = 0;
777 struct nbuf *frame;
778 uint16_t bd_status;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000779 uint32_t addr, size;
780 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400781 uchar buff[FEC_MAX_PKT_SIZE];
782
783 /*
784 * Check if any critical events have happened
785 */
786 ievent = readl(&fec->eth->ievent);
787 writel(ievent, &fec->eth->ievent);
Marek Vasut478e2d02011-10-24 23:40:03 +0000788 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400789 if (ievent & FEC_IEVENT_BABR) {
790 fec_halt(dev);
791 fec_init(dev, fec->bd);
792 printf("some error: 0x%08lx\n", ievent);
793 return 0;
794 }
795 if (ievent & FEC_IEVENT_HBERR) {
796 /* Heartbeat error */
797 writel(0x00000001 | readl(&fec->eth->x_cntrl),
798 &fec->eth->x_cntrl);
799 }
800 if (ievent & FEC_IEVENT_GRA) {
801 /* Graceful stop complete */
802 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
803 fec_halt(dev);
804 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
805 &fec->eth->x_cntrl);
806 fec_init(dev, fec->bd);
807 }
808 }
809
810 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000811 * Read the buffer status. Before the status can be read, the data cache
812 * must be invalidated, because the data in RAM might have been changed
813 * by DMA. The descriptors are properly aligned to cachelines so there's
814 * no need to worry they'd overlap.
815 *
816 * WARNING: By invalidating the descriptor here, we also invalidate
817 * the descriptors surrounding this one. Therefore we can NOT change the
818 * contents of this descriptor nor the surrounding ones. The problem is
819 * that in order to mark the descriptor as processed, we need to change
820 * the descriptor. The solution is to mark the whole cache line when all
821 * descriptors in the cache line are processed.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400822 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000823 addr = (uint32_t)rbd;
824 addr &= ~(ARCH_DMA_MINALIGN - 1);
825 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
826 invalidate_dcache_range(addr, addr + size);
827
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400828 bd_status = readw(&rbd->status);
829 debug("fec_recv: status 0x%x\n", bd_status);
830
831 if (!(bd_status & FEC_RBD_EMPTY)) {
832 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
833 ((readw(&rbd->data_length) - 4) > 14)) {
834 /*
835 * Get buffer address and size
836 */
837 frame = (struct nbuf *)readl(&rbd->data_pointer);
838 frame_length = readw(&rbd->data_length) - 4;
839 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000840 * Invalidate data cache over the buffer
841 */
842 addr = (uint32_t)frame;
843 size = roundup(frame_length, ARCH_DMA_MINALIGN);
844 invalidate_dcache_range(addr, addr + size);
845
846 /*
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400847 * Fill the buffer and pass it to upper layers
848 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000849#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000850 swap_packet((uint32_t *)frame->data, frame_length);
851#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400852 memcpy(buff, frame->data, frame_length);
853 NetReceive(buff, frame_length);
854 len = frame_length;
855 } else {
856 if (bd_status & FEC_RBD_ERR)
857 printf("error frame: 0x%08lx 0x%08x\n",
858 (ulong)rbd->data_pointer,
859 bd_status);
860 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000861
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400862 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000863 * Free the current buffer, restart the engine and move forward
864 * to the next buffer. Here we check if the whole cacheline of
865 * descriptors was already processed and if so, we mark it free
866 * as whole.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400867 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000868 size = RXDESC_PER_CACHELINE - 1;
869 if ((fec->rbd_index & size) == size) {
870 i = fec->rbd_index - size;
871 addr = (uint32_t)&fec->rbd_base[i];
872 for (; i <= fec->rbd_index ; i++) {
873 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
874 &fec->rbd_base[i]);
875 }
876 flush_dcache_range(addr,
877 addr + ARCH_DMA_MINALIGN);
878 }
879
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400880 fec_rx_task_enable(fec);
881 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
882 }
883 debug("fec_recv: stop\n");
884
885 return len;
886}
887
Marek Vasutedcd6c02011-09-16 01:13:47 +0200888static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400889{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400890 struct eth_device *edev;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200891 struct fec_priv *fec;
Troy Kisky2000c662012-02-07 14:08:47 +0000892 struct mii_dev *bus;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400893 unsigned char ethaddr[6];
Marek Vasut43b10302011-09-11 18:05:37 +0000894 uint32_t start;
895 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400896
897 /* create and fill edev struct */
898 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
899 if (!edev) {
Marek Vasutedcd6c02011-09-16 01:13:47 +0200900 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasut43b10302011-09-11 18:05:37 +0000901 ret = -ENOMEM;
902 goto err1;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200903 }
904
905 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
906 if (!fec) {
907 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasut43b10302011-09-11 18:05:37 +0000908 ret = -ENOMEM;
909 goto err2;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400910 }
Marek Vasutedcd6c02011-09-16 01:13:47 +0200911
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +0900912 memset(edev, 0, sizeof(*edev));
Marek Vasutedcd6c02011-09-16 01:13:47 +0200913 memset(fec, 0, sizeof(*fec));
914
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400915 edev->priv = fec;
916 edev->init = fec_init;
917 edev->send = fec_send;
918 edev->recv = fec_recv;
919 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +0200920 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400921
Marek Vasutedcd6c02011-09-16 01:13:47 +0200922 fec->eth = (struct ethernet_regs *)base_addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400923 fec->bd = bd;
924
Marek Vasutdbb4fce2011-09-11 18:05:33 +0000925 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400926
927 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -0700928 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasut43b10302011-09-11 18:05:37 +0000929 start = get_timer(0);
930 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
931 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
932 printf("FEC MXC: Timeout reseting chip\n");
933 goto err3;
934 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400935 udelay(10);
Marek Vasut43b10302011-09-11 18:05:37 +0000936 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400937
938 /*
939 * Set interrupt mask register
940 */
941 writel(0x00000000, &fec->eth->imask);
942
943 /*
944 * Clear FEC-Lite interrupt event register(IEVENT)
945 */
946 writel(0xffffffff, &fec->eth->ievent);
947
948 /*
949 * Set FEC-Lite receive control register(R_CNTRL):
950 */
951 /*
952 * Frame length=1518; MII mode;
953 */
Marek Vasutb8f88562011-09-11 18:05:31 +0000954 writel((PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT) | FEC_RCNTRL_FCE |
955 FEC_RCNTRL_MII_MODE, &fec->eth->r_cntrl);
Stefano Babic889f2e22010-02-01 14:51:30 +0100956 fec_mii_setspeed(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400957
Marek Vasutedcd6c02011-09-16 01:13:47 +0200958 if (dev_id == -1) {
959 sprintf(edev->name, "FEC");
960 fec->dev_id = 0;
961 } else {
962 sprintf(edev->name, "FEC%i", dev_id);
963 fec->dev_id = dev_id;
964 }
965 fec->phy_id = phy_id;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400966
Troy Kisky2000c662012-02-07 14:08:47 +0000967 bus = mdio_alloc();
968 if (!bus) {
969 printf("mdio_alloc failed\n");
970 ret = -ENOMEM;
971 goto err3;
972 }
973 bus->read = fec_phy_read;
974 bus->write = fec_phy_write;
975 sprintf(bus->name, edev->name);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000976#ifdef CONFIG_MX28
Troy Kisky2000c662012-02-07 14:08:47 +0000977 /*
978 * The i.MX28 has two ethernet interfaces, but they are not equal.
979 * Only the first one can access the MDIO bus.
980 */
981 bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
982#else
983 bus->priv = fec->eth;
984#endif
985 ret = mdio_register(bus);
986 if (ret) {
987 printf("mdio_register failed\n");
988 free(bus);
989 ret = -ENOMEM;
990 goto err3;
991 }
992 fec->bus = bus;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400993 eth_register(edev);
994
Fabio Estevam04fc1282011-12-20 05:46:31 +0000995 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
996 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
Stefano Babic889f2e22010-02-01 14:51:30 +0100997 memcpy(edev->enetaddr, ethaddr, 6);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400998 }
Troy Kisky2000c662012-02-07 14:08:47 +0000999 /* Configure phy */
1000 fec_eth_phy_config(edev);
Marek Vasut43b10302011-09-11 18:05:37 +00001001 return ret;
1002
1003err3:
1004 free(fec);
1005err2:
1006 free(edev);
1007err1:
1008 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001009}
1010
Eric Nelson3d2f7272012-03-15 18:33:25 +00001011#ifndef CONFIG_FEC_MXC_MULTI
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001012int fecmxc_initialize(bd_t *bd)
1013{
1014 int lout = 1;
1015
1016 debug("eth_init: fec_probe(bd)\n");
Marek Vasutedcd6c02011-09-16 01:13:47 +02001017 lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1018
1019 return lout;
1020}
1021#endif
1022
1023int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1024{
1025 int lout = 1;
1026
1027 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1028 lout = fec_probe(bd, dev_id, phy_id, addr);
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001029
1030 return lout;
1031}
Marek Vasut539ecee2011-09-11 18:05:36 +00001032
Troy Kisky2000c662012-02-07 14:08:47 +00001033#ifndef CONFIG_PHYLIB
Marek Vasut539ecee2011-09-11 18:05:36 +00001034int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1035{
1036 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1037 fec->mii_postcall = cb;
1038 return 0;
1039}
Troy Kisky2000c662012-02-07 14:08:47 +00001040#endif