blob: bff5fd111971dc7e7c50f1ae3801d8b8eefe94ce [file] [log] [blame]
Ilya Yanok0b23fb32009-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Ilya Yanok0b23fb32009-07-21 19:32:21 +04009 */
10
11#include <common.h>
12#include <malloc.h>
13#include <net.h>
Jeroen Hofstee84f64c82014-10-08 22:57:40 +020014#include <netdev.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040015#include <miiphy.h>
16#include "fec_mxc.h"
17
18#include <asm/arch/clock.h>
19#include <asm/arch/imx-regs.h>
Peng Fanfbecbaa2015-08-12 17:46:51 +080020#include <asm/imx-common/sys_proto.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040021#include <asm/io.h>
22#include <asm/errno.h>
Marek Vasute2a66e62012-08-26 10:19:20 +000023#include <linux/compiler.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040024
25DECLARE_GLOBAL_DATA_PTR;
26
Marek Vasutbc1ce152012-08-29 03:49:49 +000027/*
28 * Timeout the transfer after 5 mS. This is usually a bit more, since
29 * the code in the tightloops this timeout is used in adds some overhead.
30 */
31#define FEC_XFER_TIMEOUT 5000
32
Fabio Estevamdb5b7f52014-08-25 13:34:16 -030033/*
34 * The standard 32-byte DMA alignment does not work on mx6solox, which requires
35 * 64-byte alignment in the DMA RX FEC buffer.
36 * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
37 * satisfies the alignment on other SoCs (32-bytes)
38 */
39#define FEC_DMA_RX_MINALIGN 64
40
Ilya Yanok0b23fb32009-07-21 19:32:21 +040041#ifndef CONFIG_MII
42#error "CONFIG_MII has to be defined!"
43#endif
44
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000045#ifndef CONFIG_FEC_XCV_TYPE
46#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasut392b8502011-09-11 18:05:33 +000047#endif
48
Marek Vasutbe7e87e2011-11-08 23:18:10 +000049/*
50 * The i.MX28 operates with packets in big endian. We need to swap them before
51 * sending and after receiving.
52 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000053#ifdef CONFIG_MX28
54#define CONFIG_FEC_MXC_SWAP_PACKET
55#endif
56
57#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
58
59/* Check various alignment issues at compile time */
60#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
61#error "ARCH_DMA_MINALIGN must be multiple of 16!"
62#endif
63
64#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
65 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
66#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
Marek Vasutbe7e87e2011-11-08 23:18:10 +000067#endif
68
Ilya Yanok0b23fb32009-07-21 19:32:21 +040069#undef DEBUG
70
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000071#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +000072static void swap_packet(uint32_t *packet, int length)
73{
74 int i;
75
76 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
77 packet[i] = __swab32(packet[i]);
78}
79#endif
80
81/*
Ilya Yanok0b23fb32009-07-21 19:32:21 +040082 * MII-interface related functions
83 */
Troy Kisky13947f42012-02-07 14:08:47 +000084static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
85 uint8_t regAddr)
Ilya Yanok0b23fb32009-07-21 19:32:21 +040086{
Ilya Yanok0b23fb32009-07-21 19:32:21 +040087 uint32_t reg; /* convenient holder for the PHY register */
88 uint32_t phy; /* convenient holder for the PHY */
89 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +000090 int val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +040091
92 /*
93 * reading from any PHY's register is done by properly
94 * programming the FEC's MII data register.
95 */
Marek Vasutd133b882011-09-11 18:05:34 +000096 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +040097 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
98 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
99
100 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutd133b882011-09-11 18:05:34 +0000101 phy | reg, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400102
103 /*
104 * wait for the related interrupt
105 */
Graeme Russa60d1e52011-07-15 23:31:37 +0000106 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000107 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400108 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
109 printf("Read MDIO failed...\n");
110 return -1;
111 }
112 }
113
114 /*
115 * clear mii interrupt bit
116 */
Marek Vasutd133b882011-09-11 18:05:34 +0000117 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400118
119 /*
120 * it's now safe to read the PHY's register
121 */
Troy Kisky13947f42012-02-07 14:08:47 +0000122 val = (unsigned short)readl(&eth->mii_data);
123 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
124 regAddr, val);
125 return val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400126}
127
Troy Kisky575c5cc2012-10-22 16:40:41 +0000128static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic4294b242010-02-01 14:51:30 +0100129{
130 /*
131 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
132 * and do not drop the Preamble.
133 */
Markus Niebel6ba45cc2014-02-05 10:54:11 +0100134 register u32 speed = DIV_ROUND_UP(imx_get_fecclk(), 5000000);
135#ifdef FEC_QUIRK_ENET_MAC
136 speed--;
137#endif
138 speed <<= 1;
139 writel(speed, &eth->mii_speed);
Troy Kisky575c5cc2012-10-22 16:40:41 +0000140 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic4294b242010-02-01 14:51:30 +0100141}
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400142
Troy Kisky13947f42012-02-07 14:08:47 +0000143static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
144 uint8_t regAddr, uint16_t data)
145{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400146 uint32_t reg; /* convenient holder for the PHY register */
147 uint32_t phy; /* convenient holder for the PHY */
148 uint32_t start;
149
150 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
151 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
152
153 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutd133b882011-09-11 18:05:34 +0000154 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400155
156 /*
157 * wait for the MII interrupt
158 */
Graeme Russa60d1e52011-07-15 23:31:37 +0000159 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000160 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400161 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
162 printf("Write MDIO failed...\n");
163 return -1;
164 }
165 }
166
167 /*
168 * clear MII interrupt bit
169 */
Marek Vasutd133b882011-09-11 18:05:34 +0000170 writel(FEC_IEVENT_MII, &eth->ievent);
Troy Kisky13947f42012-02-07 14:08:47 +0000171 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400172 regAddr, data);
173
174 return 0;
175}
176
Jeroen Hofstee84f64c82014-10-08 22:57:40 +0200177static int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr,
178 int regAddr)
Troy Kisky13947f42012-02-07 14:08:47 +0000179{
180 return fec_mdio_read(bus->priv, phyAddr, regAddr);
181}
182
Jeroen Hofstee84f64c82014-10-08 22:57:40 +0200183static int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr,
184 int regAddr, u16 data)
Troy Kisky13947f42012-02-07 14:08:47 +0000185{
186 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
187}
188
189#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400190static int miiphy_restart_aneg(struct eth_device *dev)
191{
Stefano Babicb774fe92012-02-22 00:24:35 +0000192 int ret = 0;
193#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200194 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000195 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200196
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400197 /*
198 * Wake up from sleep if necessary
199 * Reset PHY, then delay 300ns
200 */
John Rigbycb17b922010-01-25 23:12:55 -0700201#ifdef CONFIG_MX27
Troy Kisky13947f42012-02-07 14:08:47 +0000202 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbycb17b922010-01-25 23:12:55 -0700203#endif
Troy Kisky13947f42012-02-07 14:08:47 +0000204 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400205 udelay(1000);
206
207 /*
208 * Set the auto-negotiation advertisement register bits
209 */
Troy Kisky13947f42012-02-07 14:08:47 +0000210 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500211 LPA_100FULL | LPA_100HALF | LPA_10FULL |
212 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky13947f42012-02-07 14:08:47 +0000213 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500214 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut2e5f4422011-09-11 18:05:36 +0000215
216 if (fec->mii_postcall)
217 ret = fec->mii_postcall(fec->phy_id);
218
Stefano Babicb774fe92012-02-22 00:24:35 +0000219#endif
Marek Vasut2e5f4422011-09-11 18:05:36 +0000220 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400221}
222
223static int miiphy_wait_aneg(struct eth_device *dev)
224{
225 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +0000226 int status;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200227 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000228 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400229
230 /*
231 * Wait for AN completion
232 */
Graeme Russa60d1e52011-07-15 23:31:37 +0000233 start = get_timer(0);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400234 do {
235 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
236 printf("%s: Autonegotiation timeout\n", dev->name);
237 return -1;
238 }
239
Troy Kisky13947f42012-02-07 14:08:47 +0000240 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
241 if (status < 0) {
242 printf("%s: Autonegotiation failed. status: %d\n",
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400243 dev->name, status);
244 return -1;
245 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500246 } while (!(status & BMSR_LSTATUS));
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400247
248 return 0;
249}
Troy Kisky13947f42012-02-07 14:08:47 +0000250#endif
251
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400252static int fec_rx_task_enable(struct fec_priv *fec)
253{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000254 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400255 return 0;
256}
257
258static int fec_rx_task_disable(struct fec_priv *fec)
259{
260 return 0;
261}
262
263static int fec_tx_task_enable(struct fec_priv *fec)
264{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000265 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400266 return 0;
267}
268
269static int fec_tx_task_disable(struct fec_priv *fec)
270{
271 return 0;
272}
273
274/**
275 * Initialize receive task's buffer descriptors
276 * @param[in] fec all we know about the device yet
277 * @param[in] count receive buffer count to be allocated
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000278 * @param[in] dsize desired size of each receive buffer
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400279 * @return 0 on success
280 *
Marek Vasut79e5f272013-10-12 20:36:25 +0200281 * Init all RX descriptors to default values.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400282 */
Marek Vasut79e5f272013-10-12 20:36:25 +0200283static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400284{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000285 uint32_t size;
Marek Vasut79e5f272013-10-12 20:36:25 +0200286 uint8_t *data;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000287 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400288
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400289 /*
Marek Vasut79e5f272013-10-12 20:36:25 +0200290 * Reload the RX descriptors with default values and wipe
291 * the RX buffers.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400292 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000293 size = roundup(dsize, ARCH_DMA_MINALIGN);
294 for (i = 0; i < count; i++) {
Marek Vasut79e5f272013-10-12 20:36:25 +0200295 data = (uint8_t *)fec->rbd_base[i].data_pointer;
296 memset(data, 0, dsize);
297 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
298
299 fec->rbd_base[i].status = FEC_RBD_EMPTY;
300 fec->rbd_base[i].data_length = 0;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000301 }
302
303 /* Mark the last RBD to close the ring. */
Marek Vasut79e5f272013-10-12 20:36:25 +0200304 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400305 fec->rbd_index = 0;
306
Marek Vasut79e5f272013-10-12 20:36:25 +0200307 flush_dcache_range((unsigned)fec->rbd_base,
308 (unsigned)fec->rbd_base + size);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400309}
310
311/**
312 * Initialize transmit task's buffer descriptors
313 * @param[in] fec all we know about the device yet
314 *
315 * Transmit buffers are created externally. We only have to init the BDs here.\n
316 * Note: There is a race condition in the hardware. When only one BD is in
317 * use it must be marked with the WRAP bit to use it for every transmitt.
318 * This bit in combination with the READY bit results into double transmit
319 * of each data buffer. It seems the state machine checks READY earlier then
320 * resetting it after the first transfer.
321 * Using two BDs solves this issue.
322 */
323static void fec_tbd_init(struct fec_priv *fec)
324{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000325 unsigned addr = (unsigned)fec->tbd_base;
326 unsigned size = roundup(2 * sizeof(struct fec_bd),
327 ARCH_DMA_MINALIGN);
Marek Vasut79e5f272013-10-12 20:36:25 +0200328
329 memset(fec->tbd_base, 0, size);
330 fec->tbd_base[0].status = 0;
331 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400332 fec->tbd_index = 0;
Marek Vasut79e5f272013-10-12 20:36:25 +0200333 flush_dcache_range(addr, addr + size);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400334}
335
336/**
337 * Mark the given read buffer descriptor as free
338 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
339 * @param[in] pRbd buffer descriptor to mark free again
340 */
341static void fec_rbd_clean(int last, struct fec_bd *pRbd)
342{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000343 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400344 if (last)
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000345 flags |= FEC_RBD_WRAP;
346 writew(flags, &pRbd->status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400347 writew(0, &pRbd->data_length);
348}
349
Fabio Estevambe252b62011-12-20 05:46:31 +0000350static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
351 unsigned char *mac)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400352{
Fabio Estevambe252b62011-12-20 05:46:31 +0000353 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500354 return !is_valid_ethaddr(mac);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400355}
356
Stefano Babic4294b242010-02-01 14:51:30 +0100357static int fec_set_hwaddr(struct eth_device *dev)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400358{
Stefano Babic4294b242010-02-01 14:51:30 +0100359 uchar *mac = dev->enetaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400360 struct fec_priv *fec = (struct fec_priv *)dev->priv;
361
362 writel(0, &fec->eth->iaddr1);
363 writel(0, &fec->eth->iaddr2);
364 writel(0, &fec->eth->gaddr1);
365 writel(0, &fec->eth->gaddr2);
366
367 /*
368 * Set physical address
369 */
370 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
371 &fec->eth->paddr1);
372 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
373
374 return 0;
375}
376
Marek Vasuta5990b22012-05-01 11:09:41 +0000377/*
378 * Do initial configuration of the FEC registers
379 */
380static void fec_reg_setup(struct fec_priv *fec)
381{
382 uint32_t rcntrl;
383
384 /*
385 * Set interrupt mask register
386 */
387 writel(0x00000000, &fec->eth->imask);
388
389 /*
390 * Clear FEC-Lite interrupt event register(IEVENT)
391 */
392 writel(0xffffffff, &fec->eth->ievent);
393
394
395 /*
396 * Set FEC-Lite receive control register(R_CNTRL):
397 */
398
399 /* Start with frame length = 1518, common for all modes. */
400 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advans9d2d9242012-07-19 02:12:46 +0000401 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
402 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
403 if (fec->xcv_type == RGMII)
Marek Vasuta5990b22012-05-01 11:09:41 +0000404 rcntrl |= FEC_RCNTRL_RGMII;
405 else if (fec->xcv_type == RMII)
406 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasuta5990b22012-05-01 11:09:41 +0000407
408 writel(rcntrl, &fec->eth->r_cntrl);
409}
410
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400411/**
412 * Start the FEC engine
413 * @param[in] dev Our device to handle
414 */
415static int fec_open(struct eth_device *edev)
416{
417 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Troy Kisky28774cb2012-02-07 14:08:46 +0000418 int speed;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000419 uint32_t addr, size;
420 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400421
422 debug("fec_open: fec_open(dev)\n");
423 /* full-duplex, heartbeat disabled */
424 writel(1 << 2, &fec->eth->x_cntrl);
425 fec->rbd_index = 0;
426
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000427 /* Invalidate all descriptors */
428 for (i = 0; i < FEC_RBD_NUM - 1; i++)
429 fec_rbd_clean(0, &fec->rbd_base[i]);
430 fec_rbd_clean(1, &fec->rbd_base[i]);
431
432 /* Flush the descriptors into RAM */
433 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
434 ARCH_DMA_MINALIGN);
435 addr = (uint32_t)fec->rbd_base;
436 flush_dcache_range(addr, addr + size);
437
Troy Kisky28774cb2012-02-07 14:08:46 +0000438#ifdef FEC_QUIRK_ENET_MAC
Jason Liu2ef2b952011-12-16 05:17:07 +0000439 /* Enable ENET HW endian SWAP */
440 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
441 &fec->eth->ecntrl);
442 /* Enable ENET store and forward mode */
443 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
444 &fec->eth->x_wmrk);
445#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400446 /*
447 * Enable FEC-Lite controller
448 */
John Rigbycb17b922010-01-25 23:12:55 -0700449 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
450 &fec->eth->ecntrl);
Fabio Estevam7df51fd2013-09-13 00:36:27 -0300451#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby740d6ae2010-01-25 23:12:57 -0700452 udelay(100);
453 /*
454 * setup the MII gasket for RMII mode
455 */
456
457 /* disable the gasket */
458 writew(0, &fec->eth->miigsk_enr);
459
460 /* wait for the gasket to be disabled */
461 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
462 udelay(2);
463
464 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
465 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
466
467 /* re-enable the gasket */
468 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
469
470 /* wait until MII gasket is ready */
471 int max_loops = 10;
472 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
473 if (--max_loops <= 0) {
474 printf("WAIT for MII Gasket ready timed out\n");
475 break;
476 }
477 }
478#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400479
Troy Kisky13947f42012-02-07 14:08:47 +0000480#ifdef CONFIG_PHYLIB
Troy Kisky4dc27ee2012-10-22 16:40:45 +0000481 {
Troy Kisky13947f42012-02-07 14:08:47 +0000482 /* Start up the PHY */
Timur Tabi11af8d62012-07-09 08:52:43 +0000483 int ret = phy_startup(fec->phydev);
484
485 if (ret) {
486 printf("Could not initialize PHY %s\n",
487 fec->phydev->dev->name);
488 return ret;
489 }
Troy Kisky13947f42012-02-07 14:08:47 +0000490 speed = fec->phydev->speed;
Troy Kisky13947f42012-02-07 14:08:47 +0000491 }
492#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400493 miiphy_wait_aneg(edev);
Troy Kisky28774cb2012-02-07 14:08:46 +0000494 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200495 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky13947f42012-02-07 14:08:47 +0000496#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400497
Troy Kisky28774cb2012-02-07 14:08:46 +0000498#ifdef FEC_QUIRK_ENET_MAC
499 {
500 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wangbcb6e902013-05-27 22:55:43 +0000501 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky28774cb2012-02-07 14:08:46 +0000502 if (speed == _1000BASET)
503 ecr |= FEC_ECNTRL_SPEED;
504 else if (speed != _100BASET)
505 rcr |= FEC_RCNTRL_RMII_10T;
506 writel(ecr, &fec->eth->ecntrl);
507 writel(rcr, &fec->eth->r_cntrl);
508 }
509#endif
510 debug("%s:Speed=%i\n", __func__, speed);
511
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400512 /*
513 * Enable SmartDMA receive task
514 */
515 fec_rx_task_enable(fec);
516
517 udelay(100000);
518 return 0;
519}
520
521static int fec_init(struct eth_device *dev, bd_t* bd)
522{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400523 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200524 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
Marek Vasut79e5f272013-10-12 20:36:25 +0200525 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400526
John Rigbye9319f12010-10-13 14:31:08 -0600527 /* Initialize MAC address */
528 fec_set_hwaddr(dev);
529
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400530 /*
Marek Vasut79e5f272013-10-12 20:36:25 +0200531 * Setup transmit descriptors, there are two in total.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400532 */
Marek Vasut79e5f272013-10-12 20:36:25 +0200533 fec_tbd_init(fec);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400534
Marek Vasut79e5f272013-10-12 20:36:25 +0200535 /* Setup receive descriptors. */
536 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400537
Marek Vasuta5990b22012-05-01 11:09:41 +0000538 fec_reg_setup(fec);
Marek Vasut9eb37702011-09-11 18:05:31 +0000539
benoit.thebaudeau@advansf41471e2012-07-19 02:12:58 +0000540 if (fec->xcv_type != SEVENWIRE)
Troy Kisky575c5cc2012-10-22 16:40:41 +0000541 fec_mii_setspeed(fec->bus->priv);
Marek Vasut9eb37702011-09-11 18:05:31 +0000542
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400543 /*
544 * Set Opcode/Pause Duration Register
545 */
546 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
547 writel(0x2, &fec->eth->x_wmrk);
548 /*
549 * Set multicast address filter
550 */
551 writel(0x00000000, &fec->eth->gaddr1);
552 writel(0x00000000, &fec->eth->gaddr2);
553
554
Peng Fanfbecbaa2015-08-12 17:46:51 +0800555 /* Do not access reserved register for i.MX6UL */
556 if (!is_cpu_type(MXC_CPU_MX6UL)) {
557 /* clear MIB RAM */
558 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
559 writel(0, i);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400560
Peng Fanfbecbaa2015-08-12 17:46:51 +0800561 /* FIFO receive start register */
562 writel(0x520, &fec->eth->r_fstart);
563 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400564
565 /* size and address of each buffer */
566 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
567 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
568 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
569
Troy Kisky13947f42012-02-07 14:08:47 +0000570#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400571 if (fec->xcv_type != SEVENWIRE)
572 miiphy_restart_aneg(dev);
Troy Kisky13947f42012-02-07 14:08:47 +0000573#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400574 fec_open(dev);
575 return 0;
576}
577
578/**
579 * Halt the FEC engine
580 * @param[in] dev Our device to handle
581 */
582static void fec_halt(struct eth_device *dev)
583{
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200584 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400585 int counter = 0xffff;
586
587 /*
588 * issue graceful stop command to the FEC transmitter if necessary
589 */
John Rigbycb17b922010-01-25 23:12:55 -0700590 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400591 &fec->eth->x_cntrl);
592
593 debug("eth_halt: wait for stop regs\n");
594 /*
595 * wait for graceful stop to register
596 */
597 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbycb17b922010-01-25 23:12:55 -0700598 udelay(1);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400599
600 /*
601 * Disable SmartDMA tasks
602 */
603 fec_tx_task_disable(fec);
604 fec_rx_task_disable(fec);
605
606 /*
607 * Disable the Ethernet Controller
608 * Note: this will also reset the BD index counter!
609 */
John Rigby740d6ae2010-01-25 23:12:57 -0700610 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
611 &fec->eth->ecntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400612 fec->rbd_index = 0;
613 fec->tbd_index = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400614 debug("eth_halt: done\n");
615}
616
617/**
618 * Transmit one frame
619 * @param[in] dev Our ethernet device to handle
620 * @param[in] packet Pointer to the data to be transmitted
621 * @param[in] length Data count in bytes
622 * @return 0 on success
623 */
Joe Hershberger442dac42012-05-21 14:45:27 +0000624static int fec_send(struct eth_device *dev, void *packet, int length)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400625{
626 unsigned int status;
Marek Vasutefe24d22012-08-26 10:19:21 +0000627 uint32_t size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000628 uint32_t addr;
Marek Vasutbc1ce152012-08-29 03:49:49 +0000629 int timeout = FEC_XFER_TIMEOUT;
630 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400631
632 /*
633 * This routine transmits one frame. This routine only accepts
634 * 6-byte Ethernet addresses.
635 */
636 struct fec_priv *fec = (struct fec_priv *)dev->priv;
637
638 /*
639 * Check for valid length of data.
640 */
641 if ((length > 1500) || (length <= 0)) {
Stefano Babic4294b242010-02-01 14:51:30 +0100642 printf("Payload (%d) too large\n", length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400643 return -1;
644 }
645
646 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000647 * Setup the transmit buffer. We are always using the first buffer for
648 * transmission, the second will be empty and only used to stop the DMA
649 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400650 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000651#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000652 swap_packet((uint32_t *)packet, length);
653#endif
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000654
655 addr = (uint32_t)packet;
Marek Vasutefe24d22012-08-26 10:19:21 +0000656 end = roundup(addr + length, ARCH_DMA_MINALIGN);
657 addr &= ~(ARCH_DMA_MINALIGN - 1);
658 flush_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000659
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400660 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000661 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
662
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400663 /*
664 * update BD's status now
665 * This block:
666 * - is always the last in a chain (means no chain)
667 * - should transmitt the CRC
668 * - might be the last BD in the list, so the address counter should
669 * wrap (-> keep the WRAP flag)
670 */
671 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
672 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
673 writew(status, &fec->tbd_base[fec->tbd_index].status);
674
675 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000676 * Flush data cache. This code flushes both TX descriptors to RAM.
677 * After this code, the descriptors will be safely in RAM and we
678 * can start DMA.
679 */
680 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
681 addr = (uint32_t)fec->tbd_base;
682 flush_dcache_range(addr, addr + size);
683
684 /*
Marek Vasutab94cd42013-07-12 01:03:04 +0200685 * Below we read the DMA descriptor's last four bytes back from the
686 * DRAM. This is important in order to make sure that all WRITE
687 * operations on the bus that were triggered by previous cache FLUSH
688 * have completed.
689 *
690 * Otherwise, on MX28, it is possible to observe a corruption of the
691 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
692 * for the bus structure of MX28. The scenario is as follows:
693 *
694 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
695 * to DRAM due to flush_dcache_range()
696 * 2) ARM core writes the FEC registers via AHB_ARB2
697 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
698 *
699 * Note that 2) does sometimes finish before 1) due to reordering of
700 * WRITE accesses on the AHB bus, therefore triggering 3) before the
701 * DMA descriptor is fully written into DRAM. This results in occasional
702 * corruption of the DMA descriptor.
703 */
704 readl(addr + size - 4);
705
706 /*
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400707 * Enable SmartDMA transmit task
708 */
709 fec_tx_task_enable(fec);
710
711 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000712 * Wait until frame is sent. On each turn of the wait cycle, we must
713 * invalidate data cache to see what's really in RAM. Also, we need
714 * barrier here.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400715 */
Marek Vasut67449092012-08-29 03:49:50 +0000716 while (--timeout) {
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000717 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasutbc1ce152012-08-29 03:49:49 +0000718 break;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400719 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000720
Fabio Estevamf5992882014-08-25 13:34:17 -0300721 if (!timeout) {
722 ret = -EINVAL;
723 goto out;
724 }
725
726 /*
727 * The TDAR bit is cleared when the descriptors are all out from TX
728 * but on mx6solox we noticed that the READY bit is still not cleared
729 * right after TDAR.
730 * These are two distinct signals, and in IC simulation, we found that
731 * TDAR always gets cleared prior than the READY bit of last BD becomes
732 * cleared.
733 * In mx6solox, we use a later version of FEC IP. It looks like that
734 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
735 * version.
736 *
737 * Fix this by polling the READY bit of BD after the TDAR polling,
738 * which covers the mx6solox case and does not harm the other SoCs.
739 */
740 timeout = FEC_XFER_TIMEOUT;
741 while (--timeout) {
742 invalidate_dcache_range(addr, addr + size);
743 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
744 FEC_TBD_READY))
745 break;
746 }
747
Marek Vasut67449092012-08-29 03:49:50 +0000748 if (!timeout)
749 ret = -EINVAL;
750
Fabio Estevamf5992882014-08-25 13:34:17 -0300751out:
Marek Vasut67449092012-08-29 03:49:50 +0000752 debug("fec_send: status 0x%x index %d ret %i\n",
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400753 readw(&fec->tbd_base[fec->tbd_index].status),
Marek Vasut67449092012-08-29 03:49:50 +0000754 fec->tbd_index, ret);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400755 /* for next transmission use the other buffer */
756 if (fec->tbd_index)
757 fec->tbd_index = 0;
758 else
759 fec->tbd_index = 1;
760
Marek Vasutbc1ce152012-08-29 03:49:49 +0000761 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400762}
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;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400775 uint16_t bd_status;
Marek Vasutefe24d22012-08-26 10:19:21 +0000776 uint32_t addr, size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000777 int i;
Fabio Estevamfd37f192013-09-17 23:13:10 -0300778 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400779
780 /*
781 * Check if any critical events have happened
782 */
783 ievent = readl(&fec->eth->ievent);
784 writel(ievent, &fec->eth->ievent);
Marek Vasuteda959f2011-10-24 23:40:03 +0000785 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400786 if (ievent & FEC_IEVENT_BABR) {
787 fec_halt(dev);
788 fec_init(dev, fec->bd);
789 printf("some error: 0x%08lx\n", ievent);
790 return 0;
791 }
792 if (ievent & FEC_IEVENT_HBERR) {
793 /* Heartbeat error */
794 writel(0x00000001 | readl(&fec->eth->x_cntrl),
795 &fec->eth->x_cntrl);
796 }
797 if (ievent & FEC_IEVENT_GRA) {
798 /* Graceful stop complete */
799 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
800 fec_halt(dev);
801 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
802 &fec->eth->x_cntrl);
803 fec_init(dev, fec->bd);
804 }
805 }
806
807 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000808 * Read the buffer status. Before the status can be read, the data cache
809 * must be invalidated, because the data in RAM might have been changed
810 * by DMA. The descriptors are properly aligned to cachelines so there's
811 * no need to worry they'd overlap.
812 *
813 * WARNING: By invalidating the descriptor here, we also invalidate
814 * the descriptors surrounding this one. Therefore we can NOT change the
815 * contents of this descriptor nor the surrounding ones. The problem is
816 * that in order to mark the descriptor as processed, we need to change
817 * the descriptor. The solution is to mark the whole cache line when all
818 * descriptors in the cache line are processed.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400819 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000820 addr = (uint32_t)rbd;
821 addr &= ~(ARCH_DMA_MINALIGN - 1);
822 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
823 invalidate_dcache_range(addr, addr + size);
824
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400825 bd_status = readw(&rbd->status);
826 debug("fec_recv: status 0x%x\n", bd_status);
827
828 if (!(bd_status & FEC_RBD_EMPTY)) {
829 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
830 ((readw(&rbd->data_length) - 4) > 14)) {
831 /*
832 * Get buffer address and size
833 */
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200834 addr = readl(&rbd->data_pointer);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400835 frame_length = readw(&rbd->data_length) - 4;
836 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000837 * Invalidate data cache over the buffer
838 */
Marek Vasutefe24d22012-08-26 10:19:21 +0000839 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
840 addr &= ~(ARCH_DMA_MINALIGN - 1);
841 invalidate_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000842
843 /*
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400844 * Fill the buffer and pass it to upper layers
845 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000846#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200847 swap_packet((uint32_t *)addr, frame_length);
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000848#endif
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200849 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500850 net_process_received_packet(buff, frame_length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400851 len = frame_length;
852 } else {
853 if (bd_status & FEC_RBD_ERR)
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200854 printf("error frame: 0x%08x 0x%08x\n",
855 addr, bd_status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400856 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000857
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400858 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000859 * Free the current buffer, restart the engine and move forward
860 * to the next buffer. Here we check if the whole cacheline of
861 * descriptors was already processed and if so, we mark it free
862 * as whole.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400863 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000864 size = RXDESC_PER_CACHELINE - 1;
865 if ((fec->rbd_index & size) == size) {
866 i = fec->rbd_index - size;
867 addr = (uint32_t)&fec->rbd_base[i];
868 for (; i <= fec->rbd_index ; i++) {
869 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
870 &fec->rbd_base[i]);
871 }
872 flush_dcache_range(addr,
873 addr + ARCH_DMA_MINALIGN);
874 }
875
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400876 fec_rx_task_enable(fec);
877 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
878 }
879 debug("fec_recv: stop\n");
880
881 return len;
882}
883
Troy Kiskyef8e3a32012-10-22 16:40:44 +0000884static void fec_set_dev_name(char *dest, int dev_id)
885{
886 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
887}
888
Marek Vasut79e5f272013-10-12 20:36:25 +0200889static int fec_alloc_descs(struct fec_priv *fec)
890{
891 unsigned int size;
892 int i;
893 uint8_t *data;
894
895 /* Allocate TX descriptors. */
896 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
897 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
898 if (!fec->tbd_base)
899 goto err_tx;
900
901 /* Allocate RX descriptors. */
902 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
903 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
904 if (!fec->rbd_base)
905 goto err_rx;
906
907 memset(fec->rbd_base, 0, size);
908
909 /* Allocate RX buffers. */
910
911 /* Maximum RX buffer size. */
Fabio Estevamdb5b7f52014-08-25 13:34:16 -0300912 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut79e5f272013-10-12 20:36:25 +0200913 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevamdb5b7f52014-08-25 13:34:16 -0300914 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut79e5f272013-10-12 20:36:25 +0200915 if (!data) {
916 printf("%s: error allocating rxbuf %d\n", __func__, i);
917 goto err_ring;
918 }
919
920 memset(data, 0, size);
921
922 fec->rbd_base[i].data_pointer = (uint32_t)data;
923 fec->rbd_base[i].status = FEC_RBD_EMPTY;
924 fec->rbd_base[i].data_length = 0;
925 /* Flush the buffer to memory. */
926 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
927 }
928
929 /* Mark the last RBD to close the ring. */
930 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
931
932 fec->rbd_index = 0;
933 fec->tbd_index = 0;
934
935 return 0;
936
937err_ring:
938 for (; i >= 0; i--)
939 free((void *)fec->rbd_base[i].data_pointer);
940 free(fec->rbd_base);
941err_rx:
942 free(fec->tbd_base);
943err_tx:
944 return -ENOMEM;
945}
946
947static void fec_free_descs(struct fec_priv *fec)
948{
949 int i;
950
951 for (i = 0; i < FEC_RBD_NUM; i++)
952 free((void *)fec->rbd_base[i].data_pointer);
953 free(fec->rbd_base);
954 free(fec->tbd_base);
955}
956
Troy Kiskyfe428b92012-10-22 16:40:46 +0000957#ifdef CONFIG_PHYLIB
958int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
959 struct mii_dev *bus, struct phy_device *phydev)
960#else
961static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
962 struct mii_dev *bus, int phy_id)
963#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400964{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400965 struct eth_device *edev;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200966 struct fec_priv *fec;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400967 unsigned char ethaddr[6];
Marek Vasute382fb42011-09-11 18:05:37 +0000968 uint32_t start;
969 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400970
971 /* create and fill edev struct */
972 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
973 if (!edev) {
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200974 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasute382fb42011-09-11 18:05:37 +0000975 ret = -ENOMEM;
976 goto err1;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400977 }
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200978
979 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
980 if (!fec) {
981 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasute382fb42011-09-11 18:05:37 +0000982 ret = -ENOMEM;
983 goto err2;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200984 }
985
Nobuhiro Iwamatsude0b9572010-10-19 14:03:42 +0900986 memset(edev, 0, sizeof(*edev));
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200987 memset(fec, 0, sizeof(*fec));
988
Marek Vasut79e5f272013-10-12 20:36:25 +0200989 ret = fec_alloc_descs(fec);
990 if (ret)
991 goto err3;
992
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400993 edev->priv = fec;
994 edev->init = fec_init;
995 edev->send = fec_send;
996 edev->recv = fec_recv;
997 edev->halt = fec_halt;
Heiko Schocherfb57ec92010-04-27 07:43:52 +0200998 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400999
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001000 fec->eth = (struct ethernet_regs *)base_addr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001001 fec->bd = bd;
1002
Marek Vasut392b8502011-09-11 18:05:33 +00001003 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001004
1005 /* Reset chip. */
John Rigbycb17b922010-01-25 23:12:55 -07001006 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasute382fb42011-09-11 18:05:37 +00001007 start = get_timer(0);
1008 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1009 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1010 printf("FEC MXC: Timeout reseting chip\n");
Marek Vasut79e5f272013-10-12 20:36:25 +02001011 goto err4;
Marek Vasute382fb42011-09-11 18:05:37 +00001012 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001013 udelay(10);
Marek Vasute382fb42011-09-11 18:05:37 +00001014 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001015
Marek Vasuta5990b22012-05-01 11:09:41 +00001016 fec_reg_setup(fec);
Troy Kiskyef8e3a32012-10-22 16:40:44 +00001017 fec_set_dev_name(edev->name, dev_id);
1018 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kisky13947f42012-02-07 14:08:47 +00001019 fec->bus = bus;
Troy Kiskyfe428b92012-10-22 16:40:46 +00001020 fec_mii_setspeed(bus->priv);
1021#ifdef CONFIG_PHYLIB
1022 fec->phydev = phydev;
1023 phy_connect_dev(phydev, edev);
1024 /* Configure phy */
1025 phy_config(phydev);
1026#else
1027 fec->phy_id = phy_id;
1028#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001029 eth_register(edev);
1030
Fabio Estevambe252b62011-12-20 05:46:31 +00001031 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1032 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
Stefano Babic4294b242010-02-01 14:51:30 +01001033 memcpy(edev->enetaddr, ethaddr, 6);
Eric Nelsonddb636b2013-08-02 10:37:00 -07001034 if (!getenv("ethaddr"))
1035 eth_setenv_enetaddr("ethaddr", ethaddr);
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001036 }
Marek Vasute382fb42011-09-11 18:05:37 +00001037 return ret;
Marek Vasut79e5f272013-10-12 20:36:25 +02001038err4:
1039 fec_free_descs(fec);
Marek Vasute382fb42011-09-11 18:05:37 +00001040err3:
1041 free(fec);
1042err2:
1043 free(edev);
1044err1:
1045 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001046}
1047
Troy Kiskyfe428b92012-10-22 16:40:46 +00001048struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
1049{
1050 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1051 struct mii_dev *bus;
1052 int ret;
1053
1054 bus = mdio_alloc();
1055 if (!bus) {
1056 printf("mdio_alloc failed\n");
1057 return NULL;
1058 }
1059 bus->read = fec_phy_read;
1060 bus->write = fec_phy_write;
1061 bus->priv = eth;
1062 fec_set_dev_name(bus->name, dev_id);
1063
1064 ret = mdio_register(bus);
1065 if (ret) {
1066 printf("mdio_register failed\n");
1067 free(bus);
1068 return NULL;
1069 }
1070 fec_mii_setspeed(eth);
1071 return bus;
1072}
1073
Troy Kiskyeef24482012-10-22 16:40:42 +00001074int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1075{
Troy Kiskyfe428b92012-10-22 16:40:46 +00001076 uint32_t base_mii;
1077 struct mii_dev *bus = NULL;
1078#ifdef CONFIG_PHYLIB
1079 struct phy_device *phydev = NULL;
1080#endif
1081 int ret;
1082
1083#ifdef CONFIG_MX28
1084 /*
1085 * The i.MX28 has two ethernet interfaces, but they are not equal.
1086 * Only the first one can access the MDIO bus.
1087 */
1088 base_mii = MXS_ENET0_BASE;
1089#else
1090 base_mii = addr;
1091#endif
Troy Kiskyeef24482012-10-22 16:40:42 +00001092 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001093 bus = fec_get_miibus(base_mii, dev_id);
1094 if (!bus)
1095 return -ENOMEM;
1096#ifdef CONFIG_PHYLIB
1097 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1098 if (!phydev) {
1099 free(bus);
1100 return -ENOMEM;
1101 }
1102 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1103#else
1104 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1105#endif
1106 if (ret) {
1107#ifdef CONFIG_PHYLIB
1108 free(phydev);
1109#endif
1110 free(bus);
1111 }
1112 return ret;
Troy Kiskyeef24482012-10-22 16:40:42 +00001113}
1114
Troy Kisky09439c32012-10-22 16:40:40 +00001115#ifdef CONFIG_FEC_MXC_PHYADDR
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001116int fecmxc_initialize(bd_t *bd)
1117{
Troy Kiskyeef24482012-10-22 16:40:42 +00001118 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1119 IMX_FEC_BASE);
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001120}
1121#endif
1122
Troy Kisky13947f42012-02-07 14:08:47 +00001123#ifndef CONFIG_PHYLIB
Marek Vasut2e5f4422011-09-11 18:05:36 +00001124int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1125{
1126 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1127 fec->mii_postcall = cb;
1128 return 0;
1129}
Troy Kisky13947f42012-02-07 14:08:47 +00001130#endif