blob: d8db9f0c6e808fd2d14a6f000f2f4dd0c80fbd47 [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{
Marek Vasutedcd6c02011-09-16 01:13:47 +0200190 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000191 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasut539ecee2011-09-11 18:05:36 +0000192 int ret = 0;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200193
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400194 /*
195 * Wake up from sleep if necessary
196 * Reset PHY, then delay 300ns
197 */
John Rigbye650e492010-01-25 23:12:55 -0700198#ifdef CONFIG_MX27
Troy Kisky2000c662012-02-07 14:08:47 +0000199 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700200#endif
Troy Kisky2000c662012-02-07 14:08:47 +0000201 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400202 udelay(1000);
203
204 /*
205 * Set the auto-negotiation advertisement register bits
206 */
Troy Kisky2000c662012-02-07 14:08:47 +0000207 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Mike Frysingerd63ee712010-12-23 15:40:12 -0500208 LPA_100FULL | LPA_100HALF | LPA_10FULL |
209 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky2000c662012-02-07 14:08:47 +0000210 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Mike Frysingerd63ee712010-12-23 15:40:12 -0500211 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut539ecee2011-09-11 18:05:36 +0000212
213 if (fec->mii_postcall)
214 ret = fec->mii_postcall(fec->phy_id);
215
216 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400217}
218
219static int miiphy_wait_aneg(struct eth_device *dev)
220{
221 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000222 int status;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200223 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000224 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400225
226 /*
227 * Wait for AN completion
228 */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000229 start = get_timer(0);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400230 do {
231 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
232 printf("%s: Autonegotiation timeout\n", dev->name);
233 return -1;
234 }
235
Troy Kisky2000c662012-02-07 14:08:47 +0000236 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
237 if (status < 0) {
238 printf("%s: Autonegotiation failed. status: %d\n",
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400239 dev->name, status);
240 return -1;
241 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500242 } while (!(status & BMSR_LSTATUS));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400243
244 return 0;
245}
Troy Kisky2000c662012-02-07 14:08:47 +0000246#endif
247
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400248static int fec_rx_task_enable(struct fec_priv *fec)
249{
250 writel(1 << 24, &fec->eth->r_des_active);
251 return 0;
252}
253
254static int fec_rx_task_disable(struct fec_priv *fec)
255{
256 return 0;
257}
258
259static int fec_tx_task_enable(struct fec_priv *fec)
260{
261 writel(1 << 24, &fec->eth->x_des_active);
262 return 0;
263}
264
265static int fec_tx_task_disable(struct fec_priv *fec)
266{
267 return 0;
268}
269
270/**
271 * Initialize receive task's buffer descriptors
272 * @param[in] fec all we know about the device yet
273 * @param[in] count receive buffer count to be allocated
Eric Nelson3d2f7272012-03-15 18:33:25 +0000274 * @param[in] dsize desired size of each receive buffer
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400275 * @return 0 on success
276 *
277 * For this task we need additional memory for the data buffers. And each
278 * data buffer requires some alignment. Thy must be aligned to a specific
Eric Nelson3d2f7272012-03-15 18:33:25 +0000279 * boundary each.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400280 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000281static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400282{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000283 uint32_t size;
284 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400285
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400286 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000287 * Allocate memory for the buffers. This allocation respects the
288 * alignment
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400289 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000290 size = roundup(dsize, ARCH_DMA_MINALIGN);
291 for (i = 0; i < count; i++) {
292 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
293 if (data_ptr == 0) {
294 uint8_t *data = memalign(ARCH_DMA_MINALIGN,
295 size);
296 if (!data) {
297 printf("%s: error allocating rxbuf %d\n",
298 __func__, i);
299 goto err;
300 }
301 writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
302 } /* needs allocation */
303 writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
304 writew(0, &fec->rbd_base[i].data_length);
305 }
306
307 /* Mark the last RBD to close the ring. */
308 writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400309 fec->rbd_index = 0;
310
311 return 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000312
313err:
314 for (; i >= 0; i--) {
315 uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
316 free((void *)data_ptr);
317 }
318
319 return -ENOMEM;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400320}
321
322/**
323 * Initialize transmit task's buffer descriptors
324 * @param[in] fec all we know about the device yet
325 *
326 * Transmit buffers are created externally. We only have to init the BDs here.\n
327 * Note: There is a race condition in the hardware. When only one BD is in
328 * use it must be marked with the WRAP bit to use it for every transmitt.
329 * This bit in combination with the READY bit results into double transmit
330 * of each data buffer. It seems the state machine checks READY earlier then
331 * resetting it after the first transfer.
332 * Using two BDs solves this issue.
333 */
334static void fec_tbd_init(struct fec_priv *fec)
335{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000336 unsigned addr = (unsigned)fec->tbd_base;
337 unsigned size = roundup(2 * sizeof(struct fec_bd),
338 ARCH_DMA_MINALIGN);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400339 writew(0x0000, &fec->tbd_base[0].status);
340 writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
341 fec->tbd_index = 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000342 flush_dcache_range(addr, addr+size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400343}
344
345/**
346 * Mark the given read buffer descriptor as free
347 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
348 * @param[in] pRbd buffer descriptor to mark free again
349 */
350static void fec_rbd_clean(int last, struct fec_bd *pRbd)
351{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000352 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400353 if (last)
Eric Nelson3d2f7272012-03-15 18:33:25 +0000354 flags |= FEC_RBD_WRAP;
355 writew(flags, &pRbd->status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400356 writew(0, &pRbd->data_length);
357}
358
Fabio Estevam04fc1282011-12-20 05:46:31 +0000359static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
360 unsigned char *mac)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400361{
Fabio Estevam04fc1282011-12-20 05:46:31 +0000362 imx_get_mac_from_fuse(dev_id, mac);
Eric Jarrigecc0d0d42010-04-16 00:03:19 +0200363 return !is_valid_ether_addr(mac);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400364}
365
Stefano Babic889f2e22010-02-01 14:51:30 +0100366static int fec_set_hwaddr(struct eth_device *dev)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400367{
Stefano Babic889f2e22010-02-01 14:51:30 +0100368 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400369 struct fec_priv *fec = (struct fec_priv *)dev->priv;
370
371 writel(0, &fec->eth->iaddr1);
372 writel(0, &fec->eth->iaddr2);
373 writel(0, &fec->eth->gaddr1);
374 writel(0, &fec->eth->gaddr2);
375
376 /*
377 * Set physical address
378 */
379 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
380 &fec->eth->paddr1);
381 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
382
383 return 0;
384}
385
Troy Kisky2000c662012-02-07 14:08:47 +0000386static void fec_eth_phy_config(struct eth_device *dev)
387{
388#ifdef CONFIG_PHYLIB
389 struct fec_priv *fec = (struct fec_priv *)dev->priv;
390 struct phy_device *phydev;
391
392 phydev = phy_connect(fec->bus, fec->phy_id, dev,
393 PHY_INTERFACE_MODE_RGMII);
394 if (phydev) {
395 fec->phydev = phydev;
396 phy_config(phydev);
397 }
398#endif
399}
400
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400401/**
402 * Start the FEC engine
403 * @param[in] dev Our device to handle
404 */
405static int fec_open(struct eth_device *edev)
406{
407 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Troy Kisky01112132012-02-07 14:08:46 +0000408 int speed;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000409 uint32_t addr, size;
410 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400411
412 debug("fec_open: fec_open(dev)\n");
413 /* full-duplex, heartbeat disabled */
414 writel(1 << 2, &fec->eth->x_cntrl);
415 fec->rbd_index = 0;
416
Eric Nelson3d2f7272012-03-15 18:33:25 +0000417 /* Invalidate all descriptors */
418 for (i = 0; i < FEC_RBD_NUM - 1; i++)
419 fec_rbd_clean(0, &fec->rbd_base[i]);
420 fec_rbd_clean(1, &fec->rbd_base[i]);
421
422 /* Flush the descriptors into RAM */
423 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
424 ARCH_DMA_MINALIGN);
425 addr = (uint32_t)fec->rbd_base;
426 flush_dcache_range(addr, addr + size);
427
Troy Kisky01112132012-02-07 14:08:46 +0000428#ifdef FEC_QUIRK_ENET_MAC
Jason Liubbcef6c2011-12-16 05:17:07 +0000429 /* Enable ENET HW endian SWAP */
430 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
431 &fec->eth->ecntrl);
432 /* Enable ENET store and forward mode */
433 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
434 &fec->eth->x_wmrk);
435#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400436 /*
437 * Enable FEC-Lite controller
438 */
John Rigbye650e492010-01-25 23:12:55 -0700439 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
440 &fec->eth->ecntrl);
Liu Hui-R64343c11cf872011-01-03 22:27:36 +0000441#if defined(CONFIG_MX25) || defined(CONFIG_MX53)
John Rigby99d5fed2010-01-25 23:12:57 -0700442 udelay(100);
443 /*
444 * setup the MII gasket for RMII mode
445 */
446
447 /* disable the gasket */
448 writew(0, &fec->eth->miigsk_enr);
449
450 /* wait for the gasket to be disabled */
451 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
452 udelay(2);
453
454 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
455 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
456
457 /* re-enable the gasket */
458 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
459
460 /* wait until MII gasket is ready */
461 int max_loops = 10;
462 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
463 if (--max_loops <= 0) {
464 printf("WAIT for MII Gasket ready timed out\n");
465 break;
466 }
467 }
468#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400469
Troy Kisky2000c662012-02-07 14:08:47 +0000470#ifdef CONFIG_PHYLIB
471 if (!fec->phydev)
472 fec_eth_phy_config(edev);
473 if (fec->phydev) {
474 /* Start up the PHY */
475 phy_startup(fec->phydev);
476 speed = fec->phydev->speed;
477 } else {
478 speed = _100BASET;
479 }
480#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400481 miiphy_wait_aneg(edev);
Troy Kisky01112132012-02-07 14:08:46 +0000482 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasutedcd6c02011-09-16 01:13:47 +0200483 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky2000c662012-02-07 14:08:47 +0000484#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400485
Troy Kisky01112132012-02-07 14:08:46 +0000486#ifdef FEC_QUIRK_ENET_MAC
487 {
488 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
489 u32 rcr = (readl(&fec->eth->r_cntrl) &
490 ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
491 FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
492 if (speed == _1000BASET)
493 ecr |= FEC_ECNTRL_SPEED;
494 else if (speed != _100BASET)
495 rcr |= FEC_RCNTRL_RMII_10T;
496 writel(ecr, &fec->eth->ecntrl);
497 writel(rcr, &fec->eth->r_cntrl);
498 }
499#endif
500 debug("%s:Speed=%i\n", __func__, speed);
501
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400502 /*
503 * Enable SmartDMA receive task
504 */
505 fec_rx_task_enable(fec);
506
507 udelay(100000);
508 return 0;
509}
510
511static int fec_init(struct eth_device *dev, bd_t* bd)
512{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400513 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200514 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
Marek Vasutb8f88562011-09-11 18:05:31 +0000515 uint32_t rcntrl;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000516 uint32_t size;
517 int i, ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400518
John Rigbya4a30552010-10-13 14:31:08 -0600519 /* Initialize MAC address */
520 fec_set_hwaddr(dev);
521
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400522 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000523 * Allocate transmit descriptors, there are two in total. This
524 * allocation respects cache alignment.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400525 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000526 if (!fec->tbd_base) {
527 size = roundup(2 * sizeof(struct fec_bd),
528 ARCH_DMA_MINALIGN);
529 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
530 if (!fec->tbd_base) {
531 ret = -ENOMEM;
532 goto err1;
533 }
534 memset(fec->tbd_base, 0, size);
535 fec_tbd_init(fec);
536 flush_dcache_range((unsigned)fec->tbd_base, size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400537 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400538
Eric Nelson3d2f7272012-03-15 18:33:25 +0000539 /*
540 * Allocate receive descriptors. This allocation respects cache
541 * alignment.
542 */
543 if (!fec->rbd_base) {
544 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
545 ARCH_DMA_MINALIGN);
546 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
547 if (!fec->rbd_base) {
548 ret = -ENOMEM;
549 goto err2;
550 }
551 memset(fec->rbd_base, 0, size);
552 /*
553 * Initialize RxBD ring
554 */
555 if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
556 ret = -ENOMEM;
557 goto err3;
558 }
559 flush_dcache_range((unsigned)fec->rbd_base,
560 (unsigned)fec->rbd_base + size);
561 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400562
563 /*
564 * Set interrupt mask register
565 */
566 writel(0x00000000, &fec->eth->imask);
567
568 /*
569 * Clear FEC-Lite interrupt event register(IEVENT)
570 */
571 writel(0xffffffff, &fec->eth->ievent);
572
573
574 /*
575 * Set FEC-Lite receive control register(R_CNTRL):
576 */
Stefano Babic889f2e22010-02-01 14:51:30 +0100577
Marek Vasutb8f88562011-09-11 18:05:31 +0000578 /* Start with frame length = 1518, common for all modes. */
579 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
580 if (fec->xcv_type == SEVENWIRE)
581 rcntrl |= FEC_RCNTRL_FCE;
Jason Liubbcef6c2011-12-16 05:17:07 +0000582 else if (fec->xcv_type == RGMII)
583 rcntrl |= FEC_RCNTRL_RGMII;
Marek Vasut95efbfa2011-09-11 18:05:32 +0000584 else if (fec->xcv_type == RMII)
585 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasutb8f88562011-09-11 18:05:31 +0000586 else /* MII mode */
587 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
588
589 writel(rcntrl, &fec->eth->r_cntrl);
590
591 if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
Stefano Babic889f2e22010-02-01 14:51:30 +0100592 fec_mii_setspeed(fec);
Marek Vasutb8f88562011-09-11 18:05:31 +0000593
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400594 /*
595 * Set Opcode/Pause Duration Register
596 */
597 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
598 writel(0x2, &fec->eth->x_wmrk);
599 /*
600 * Set multicast address filter
601 */
602 writel(0x00000000, &fec->eth->gaddr1);
603 writel(0x00000000, &fec->eth->gaddr2);
604
605
606 /* clear MIB RAM */
Marek Vasutedcd6c02011-09-16 01:13:47 +0200607 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
608 writel(0, i);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400609
610 /* FIFO receive start register */
611 writel(0x520, &fec->eth->r_fstart);
612
613 /* size and address of each buffer */
614 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
615 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
616 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
617
Troy Kisky2000c662012-02-07 14:08:47 +0000618#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400619 if (fec->xcv_type != SEVENWIRE)
620 miiphy_restart_aneg(dev);
Troy Kisky2000c662012-02-07 14:08:47 +0000621#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400622 fec_open(dev);
623 return 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000624
625err3:
626 free(fec->rbd_base);
627err2:
628 free(fec->tbd_base);
629err1:
630 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400631}
632
633/**
634 * Halt the FEC engine
635 * @param[in] dev Our device to handle
636 */
637static void fec_halt(struct eth_device *dev)
638{
Marek Vasutedcd6c02011-09-16 01:13:47 +0200639 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400640 int counter = 0xffff;
641
642 /*
643 * issue graceful stop command to the FEC transmitter if necessary
644 */
John Rigbye650e492010-01-25 23:12:55 -0700645 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400646 &fec->eth->x_cntrl);
647
648 debug("eth_halt: wait for stop regs\n");
649 /*
650 * wait for graceful stop to register
651 */
652 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700653 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400654
655 /*
656 * Disable SmartDMA tasks
657 */
658 fec_tx_task_disable(fec);
659 fec_rx_task_disable(fec);
660
661 /*
662 * Disable the Ethernet Controller
663 * Note: this will also reset the BD index counter!
664 */
John Rigby99d5fed2010-01-25 23:12:57 -0700665 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
666 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400667 fec->rbd_index = 0;
668 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400669 debug("eth_halt: done\n");
670}
671
672/**
673 * Transmit one frame
674 * @param[in] dev Our ethernet device to handle
675 * @param[in] packet Pointer to the data to be transmitted
676 * @param[in] length Data count in bytes
677 * @return 0 on success
678 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000679static int fec_send(struct eth_device *dev, volatile void *packet, int length)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400680{
681 unsigned int status;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000682 uint32_t size;
683 uint32_t addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400684
685 /*
686 * This routine transmits one frame. This routine only accepts
687 * 6-byte Ethernet addresses.
688 */
689 struct fec_priv *fec = (struct fec_priv *)dev->priv;
690
691 /*
692 * Check for valid length of data.
693 */
694 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100695 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400696 return -1;
697 }
698
699 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000700 * Setup the transmit buffer. We are always using the first buffer for
701 * transmission, the second will be empty and only used to stop the DMA
702 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400703 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000704#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000705 swap_packet((uint32_t *)packet, length);
706#endif
Eric Nelson3d2f7272012-03-15 18:33:25 +0000707
708 addr = (uint32_t)packet;
709 size = roundup(length, ARCH_DMA_MINALIGN);
710 flush_dcache_range(addr, addr + size);
711
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400712 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000713 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
714
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400715 /*
716 * update BD's status now
717 * This block:
718 * - is always the last in a chain (means no chain)
719 * - should transmitt the CRC
720 * - might be the last BD in the list, so the address counter should
721 * wrap (-> keep the WRAP flag)
722 */
723 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
724 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
725 writew(status, &fec->tbd_base[fec->tbd_index].status);
726
727 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000728 * Flush data cache. This code flushes both TX descriptors to RAM.
729 * After this code, the descriptors will be safely in RAM and we
730 * can start DMA.
731 */
732 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
733 addr = (uint32_t)fec->tbd_base;
734 flush_dcache_range(addr, addr + size);
735
736 /*
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400737 * Enable SmartDMA transmit task
738 */
739 fec_tx_task_enable(fec);
740
741 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000742 * Wait until frame is sent. On each turn of the wait cycle, we must
743 * invalidate data cache to see what's really in RAM. Also, we need
744 * barrier here.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400745 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000746 invalidate_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400747 while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
John Rigbye650e492010-01-25 23:12:55 -0700748 udelay(1);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000749 invalidate_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400750 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000751
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400752 debug("fec_send: status 0x%x index %d\n",
753 readw(&fec->tbd_base[fec->tbd_index].status),
754 fec->tbd_index);
755 /* for next transmission use the other buffer */
756 if (fec->tbd_index)
757 fec->tbd_index = 0;
758 else
759 fec->tbd_index = 1;
760
761 return 0;
762}
763
764/**
765 * Pull one frame from the card
766 * @param[in] dev Our ethernet device to handle
767 * @return Length of packet read
768 */
769static int fec_recv(struct eth_device *dev)
770{
771 struct fec_priv *fec = (struct fec_priv *)dev->priv;
772 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
773 unsigned long ievent;
774 int frame_length, len = 0;
775 struct nbuf *frame;
776 uint16_t bd_status;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000777 uint32_t addr, size;
778 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400779 uchar buff[FEC_MAX_PKT_SIZE];
780
781 /*
782 * Check if any critical events have happened
783 */
784 ievent = readl(&fec->eth->ievent);
785 writel(ievent, &fec->eth->ievent);
Marek Vasut478e2d02011-10-24 23:40:03 +0000786 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400787 if (ievent & FEC_IEVENT_BABR) {
788 fec_halt(dev);
789 fec_init(dev, fec->bd);
790 printf("some error: 0x%08lx\n", ievent);
791 return 0;
792 }
793 if (ievent & FEC_IEVENT_HBERR) {
794 /* Heartbeat error */
795 writel(0x00000001 | readl(&fec->eth->x_cntrl),
796 &fec->eth->x_cntrl);
797 }
798 if (ievent & FEC_IEVENT_GRA) {
799 /* Graceful stop complete */
800 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
801 fec_halt(dev);
802 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
803 &fec->eth->x_cntrl);
804 fec_init(dev, fec->bd);
805 }
806 }
807
808 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000809 * Read the buffer status. Before the status can be read, the data cache
810 * must be invalidated, because the data in RAM might have been changed
811 * by DMA. The descriptors are properly aligned to cachelines so there's
812 * no need to worry they'd overlap.
813 *
814 * WARNING: By invalidating the descriptor here, we also invalidate
815 * the descriptors surrounding this one. Therefore we can NOT change the
816 * contents of this descriptor nor the surrounding ones. The problem is
817 * that in order to mark the descriptor as processed, we need to change
818 * the descriptor. The solution is to mark the whole cache line when all
819 * descriptors in the cache line are processed.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400820 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000821 addr = (uint32_t)rbd;
822 addr &= ~(ARCH_DMA_MINALIGN - 1);
823 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
824 invalidate_dcache_range(addr, addr + size);
825
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400826 bd_status = readw(&rbd->status);
827 debug("fec_recv: status 0x%x\n", bd_status);
828
829 if (!(bd_status & FEC_RBD_EMPTY)) {
830 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
831 ((readw(&rbd->data_length) - 4) > 14)) {
832 /*
833 * Get buffer address and size
834 */
835 frame = (struct nbuf *)readl(&rbd->data_pointer);
836 frame_length = readw(&rbd->data_length) - 4;
837 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000838 * Invalidate data cache over the buffer
839 */
840 addr = (uint32_t)frame;
841 size = roundup(frame_length, ARCH_DMA_MINALIGN);
842 invalidate_dcache_range(addr, addr + size);
843
844 /*
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400845 * Fill the buffer and pass it to upper layers
846 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000847#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000848 swap_packet((uint32_t *)frame->data, frame_length);
849#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400850 memcpy(buff, frame->data, frame_length);
851 NetReceive(buff, frame_length);
852 len = frame_length;
853 } else {
854 if (bd_status & FEC_RBD_ERR)
855 printf("error frame: 0x%08lx 0x%08x\n",
856 (ulong)rbd->data_pointer,
857 bd_status);
858 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000859
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400860 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000861 * Free the current buffer, restart the engine and move forward
862 * to the next buffer. Here we check if the whole cacheline of
863 * descriptors was already processed and if so, we mark it free
864 * as whole.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400865 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000866 size = RXDESC_PER_CACHELINE - 1;
867 if ((fec->rbd_index & size) == size) {
868 i = fec->rbd_index - size;
869 addr = (uint32_t)&fec->rbd_base[i];
870 for (; i <= fec->rbd_index ; i++) {
871 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
872 &fec->rbd_base[i]);
873 }
874 flush_dcache_range(addr,
875 addr + ARCH_DMA_MINALIGN);
876 }
877
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400878 fec_rx_task_enable(fec);
879 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
880 }
881 debug("fec_recv: stop\n");
882
883 return len;
884}
885
Marek Vasutedcd6c02011-09-16 01:13:47 +0200886static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400887{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400888 struct eth_device *edev;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200889 struct fec_priv *fec;
Troy Kisky2000c662012-02-07 14:08:47 +0000890 struct mii_dev *bus;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400891 unsigned char ethaddr[6];
Marek Vasut43b10302011-09-11 18:05:37 +0000892 uint32_t start;
893 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400894
895 /* create and fill edev struct */
896 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
897 if (!edev) {
Marek Vasutedcd6c02011-09-16 01:13:47 +0200898 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasut43b10302011-09-11 18:05:37 +0000899 ret = -ENOMEM;
900 goto err1;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200901 }
902
903 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
904 if (!fec) {
905 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasut43b10302011-09-11 18:05:37 +0000906 ret = -ENOMEM;
907 goto err2;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400908 }
Marek Vasutedcd6c02011-09-16 01:13:47 +0200909
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +0900910 memset(edev, 0, sizeof(*edev));
Marek Vasutedcd6c02011-09-16 01:13:47 +0200911 memset(fec, 0, sizeof(*fec));
912
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400913 edev->priv = fec;
914 edev->init = fec_init;
915 edev->send = fec_send;
916 edev->recv = fec_recv;
917 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +0200918 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400919
Marek Vasutedcd6c02011-09-16 01:13:47 +0200920 fec->eth = (struct ethernet_regs *)base_addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400921 fec->bd = bd;
922
Marek Vasutdbb4fce2011-09-11 18:05:33 +0000923 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400924
925 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -0700926 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasut43b10302011-09-11 18:05:37 +0000927 start = get_timer(0);
928 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
929 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
930 printf("FEC MXC: Timeout reseting chip\n");
931 goto err3;
932 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400933 udelay(10);
Marek Vasut43b10302011-09-11 18:05:37 +0000934 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400935
936 /*
937 * Set interrupt mask register
938 */
939 writel(0x00000000, &fec->eth->imask);
940
941 /*
942 * Clear FEC-Lite interrupt event register(IEVENT)
943 */
944 writel(0xffffffff, &fec->eth->ievent);
945
946 /*
947 * Set FEC-Lite receive control register(R_CNTRL):
948 */
949 /*
950 * Frame length=1518; MII mode;
951 */
Marek Vasutb8f88562011-09-11 18:05:31 +0000952 writel((PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT) | FEC_RCNTRL_FCE |
953 FEC_RCNTRL_MII_MODE, &fec->eth->r_cntrl);
Stefano Babic889f2e22010-02-01 14:51:30 +0100954 fec_mii_setspeed(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400955
Marek Vasutedcd6c02011-09-16 01:13:47 +0200956 if (dev_id == -1) {
957 sprintf(edev->name, "FEC");
958 fec->dev_id = 0;
959 } else {
960 sprintf(edev->name, "FEC%i", dev_id);
961 fec->dev_id = dev_id;
962 }
963 fec->phy_id = phy_id;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400964
Troy Kisky2000c662012-02-07 14:08:47 +0000965 bus = mdio_alloc();
966 if (!bus) {
967 printf("mdio_alloc failed\n");
968 ret = -ENOMEM;
969 goto err3;
970 }
971 bus->read = fec_phy_read;
972 bus->write = fec_phy_write;
973 sprintf(bus->name, edev->name);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000974#ifdef CONFIG_MX28
Troy Kisky2000c662012-02-07 14:08:47 +0000975 /*
976 * The i.MX28 has two ethernet interfaces, but they are not equal.
977 * Only the first one can access the MDIO bus.
978 */
979 bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
980#else
981 bus->priv = fec->eth;
982#endif
983 ret = mdio_register(bus);
984 if (ret) {
985 printf("mdio_register failed\n");
986 free(bus);
987 ret = -ENOMEM;
988 goto err3;
989 }
990 fec->bus = bus;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400991 eth_register(edev);
992
Fabio Estevam04fc1282011-12-20 05:46:31 +0000993 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
994 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
Stefano Babic889f2e22010-02-01 14:51:30 +0100995 memcpy(edev->enetaddr, ethaddr, 6);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400996 }
Troy Kisky2000c662012-02-07 14:08:47 +0000997 /* Configure phy */
998 fec_eth_phy_config(edev);
Marek Vasut43b10302011-09-11 18:05:37 +0000999 return ret;
1000
1001err3:
1002 free(fec);
1003err2:
1004 free(edev);
1005err1:
1006 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001007}
1008
Eric Nelson3d2f7272012-03-15 18:33:25 +00001009#ifndef CONFIG_FEC_MXC_MULTI
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001010int fecmxc_initialize(bd_t *bd)
1011{
1012 int lout = 1;
1013
1014 debug("eth_init: fec_probe(bd)\n");
Marek Vasutedcd6c02011-09-16 01:13:47 +02001015 lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
1016
1017 return lout;
1018}
1019#endif
1020
1021int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1022{
1023 int lout = 1;
1024
1025 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1026 lout = fec_probe(bd, dev_id, phy_id, addr);
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001027
1028 return lout;
1029}
Marek Vasut539ecee2011-09-11 18:05:36 +00001030
Troy Kisky2000c662012-02-07 14:08:47 +00001031#ifndef CONFIG_PHYLIB
Marek Vasut539ecee2011-09-11 18:05:36 +00001032int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1033{
1034 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1035 fec->mii_postcall = cb;
1036 return 0;
1037}
Troy Kisky2000c662012-02-07 14:08:47 +00001038#endif