blob: 4cefda48e45b533453a1671e1c3618ae6d4ba853 [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>
14#include <miiphy.h>
15#include "fec_mxc.h"
16
17#include <asm/arch/clock.h>
18#include <asm/arch/imx-regs.h>
19#include <asm/io.h>
20#include <asm/errno.h>
Marek Vasute2a66e62012-08-26 10:19:20 +000021#include <linux/compiler.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040022
23DECLARE_GLOBAL_DATA_PTR;
24
Marek Vasutbc1ce152012-08-29 03:49:49 +000025/*
26 * Timeout the transfer after 5 mS. This is usually a bit more, since
27 * the code in the tightloops this timeout is used in adds some overhead.
28 */
29#define FEC_XFER_TIMEOUT 5000
30
Ilya Yanok0b23fb32009-07-21 19:32:21 +040031#ifndef CONFIG_MII
32#error "CONFIG_MII has to be defined!"
33#endif
34
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000035#ifndef CONFIG_FEC_XCV_TYPE
36#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasut392b8502011-09-11 18:05:33 +000037#endif
38
Marek Vasutbe7e87e2011-11-08 23:18:10 +000039/*
40 * The i.MX28 operates with packets in big endian. We need to swap them before
41 * sending and after receiving.
42 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000043#ifdef CONFIG_MX28
44#define CONFIG_FEC_MXC_SWAP_PACKET
45#endif
46
47#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
48
49/* Check various alignment issues at compile time */
50#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
51#error "ARCH_DMA_MINALIGN must be multiple of 16!"
52#endif
53
54#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
55 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
56#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
Marek Vasutbe7e87e2011-11-08 23:18:10 +000057#endif
58
Ilya Yanok0b23fb32009-07-21 19:32:21 +040059#undef DEBUG
60
61struct nbuf {
62 uint8_t data[1500]; /**< actual data */
63 int length; /**< actual length */
64 int used; /**< buffer in use or not */
65 uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
66};
67
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000068#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +000069static void swap_packet(uint32_t *packet, int length)
70{
71 int i;
72
73 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
74 packet[i] = __swab32(packet[i]);
75}
76#endif
77
78/*
Ilya Yanok0b23fb32009-07-21 19:32:21 +040079 * MII-interface related functions
80 */
Troy Kisky13947f42012-02-07 14:08:47 +000081static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
82 uint8_t regAddr)
Ilya Yanok0b23fb32009-07-21 19:32:21 +040083{
Ilya Yanok0b23fb32009-07-21 19:32:21 +040084 uint32_t reg; /* convenient holder for the PHY register */
85 uint32_t phy; /* convenient holder for the PHY */
86 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +000087 int val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +040088
89 /*
90 * reading from any PHY's register is done by properly
91 * programming the FEC's MII data register.
92 */
Marek Vasutd133b882011-09-11 18:05:34 +000093 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +040094 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
95 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
96
97 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutd133b882011-09-11 18:05:34 +000098 phy | reg, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +040099
100 /*
101 * wait for the related interrupt
102 */
Graeme Russa60d1e52011-07-15 23:31:37 +0000103 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000104 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400105 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
106 printf("Read MDIO failed...\n");
107 return -1;
108 }
109 }
110
111 /*
112 * clear mii interrupt bit
113 */
Marek Vasutd133b882011-09-11 18:05:34 +0000114 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400115
116 /*
117 * it's now safe to read the PHY's register
118 */
Troy Kisky13947f42012-02-07 14:08:47 +0000119 val = (unsigned short)readl(&eth->mii_data);
120 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
121 regAddr, val);
122 return val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400123}
124
Troy Kisky575c5cc2012-10-22 16:40:41 +0000125static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic4294b242010-02-01 14:51:30 +0100126{
127 /*
128 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
129 * and do not drop the Preamble.
130 */
Markus Niebel6ba45cc2014-02-05 10:54:11 +0100131 register u32 speed = DIV_ROUND_UP(imx_get_fecclk(), 5000000);
132#ifdef FEC_QUIRK_ENET_MAC
133 speed--;
134#endif
135 speed <<= 1;
136 writel(speed, &eth->mii_speed);
Troy Kisky575c5cc2012-10-22 16:40:41 +0000137 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic4294b242010-02-01 14:51:30 +0100138}
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400139
Troy Kisky13947f42012-02-07 14:08:47 +0000140static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyAddr,
141 uint8_t regAddr, uint16_t data)
142{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400143 uint32_t reg; /* convenient holder for the PHY register */
144 uint32_t phy; /* convenient holder for the PHY */
145 uint32_t start;
146
147 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
148 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
149
150 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutd133b882011-09-11 18:05:34 +0000151 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400152
153 /*
154 * wait for the MII interrupt
155 */
Graeme Russa60d1e52011-07-15 23:31:37 +0000156 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000157 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400158 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
159 printf("Write MDIO failed...\n");
160 return -1;
161 }
162 }
163
164 /*
165 * clear MII interrupt bit
166 */
Marek Vasutd133b882011-09-11 18:05:34 +0000167 writel(FEC_IEVENT_MII, &eth->ievent);
Troy Kisky13947f42012-02-07 14:08:47 +0000168 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400169 regAddr, data);
170
171 return 0;
172}
173
Troy Kisky13947f42012-02-07 14:08:47 +0000174int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
175{
176 return fec_mdio_read(bus->priv, phyAddr, regAddr);
177}
178
179int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
180 u16 data)
181{
182 return fec_mdio_write(bus->priv, phyAddr, regAddr, data);
183}
184
185#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400186static int miiphy_restart_aneg(struct eth_device *dev)
187{
Stefano Babicb774fe92012-02-22 00:24:35 +0000188 int ret = 0;
189#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200190 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000191 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200192
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400193 /*
194 * Wake up from sleep if necessary
195 * Reset PHY, then delay 300ns
196 */
John Rigbycb17b922010-01-25 23:12:55 -0700197#ifdef CONFIG_MX27
Troy Kisky13947f42012-02-07 14:08:47 +0000198 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbycb17b922010-01-25 23:12:55 -0700199#endif
Troy Kisky13947f42012-02-07 14:08:47 +0000200 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400201 udelay(1000);
202
203 /*
204 * Set the auto-negotiation advertisement register bits
205 */
Troy Kisky13947f42012-02-07 14:08:47 +0000206 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500207 LPA_100FULL | LPA_100HALF | LPA_10FULL |
208 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky13947f42012-02-07 14:08:47 +0000209 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500210 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut2e5f4422011-09-11 18:05:36 +0000211
212 if (fec->mii_postcall)
213 ret = fec->mii_postcall(fec->phy_id);
214
Stefano Babicb774fe92012-02-22 00:24:35 +0000215#endif
Marek Vasut2e5f4422011-09-11 18:05:36 +0000216 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400217}
218
219static int miiphy_wait_aneg(struct eth_device *dev)
220{
221 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +0000222 int status;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200223 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000224 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400225
226 /*
227 * Wait for AN completion
228 */
Graeme Russa60d1e52011-07-15 23:31:37 +0000229 start = get_timer(0);
Ilya Yanok0b23fb32009-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 Kisky13947f42012-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 Yanok0b23fb32009-07-21 19:32:21 +0400239 dev->name, status);
240 return -1;
241 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500242 } while (!(status & BMSR_LSTATUS));
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400243
244 return 0;
245}
Troy Kisky13947f42012-02-07 14:08:47 +0000246#endif
247
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400248static int fec_rx_task_enable(struct fec_priv *fec)
249{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000250 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400251 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{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000261 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400262 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 Nelson5c1ad3e2012-03-15 18:33:25 +0000274 * @param[in] dsize desired size of each receive buffer
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400275 * @return 0 on success
276 *
Marek Vasut79e5f272013-10-12 20:36:25 +0200277 * Init all RX descriptors to default values.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400278 */
Marek Vasut79e5f272013-10-12 20:36:25 +0200279static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400280{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000281 uint32_t size;
Marek Vasut79e5f272013-10-12 20:36:25 +0200282 uint8_t *data;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000283 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400284
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400285 /*
Marek Vasut79e5f272013-10-12 20:36:25 +0200286 * Reload the RX descriptors with default values and wipe
287 * the RX buffers.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400288 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000289 size = roundup(dsize, ARCH_DMA_MINALIGN);
290 for (i = 0; i < count; i++) {
Marek Vasut79e5f272013-10-12 20:36:25 +0200291 data = (uint8_t *)fec->rbd_base[i].data_pointer;
292 memset(data, 0, dsize);
293 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
294
295 fec->rbd_base[i].status = FEC_RBD_EMPTY;
296 fec->rbd_base[i].data_length = 0;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000297 }
298
299 /* Mark the last RBD to close the ring. */
Marek Vasut79e5f272013-10-12 20:36:25 +0200300 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400301 fec->rbd_index = 0;
302
Marek Vasut79e5f272013-10-12 20:36:25 +0200303 flush_dcache_range((unsigned)fec->rbd_base,
304 (unsigned)fec->rbd_base + size);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400305}
306
307/**
308 * Initialize transmit task's buffer descriptors
309 * @param[in] fec all we know about the device yet
310 *
311 * Transmit buffers are created externally. We only have to init the BDs here.\n
312 * Note: There is a race condition in the hardware. When only one BD is in
313 * use it must be marked with the WRAP bit to use it for every transmitt.
314 * This bit in combination with the READY bit results into double transmit
315 * of each data buffer. It seems the state machine checks READY earlier then
316 * resetting it after the first transfer.
317 * Using two BDs solves this issue.
318 */
319static void fec_tbd_init(struct fec_priv *fec)
320{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000321 unsigned addr = (unsigned)fec->tbd_base;
322 unsigned size = roundup(2 * sizeof(struct fec_bd),
323 ARCH_DMA_MINALIGN);
Marek Vasut79e5f272013-10-12 20:36:25 +0200324
325 memset(fec->tbd_base, 0, size);
326 fec->tbd_base[0].status = 0;
327 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400328 fec->tbd_index = 0;
Marek Vasut79e5f272013-10-12 20:36:25 +0200329 flush_dcache_range(addr, addr + size);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400330}
331
332/**
333 * Mark the given read buffer descriptor as free
334 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
335 * @param[in] pRbd buffer descriptor to mark free again
336 */
337static void fec_rbd_clean(int last, struct fec_bd *pRbd)
338{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000339 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400340 if (last)
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000341 flags |= FEC_RBD_WRAP;
342 writew(flags, &pRbd->status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400343 writew(0, &pRbd->data_length);
344}
345
Fabio Estevambe252b62011-12-20 05:46:31 +0000346static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
347 unsigned char *mac)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400348{
Fabio Estevambe252b62011-12-20 05:46:31 +0000349 imx_get_mac_from_fuse(dev_id, mac);
Eric Jarrige2e236bf2010-04-16 00:03:19 +0200350 return !is_valid_ether_addr(mac);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400351}
352
Stefano Babic4294b242010-02-01 14:51:30 +0100353static int fec_set_hwaddr(struct eth_device *dev)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400354{
Stefano Babic4294b242010-02-01 14:51:30 +0100355 uchar *mac = dev->enetaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400356 struct fec_priv *fec = (struct fec_priv *)dev->priv;
357
358 writel(0, &fec->eth->iaddr1);
359 writel(0, &fec->eth->iaddr2);
360 writel(0, &fec->eth->gaddr1);
361 writel(0, &fec->eth->gaddr2);
362
363 /*
364 * Set physical address
365 */
366 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
367 &fec->eth->paddr1);
368 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
369
370 return 0;
371}
372
Marek Vasuta5990b22012-05-01 11:09:41 +0000373/*
374 * Do initial configuration of the FEC registers
375 */
376static void fec_reg_setup(struct fec_priv *fec)
377{
378 uint32_t rcntrl;
379
380 /*
381 * Set interrupt mask register
382 */
383 writel(0x00000000, &fec->eth->imask);
384
385 /*
386 * Clear FEC-Lite interrupt event register(IEVENT)
387 */
388 writel(0xffffffff, &fec->eth->ievent);
389
390
391 /*
392 * Set FEC-Lite receive control register(R_CNTRL):
393 */
394
395 /* Start with frame length = 1518, common for all modes. */
396 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advans9d2d9242012-07-19 02:12:46 +0000397 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
398 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
399 if (fec->xcv_type == RGMII)
Marek Vasuta5990b22012-05-01 11:09:41 +0000400 rcntrl |= FEC_RCNTRL_RGMII;
401 else if (fec->xcv_type == RMII)
402 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasuta5990b22012-05-01 11:09:41 +0000403
404 writel(rcntrl, &fec->eth->r_cntrl);
405}
406
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400407/**
408 * Start the FEC engine
409 * @param[in] dev Our device to handle
410 */
411static int fec_open(struct eth_device *edev)
412{
413 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Troy Kisky28774cb2012-02-07 14:08:46 +0000414 int speed;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000415 uint32_t addr, size;
416 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400417
418 debug("fec_open: fec_open(dev)\n");
419 /* full-duplex, heartbeat disabled */
420 writel(1 << 2, &fec->eth->x_cntrl);
421 fec->rbd_index = 0;
422
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000423 /* Invalidate all descriptors */
424 for (i = 0; i < FEC_RBD_NUM - 1; i++)
425 fec_rbd_clean(0, &fec->rbd_base[i]);
426 fec_rbd_clean(1, &fec->rbd_base[i]);
427
428 /* Flush the descriptors into RAM */
429 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
430 ARCH_DMA_MINALIGN);
431 addr = (uint32_t)fec->rbd_base;
432 flush_dcache_range(addr, addr + size);
433
Troy Kisky28774cb2012-02-07 14:08:46 +0000434#ifdef FEC_QUIRK_ENET_MAC
Jason Liu2ef2b952011-12-16 05:17:07 +0000435 /* Enable ENET HW endian SWAP */
436 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
437 &fec->eth->ecntrl);
438 /* Enable ENET store and forward mode */
439 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
440 &fec->eth->x_wmrk);
441#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400442 /*
443 * Enable FEC-Lite controller
444 */
John Rigbycb17b922010-01-25 23:12:55 -0700445 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
446 &fec->eth->ecntrl);
Fabio Estevam7df51fd2013-09-13 00:36:27 -0300447#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby740d6ae2010-01-25 23:12:57 -0700448 udelay(100);
449 /*
450 * setup the MII gasket for RMII mode
451 */
452
453 /* disable the gasket */
454 writew(0, &fec->eth->miigsk_enr);
455
456 /* wait for the gasket to be disabled */
457 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
458 udelay(2);
459
460 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
461 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
462
463 /* re-enable the gasket */
464 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
465
466 /* wait until MII gasket is ready */
467 int max_loops = 10;
468 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
469 if (--max_loops <= 0) {
470 printf("WAIT for MII Gasket ready timed out\n");
471 break;
472 }
473 }
474#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400475
Troy Kisky13947f42012-02-07 14:08:47 +0000476#ifdef CONFIG_PHYLIB
Troy Kisky4dc27ee2012-10-22 16:40:45 +0000477 {
Troy Kisky13947f42012-02-07 14:08:47 +0000478 /* Start up the PHY */
Timur Tabi11af8d62012-07-09 08:52:43 +0000479 int ret = phy_startup(fec->phydev);
480
481 if (ret) {
482 printf("Could not initialize PHY %s\n",
483 fec->phydev->dev->name);
484 return ret;
485 }
Troy Kisky13947f42012-02-07 14:08:47 +0000486 speed = fec->phydev->speed;
Troy Kisky13947f42012-02-07 14:08:47 +0000487 }
488#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400489 miiphy_wait_aneg(edev);
Troy Kisky28774cb2012-02-07 14:08:46 +0000490 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200491 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky13947f42012-02-07 14:08:47 +0000492#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400493
Troy Kisky28774cb2012-02-07 14:08:46 +0000494#ifdef FEC_QUIRK_ENET_MAC
495 {
496 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wangbcb6e902013-05-27 22:55:43 +0000497 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky28774cb2012-02-07 14:08:46 +0000498 if (speed == _1000BASET)
499 ecr |= FEC_ECNTRL_SPEED;
500 else if (speed != _100BASET)
501 rcr |= FEC_RCNTRL_RMII_10T;
502 writel(ecr, &fec->eth->ecntrl);
503 writel(rcr, &fec->eth->r_cntrl);
504 }
505#endif
506 debug("%s:Speed=%i\n", __func__, speed);
507
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400508 /*
509 * Enable SmartDMA receive task
510 */
511 fec_rx_task_enable(fec);
512
513 udelay(100000);
514 return 0;
515}
516
517static int fec_init(struct eth_device *dev, bd_t* bd)
518{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400519 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200520 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
Marek Vasut79e5f272013-10-12 20:36:25 +0200521 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400522
John Rigbye9319f12010-10-13 14:31:08 -0600523 /* Initialize MAC address */
524 fec_set_hwaddr(dev);
525
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400526 /*
Marek Vasut79e5f272013-10-12 20:36:25 +0200527 * Setup transmit descriptors, there are two in total.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400528 */
Marek Vasut79e5f272013-10-12 20:36:25 +0200529 fec_tbd_init(fec);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400530
Marek Vasut79e5f272013-10-12 20:36:25 +0200531 /* Setup receive descriptors. */
532 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400533
Marek Vasuta5990b22012-05-01 11:09:41 +0000534 fec_reg_setup(fec);
Marek Vasut9eb37702011-09-11 18:05:31 +0000535
benoit.thebaudeau@advansf41471e2012-07-19 02:12:58 +0000536 if (fec->xcv_type != SEVENWIRE)
Troy Kisky575c5cc2012-10-22 16:40:41 +0000537 fec_mii_setspeed(fec->bus->priv);
Marek Vasut9eb37702011-09-11 18:05:31 +0000538
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400539 /*
540 * Set Opcode/Pause Duration Register
541 */
542 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
543 writel(0x2, &fec->eth->x_wmrk);
544 /*
545 * Set multicast address filter
546 */
547 writel(0x00000000, &fec->eth->gaddr1);
548 writel(0x00000000, &fec->eth->gaddr2);
549
550
551 /* clear MIB RAM */
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200552 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
553 writel(0, i);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400554
555 /* FIFO receive start register */
556 writel(0x520, &fec->eth->r_fstart);
557
558 /* size and address of each buffer */
559 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
560 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
561 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
562
Troy Kisky13947f42012-02-07 14:08:47 +0000563#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400564 if (fec->xcv_type != SEVENWIRE)
565 miiphy_restart_aneg(dev);
Troy Kisky13947f42012-02-07 14:08:47 +0000566#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400567 fec_open(dev);
568 return 0;
569}
570
571/**
572 * Halt the FEC engine
573 * @param[in] dev Our device to handle
574 */
575static void fec_halt(struct eth_device *dev)
576{
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200577 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400578 int counter = 0xffff;
579
580 /*
581 * issue graceful stop command to the FEC transmitter if necessary
582 */
John Rigbycb17b922010-01-25 23:12:55 -0700583 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400584 &fec->eth->x_cntrl);
585
586 debug("eth_halt: wait for stop regs\n");
587 /*
588 * wait for graceful stop to register
589 */
590 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbycb17b922010-01-25 23:12:55 -0700591 udelay(1);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400592
593 /*
594 * Disable SmartDMA tasks
595 */
596 fec_tx_task_disable(fec);
597 fec_rx_task_disable(fec);
598
599 /*
600 * Disable the Ethernet Controller
601 * Note: this will also reset the BD index counter!
602 */
John Rigby740d6ae2010-01-25 23:12:57 -0700603 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
604 &fec->eth->ecntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400605 fec->rbd_index = 0;
606 fec->tbd_index = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400607 debug("eth_halt: done\n");
608}
609
610/**
611 * Transmit one frame
612 * @param[in] dev Our ethernet device to handle
613 * @param[in] packet Pointer to the data to be transmitted
614 * @param[in] length Data count in bytes
615 * @return 0 on success
616 */
Joe Hershberger442dac42012-05-21 14:45:27 +0000617static int fec_send(struct eth_device *dev, void *packet, int length)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400618{
619 unsigned int status;
Marek Vasutefe24d22012-08-26 10:19:21 +0000620 uint32_t size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000621 uint32_t addr;
Marek Vasutbc1ce152012-08-29 03:49:49 +0000622 int timeout = FEC_XFER_TIMEOUT;
623 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400624
625 /*
626 * This routine transmits one frame. This routine only accepts
627 * 6-byte Ethernet addresses.
628 */
629 struct fec_priv *fec = (struct fec_priv *)dev->priv;
630
631 /*
632 * Check for valid length of data.
633 */
634 if ((length > 1500) || (length <= 0)) {
Stefano Babic4294b242010-02-01 14:51:30 +0100635 printf("Payload (%d) too large\n", length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400636 return -1;
637 }
638
639 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000640 * Setup the transmit buffer. We are always using the first buffer for
641 * transmission, the second will be empty and only used to stop the DMA
642 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400643 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000644#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000645 swap_packet((uint32_t *)packet, length);
646#endif
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000647
648 addr = (uint32_t)packet;
Marek Vasutefe24d22012-08-26 10:19:21 +0000649 end = roundup(addr + length, ARCH_DMA_MINALIGN);
650 addr &= ~(ARCH_DMA_MINALIGN - 1);
651 flush_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000652
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400653 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000654 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
655
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400656 /*
657 * update BD's status now
658 * This block:
659 * - is always the last in a chain (means no chain)
660 * - should transmitt the CRC
661 * - might be the last BD in the list, so the address counter should
662 * wrap (-> keep the WRAP flag)
663 */
664 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
665 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
666 writew(status, &fec->tbd_base[fec->tbd_index].status);
667
668 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000669 * Flush data cache. This code flushes both TX descriptors to RAM.
670 * After this code, the descriptors will be safely in RAM and we
671 * can start DMA.
672 */
673 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
674 addr = (uint32_t)fec->tbd_base;
675 flush_dcache_range(addr, addr + size);
676
677 /*
Marek Vasutab94cd42013-07-12 01:03:04 +0200678 * Below we read the DMA descriptor's last four bytes back from the
679 * DRAM. This is important in order to make sure that all WRITE
680 * operations on the bus that were triggered by previous cache FLUSH
681 * have completed.
682 *
683 * Otherwise, on MX28, it is possible to observe a corruption of the
684 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
685 * for the bus structure of MX28. The scenario is as follows:
686 *
687 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
688 * to DRAM due to flush_dcache_range()
689 * 2) ARM core writes the FEC registers via AHB_ARB2
690 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
691 *
692 * Note that 2) does sometimes finish before 1) due to reordering of
693 * WRITE accesses on the AHB bus, therefore triggering 3) before the
694 * DMA descriptor is fully written into DRAM. This results in occasional
695 * corruption of the DMA descriptor.
696 */
697 readl(addr + size - 4);
698
699 /*
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400700 * Enable SmartDMA transmit task
701 */
702 fec_tx_task_enable(fec);
703
704 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000705 * Wait until frame is sent. On each turn of the wait cycle, we must
706 * invalidate data cache to see what's really in RAM. Also, we need
707 * barrier here.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400708 */
Marek Vasut67449092012-08-29 03:49:50 +0000709 while (--timeout) {
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000710 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasutbc1ce152012-08-29 03:49:49 +0000711 break;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400712 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000713
Marek Vasut67449092012-08-29 03:49:50 +0000714 if (!timeout)
715 ret = -EINVAL;
716
717 invalidate_dcache_range(addr, addr + size);
718 if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
719 ret = -EINVAL;
720
721 debug("fec_send: status 0x%x index %d ret %i\n",
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400722 readw(&fec->tbd_base[fec->tbd_index].status),
Marek Vasut67449092012-08-29 03:49:50 +0000723 fec->tbd_index, ret);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400724 /* for next transmission use the other buffer */
725 if (fec->tbd_index)
726 fec->tbd_index = 0;
727 else
728 fec->tbd_index = 1;
729
Marek Vasutbc1ce152012-08-29 03:49:49 +0000730 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400731}
732
733/**
734 * Pull one frame from the card
735 * @param[in] dev Our ethernet device to handle
736 * @return Length of packet read
737 */
738static int fec_recv(struct eth_device *dev)
739{
740 struct fec_priv *fec = (struct fec_priv *)dev->priv;
741 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
742 unsigned long ievent;
743 int frame_length, len = 0;
744 struct nbuf *frame;
745 uint16_t bd_status;
Marek Vasutefe24d22012-08-26 10:19:21 +0000746 uint32_t addr, size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000747 int i;
Fabio Estevamfd37f192013-09-17 23:13:10 -0300748 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400749
750 /*
751 * Check if any critical events have happened
752 */
753 ievent = readl(&fec->eth->ievent);
754 writel(ievent, &fec->eth->ievent);
Marek Vasuteda959f2011-10-24 23:40:03 +0000755 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400756 if (ievent & FEC_IEVENT_BABR) {
757 fec_halt(dev);
758 fec_init(dev, fec->bd);
759 printf("some error: 0x%08lx\n", ievent);
760 return 0;
761 }
762 if (ievent & FEC_IEVENT_HBERR) {
763 /* Heartbeat error */
764 writel(0x00000001 | readl(&fec->eth->x_cntrl),
765 &fec->eth->x_cntrl);
766 }
767 if (ievent & FEC_IEVENT_GRA) {
768 /* Graceful stop complete */
769 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
770 fec_halt(dev);
771 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
772 &fec->eth->x_cntrl);
773 fec_init(dev, fec->bd);
774 }
775 }
776
777 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000778 * Read the buffer status. Before the status can be read, the data cache
779 * must be invalidated, because the data in RAM might have been changed
780 * by DMA. The descriptors are properly aligned to cachelines so there's
781 * no need to worry they'd overlap.
782 *
783 * WARNING: By invalidating the descriptor here, we also invalidate
784 * the descriptors surrounding this one. Therefore we can NOT change the
785 * contents of this descriptor nor the surrounding ones. The problem is
786 * that in order to mark the descriptor as processed, we need to change
787 * the descriptor. The solution is to mark the whole cache line when all
788 * descriptors in the cache line are processed.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400789 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000790 addr = (uint32_t)rbd;
791 addr &= ~(ARCH_DMA_MINALIGN - 1);
792 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
793 invalidate_dcache_range(addr, addr + size);
794
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400795 bd_status = readw(&rbd->status);
796 debug("fec_recv: status 0x%x\n", bd_status);
797
798 if (!(bd_status & FEC_RBD_EMPTY)) {
799 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
800 ((readw(&rbd->data_length) - 4) > 14)) {
801 /*
802 * Get buffer address and size
803 */
804 frame = (struct nbuf *)readl(&rbd->data_pointer);
805 frame_length = readw(&rbd->data_length) - 4;
806 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000807 * Invalidate data cache over the buffer
808 */
809 addr = (uint32_t)frame;
Marek Vasutefe24d22012-08-26 10:19:21 +0000810 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
811 addr &= ~(ARCH_DMA_MINALIGN - 1);
812 invalidate_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000813
814 /*
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400815 * Fill the buffer and pass it to upper layers
816 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000817#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000818 swap_packet((uint32_t *)frame->data, frame_length);
819#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400820 memcpy(buff, frame->data, frame_length);
821 NetReceive(buff, frame_length);
822 len = frame_length;
823 } else {
824 if (bd_status & FEC_RBD_ERR)
825 printf("error frame: 0x%08lx 0x%08x\n",
826 (ulong)rbd->data_pointer,
827 bd_status);
828 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000829
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400830 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000831 * Free the current buffer, restart the engine and move forward
832 * to the next buffer. Here we check if the whole cacheline of
833 * descriptors was already processed and if so, we mark it free
834 * as whole.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400835 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000836 size = RXDESC_PER_CACHELINE - 1;
837 if ((fec->rbd_index & size) == size) {
838 i = fec->rbd_index - size;
839 addr = (uint32_t)&fec->rbd_base[i];
840 for (; i <= fec->rbd_index ; i++) {
841 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
842 &fec->rbd_base[i]);
843 }
844 flush_dcache_range(addr,
845 addr + ARCH_DMA_MINALIGN);
846 }
847
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400848 fec_rx_task_enable(fec);
849 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
850 }
851 debug("fec_recv: stop\n");
852
853 return len;
854}
855
Troy Kiskyef8e3a32012-10-22 16:40:44 +0000856static void fec_set_dev_name(char *dest, int dev_id)
857{
858 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
859}
860
Marek Vasut79e5f272013-10-12 20:36:25 +0200861static int fec_alloc_descs(struct fec_priv *fec)
862{
863 unsigned int size;
864 int i;
865 uint8_t *data;
866
867 /* Allocate TX descriptors. */
868 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
869 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
870 if (!fec->tbd_base)
871 goto err_tx;
872
873 /* Allocate RX descriptors. */
874 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
875 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
876 if (!fec->rbd_base)
877 goto err_rx;
878
879 memset(fec->rbd_base, 0, size);
880
881 /* Allocate RX buffers. */
882
883 /* Maximum RX buffer size. */
884 size = roundup(FEC_MAX_PKT_SIZE, ARCH_DMA_MINALIGN);
885 for (i = 0; i < FEC_RBD_NUM; i++) {
886 data = memalign(ARCH_DMA_MINALIGN, size);
887 if (!data) {
888 printf("%s: error allocating rxbuf %d\n", __func__, i);
889 goto err_ring;
890 }
891
892 memset(data, 0, size);
893
894 fec->rbd_base[i].data_pointer = (uint32_t)data;
895 fec->rbd_base[i].status = FEC_RBD_EMPTY;
896 fec->rbd_base[i].data_length = 0;
897 /* Flush the buffer to memory. */
898 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
899 }
900
901 /* Mark the last RBD to close the ring. */
902 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
903
904 fec->rbd_index = 0;
905 fec->tbd_index = 0;
906
907 return 0;
908
909err_ring:
910 for (; i >= 0; i--)
911 free((void *)fec->rbd_base[i].data_pointer);
912 free(fec->rbd_base);
913err_rx:
914 free(fec->tbd_base);
915err_tx:
916 return -ENOMEM;
917}
918
919static void fec_free_descs(struct fec_priv *fec)
920{
921 int i;
922
923 for (i = 0; i < FEC_RBD_NUM; i++)
924 free((void *)fec->rbd_base[i].data_pointer);
925 free(fec->rbd_base);
926 free(fec->tbd_base);
927}
928
Troy Kiskyfe428b92012-10-22 16:40:46 +0000929#ifdef CONFIG_PHYLIB
930int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
931 struct mii_dev *bus, struct phy_device *phydev)
932#else
933static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
934 struct mii_dev *bus, int phy_id)
935#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400936{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400937 struct eth_device *edev;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200938 struct fec_priv *fec;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400939 unsigned char ethaddr[6];
Marek Vasute382fb42011-09-11 18:05:37 +0000940 uint32_t start;
941 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400942
943 /* create and fill edev struct */
944 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
945 if (!edev) {
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200946 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasute382fb42011-09-11 18:05:37 +0000947 ret = -ENOMEM;
948 goto err1;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400949 }
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200950
951 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
952 if (!fec) {
953 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasute382fb42011-09-11 18:05:37 +0000954 ret = -ENOMEM;
955 goto err2;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200956 }
957
Nobuhiro Iwamatsude0b9572010-10-19 14:03:42 +0900958 memset(edev, 0, sizeof(*edev));
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200959 memset(fec, 0, sizeof(*fec));
960
Marek Vasut79e5f272013-10-12 20:36:25 +0200961 ret = fec_alloc_descs(fec);
962 if (ret)
963 goto err3;
964
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400965 edev->priv = fec;
966 edev->init = fec_init;
967 edev->send = fec_send;
968 edev->recv = fec_recv;
969 edev->halt = fec_halt;
Heiko Schocherfb57ec92010-04-27 07:43:52 +0200970 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400971
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200972 fec->eth = (struct ethernet_regs *)base_addr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400973 fec->bd = bd;
974
Marek Vasut392b8502011-09-11 18:05:33 +0000975 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400976
977 /* Reset chip. */
John Rigbycb17b922010-01-25 23:12:55 -0700978 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasute382fb42011-09-11 18:05:37 +0000979 start = get_timer(0);
980 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
981 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
982 printf("FEC MXC: Timeout reseting chip\n");
Marek Vasut79e5f272013-10-12 20:36:25 +0200983 goto err4;
Marek Vasute382fb42011-09-11 18:05:37 +0000984 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400985 udelay(10);
Marek Vasute382fb42011-09-11 18:05:37 +0000986 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400987
Marek Vasuta5990b22012-05-01 11:09:41 +0000988 fec_reg_setup(fec);
Troy Kiskyef8e3a32012-10-22 16:40:44 +0000989 fec_set_dev_name(edev->name, dev_id);
990 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kisky13947f42012-02-07 14:08:47 +0000991 fec->bus = bus;
Troy Kiskyfe428b92012-10-22 16:40:46 +0000992 fec_mii_setspeed(bus->priv);
993#ifdef CONFIG_PHYLIB
994 fec->phydev = phydev;
995 phy_connect_dev(phydev, edev);
996 /* Configure phy */
997 phy_config(phydev);
998#else
999 fec->phy_id = phy_id;
1000#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001001 eth_register(edev);
1002
Fabio Estevambe252b62011-12-20 05:46:31 +00001003 if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
1004 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
Stefano Babic4294b242010-02-01 14:51:30 +01001005 memcpy(edev->enetaddr, ethaddr, 6);
Eric Nelsonddb636b2013-08-02 10:37:00 -07001006 if (!getenv("ethaddr"))
1007 eth_setenv_enetaddr("ethaddr", ethaddr);
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001008 }
Marek Vasute382fb42011-09-11 18:05:37 +00001009 return ret;
Marek Vasut79e5f272013-10-12 20:36:25 +02001010err4:
1011 fec_free_descs(fec);
Marek Vasute382fb42011-09-11 18:05:37 +00001012err3:
1013 free(fec);
1014err2:
1015 free(edev);
1016err1:
1017 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001018}
1019
Troy Kiskyfe428b92012-10-22 16:40:46 +00001020struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
1021{
1022 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1023 struct mii_dev *bus;
1024 int ret;
1025
1026 bus = mdio_alloc();
1027 if (!bus) {
1028 printf("mdio_alloc failed\n");
1029 return NULL;
1030 }
1031 bus->read = fec_phy_read;
1032 bus->write = fec_phy_write;
1033 bus->priv = eth;
1034 fec_set_dev_name(bus->name, dev_id);
1035
1036 ret = mdio_register(bus);
1037 if (ret) {
1038 printf("mdio_register failed\n");
1039 free(bus);
1040 return NULL;
1041 }
1042 fec_mii_setspeed(eth);
1043 return bus;
1044}
1045
Troy Kiskyeef24482012-10-22 16:40:42 +00001046int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1047{
Troy Kiskyfe428b92012-10-22 16:40:46 +00001048 uint32_t base_mii;
1049 struct mii_dev *bus = NULL;
1050#ifdef CONFIG_PHYLIB
1051 struct phy_device *phydev = NULL;
1052#endif
1053 int ret;
1054
1055#ifdef CONFIG_MX28
1056 /*
1057 * The i.MX28 has two ethernet interfaces, but they are not equal.
1058 * Only the first one can access the MDIO bus.
1059 */
1060 base_mii = MXS_ENET0_BASE;
1061#else
1062 base_mii = addr;
1063#endif
Troy Kiskyeef24482012-10-22 16:40:42 +00001064 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001065 bus = fec_get_miibus(base_mii, dev_id);
1066 if (!bus)
1067 return -ENOMEM;
1068#ifdef CONFIG_PHYLIB
1069 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1070 if (!phydev) {
1071 free(bus);
1072 return -ENOMEM;
1073 }
1074 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1075#else
1076 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1077#endif
1078 if (ret) {
1079#ifdef CONFIG_PHYLIB
1080 free(phydev);
1081#endif
1082 free(bus);
1083 }
1084 return ret;
Troy Kiskyeef24482012-10-22 16:40:42 +00001085}
1086
Troy Kisky09439c32012-10-22 16:40:40 +00001087#ifdef CONFIG_FEC_MXC_PHYADDR
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001088int fecmxc_initialize(bd_t *bd)
1089{
Troy Kiskyeef24482012-10-22 16:40:42 +00001090 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1091 IMX_FEC_BASE);
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001092}
1093#endif
1094
Troy Kisky13947f42012-02-07 14:08:47 +00001095#ifndef CONFIG_PHYLIB
Marek Vasut2e5f4422011-09-11 18:05:36 +00001096int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1097{
1098 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1099 fec->mii_postcall = cb;
1100 return 0;
1101}
Troy Kisky13947f42012-02-07 14:08:47 +00001102#endif